#include "imapauthenticator.h"
#include <QDebug>

ImapAuthenticator::ImapAuthenticator(QObject *parent)
    : Authenticator(parent)
{
}

void ImapAuthenticator::authenticate(const QString &email)
{
    qDebug() << "[ImapAuthenticator] Authenticating IMAP account:" << email;

    Account account;
    account.setEmail(email);
    account.setType(AccountType::IMAP);
    account.setAccessToken(m_password);  // Store password as access token for IMAP
    account.setRefreshToken(m_username);
    account.setTokenExpires(QDateTime::currentDateTimeUtc().addYears(1));  // IMAP credentials don't expire

    emit authenticationCompleted(account);
}

void ImapAuthenticator::refreshToken(const Account &account)
{
    qDebug() << "[ImapAuthenticator] IMAP tokens don't expire, reusing credentials";
    emit tokenRefreshed(QString::number(account.id()), account.accessToken());
}

void ImapAuthenticator::configure(const QString &imapServer, int imapPort,
                                   const QString &smtpServer, int smtpPort,
                                   const QString &username, const QString &password)
{
    m_imapServer = imapServer;
    m_imapPort = imapPort;
    m_smtpServer = smtpServer;
    m_smtpPort = smtpPort;
    m_username = username;
    m_password = password;
}

#include "imapauthenticator.moc"
