#ifndef EMAILLISTMODEL_H
#define EMAILLISTMODEL_H

#include <QAbstractTableModel>
#include <QHash>
#include <QByteArray>
#include "../db/dao/mailitemdao.h"

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

    enum EmailRoles {
        IdRole = Qt::UserRole + 1,
        SubjectRole,
        SenderRole,
        RecipientRole,
        DateRole,
        ReadRole,
        FlaggedRole,
        AttachmentsRole,
        FileIdRole,
        SizeRole,
        MessageIdRole,
        SenderInitialRole // computed from sender
    };

    // Column indices (match role order for simplicity)
    enum Columns {
        ColSubject = 0,
        ColSender = 1,
        ColDate = 2,
        ColCount = 3 // number of visible columns
    };

    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    int columnCount(const QModelIndex &parent = QModelIndex()) const override;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
    QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
    QHash<int, QByteArray> roleNames() const override;

    // Set the folderId to filter emails; -1 means all folders
    void setFolderId(int folderId);
    // Refresh the model from the database based on current folderId
    void refresh();
    // Set search filter on the model
    void setSearchFilter(const QString &filter);
    // Set filter flags
    void setShowUnreadOnly(bool show);
    void setShowFlaggedOnly(bool show);
    void setShowHasAttachments(bool show);

    // Get all emails currently in the model (after filtering)
    const QVector<MailItem>& emails() const { return m_emails; }

private:
    QVector<MailItem> m_emails;
    int m_folderId{-1}; // -1 means all folders
    QString m_searchFilter;
    bool m_unreadOnly{false};
    bool m_flaggedOnly{false};
    bool m_hasAttachments{false};
};
#endif // EMAILLISTMODEL_H