<?php

declare(strict_types=1);

final class MyronAiConnectionRevocationService
{
    public function __construct(
        private readonly MyronAiIntegrationRepository $integrations,
        private readonly MyronMcpOAuthClientRepository $oauthClients,
    ) {
    }

    /**
     * Revoke one integration identity and every associated OAuth access/refresh token.
     *
     * @return array{integrationId:string,revoked:bool}
     */
    public function revoke(string $integrationId): array
    {
        $integrationId = trim($integrationId);
        if ($integrationId === '') {
            throw new InvalidArgumentException('integrationId is required.');
        }
        if ($this->integrations->findById($integrationId) === null) {
            throw new InvalidArgumentException('AI integration not found: ' . $integrationId);
        }

        $this->integrations->revoke($integrationId);
        $this->oauthClients->revokeIntegrationTokens($integrationId);

        return ['integrationId' => $integrationId, 'revoked' => true];
    }
}
