<?php

declare(strict_types=1);

namespace App\Mcp\Tool\Machine;

use App\Entity\MachineComponentLink;
use App\Entity\MachinePieceLink;
use App\Entity\MachineProductLink;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\ComposantRepository;
use App\Repository\MachineComponentLinkRepository;
use App\Repository\MachinePieceLinkRepository;
use App\Repository\MachineRepository;
use App\Repository\PieceRepository;
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: 'add_machine_links',
    description: 'Add one or more links (composant, piece, product) to a machine. Each link specifies a type, entityId, and optional parentLinkId / overrides. Requires ROLE_GESTIONNAIRE.',
)]
class AddMachineLinksTool
{
    use McpToolHelper;

    public function __construct(
        private readonly EntityManagerInterface $em,
        private readonly Security $security,
        private readonly MachineRepository $machines,
        private readonly ComposantRepository $composants,
        private readonly PieceRepository $pieces,
        private readonly ProductRepository $products,
        private readonly MachineComponentLinkRepository $componentLinks,
        private readonly MachinePieceLinkRepository $pieceLinks,
    ) {}

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

        $machine = $this->machines->find($machineId);
        if (null === $machine) {
            $this->mcpError('NotFound', "Machine {$machineId} not found.");
        }

        $created = [];

        foreach ($links as $linkData) {
            $type     = $linkData['type'] ?? '';
            $entityId = $linkData['entityId'] ?? '';

            switch ($type) {
                case 'composant':
                    $composant = $this->composants->find($entityId);
                    if (null === $composant) {
                        $this->mcpError('NotFound', "Composant {$entityId} not found.");
                    }

                    $link = new MachineComponentLink();
                    $link->setMachine($machine);
                    $link->setComposant($composant);

                    if (!empty($linkData['parentLinkId'])) {
                        $parent = $this->componentLinks->find($linkData['parentLinkId']);
                        if (null !== $parent) {
                            $link->setParentLink($parent);
                        }
                    }
                    if (isset($linkData['nameOverride'])) {
                        $link->setNameOverride($linkData['nameOverride']);
                    }
                    if (isset($linkData['referenceOverride'])) {
                        $link->setReferenceOverride($linkData['referenceOverride']);
                    }
                    if (isset($linkData['prixOverride'])) {
                        $link->setPrixOverride($linkData['prixOverride']);
                    }

                    $this->em->persist($link);
                    $created[] = ['id' => $link->getId(), 'type' => 'composant', 'entityId' => $entityId];

                    break;

                case 'piece':
                    $piece = $this->pieces->find($entityId);
                    if (null === $piece) {
                        $this->mcpError('NotFound', "Piece {$entityId} not found.");
                    }

                    $link = new MachinePieceLink();
                    $link->setMachine($machine);
                    $link->setPiece($piece);
                    $link->setQuantity((int) ($linkData['quantity'] ?? 1));

                    if (!empty($linkData['parentLinkId'])) {
                        $parent = $this->componentLinks->find($linkData['parentLinkId']);
                        if (null !== $parent) {
                            $link->setParentLink($parent);
                        }
                    }
                    if (isset($linkData['nameOverride'])) {
                        $link->setNameOverride($linkData['nameOverride']);
                    }
                    if (isset($linkData['referenceOverride'])) {
                        $link->setReferenceOverride($linkData['referenceOverride']);
                    }
                    if (isset($linkData['prixOverride'])) {
                        $link->setPrixOverride($linkData['prixOverride']);
                    }

                    $this->em->persist($link);
                    $created[] = ['id' => $link->getId(), 'type' => 'piece', 'entityId' => $entityId];

                    break;

                case 'product':
                    $product = $this->products->find($entityId);
                    if (null === $product) {
                        $this->mcpError('NotFound', "Product {$entityId} not found.");
                    }

                    $link = new MachineProductLink();
                    $link->setMachine($machine);
                    $link->setProduct($product);

                    if (!empty($linkData['parentLinkId'])) {
                        $parentProduct = $this->em->getRepository(MachineProductLink::class)->find($linkData['parentLinkId']);
                        if (null !== $parentProduct) {
                            $link->setParentLink($parentProduct);
                        }
                    }
                    if (!empty($linkData['parentComponentLinkId'])) {
                        $parentComp = $this->componentLinks->find($linkData['parentComponentLinkId']);
                        if (null !== $parentComp) {
                            $link->setParentComponentLink($parentComp);
                        }
                    }
                    if (!empty($linkData['parentPieceLinkId'])) {
                        $parentPiece = $this->pieceLinks->find($linkData['parentPieceLinkId']);
                        if (null !== $parentPiece) {
                            $link->setParentPieceLink($parentPiece);
                        }
                    }

                    $this->em->persist($link);
                    $created[] = ['id' => $link->getId(), 'type' => 'product', 'entityId' => $entityId];

                    break;

                default:
                    $this->mcpError('Validation', "Unknown link type '{$type}'. Expected composant, piece, or product.");
            }
        }

        $this->em->flush();

        return $this->jsonResponse(['created' => $created]);
    }
}
