/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2026 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.dao.id.CompositeID
import org.jetbrains.exposed.v1.core.dao.id.CompositeIdTable
import org.jetbrains.exposed.v1.core.dao.id.EntityID
import org.jetbrains.exposed.v1.core.eq
import org.jetbrains.exposed.v1.dao.CompositeEntity
import org.jetbrains.exposed.v1.dao.CompositeEntityClass
import org.jetbrains.exposed.v1.javatime.timestamp
import org.jetbrains.exposed.v1.jdbc.deleteWhere
import org.jetbrains.exposed.v1.jdbc.insertIgnore
import java.sql.Timestamp
import java.time.Instant

object BannedUserIPsTable : CompositeIdTable("BannedUserIPs") {
    val userId = reference("UserID", SteamwarUserTable).index()
    val timestamp = timestamp("Timestamp").index()
    val ip = varchar("IP", 45).entityId()

    init {
        addIdColumn(userId)
    }

    override val primaryKey = PrimaryKey(userId, ip)
}

class BannedUserIPs(id: EntityID<CompositeID>) : CompositeEntity(id) {
    companion object : CompositeEntityClass<BannedUserIPs>(BannedUserIPsTable) {

        @JvmStatic
        fun get(userId: Int) =
            useDb {
                find { BannedUserIPsTable.userId eq userId }.toList()
            }

        @JvmStatic
        fun get(ip: String) =
            useDb {
                find { BannedUserIPsTable.ip eq ip }.toList()
            }

        @JvmStatic
        fun banIP(userId: Int, ip: String) =
            useDb {
                BannedUserIPsTable.insertIgnore {
                    it[BannedUserIPsTable.userId] = userId
                    it[BannedUserIPsTable.ip] = ip
                    it[BannedUserIPsTable.timestamp] = Instant.now()
                }
            }

        @JvmStatic
        fun unbanIPs(userId: Int) =
            useDb {
                BannedUserIPsTable.deleteWhere { BannedUserIPsTable.userId eq userId }
            }
    }

    val userID by BannedUserIPsTable.userId.transform({ EntityID(it, SteamwarUserTable) }, { it.value })
    val timestamp: Timestamp by BannedUserIPsTable.timestamp.transform({ it.toInstant() }, { Timestamp.from(it) })
    val ip by BannedUserIPsTable.ip

    fun remove() =
        useDb {
            delete()
        }
}