<?php

declare(strict_types=1);

namespace App\Mcp\Tool\Site;

use App\Mcp\Tool\McpToolHelper;
use App\Repository\SiteRepository;
use Doctrine\ORM\EntityManagerInterface;
use Mcp\Capability\Attribute\McpTool;
use Mcp\Schema\Result\CallToolResult;
use Symfony\Bundle\SecurityBundle\Security;

#[McpTool(
    name: 'delete_site',
    description: 'Delete a site by ID. Requires ROLE_GESTIONNAIRE.',
)]
class DeleteSiteTool
{
    use McpToolHelper;

    public function __construct(
        private readonly SiteRepository $sites,
        private readonly EntityManagerInterface $em,
        private readonly Security $security,
    ) {}

    public function __invoke(string $siteId): CallToolResult
    {
        $this->requireRole($this->security, 'ROLE_GESTIONNAIRE');

        $site = $this->sites->find($siteId);

        if (!$site) {
            $this->mcpError('not_found', "Site not found: {$siteId}");
        }

        $this->em->remove($site);
        $this->em->flush();

        return $this->jsonResponse(['deleted' => true, 'id' => $siteId]);
    }
}
