<?php

declare(strict_types=1);

/** APO-197 Card 6 — POST /api/v1/administration/ai-connections/{integrationId}/revoke */
final class MyronRevokeAiConnectionAction implements MyronAction
{
    public function __construct(private readonly MyronAiConnectionRevocationService $service) {}
    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri: '/api/v1/administration/ai-connections/{integrationId}/revoke', method: 'POST',
            domain: 'administration', resource: 'ai-connection', verb: 'revoke',
            inputSchema: [
                'type' => 'object',
                'properties' => ['integrationId' => ['type' => 'string']],
                'required' => ['integrationId'],
            ],
            outputSchema: ['type' => 'object', 'properties' => ['revoked' => ['type' => 'boolean']]],
            requiredScope: MyronApiScopes::ADMINISTRATION_DESTRUCTIVE,
            summary: 'Revoke an AI integration. The token immediately stops authorising; integration record preserved for audit.',
            sideEffects: ['sets ai_integrations.revoked_at', 'revokes associated OAuth access and refresh tokens'], audienceClass: 4,
            adminAliases: ['revoke-ai-connection', 'revoke-oauth-connection'],
            snapshotInclude: false, exposeToMcp: false,
        );
    }
    public function execute(array $input, MyronActionCallerContext $caller): MyronActionResult
    {
        try {
            return MyronActionResult::ok($this->service->revoke((string) ($input['integrationId'] ?? '')));
        } catch (Throwable $e) { return MyronActionResult::error('business_rule_failed', $e->getMessage()); }
    }
}
