<?php

declare(strict_types=1);

final class MyronUpdateRailDefaultsAction implements MyronAction
{
    public function __construct(private readonly MyronRailDesignActionService $service)
    {
    }

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri: '/api/v1/design/rails/{railId}/defaults',
            method: 'PATCH',
            domain: 'design',
            resource: 'rail',
            verb: 'update-defaults',
            inputSchema: [
                'type' => 'object',
                'properties' => [
                    'railId' => ['type' => 'string'],
                    'name' => ['type' => 'string'],
                    'placement_default' => ['type' => 'string'],
                    'content' => ['type' => 'object'],
                ],
                'required' => ['railId'],
            ],
            outputSchema: ['type' => 'object'],
            requiredScope: MyronApiScopes::DESIGN_WRITE,
            summary: 'Update reusable Blog rail defaults and slot labels.',
            sideEffects: ['updates rail_designs configuration'],
            preconditions: ['rail exists'],
            postconditions: ['rail configuration reflects input'],
            audienceClass: 4,
            adminAliases: ['update-rail-defaults'],
            snapshotInclude: false,
            exposeToMcp: true,
        );
    }

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