<?php

declare(strict_types=1);

namespace App\Scolarite\Infrastructure\Service;

use App\Administration\Application\Port\TeacherAssignmentChecker;
use App\Administration\Domain\Model\SchoolClass\AcademicYearId;
use App\Administration\Domain\Model\SchoolClass\ClassId;
use App\Administration\Domain\Model\Subject\SubjectId;
use App\Administration\Domain\Model\User\UserId;
use App\Administration\Infrastructure\Service\CurrentAcademicYearResolver;
use App\Scolarite\Application\Port\EnseignantAffectationChecker;
use App\Shared\Domain\Tenant\TenantId;
use Override;

/**
 * Vérifie l'affectation enseignant en résolvant automatiquement l'année académique courante.
 */
final readonly class CurrentYearEnseignantAffectationChecker implements EnseignantAffectationChecker
{
    public function __construct(
        private TeacherAssignmentChecker $assignmentChecker,
        private CurrentAcademicYearResolver $academicYearResolver,
    ) {
    }

    #[Override]
    public function estAffecte(
        UserId $teacherId,
        ClassId $classId,
        SubjectId $subjectId,
        TenantId $tenantId,
    ): bool {
        $academicYearId = $this->academicYearResolver->resolve('current');

        if ($academicYearId === null) {
            return true;
        }

        return $this->assignmentChecker->estAffecte(
            $teacherId,
            $classId,
            $subjectId,
            AcademicYearId::fromString($academicYearId),
            $tenantId,
        );
    }
}
