<?php

declare(strict_types=1);

/** APO-195 Card 4 — POST /api/v1/content/menus/{menuId}/items */
final class MyronAddPageToMenuAction implements MyronAction
{
    public function __construct(private readonly MyronMenuActionService $service) {}
    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri: '/api/v1/content/menus/{menuId}/items', method: 'POST',
            domain: 'content', resource: 'menu-item', verb: 'add',
            inputSchema: [
                'type' => 'object',
                'properties' => ['menuId' => ['type' => 'string'], 'pageId' => ['type' => 'string'], 'label' => ['type' => 'string']],
                'required' => ['menuId', 'pageId'],
            ],
            outputSchema: ['type' => 'object', 'properties' => []],
            requiredScope: MyronApiScopes::CONTENT_WRITE,
            summary: 'Add a page reference to a menu with an optional override label.',
            sideEffects: ['inserts menu_items row'], audienceClass: 4,
            adminAliases: ['add-to-menu'], snapshotInclude: false, exposeToMcp: true,
        );
    }
    public function execute(array $input, MyronActionCallerContext $caller): MyronActionResult
    {
        try {
            return MyronActionResult::ok($this->service->addPageToMenu(
                (string) ($input['menuId'] ?? ''),
                (string) ($input['pageId'] ?? ''),
                (string) ($input['label'] ?? ''),
                $caller
            ));
        } catch (InvalidArgumentException $e) { return MyronActionResult::error('input_invalid', $e->getMessage()); }
        catch (Throwable $e) { return MyronActionResult::error('business_rule_failed', $e->getMessage()); }
    }
}
