<?php

declare(strict_types=1);

/**
 * APO-194 Card 4 — Delete a video asset (cascade-removes all renditions + bytes).
 *
 * URI:   DELETE /api/v1/files/video-assets/{assetId}
 * Scope: content.write
 */
final class MyronDeleteVideoAssetAction implements MyronAction
{
    public function __construct(
        private readonly MyronVideoAssetActionService $service,
    ) {
    }

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri:           '/api/v1/files/video-assets/{assetId}',
            method:        'DELETE',
            domain:        'content',
            resource:      'video-asset',
            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 a video asset including all renditions and their file bytes.',
            sideEffects:     ['deletes video_assets row; deletes all rendition file rows + bytes'],
            preconditions:   ['asset exists; not in use by a published page or section variant'],
            postconditions:  ['asset row and rendition rows removed; bytes purged'],
            audienceClass:   4,
            adminAliases:    ['delete-video-asset'],
            snapshotInclude: false,
            exposeToMcp:     true,
        );
    }

    public function execute(array $input, MyronActionCallerContext $caller): MyronActionResult
    {
        try {
            return MyronActionResult::ok($this->service->delete(
                (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());
        }
    }
}
