<?php

declare(strict_types=1);

/** APO-401 — POST /api/v1/design/custom-interactions/fonts/files */
final class MyronUploadCustomInteractionFontAction implements MyronAction
{
    public function __construct(private readonly MyronCustomCssActionService $service)
    {
    }

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri: '/api/v1/design/custom-interactions/fonts/files',
            method: 'POST',
            domain: 'design',
            resource: 'custom-interactions',
            verb: 'upload-font',
            inputSchema: [
                'type' => 'object',
                'properties' => [
                    'filename' => ['type' => 'string'],
                    'contentType' => ['type' => 'string'],
                    'contentBase64' => ['type' => 'string'],
                    'sizeBytes' => ['type' => 'integer'],
                    'sha256' => ['type' => 'string'],
                    'label' => ['type' => 'string'],
                    'familyName' => ['type' => 'string'],
                    'weight' => ['type' => 'string'],
                    'style' => ['type' => 'string'],
                    'display' => ['type' => 'string'],
                ],
                'required' => ['filename', 'contentType', 'contentBase64', 'sizeBytes', 'sha256', 'familyName'],
            ],
            outputSchema: [
                'type' => 'object',
                'properties' => [
                    'id' => ['type' => 'string'],
                    'label' => ['type' => 'string'],
                    'familyName' => ['type' => 'string'],
                    'url' => ['type' => 'string'],
                    'format' => ['type' => 'string'],
                    'sha256' => ['type' => 'string'],
                    'sizeBytes' => ['type' => 'integer'],
                ],
            ],
            requiredScope: MyronApiScopes::DESIGN_WRITE,
            summary: 'Upload a site-wide Custom Interactions web font file using the canonical JSON-content envelope.',
            sideEffects: ['writes bytes to app/uploads/fonts', 'updates custom_css_settings uploaded_font_files record'],
            preconditions: ['filename is a supported web font extension', 'sha256 matches decoded bytes', 'payload is 2 MB or smaller'],
            postconditions: ['uploaded font has a deterministic MyronCMS-hosted font URL and @font-face metadata'],
            audienceClass: 4,
            adminAliases: ['upload-custom-interaction-font'],
            snapshotInclude: false,
            exposeToMcp: true,
            mcpAltitude: 'last-resort',
            mcpPreferInstead: ['templates', 'registered sections', 'section variants', 'design tokens'],
        );
    }

    public function execute(array $input, MyronActionCallerContext $caller): MyronActionResult
    {
        try {
            $envelope = [
                'filename' => $input['filename'] ?? '',
                'contentType' => $input['contentType'] ?? '',
                'contentBase64' => $input['contentBase64'] ?? '',
                'sizeBytes' => $input['sizeBytes'] ?? 0,
                'sha256' => $input['sha256'] ?? '',
            ];

            return MyronActionResult::ok($this->service->uploadInteractionFontFromEnvelope(
                $envelope,
                (string) ($input['label'] ?? ''),
                (string) ($input['familyName'] ?? ''),
                (string) ($input['weight'] ?? '400'),
                (string) ($input['style'] ?? 'normal'),
                (string) ($input['display'] ?? 'swap'),
                $caller
            ));
        } catch (MyronJsonContentEnvelopeError $e) {
            return MyronActionResult::error('input_invalid', $e->getMessage());
        } catch (InvalidArgumentException $e) {
            return MyronActionResult::error('input_invalid', $e->getMessage());
        } catch (Throwable $e) {
            return MyronActionResult::error('business_rule_failed', $e->getMessage());
        }
    }
}
