#ifndef ACCOUNTSERVICE_H
#define ACCOUNTSERVICE_H

#include <QObject>
#include <QVector>
#include "models/account.h"
#include "db/dao/accountdao.h"
#include "core/authenticator.h"
#include "core/gmailauthenticator.h"
#include "core/outlookauthenticator.h"
#include "core/imapauthenticator.h"
#include "core/synchronizerprovider.h"
#include "core/eventbus.h"
#include "core/events.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 QString &email, const QString &providerType,
                    const QString &accessToken = QString(),
                    const QString &refreshToken = QString());
    void removeAccount(int accountId);
    void updateAccount(const Account &account);

    void startAuthentication(const QString &email, const QString &providerType);
    
    Authenticator* createAuthenticator(const QString &providerType);

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);
};

#endif // ACCOUNTSERVICE_H
