<?php

declare(strict_types=1);

namespace App\Administration\Application\Command\UnlinkParentFromStudent;

use App\Administration\Domain\Model\StudentGuardian\StudentGuardian;
use App\Administration\Domain\Model\StudentGuardian\StudentGuardianId;
use App\Administration\Domain\Repository\StudentGuardianRepository;
use App\Shared\Domain\Clock;
use App\Shared\Domain\Tenant\TenantId;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;

/**
 * Handler pour supprimer une liaison parent-élève.
 */
#[AsMessageHandler(bus: 'command.bus')]
final readonly class UnlinkParentFromStudentHandler
{
    public function __construct(
        private StudentGuardianRepository $repository,
        private Clock $clock,
    ) {
    }

    public function __invoke(UnlinkParentFromStudentCommand $command): StudentGuardian
    {
        $linkId = StudentGuardianId::fromString($command->linkId);
        $tenantId = TenantId::fromString($command->tenantId);
        $link = $this->repository->get($linkId, $tenantId);

        $link->delier($this->clock->now());
        $this->repository->delete($linkId, $tenantId);

        return $link;
    }
}
