<?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\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20260706100000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Ajoute le mode automatique aux collections de variants.';
    }

    public function up(Schema $schema): void
    {
        $this->addSql("ALTER TABLE product_variant_collection ADD COLUMN IF NOT EXISTS mode VARCHAR(20) DEFAULT 'manual' NOT NULL");
        $this->addSql('ALTER TABLE product_variant_collection ADD COLUMN IF NOT EXISTS automatic_category_id UUID DEFAULT NULL');
        $this->addSql('ALTER TABLE product_variant_collection ADD COLUMN IF NOT EXISTS automatic_type_id UUID DEFAULT NULL');
        $this->addSql('ALTER TABLE product_variant_collection ADD COLUMN IF NOT EXISTS automatic_only_cuvee BOOLEAN DEFAULT true NOT NULL');
        $this->addSql('ALTER TABLE product_variant_collection ADD COLUMN IF NOT EXISTS automatic_only_new BOOLEAN DEFAULT false NOT NULL');
        $this->addSql('ALTER TABLE product_variant_collection ADD COLUMN IF NOT EXISTS automatic_exclude_sold_out BOOLEAN DEFAULT false NOT NULL');
        $this->addSql('ALTER TABLE product_variant_collection ADD COLUMN IF NOT EXISTS automatic_limit INT DEFAULT NULL');
        $this->addSql('CREATE INDEX IF NOT EXISTS IDX_PRODUCT_VARIANT_COLLECTION_AUTOMATIC_CATEGORY ON product_variant_collection (automatic_category_id)');
        $this->addSql('CREATE INDEX IF NOT EXISTS IDX_PRODUCT_VARIANT_COLLECTION_AUTOMATIC_TYPE ON product_variant_collection (automatic_type_id)');
        $this->addSql(<<<'SQL'
DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM pg_constraint WHERE conname = 'FK_PRODUCT_VARIANT_COLLECTION_AUTOMATIC_CATEGORY'
    ) THEN
        ALTER TABLE product_variant_collection
            ADD CONSTRAINT FK_PRODUCT_VARIANT_COLLECTION_AUTOMATIC_CATEGORY
            FOREIGN KEY (automatic_category_id)
            REFERENCES product_category (id)
            ON DELETE SET NULL
            NOT DEFERRABLE INITIALLY IMMEDIATE;
    END IF;

    IF NOT EXISTS (
        SELECT 1 FROM pg_constraint WHERE conname = 'FK_PRODUCT_VARIANT_COLLECTION_AUTOMATIC_TYPE'
    ) THEN
        ALTER TABLE product_variant_collection
            ADD CONSTRAINT FK_PRODUCT_VARIANT_COLLECTION_AUTOMATIC_TYPE
            FOREIGN KEY (automatic_type_id)
            REFERENCES product_type (id)
            ON DELETE SET NULL
            NOT DEFERRABLE INITIALLY IMMEDIATE;
    END IF;
END $$;
SQL);
    }

    public function down(Schema $schema): void
    {
        $this->addSql('ALTER TABLE product_variant_collection DROP CONSTRAINT IF EXISTS FK_PRODUCT_VARIANT_COLLECTION_AUTOMATIC_CATEGORY');
        $this->addSql('ALTER TABLE product_variant_collection DROP CONSTRAINT IF EXISTS FK_PRODUCT_VARIANT_COLLECTION_AUTOMATIC_TYPE');
        $this->addSql('DROP INDEX IF EXISTS IDX_PRODUCT_VARIANT_COLLECTION_AUTOMATIC_CATEGORY');
        $this->addSql('DROP INDEX IF EXISTS IDX_PRODUCT_VARIANT_COLLECTION_AUTOMATIC_TYPE');
        $this->addSql('ALTER TABLE product_variant_collection DROP COLUMN IF EXISTS mode');
        $this->addSql('ALTER TABLE product_variant_collection DROP COLUMN IF EXISTS automatic_category_id');
        $this->addSql('ALTER TABLE product_variant_collection DROP COLUMN IF EXISTS automatic_type_id');
        $this->addSql('ALTER TABLE product_variant_collection DROP COLUMN IF EXISTS automatic_only_cuvee');
        $this->addSql('ALTER TABLE product_variant_collection DROP COLUMN IF EXISTS automatic_only_new');
        $this->addSql('ALTER TABLE product_variant_collection DROP COLUMN IF EXISTS automatic_exclude_sold_out');
        $this->addSql('ALTER TABLE product_variant_collection DROP COLUMN IF EXISTS automatic_limit');
    }
}
