<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

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

    public function up(Schema $schema): void
    {
        $this->addSql(<<<'SQL'
                CREATE TABLE absence_balance (
                  id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
                  type VARCHAR(32) NOT NULL,
                  period VARCHAR(16) NOT NULL,
                  acquired DOUBLE PRECISION NOT NULL,
                  taken DOUBLE PRECISION NOT NULL,
                  pending DOUBLE PRECISION NOT NULL,
                  user_id INT NOT NULL,
                  PRIMARY KEY (id)
                )
            SQL);
        $this->addSql('CREATE INDEX IDX_65723A76A76ED395 ON absence_balance (user_id)');
        $this->addSql('CREATE UNIQUE INDEX uniq_absence_balance_user_type_period ON absence_balance (user_id, type, period)');
        $this->addSql(<<<'SQL'
                ALTER TABLE
                  absence_balance
                ADD
                  CONSTRAINT FK_65723A76A76ED395 FOREIGN KEY (user_id) REFERENCES "user" (id) ON DELETE CASCADE NOT DEFERRABLE
            SQL);
    }

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