<?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 Version20260722230000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Ajoute la configuration persistée et sécurisée du catalogue Commerce central.';
    }

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

        $this->addSql("CREATE TABLE commerce_catalog_configuration (id VARCHAR(32) NOT NULL, enabled BOOLEAN DEFAULT false NOT NULL, hub_url TEXT DEFAULT NULL, instance_id UUID NOT NULL, currency VARCHAR(3) DEFAULT 'EUR' NOT NULL, auth_audience VARCHAR(255) DEFAULT 'viticore:catalog' NOT NULL, enrollment_token_ciphertext TEXT DEFAULT NULL, enrollment_token_expires_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, enrollment_token_jti VARCHAR(255) DEFAULT NULL, webhook_secret_ciphertext TEXT DEFAULT NULL, jwt_public_key_pem TEXT DEFAULT NULL, jwt_issuer VARCHAR(255) DEFAULT NULL, jwt_key_id VARCHAR(128) DEFAULT NULL, source_id VARCHAR(255) DEFAULT NULL, webhook_url TEXT DEFAULT NULL, enrolled_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, status VARCHAR(32) DEFAULT 'disabled' NOT NULL, last_error TEXT DEFAULT NULL, authorization_state_hash VARCHAR(64) DEFAULT NULL, authorization_pkce_verifier_ciphertext TEXT DEFAULT NULL, authorization_expires_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, lock_version INT DEFAULT 1 NOT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, updated_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, PRIMARY KEY(id))");
        $this->addSql('CREATE UNIQUE INDEX UNIQ_BD9764E13A51721D ON commerce_catalog_configuration (instance_id)');
        $this->addSql("COMMENT ON COLUMN commerce_catalog_configuration.enrollment_token_expires_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql("COMMENT ON COLUMN commerce_catalog_configuration.enrolled_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql("COMMENT ON COLUMN commerce_catalog_configuration.authorization_expires_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql("COMMENT ON COLUMN commerce_catalog_configuration.created_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql("COMMENT ON COLUMN commerce_catalog_configuration.updated_at IS '(DC2Type:datetime_immutable)'");
    }

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

        $this->addSql('DROP TABLE commerce_catalog_configuration');
    }
}
