<?php

declare(strict_types=1);

/** APO-194 Card 7 — GET /api/v1/files/{fileId} */
final class MyronGetFileAction implements MyronAction
{
    public function __construct(private readonly MyronFileRepository $repo) {}

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri: '/api/v1/files/{fileId}', method: 'GET',
            domain: 'content', resource: 'file', verb: 'get',
            inputSchema: ['type' => 'object', 'properties' => ['fileId' => ['type' => 'string']], 'required' => ['fileId']],
            outputSchema: ['type' => 'object', 'properties' => []],
            requiredScope: MyronApiScopes::CONTENT_READ,
            summary: 'Get a single file by ID. Response includes a "variants" array describing alternative formats (e.g. WebP sidecars) available via the same file_url when the client sends a matching Accept header. Each variant entry has {format, mime, size, url, accept_header}; an empty array means only the canonical original is served.',
            audienceClass: 3,
            snapshotInclude: false,
            exposeToMcp: true,
        );
    }

    public function execute(array $input, MyronActionCallerContext $caller): MyronActionResult
    {
        $id = trim((string) ($input['fileId'] ?? ''));
        if ($id === '') return MyronActionResult::error('input_invalid', 'fileId is required.');
        $row = $this->repo->getFileById($id);
        if (!is_array($row)) return MyronActionResult::error('precondition_failed', 'File not found.');
        return MyronActionResult::ok($row);
    }
}
