<?php

/*
 * 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 Version20260826200000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Trace les imports uniques de commandes depuis VitiCore master MySQL.';
    }

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

        $this->addSql(<<<'SQL'
            CREATE TABLE cart_legacy_order_import_run (
                id UUID NOT NULL,
                source_key VARCHAR(64) NOT NULL,
                source_label VARCHAR(255) NOT NULL,
                scope VARCHAR(16) NOT NULL,
                status VARCHAR(16) NOT NULL,
                attempt_count INT DEFAULT 1 NOT NULL,
                source_count INT DEFAULT 0 NOT NULL,
                imported_count INT DEFAULT 0 NOT NULL,
                skipped_count INT DEFAULT 0 NOT NULL,
                total_ttc BIGINT DEFAULT 0 NOT NULL,
                report JSONB NOT NULL,
                started_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL,
                completed_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL,
                PRIMARY KEY(id)
            )
            SQL);
        $this->addSql('CREATE UNIQUE INDEX uniq_legacy_order_import_run_source ON cart_legacy_order_import_run (source_key)');
        $this->addSql(<<<'SQL'
            CREATE TABLE cart_legacy_order_import_map (
                id UUID NOT NULL,
                run_id UUID NOT NULL,
                source_key VARCHAR(64) NOT NULL,
                source_checkout_id VARCHAR(36) NOT NULL,
                source_cart_id VARCHAR(36) NOT NULL,
                target_cart_id UUID NOT NULL,
                source_checksum VARCHAR(64) NOT NULL,
                imported_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL,
                PRIMARY KEY(id)
            )
            SQL);
        $this->addSql('CREATE INDEX idx_legacy_order_import_map_run ON cart_legacy_order_import_map (run_id)');
        $this->addSql('CREATE UNIQUE INDEX uniq_legacy_order_import_map_cart ON cart_legacy_order_import_map (source_key, source_cart_id)');
        $this->addSql('CREATE UNIQUE INDEX uniq_legacy_order_import_map_checkout ON cart_legacy_order_import_map (source_key, source_checkout_id)');
        $this->addSql('CREATE UNIQUE INDEX uniq_legacy_order_import_map_target ON cart_legacy_order_import_map (target_cart_id)');
        $this->addSql(<<<'SQL'
            ALTER TABLE cart_legacy_order_import_map
                ADD CONSTRAINT fk_legacy_order_import_map_run
                FOREIGN KEY (run_id) REFERENCES cart_legacy_order_import_run (id)
                ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE
            SQL);
        $this->addSql(<<<'SQL'
            ALTER TABLE cart_legacy_order_import_map
                ADD CONSTRAINT fk_legacy_order_import_map_cart
                FOREIGN KEY (target_cart_id) REFERENCES cart_cart (id)
                NOT DEFERRABLE INITIALLY IMMEDIATE
            SQL);
    }

    public function down(Schema $schema): void
    {
        $this->abortIf(!$this->connection->getDatabasePlatform() instanceof PostgreSQLPlatform, 'Cette migration nécessite PostgreSQL.');
        $this->abortIf(
            (int) $this->connection->fetchOne('SELECT COUNT(*) FROM cart_legacy_order_import_map') > 0,
            'Impossible de supprimer la tracabilite : des commandes historiques ont deja ete importees. Restaurez une sauvegarde pour annuler cet import.',
        );

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