<?php

declare(strict_types=1);

/**
 * APO-194 Card 6 — Reset design system to baseline.
 *
 * URI:   POST /api/v1/design/definitions/reset-baseline
 * Scope: administration.write  · exposeToMcp:TRUE (APO-205-006)
 */
final class MyronResetBaselineDefinitionAction implements MyronAction
{
    public function __construct(
        private readonly MyronDefinitionsActionService $service,
    ) {
    }

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri:           '/api/v1/design/definitions/reset-baseline',
            method:        'POST',
            domain:        'design',
            resource:      'definition',
            verb:          'reset-baseline',
            inputSchema:   ['type' => 'object', 'properties' => []],
            outputSchema:  ['type' => 'object', 'properties' => ['id' => ['type' => 'string'], 'name' => ['type' => 'string']]],
            requiredScope:   MyronApiScopes::DESIGN_WRITE,
            summary:         'Reset the install to the baseline JSONM design definition (destructive: clears active token customisations and reverts to the packaged baseline).',
            sideEffects:     ['updates active JSONM design definition to baseline; recompiles runtime design tokens'],
            preconditions:   [],
            postconditions:  ['active definition is baseline'],
            audienceClass:   2,
            adminAliases:    ['reset-baseline'],
            snapshotInclude: false,
            exposeToMcp:     true,
        );
    }

    public function execute(array $input, MyronActionCallerContext $caller): MyronActionResult
    {
        try {
            return MyronActionResult::ok($this->service->resetBaseline($caller));
        } catch (MyronActionPreconditionError $e) {
            return MyronActionResult::error('precondition_failed', $e->getMessage());
        } catch (Throwable $e) {
            return MyronActionResult::error('business_rule_failed', $e->getMessage());
        }
    }
}
