<?php

declare(strict_types=1);

namespace App\Mcp\Tool\Machine;

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

#[McpTool(
    name: 'delete_machine',
    description: 'Delete a machine by ID. Requires ROLE_GESTIONNAIRE.',
)]
class DeleteMachineTool
{
    use McpToolHelper;

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

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

        $machine = $this->machines->find($machineId);

        if (!$machine) {
            $this->mcpError('not_found', "Machine not found: {$machineId}");
        }

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

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