<?php

declare(strict_types=1);

namespace App\Scolarite\Application\Query\GetActiveReplacements;

use App\Scolarite\Domain\Repository\TeacherReplacementRepository;
use App\Shared\Domain\Clock;
use App\Shared\Domain\Tenant\TenantId;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;

#[AsMessageHandler(bus: 'query.bus')]
final readonly class GetActiveReplacementsHandler
{
    public function __construct(
        private TeacherReplacementRepository $replacementRepository,
        private Clock $clock,
    ) {
    }

    /** @return array<ReplacementDto> */
    public function __invoke(GetActiveReplacementsQuery $query): array
    {
        $replacements = $this->replacementRepository->findActiveByTenant(
            TenantId::fromString($query->tenantId),
            $this->clock->now(),
        );

        return array_map(
            static fn ($r) => ReplacementDto::fromDomain($r),
            $replacements,
        );
    }
}
