<?php

declare(strict_types=1);

namespace App\Mcp\Tool\Document;

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

#[McpTool(
    name: 'delete_document',
    description: 'Delete a document by ID. Requires ROLE_GESTIONNAIRE.',
)]
class DeleteDocumentTool
{
    use McpToolHelper;

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

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

        $document = $this->documents->find($documentId);

        if (!$document) {
            $this->mcpError('not_found', "Document not found: {$documentId}");
        }

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

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