<?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 Version20260723170000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Encadre la modération des avis, historise les décisions et ajoute le complément de charte.';
    }

    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 policy_additional_content TEXT DEFAULT NULL');
        $this->addSql('ALTER TABLE marketing_product_review ADD rejection_reason_code VARCHAR(64) DEFAULT NULL');
        $this->addSql('ALTER TABLE marketing_product_review ADD withdrawal_reason_code VARCHAR(64) DEFAULT NULL');
        $this->addSql(<<<'SQL'
            CREATE TABLE marketing_product_review_moderation_action (
                id UUID NOT NULL,
                review_id UUID NOT NULL,
                action VARCHAR(40) NOT NULL,
                reason_code VARCHAR(64) DEFAULT NULL,
                reason_detail TEXT DEFAULT NULL,
                actor_identifier VARCHAR(180) NOT NULL,
                snapshot JSON NOT NULL,
                occurred_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL,
                PRIMARY KEY(id)
            )
            SQL);
        $this->addSql('CREATE INDEX marketing_review_action_review_idx ON marketing_product_review_moderation_action (review_id, occurred_at)');
        $this->addSql("COMMENT ON COLUMN marketing_product_review_moderation_action.occurred_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql('ALTER TABLE marketing_product_review_moderation_action ADD CONSTRAINT fk_marketing_review_action_review FOREIGN KEY (review_id) REFERENCES marketing_product_review (id) ON DELETE CASCADE 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_moderation_action');
        $this->addSql('ALTER TABLE marketing_product_review DROP rejection_reason_code');
        $this->addSql('ALTER TABLE marketing_product_review DROP withdrawal_reason_code');
        $this->addSql('ALTER TABLE marketing_review_request_configuration DROP policy_additional_content');
    }
}
