<?php

declare(strict_types=1);

/**
 * APO-194 Card 4 — Create a video asset (initial rendition) via JSON-content envelope.
 *
 * URI:   POST /api/v1/files/video-assets
 * Scope: content.write
 */
final class MyronCreateVideoAssetAction implements MyronAction
{
    public function __construct(
        private readonly MyronVideoAssetActionService $service,
    ) {
    }

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri:           '/api/v1/files/video-assets',
            method:        'POST',
            domain:        'content',
            resource:      'video-asset',
            verb:          'create',
            inputSchema:   [
                'type'       => 'object',
                'properties' => [
                    'filename'      => ['type' => 'string'],
                    'contentType'   => ['type' => 'string'],
                    'contentBase64' => ['type' => 'string'],
                    'sizeBytes'     => ['type' => 'integer'],
                    'sha256'        => ['type' => 'string'],
                    'qualityLabel'  => ['type' => 'string'],
                    'metadata'      => ['type' => 'object'],
                ],
                'required'   => ['filename', 'contentType', 'contentBase64', 'sizeBytes', 'sha256'],
            ],
            outputSchema:  ['type' => 'object', 'properties' => ['id' => ['type' => 'string']]],
            requiredScope:   MyronApiScopes::CONTENT_WRITE,
            summary:         'Create a video asset by uploading an initial rendition via JSON-content envelope.',
            sideEffects:     ['uploads file', 'inserts video_assets row binding asset to file'],
            preconditions:   ['envelope decodes cleanly', 'video MIME type'],
            postconditions:  ['video asset row exists; initial rendition bound'],
            audienceClass:   4,
            adminAliases:    ['create-video-asset'],
            snapshotInclude: false,
            exposeToMcp:     true,
        );
    }

    public function execute(array $input, MyronActionCallerContext $caller): MyronActionResult
    {
        try {
            $envelope = [
                'filename'      => $input['filename']      ?? '',
                'contentType'   => $input['contentType']   ?? '',
                'contentBase64' => $input['contentBase64'] ?? '',
                'sizeBytes'     => $input['sizeBytes']     ?? 0,
                'sha256'        => $input['sha256']        ?? '',
            ];
            $metadata = is_array($input['metadata'] ?? null) ? (array) $input['metadata'] : [];
            return MyronActionResult::ok($this->service->createFromEnvelope(
                $envelope,
                $metadata,
                (string) ($input['qualityLabel'] ?? '1080p'),
                $caller
            ));
        } catch (MyronJsonContentEnvelopeError $e) {
            return MyronActionResult::error('input_invalid', $e->getMessage());
        } catch (InvalidArgumentException $e) {
            return MyronActionResult::error('input_invalid', $e->getMessage());
        } catch (Throwable $e) {
            return MyronActionResult::error('business_rule_failed', $e->getMessage());
        }
    }
}
