<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

/**
 * Absence management: split the paid-leave balance into "acquired" (Congés N-1,
 * finalized and available) and "acquiring" (Congés N, "en cours d'acquisition"),
 * mirroring the two columns on a French payslip.
 *
 * Existing paid-leave balances were filled solely by the monthly accrual, which
 * credited `acquired`; those days actually belong to the current period's
 * "en cours d'acquisition", so they are moved to `acquiring` on the way up.
 */
final class Version20260522090000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Add acquiring (en cours d\'acquisition) to absence_balance and reclassify existing paid-leave days';
    }

    public function up(Schema $schema): void
    {
        $this->addSql('ALTER TABLE absence_balance ADD acquiring DOUBLE PRECISION DEFAULT 0 NOT NULL');
        $this->addSql("UPDATE absence_balance SET acquiring = acquired, acquired = 0 WHERE type = 'cp'");
        $this->addSql('ALTER TABLE absence_balance ALTER acquiring DROP DEFAULT');
    }

    public function down(Schema $schema): void
    {
        // Fold the in-progress days back into acquired before dropping the column.
        $this->addSql("UPDATE absence_balance SET acquired = acquired + acquiring WHERE type = 'cp'");
        $this->addSql('ALTER TABLE absence_balance DROP acquiring');
    }
}
