<?php

declare(strict_types=1);

final class MicTableReadAction implements MyronAction
{
    public function __construct(private readonly MicTableService $service)
    {
    }

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri: '/api/v1/content/tables/{tableId}',
            method: 'GET',
            domain: 'content.tables',
            resource: 'table',
            verb: 'read',
            inputSchema: ['type' => 'object', 'properties' => ['tableId' => ['type' => 'string']], 'required' => ['tableId']],
            outputSchema: ['type' => 'object', 'properties' => ['table' => ['type' => 'object'], 'referencesCount' => ['type' => 'integer']]],
            requiredScope: MyronApiScopes::CONTENT_READ,
            summary: 'Read one reusable Content Bank table.',
            audienceClass: 3,
            exposeToMcp: true,
        );
    }

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

