<?php

declare(strict_types=1);

namespace App\Mcp\Tool\Product;

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

#[McpTool(
    name: 'delete_product',
    description: 'Delete a product by ID. Requires ROLE_GESTIONNAIRE.',
)]
class DeleteProductTool
{
    use McpToolHelper;

    public function __construct(
        private readonly ProductRepository $products,
        private readonly EntityManagerInterface $em,
        private readonly Security $security,
    ) {}

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

        $product = $this->products->find($productId);

        if (!$product) {
            $this->mcpError('not_found', "Product not found: {$productId}");
        }

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

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