#include <cstdio>
#include <filesystem>
#include <fstream>

#include "qwennlpengine.h"
#include "util.h"

void getModelInfo() {
    QwenNlpEngine qwenNlpEngine;
    printf("Engine name is %s\n", qwenNlpEngine.engineName().c_str());
    printf("Engine current model name is %s\n", qwenNlpEngine.currentModel().c_str());
    printf("Engine modelinfo is %s\n", qwenNlpEngine.modelInfo().c_str());
}

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

void testChatEngine() {
    QwenNlpEngine qwenNlpEngine;
    if (qwenNlpEngine.isCloud()) {
        printf("Engine is cloud\n");
    }
    qwenNlpEngine.setConfig(getConfig());
    qwenNlpEngine.setCurrentModel("qwen-plus");
    auto result = qwenNlpEngine.init();
    if (!result.success) {
        fprintf(stderr, "Error info: %d %s\n", result.error.getCode(), result.error.getMessage().c_str());
        return;
    }

    qwenNlpEngine.setTextGenerationResultCallback([](const ai_engine::gen_ai::nlp::TextGenerationResult &result) {
        if (!result.success) {
            fprintf(stderr, "Error info: %d %s\n", result.error.getCode(), result.error.getMessage().c_str());
            return;
        }
        Json::Value json = qwen_nlp_util::stringToJson(result.text);
        printf("%s", json["result"].asCString());
    });
    const std::string message = R"(
        {
             "messages": [
                {
                    "role": "system",
                    "content": "你是一个作家，善于用优美的语言写文章"
                },
                {
                    "role": "user",
                    "content": "今天天气不错"
                }
            ],
            "top_k": 4
        }
    )";
    result = qwenNlpEngine.chat(message);
    if (!result.success) {
        fprintf(stderr, "Error info: %d %s\n", result.error.getCode(), result.error.getMessage().c_str());
        return;
    }
    while (std::getchar() != '\n') {
    }
    qwenNlpEngine.stopChat();
    qwenNlpEngine.destroy();
}

void testGenerateContentEngine() {
    QwenNlpEngine qwenNlpEngine;
    if (qwenNlpEngine.isCloud()) {
        printf("Engine is cloud\n");
    }
    qwenNlpEngine.setConfig(getConfig());
    auto result = qwenNlpEngine.init();
    if (!result.success) {
        fprintf(stderr, "Error info: %d %s\n", result.error.getCode(), result.error.getMessage().c_str());
        return;
    }

    qwenNlpEngine.setTextGenerationResultCallback([](const ai_engine::gen_ai::nlp::TextGenerationResult &result) {
        if (!result.success) {
            fprintf(stderr, "Error info: %d %s\n", result.error.getCode(), result.error.getMessage().c_str());
            return;
        }
        Json::Value json = qwen_nlp_util::stringToJson(result.text);
        printf("%s", json["result"].asCString());
    });
    const std::string message = R"(
        {
             "prompt": "你是谁"
        }
    )";
    result = qwenNlpEngine.generateContent(message);
    if (!result.success) {
        fprintf(stderr, "Error info: %d %s\n", result.error.getCode(), result.error.getMessage().c_str());
        return;
    }

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

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