<?php

declare(strict_types=1);

namespace App\Administration\Application\Query\GetSubjects;

use App\Administration\Domain\Model\SchoolClass\SchoolId;
use App\Administration\Domain\Repository\SubjectRepository;
use App\Shared\Domain\Tenant\TenantId;

use function array_map;

use Symfony\Component\Messenger\Attribute\AsMessageHandler;

/**
 * Handler pour récupérer les matières actives d'un tenant.
 */
#[AsMessageHandler(bus: 'query.bus')]
final readonly class GetSubjectsHandler
{
    public function __construct(
        private SubjectRepository $subjectRepository,
    ) {
    }

    /**
     * @return SubjectDto[]
     */
    public function __invoke(GetSubjectsQuery $query): array
    {
        $subjects = $this->subjectRepository->findActiveByTenantAndSchool(
            TenantId::fromString($query->tenantId),
            SchoolId::fromString($query->schoolId),
        );

        // TODO: Récupérer les comptages d'enseignants et de classes
        // quand les modules Affectations seront implémentés (T7)

        return array_map(
            static fn ($subject) => SubjectDto::fromDomain(
                $subject,
                teacherCount: 0,  // Placeholder - T7
                classCount: 0,    // Placeholder - T7
            ),
            $subjects,
        );
    }
}
