/*
 * Copyright 2024 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

#ifndef BAIDUNLPENGINE_P_H
#define BAIDUNLPENGINE_P_H

#include <functional>
#include <jsoncpp/json/json.h>
#include <kylin-ai/ai-engine/aiengineerror.h>
#include <kylin-ai/ai-engine/textgenerationengine.h>
#include "cpr/cpr.h"

class BaiduNlpEnginePrivate
    : public ai_engine::gen_ai::nlp::AbstractTextGenerationEngine {
public:
    BaiduNlpEnginePrivate();
    ~BaiduNlpEnginePrivate() override;
    std::string engineName() const override;
    bool isCloud() const override { return true; }
    bool isBuiltInEngine() const override { return false; }
    bool isCustomModelEngine() const override { return false; }

    void setConfig(const std::string &config) override;
    bool setCurrentModel(const std::string &model) override;
    std::string currentModel() const override { return currentModel_; }
    std::string currentModelName() const override { return currentModelName_; }
    std::string modelInfo() const override;

    void setTextGenerationResultCallback(
        ai_engine::gen_ai::nlp::TextGenerationResultCallback callback) override;

    ai_engine::result::AiEngineBaseResult
    generateContent(const std::string &input) override;
    ai_engine::result::AiEngineBaseResult
    chat(const std::string &message) override;
    void stopGenerateContent() override;
    void stopChat() override;

    ai_engine::result::AiEngineBaseResult init() override;
    ai_engine::result::AiEngineBaseResult destroy() override;

private:
    std::string apiKey_;
    std::string secretKey_;
    std::string accessToken_;
    std::string currentModel_{"ernie-4.0-8k"};
    std::string currentModelName_{"百度-ERNIE-Bot-4"};
    bool refreshAccessTokenRequired_{false};
    bool refreshAccessTokenCompleted_{false};
    int maxTokens_;
    std::string &getAccessToken();
    bool generateAccessToken();

    std::mutex mutex_;
    ai_engine::gen_ai::nlp::TextGenerationResultCallback
        textGenerationResultCallback_{nullptr};

    Json::Value context_;
    void clearContext();
    void addContext(const std::string &role, const std::string &message);
    void addSystemContext();
    void removeLastContext();
    void decreaseChatCounter();
    void increaseChatCounter();
    bool isChatRunning() const;
    std::vector<Json::Value> streamChatData_;
    std::string tempStreamChatData_;

    friend bool writeChatData(const std::string &data, intptr_t userData);
    int getMaxTokens(const std::string &model);

    ai_engine::result::AiEngineBaseResult
    processChatResponse(cpr::Response &response);

    ai_engine::EngineError currentError_;

    bool waitForStopped();
    std::atomic<bool> isStopped_{false};
    std::atomic<int> chatCounter_{0};
    std::string systemRole_;
};

#endif // BAIDUNLPENGINE_P_H
