#ifndef AUTHENTICATOR_H
#define AUTHENTICATOR_H

#include <QObject>
#include <QString>
#include <QUrl>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QJsonDocument>
#include <QJsonObject>
#include "core/models/account.h"

class Authenticator : public QObject
{
    Q_OBJECT
public:
    explicit Authenticator(QObject *parent = nullptr);
    ~Authenticator() override = default;

    virtual void authenticate(const QString &email) = 0;
    virtual void refreshToken(const Account &account) = 0;
    virtual QString providerName() const = 0;

signals:
    void authenticationCompleted(const Account &account);
    void authenticationFailed(const QString &errorMessage);
    void tokenRefreshed(const QString &accountId, const QString &newToken);

protected:
    QNetworkAccessManager *m_networkManager;
    QString m_clientId;
    QString m_clientSecret;
    QString m_redirectUri;
    QString m_authEndpoint;
    QString m_tokenEndpoint;
    QString m_scopes;

    void exchangeAuthorizationCode(const QString &code, const QString &redirectUri, const QString &email);
    void handleTokenResponse(const QByteArray &data, const QString &email, const QString &providerType);
    QNetworkRequest createTokenRequest(const QUrl &url) const;
};

#endif // AUTHENTICATOR_H
