<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

final class Version20260519211723 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Mail integration: create mail_configuration, mail_folder, mail_message, task_mail_link tables';
    }

    public function up(Schema $schema): void
    {
        $this->addSql(<<<'SQL'
                CREATE TABLE mail_configuration (
                    id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
                    protocol VARCHAR(10) NOT NULL,
                    imap_host VARCHAR(255) DEFAULT NULL,
                    imap_port INT NOT NULL,
                    imap_encryption VARCHAR(10) NOT NULL,
                    smtp_host VARCHAR(255) DEFAULT NULL,
                    smtp_port INT NOT NULL,
                    smtp_encryption VARCHAR(10) NOT NULL,
                    username VARCHAR(255) DEFAULT NULL,
                    encrypted_password TEXT DEFAULT NULL,
                    sent_folder_path VARCHAR(255) NOT NULL,
                    enabled BOOLEAN NOT NULL,
                    PRIMARY KEY (id)
                )
            SQL);

        $this->addSql(<<<'SQL'
                CREATE TABLE mail_folder (
                    id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
                    path VARCHAR(500) NOT NULL,
                    display_name VARCHAR(255) NOT NULL,
                    parent_path VARCHAR(500) DEFAULT NULL,
                    unread_count INT NOT NULL,
                    total_count INT NOT NULL,
                    last_synced_at TIMESTAMP(0) WITH TIME ZONE DEFAULT NULL,
                    PRIMARY KEY (id)
                )
            SQL);

        $this->addSql('CREATE UNIQUE INDEX UNIQ_319BB6A6B548B0F ON mail_folder (path)');
        $this->addSql('CREATE INDEX idx_mail_folder_parent_path ON mail_folder (parent_path)');

        $this->addSql(<<<'SQL'
                CREATE TABLE mail_message (
                    id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
                    message_id VARCHAR(500) NOT NULL,
                    folder_id INT NOT NULL,
                    uid INT NOT NULL,
                    subject VARCHAR(500) DEFAULT NULL,
                    from_address VARCHAR(255) NOT NULL,
                    from_name VARCHAR(255) DEFAULT NULL,
                    to_addresses JSON NOT NULL,
                    cc_addresses JSON DEFAULT NULL,
                    sent_at TIMESTAMP(0) WITH TIME ZONE NOT NULL,
                    is_read BOOLEAN NOT NULL,
                    is_flagged BOOLEAN NOT NULL,
                    has_attachments BOOLEAN NOT NULL,
                    snippet TEXT DEFAULT NULL,
                    synced_at TIMESTAMP(0) WITH TIME ZONE NOT NULL,
                    PRIMARY KEY (id)
                )
            SQL);

        $this->addSql('CREATE UNIQUE INDEX UNIQ_6C00B110537A1329 ON mail_message (message_id)');
        $this->addSql('CREATE UNIQUE INDEX uq_mail_message_folder_uid ON mail_message (folder_id, uid)');
        $this->addSql('CREATE INDEX IDX_6C00B110162CB942 ON mail_message (folder_id)');
        $this->addSql('CREATE INDEX idx_mail_message_sent_at ON mail_message (sent_at)');
        $this->addSql('CREATE INDEX idx_mail_message_is_read ON mail_message (is_read)');
        $this->addSql('ALTER TABLE mail_message ADD CONSTRAINT FK_6C00B110162CB942 FOREIGN KEY (folder_id) REFERENCES mail_folder (id) ON DELETE CASCADE NOT DEFERRABLE');

        $this->addSql(<<<'SQL'
                CREATE TABLE task_mail_link (
                    id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
                    task_id INT NOT NULL,
                    mail_message_id INT NOT NULL,
                    linked_at TIMESTAMP(0) WITH TIME ZONE NOT NULL,
                    linked_by_id INT DEFAULT NULL,
                    PRIMARY KEY (id)
                )
            SQL);

        $this->addSql('CREATE UNIQUE INDEX uq_task_mail_link ON task_mail_link (task_id, mail_message_id)');
        $this->addSql('CREATE INDEX IDX_E4FDC7C98DB60186 ON task_mail_link (task_id)');
        $this->addSql('CREATE INDEX IDX_E4FDC7C987B9F9D5 ON task_mail_link (mail_message_id)');
        $this->addSql('CREATE INDEX IDX_E4FDC7C91AE3CFF3 ON task_mail_link (linked_by_id)');
        $this->addSql('ALTER TABLE task_mail_link ADD CONSTRAINT FK_E4FDC7C98DB60186 FOREIGN KEY (task_id) REFERENCES task (id) ON DELETE CASCADE NOT DEFERRABLE');
        $this->addSql('ALTER TABLE task_mail_link ADD CONSTRAINT FK_E4FDC7C987B9F9D5 FOREIGN KEY (mail_message_id) REFERENCES mail_message (id) ON DELETE CASCADE NOT DEFERRABLE');
        $this->addSql('ALTER TABLE task_mail_link ADD CONSTRAINT FK_E4FDC7C91AE3CFF3 FOREIGN KEY (linked_by_id) REFERENCES "user" (id) ON DELETE SET NULL NOT DEFERRABLE');
    }

    public function down(Schema $schema): void
    {
        $this->addSql('ALTER TABLE task_mail_link DROP CONSTRAINT FK_E4FDC7C98DB60186');
        $this->addSql('ALTER TABLE task_mail_link DROP CONSTRAINT FK_E4FDC7C987B9F9D5');
        $this->addSql('ALTER TABLE task_mail_link DROP CONSTRAINT FK_E4FDC7C91AE3CFF3');
        $this->addSql('DROP TABLE task_mail_link');

        $this->addSql('ALTER TABLE mail_message DROP CONSTRAINT FK_6C00B110162CB942');
        $this->addSql('DROP TABLE mail_message');

        $this->addSql('DROP TABLE mail_folder');

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