package de.tsschulz.tt_tagebuch.shared.api

/**
 * Rollen-Standardrechte (analog [backend/services/permissionService.js] ROLE_PERMISSIONS)
 * für die mobile Berechtigungs-UI (Erbe / explizit erlauben / explizit verbieten).
 */
object RolePermissionMatrix {

    private val admin = resourceMap(
        diary = triple(true, true, true),
        members = triple(true, true, true),
        teams = triple(true, true, true),
        schedule = triple(true, true, true),
        tournaments = triple(true, true, true),
        statistics = pair(true, true),
        settings = pair(true, true),
        permissions = pair(true, true),
        approvals = pair(true, true),
        mytischtennis_admin = pair(true, true),
        predefined_activities = triple(true, true, true),
    )

    private val trainer = resourceMap(
        diary = triple(true, true, true),
        members = triple(true, true, false),
        teams = triple(true, true, false),
        schedule = triple(true, false, false),
        tournaments = triple(true, true, false),
        statistics = pair(true, false),
        settings = pair(false, false),
        permissions = pair(false, false),
        approvals = pair(false, false),
        mytischtennis_admin = pair(false, false),
        predefined_activities = triple(true, true, true),
    )

    private val teamManager = resourceMap(
        diary = triple(false, false, false),
        members = triple(true, false, false),
        teams = triple(true, true, false),
        schedule = triple(true, true, false),
        tournaments = triple(true, false, false),
        statistics = pair(true, false),
        settings = pair(false, false),
        permissions = pair(false, false),
        approvals = pair(false, false),
        mytischtennis_admin = pair(false, false),
        predefined_activities = triple(false, false, false),
    )

    private val tournamentManager = resourceMap(
        diary = triple(false, false, false),
        members = triple(true, false, false),
        teams = triple(false, false, false),
        schedule = triple(false, false, false),
        tournaments = triple(true, true, false),
        statistics = pair(true, false),
        settings = pair(false, false),
        permissions = pair(false, false),
        approvals = pair(false, false),
        mytischtennis_admin = pair(false, false),
        predefined_activities = triple(false, false, false),
    )

    private val member = resourceMap(
        diary = triple(false, false, false),
        members = triple(false, false, false),
        teams = triple(false, false, false),
        schedule = triple(false, false, false),
        tournaments = triple(false, false, false),
        statistics = pair(true, false),
        settings = pair(false, false),
        permissions = pair(false, false),
        approvals = pair(false, false),
        mytischtennis_admin = pair(false, false),
        predefined_activities = triple(false, false, false),
    )

    private fun triple(r: Boolean, w: Boolean, d: Boolean): Map<String, Boolean> =
        mapOf("read" to r, "write" to w, "delete" to d)

    private fun pair(r: Boolean, w: Boolean): Map<String, Boolean> =
        mapOf("read" to r, "write" to w)

    private fun resourceMap(
        diary: Map<String, Boolean>,
        members: Map<String, Boolean>,
        teams: Map<String, Boolean>,
        schedule: Map<String, Boolean>,
        tournaments: Map<String, Boolean>,
        statistics: Map<String, Boolean>,
        settings: Map<String, Boolean>,
        permissions: Map<String, Boolean>,
        approvals: Map<String, Boolean>,
        mytischtennis_admin: Map<String, Boolean>,
        predefined_activities: Map<String, Boolean>,
    ): Map<String, Map<String, Boolean>> = mapOf(
        "diary" to diary,
        "members" to members,
        "teams" to teams,
        "schedule" to schedule,
        "tournaments" to tournaments,
        "statistics" to statistics,
        "settings" to settings,
        "permissions" to permissions,
        "approvals" to approvals,
        "mytischtennis_admin" to mytischtennis_admin,
        "predefined_activities" to predefined_activities,
    )

    fun defaultsForRole(role: String): Map<String, Map<String, Boolean>> =
        when (role) {
            "admin" -> admin
            "trainer" -> trainer
            "team_manager" -> teamManager
            "tournament_manager" -> tournamentManager
            else -> member
        }

    fun defaultAction(role: String, resource: String, action: String): Boolean =
        defaultsForRole(role)[resource]?.get(action) ?: false
}
