/*
 *   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.Table
import org.jetbrains.exposed.v1.core.and
import org.jetbrains.exposed.v1.core.eq
import org.jetbrains.exposed.v1.jdbc.deleteWhere
import org.jetbrains.exposed.v1.jdbc.insert
import org.jetbrains.exposed.v1.jdbc.selectAll

object UserPermTable : Table("UserPerm") {
    val user = reference("User", SteamwarUserTable.id).index()
    val perm = enumerationByName("Perm", 32, UserPerm::class)

    override val primaryKey = PrimaryKey(user, perm)
}

enum class UserPerm {
    PREFIX_NONE, // special value, not stored in database
    PREFIX_YOUTUBER,
    PREFIX_GUIDE,
    PREFIX_BUILDER,
    PREFIX_SUPPORTER,
    PREFIX_MODERATOR,
    PREFIX_DEVELOPER,
    PREFIX_ADMIN,

    RESTRICTED_MODS,
    COLOR_CHAT,
    TEAM,
    PUNISHMENTS,
    TICKET_LOG,
    BUILD,
    CHECK,
    MODERATION,
    ADMINISTRATION;

    companion object {
        @JvmField
        val emptyPrefix = Prefix("§7", "")

        @JvmField
        val prefixes = mapOf(
            PREFIX_NONE to emptyPrefix,
            PREFIX_YOUTUBER to Prefix("§x§8§A§2§B§E§5", "CC"), // 8A2BE5
            PREFIX_GUIDE to Prefix("§x§e§7§6§2§e§d", "Guide"), // E762ED
            PREFIX_SUPPORTER to Prefix("§x§6§0§9§5§F§B", "Sup"), // 6095FB
            PREFIX_MODERATOR to Prefix("§x§F§F§A§2§5§C", "Mod"), // FFA25C
            PREFIX_BUILDER to Prefix("§x§6§0§F§F§6§A", "Arch"), // 60FF6A
            PREFIX_DEVELOPER to Prefix("§x§0§B§B§C§B§3", "Dev"), // 0BBCB3
            PREFIX_ADMIN to Prefix("§x§F§F§2§B§2§4", "Admin"), // FF2B24
        )

        @JvmStatic
        fun getPerms(id: Int) =
            useDb {
                UserPermTable.selectAll().where { UserPermTable.user eq id }.map { it[UserPermTable.perm] }.toSet()
            }

        @JvmStatic
        fun addPerm(user: SteamwarUser, perm: UserPerm) =
            useDb {
                UserPermTable.insert {
                    it[UserPermTable.user] = user.getId()
                    it[UserPermTable.perm] = perm
                }
            }

        @JvmStatic
        fun removePerm(user: SteamwarUser, perm: UserPerm) =
            useDb {
                UserPermTable.deleteWhere {
                    (UserPermTable.user eq user.getId()) and (UserPermTable.perm eq perm)
                }
            }
    }

    data class Prefix(val colorCode: String, val chatPrefix: String)
}