#pragma once

#include "../synchronizer.h"
#include <QObject>
#include <QSslSocket>
#include <QStringList>

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

    // Synchronizer interface
    bool initialize(const Account& account) override;
    bool syncFolder(const Folder& folder) override;
    QVector<Folder> getFolders() const override;
    QVector<MailItem> fetchMailItems(const QString& folderId,
                                     qint64 sinceUid = 0) override;
    bool appendMailItem(const QString& folderId, const MailItem& item) override;
    bool updateMailItemFlags(const QString& folderId,
                             const QString& itemUid,
                             bool read, bool flagged) override;
    bool deleteMailItem(const QString& folderId,
                        const QString& itemUid) override;

signals:
    void progressChanged(int percent) const;
    void statusMessage(const QString& message) const;

private:
    // Connection details
    mutable QString m_host;
    mutable quint16 m_port{0};
    mutable bool m_useSsl{false};
    mutable QString m_username;
    mutable QString m_password;
    mutable QString m_authMethod; // "plain" or "oauth2"
    mutable QByteArray m_capabilities;

    // Helper para generar IDs únicos de eventos
    QString generateEventId() const;

    // IMAP command helpers
    bool sendCommand(QSslSocket& socket, const QString& command, QString& response) const;
    bool waitForResponse(QSslSocket& socket, const QString& expectedTag, QString& response) const;
    QVector<Folder> parseListResponse(const QString& response) const;
};