<?php

declare(strict_types=1);

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

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri: '/api/v1/design/rails/{railId}/default',
            method: 'POST',
            domain: 'design',
            resource: 'rail',
            verb: 'set-default',
            inputSchema: ['type' => 'object', 'properties' => ['railId' => ['type' => 'string']], 'required' => ['railId']],
            outputSchema: ['type' => 'object', 'properties' => ['railId' => ['type' => 'string'], 'previousDefault' => ['type' => 'string']]],
            requiredScope: MyronApiScopes::DESIGN_WRITE,
            summary: 'Set the default reusable Blog rail.',
            sideEffects: ['updates rail_designs.is_default'],
            preconditions: ['rail exists and is activated'],
            postconditions: ['selected rail is the default rail'],
            audienceClass: 4,
            adminAliases: ['set-default-rail'],
            snapshotInclude: false,
            exposeToMcp: true,
        );
    }

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