<?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 Version20260723100000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Ajoute le second rappel configurable aux invitations d’avis.';
    }

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

        $this->addSql('ALTER TABLE marketing_review_request_configuration ADD second_reminder_enabled BOOLEAN DEFAULT FALSE NOT NULL');
        $this->addSql('ALTER TABLE marketing_review_request_configuration ADD second_reminder_delay_days SMALLINT DEFAULT 7 NOT NULL');

        $this->addSql('ALTER TABLE marketing_review_invitation ADD reminder_token_hash VARCHAR(64) DEFAULT NULL');
        $this->addSql('ALTER TABLE marketing_review_invitation ADD reminder_queued_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL');
        $this->addSql('ALTER TABLE marketing_review_invitation ADD reminder_sent_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL');
        $this->addSql('ALTER TABLE marketing_review_invitation ADD reminder_suppressed_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL');
        $this->addSql("COMMENT ON COLUMN marketing_review_invitation.reminder_queued_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql("COMMENT ON COLUMN marketing_review_invitation.reminder_sent_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql("COMMENT ON COLUMN marketing_review_invitation.reminder_suppressed_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql('CREATE UNIQUE INDEX uniq_marketing_review_invitation_reminder_token_hash ON marketing_review_invitation (reminder_token_hash)');
        $this->addSql('CREATE INDEX marketing_review_invitation_reminder_due_idx ON marketing_review_invitation (status, reminder_sent_at, reminder_suppressed_at, sent_at)');
    }

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

        $this->addSql('DROP INDEX uniq_marketing_review_invitation_reminder_token_hash');
        $this->addSql('DROP INDEX marketing_review_invitation_reminder_due_idx');
        $this->addSql('ALTER TABLE marketing_review_invitation DROP reminder_token_hash');
        $this->addSql('ALTER TABLE marketing_review_invitation DROP reminder_queued_at');
        $this->addSql('ALTER TABLE marketing_review_invitation DROP reminder_sent_at');
        $this->addSql('ALTER TABLE marketing_review_invitation DROP reminder_suppressed_at');

        $this->addSql('ALTER TABLE marketing_review_request_configuration DROP second_reminder_enabled');
        $this->addSql('ALTER TABLE marketing_review_request_configuration DROP second_reminder_delay_days');
    }
}
