<?php

declare(strict_types=1);

namespace App\Mcp\Tool\Product;

use App\Entity\ProductConstructeurLink;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\ConstructeurRepository;
use App\Repository\ModelTypeRepository;
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: 'update_product',
    description: 'Update an existing product. Only provided fields are changed. supplierPrice must be a string. Requires ROLE_GESTIONNAIRE.',
)]
class UpdateProductTool
{
    use McpToolHelper;

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

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

        $product = $this->products->find($productId);

        if (!$product) {
            $this->mcpError('not_found', "Product not found: {$productId}");
        }

        if (null !== $name) {
            $product->setName($name);
        }
        if (null !== $reference) {
            $product->setReference($reference);
        }
        if (null !== $supplierPrice) {
            $product->setSupplierPrice($supplierPrice);
        }

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

        if (null !== $constructeurIds) {
            foreach ($product->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 ProductConstructeurLink();
                $link->setProduct($product);
                $link->setConstructeur($c);
                $this->em->persist($link);
            }
        }

        $this->em->flush();

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