<?php

declare(strict_types=1);

namespace CpCreation\VitiCore\Bridge\Doctrine\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20260515143000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Ajoute la configuration des emails transactionnels et leur file de planification.';
    }

    public function up(Schema $schema): void
    {
        $this->addSql('CREATE TABLE transactional_email (id CHAR(36) NOT NULL COMMENT \'(DC2Type:uuid)\', name VARCHAR(255) NOT NULL, trigger_event VARCHAR(64) NOT NULL, recipient_type VARCHAR(32) NOT NULL, delay_days INT DEFAULT 0 NOT NULL, template_code VARCHAR(255) DEFAULT NULL, description LONGTEXT DEFAULT NULL, disabled TINYINT(1) DEFAULT 0 NOT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', updated_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE `utf8_unicode_ci` ENGINE = InnoDB');
        $this->addSql('CREATE TABLE transactional_email_queue (id CHAR(36) NOT NULL COMMENT \'(DC2Type:uuid)\', transactional_email_id CHAR(36) NOT NULL COMMENT \'(DC2Type:uuid)\', trigger_event VARCHAR(64) NOT NULL, related_type VARCHAR(64) NOT NULL, related_id VARCHAR(64) NOT NULL, recipient_email VARCHAR(255) DEFAULT NULL, scheduled_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', sent_at DATETIME DEFAULT NULL COMMENT \'(DC2Type:datetime_immutable)\', skipped_at DATETIME DEFAULT NULL COMMENT \'(DC2Type:datetime_immutable)\', failed_at DATETIME DEFAULT NULL COMMENT \'(DC2Type:datetime_immutable)\', error_message LONGTEXT DEFAULT NULL, context JSON DEFAULT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', updated_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_D40FC504160F4887 (transactional_email_id), INDEX idx_transactional_email_queue_due (scheduled_at, sent_at, skipped_at, failed_at), UNIQUE INDEX uniq_transactional_email_queue_source (transactional_email_id, related_type, related_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE `utf8_unicode_ci` ENGINE = InnoDB');
        $this->addSql('ALTER TABLE transactional_email_queue ADD CONSTRAINT FK_D40FC504160F4887 FOREIGN KEY (transactional_email_id) REFERENCES transactional_email (id) ON DELETE CASCADE');
    }

    public function down(Schema $schema): void
    {
        $this->addSql('ALTER TABLE transactional_email_queue DROP FOREIGN KEY FK_D40FC504160F4887');
        $this->addSql('DROP TABLE transactional_email_queue');
        $this->addSql('DROP TABLE transactional_email');
    }
}
