package de.steamwar.commands.user

import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.CliktError
import com.github.ajalt.clikt.parameters.arguments.argument
import com.github.ajalt.clikt.parameters.arguments.help
import com.github.ajalt.mordant.table.table
import de.steamwar.db.findUser
import de.steamwar.db.useDb
import de.steamwar.sql.Punishment
import de.steamwar.sql.SessionTable
import de.steamwar.sql.SteamwarUser
import de.steamwar.sql.Team
import org.jetbrains.exposed.v1.core.eq
import org.jetbrains.exposed.v1.jdbc.selectAll
import java.time.Duration

class UserInfoCommand : CliktCommand("info") {
    val userId by argument().help("Id, Name, UUID or DiscordId")
    val user by lazy {
        findUser(userId)
            ?: throw CliktError("User not found")
    }

    override val printHelpOnEmptyArgs = true

    override fun run() =
        useDb {
            val sessions =
                SessionTable.selectAll().where { SessionTable.userId eq user.id.value }
                    .map { it[SessionTable.startTime] to it[SessionTable.endTime] }

            val totalPlayed = sessions.sumOf { Duration.between(it.first, it.second).toMinutes() } / 60.0
            val firstJoin = sessions.minByOrNull { it.first }?.first
            val lastJoin = sessions.maxByOrNull { it.second }?.second

            val punishments = Punishment.getAllPunishmentsOfPlayer(user.id.value)

            echo(
                table {
                    body {
                        row("Name", user.userName)
                        row("UUID", user.uuid)
                        row("Team", Team.byId(user.team).teamName)
                        row("Leader", user.leader)
                        row("Locale", user.locale)
                        row("Beigetreten am", firstJoin)
                        row("Zuletzt gesehen am", lastJoin)
                        row("Spielzeit", totalPlayed.toString() + "h")
                        row("Punishments", if (punishments.isEmpty()) "Keine" else table {
                            header { row("Typ", "Ersteller", "Von", "Bis", "Grund") }
                            body {
                                punishments.map {
                                    row(
                                        it.type,
                                        SteamwarUser.byId(it.punisher)?.userName
                                            ?: it.punisher,
                                        it.startTime.toString(),
                                        if (it.perma) "Perma" else it.endTime.toString(),
                                        it.reason
                                    )
                                }
                            }
                        })
                    }
                }
            )

        }
}
