<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

final class Version20260427222300 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Rattache les étudiants et inscriptions existants à l’année active de leur madrassa.';
    }

    public function up(Schema $schema): void
    {
        $this->addSql("UPDATE etudiants e SET annee_scolaire_id = (SELECT a.id FROM annees_scolaires a WHERE a.ecole_id = e.ecole_id AND a.active = 1 AND a.statut = 'ACTIVE' ORDER BY a.date_debut DESC, a.id DESC LIMIT 1) WHERE e.annee_scolaire_id IS NULL");
        $this->addSql("UPDATE inscriptions i SET annee_scolaire_id = (SELECT a.id FROM annees_scolaires a WHERE a.ecole_id = i.ecole_id AND a.active = 1 AND a.statut = 'ACTIVE' ORDER BY a.date_debut DESC, a.id DESC LIMIT 1) WHERE i.annee_scolaire_id IS NULL");
    }

    public function down(Schema $schema): void
    {
    }
}
