<?php

declare(strict_types=1);

namespace App\Administration\Domain\Model\AcademicYear;

use DateTimeImmutable;

/**
 * Factory pour générer les périodes par défaut selon le type choisi.
 *
 * Trimestres (collège/lycée) : Sept-Nov / Déc-Fév / Mars-Juin
 * Semestres : Sept-Jan / Fév-Juin
 */
final class DefaultPeriods
{
    /**
     * @param int $startYear L'année de début (ex: 2024 pour 2024-2025)
     */
    public static function forType(PeriodType $type, int $startYear): PeriodConfiguration
    {
        $endYear = $startYear + 1;
        $lastDayOfFeb = (new DateTimeImmutable("{$endYear}-03-01"))->modify('-1 day');
        $firstDayAfterFeb = $lastDayOfFeb->modify('+1 day');

        return match ($type) {
            PeriodType::TRIMESTER => new PeriodConfiguration($type, [
                new AcademicPeriod(
                    sequence: 1,
                    label: 'T1',
                    startDate: new DateTimeImmutable("{$startYear}-09-01"),
                    endDate: new DateTimeImmutable("{$startYear}-11-30"),
                ),
                new AcademicPeriod(
                    sequence: 2,
                    label: 'T2',
                    startDate: new DateTimeImmutable("{$startYear}-12-01"),
                    endDate: $lastDayOfFeb,
                ),
                new AcademicPeriod(
                    sequence: 3,
                    label: 'T3',
                    startDate: $firstDayAfterFeb,
                    endDate: new DateTimeImmutable("{$endYear}-06-30"),
                ),
            ]),
            PeriodType::SEMESTER => new PeriodConfiguration($type, [
                new AcademicPeriod(
                    sequence: 1,
                    label: 'S1',
                    startDate: new DateTimeImmutable("{$startYear}-09-01"),
                    endDate: new DateTimeImmutable("{$endYear}-01-31"),
                ),
                new AcademicPeriod(
                    sequence: 2,
                    label: 'S2',
                    startDate: new DateTimeImmutable("{$endYear}-02-01"),
                    endDate: new DateTimeImmutable("{$endYear}-06-30"),
                ),
            ]),
        };
    }
}
