<?php

declare(strict_types=1);

final class MyronGetPageCategoriesAction implements MyronAction
{
    public function __construct(private readonly MyronPagesActionService $service) {}

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri: '/api/v1/content/pages/{pageId}/categories',
            method: 'GET',
            domain: 'content',
            resource: 'page-categories',
            verb: 'get',
            inputSchema: [
                'type' => 'object',
                'properties' => [
                    'pageId' => ['type' => 'string'],
                ],
                'required' => ['pageId'],
            ],
            outputSchema: ['type' => 'object', 'properties' => ['pageId' => ['type' => 'string'], 'categories' => ['type' => 'array']]],
            requiredScope: MyronApiScopes::CONTENT_READ,
            summary: 'Read the category assignments for a page.',
            preconditions: ['page exists'],
            postconditions: ['returns categories only; no content is changed'],
            audienceClass: 4,
            snapshotInclude: false,
            exposeToMcp: true,
        );
    }

    public function execute(array $input, MyronActionCallerContext $caller): MyronActionResult
    {
        try {
            return MyronActionResult::ok($this->service->getCategories((string) ($input['pageId'] ?? ''), $caller));
        } catch (MyronActionPreconditionError $e) {
            return MyronActionResult::error('precondition_failed', $e->getMessage());
        } catch (InvalidArgumentException $e) {
            return MyronActionResult::error('input_invalid', $e->getMessage());
        } catch (Throwable $e) {
            return MyronActionResult::error('business_rule_failed', $e->getMessage());
        }
    }
}
