/*
 *   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.and
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

object PersonalKitTable : CompositeIdTable("PersonalKit") {
    val userId = reference("UserId", SteamwarUserTable)
    val gamemode = varchar("Gamemode", 64).entityId()
    val kitName = varchar("Name", 64).entityId()
    val inventory = text("Inventory", eagerLoading = true)
    val armor = text("Armor", eagerLoading = true)
    val inUse = bool("InUse")

    override val primaryKey = PrimaryKey(userId, gamemode, kitName)

    init {
        addIdColumn(userId)
        index(false, userId, gamemode)
    }
}

class InternalKit(id: EntityID<CompositeID>) : CompositeEntity(id) {
    companion object : CompositeEntityClass<InternalKit>(PersonalKitTable) {
        @JvmStatic
        fun get(userId: Int, gamemode: String) =
            useDb {
                find { PersonalKitTable.userId eq userId and (PersonalKitTable.gamemode eq gamemode) }
                    .toList()
            }

        @JvmStatic
        fun get(userId: Int, gamemode: String, kitName: String) =
            useDb {
                findById(CompositeID {
                    it[PersonalKitTable.userId] = EntityID(userId, SteamwarUserTable)
                    it[PersonalKitTable.gamemode] = gamemode
                    it[PersonalKitTable.kitName] = kitName
                })
            }

        @JvmStatic
        fun create(userId: Int, gamemode: String, kitName: String, rawInventory: String, rawArmor: String) =
            useDb {
                new(
                    CompositeID {
                        it[PersonalKitTable.userId] = EntityID(userId, SteamwarUserTable)
                        it[PersonalKitTable.gamemode] = gamemode
                        it[PersonalKitTable.kitName] = kitName
                    }
                ) {
                    this.rawInventory = rawInventory
                    this.rawArmor = rawArmor
                    this.inUse = false
                }.also { it.setDefault() }
            }

        @JvmStatic
        fun getKitInUse(userId: Int, gamemode: String) =
            useDb {
                find { PersonalKitTable.userId eq userId and (PersonalKitTable.gamemode eq gamemode) and (PersonalKitTable.inUse eq true) }
                    .firstOrNull()
            }
    }

    var userID by PersonalKitTable.userId.transform({ EntityID(it, SteamwarUserTable) }, { it.value })
        private set
    private var gameMode by PersonalKitTable.gamemode
    val gamemode: String
        get() = gameMode.value

    var kitName by PersonalKitTable.kitName
        private set
    val name: String
        get() = kitName.value

    var rawInventory by PersonalKitTable.inventory
        private set
    var rawArmor by PersonalKitTable.armor
        private set
    var inUse by PersonalKitTable.inUse
        private set

    var inventory: String
        get() = rawInventory
        set(value) = useDb { rawInventory = value }
    var armor: String
        get() = rawArmor
        set(value) = useDb { rawArmor = value }

    fun setDefault() =
        useDb {
            find { PersonalKitTable.userId eq userID and (PersonalKitTable.gamemode eq gameMode) and (PersonalKitTable.inUse eq true) }
                .filter { it.id.value != this@InternalKit.id.value }
                .forEach { it.inUse = false }
            inUse = true
        }

    override fun delete() =
        useDb {
            super.delete()
        }
}