<?php

declare(strict_types=1);

/**
 * APO-195 Card 1 — Update a page.
 *
 * URI:   PATCH /api/v1/content/pages/{pageId}
 * Scope: content.write
 */
final class MyronUpdatePageAction implements MyronAction
{
    public function __construct(private readonly MyronPagesActionService $service) {}

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri: '/api/v1/content/pages/{pageId}', method: 'PATCH',
            domain: 'content', resource: 'page', verb: 'update',
            inputSchema: [
                'type' => 'object',
                'properties' => [
                    'pageId'          => ['type' => 'string'],
                    'mode'            => ['type' => 'string', 'enum' => ['merge', 'replace']],
                    'templateId'      => ['type' => 'string'],
                    'name'            => ['type' => 'string'],
                    'slug'            => ['type' => 'string'],
                    'parentPageId'    => ['type' => 'string'],
                    'seoTitle'        => ['type' => 'string'],
                    'metaDescription' => ['type' => 'string'],
                    'content'         => ['type' => 'object'],
                    'categoryIds'     => ['type' => 'array', 'items' => ['type' => 'string']],
                    'includeInNav'     => ['type' => 'boolean'],
                    'author'          => [
                        'type' => 'object',
                        'properties' => [
                            'authorName' => ['type' => 'string'],
                            'authorUserId' => ['type' => 'string'],
                            'avatarFileId' => ['type' => 'string'],
                            'bio' => ['type' => 'string'],
                        ],
                    ],
                    'publish'         => ['type' => 'boolean'],
                    'publishedAt'     => ['type' => 'string'],
                ],
                'required' => ['pageId'],
            ],
            outputSchema: ['type' => 'object', 'properties' => ['pageId' => ['type' => 'string']]],
            requiredScope: MyronApiScopes::CONTENT_WRITE,
            summary: 'Merge-safe update for an existing page. Omitted fields preserve current state; use mode:replace to intentionally replace supplied content.',
            sideEffects: ['updates pages row', 'inserts new page_versions row'],
            preconditions: ['page exists; template exists'],
            postconditions: ['page state matches input'],
            audienceClass: 4,
            adminAliases: [],
            snapshotInclude: false,
            exposeToMcp: true,
        );
    }

    public function execute(array $input, MyronActionCallerContext $caller): MyronActionResult
    {
        try {
            return MyronActionResult::ok($this->service->updateFromInput($input, $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());
        }
    }
}
