<?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 Version20260723130000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Ajoute les campagnes de relance de panier abandonné et leurs coupons conditionnels.';
    }

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

        $this->addSql('ALTER TABLE cart_promo ADD restricted_country_codes JSON DEFAULT NULL');

        $this->addSql(<<<'SQL'
            CREATE TABLE marketing_abandoned_cart_configuration (
                id UUID NOT NULL,
                enabled BOOLEAN DEFAULT FALSE NOT NULL,
                enabled_at TIMESTAMP(0) WITHOUT TIME ZONE 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("COMMENT ON COLUMN marketing_abandoned_cart_configuration.enabled_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql("COMMENT ON COLUMN marketing_abandoned_cart_configuration.created_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql("COMMENT ON COLUMN marketing_abandoned_cart_configuration.updated_at IS '(DC2Type:datetime_immutable)'");

        $this->addSql(<<<'SQL'
            CREATE TABLE marketing_abandoned_cart_rule (
                id UUID NOT NULL,
                configuration_id UUID NOT NULL,
                name VARCHAR(120) NOT NULL,
                enabled BOOLEAN DEFAULT TRUE NOT NULL,
                position SMALLINT DEFAULT 0 NOT NULL,
                delay_hours SMALLINT DEFAULT 24 NOT NULL,
                min_subtotal_ttc INT DEFAULT NULL,
                max_subtotal_ttc INT DEFAULT NULL,
                country_codes JSON DEFAULT '[]' NOT NULL,
                customer_type VARCHAR(16) DEFAULT 'any' NOT NULL,
                discount_type VARCHAR(24) DEFAULT 'none' NOT NULL,
                discount_amount_ttc INT DEFAULT NULL,
                discount_percent NUMERIC(5, 2) DEFAULT NULL,
                validity_days SMALLINT DEFAULT 7 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 INDEX IDX_3511F13573F32DD8 ON marketing_abandoned_cart_rule (configuration_id)');
        $this->addSql('CREATE INDEX marketing_abandoned_cart_rule_dispatch_idx ON marketing_abandoned_cart_rule (enabled, position)');
        $this->addSql("COMMENT ON COLUMN marketing_abandoned_cart_rule.created_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql("COMMENT ON COLUMN marketing_abandoned_cart_rule.updated_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql('ALTER TABLE marketing_abandoned_cart_rule ADD CONSTRAINT FK_MARKETING_ABANDONED_CART_RULE_CONFIGURATION FOREIGN KEY (configuration_id) REFERENCES marketing_abandoned_cart_configuration (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');

        $this->addSql(<<<'SQL'
            CREATE TABLE marketing_abandoned_cart_reminder (
                id UUID NOT NULL,
                cart_id UUID NOT NULL,
                rule_id UUID NOT NULL,
                promo_id UUID DEFAULT NULL,
                recipient_email VARCHAR(255) NOT NULL,
                token_hash VARCHAR(64) NOT NULL,
                status VARCHAR(24) NOT NULL,
                scheduled_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL,
                expires_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL,
                sent_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL,
                recovered_at TIMESTAMP(0) WITHOUT TIME ZONE 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 UNIQUE INDEX uniq_marketing_abandoned_cart_reminder_cart_rule ON marketing_abandoned_cart_reminder (cart_id, rule_id)');
        $this->addSql('CREATE UNIQUE INDEX uniq_marketing_abandoned_cart_reminder_token_hash ON marketing_abandoned_cart_reminder (token_hash)');
        $this->addSql('CREATE UNIQUE INDEX UNIQ_C09E70FD0C07AFF ON marketing_abandoned_cart_reminder (promo_id)');
        $this->addSql('CREATE INDEX IDX_C09E70F744E0351 ON marketing_abandoned_cart_reminder (rule_id)');
        $this->addSql('CREATE INDEX marketing_abandoned_cart_reminder_status_idx ON marketing_abandoned_cart_reminder (status)');
        $this->addSql("COMMENT ON COLUMN marketing_abandoned_cart_reminder.scheduled_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql("COMMENT ON COLUMN marketing_abandoned_cart_reminder.expires_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql("COMMENT ON COLUMN marketing_abandoned_cart_reminder.sent_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql("COMMENT ON COLUMN marketing_abandoned_cart_reminder.recovered_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql("COMMENT ON COLUMN marketing_abandoned_cart_reminder.created_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql("COMMENT ON COLUMN marketing_abandoned_cart_reminder.updated_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql('ALTER TABLE marketing_abandoned_cart_reminder ADD CONSTRAINT FK_MARKETING_ABANDONED_CART_REMINDER_CART FOREIGN KEY (cart_id) REFERENCES cart_cart (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
        $this->addSql('ALTER TABLE marketing_abandoned_cart_reminder ADD CONSTRAINT FK_MARKETING_ABANDONED_CART_REMINDER_RULE FOREIGN KEY (rule_id) REFERENCES marketing_abandoned_cart_rule (id) ON DELETE RESTRICT NOT DEFERRABLE INITIALLY IMMEDIATE');
        $this->addSql('ALTER TABLE marketing_abandoned_cart_reminder ADD CONSTRAINT FK_MARKETING_ABANDONED_CART_REMINDER_PROMO FOREIGN KEY (promo_id) REFERENCES cart_promo (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_abandoned_cart_reminder');
        $this->addSql('DROP TABLE marketing_abandoned_cart_rule');
        $this->addSql('DROP TABLE marketing_abandoned_cart_configuration');
        $this->addSql('ALTER TABLE cart_promo DROP restricted_country_codes');
    }
}
