<?php

declare(strict_types=1);

namespace App\Administration\Application\Query\GetTeachersForClass;

use App\Administration\Application\Query\GetAssignmentsForTeacher\TeacherAssignmentDto;
use App\Administration\Domain\Model\SchoolClass\ClassId;
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 GetTeachersForClassHandler
{
    public function __construct(
        private TeacherAssignmentRepository $assignmentRepository,
    ) {
    }

    /**
     * @return TeacherAssignmentDto[]
     */
    public function __invoke(GetTeachersForClassQuery $query): array
    {
        $classId = ClassId::fromString($query->classId);
        $tenantId = TenantId::fromString($query->tenantId);

        $assignments = $this->assignmentRepository->findActiveByClass($classId, $tenantId);

        return array_map(TeacherAssignmentDto::fromDomain(...), $assignments);
    }
}
