<?php

declare(strict_types=1);

namespace CpCreation\VitiCore\Bridge\Doctrine\Migrations;

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

final class Version20260521133000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Ajoute le journal des emails envoyés avec liens commande et utilisateur.';
    }

    public function up(Schema $schema): void
    {
        $this->addSql('CREATE TABLE mailer_email_log (id UUID NOT NULL, user_id UUID DEFAULT NULL, cart_id UUID DEFAULT NULL, type VARCHAR(80) NOT NULL, recipient_email VARCHAR(255) NOT NULL, recipient_name VARCHAR(255) DEFAULT NULL, subject VARCHAR(255) DEFAULT NULL, status VARCHAR(20) NOT NULL, transport VARCHAR(255) DEFAULT NULL, error TEXT DEFAULT NULL, queued_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, sent_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, failed_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, PRIMARY KEY(id))');
        $this->addSql('CREATE INDEX mailer_email_log_user_idx ON mailer_email_log (user_id)');
        $this->addSql('CREATE INDEX mailer_email_log_cart_idx ON mailer_email_log (cart_id)');
        $this->addSql('CREATE INDEX mailer_email_log_status_idx ON mailer_email_log (status)');
        $this->addSql('COMMENT ON COLUMN mailer_email_log.id IS \'(DC2Type:uuid)\'');
        $this->addSql('COMMENT ON COLUMN mailer_email_log.user_id IS \'(DC2Type:uuid)\'');
        $this->addSql('COMMENT ON COLUMN mailer_email_log.cart_id IS \'(DC2Type:uuid)\'');
        $this->addSql('COMMENT ON COLUMN mailer_email_log.queued_at IS \'(DC2Type:datetime_immutable)\'');
        $this->addSql('COMMENT ON COLUMN mailer_email_log.sent_at IS \'(DC2Type:datetime_immutable)\'');
        $this->addSql('COMMENT ON COLUMN mailer_email_log.failed_at IS \'(DC2Type:datetime_immutable)\'');
        $this->addSql('ALTER TABLE mailer_email_log ADD CONSTRAINT FK_MAILER_EMAIL_LOG_USER FOREIGN KEY (user_id) REFERENCES user_user (id) ON DELETE SET NULL NOT DEFERRABLE INITIALLY IMMEDIATE');
        $this->addSql('ALTER TABLE mailer_email_log ADD CONSTRAINT FK_MAILER_EMAIL_LOG_CART FOREIGN KEY (cart_id) REFERENCES cart_cart (id) ON DELETE SET NULL NOT DEFERRABLE INITIALLY IMMEDIATE');
    }

    public function down(Schema $schema): void
    {
        $this->addSql('ALTER TABLE mailer_email_log DROP CONSTRAINT FK_MAILER_EMAIL_LOG_USER');
        $this->addSql('ALTER TABLE mailer_email_log DROP CONSTRAINT FK_MAILER_EMAIL_LOG_CART');
        $this->addSql('DROP TABLE mailer_email_log');
    }
}
