<?php

declare(strict_types=1);

namespace App\Scolarite\Application\Command\DesignateReplacement;

use App\Administration\Domain\Exception\TenantMismatchException;
use App\Administration\Domain\Model\SchoolClass\ClassId;
use App\Administration\Domain\Model\Subject\SubjectId;
use App\Administration\Domain\Model\User\Role;
use App\Administration\Domain\Model\User\UserId;
use App\Administration\Domain\Repository\UserRepository;
use App\Scolarite\Domain\Exception\UtilisateurNonEnseignantException;
use App\Scolarite\Domain\Model\TeacherReplacement\ClassSubjectPair;
use App\Scolarite\Domain\Model\TeacherReplacement\TeacherReplacement;
use App\Scolarite\Domain\Repository\TeacherReplacementRepository;
use App\Shared\Domain\Clock;
use App\Shared\Domain\Tenant\TenantId;
use DateTimeImmutable;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;

#[AsMessageHandler(bus: 'command.bus')]
final readonly class DesignateReplacementHandler
{
    public function __construct(
        private TeacherReplacementRepository $replacementRepository,
        private UserRepository $userRepository,
        private Clock $clock,
    ) {
    }

    public function __invoke(DesignateReplacementCommand $command): TeacherReplacement
    {
        $tenantId = TenantId::fromString($command->tenantId);
        $replacedTeacherId = UserId::fromString($command->replacedTeacherId);
        $replacementTeacherId = UserId::fromString($command->replacementTeacherId);

        // Valider l'existence, le tenant et le rôle des enseignants
        $replacedTeacher = $this->userRepository->get($replacedTeacherId);
        if (!$replacedTeacher->tenantId->equals($tenantId)) {
            throw TenantMismatchException::pourUtilisateur($replacedTeacherId, $tenantId);
        }
        if (!$replacedTeacher->aLeRole(Role::PROF)) {
            throw UtilisateurNonEnseignantException::pourUtilisateur($replacedTeacherId);
        }

        $replacementTeacher = $this->userRepository->get($replacementTeacherId);
        if (!$replacementTeacher->tenantId->equals($tenantId)) {
            throw TenantMismatchException::pourUtilisateur($replacementTeacherId, $tenantId);
        }
        if (!$replacementTeacher->aLeRole(Role::PROF)) {
            throw UtilisateurNonEnseignantException::pourUtilisateur($replacementTeacherId);
        }

        $classes = array_map(
            static fn (array $pair) => new ClassSubjectPair(
                ClassId::fromString($pair['classId']),
                SubjectId::fromString($pair['subjectId']),
            ),
            $command->classes,
        );

        $replacement = TeacherReplacement::designer(
            tenantId: $tenantId,
            replacedTeacherId: $replacedTeacherId,
            replacementTeacherId: $replacementTeacherId,
            startDate: new DateTimeImmutable($command->startDate),
            endDate: new DateTimeImmutable($command->endDate),
            classes: $classes,
            reason: $command->reason,
            createdBy: UserId::fromString($command->createdBy),
            now: $this->clock->now(),
        );

        $this->replacementRepository->save($replacement);

        return $replacement;
    }
}
