<?php

declare(strict_types=1);

/**
 * APO-193 Card 3 — Configured compatibility document endpoint.
 *
 * URI:    GET /api/v1/administration/compatibility/configured
 * Scope:  administration.read
 *
 * Returns the LIVE state of the running install — composed at request time
 * from the snapshot service. Includes active site type, active bundle,
 * current default footer/navigation, full manifest summary, and the
 * per-domain snapshot composed from every snapshotInclude:true read action.
 *
 * Cross-reference: full-parity-mcp-2026-05-10.md §12.
 */
final class MyronGetCompatibilityDocumentConfiguredAction implements MyronAction
{
    public function __construct(
        private readonly MyronCompatibilityDocumentService $service,
    ) {
    }

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri:           '/api/v1/administration/compatibility/configured',
            method:        'GET',
            domain:        'administration',
            resource:      'compatibility-configured',
            verb:          'read',
            inputSchema:   [
                'type'       => 'object',
                'properties' => [],
            ],
            outputSchema:  [
                'type'       => 'object',
                'properties' => [
                    'mode'                  => ['type' => 'string'],
                    // APO-205 Cards 2+3 — paired code+label objects, plus design surface.
                    'activeSiteType'        => ['type' => 'object'],
                    'activeBundle'          => ['type' => 'string'],
                    'currentDefaultFooter'  => ['type' => 'object'],
                    'design'                => ['type' => 'object'],
                    'manifest'              => ['type' => 'object'],
                    'snapshot'              => ['type' => 'object'],
                ],
            ],
            requiredScope:    MyronApiScopes::ADMINISTRATION_READ,
            summary:          'Live install state for the running MyronCMS install — composed snapshot of activated configuration, registered action catalog, current default footer (object: variantId + label), active site type (object: code + label + description), and design surface enumeration (currentDesignSystem, availableArchetypes, storedDefinitions, recommendedArchetypeForActiveBundle).',
            sideEffects:      [],
            preconditions:    [],
            postconditions:   [],
            audienceClass:    3,
            adminAliases:     [],
            // NB: snapshotInclude:false on purpose — this action IS the
            // snapshot wrapper; including it as a participant would create a
            // re-entrant compose() call.
            snapshotInclude:  false,
            snapshotKey:      null,
            exposeToMcp:      true,
        );
    }

    public function execute(array $input, MyronActionCallerContext $caller): MyronActionResult
    {
        try {
            return MyronActionResult::ok($this->service->configured());
        } catch (Throwable $e) {
            return MyronActionResult::error('business_rule_failed', $e->getMessage());
        }
    }
}
