<?php

declare(strict_types=1);

/**
 * APO-193 Card 5 — Clear ALL token overrides.
 *
 * URI:   DELETE /api/v1/design/tokens/patches
 * Scope: design.write
 */
final class MyronClearAllTokenPatchesAction implements MyronAction
{
    public function __construct(
        private readonly MyronTokenPatchActionService $service,
    ) {
    }

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri:           '/api/v1/design/tokens/patches',
            method:        'DELETE',
            domain:        'design',
            resource:      'token-patch',
            verb:          'clear-all',
            inputSchema:   [
                'type'       => 'object',
                'properties' => [],
            ],
            outputSchema:  [
                'type'       => 'object',
                'properties' => ['cleared' => ['type' => 'integer']],
            ],
            requiredScope:   MyronApiScopes::DESIGN_WRITE,
            summary:         'Clear ALL token overrides. The install reverts to canonical token values across all surfaces.',
            sideEffects:     ['truncates token_patches', 'recompiles runtime overrides'],
            preconditions:   [],
            postconditions:  ['token_patches is empty'],
            audienceClass:   4,
            adminAliases:    ['clear-token-patches'],
            snapshotInclude: false,
            exposeToMcp:     true,
        );
    }

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