<?php

declare(strict_types=1);

namespace App\Mcp\Tool\Piece;

use App\Entity\PieceConstructeurLink;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\ConstructeurRepository;
use App\Repository\ModelTypeRepository;
use App\Repository\PieceRepository;
use Doctrine\ORM\EntityManagerInterface;
use Mcp\Capability\Attribute\McpTool;
use Mcp\Schema\Result\CallToolResult;
use Symfony\Bundle\SecurityBundle\Security;

#[McpTool(
    name: 'update_piece',
    description: 'Update an existing piece. Only provided fields are changed. prix must be a string. Requires ROLE_GESTIONNAIRE.',
)]
class UpdatePieceTool
{
    use McpToolHelper;

    public function __construct(
        private readonly PieceRepository $pieces,
        private readonly EntityManagerInterface $em,
        private readonly Security $security,
        private readonly ModelTypeRepository $modelTypes,
        private readonly ConstructeurRepository $constructeurs,
    ) {}

    /**
     * @param null|string[] $constructeurIds
     */
    public function __invoke(
        string $pieceId,
        ?string $name = null,
        ?string $reference = null,
        ?string $description = null,
        ?string $prix = null,
        ?string $modelTypeId = null,
        ?array $constructeurIds = null,
    ): CallToolResult {
        $this->requireRole($this->security, 'ROLE_GESTIONNAIRE');

        $piece = $this->pieces->find($pieceId);

        if (!$piece) {
            $this->mcpError('not_found', "Piece not found: {$pieceId}");
        }

        if (null !== $name) {
            $piece->setName($name);
        }
        if (null !== $reference) {
            $piece->setReference($reference);
        }
        if (null !== $description) {
            $piece->setDescription($description);
        }
        if (null !== $prix) {
            $piece->setPrix($prix);
        }

        if (null !== $modelTypeId) {
            if ('' === $modelTypeId) {
                $piece->setTypePiece(null);
            } else {
                $modelType = $this->modelTypes->find($modelTypeId);
                if (!$modelType) {
                    $this->mcpError('not_found', "ModelType not found: {$modelTypeId}");
                }
                $piece->setTypePiece($modelType);
            }
        }

        if (null !== $constructeurIds) {
            foreach ($piece->getConstructeurLinks()->toArray() as $existing) {
                $this->em->remove($existing);
            }
            foreach ($constructeurIds as $cId) {
                $c = $this->constructeurs->find($cId);
                if (!$c) {
                    $this->mcpError('not_found', "Constructeur not found: {$cId}");
                }
                $link = new PieceConstructeurLink();
                $link->setPiece($piece);
                $link->setConstructeur($c);
                $this->em->persist($link);
            }
        }

        $this->em->flush();

        return $this->jsonResponse(['id' => $piece->getId(), 'name' => $piece->getName()]);
    }
}
