#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 model info 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()) {
        LOGI("Baidu NLP engine is cloud.");
    }
    baiduNlpEngine.setConfig(getConfig());
    bool setResult = baiduNlpEngine.setCurrentModel("ernie-4.0-8k");
    if (setResult != true) {
        LOGE("Set current error.");
    }
    auto result = baiduNlpEngine.init();
    if (!result.success) {
        LOGE("Baidu NLP engine init error:", result.error.getCode(),
             result.error.getMessage());
        return;
    }

    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 message1 = R"(
        {
             "messages": [
                {
                    "role": "system",
                    "content": "你是一个作家，善于用优美的语言写文章"
                },
                {
                    "role": "user",
                    "content": "今天天气不错"
                }
            ]
        }
    )";
    result = baiduNlpEngine.chat(message1);
    if (result.success != true) {
        LOGE("Baidu NLP engine chat error:", result.error.getCode(),
             result.error.getMessage());
    };

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

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

    baiduNlpEngine.stopChat();
    baiduNlpEngine.destroy();
}

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

    ai_engine::EngineError error;

    BaiduNlpEngine baiduNlpEngine;
    if (baiduNlpEngine.isCloud()) {
        LOGI("Baidu NLP engine is cloud.");
    }
    baiduNlpEngine.setConfig(getConfig());
    auto result = baiduNlpEngine.init();
    if (!result.success) {
        LOGE("Baidu NLP engine init error:", result.error.getCode(),
             result.error.getMessage());
        return;
    }

    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;
}