<?php

declare(strict_types=1);

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

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri: '/api/v1/design/custom-interactions/fonts/files/{fontId}',
            method: 'DELETE',
            domain: 'design',
            resource: 'custom-interactions',
            verb: 'delete-font',
            inputSchema: [
                'type' => 'object',
                'properties' => [
                    'fontId' => ['type' => 'string'],
                ],
                'required' => ['fontId'],
            ],
            outputSchema: [
                'type' => 'object',
                'properties' => [
                    'fontId' => ['type' => 'string'],
                    'deleted' => ['type' => 'boolean'],
                    'storedFilename' => ['type' => 'string'],
                    'fileDeleted' => ['type' => 'boolean'],
                    'fileMissing' => ['type' => 'boolean'],
                    'remainingFonts' => ['type' => 'integer'],
                ],
            ],
            requiredScope: MyronApiScopes::DESIGN_WRITE,
            summary: 'Delete a site-wide Custom Interactions uploaded web font file.',
            sideEffects: ['removes uploaded_font_files metadata entry', 'deletes stored app/uploads/fonts bytes when present'],
            preconditions: ['uploaded font id exists'],
            postconditions: ['uploaded font no longer renders as a Custom Interactions @font-face rule'],
            audienceClass: 4,
            adminAliases: ['delete-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 {
            return MyronActionResult::ok($this->service->deleteInteractionFont(
                (string) ($input['fontId'] ?? ''),
                $caller
            ));
        } catch (InvalidArgumentException $e) {
            return MyronActionResult::error('input_invalid', $e->getMessage());
        } catch (Throwable $e) {
            return MyronActionResult::error('business_rule_failed', $e->getMessage());
        }
    }
}
