<?php

declare(strict_types=1);

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

use App\Module\Directory\Domain\Repository\AddressRepositoryInterface;
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-address', description: 'Get an address by ID.')]
class GetAddressTool
{
    public function __construct(
        private readonly AddressRepositoryInterface $addressRepository,
        private readonly Security $security,
    ) {}

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

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

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