<?php

declare(strict_types=1);

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

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri:           '/api/v1/files/video-assets/{assetId}/renditions',
            method:        'POST',
            domain:        'content',
            resource:      'video-rendition',
            verb:          'add',
            inputSchema:   [
                'type'       => 'object',
                'properties' => [
                    'assetId'       => ['type' => 'string'],
                    'filename'      => ['type' => 'string'],
                    'contentType'   => ['type' => 'string'],
                    'contentBase64' => ['type' => 'string'],
                    'sizeBytes'     => ['type' => 'integer'],
                    'sha256'        => ['type' => 'string'],
                    'qualityLabel'  => ['type' => 'string'],
                ],
                'required'   => ['assetId', 'filename', 'contentType', 'contentBase64', 'sizeBytes', 'sha256'],
            ],
            outputSchema:  [
                'type'       => 'object',
                'properties' => [
                    'assetId' => ['type' => 'string'],
                    'fileId'  => ['type' => 'string'],
                    'quality' => ['type' => 'string'],
                ],
            ],
            requiredScope:   MyronApiScopes::CONTENT_WRITE,
            summary:         'Upload an additional rendition (e.g. 720p) for an existing video asset via JSON-content envelope.',
            sideEffects:     ['uploads file; inserts video_asset_renditions row'],
            preconditions:   ['asset exists', 'envelope decodes cleanly'],
            postconditions:  ['rendition bound to asset'],
            audienceClass:   4,
            adminAliases:    ['add-video-rendition'],
            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']        ?? '',
            ];
            return MyronActionResult::ok($this->service->addRenditionFromEnvelope(
                (string) ($input['assetId'] ?? ''),
                $envelope,
                (string) ($input['qualityLabel'] ?? '720p'),
                $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());
        }
    }
}
