#pragma once

#include <QSortFilterProxyModel>
#include <QDateTime>
#include <QVector>
#include <QPair>

class DateGroupProxyModel : public QSortFilterProxyModel
{
    Q_OBJECT

public:
    enum GroupBy {
        GroupByDate = 0
    };

    explicit DateGroupProxyModel(QObject *parent = nullptr);

    // Grouping methods
    void setGroupByDate(bool enabled);
    bool isGroupByDateEnabled() const { return m_groupByDate; }

    // Override to provide group headers
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
    QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
    
    // For grouping - we'll use a custom role for group identification
    static constexpr int GroupRole = Qt::UserRole + 100;
    static constexpr int IsGroupHeaderRole = Qt::UserRole + 101;
    static constexpr int GroupSectionRole = Qt::UserRole + 102;

    // Helper to get group name for a date
    static QString groupNameForDate(const QDateTime &date);

protected:
    bool lessThan(const QModelIndex &left, const QModelIndex &right) const override;
    bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;

private:
    bool m_groupByDate = false;
    mutable QVector<int> m_groupRowCounts; // cached group sizes
    mutable QVector<int> m_groupStartRows; // cached start rows for each group
    
    void updateGroupCache() const;
    int groupForRow(int row) const;
    QString groupNameForRow(int row) const;
};