<?php

declare(strict_types=1);

/** APO-195 Card 4 — PATCH /api/v1/content/menus/{menuId}/items/{menuItemId}/position */
final class MyronMoveMenuItemAction implements MyronAction
{
    public function __construct(private readonly MyronMenuActionService $service) {}
    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri: '/api/v1/content/menus/{menuId}/items/{menuItemId}/position', method: 'PATCH',
            domain: 'content', resource: 'menu-item', verb: 'move',
            inputSchema: [
                'type' => 'object',
                'properties' => [
                    'menuId'           => ['type' => 'string'],
                    'menuItemId'       => ['type' => 'string'],
                    'parentMenuItemId' => ['type' => 'string'],
                    'afterMenuItemId'  => ['type' => 'string'],
                ],
                'required' => ['menuId', 'menuItemId'],
            ],
            outputSchema: ['type' => 'object', 'properties' => []],
            requiredScope: MyronApiScopes::CONTENT_WRITE,
            summary: 'Move a menu item (reparent and/or reorder).',
            sideEffects: ['updates menu_items row'], audienceClass: 4,
            adminAliases: ['move-menu-item'], snapshotInclude: false, exposeToMcp: true,
        );
    }
    public function execute(array $input, MyronActionCallerContext $caller): MyronActionResult
    {
        try {
            return MyronActionResult::ok($this->service->moveMenuItem(
                (string) ($input['menuId'] ?? ''),
                (string) ($input['menuItemId'] ?? ''),
                isset($input['parentMenuItemId']) ? (string) $input['parentMenuItemId'] : null,
                isset($input['afterMenuItemId']) ? (string) $input['afterMenuItemId'] : null,
                $caller
            ));
        } catch (InvalidArgumentException $e) { return MyronActionResult::error('input_invalid', $e->getMessage()); }
        catch (Throwable $e) { return MyronActionResult::error('business_rule_failed', $e->getMessage()); }
    }
}
