/*
   SPDX-FileCopyrightText: 2026 Laurent Montel <montel@kde.org>

   SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "readreceiptsmodel.h"
#include <KLocalizedString>

using namespace Qt::Literals::StringLiterals;
ReadReceiptsModel::ReadReceiptsModel(QObject *parent)
    : QAbstractListModel(parent)
{
}

ReadReceiptsModel::~ReadReceiptsModel() = default;

int ReadReceiptsModel::rowCount(const QModelIndex &parent) const
{
    if (parent.isValid()) { // flat model
        return 0;
    }
    return mReadReceipts.count();
}

QVariant ReadReceiptsModel::data(const QModelIndex &index, int role) const
{
    if (index.row() < 0 || index.row() >= mReadReceipts.count()) {
        return {};
    }
    const ReadReceipt receiptInfo = mReadReceipts.at(index.row());
    switch (role) {
    case Qt::DisplayRole:
    case ReadReceiptsModel::ReadReceiptsInfo::UserName:
        return receiptInfo.userName();
    case ReadReceiptsModel::ReadReceiptsInfo::UserId:
        return receiptInfo.userId();
    case ReadReceiptsModel::ReadReceiptsInfo::Name:
        return receiptInfo.name();
    case ReadReceiptsModel::ReadReceiptsInfo::TimeStamp:
        return receiptInfo.timeStamp();
    case ReadReceiptsModel::AvatarInfo:
        return QVariant::fromValue(avatarInfo(receiptInfo));
    default:
        break;
    }
    return {};
}

ReadReceipts ReadReceiptsModel::readReceipts() const
{
    return mReadReceipts;
}

void ReadReceiptsModel::setReadReceipts(const ReadReceipts &newReadReceipts)
{
    beginResetModel();
    mReadReceipts = newReadReceipts;
    endResetModel();
}

Utils::AvatarInfo ReadReceiptsModel::avatarInfo(const ReadReceipt &user) const
{
    const Utils::AvatarInfo info{
        .etag = {},
        .identifier = user.userName(),
        .avatarType = Utils::AvatarType::User,
    };
    return info;
}

#include "moc_readreceiptsmodel.cpp"
