<?php

declare(strict_types=1);

/** APO-400 — DELETE /api/v1/design/custom-interactions/libraries/{libraryId} */
final class MyronDeleteCustomInteractionLibraryAction implements MyronAction
{
    public function __construct(private readonly MyronCustomCssActionService $service)
    {
    }

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri: '/api/v1/design/custom-interactions/libraries/{libraryId}',
            method: 'DELETE',
            domain: 'design',
            resource: 'custom-interactions',
            verb: 'delete-library',
            inputSchema: [
                'type' => 'object',
                'properties' => [
                    'libraryId' => ['type' => 'string'],
                ],
                'required' => ['libraryId'],
            ],
            outputSchema: [
                'type' => 'object',
                'properties' => [
                    'libraryId' => ['type' => 'string'],
                    'deleted' => ['type' => 'boolean'],
                    'storedFilename' => ['type' => 'string'],
                    'fileDeleted' => ['type' => 'boolean'],
                    'fileMissing' => ['type' => 'boolean'],
                ],
            ],
            requiredScope: MyronApiScopes::DESIGN_WRITE,
            summary: 'Delete a site-wide Custom Interactions uploaded JavaScript library.',
            sideEffects: ['removes uploaded_js_libraries metadata entry', 'deletes stored app/uploads/interactions bytes when present'],
            preconditions: ['uploaded library id exists'],
            postconditions: ['uploaded library no longer renders as a Custom Interactions script tag'],
            audienceClass: 4,
            adminAliases: ['delete-custom-interaction-library'],
            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->deleteInteractionLibrary(
                (string) ($input['libraryId'] ?? ''),
                $caller
            ));
        } catch (InvalidArgumentException $e) {
            return MyronActionResult::error('input_invalid', $e->getMessage());
        } catch (Throwable $e) {
            return MyronActionResult::error('business_rule_failed', $e->getMessage());
        }
    }
}
