/*
 * Copyright 2025 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 CUSTOMNLPENGINE_P_H
#define CUSTOMNLPENGINE_P_H

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

#include <functional>

#include "cpr/cpr.h"

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

    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:
    bool isValidResponse(const std::string &response) const;
    Json::Value formatResultJsonFromInputString(const std::string &inputString);
    bool preProcessResultJson(const Json::Value &inputResultJson);
    bool isOpenAiApiResult(const Json::Value &result) const;
    bool isOllamaApiResult(const Json::Value &result) const;
    bool isResultEnd(const Json::Value &result) const;
    bool isThinking() const;
    std::string getThinkingResult(const Json::Value &result) const;
    std::string getResult(const Json::Value &result) const;
    void buildChatAndContentGenerationUrl(const std::string &apiUrl);
    void decreaseChatCounter();
    void increaseChatCounter();
    bool isChatRunning() const;

private:
    std::string apiKey_;
    std::string currentModel_;
    std::string currentModelName_;
    std::string chatUrl_;
    std::string generateContentUrl_;

    ai_engine::gen_ai::nlp::TextGenerationResultCallback
        textGenerationResultCallback_{nullptr};

    Json::Value context_;
    int sentenceId_{0};
    void clearContext();
    std::string endStr_ = "\n";
    void addContext_(const std::string &role, const std::string &message);
    void removeLastContext_();
    std::vector<Json::Value> streamChatData_;
    std::string tempStreamChatData_;

    friend bool writeChatData(std::string data, intptr_t userData);

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

    ai_engine::EngineError currentError_;
    std::string currentErrorInfo_;

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

    static std::string systemRole_;

    bool isThinking_{false};
    bool hasReasoningContent_{false};
};

#endif  // CUSTOMNLPENGINE_P_H
