<?php

declare(strict_types=1);

namespace App\Scolarite\Application\Command\DeleteScheduleSlot;

use App\Scolarite\Domain\Model\Schedule\ScheduleSlot;
use App\Scolarite\Domain\Model\Schedule\ScheduleSlotId;
use App\Scolarite\Domain\Repository\ScheduleSlotRepository;
use App\Shared\Domain\Clock;
use App\Shared\Domain\Tenant\TenantId;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;

#[AsMessageHandler(bus: 'command.bus')]
final readonly class DeleteScheduleSlotHandler
{
    public function __construct(
        private ScheduleSlotRepository $repository,
        private Clock $clock,
    ) {
    }

    /**
     * @return ScheduleSlot Le slot supprimé (pour dispatch events)
     */
    public function __invoke(DeleteScheduleSlotCommand $command): ScheduleSlot
    {
        $tenantId = TenantId::fromString($command->tenantId);
        $slotId = ScheduleSlotId::fromString($command->slotId);

        $slot = $this->repository->get($slotId, $tenantId);

        $slot->supprimer($this->clock->now());
        $this->repository->delete($slotId, $tenantId);

        return $slot;
    }
}
