#include <filesystem>
#include <fstream>
#include <iostream>
#include <vector>

#include "customnlpengine.h"
#include "util.h"
void getmodelinfo() {
    CustomNlpEngine customNlpEngine;
    printf("engine name is %s \n", customNlpEngine.engineName().c_str());
    printf("engine current model name is %s\n",
           customNlpEngine.currentModel().c_str());
    printf("engine modelinfo is %s\n", customNlpEngine.modelInfo().c_str());
}

std::string getConfig1() {
    return R"(
        {
            "appId" : "",
            "apiKey" : "",
            "secretKey" : "",
            "apiUrl" : "http://10.41.116.100:11434/api/chat",
            "modelName" : "深度求索",
            "modelVersion" : "deepseek-r1:1.5b"
        }
    )";
}
std::string getConfig2() {
    return R"(
        {
            "appId" : "",
            "apiKey" : "sk-cd05ca96ede445859997408862c80ed6",
            "secretKey" : "",
            "apiUrl" : "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions",
            "modelName" : "千问",
            "modelVersion" : "qwen-plus"
        }
    )";
}

std::string getConfig3() {
    return R"(
        {
           "apiKey" : "",
           "apiUrl" : "https://api.lkeap.cloud.tencent.com/v1/chat/completions",
            "modelName" : "千问",
           "modelVersion" : "deepseek-r1"
        }
    )";
}

std::string getConfig4() {
    return R"(
        {
           "apiKey" : "",
           "apiUrl" : "http://www.baidu.com",
            "modelName" : "",
           "modelVersion" : ""
        }
    )";
}
std::string getConfig5() {
    return R"(
        {
           "apiKey" : "",
           "apiUrl" : "http://10.41.160.151:11434/api/chat",
            "modelName" : "",
           "modelVersion" : "DeepSeek-R1-Distill-Qwen-14B"
        }
    )";
}

void testchatengine() {
    namespace fs = std::filesystem;

    ai_engine::EngineError error;

    CustomNlpEngine customNlpEngine;
    if (customNlpEngine.isCloud()) {
        printf("有网络\n");
    }
    customNlpEngine.setConfig(getConfig2());
    auto result = customNlpEngine.init();
    if (!result.success) {
        std::cout << result.error.getCode() << result.error.getMessage()
                  << std::endl;
    }

    customNlpEngine.setTextGenerationResultCallback(
        [](const ai_engine::gen_ai::nlp::TextGenerationResult &result) {
            std::cout << result.modelName << std::endl;
            std::cout << result.text << std::endl;
            std::cout << result.error.getCode() << result.error.getMessage()
                      << std::endl;
        });
    const std::string message = R"(
        {
             "messages": [
                {
                    "role": "system",
                    "content": "你是一个能够理解自然语言并生成智能回复的助手，名为麒麟AI助手。"
                },
                {
                    "role": "user",
                    "content": "你好"
                }
            ]
        }
    )";
    result = customNlpEngine.chat(message);

    if (!result.success) {
        std::cout << result.error.getCode() << result.error.getMessage()
                  << std::endl;
    }

    while (std::getchar() != '\n') {
        customNlpEngine.stopChat();
        customNlpEngine.destroy();
    }
}

void testGenerateContentEngine() {
    ai_engine::EngineError error;

    CustomNlpEngine customNlpEngine;
    if (customNlpEngine.isCloud()) {
        printf("有网络");
    }
    customNlpEngine.setConfig(getConfig1());
    customNlpEngine.init();

    customNlpEngine.setTextGenerationResultCallback(
        [](const ai_engine::gen_ai::nlp::TextGenerationResult &result) {
            std::cout << result.modelName << std::endl;
            std::cout << result.text << std::endl;
            std::cout << result.error.getCode() << result.error.getMessage()
                      << std::endl;
        });
    const std::string message = R"(
        {
             "prompt": "你是谁"
    )";
    customNlpEngine.generateContent(message);

    while (std::getchar() != '\n') {
        customNlpEngine.stopGenerateContent();
        customNlpEngine.destroy();
    }
}

int main() {
    getmodelinfo();
    testchatengine();
    testGenerateContentEngine();
    return 0;
}