<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

/**
 * Absence management: create the absence_request table.
 */
final class Version20260521123522 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Create absence_request table';
    }

    public function up(Schema $schema): void
    {
        $this->addSql(<<<'SQL'
                CREATE TABLE absence_request (
                  id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
                  type VARCHAR(32) NOT NULL,
                  start_date DATE NOT NULL,
                  end_date DATE NOT NULL,
                  start_half_day VARCHAR(16) DEFAULT NULL,
                  end_half_day VARCHAR(16) DEFAULT NULL,
                  counted_days DOUBLE PRECISION NOT NULL,
                  reason TEXT DEFAULT NULL,
                  justification_file_name VARCHAR(255) DEFAULT NULL,
                  status VARCHAR(16) NOT NULL,
                  rejection_reason TEXT DEFAULT NULL,
                  created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL,
                  reviewed_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL,
                  user_id INT NOT NULL,
                  reviewed_by_id INT DEFAULT NULL,
                  PRIMARY KEY (id)
                )
            SQL);
        $this->addSql('CREATE INDEX IDX_F211AA17A76ED395 ON absence_request (user_id)');
        $this->addSql('CREATE INDEX IDX_F211AA17FC6B21F1 ON absence_request (reviewed_by_id)');
        $this->addSql(<<<'SQL'
                ALTER TABLE
                  absence_request
                ADD
                  CONSTRAINT FK_F211AA17A76ED395 FOREIGN KEY (user_id) REFERENCES "user" (id) ON DELETE CASCADE NOT DEFERRABLE
            SQL);
        $this->addSql(<<<'SQL'
                ALTER TABLE
                  absence_request
                ADD
                  CONSTRAINT FK_F211AA17FC6B21F1 FOREIGN KEY (reviewed_by_id) REFERENCES "user" (id) ON DELETE SET NULL NOT DEFERRABLE
            SQL);
    }

    public function down(Schema $schema): void
    {
        $this->addSql('ALTER TABLE absence_request DROP CONSTRAINT FK_F211AA17A76ED395');
        $this->addSql('ALTER TABLE absence_request DROP CONSTRAINT FK_F211AA17FC6B21F1');
        $this->addSql('DROP TABLE absence_request');
    }
}
