<?php

declare(strict_types=1);

namespace App\Module\Directory\Infrastructure\Mcp\Tool;

use App\Module\Directory\Domain\Repository\ProspectRepositoryInterface;
use App\Shared\Infrastructure\Mcp\Serializer;
use InvalidArgumentException;
use Mcp\Capability\Attribute\McpTool;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;

use function sprintf;

#[McpTool(name: 'get-prospect', description: 'Get a prospect by ID with full details.')]
class GetProspectTool
{
    public function __construct(
        private readonly ProspectRepositoryInterface $prospectRepository,
        private readonly Security $security,
    ) {}

    public function __invoke(int $id): string
    {
        if (!$this->security->isGranted('ROLE_USER')) {
            throw new AccessDeniedException('Access denied: ROLE_USER required.');
        }

        $prospect = $this->prospectRepository->findById($id);
        if (null === $prospect) {
            throw new InvalidArgumentException(sprintf('Prospect with ID %d not found.', $id));
        }

        return json_encode(Serializer::prospect($prospect));
    }
}
