<?php

declare(strict_types=1);

/**
 * APO-194 Card 4 — Update video asset metadata.
 *
 * URI:   PATCH /api/v1/files/video-assets/{assetId}
 * Scope: content.write
 */
final class MyronUpdateVideoAssetAction implements MyronAction
{
    public function __construct(
        private readonly MyronVideoAssetActionService $service,
    ) {
    }

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri:           '/api/v1/files/video-assets/{assetId}',
            method:        'PATCH',
            domain:        'content',
            resource:      'video-asset',
            verb:          'update',
            inputSchema:   [
                'type'       => 'object',
                'properties' => [
                    'assetId'     => ['type' => 'string'],
                    'title'       => ['type' => 'string'],
                    'description' => ['type' => 'string'],
                    'caption'     => ['type' => 'string'],
                ],
                'required'   => ['assetId'],
            ],
            outputSchema:  ['type' => 'object', 'properties' => ['assetId' => ['type' => 'string']]],
            requiredScope:   MyronApiScopes::CONTENT_WRITE,
            summary:         'Update video asset metadata (title, description, caption).',
            sideEffects:     ['updates video_assets row'],
            preconditions:   ['asset exists'],
            postconditions:  ['metadata reflects input'],
            audienceClass:   4,
            adminAliases:    ['update-video-asset'],
            snapshotInclude: false,
            exposeToMcp:     true,
        );
    }

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