<?php

declare(strict_types=1);

/**
 * APO-193 Card 9 — Get current layout settings.
 *
 * URI:   GET /api/v1/design/layout
 * Scope: design.read
 */
final class MyronGetLayoutSettingsAction implements MyronAction
{
    /**
     * @param object $registry DesignSystemRegistry instance (JSONM-side).
     */
    public function __construct(
        private readonly object $registry,
    ) {
    }

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri:           '/api/v1/design/layout',
            method:        'GET',
            domain:        'design',
            resource:      'layout',
            verb:          'get',
            inputSchema:   ['type' => 'object', 'properties' => []],
            outputSchema:  ['type' => 'object', 'properties' => []],
            requiredScope:   MyronApiScopes::DESIGN_READ,
            summary:         'Get current layout settings (container widths, gutters, breakpoints) from the install\'s active JSONM design definition. Layout settings are a sub-slice of the design token surface.',
            sideEffects:     [],
            preconditions:   [],
            postconditions:  [],
            audienceClass:   3,
            adminAliases:    [],
            snapshotInclude: true,
            snapshotKey:     'design.layout',
            exposeToMcp:     true,
        );
    }

    public function execute(array $input, MyronActionCallerContext $caller): MyronActionResult
    {
        try {
            if (!method_exists($this->registry, 'currentLayoutTokens')) {
                return MyronActionResult::error('business_rule_failed', 'currentLayoutTokens not available on registry.');
            }
            $layout = (array) $this->registry->currentLayoutTokens();
            return MyronActionResult::ok($layout);
        } catch (Throwable $e) {
            return MyronActionResult::error('business_rule_failed', $e->getMessage());
        }
    }
}
