<?php

declare(strict_types=1);

final class MyronUpdateVerificationSettingsAction implements MyronAction
{
    public function __construct(private readonly MyronVerificationSettingsRepository $repo)
    {
    }

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri: '/api/v1/administration/verification-settings',
            method: 'PATCH',
            domain: 'administration',
            resource: 'verification-settings',
            verb: 'update',
            inputSchema: [
                'type' => 'object',
                'properties' => [
                    'settings' => ['type' => 'object'],
                ],
                'required' => ['settings'],
            ],
            outputSchema: ['type' => 'object'],
            requiredScope: MyronApiScopes::ADMINISTRATION_WRITE,
            summary: 'Update form protection verification settings.',
            sideEffects: ['updates site_settings verification columns'],
            preconditions: ['caller holds administration.write'],
            postconditions: ['settings persisted'],
            audienceClass: 4,
            adminAliases: ['save-verification-settings'],
            exposeToMcp: true,
        );
    }

    public function execute(array $input, MyronActionCallerContext $caller): MyronActionResult
    {
        try {
            $settings = is_array($input['settings'] ?? null) ? (array) $input['settings'] : [];
            return MyronActionResult::ok(['settings' => $this->repo->saveSettings($settings)]);
        } catch (Throwable $e) {
            return MyronActionResult::error('business_rule_failed', $e->getMessage());
        }
    }
}

