<?php

declare(strict_types=1);

final class MyronDeleteAudioTranscriptAction implements MyronAction
{
    public function __construct(private readonly MyronMediaAccessibilityActionService $service)
    {
    }

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri: '/api/v1/files/audio/{fileId}/transcript',
            method: 'DELETE',
            domain: 'content',
            resource: 'audio-transcript',
            verb: 'delete',
            inputSchema: ['type' => 'object', 'properties' => ['fileId' => ['type' => 'string']], 'required' => ['fileId']],
            outputSchema: ['type' => 'object', 'properties' => ['fileId' => ['type' => 'string'], 'deleted' => ['type' => 'boolean']]],
            requiredScope: MyronApiScopes::CONTENT_WRITE,
            summary: 'Delete a transcript from an audio asset.',
            sideEffects: ['deletes media_accessibility_attachments row', 'deletes unreferenced transcript file'],
            preconditions: ['fileId is provided'],
            postconditions: ['audio transcript attachment is removed'],
            audienceClass: 4,
            adminAliases: ['delete-audio-transcript'],
            snapshotInclude: false,
            exposeToMcp: true,
        );
    }

    public function execute(array $input, MyronActionCallerContext $caller): MyronActionResult
    {
        try {
            return MyronActionResult::ok($this->service->deleteAudioTranscript((string) ($input['fileId'] ?? ''), $caller));
        } catch (InvalidArgumentException $e) {
            return MyronActionResult::error('input_invalid', $e->getMessage());
        } catch (Throwable $e) {
            return MyronActionResult::error('business_rule_failed', $e->getMessage());
        }
    }
}
