<?php

declare(strict_types=1);

namespace App\Administration\Application\Command\UnblockUser;

use App\Administration\Domain\Exception\UserNotFoundException;
use App\Administration\Domain\Model\User\User;
use App\Administration\Domain\Model\User\UserId;
use App\Administration\Domain\Repository\UserRepository;
use App\Shared\Domain\Clock;
use App\Shared\Domain\Tenant\TenantId;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;

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

    public function __invoke(UnblockUserCommand $command): User
    {
        $userId = UserId::fromString($command->userId);
        $user = $this->userRepository->get($userId);

        if ($command->tenantId !== '' && !$user->tenantId->equals(TenantId::fromString($command->tenantId))) {
            throw UserNotFoundException::withId($userId);
        }

        $user->debloquer($this->clock->now());

        $this->userRepository->save($user);

        return $user;
    }
}
