<?php

declare(strict_types=1);

namespace App\Mcp\Tool\ModelType;

use App\Mcp\Tool\McpToolHelper;
use App\Repository\ModelTypeRepository;
use Mcp\Capability\Attribute\McpTool;
use Mcp\Schema\Result\CallToolResult;

#[McpTool(
    name: 'get_model_type',
    description: 'Get a single model type by ID with full details including skeleton requirements.',
)]
class GetModelTypeTool
{
    use McpToolHelper;

    public function __construct(
        private readonly ModelTypeRepository $modelTypes,
    ) {}

    public function __invoke(string $modelTypeId): CallToolResult
    {
        $mt = $this->modelTypes->find($modelTypeId);

        if (!$mt) {
            $this->mcpError('not_found', "ModelType not found: {$modelTypeId}");
        }

        $skeletonPieces = [];
        foreach ($mt->getSkeletonPieceRequirements() as $req) {
            $skeletonPieces[] = [
                'id'          => $req->getId(),
                'typePieceId' => $req->getTypePiece()->getId(),
                'typePiece'   => $req->getTypePiece()->getName(),
                'position'    => $req->getPosition(),
            ];
        }

        $skeletonProducts = [];
        foreach ($mt->getSkeletonProductRequirements() as $req) {
            $skeletonProducts[] = [
                'id'            => $req->getId(),
                'typeProductId' => $req->getTypeProduct()->getId(),
                'typeProduct'   => $req->getTypeProduct()->getName(),
                'familyCode'    => $req->getFamilyCode(),
                'position'      => $req->getPosition(),
            ];
        }

        $skeletonSubcomponents = [];
        foreach ($mt->getSkeletonSubcomponentRequirements() as $req) {
            $skeletonSubcomponents[] = [
                'id'              => $req->getId(),
                'alias'           => $req->getAlias(),
                'familyCode'      => $req->getFamilyCode(),
                'typeComposantId' => $req->getTypeComposant()?->getId(),
                'typeComposant'   => $req->getTypeComposant()?->getName(),
                'position'        => $req->getPosition(),
            ];
        }

        return $this->jsonResponse([
            'id'                               => $mt->getId(),
            'name'                             => $mt->getName(),
            'code'                             => $mt->getCode(),
            'category'                         => $mt->getCategory()->value,
            'notes'                            => $mt->getNotes(),
            'description'                      => $mt->getDescription(),
            'skeletonPieceRequirements'        => $skeletonPieces,
            'skeletonProductRequirements'      => $skeletonProducts,
            'skeletonSubcomponentRequirements' => $skeletonSubcomponents,
            'createdAt'                        => $mt->getCreatedAt()->format('Y-m-d H:i:s'),
            'updatedAt'                        => $mt->getUpdatedAt()->format('Y-m-d H:i:s'),
        ]);
    }
}
