<?php

/*
 * 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 Version20260724100000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Ajoute la configuration Brevo chiffrée et les campagnes emailing texte.';
    }

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

        $this->addSql('CREATE TABLE marketing_brevo_configuration (id VARCHAR(32) NOT NULL, enabled BOOLEAN DEFAULT false NOT NULL, api_key_ciphertext TEXT DEFAULT NULL, api_key_hint VARCHAR(12) DEFAULT NULL, account_email VARCHAR(255) DEFAULT NULL, company_name VARCHAR(255) DEFAULT NULL, connection_status VARCHAR(20) DEFAULT \'not_configured\' NOT NULL, last_checked_at TIMESTAMP(0) WITH TIME ZONE DEFAULT NULL, last_error TEXT DEFAULT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, updated_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, PRIMARY KEY(id))');
        $this->addSql('CREATE TABLE marketing_email_campaign (id UUID NOT NULL, title VARCHAR(255) NOT NULL, subject VARCHAR(255) NOT NULL, text_content TEXT NOT NULL, sender_id INT NOT NULL, recipient_list_ids JSON NOT NULL, utm_source VARCHAR(255) NOT NULL, utm_medium VARCHAR(255) DEFAULT NULL, utm_campaign VARCHAR(255) DEFAULT NULL, utm_term VARCHAR(255) DEFAULT NULL, utm_content VARCHAR(255) DEFAULT NULL, brevo_campaign_id INT DEFAULT NULL, status VARCHAR(20) DEFAULT \'draft\' NOT NULL, last_error TEXT DEFAULT NULL, sent_at TIMESTAMP(0) WITH 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))');
        $this->addSql('CREATE INDEX marketing_email_campaign_created_idx ON marketing_email_campaign (created_at)');
        $this->addSql('COMMENT ON COLUMN marketing_email_campaign.id IS \'(DC2Type:uuid)\'');
    }

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

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