#ifndef AUTHMANAGER_H
#define AUTHMANAGER_H

#include <QObject>

QT_BEGIN_NAMESPACE
class QSettings;
QT_END_NAMESPACE

struct SessionData
{
  QString id;
  QString email;
  QString emailPrefix;
};

class AuthManager : public QObject
{
  Q_OBJECT
public:
  explicit AuthManager(QObject *parent = nullptr);

  bool isSignedIn() const;

  /// @brief If isSignedIn() this will contain the human's session data.
  /// @returns Session data for the authed human. nullptr if not signed in.
  SessionData *sessionData() const;

public slots:
  /// @brief Sends a code to the requested email.
  ///
  /// You must verify the code in the signIn method to receive a session.
  ///
  /// @params email to send code to.
  void requestSignInCode(const QString &email);

  /// @brief Creates a session if authenticated.
  ///
  /// @params code that was sent to the email in the previous step
  void signIn(const QString &code);

  void signOut();

  /// @brief checks if there is a cached session that is valid
  void tryRestoreSession();

signals:
  void sessionRestoreCompleted();

  void isSignedInChanged();
  void signedOut();
  void signedIn();

  void codeSentToEmail(const QString &email);

  void errorOccurred(const QString &message);

private:
  QString m_lastEmail;

  bool m_isSignedIn;
  void setIsSignedIn(bool);

  SessionData *m_sessionData;

  QSettings *m_settings;
  QString m_sessionToken;

  /// Sets session token internally to class, in storage, and sets networkmanager appropriately
  /// Pass "" if you want to remove it.
  void setSessionToken(const QString &);
};

#endif // AUTHMANAGER_H
