<?php

declare(strict_types=1);

/**
 * APO-231 — Read an operator-owned Custom Section Template.
 *
 * URI:   GET /api/v1/design/section-templates/custom/{sectionKey}
 * Scope: design.read
 */
final class MyronGetCustomSectionTemplateAction implements MyronAction
{
    public function __construct(
        private readonly MyronCustomSectionTemplateActionService $service,
    ) {
    }

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri:           '/api/v1/design/section-templates/custom/{sectionKey}',
            method:        'GET',
            domain:        'design',
            resource:      'section-template',
            verb:          'get-custom',
            inputSchema:   [
                'type' => 'object',
                'properties' => ['sectionKey' => ['type' => 'string']],
                'required' => ['sectionKey'],
            ],
            outputSchema:  ['type' => 'object', 'properties' => ['sectionKey' => ['type' => 'string']]],
            requiredScope: MyronApiScopes::DESIGN_READ,
            summary:       'Read a Custom Section Template and usage metadata.',
            sideEffects:   [],
            preconditions: ['sectionKey references an existing custom section template'],
            postconditions:[],
            audienceClass: 3,
            adminAliases:  [],
            snapshotInclude: false,
            exposeToMcp:   true,
        );
    }

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