<?php

declare(strict_types=1);

/*
 * This file is part of the VITICORE2 package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\Bridge\Doctrine\Migrations;

use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20260722232000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Renomme le protocole Commerce VitiCore en Serenity et force une nouvelle autorisation.';
    }

    public function up(Schema $schema): void
    {
        $this->abortIf(!$this->connection->getDatabasePlatform() instanceof PostgreSQLPlatform, 'Cette migration nécessite PostgreSQL.');

        $this->addSql("ALTER TABLE commerce_catalog_configuration ALTER auth_audience SET DEFAULT 'serenity:catalog'");
        $this->resetConnections('viticore:catalog', 'serenity:catalog', 'viticore-commerce-hub', '/webhooks/viticore/');
    }

    public function down(Schema $schema): void
    {
        $this->abortIf(!$this->connection->getDatabasePlatform() instanceof PostgreSQLPlatform, 'Cette migration nécessite PostgreSQL.');

        $this->addSql("ALTER TABLE commerce_catalog_configuration ALTER auth_audience SET DEFAULT 'viticore:catalog'");
        $this->resetConnections('serenity:catalog', 'viticore:catalog', 'serenity-commerce-hub', '/webhooks/serenity/');
    }

    private function resetConnections(
        string $previousAudience,
        string $nextAudience,
        string $previousIssuer,
        string $previousWebhookPath,
    ): void {
        $this->addSql(
            <<<'SQL'
                UPDATE commerce_catalog_configuration
                SET auth_audience = ?,
                    enrollment_token_ciphertext = NULL,
                    enrollment_token_expires_at = NULL,
                    enrollment_token_jti = NULL,
                    webhook_secret_ciphertext = NULL,
                    jwt_public_key_pem = NULL,
                    jwt_issuer = NULL,
                    jwt_key_id = NULL,
                    source_id = NULL,
                    webhook_url = NULL,
                    enrolled_at = NULL,
                    status = CASE WHEN enabled THEN 'pending_authorization' ELSE 'disabled' END,
                    last_error = NULL,
                    authorization_state_hash = NULL,
                    authorization_pkce_verifier_ciphertext = NULL,
                    authorization_expires_at = NULL,
                    lock_version = lock_version + 1,
                    updated_at = NOW()
                WHERE auth_audience = ?
                   OR jwt_issuer = ?
                   OR webhook_url LIKE ?
                SQL,
            [
                $nextAudience,
                $previousAudience,
                $previousIssuer,
                '%' . $previousWebhookPath . '%',
            ],
        );
    }
}
