<?php

declare(strict_types=1);

namespace App\Administration\Application\Query\GetAssignment;

use App\Administration\Application\Query\GetAssignmentsForTeacher\TeacherAssignmentDto;
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\Domain\Repository\TeacherAssignmentRepository;
use App\Shared\Domain\Tenant\TenantId;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;

#[AsMessageHandler(bus: 'query.bus')]
final readonly class GetAssignmentHandler
{
    public function __construct(
        private TeacherAssignmentRepository $assignmentRepository,
    ) {
    }

    public function __invoke(GetAssignmentQuery $query): ?TeacherAssignmentDto
    {
        $assignment = $this->assignmentRepository->findByTeacherClassSubject(
            UserId::fromString($query->teacherId),
            ClassId::fromString($query->classId),
            SubjectId::fromString($query->subjectId),
            AcademicYearId::fromString($query->academicYearId),
            TenantId::fromString($query->tenantId),
        );

        if ($assignment === null) {
            return null;
        }

        return TeacherAssignmentDto::fromDomain($assignment);
    }
}
