<?php

declare(strict_types=1);

namespace App\Administration\Application\Command\RemoveAssignment;

use App\Administration\Domain\Model\TeacherAssignment\TeacherAssignment;
use App\Administration\Domain\Model\TeacherAssignment\TeacherAssignmentId;
use App\Administration\Domain\Repository\TeacherAssignmentRepository;
use App\Shared\Domain\Clock;
use App\Shared\Domain\Tenant\TenantId;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;

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

    public function __invoke(RemoveAssignmentCommand $command): TeacherAssignment
    {
        $assignmentId = TeacherAssignmentId::fromString($command->assignmentId);
        $tenantId = TenantId::fromString($command->tenantId);

        $assignment = $this->assignmentRepository->get($assignmentId, $tenantId);

        $assignment->retirer($this->clock->now());

        $this->assignmentRepository->save($assignment);

        return $assignment;
    }
}
