#ifndef JOURNALSERVICE_H
#define JOURNALSERVICE_H

#include "utils.h"
#include "HttpClient.h"
#include "logs_r.h"

#include <QObject>
#include <QString>
#include <QJsonObject>
#include <QJsonArray>
#include <QList>
#include <QStringList>
#include <QJsonDocument>

class JournalService : public QObject
{
    Q_OBJECT

public:
    explicit JournalService(HttpClient* client, QObject *parent = nullptr)
        : QObject(parent), httpClient(client) {}

    int getLogsAmount(const QMap<QString, QString>& groupWithSensors, qint64 start,  qint64 end) {
        QJsonObject rootObj;
        rootObj["start_time"] = start;
        rootObj["end_time"] = end;
        QJsonObject sensorsObj;
        for (auto it = groupWithSensors.constBegin(); it != groupWithSensors.constEnd(); ++it) {
            QJsonArray sensorArray;
            sensorArray.append(it.value());
            sensorsObj[it.key()] = sensorArray;
        }
        rootObj["sensors"] = sensorsObj;
        QJsonDocument doc(rootObj);
        QByteArray requestData = doc.toJson();

        QJsonObject responseJson = httpClient->post(utils::API_URL + "/logs/getLogsAmount", QJsonDocument::fromJson(requestData).object());


        if (responseJson.contains("amount")) {
            return responseJson["amount"].toInt();
        }
        return 0;
    }

    QList<LogsR> tmpB(int pageNum, int pageSize, qint64 start = 1734063255,  qint64 end = 1735392656) {
        QJsonObject rootObj;
        rootObj["start_time"] = start;
        rootObj["end_time"] = end;
        rootObj["page_number"] = pageNum;
        rootObj["page_size"] = pageSize;
        QJsonDocument doc(rootObj);
        QByteArray requestData = doc.toJson();


        QJsonObject responseJson = httpClient->post(utils::API_URL + "/logs/getLogs", QJsonDocument::fromJson(requestData).object());

        QList<LogsR> logList;

        if (responseJson.contains("logs") && responseJson["logs"].isArray()) {
            QJsonArray alarmsArray = responseJson["logs"].toArray();

            for (const QJsonValue& value : alarmsArray) {
                if (value.isObject()) {
                    LogsR log;
                    log.parseResponse(value.toObject());
                    logList.append(log);
                }
            }
        }

        return logList;
    }

    int tmpA(qint64 start = 1734063255,  qint64 end = 1735392656) {
        QJsonObject rootObj;
        rootObj["start_time"] = start;
        rootObj["end_time"] = end;
        QJsonDocument doc(rootObj);
        QByteArray requestData = doc.toJson();


        QJsonObject responseJson = httpClient->post(utils::API_URL + "/logs/getLogsAmount", QJsonDocument::fromJson(requestData).object());

        if (responseJson.contains("amount")) {
            return responseJson["amount"].toInt();
        }
        return 0;
    }

    QStringList getLogs(const QMap<QString, QString>& groupWithSensors, const QList<int>& threatLevels, qint64 start,  qint64 end, int pageNum, int pageSize) {
        QJsonObject rootObj;
        QJsonArray threatLevelsArray;
        for (int level : threatLevels) {
            threatLevelsArray.append(level);
        }
        rootObj["threat_levels"] = threatLevelsArray;
        rootObj["start_time"] = start;
        rootObj["end_time"] = end;
        rootObj["page_number"] = pageNum;
        rootObj["page_size"] = pageSize;
        QJsonObject sensorsObj;
        for (auto it = groupWithSensors.constBegin(); it != groupWithSensors.constEnd(); ++it) {
            QJsonArray sensorArray;
            sensorArray.append(it.value());
            sensorsObj[it.key()] = sensorArray;
        }
        rootObj["sensors"] = sensorsObj;
        QJsonDocument doc(rootObj);
        QByteArray requestData = doc.toJson();


        QJsonObject responseJson = httpClient->post(utils::API_URL + "/logs/getLogs", QJsonDocument::fromJson(requestData).object());

        QStringList logsList;

        if (responseJson.contains("logs") && responseJson["logs"].isArray()) {
            QJsonArray logsArray = responseJson["logs"].toArray();

            for (const QJsonValue& logValue : logsArray) {
                if (logValue.isObject()) {
                    LogsR log;
                    log.parseResponse(logValue.toObject());
                    logsList.append(log.getMark() + " - " + log.getMessage());
                }
            }
        }

        return logsList;
    }

private:
    HttpClient* httpClient;
};

#endif // JOURNALSERVICE_H
