<?php

declare(strict_types=1);

namespace App\Scolarite\Application\Command\EndReplacement;

use App\Scolarite\Domain\Model\TeacherReplacement\TeacherReplacementId;
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: 'command.bus')]
final readonly class EndReplacementHandler
{
    public function __construct(
        private TeacherReplacementRepository $replacementRepository,
        private Clock $clock,
    ) {
    }

    public function __invoke(EndReplacementCommand $command): void
    {
        $replacement = $this->replacementRepository->get(
            TeacherReplacementId::fromString($command->replacementId),
            TenantId::fromString($command->tenantId),
        );

        $replacement->terminer($this->clock->now());
        $this->replacementRepository->save($replacement);
    }
}
