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

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

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

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

    const AccountItem &item = m_accounts.at(iIndex.row());
    switch (iRole) {
    case NameRole:
        return item.name;
    case BankRole:
        return item.bank;
    case TypeRole:
        return item.type;
    case UnitRole:
        return item.unit;
    case AmountTextRole:
        return item.amountText;
    case AmountRole:
        return item.amount;
    case ClosedRole:
        return item.closed;
    case KindRole:
        return static_cast<int>(item.kind);
    default:
        return {};
    }
}

QHash<int, QByteArray> SKGMAccountListModel::roleNames() const
{
    return {
        {NameRole, "name"},
        {BankRole, "bank"},
        {TypeRole, "type"},
        {UnitRole, "unit"},
        {AmountTextRole, "amountText"},
        {AmountRole, "amount"},
        {ClosedRole, "closed"},
        {KindRole, "kind"},
    };
}

void SKGMAccountListModel::setAccounts(QVector<AccountItem> iAccounts)
{
    beginResetModel();
    m_accounts = std::move(iAccounts);
    endResetModel();
}

void SKGMAccountListModel::clear()
{
    beginResetModel();
    m_accounts.clear();
    endResetModel();
}
