// SPDX-FileCopyrightText: 2025 Manuel Alcaraz Zambrano <manuel@alcarazzam.dev>
// SPDX-License-Identifier: GPL-3.0-or-later

#include "roundmodel.h"

#include "tournament/tournament.h"

RoundModel::RoundModel(QObject *parent)
    : QAbstractListModel(parent)
{
}

Tournament *RoundModel::tournament()
{
    return m_tournament;
}

void RoundModel::setTournament(Tournament *tournament)
{
    if (m_tournament == tournament) {
        return;
    }

    beginResetModel();
    m_tournament = tournament;
    endResetModel();

    connect(m_tournament, &Tournament::numberOfRoundsChanged, [this]() {
        beginResetModel();
        endResetModel();
    });

    Q_EMIT tournamentChanged();
}

int RoundModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);

    return m_tournament->numberOfRounds();
}

QVariant RoundModel::data(const QModelIndex &index, int role) const
{
    Q_ASSERT(checkIndex(index, CheckIndexOption::IndexIsValid | CheckIndexOption::ParentIsInvalid));

    const auto round = m_tournament->round(index.row() + 1);

    switch (role) {
    case RoundModel::RoundRole::DateTimeRole: {
        if (round == nullptr) {
            return QDateTime{};
        }
        return round->dateTime();
    }
    }

    return {};
}

bool RoundModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    Q_ASSERT(checkIndex(index, CheckIndexOption::IndexIsValid | CheckIndexOption::ParentIsInvalid));

    if (const auto ok = m_tournament->ensureRoundExists(index.row() + 1); !ok) {
        return false;
    }
    const auto round = m_tournament->round(index.row() + 1);

    switch (role) {
    case RoundModel::RoundRole::DateTimeRole: {
        auto dateTime = value.toDateTime();
        const auto time = dateTime.time();
        dateTime.setTime({time.hour(), time.minute(), 0});
        if (round->dateTime() == dateTime) {
            return false;
        }
        round->setDateTime(dateTime);
        if (const auto ok = m_tournament->saveRound(round); !ok) {
            return false;
        }
        break;
    }
    }

    Q_EMIT dataChanged(this->index(index.row()), this->index(index.row()));

    return true;
}

QHash<int, QByteArray> RoundModel::roleNames() const
{
    return {
        {RoundRole::DateTimeRole, "dateTime"},
    };
}

#include "moc_roundmodel.cpp"
