<?php

declare(strict_types=1);

namespace App\Mcp\Tool\CustomField;

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

#[McpTool(
    name: 'delete_custom_field_value',
    description: 'Delete a custom field value by ID. Requires ROLE_GESTIONNAIRE.',
)]
class DeleteCustomFieldValueTool
{
    use McpToolHelper;

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

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

        $cfv = $this->em->getRepository(CustomFieldValue::class)->find($customFieldValueId);

        if (null === $cfv) {
            $this->mcpError('not_found', "CustomFieldValue not found: {$customFieldValueId}");
        }

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

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