#pragma once

#include <QString>
#include <QDateTime>
#include <QtGlobal>
#include <QByteArray>

enum class AccountType {
    POP3,
    Outlook,
    Gmail,
    IMAP
};

class Account
{
public:
    Account() = default;
    Account(int id, const QString& email, const QString& displayName,
            AccountType type, const QString& accessToken = QString(),
            const QString& refreshToken = QString(),
            const QDateTime& tokenExpires = QDateTime());

    int id() const { return m_id; }
    void setId(int id) { m_id = id; }

    QString email() const { return m_email; }
    void setEmail(const QString& email) { m_email = email; }

    QString displayName() const { return m_displayName; }
    void setDisplayName(const QString& displayName) { m_displayName = displayName; }

    AccountType type() const { return m_type; }
    void setType(AccountType type) { m_type = type; }

    QString accessToken() const { return m_accessToken; }
    void setAccessToken(const QString& token) { m_accessToken = token; }

    QString refreshToken() const { return m_refreshToken; }
    void setRefreshToken(const QString& token) { m_refreshToken = token; }

    QDateTime tokenExpires() const { return m_tokenExpires; }
    void setTokenExpires(const QDateTime& expires) { m_tokenExpires = expires; }

    bool isTokenValid() const {
        return !m_accessToken.isEmpty() && m_tokenExpires.isValid() &&
               m_tokenExpires > QDateTime::currentDateTimeUtc();
    }

    /// Connection settings for incoming and outgoing servers
    struct ConnectionSettings {
        QString type; // "imap" or "pop3"
        QString incomingHost;
        quint16 incomingPort = 0;
        bool incomingSsl = false;
        QString outgoingHost;
        quint16 outgoingPort = 0;
        bool outgoingSsl = false;
        QString username;
        QString password;
        QString authMethod; // "plain" or "oauth2"
    };

    const ConnectionSettings& connectionSettings() const { return m_connectionSettings; }
    void setConnectionSettings(const ConnectionSettings& settings) { m_connectionSettings = settings; }

    /// Simple encryption/decryption for passwords (XOR with fixed key, base64 encoded)
    static QString encryptPassword(const QString& plainText);
    static QString decryptPassword(const QString& encrypted);

private:
    int m_id{0};
    QString m_email;
    QString m_displayName;
    AccountType m_type{AccountType::IMAP};
    QString m_accessToken;
    QString m_refreshToken;
    QDateTime m_tokenExpires;
    ConnectionSettings m_connectionSettings;
};