<?php

declare(strict_types=1);

/**
 * APO-193 Card 9 — Get a single component by ID.
 *
 * URI:   GET /api/v1/design/components/{componentId}
 * Scope: design.read
 */
final class MyronGetComponentAction implements MyronAction
{
    public function __construct(
        private readonly MyronComponentRegistryRepository $components,
    ) {
    }

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri:           '/api/v1/design/components/{componentId}',
            method:        'GET',
            domain:        'design',
            resource:      'component',
            verb:          'get',
            inputSchema:   [
                'type'       => 'object',
                'properties' => ['componentId' => ['type' => 'string']],
                'required'   => ['componentId'],
            ],
            outputSchema:  ['type' => 'object', 'properties' => []],
            requiredScope:   MyronApiScopes::DESIGN_READ,
            summary:         'Get a single registered component by ID — schema, editable tokens, accepted variants, classification.',
            sideEffects:     [],
            preconditions:   ['component exists'],
            postconditions:  [],
            audienceClass:   3,
            adminAliases:    [],
            snapshotInclude: false,
            exposeToMcp:     true,
        );
    }

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