#ifndef ACCOUNTSERVICE_H
#define ACCOUNTSERVICE_H

#include <QObject>
#include <QVector>
#include "models/account.h"
#include "core/authenticator.h"

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

    QVector<Account> getAllAccounts();
    Account* findAccountById(int id);
    Account* findAccountByEmail(const QString &email);
    void addAccount(const Account &account);
    void addAccount(const QString &email, const QString &providerType,
                    const QString &accessToken, const QString &refreshToken);
    void removeAccount(int accountId);
    void updateAccount(const Account &account);
    void notifyAccountAdded(const Account &account);

    void startAuthentication(const QString &email, const QString &providerType);

    Authenticator* createAuthenticator(const QString &providerType);

    // Test connection to IMAP server using given settings
    bool testConnection(const Account::ConnectionSettings &settings, QString &errorMessage);

signals:
    void accountListChanged();
    void accountAdded(const Account &account);
    void accountRemoved(int accountId);
    void authenticationRequired(const QString &email, const QString &authUrl);

private:
    void publishAccountEvent(const Account &account, bool added);
    void syncFoldersForAccount(const Account &account);
};

#endif // ACCOUNTSERVICE_H