<?php

declare(strict_types=1);

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

use App\Module\Directory\Domain\Repository\PrestataireRepositoryInterface;
use Mcp\Capability\Attribute\McpTool;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;

#[McpTool(name: 'list-prestataires', description: 'List all prestataires with their IDs, names, and emails. Use this to discover valid prestataire IDs.')]
class ListPrestatairesTool
{
    public function __construct(
        private readonly PrestataireRepositoryInterface $prestataireRepository,
        private readonly Security $security,
    ) {}

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

        $prestataires = $this->prestataireRepository->findBy([], ['name' => 'ASC']);

        return json_encode(array_map(fn ($p) => [
            'id'    => $p->getId(),
            'name'  => $p->getName(),
            'email' => $p->getEmail(),
        ], $prestataires));
    }
}
