<?php

declare(strict_types=1);

namespace CpCreation\VitiCore\Bridge\Doctrine\Migrations;

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

final class Version20260415113000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Ajoute la relation d incompatibilite entre promos automatiques.';
    }

    public function up(Schema $schema): void
    {
        $this->abortIf(
            'postgresql' !== $this->connection->getDatabasePlatform()->getName(),
            'Cette migration ne supporte que PostgreSQL.'
        );

        $this->addSql('CREATE TABLE cart_promo_incompatible_auto (promo_source_id UUID NOT NULL, promo_target_id UUID NOT NULL, PRIMARY KEY(promo_source_id, promo_target_id))');
        $this->addSql('CREATE INDEX IDX_PROMO_INCOMPATIBLE_AUTO_SOURCE ON cart_promo_incompatible_auto (promo_source_id)');
        $this->addSql('CREATE INDEX IDX_PROMO_INCOMPATIBLE_AUTO_TARGET ON cart_promo_incompatible_auto (promo_target_id)');
        $this->addSql('ALTER TABLE cart_promo_incompatible_auto ADD CONSTRAINT FK_PROMO_INCOMPATIBLE_AUTO_SOURCE FOREIGN KEY (promo_source_id) REFERENCES cart_promo (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
        $this->addSql('ALTER TABLE cart_promo_incompatible_auto ADD CONSTRAINT FK_PROMO_INCOMPATIBLE_AUTO_TARGET FOREIGN KEY (promo_target_id) REFERENCES cart_promo (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
        $this->addSql('ALTER TABLE cart_promo_incompatible_auto ADD CONSTRAINT CHK_PROMO_INCOMPATIBLE_AUTO_SELF CHECK (promo_source_id <> promo_target_id)');
    }

    public function down(Schema $schema): void
    {
        $this->abortIf(
            'postgresql' !== $this->connection->getDatabasePlatform()->getName(),
            'Cette migration ne supporte que PostgreSQL.'
        );

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