<?php

declare(strict_types=1);

/**
 * APO-196 Card 3 — POST /api/v1/administration/site-setup/reset (exposeToMcp:true, APO-205-007).
 *
 * Destructive but operator-equivalent (same class as content.page.delete which is exposed).
 * Confirmation-phrase input is the in-action safety gate; substrate trust is the scope grant.
 */
final class MyronResetSiteBaselineAction implements MyronAction
{
    public function __construct(private readonly MyronSiteSetupActionService $service) {}
    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri: '/api/v1/administration/site-setup/reset', method: 'POST',
            domain: 'administration', resource: 'site-setup', verb: 'reset',
            inputSchema: [
                'type' => 'object',
                'properties' => ['confirmationPhrase' => ['type' => 'string']],
                'required' => ['confirmationPhrase'],
            ],
            outputSchema: ['type' => 'object', 'properties' => []],
            requiredScope: MyronApiScopes::ADMINISTRATION_DESTRUCTIVE,
            summary: 'Reset the install to baseline (destructive: clears configured site types, removes stored design definitions, deletes uploaded files). Requires confirmationPhrase input as an in-action safety gate.',
            sideEffects: ['truncates configured state; removes design systems; deletes files'],
            preconditions: ['confirmation phrase matches'],
            postconditions: ['install reverted to baseline'],
            audienceClass: 4, adminAliases: ['reset-site-baseline'],
            snapshotInclude: false, exposeToMcp: true,
        );
    }
    public function execute(array $input, MyronActionCallerContext $caller): MyronActionResult
    {
        try { return MyronActionResult::ok($this->service->resetSiteBaseline((string) ($input['confirmationPhrase'] ?? ''), $caller)); }
        catch (MyronActionPreconditionError $e) { return MyronActionResult::error('precondition_failed', $e->getMessage()); }
        catch (InvalidArgumentException $e) { return MyronActionResult::error('input_invalid', $e->getMessage()); }
        catch (Throwable $e) { return MyronActionResult::error('business_rule_failed', $e->getMessage()); }
    }
}
