<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

/**
 * Absence module: add Timestampable/Blamable columns to absence_balance and
 * absence_policy.
 *
 * AbsenceBalance and AbsencePolicy adopt TimestampableBlamableTrait.
 * AbsenceRequest is intentionally untouched (it already carries createdAt /
 * reviewedAt). This migration is purely additive — nullable columns + nullable
 * FK to "user" with ON DELETE SET NULL. No DROP/ALTER on existing data. Columns
 * are lowercase snake_case. Hand-written to guarantee zero destructive
 * instruction.
 */
final class Version20260620170000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Absence: add timestampable/blamable columns to absence_balance and absence_policy (additive)';
    }

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

        // absence_policy
        $this->addSql('ALTER TABLE absence_policy ADD created_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL');
        $this->addSql('ALTER TABLE absence_policy ADD updated_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL');
        $this->addSql('ALTER TABLE absence_policy ADD created_by INT DEFAULT NULL');
        $this->addSql('ALTER TABLE absence_policy ADD updated_by INT DEFAULT NULL');
        $this->addSql('ALTER TABLE absence_policy ADD CONSTRAINT FK_7A780B65DE12AB56 FOREIGN KEY (created_by) REFERENCES "user" (id) ON DELETE SET NULL NOT DEFERRABLE');
        $this->addSql('ALTER TABLE absence_policy ADD CONSTRAINT FK_7A780B6516FE72E1 FOREIGN KEY (updated_by) REFERENCES "user" (id) ON DELETE SET NULL NOT DEFERRABLE');
        $this->addSql('CREATE INDEX IDX_7A780B65DE12AB56 ON absence_policy (created_by)');
        $this->addSql('CREATE INDEX IDX_7A780B6516FE72E1 ON absence_policy (updated_by)');
        $this->addSql("COMMENT ON COLUMN absence_policy.created_at IS 'Creation timestamp (Timestampable, set on prePersist)'");
        $this->addSql("COMMENT ON COLUMN absence_policy.updated_at IS 'Last update timestamp (Timestampable, set on prePersist/preUpdate)'");
        $this->addSql("COMMENT ON COLUMN absence_policy.created_by IS 'User who created the entry (Blamable, FK user.id, SET NULL on delete)'");
        $this->addSql("COMMENT ON COLUMN absence_policy.updated_by IS 'User who last updated the entry (Blamable, FK user.id, SET NULL on delete)'");
    }

    public function down(Schema $schema): void
    {
        // absence_balance
        $this->addSql('ALTER TABLE absence_balance DROP CONSTRAINT FK_65723A76DE12AB56');
        $this->addSql('ALTER TABLE absence_balance DROP CONSTRAINT FK_65723A7616FE72E1');
        $this->addSql('DROP INDEX IDX_65723A76DE12AB56');
        $this->addSql('DROP INDEX IDX_65723A7616FE72E1');
        $this->addSql('ALTER TABLE absence_balance DROP created_at');
        $this->addSql('ALTER TABLE absence_balance DROP updated_at');
        $this->addSql('ALTER TABLE absence_balance DROP created_by');
        $this->addSql('ALTER TABLE absence_balance DROP updated_by');

        // absence_policy
        $this->addSql('ALTER TABLE absence_policy DROP CONSTRAINT FK_7A780B65DE12AB56');
        $this->addSql('ALTER TABLE absence_policy DROP CONSTRAINT FK_7A780B6516FE72E1');
        $this->addSql('DROP INDEX IDX_7A780B65DE12AB56');
        $this->addSql('DROP INDEX IDX_7A780B6516FE72E1');
        $this->addSql('ALTER TABLE absence_policy DROP created_at');
        $this->addSql('ALTER TABLE absence_policy DROP updated_at');
        $this->addSql('ALTER TABLE absence_policy DROP created_by');
        $this->addSql('ALTER TABLE absence_policy DROP updated_by');
    }
}
