package com.anypreta.bountyboard.data

import com.anypreta.bountyboard.data.model.User
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow

/** Holds the authenticated user + derived auth state for the whole app. */
class SessionManager {

    private val _user = MutableStateFlow<User?>(null)
    val user: StateFlow<User?> = _user.asStateFlow()

    private val _mustChangePassword = MutableStateFlow(false)
    val mustChangePassword: StateFlow<Boolean> = _mustChangePassword.asStateFlow()

    private val _oidcEnabled = MutableStateFlow(false)
    val oidcEnabled: StateFlow<Boolean> = _oidcEnabled.asStateFlow()

    fun setSession(user: User, mustChange: Boolean, oidc: Boolean = _oidcEnabled.value) {
        _user.value = user
        _mustChangePassword.value = mustChange
        _oidcEnabled.value = oidc
    }

    fun setMustChange(value: Boolean) {
        _mustChangePassword.value = value
    }

    fun setOidcEnabled(value: Boolean) {
        _oidcEnabled.value = value
    }

    fun clear() {
        _user.value = null
        _mustChangePassword.value = false
    }
}
