/*
 *   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.SortOrder
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 ModTable : CompositeIdTable("Mods") {
    val platform = enumeration("Platform", Mod.Platform::class)
    val modName = varchar("ModName", 100)
    val modeType = enumerationByName("ModType", 10, Mod.ModType::class)
}

class Mod(id: EntityID<CompositeID>) : CompositeEntity(id) {
    companion object : CompositeEntityClass<Mod>(ModTable) {
        @JvmStatic
        fun get(modName: String, platform: Platform) =
            useDb {
                find { ModTable.platform eq platform and (ModTable.modName eq modName) }.firstOrNull()
            }

        @JvmStatic
        fun getOrCreate(modName: String, platform: Platform) =
            useDb {
                get(modName, platform)
                    ?: new {
                        this.platform = platform
                        this.modName = modName
                        this.type = ModType.UNKLASSIFIED
                    }
            }

        @JvmStatic
        fun getAllModsFiltered(page: Int, elementsPerPage: Int, filter: ModType) =
            useDb {
                find { ModTable.modeType eq filter }.limit(elementsPerPage).offset((elementsPerPage * page).toLong())
                    .orderBy(
                        ModTable.modName to SortOrder.DESC
                    ).toList()
            }

        @JvmStatic
        fun findFirstMod() =
            useDb {
                find { ModTable.modeType eq ModType.UNKLASSIFIED }.limit(1).firstOrNull()
            }
    }

    var platform by ModTable.platform
    var modName by ModTable.modName
    private var type by ModTable.modeType
    var modType: ModType
        get() = type
        set(value) = useDb { type = value }

    enum class Platform {
        FORGE,
        LABYMOD,
        FABRIC
    }

    enum class ModType(val colorCode: String) {
        UNKLASSIFIED("7"),
        GREEN("a"),
        YELLOW("e"),
        RED("c"),
        YOUTUBER_ONLY("6");
    }
}