package de.tsschulz.tt_tagebuch.shared.api.models

import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.booleanOrNull
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive

private fun JsonObject.boolAt(module: String, key: String): Boolean {
    val mod = this[module]?.jsonObject ?: return false
    return mod[key]?.jsonPrimitive?.booleanOrNull == true
}

/** Vereinsbesitzer haben volle Rechte. */
fun UserClubPermissions.canReadDiary(): Boolean {
    if (isOwner) return true
    return permissions.boolAt("diary", "read")
}

fun UserClubPermissions.canWriteDiary(): Boolean {
    if (isOwner) return true
    return permissions.boolAt("diary", "write")
}

fun UserClubPermissions.canReadMembers(): Boolean {
    if (isOwner) return true
    return permissions.boolAt("members", "read")
}

fun UserClubPermissions.canWriteMembers(): Boolean {
    if (isOwner) return true
    return permissions.boolAt("members", "write")
}

fun UserClubPermissions.canReadSchedule(): Boolean {
    if (isOwner) return true
    return permissions.boolAt("schedule", "read")
}

fun UserClubPermissions.canWriteSchedule(): Boolean {
    if (isOwner) return true
    return permissions.boolAt("schedule", "write")
}

fun UserClubPermissions.canReadApprovals(): Boolean {
    if (isOwner) return true
    if (role.equals("admin", ignoreCase = true)) return true
    return permissions.boolAt("approvals", "read")
}

fun UserClubPermissions.canWriteApprovals(): Boolean {
    if (isOwner) return true
    return permissions.boolAt("approvals", "write")
}

/** Lesen der Berechtigungsverwaltung (Modul `permissions` im Backend). */
fun UserClubPermissions.canReadClubPermissions(): Boolean {
    if (isOwner) return true
    return permissions.boolAt("permissions", "read")
}

fun UserClubPermissions.canWriteClubPermissions(): Boolean {
    if (isOwner) return true
    return permissions.boolAt("permissions", "write")
}

fun UserClubPermissions.canReadTeams(): Boolean {
    if (isOwner) return true
    return permissions.boolAt("teams", "read")
}

fun UserClubPermissions.canWriteTeams(): Boolean {
    if (isOwner) return true
    return permissions.boolAt("teams", "write")
}

fun UserClubPermissions.canReadTournaments(): Boolean {
    if (isOwner) return true
    return permissions.boolAt("tournaments", "read")
}

fun UserClubPermissions.canWriteTournaments(): Boolean {
    if (isOwner) return true
    return permissions.boolAt("tournaments", "write")
}

fun UserClubPermissions.canReadClubSettings(): Boolean {
    if (isOwner) return true
    return permissions.boolAt("settings", "read")
}

fun UserClubPermissions.canWriteClubSettings(): Boolean {
    if (isOwner) return true
    return permissions.boolAt("settings", "write")
}

fun UserClubPermissions.canReadPredefinedActivities(): Boolean {
    if (isOwner) return true
    return permissions.boolAt("predefined_activities", "read")
}

fun UserClubPermissions.canWritePredefinedActivities(): Boolean {
    if (isOwner) return true
    return permissions.boolAt("predefined_activities", "write")
}

fun UserClubPermissions.canReadStatistics(): Boolean {
    if (isOwner) return true
    return permissions.boolAt("statistics", "read")
}

fun UserClubPermissions.canWriteMyTischtennis(): Boolean {
    if (isOwner) return true
    return permissions.boolAt("mytischtennis", "write")
}
