<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

/**
 * Mail module: add Timestampable + Blamable tracking on mail_configuration.
 *
 * Purely additive — adds nullable audit columns to an existing table:
 *   created_at / updated_at (Timestampable)
 *   created_by / updated_by -> "user"(id) ON DELETE SET NULL (Blamable)
 * No DROP/ALTER on existing data. Columns are lowercase snake_case.
 * Hand-written to mirror the schema dump and guarantee zero destructive
 * instruction. down() drops the new columns and their FKs/indexes.
 */
final class Version20260620200000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Mail: add Timestampable/Blamable columns on mail_configuration (additive)';
    }

    public function up(Schema $schema): void
    {
        $this->addSql('ALTER TABLE mail_configuration ADD created_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL');
        $this->addSql('ALTER TABLE mail_configuration ADD updated_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL');
        $this->addSql('ALTER TABLE mail_configuration ADD created_by INT DEFAULT NULL');
        $this->addSql('ALTER TABLE mail_configuration ADD updated_by INT DEFAULT NULL');
        $this->addSql('ALTER TABLE mail_configuration ADD CONSTRAINT FK_BFC0A7DBDE12AB56 FOREIGN KEY (created_by) REFERENCES "user" (id) ON DELETE SET NULL NOT DEFERRABLE');
        $this->addSql('ALTER TABLE mail_configuration ADD CONSTRAINT FK_BFC0A7DB16FE72E1 FOREIGN KEY (updated_by) REFERENCES "user" (id) ON DELETE SET NULL NOT DEFERRABLE');
        $this->addSql('CREATE INDEX IDX_BFC0A7DBDE12AB56 ON mail_configuration (created_by)');
        $this->addSql('CREATE INDEX IDX_BFC0A7DB16FE72E1 ON mail_configuration (updated_by)');
        $this->addSql("COMMENT ON COLUMN mail_configuration.created_at IS 'Creation timestamp (Timestampable, set on prePersist)'");
        $this->addSql("COMMENT ON COLUMN mail_configuration.updated_at IS 'Last update timestamp (Timestampable, set on prePersist/preUpdate)'");
        $this->addSql("COMMENT ON COLUMN mail_configuration.created_by IS 'User who created the entry (Blamable, FK user.id, SET NULL on delete)'");
        $this->addSql("COMMENT ON COLUMN mail_configuration.updated_by IS 'User who last updated the entry (Blamable, FK user.id, SET NULL on delete)'");
    }

    public function down(Schema $schema): void
    {
        $this->addSql('ALTER TABLE mail_configuration DROP CONSTRAINT FK_BFC0A7DBDE12AB56');
        $this->addSql('ALTER TABLE mail_configuration DROP CONSTRAINT FK_BFC0A7DB16FE72E1');
        $this->addSql('DROP INDEX IDX_BFC0A7DBDE12AB56');
        $this->addSql('DROP INDEX IDX_BFC0A7DB16FE72E1');
        $this->addSql('ALTER TABLE mail_configuration DROP created_at');
        $this->addSql('ALTER TABLE mail_configuration DROP updated_at');
        $this->addSql('ALTER TABLE mail_configuration DROP created_by');
        $this->addSql('ALTER TABLE mail_configuration DROP updated_by');
    }
}
