<?php

declare(strict_types=1);

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

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri: '/api/v1/content/tables/{tableId}',
            method: 'PATCH',
            domain: 'content.tables',
            resource: 'table',
            verb: 'update',
            inputSchema: [
                'type' => 'object',
                'properties' => [
                    'tableId' => ['type' => 'string'],
                    'name' => ['type' => 'string'],
                    'description' => ['type' => 'string'],
                    'caption' => ['type' => 'string'],
                    'columns' => ['type' => 'array'],
                    'rows' => ['type' => 'array'],
                    'header_style' => ['type' => 'string'],
                    'responsive_strategy' => ['type' => 'string'],
                ],
                'required' => ['tableId'],
            ],
            outputSchema: ['type' => 'object', 'properties' => ['table' => ['type' => 'object']]],
            requiredScope: MyronApiScopes::CONTENT_WRITE,
            summary: 'Update a reusable Content Bank table.',
            sideEffects: ['updates mic_tables row'],
            audienceClass: 4,
            adminAliases: ['tables.update'],
            exposeToMcp: true,
        );
    }

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

