<?php

declare(strict_types=1);

/**
 * APO-193 Card 3 — Default compatibility document endpoint.
 *
 * URI:    GET /api/v1/system/compatibility/default
 * Scope:  null (public; mirrors the manifest's introspection posture)
 *
 * Returns the per-site-type defaults: what activates out-of-the-box for a
 * fresh install of a given site type. Used by GTM material, site-setup
 * wizards, and AI clients exploring "what does a BIZ install look like?"
 *
 * Cross-reference: full-parity-mcp-2026-05-10.md §12.
 */
final class MyronGetCompatibilityDocumentDefaultAction implements MyronAction
{
    public function __construct(
        private readonly MyronCompatibilityDocumentService $service,
    ) {
    }

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri:           '/api/v1/system/compatibility/default',
            method:        'GET',
            domain:        'system',
            resource:      'compatibility-default',
            verb:          'read',
            inputSchema:   [
                'type'       => 'object',
                'properties' => [
                    'siteType' => ['type' => 'string'],
                ],
                'required'   => ['siteType'],
            ],
            outputSchema:  [
                'type'       => 'object',
                'properties' => [
                    'mode'              => ['type' => 'string'],
                    'siteType'          => ['type' => 'object'],
                    'recommendedBundle' => ['type' => 'object'],
                    // APO-205 Cards 2+3 — footer object (variantId+label) and design surface.
                    'defaultFooter'     => ['type' => 'object'],
                    'design'            => ['type' => 'object'],
                ],
            ],
            requiredScope:    null,
            summary:          'Default compatibility document for a site type — what activates out-of-the-box: site-type metadata (key + label + description), recommended bundle, default footer (object: variantId + label), the design surface (currentDesignSystem, availableArchetypes, storedDefinitions, recommendedArchetypeForActiveBundle), and supported action URIs.',
            sideEffects:      [],
            preconditions:    ['siteType matches a known site type key (BIZ, PORT, BLOG, AGY, ECOM, SAAS, NEWS, EDU, MKT, RST)'],
            postconditions:   [],
            audienceClass:    2,
            adminAliases:     [],
            snapshotInclude:  false,
            snapshotKey:      null,
            exposeToMcp:      true,
        );
    }

    public function execute(array $input, MyronActionCallerContext $caller): MyronActionResult
    {
        $siteType = trim((string) ($input['siteType'] ?? ''));
        if ($siteType === '') {
            return MyronActionResult::error('input_invalid', 'siteType is required.');
        }
        try {
            $doc = $this->service->default($siteType);
            if (isset($doc['error']) && $doc['error'] === 'unknown_site_type') {
                return MyronActionResult::error('precondition_failed', 'Unknown site type: ' . $siteType);
            }
            return MyronActionResult::ok($doc);
        } catch (Throwable $e) {
            return MyronActionResult::error('business_rule_failed', $e->getMessage());
        }
    }
}
