<?php

declare(strict_types=1);

namespace App\Administration\Application\Query\GetAssignmentsForTeacher;

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 GetAssignmentsForTeacherHandler
{
    public function __construct(
        private TeacherAssignmentRepository $assignmentRepository,
    ) {
    }

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

        $assignments = $this->assignmentRepository->findActiveByTeacher($teacherId, $tenantId);

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