#ifndef BACKENDAPIMANAGER_H
#define BACKENDAPIMANAGER_H

#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QObject>

class backendAPIManager : public QObject {
    Q_OBJECT

   public:
    static void initialize(const QString &backendUrl);
    static void cleanup();
    static void setAuthToken(const QString &token);

    static bool uploadBlastProjectFull(const QJsonObject &projectData);
    static bool uploadBlastProject(const QJsonObject &projectData);
    static bool uploadBlastProjectDets(const QJsonObject &detsData);
    static bool uploadBlastEquipments(const QJsonObject &regDetsData);
    static QJsonArray getSysUsers();
    static QJsonArray getHAddresses();

   signals:
    void uploadFinished(const QJsonDocument &response);
    void uploadError(const QString &errorString);

   private:
    explicit backendAPIManager(const QString &backendUrl, QObject *parent = nullptr);
    ~backendAPIManager();

    // HTTP methods
    QNetworkReply *sendRequest(const QString &endpoint, const QJsonObject &data,
                               const QString &method = "POST");
    void setupRequest(QNetworkRequest &request, const QString &endpoint);
    void handleNetworkReply(QNetworkReply *reply);

    static backendAPIManager *s_instance;
    QString backendUrl;
    QNetworkAccessManager *networkManager;
    bool requestSuccess;
    QString authToken;

   private slots:
    void handleUploadFinished(const QJsonDocument &response, const QUrl &url);
    void handleUploadError(const QString &error);
};

#endif  // BACKENDAPIMANAGER_H
