<?php

declare(strict_types=1);

/** APO-195 Card 2 — PATCH .../sections/{position}/design (update-section-design) */
final class MyronUpdateSectionDesignAction implements MyronAction
{
    public function __construct(private readonly MyronSectionEditActionService $service) {}

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri: '/api/v1/content/templates/{sourceTemplateId}/sections/{position}/design', method: 'PATCH',
            domain: 'content', resource: 'template-section', verb: 'update-design',
            inputSchema: [
                'type' => 'object',
                'properties' => [
                    'sourceTemplateId' => ['type' => 'string'],
                    'position'         => ['type' => 'integer'],
                    'design'           => [
                        'type' => 'object',
                        'properties' => [
                            'customCss' => ['type' => 'string'],
                            'tokenOverrides' => ['type' => 'object'],
                            'mediaLayout' => [
                                'type' => 'string',
                                'enum' => ['auto', 'split', 'full-bleed', 'full-width'],
                                'description' => 'Hero-only image fetch/layout policy. Auto preserves variant-derived behavior.',
                            ],
                        ],
                    ],
                ],
                'required' => ['sourceTemplateId', 'position', 'design'],
            ],
            outputSchema: ['type' => 'object', 'properties' => ['position' => ['type' => 'integer']]],
            requiredScope: MyronApiScopes::CONTENT_WRITE,
            summary: 'Update section design overrides (customCss, tokenOverrides, and hero mediaLayout) on the runtime template draft.',
            sideEffects: ['updates runtime template schema sections[position] design fields'],
            preconditions: ['runtime draft exists', 'position is valid'],
            postconditions: ['section design overrides reflect input'],
            audienceClass: 4, adminAliases: ['update-section-design'],
            snapshotInclude: false, exposeToMcp: true,
        );
    }

    public function execute(array $input, MyronActionCallerContext $caller): MyronActionResult
    {
        try {
            $design = is_array($input['design'] ?? null) ? (array) $input['design'] : [];
            return MyronActionResult::ok($this->service->updateSectionDesign(
                (string) ($input['sourceTemplateId'] ?? ''),
                (int) ($input['position'] ?? -1),
                $design,
                $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()); }
    }
}
