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

#include "baidunlpengine.h"
#include "logger.h"
#include "util.h"
void getmodelinfo() {
    BaiduNlpEngine baiduNlpEngine;
    LOGD("engine name is", baiduNlpEngine.engineName());
    LOGD("engine current model name is", baiduNlpEngine.currentModel());
    LOGD("engine modelinfo is", baiduNlpEngine.modelInfo());
}

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

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

    ai_engine::EngineError error;

    BaiduNlpEngine baiduNlpEngine;
    if (baiduNlpEngine.isCloud()) {
        LOGDF("有网络");
    }
    baiduNlpEngine.setConfig(getConfig());
    bool setResult = baiduNlpEngine.setCurrentModel("ernie-4.0-8k");
    if (setResult != true) {
        LOGE("set error");
    }
    auto result = baiduNlpEngine.init();
    if (result.success != true) {
        LOGE("baiduNlpEngine init error {}", result.error.getCode(),
             result.error.getMessage());
    };

    baiduNlpEngine.setTextGenerationResultCallback(
        [](const ai_engine::gen_ai::nlp::TextGenerationResult &result) {
            LOGD("result.success", result.success);
            LOGD("result.modelName", result.modelName);
            LOGD("result.text.c_str()", result.text.c_str());
            LOGD("result.error.getCode()", result.error.getCode());
            LOGD("result.error.getModule()", result.error.getModule());
            LOGD("result.error.getMessage()", result.error.getMessage());
        });
    const std::string message = R"(
        {
             "messages": [
                {
                    "role": "system",
                    "content": "你是一个作家，善于用优美的语言写文章"
                },
                {
                    "role": "user",
                    "content": "今天天气不错"
                }
            ]
        }
    )";
    result = baiduNlpEngine.chat(message);
    if (result.success != true) {
        LOGE("baiduNlpEngine chat error {}", result.error.getCode(),
             result.error.getMessage());
    };

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

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

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

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

    ai_engine::EngineError error;

    BaiduNlpEngine baiduNlpEngine;
    if (baiduNlpEngine.isCloud()) {
        LOGDF("有网络");
    }
    baiduNlpEngine.setConfig(getConfig());
    auto result = baiduNlpEngine.init();
    if (result.success != true) {
        LOGE("baiduNlpEngine init error", result.error.getCode(),
             result.error.getMessage());
    };

    baiduNlpEngine.setTextGenerationResultCallback(
        [](const ai_engine::gen_ai::nlp::TextGenerationResult &result) {
            LOGD("result.success", result.success);
            LOGD("result.modelName", result.modelName);
            LOGD("result.text.c_str()", result.text.c_str());
            LOGD("result.error.getCode()", result.error.getCode());
            LOGD("result.error.getModule()", result.error.getModule());
            LOGD("result.error.getMessage()", result.error.getMessage());
        });
    const std::string prompt = R"(
        {
             "prompt1": "你是谁",
             "prompt": "你是谁"
        }
    )";
    baiduNlpEngine.generateContent(prompt);

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

int main() {
    getmodelinfo();
    // testchat();
    testChatGenerateContent();
    return 0;
}