<?php

declare(strict_types=1);

/*
 * 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 Version20260723120000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Ajoute l’opposition durable aux futures sollicitations d’avis.';
    }

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

        $this->addSql(<<<'SQL'
            CREATE TABLE marketing_review_solicitation_opt_out (
                id UUID NOT NULL,
                source_invitation_id UUID DEFAULT NULL,
                email_hash VARCHAR(64) NOT 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 UNIQUE INDEX uniq_marketing_review_opt_out_email_hash ON marketing_review_solicitation_opt_out (email_hash)');
        $this->addSql('CREATE INDEX marketing_review_opt_out_source_idx ON marketing_review_solicitation_opt_out (source_invitation_id)');
        $this->addSql("COMMENT ON COLUMN marketing_review_solicitation_opt_out.created_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql("COMMENT ON COLUMN marketing_review_solicitation_opt_out.updated_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql('ALTER TABLE marketing_review_solicitation_opt_out ADD CONSTRAINT fk_marketing_review_opt_out_source FOREIGN KEY (source_invitation_id) REFERENCES marketing_review_invitation (id) ON DELETE SET NULL NOT DEFERRABLE INITIALLY IMMEDIATE');
    }

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

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