/*
 *   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.EntityID
import org.jetbrains.exposed.v1.core.dao.id.IntIdTable
import org.jetbrains.exposed.v1.dao.IntEntity
import org.jetbrains.exposed.v1.dao.IntEntityClass
import org.jetbrains.exposed.v1.javatime.timestamp
import java.time.Instant

object AuditLogTable : IntIdTable("AuditLog", "AuditLogId") {
    val time = timestamp("Time").index()
    val server = varchar("ServerName", 255).index()
    val serverOwner = reference("ServerOwner", SteamwarUserTable).nullable().index()
    val actor = reference("Actor", SteamwarUserTable).index()
    val action = enumerationByName("ActionType", 255, AuditLog.Type::class).index()
    val actionText = text("ActionText")
}

class AuditLog(id: EntityID<Int>) : IntEntity(id) {
    companion object : IntEntityClass<AuditLog>(AuditLogTable) {
        const val SERVER_NAME_VELOCITY: String = "Velocity"

        private fun create(
            serverName: String,
            serverOwner: SteamwarUser?,
            actor: SteamwarUser,
            actionType: Type,
            text: String = ""
        ) =
            useDb {
                new {
                    this.time = Instant.now()
                    this.server = serverName
                    this.serverOwner = serverOwner?.id
                    this.actor = actor.id
                    this.action = actionType
                    this.actionText = text
                }
            }

        @JvmStatic
        fun createJoin(jointServerName: String, serverOwner: SteamwarUser?, joinedPlayer: SteamwarUser) =
            create(jointServerName, serverOwner, joinedPlayer, Type.JOIN)

        @JvmStatic
        fun createLeave(leftServerName: String, serverOwner: SteamwarUser?, joinedPlayer: SteamwarUser) =
            create(leftServerName, serverOwner, joinedPlayer, Type.LEAVE)

        @JvmStatic
        fun createCommand(serverName: String, serverOwner: SteamwarUser?, player: SteamwarUser?, command: String) =
            player?.let { create(serverName, serverOwner, it, Type.COMMAND, command) }

        @JvmStatic
        fun createSensitiveCommand(
            serverName: String,
            serverOwner: SteamwarUser?,
            player: SteamwarUser?,
            command: String
        ) =
            player?.let { create(serverName, serverOwner, it, Type.SENSITIVE_COMMAND, command) }

        @JvmStatic
        fun createChat(serverName: String, serverOwner: SteamwarUser?, chatter: SteamwarUser, chat: String) =
            create(serverName, serverOwner, chatter, Type.CHAT, chat)

        @JvmStatic
        fun createGuiOpen(serverName: String, serverOwner: SteamwarUser?, player: SteamwarUser, guiName: String) =
            create(serverName, serverOwner, player, Type.GUI_OPEN, guiName)

        @JvmStatic
        fun createGuiClick(
            serverName: String,
            serverOwner: SteamwarUser?,
            player: SteamwarUser,
            guiName: String,
            clickType: String,
            slot: Int,
            itemName: String
        ) =
            create(
                serverName,
                serverOwner,
                player,
                Type.GUI_CLICK,
                "Gui: $guiName\nSlot: $slot\nClickType: $clickType\nItemName: $itemName"
            )

        @JvmStatic
        fun createGuiClose(serverName: String, serverOwner: SteamwarUser?, player: SteamwarUser, guiName: String) =
            create(serverName, serverOwner, player, Type.GUI_CLOSE, guiName)
    }

    var time by AuditLogTable.time
    var server by AuditLogTable.server
    var serverOwner by AuditLogTable.serverOwner
    var actor by AuditLogTable.actor
    var action by AuditLogTable.action
    var actionText by AuditLogTable.actionText

    enum class Type {
        JOIN,
        LEAVE,
        COMMAND,
        SENSITIVE_COMMAND,
        CHAT,
        GUI_OPEN,
        GUI_CLOSE,
        GUI_CLICK,
    }
}