<?php

declare(strict_types=1);

namespace App\Mcp\Tool\Composant;

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

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

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

    /**
     * @param null|string[] $constructeurIds
     */
    public function __invoke(
        string $composantId,
        ?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');

        $composant = $this->composants->find($composantId);

        if (!$composant) {
            $this->mcpError('not_found', "Composant not found: {$composantId}");
        }

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

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

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

        $this->em->flush();

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