<?php

declare(strict_types=1);

final class MyronGetPageContractAction implements MyronAction
{
    public function __construct(private readonly MyronPageContractService $service) {}

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri: '/api/v1/content/pages/{pageId}/contract',
            method: 'GET',
            domain: 'content',
            resource: 'page-contract',
            verb: 'get',
            inputSchema: [
                'type' => 'object',
                'properties' => [
                    'pageId' => ['type' => 'string'],
                ],
                'required' => ['pageId'],
            ],
            outputSchema: ['type' => 'object', 'properties' => []],
            requiredScope: MyronApiScopes::CONTENT_READ,
            summary: 'Get the deterministic Page Contract for a page: sections, editable fields, current values, constraints, and valid section update targets.',
            preconditions: ['page exists'],
            postconditions: ['returns contract 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->contractForPage((string) ($input['pageId'] ?? '')));
        } 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());
        }
    }
}
