<?php

declare(strict_types=1);

namespace App\Mcp\Tool\ModelType;

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

#[McpTool(
    name: 'delete_model_type',
    description: 'Delete a model type by ID. Requires ROLE_GESTIONNAIRE.',
)]
class DeleteModelTypeTool
{
    use McpToolHelper;

    public function __construct(
        private readonly ModelTypeRepository $modelTypes,
        private readonly EntityManagerInterface $em,
        private readonly Security $security,
    ) {}

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

        $mt = $this->modelTypes->find($modelTypeId);

        if (!$mt) {
            $this->mcpError('not_found', "ModelType not found: {$modelTypeId}");
        }

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

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