<?php

declare(strict_types=1);

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

use App\Module\Directory\Domain\Entity\Prestataire;
use App\Shared\Infrastructure\Mcp\Serializer;
use Doctrine\ORM\EntityManagerInterface;
use Mcp\Capability\Attribute\McpTool;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;

#[McpTool(name: 'create-prestataire', description: 'Create a prestataire / service provider (admin). Only name is required.')]
class CreatePrestataireTool
{
    public function __construct(
        private readonly EntityManagerInterface $entityManager,
        private readonly Security $security,
    ) {}

    public function __invoke(
        string $name,
        ?string $email = null,
        ?string $phone = null,
        ?string $website = null,
    ): string {
        if (!$this->security->isGranted('ROLE_ADMIN')) {
            throw new AccessDeniedException('Access denied: ROLE_ADMIN required.');
        }

        $prestataire = new Prestataire();
        $prestataire->setName($name);
        $prestataire->setEmail($email);
        $prestataire->setPhone($phone);
        $prestataire->setWebsite($website);

        $this->entityManager->persist($prestataire);
        $this->entityManager->flush();

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