<?php

declare(strict_types=1);

/**
 * APO-193 Card 9 — Get a single saved section variant.
 *
 * URI:   GET /api/v1/design/section-variants/{variantId}
 * Scope: design.read
 */
final class MyronGetSectionVariantAction implements MyronAction
{
    public function __construct(
        private readonly MyronSectionVariantRepository $variants,
    ) {
    }

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri:           '/api/v1/design/section-variants/{variantId}',
            method:        'GET',
            domain:        'design',
            resource:      'section-variant',
            verb:          'get',
            inputSchema:   [
                'type'       => 'object',
                'properties' => ['variantId' => ['type' => 'string']],
                'required'   => ['variantId'],
            ],
            outputSchema:  ['type' => 'object', 'properties' => []],
            requiredScope:   MyronApiScopes::DESIGN_READ,
            summary:         'Get a single saved section variant by ID.',
            sideEffects:     [],
            preconditions:   ['variant exists'],
            postconditions:  [],
            audienceClass:   3,
            adminAliases:    [],
            snapshotInclude: false,
            exposeToMcp:     true,
        );
    }

    public function execute(array $input, MyronActionCallerContext $caller): MyronActionResult
    {
        $variantId = trim((string) ($input['variantId'] ?? ''));
        if ($variantId === '') {
            return MyronActionResult::error('input_invalid', 'variantId is required.');
        }
        $row = $this->variants->find($variantId);
        if ($row === null) {
            return MyronActionResult::error('precondition_failed', 'Variant not found: ' . $variantId);
        }
        return MyronActionResult::ok((array) $row);
    }
}
