<?php

declare(strict_types=1);

namespace App\Scolarite\Infrastructure\Service;

use App\Administration\Domain\Model\User\UserId;
use App\Administration\Domain\Repository\StudentGuardianRepository;
use App\Administration\Domain\Repository\UserRepository;
use App\Scolarite\Application\Port\ParentChildrenReader;
use App\Shared\Domain\Tenant\TenantId;

use function array_map;

use Override;

/**
 * Résout les enfants d'un parent via la liaison StudentGuardian.
 */
final readonly class GuardianParentChildrenReader implements ParentChildrenReader
{
    public function __construct(
        private StudentGuardianRepository $studentGuardianRepository,
        private UserRepository $userRepository,
    ) {
    }

    #[Override]
    public function childrenOf(string $guardianId, TenantId $tenantId): array
    {
        $links = $this->studentGuardianRepository->findStudentsForGuardian(
            UserId::fromString($guardianId),
            $tenantId,
        );

        return array_map(function ($link): array {
            $student = $this->userRepository->get($link->studentId);

            return [
                'studentId' => (string) $link->studentId,
                'firstName' => $student->firstName,
                'lastName' => $student->lastName,
            ];
        }, $links);
    }
}
