// src/services/pop3/pop3synchronizer_mailio.h
// mailio-based POP3 synchronizer

#pragma once

#include "synchronizer.h"
#include <QObject>
#include <QString>

#if defined(USE_MAILIO_IMAP) && USE_MAILIO_IMAP
#include <mailio/pop3.hpp>
#endif

class Pop3SynchronizerMailio : public Synchronizer
{
    Q_OBJECT
public:
    explicit Pop3SynchronizerMailio(QObject* parent = nullptr);
    ~Pop3SynchronizerMailio() 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 (set by initialize)
    mutable std::string m_host;
    mutable quint16 m_port{0};
    mutable bool m_useSsl{false};
    mutable std::string m_username;
    mutable std::string m_password;
    mutable std::string m_authMethod; // "plain", "xoauth2"

    // Helpers
#if defined(USE_MAILIO_IMAP) && USE_MAILIO_IMAP
    bool persistFetchedMessage(const Folder& folder, const mailio::message& msg);
    MailItem parseMailioMessage(const mailio::message& msg);
    mailio::message createMailioMessage(const MailItem& item);
#endif
};