<?php

declare(strict_types=1);

namespace App\Administration\Application\Query\GetParentsForStudent;

use App\Administration\Domain\Model\User\UserId;
use App\Administration\Domain\Repository\StudentGuardianRepository;
use App\Administration\Domain\Repository\UserRepository;
use App\Shared\Domain\Tenant\TenantId;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;

/**
 * Handler pour récupérer les parents/tuteurs liés à un élève.
 */
#[AsMessageHandler(bus: 'query.bus')]
final readonly class GetParentsForStudentHandler
{
    public function __construct(
        private StudentGuardianRepository $repository,
        private UserRepository $userRepository,
    ) {
    }

    /**
     * @return GuardianForStudentDto[]
     */
    public function __invoke(GetParentsForStudentQuery $query): array
    {
        $links = $this->repository->findGuardiansForStudent(
            UserId::fromString($query->studentId),
            TenantId::fromString($query->tenantId),
        );

        return array_map(
            fn ($link) => GuardianForStudentDto::fromDomainWithUser(
                $link,
                $this->userRepository->get($link->guardianId),
            ),
            $links,
        );
    }
}
