<?php

declare(strict_types=1);

namespace App\Mcp\Tool\Composant;

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

#[McpTool(
    name: 'delete_composant',
    description: 'Delete a composant by ID. Requires ROLE_GESTIONNAIRE.',
)]
class DeleteComposantTool
{
    use McpToolHelper;

    public function __construct(
        private readonly ComposantRepository $composants,
        private readonly EntityManagerInterface $em,
        private readonly Security $security,
    ) {}

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

        $composant = $this->composants->find($composantId);

        if (!$composant) {
            $this->mcpError('not_found', "Composant not found: {$composantId}");
        }

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

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