<?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 Version20260629100000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Ajoute les collections de variants produit.';
    }

    public function up(Schema $schema): void
    {
        $this->addSql(<<<'SQL'
CREATE TABLE IF NOT EXISTS product_variant_collection (
    id UUID NOT NULL,
    name VARCHAR(255) NOT NULL,
    description TEXT DEFAULT NULL,
    created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL,
    updated_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL,
    PRIMARY KEY(id)
)
SQL);
        $this->addSql(<<<'SQL'
CREATE TABLE IF NOT EXISTS product_variant_collection_variant (
    id UUID NOT NULL,
    collection_id UUID NOT NULL,
    variant_id UUID NOT NULL,
    position INT DEFAULT NULL,
    created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL,
    updated_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL,
    PRIMARY KEY(id)
)
SQL);
        $this->addSql('CREATE INDEX IF NOT EXISTS IDX_580F5E7F514956FD ON product_variant_collection_variant (collection_id)');
        $this->addSql('CREATE INDEX IF NOT EXISTS IDX_580F5E7F3B69A9AF ON product_variant_collection_variant (variant_id)');
        $this->addSql('CREATE UNIQUE INDEX IF NOT EXISTS uniq_product_variant_collection_variant ON product_variant_collection_variant (collection_id, variant_id)');
        $this->addSql('ALTER TABLE product_variant_collection_variant ADD CONSTRAINT FK_580F5E7F514956FD FOREIGN KEY (collection_id) REFERENCES product_variant_collection (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
        $this->addSql('ALTER TABLE product_variant_collection_variant ADD CONSTRAINT FK_580F5E7F3B69A9AF FOREIGN KEY (variant_id) REFERENCES product_variant (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
        $this->addSql(<<<'SQL'
CREATE TABLE IF NOT EXISTS product_variant_collection_audit (
    id SERIAL NOT NULL,
    type VARCHAR(10) NOT NULL,
    object_id VARCHAR(255) NOT NULL,
    discriminator VARCHAR(255) DEFAULT NULL,
    transaction_hash VARCHAR(40) DEFAULT NULL,
    diffs JSON DEFAULT NULL,
    blame_id VARCHAR(255) DEFAULT NULL,
    blame_user VARCHAR(255) DEFAULT NULL,
    blame_user_fqdn VARCHAR(255) DEFAULT NULL,
    blame_user_firewall VARCHAR(100) DEFAULT NULL,
    ip VARCHAR(45) DEFAULT NULL,
    created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL,
    PRIMARY KEY(id)
)
SQL);
        $this->addSql('CREATE INDEX IF NOT EXISTS type_'.md5('product_variant_collection_audit').'_idx ON product_variant_collection_audit (type)');
        $this->addSql('CREATE INDEX IF NOT EXISTS object_id_'.md5('product_variant_collection_audit').'_idx ON product_variant_collection_audit (object_id)');
        $this->addSql('CREATE INDEX IF NOT EXISTS discriminator_'.md5('product_variant_collection_audit').'_idx ON product_variant_collection_audit (discriminator)');
        $this->addSql('CREATE INDEX IF NOT EXISTS transaction_hash_'.md5('product_variant_collection_audit').'_idx ON product_variant_collection_audit (transaction_hash)');
        $this->addSql('CREATE INDEX IF NOT EXISTS blame_id_'.md5('product_variant_collection_audit').'_idx ON product_variant_collection_audit (blame_id)');
        $this->addSql('CREATE INDEX IF NOT EXISTS created_at_'.md5('product_variant_collection_audit').'_idx ON product_variant_collection_audit (created_at)');
        $this->addSql("COMMENT ON COLUMN product_variant_collection_audit.created_at IS '(DC2Type:datetime_immutable)'");
    }

    public function down(Schema $schema): void
    {
        $this->addSql('DROP TABLE IF EXISTS product_variant_collection_variant');
        $this->addSql('DROP TABLE IF EXISTS product_variant_collection_audit');
        $this->addSql('DROP SEQUENCE IF EXISTS product_variant_collection_audit_id_seq CASCADE');
        $this->addSql('DROP TABLE IF EXISTS product_variant_collection');
    }
}
