<?php

declare(strict_types=1);

namespace App\Administration\Application\Query\GetClasses;

use App\Administration\Domain\Model\SchoolClass\AcademicYearId;
use App\Administration\Domain\Repository\ClassRepository;
use App\Shared\Domain\Tenant\TenantId;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;

/**
 * Handler pour récupérer les classes actives d'un tenant.
 */
#[AsMessageHandler(bus: 'query.bus')]
final readonly class GetClassesHandler
{
    public function __construct(
        private ClassRepository $classRepository,
    ) {
    }

    /**
     * @return ClassDto[]
     */
    public function __invoke(GetClassesQuery $query): array
    {
        $classes = $this->classRepository->findActiveByTenantAndYear(
            TenantId::fromString($query->tenantId),
            AcademicYearId::fromString($query->academicYearId),
        );

        return array_map(
            static fn ($class) => ClassDto::fromDomain($class),
            $classes,
        );
    }
}
