<?php

declare(strict_types=1);

namespace CpCreation\VitiCore\Bridge\Doctrine\Migrations;

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

final class Version20260519103000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Ajoute les collections de produits manuelles.';
    }

    public function up(Schema $schema): void
    {
        $this->addSql('CREATE TABLE product_collection (id UUID NOT NULL, position INT NOT NULL, disabled BOOLEAN DEFAULT false 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('COMMENT ON COLUMN product_collection.id IS \'(DC2Type:uuid)\'');
        $this->addSql('COMMENT ON COLUMN product_collection.created_at IS \'(DC2Type:datetime_immutable)\'');
        $this->addSql('COMMENT ON COLUMN product_collection.updated_at IS \'(DC2Type:datetime_immutable)\'');

        $this->addSql('CREATE TABLE product_collection_translation (id UUID NOT NULL, translatable_id UUID DEFAULT NULL, name TEXT NOT NULL, description TEXT DEFAULT NULL, slug VARCHAR(255) NOT NULL, locale VARCHAR(5) NOT NULL, PRIMARY KEY(id))');
        $this->addSql('CREATE UNIQUE INDEX UNIQ_56DAB011989D9B62 ON product_collection_translation (slug)');
        $this->addSql('CREATE INDEX IDX_56DAB0112C2AC5D3 ON product_collection_translation (translatable_id)');
        $this->addSql('CREATE UNIQUE INDEX product_collection_translation_unique_translation ON product_collection_translation (translatable_id, locale)');
        $this->addSql('COMMENT ON COLUMN product_collection_translation.id IS \'(DC2Type:uuid)\'');
        $this->addSql('COMMENT ON COLUMN product_collection_translation.translatable_id IS \'(DC2Type:uuid)\'');

        $this->addSql('CREATE SEQUENCE product_collection_item_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
        $this->addSql('CREATE TABLE product_collection_item (id INT NOT NULL, product_collection_id UUID NOT NULL, product_id UUID NOT NULL, position INT DEFAULT 0, created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, updated_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, PRIMARY KEY(id))');
        $this->addSql('CREATE INDEX IDX_54F7E05EFEF59FE3 ON product_collection_item (product_collection_id)');
        $this->addSql('CREATE INDEX IDX_54F7E05E4584665A ON product_collection_item (product_id)');
        $this->addSql('COMMENT ON COLUMN product_collection_item.product_collection_id IS \'(DC2Type:uuid)\'');
        $this->addSql('COMMENT ON COLUMN product_collection_item.product_id IS \'(DC2Type:uuid)\'');
        $this->addSql('COMMENT ON COLUMN product_collection_item.created_at IS \'(DC2Type:datetime_immutable)\'');
        $this->addSql('COMMENT ON COLUMN product_collection_item.updated_at IS \'(DC2Type:datetime_immutable)\'');
        $this->addSql('ALTER TABLE product_collection_item ALTER COLUMN id SET DEFAULT NEXTVAL(\'product_collection_item_id_seq\')');
        $this->addSql('ALTER SEQUENCE product_collection_item_id_seq OWNED BY product_collection_item.id');

        $this->addSql('ALTER TABLE product_collection_translation ADD CONSTRAINT FK_56DAB0112C2AC5D3 FOREIGN KEY (translatable_id) REFERENCES product_collection (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
        $this->addSql('ALTER TABLE product_collection_item ADD CONSTRAINT FK_54F7E05EFEF59FE3 FOREIGN KEY (product_collection_id) REFERENCES product_collection (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
        $this->addSql('ALTER TABLE product_collection_item ADD CONSTRAINT FK_54F7E05E4584665A FOREIGN KEY (product_id) REFERENCES product_product (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
    }

    public function down(Schema $schema): void
    {
        $this->addSql('ALTER TABLE product_collection_translation DROP CONSTRAINT FK_56DAB0112C2AC5D3');
        $this->addSql('ALTER TABLE product_collection_item DROP CONSTRAINT FK_54F7E05EFEF59FE3');
        $this->addSql('ALTER TABLE product_collection_item DROP CONSTRAINT FK_54F7E05E4584665A');
        $this->addSql('DROP TABLE product_collection');
        $this->addSql('DROP TABLE product_collection_translation');
        $this->addSql('DROP TABLE product_collection_item');
        $this->addSql('DROP SEQUENCE product_collection_item_id_seq CASCADE');
    }
}
