<?php

declare(strict_types=1);

/** APO-349 — GET /api/v1/design/custom-interactions/site */
final class MyronGetCustomInteractionsAction implements MyronAction
{
    private const SNAPSHOT_INLINE_TEXT_BYTES = 48_000;

    public function __construct(private readonly MyronCustomCssRepository $repository)
    {
    }

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri: '/api/v1/design/custom-interactions/site',
            method: 'GET',
            domain: 'design',
            resource: 'custom-interactions',
            verb: 'get-site',
            inputSchema: ['type' => 'object', 'properties' => []],
            outputSchema: [
                'type' => 'object',
                'properties' => [
                    'css' => ['type' => 'string'],
                    'cssInline' => ['type' => 'boolean'],
                    'cssFetch' => ['type' => 'object'],
                    'externalLibraries' => ['type' => 'array'],
                    'uploadedLibraries' => ['type' => 'array'],
                    'customJs' => ['type' => 'string'],
                    'customJsInline' => ['type' => 'boolean'],
                    'customJsFetch' => ['type' => 'object'],
                    'externalFonts' => ['type' => 'array'],
                    'uploadedFonts' => ['type' => 'array'],
                ],
            ],
            requiredScope: MyronApiScopes::DESIGN_READ,
            summary: 'Get site-wide Custom Interactions settings: Custom CSS, JS libraries, custom JS, external font stylesheets, and uploaded fonts.',
            sideEffects: [],
            preconditions: [],
            postconditions: [],
            audienceClass: 3,
            adminAliases: [],
            snapshotInclude: true,
            snapshotKey: 'design.customInteractions',
            exposeToMcp: true,
        );
    }

    public function execute(array $input, MyronActionCallerContext $caller): MyronActionResult
    {
        $state = $this->repository->customInteractions();
        if ($caller->callerType !== 'system' || $caller->callerId !== 'snapshot-composer') {
            return MyronActionResult::ok($state);
        }

        $css = (string) ($state['css'] ?? '');
        if (strlen($css) > self::SNAPSHOT_INLINE_TEXT_BYTES) {
            $state['css'] = '';
            $state['cssInline'] = false;
            $state['cssTruncated'] = true;
            $state['cssBytes'] = strlen($css);
            $state['cssSha256'] = hash('sha256', $css);
            $state['cssFetch'] = [
                'api' => '/api/v1/design/custom-css/site',
                'tool' => 'design_custom_css_get_site',
                'chunkTool' => 'design_custom_css_get_chunk',
            ];
        } else {
            $state['cssInline'] = true;
            $state['cssTruncated'] = false;
            $state['cssBytes'] = strlen($css);
            $state['cssSha256'] = hash('sha256', $css);
        }

        $customJs = (string) ($state['customJs'] ?? '');
        if (strlen($customJs) > self::SNAPSHOT_INLINE_TEXT_BYTES) {
            $state['customJs'] = '';
            $state['customJsInline'] = false;
            $state['customJsTruncated'] = true;
            $state['customJsBytes'] = strlen($customJs);
            $state['customJsSha256'] = hash('sha256', $customJs);
            $state['customJsFetch'] = [
                'api' => '/api/v1/design/custom-interactions/site/custom-js/chunk',
                'tool' => 'design_custom_js_get_chunk',
            ];
        } else {
            $state['customJsInline'] = true;
            $state['customJsTruncated'] = false;
            $state['customJsBytes'] = strlen($customJs);
            $state['customJsSha256'] = hash('sha256', $customJs);
        }

        return MyronActionResult::ok($state);
    }
}
