/***************************************************************************
 * SPDX-FileCopyrightText: 2026 S. MANKOWSKI stephane@mankowski.fr
 * SPDX-License-Identifier: GPL-3.0-or-later
 ***************************************************************************/
#include "skgmtransactionlistmodel.h"

SKGMTransactionListModel::SKGMTransactionListModel(QObject *iParent)
    : QAbstractListModel(iParent)
{
}

int SKGMTransactionListModel::rowCount(const QModelIndex &iParent) const
{
    if (iParent.isValid()) {
        return 0;
    }
    return m_transactions.count();
}

QVariant SKGMTransactionListModel::data(const QModelIndex &iIndex, int iRole) const
{
    if (!iIndex.isValid() || iIndex.row() < 0 || iIndex.row() >= m_transactions.count()) {
        return {};
    }

    const TransactionItem &item = m_transactions.at(iIndex.row());
    switch (iRole) {
    case Qt::DisplayRole:
    case Qt::UserRole:
        return QStringLiteral("%1 %2 %3 %4 %5").arg(item.payee, item.category, item.comment, item.mode, item.number);
    case DateRole:
        return item.date;
    case OperationIdRole:
        return item.operationId;
    case PayeeRole:
        return item.payee;
    case CategoryRole:
        return item.category;
    case CommentRole:
        return item.comment;
    case ModeRole:
        return item.mode;
    case NumberRole:
        return item.number;
    case AmountTextRole:
        return item.amountText;
    case AmountRole:
        return item.amount;
    default:
        return {};
    }
}

QHash<int, QByteArray> SKGMTransactionListModel::roleNames() const
{
    return {
        {DateRole, "date"},
        {OperationIdRole, "operationId"},
        {PayeeRole, "payee"},
        {CategoryRole, "category"},
        {CommentRole, "comment"},
        {ModeRole, "mode"},
        {NumberRole, "number"},
        {AmountTextRole, "amountText"},
        {AmountRole, "amount"},
    };
}

void SKGMTransactionListModel::setTransactions(QVector<TransactionItem> iTransactions)
{
    beginResetModel();
    m_transactions = std::move(iTransactions);
    endResetModel();
}

void SKGMTransactionListModel::clear()
{
    beginResetModel();
    m_transactions.clear();
    endResetModel();
}
