<?php

declare(strict_types=1);

/**
 * APO-193 Card 9 — List registered components.
 *
 * URI:   GET /api/v1/design/components
 * Scope: design.read
 */
final class MyronListComponentsAction implements MyronAction
{
    public function __construct(
        private readonly MyronComponentRegistryRepository $components,
    ) {
    }

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri:           '/api/v1/design/components',
            method:        'GET',
            domain:        'design',
            resource:      'component',
            verb:          'list',
            inputSchema:   [
                'type'       => 'object',
                'properties' => ['classification' => ['type' => 'string']],
            ],
            outputSchema:  [
                'type'       => 'object',
                'properties' => ['items' => ['type' => 'array']],
            ],
            requiredScope:   MyronApiScopes::DESIGN_READ,
            summary:         'List registered live render components and their declared editable tokens, accepted variants, and classification.',
            sideEffects:     [],
            preconditions:   [],
            postconditions:  [],
            audienceClass:   3,
            adminAliases:    [],
            snapshotInclude: true,
            snapshotKey:     'design.components',
            exposeToMcp:     true,
        );
    }

    public function execute(array $input, MyronActionCallerContext $caller): MyronActionResult
    {
        $classification = trim((string) ($input['classification'] ?? ''));
        $items = $classification !== ''
            ? $this->components->listComponents($classification)
            : $this->components->listComponents();
        return MyronActionResult::ok(['items' => $items]);
    }
}
