<?php

declare(strict_types=1);

namespace App\Scolarite\Domain\Service;

use App\Administration\Domain\Model\SchoolCalendar\SchoolCalendar;
use App\Scolarite\Domain\Exception\DateEcheanceInvalideException;
use DateTimeImmutable;

final readonly class DueDateValidator
{
    public function valider(DateTimeImmutable $dueDate, DateTimeImmutable $now, SchoolCalendar $calendar): void
    {
        $tomorrow = $now->modify('+1 day')->setTime(0, 0);

        if ($dueDate < $tomorrow) {
            throw DateEcheanceInvalideException::dansLePasse($dueDate);
        }

        if (!$calendar->estJourOuvre($dueDate)) {
            $entry = $calendar->trouverEntreePourDate($dueDate);
            $raison = $entry !== null ? $entry->label : 'weekend';

            throw DateEcheanceInvalideException::jourBloque($dueDate, $raison);
        }
    }
}
