<?php

declare(strict_types=1);

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

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

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