#ifndef SYSTEMCHAT_H
#define SYSTEMCHAT_H

#include "global/kaglobal.h"
#include "commandtemplatelist.h"
#include "modelitemlistmodel.h"

#include <QDebug>
#include <QFile>
#include <QFileInfo>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QObject>
//#include <kylin-ai/private/osassistant/osassistant.h>
#include <string>
#include <aiconfig/modelconfig.h>
#include <QTimer>
#include <string>
#include <aiconfig/modelconfig.h>
#include <QTimer>
#include <QThread>
#include <QEventLoop>
#include <chatdatabase.h>

using namespace kyai::assistant;

class SystemChat : public QThread
{
    Q_OBJECT
public:
    explicit SystemChat(QObject *parent = nullptr);
    ~SystemChat();

    static SystemChat *getPtr();
    void initSystemChat();
	void initCommandList();
    void initWithHistory(std::vector<Message> messages);

    void setActor(int actorId);
    std::vector<Prompt> getPrompts();
    int createCommand(QString name, QString content);
    bool updateCommand(int actorId, QString name, QString content);
    bool deleteCommand(int actorId);
    bool setCommandOrder(QList<CommandTemplateItem> commandTemplateList);

    void stopChat();
    void clearContext();
    void sendMessage(QString text, QString filePath);
    void sendToolMessage(int toolId, FileType fileType, QString para);
    void setSession(int sessionID);
    int getSession();
    bool getFeatureStatus(Features::Feature feature);   //根据AI SDK返回是否支持某种能力

    ///
    /// \brief getNLPModelList  获取当前NLP所有可用模型信息
    /// \return 模型信息列表
    ///
    QList<ModelItem *> getNLPModelList();

    ///
    /// \brief getNLPCurModel  获取当前配置NLP模型名
    /// \return 模型名称
    ///
    QString getNLPCurModel();

    ///
    /// \brief setNLPModel 设置使用NLP模型
    /// \param name 模型名称
    ///
    void setNLPModel(QString name);

protected:
    void run();

signals:
    void reply(const QString &reasoningContentMsg, const QString &message, bool is_reasoning_end, bool is_end, int index, int errorCode, QString model, QString feedbackURL);
    void sigGenerateSummary(const QString &message, bool is_end, int index, int errorCode, QString model);
    void sigPictureUpdate(const QString &data, const QString &errorMessage);
    void pictureOperateUpdate(const QString &data, const QString &errorMessage);
    void sigInfoSourceUpdate(QMap<int, QString> imageToolMap);
    void toolReply(const QString &message, QMap<int, QString> imageToolMap, QString imageUrl, int errorCode,  QString model, QString feedbackURL);
    void sigAddScheduleDate(int operatorId, QString schedule_date, QString msg, QString model, QString feedbackURL);
    void sigSoundMsg(const QString &message, bool is_end, int index);
    void sigGeneratingPic(QString model, QString feedbackURL);
    void sigInitCompleted();
    void sigNLPModelChanged();

private:
    FileType judgeFileType(QString filePath);

public:
    TartgetType target = RECVMSG;
    bool is_end;
    bool is_reasoning_end;
    QString result_msg;
    int result_index;
    QString text;
    bool soundFlag = false;
    QString reasoningContentMsg;

private:
    OsAssistant *m_osassistant = nullptr;
    std::vector<Prompt> m_promptList;
    Features *m_features = nullptr;
    int m_sessionID;            //助手维护，用于区分会话上下文
};

#endif // SYSTEMCHAT_H
