<?php

declare(strict_types=1);

/**
 * APO-193 Card 7 — Create a saved section variant.
 *
 * URI:   POST /api/v1/design/section-variants
 * Scope: design.write
 */
final class MyronCreateSectionVariantAction implements MyronAction
{
    public function __construct(
        private readonly MyronSectionVariantActionService $service,
    ) {
    }

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri:           '/api/v1/design/section-variants',
            method:        'POST',
            domain:        'design',
            resource:      'section-variant',
            verb:          'create',
            inputSchema:   [
                'type'       => 'object',
                'properties' => [
                    'name'        => ['type' => 'string'],
                    'sectionKey'  => ['type' => 'string'],
                    'componentId' => ['type' => 'string'],
                    'variant'     => ['type' => 'string'],
                    'design'      => [
                        'type'       => 'object',
                        'properties' => [
                            'customCss'      => ['type' => 'string'],
                            'tokenOverrides' => ['type' => 'object'],
                            'editableTokens' => ['type' => 'array'],
                            'creative'       => [
                                'type'       => 'object',
                                'nullable'   => true,
                                'properties' => [
                                    'motionProfile'  => ['type' => 'string', 'nullable' => true],
                                    'sceneBackground' => ['type' => 'string', 'nullable' => true],
                                    'params'         => ['type' => 'object'],
                                ],
                            ],
                        ],
                    ],
                    'content'     => ['type' => 'object'],
                ],
                'required'   => ['name', 'componentId'],
            ],
            outputSchema:  [
                'type'       => 'object',
                'properties' => [
                    'id'   => ['type' => 'string'],
                    'name' => ['type' => 'string'],
                ],
            ],
            requiredScope:   MyronApiScopes::DESIGN_WRITE,
            summary:         'Create a saved section variant (operator-curated section configuration applicable across pages).',
            sideEffects:     ['writes section_variants table'],
            preconditions:   ['componentId references a known registered component'],
            postconditions:  ['variant row exists in section_variants'],
            audienceClass:   4,
            adminAliases:    ['create-variant'],
            snapshotInclude: false,
            exposeToMcp:     true,
        );
    }

    public function execute(array $input, MyronActionCallerContext $caller): MyronActionResult
    {
        try {
            $design = is_array($input['design'] ?? null) ? (array) $input['design'] : [];
            $content = is_array($input['content'] ?? null) ? (array) $input['content'] : [];
            return MyronActionResult::ok($this->service->create(
                (string) ($input['name'] ?? ''),
                (string) ($input['sectionKey'] ?? ''),
                (string) ($input['componentId'] ?? ''),
                (string) ($input['variant'] ?? 'default'),
                $design,
                $content,
                $caller
            ));
        } catch (InvalidArgumentException $e) {
            return MyronActionResult::error('input_invalid', $e->getMessage());
        } catch (Throwable $e) {
            return MyronActionResult::error('business_rule_failed', $e->getMessage());
        }
    }
}
