#ifndef CHATDATABASE_H
#define CHATDATABASE_H

#include <memory>
#include <QDateTime>
#include <QVariantList>
#include <QSqlDatabase>
#include <QQueue>

#include "global/kaglobal.h"

class ChatDatabase
{
public:
    ChatDatabase();
    ~ChatDatabase();

    bool insertOne(QString sessionID, int index, QString message, int chatType);
    bool insertOneWithRequestData(QString sessionID, int index, QString message, int chatType, QString requestData);
    bool updateOperateTime(QString sessionID, int index);
    bool deleteBySessionID(QString sessionID);
    bool deleteBySessionIDAndIndex(QString sessionID, int index);
    bool updateFeedback(QString sessionID, int index, QString message);

    QStringList selectBySessionID(QString sessionID);
    QStringList selectBySessionIDAndChatType(QString sessionID, int chatType, int msgIndex);
    QString selectRequestDataBySessionIDAndIndex(QString sessionID, int index); // 获取指定消息的请求数据
    QStringList selectAll();
    QStringList selectAllDialogue();
    //会议助手
    bool insertMeetingSingleMsg(QString meetingID,int index , QString message);
    bool insertMeetingSingleInfo(QString meetingID, QString summary, QString fileName, QString filePath );
    QString selectHumanMessagesByMeetingName(QString meetingName);
    QJsonArray selectAllErrorMeeting();
    int getLastSaveMeetingID();
    int getMeetingIDFromName(const QString meetingName );
    bool deleteByMeetingID(const int  meetingID);
    bool deleteByMeetingName(const QString meetingName);
    QString formatMeetingTime(const QString meetingName);
    int selectMaxHistoryId();
    void updateMaxHistoryId(int history_id);
    static ChatDatabase* getChatDatabase();
    int selectLastRecordChatType(QString sessionID);
    int selectLastChatId(QString sessionID, int chatType);// 查询历史对话恢复最后的符合条件的id
    
    //语音通话
    int selectMaxVoiceHistoryId(); // 查询最大的语音通话id
    void updateMaxVoiceHistoryId(int voiceHistory_id); // 更新最大的语音通话id

    //写作对话
    int selectMaxWritingHistoryId(); // 查询最大的写作对话id
    void updateMaxWritingHistoryId(int writingHistory_id); // 更新最大的写作对话id

    void updateIsCollect(QString sessionID, int isCollect);
    int selectHistoryCount(int isCollect);
    void deleteTheEarliestHistory();

private:
    void initBase();
    void createTable();
    void createMeetingTable();
    bool columnExists(const QSqlDatabase &db, const QString &tableName, const QString &columnName);

private:
    class Private;
    std::shared_ptr<Private> d;
    QString activeMeetingID ;
    int activeMeetingIndex = 0;
};

#endif // CHATDATABASE_H
