package com.fabledsword.minstrel.theme

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.fabledsword.minstrel.auth.AuthStore
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import javax.inject.Inject

private const val SHARE_STOP_TIMEOUT_MS = 5_000L

/**
 * Surfaces AuthStore.themeMode as a typed [ThemeMode] StateFlow and
 * lets callers write through. Used by:
 *   - MainActivity's App() to pick the darkOverride to wrap the
 *     NavGraph in.
 *   - SettingsScreen's Appearance section as the 3-way picker.
 *
 * Lives in the theme package rather than a UI package because it's
 * tightly coupled to the theme tokens; SettingsScreen just consumes it.
 */
@HiltViewModel
class ThemePreferenceViewModel @Inject constructor(
    private val authStore: AuthStore,
) : ViewModel() {

    val themeMode: StateFlow<ThemeMode> =
        authStore.themeMode
            .map { ThemeMode.fromWire(it) }
            .stateIn(
                scope = viewModelScope,
                started = SharingStarted.WhileSubscribed(SHARE_STOP_TIMEOUT_MS),
                initialValue = ThemeMode.fromWire(authStore.themeMode.value),
            )

    fun setThemeMode(mode: ThemeMode) {
        // Persist null for SYSTEM so a "never set" row reads identically
        // to an explicit "follow system" pick — keeps the wire/storage
        // round-trip clean.
        authStore.setThemeMode(if (mode == ThemeMode.SYSTEM) null else mode.wire)
    }
}
