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

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

std::string getConfig() {
    return R"(
        {
            "appId" : "",
            "apiKey" : "sk-71f067d1ab6b4828a34cbed139b8d71a",
            "secretKey" : ""
        }
    )";
}

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

    ai_engine::EngineError error;

    DeepseekNlpEngine deepseekNlpEngine;
    if (deepseekNlpEngine.isCloud()) {
        printf("有网络\n");
    }
    deepseekNlpEngine.setConfig(getConfig());
    deepseekNlpEngine.setCurrentModel("deepseek-chat");
    auto result = deepseekNlpEngine.init();
    if (!result.success) {
        std::cout << result.error.getCode() << result.error.getMessage()
                  << std::endl;
    }

    deepseekNlpEngine.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": " "
                },
                {
                    "role": "user",
                    "content": "你是谁"
                }
            ]
        }
    )";
    result = deepseekNlpEngine.chat(message);

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

    const std::string message1 = R"(
        {
             "messages": [
                {
                    "role": "user",
                    "content": "我刚才问的什么"
                }
            ]
        }
    )";

    while (std::getchar() != '\n') {
    }

    deepseekNlpEngine.chat(message1);

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

void testGenerateContentEngine() {
    ai_engine::EngineError error;

    DeepseekNlpEngine deepseekNlpEngine;
    if (deepseekNlpEngine.isCloud()) {
        printf("有网络");
    }
    deepseekNlpEngine.setConfig(getConfig());
    deepseekNlpEngine.setCurrentModel("deepseek-reasoner");
    deepseekNlpEngine.init();

    deepseekNlpEngine.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": "你是谁"
    )";
    deepseekNlpEngine.generateContent(message);

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

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