<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

final class Version20260226210000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Reassign legacy INTERIM contract to a TIME 35h contract and remove legacy INTERIM contract row';
    }

    public function up(Schema $schema): void
    {
        $this->addSql(
            <<<'SQL'
                DO $$
                DECLARE
                    legacy_interim_contract_id INT;
                    target_time_35h_contract_id INT;
                BEGIN
                    -- Contrat legacy "Intérim" (ancien modèle).
                    SELECT c.id
                    INTO legacy_interim_contract_id
                    FROM contracts c
                    WHERE LOWER(c.name) LIKE '%interim%'
                    ORDER BY c.id
                    LIMIT 1;

                    -- Si déjà supprimé, on ne fait rien.
                    IF legacy_interim_contract_id IS NULL THEN
                        RETURN;
                    END IF;

                    -- Contrat cible: suivi horaire 35h.
                    SELECT c.id
                    INTO target_time_35h_contract_id
                    FROM contracts c
                    WHERE c.tracking_mode = 'TIME'
                      AND c.weekly_hours = 35
                      AND c.id <> legacy_interim_contract_id
                    ORDER BY
                        CASE WHEN LOWER(c.name) = '35h' THEN 0 ELSE 1 END,
                        c.id
                    LIMIT 1;

                    IF target_time_35h_contract_id IS NULL THEN
                        RAISE EXCEPTION 'No TIME 35h contract found to replace legacy INTERIM contract id=%', legacy_interim_contract_id;
                    END IF;

                    -- Ré-assigne l'historique de périodes.
                    UPDATE employee_contract_periods
                    SET contract_id = target_time_35h_contract_id
                    WHERE contract_id = legacy_interim_contract_id;

                    -- Ré-assigne le pointeur actuel employé (compat legacy / affichage).
                    UPDATE employees
                    SET contract_id = target_time_35h_contract_id
                    WHERE contract_id = legacy_interim_contract_id;

                    -- Garde-fou FK avant suppression.
                    IF EXISTS (SELECT 1 FROM employee_contract_periods p WHERE p.contract_id = legacy_interim_contract_id)
                       OR EXISTS (SELECT 1 FROM employees e WHERE e.contract_id = legacy_interim_contract_id) THEN
                        RAISE EXCEPTION 'Legacy INTERIM contract id=% is still referenced', legacy_interim_contract_id;
                    END IF;

                    DELETE FROM contracts WHERE id = legacy_interim_contract_id;
                END $$;
                SQL
        );
    }

    public function down(Schema $schema): void
    {
        throw new IrreversibleMigration('This migration performs data reassignment and contract deletion.');
    }
}
