<?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 Version20260723090000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Ajoute la fondation Marketing pour les relances d’avis après achat.';
    }

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

        $this->addSql('ALTER TABLE cart_cart ADD fulfilled_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL');
        $this->addSql("COMMENT ON COLUMN cart_cart.fulfilled_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql('UPDATE cart_cart SET fulfilled_at = updated_at WHERE delivery = TRUE AND fulfilled_at IS NULL');

        $this->addSql(<<<'SQL'
            CREATE TABLE marketing_review_request_configuration (
                id UUID NOT NULL,
                enabled BOOLEAN DEFAULT FALSE NOT NULL,
                send_delay_days SMALLINT DEFAULT 10 NOT NULL,
                token_lifetime_days SMALLINT DEFAULT 90 NOT NULL,
                moderation_required BOOLEAN DEFAULT TRUE NOT NULL,
                fulfilled_after TIMESTAMP(0) WITHOUT TIME ZONE 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("COMMENT ON COLUMN marketing_review_request_configuration.fulfilled_after IS '(DC2Type:datetime_immutable)'");
        $this->addSql("COMMENT ON COLUMN marketing_review_request_configuration.created_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql("COMMENT ON COLUMN marketing_review_request_configuration.updated_at IS '(DC2Type:datetime_immutable)'");

        $this->addSql(<<<'SQL'
            CREATE TABLE marketing_review_invitation (
                id UUID NOT NULL,
                cart_id UUID NOT 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,
                submitted_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_review_invitation_cart ON marketing_review_invitation (cart_id)');
        $this->addSql('CREATE UNIQUE INDEX uniq_marketing_review_invitation_token_hash ON marketing_review_invitation (token_hash)');
        $this->addSql('CREATE INDEX marketing_review_invitation_status_idx ON marketing_review_invitation (status)');
        $this->addSql("COMMENT ON COLUMN marketing_review_invitation.scheduled_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql("COMMENT ON COLUMN marketing_review_invitation.expires_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql("COMMENT ON COLUMN marketing_review_invitation.sent_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql("COMMENT ON COLUMN marketing_review_invitation.submitted_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql("COMMENT ON COLUMN marketing_review_invitation.created_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql("COMMENT ON COLUMN marketing_review_invitation.updated_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql('ALTER TABLE marketing_review_invitation ADD CONSTRAINT fk_marketing_review_invitation_cart FOREIGN KEY (cart_id) REFERENCES cart_cart (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');

        $this->addSql(<<<'SQL'
            CREATE TABLE marketing_product_review (
                id UUID NOT NULL,
                invitation_id UUID NOT NULL,
                product_id UUID NOT NULL,
                cart_item_id UUID DEFAULT NULL,
                rating SMALLINT NOT NULL,
                author_display_name VARCHAR(80) NOT NULL,
                title VARCHAR(120) DEFAULT NULL,
                comment TEXT NOT NULL,
                status VARCHAR(24) NOT NULL,
                experience_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL,
                submitted_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL,
                published_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL,
                rejected_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL,
                rejection_reason TEXT DEFAULT NULL,
                merchant_reply TEXT 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_product_review_invitation_product ON marketing_product_review (invitation_id, product_id)');
        $this->addSql('CREATE INDEX marketing_product_review_status_idx ON marketing_product_review (status)');
        $this->addSql('CREATE INDEX marketing_product_review_product_idx ON marketing_product_review (product_id)');
        $this->addSql('CREATE INDEX IDX_49063F27A35D7AF0 ON marketing_product_review (invitation_id)');
        $this->addSql('CREATE INDEX IDX_49063F27E9B59A59 ON marketing_product_review (cart_item_id)');
        $this->addSql("COMMENT ON COLUMN marketing_product_review.experience_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql("COMMENT ON COLUMN marketing_product_review.submitted_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql("COMMENT ON COLUMN marketing_product_review.published_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql("COMMENT ON COLUMN marketing_product_review.rejected_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql("COMMENT ON COLUMN marketing_product_review.created_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql("COMMENT ON COLUMN marketing_product_review.updated_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql('ALTER TABLE marketing_product_review ADD CONSTRAINT fk_marketing_product_review_invitation FOREIGN KEY (invitation_id) REFERENCES marketing_review_invitation (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
        $this->addSql('ALTER TABLE marketing_product_review ADD CONSTRAINT fk_marketing_product_review_product FOREIGN KEY (product_id) REFERENCES product_product (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
        $this->addSql('ALTER TABLE marketing_product_review ADD CONSTRAINT fk_marketing_product_review_cart_item FOREIGN KEY (cart_item_id) REFERENCES cart_cart_item (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_product_review');
        $this->addSql('DROP TABLE marketing_review_invitation');
        $this->addSql('DROP TABLE marketing_review_request_configuration');
        $this->addSql('ALTER TABLE cart_cart DROP fulfilled_at');
    }
}
