/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2025 SteamWar.de-Serverteam
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   GNU Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package de.steamwar.sql

import de.steamwar.sql.internal.useDb
import org.jetbrains.exposed.v1.core.IntegerColumnType
import java.sql.ResultSet

private val getNum: (ResultSet) -> Int? = {
    if (it.next()) it.getInt("num") else null
}

private const val eventFightParticipation =
    "SELECT FightPlayer.UserID, COUNT(UserID) as num FROM FightPlayer INNER JOIN Fight on FightPlayer.FightID = Fight.FightID WHERE Fight.Server LIKE '%vs%' AND FightPlayer.UserID = ? GROUP BY FightPlayer.UserID"

fun getEventFightParticipation(user: SteamwarUser) =
    useDb {
        exec(
            eventFightParticipation, args = listOf(
                IntegerColumnType() to user.getId()
            )
        ) { getNum(it) }
    }

private const val eventParticipation =
    "SELECT FightPlayer.UserID, COUNT(DISTINCT EventID) as num FROM FightPlayer INNER JOIN Fight F on FightPlayer.FightID = F.FightID INNER JOIN EventFight EF on F.FightID = EF.Fight WHERE F.FightID = FightPlayer.FightID AND FightPlayer.FightID = EF.Fight AND F.Server LIKE '%vs%' AND FightPlayer.UserID = ? GROUP BY FightPlayer.UserID"

fun getEventParticipation(user: SteamwarUser) =
    useDb {
        exec(
            eventParticipation, args = listOf(
                IntegerColumnType() to user.getId()
            )
        ) { getNum(it) }
    }

private const val acceptedSchematics =
    "SELECT NodeOwner, COUNT(DISTINCT NodeId) AS num FROM SchematicNode WHERE NodeType != 'normal' AND NodeType IS NOT NULL AND NodeType NOT LIKE 'c%' AND NodeOwner = ?"

fun getAcceptedSchematics(user: SteamwarUser) =
    useDb { exec(acceptedSchematics, args = listOf(IntegerColumnType() to user.getId())) { getNum(it) } }

private val fightCount = "SELECT COUNT(*) AS num FROM FightPlayer WHERE UserID = ?"

fun getFightCount(user: SteamwarUser) =
    useDb { exec(fightCount, args = listOf(IntegerColumnType() to user.getId())) { getNum(it) } }

fun getRankedList(gamemode: String): List<Pair<String, Int>> =
    useDb {
        emptyList()
    }

private const val fightList =
    "SELECT DATE(StartTime) AS Datum, GameMode AS Modus, COUNT(*) AS Anzahl FROM Fight WHERE DATE(StartTime) >= DATE(NOW()) - INTERVAL 1 WEEK GROUP BY Datum, GameMode ORDER BY Datum ASC"

fun getFightList(): List<Triple<String, String, Int>> =
    useDb {
        exec(fightList) {
            val list = mutableListOf<Triple<String, String, Int>>()
            while (it.next()) {
                list.add(Triple(it.getString("Datum"), it.getString("Modus"), it.getInt("Anzahl")))
            }
            list
        }
    }
        ?: emptyList()
