<?php

declare(strict_types=1);

/**
 * APO-231 — Create an operator-owned Custom Section Template from a renderable primitive.
 *
 * URI:   POST /api/v1/design/section-templates/custom
 * Scope: design.write
 */
final class MyronCreateCustomSectionTemplateAction implements MyronAction
{
    public function __construct(
        private readonly MyronCustomSectionTemplateActionService $service,
    ) {
    }

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri:           '/api/v1/design/section-templates/custom',
            method:        'POST',
            domain:        'design',
            resource:      'section-template',
            verb:          'create-custom',
            inputSchema:   self::inputSchema(['label', 'primaryComponentId']),
            outputSchema:  ['type' => 'object', 'properties' => ['sectionKey' => ['type' => 'string']]],
            requiredScope: MyronApiScopes::DESIGN_WRITE,
            summary:       'Create a Custom Section Template backed by an existing renderable primitive/component.',
            sideEffects:   ['writes custom_section_templates table'],
            preconditions: ['primaryComponentId references a renderable registered primitive/component'],
            postconditions:['custom section template is available for governed insertion'],
            audienceClass: 4,
            adminAliases:  ['create-custom-section-template'],
            snapshotInclude: false,
            exposeToMcp:   true,
        );
    }

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

    /**
     * @param list<string> $required
     * @return array<string, mixed>
     */
    public static function inputSchema(array $required = []): array
    {
        return [
            'type' => 'object',
            'properties' => [
                'sectionKey' => ['type' => 'string'],
                'label' => ['type' => 'string'],
                'description' => ['type' => 'string'],
                'jtbdGroup' => ['type' => 'string'],
                'sectionType' => ['type' => 'string'],
                'primaryComponentId' => ['type' => 'string'],
                'variant' => ['type' => 'string'],
                'defaultContentEditable' => ['type' => 'boolean'],
                'defaultContent' => ['type' => 'object'],
                'editing' => [
                    'type' => 'object',
                    'properties' => [
                        'fields' => ['type' => 'array'],
                        'repeaters' => ['type' => 'array'],
                    ],
                ],
                'fields' => ['type' => 'array'],
                'repeaters' => ['type' => 'array'],
            ],
            'required' => $required,
        ];
    }
}
