/*
 *   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.BauweltMemberTable.bauweltId
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
import org.jetbrains.exposed.v1.jdbc.insertIgnore
import java.util.*

object BauweltMemberTable : CompositeIdTable("BauweltMember") {
    val bauweltId = reference("BauweltID", SteamwarUserTable).index()
    val memberId = reference("MemberID", SteamwarUserTable).index()
    val build = bool("Build")
    val worldEdit = bool("WorldEdit")
    val world = bool("World")

    override val primaryKey = PrimaryKey(bauweltId, memberId)

    init {
        addIdColumn(bauweltId)
        addIdColumn(memberId)
    }
}

class BauweltMember(id: EntityID<CompositeID>) : CompositeEntity(id) {
    companion object : CompositeEntityClass<BauweltMember>(BauweltMemberTable) {
        private val cache = mutableMapOf<Int, BauweltMember>()

        private fun cache(member: BauweltMember) =
            cache.put(member.memberID, member)

        @JvmStatic
        fun clear() =
            cache.clear()

        @JvmStatic
        @Deprecated("Use addMember(ownerId: Int, newMemberId: Int)")
        fun addMember(ownerId: UUID, newMemberId: UUID) =
            addMember(SteamwarUser.get(ownerId)!!.id, SteamwarUser.get(newMemberId)!!.id)

        @JvmStatic
        fun addMember(ownerId: Int, newMemberId: Int) =
            addMember(EntityID(ownerId, SteamwarUserTable), EntityID(newMemberId, SteamwarUserTable))

        fun addMember(ownerId: EntityID<Int>, newMemberId: EntityID<Int>) =
            useDb {
                BauweltMemberTable.insertIgnore {
                    it[bauweltId] = ownerId
                    it[memberId] = newMemberId
                    it[build] = false
                    it[worldEdit] = false
                    it[world] = false
                }
            }

        @JvmStatic
        @Deprecated("Use getBauMember(bauwelt: Int, member: Int)")
        fun getBauMember(bauwelt: UUID, member: UUID) =
            useDb {
                find { (bauweltId eq SteamwarUser.get(bauwelt)!!.id) and (BauweltMemberTable.memberId eq SteamwarUser.get(member)!!.id) }.firstOrNull()?.also { cache(it) }
            }

        @JvmStatic
        fun getBauMember(bauwelt: Int, member: Int) =
            useDb {
                find { (bauweltId eq bauwelt) and (BauweltMemberTable.memberId eq member) }.firstOrNull()?.also { cache(it) }
            }

        @JvmStatic
        @Deprecated("Use getMembers(bauwelt: Int)")
        fun getMembers(bauwelt: UUID) =
            getMembers(SteamwarUser.get(bauwelt)!!.id.value)

        @JvmStatic
        fun getMembers(bauwelt: Int) =
            useDb {
                find { bauweltId eq bauwelt }.toList().also { it.forEach { cache(it) } }
            }
    }

    val bauweltID by BauweltMemberTable.bauweltId.transform({ EntityID(it, SteamwarUserTable) }, { it.value })
    val memberID by BauweltMemberTable.memberId.transform({ EntityID(it, SteamwarUserTable) }, { it.value })
    private var worldEditInternal by BauweltMemberTable.worldEdit
    var worldEdit: Boolean
        get() = worldEditInternal
        set(value) = useDb {
            worldEditInternal = value
        }
    private var buildInternal by BauweltMemberTable.build
    var build: Boolean
        get() = buildInternal
        set(value) = useDb {
            buildInternal = value
        }
    private var worldInternal by BauweltMemberTable.world
    var world: Boolean
        get() = worldInternal
        set(value) = useDb {
            worldInternal = value
        }
    var supervisor: Boolean
        get() = world
        set(value) = useDb {
            world = value
        }

    fun isBuild() =
        build

    fun isSupervisor() =
        world

    fun isWorldEdit() =
        build

    fun isWorld() =
        world

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