package dev.ulfrx.recipe

import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import dev.ulfrx.recipe.auth.AuthSession
import dev.ulfrx.recipe.auth.AuthState
import dev.ulfrx.recipe.ui.screens.auth.LoginScreen
import dev.ulfrx.recipe.ui.screens.auth.LoginViewModel
import dev.ulfrx.recipe.ui.screens.auth.SplashScreen
import dev.ulfrx.recipe.ui.screens.shell.AppShell
import dev.ulfrx.recipe.ui.theme.RecipeTheme
import dev.ulfrx.recipe.user.UserRepository
import org.koin.compose.koinInject
import org.koin.compose.viewmodel.koinViewModel

/**
 * Pure routing decision for [App] — facilitates unit testing of the auth gate
 * (V-04 in AppShellGateTest). Maps an [AuthState] + nullable currentUser to one
 * of three top-level branches.
 */
enum class RootRoute { Splash, Login, Shell }

/**
 * Pure helper — returned route is what [App] should render. Two-layer gate:
 * [AuthSession] tells us whether tokens exist; [UserRepository] tells us who
 * the authenticated principal is in the app's data model. While tokens are
 * present but the `/me` fetch hasn't returned yet, we hold on splash so the
 * user never sees an empty post-login screen.
 */
internal fun resolveRootRoute(
    authState: AuthState,
    hasCurrentUser: Boolean,
): RootRoute =
    when (authState) {
        AuthState.Loading -> RootRoute.Splash
        AuthState.Unauthenticated -> RootRoute.Login
        AuthState.Authenticated -> if (hasCurrentUser) RootRoute.Shell else RootRoute.Splash
    }

/**
 * Two-layer gate: [AuthSession] tells us whether tokens exist; [UserRepository]
 * tells us who the authenticated principal is in the app's data model. While
 * tokens are present but the `/me` fetch hasn't returned yet, we hold the
 * splash so the user never sees an empty post-login screen. Phase 3 replaces
 * the `Authenticated + user` branch with `HouseholdGate`.
 */
@Composable
@Preview
fun App() {
    RecipeTheme {
//        val authSession = koinInject<AuthSession>()
//        val userRepository = koinInject<UserRepository>()
//        val authState by authSession.state.collectAsStateWithLifecycle()
//        val currentUser by userRepository.currentUser.collectAsStateWithLifecycle()
//
//        // Kick off the persisted-session restore once. AuthSession.initialize()
//        // refreshes the stored AuthState (or transitions to Unauthenticated on
//        // empty store / refresh failure) and the gate below recomposes accordingly.
//        LaunchedEffect(authSession) {
//            authSession.initialize()
//        }
//
//        when (resolveRootRoute(authState, hasCurrentUser = currentUser != null)) {
//            RootRoute.Splash -> SplashScreen()
//            RootRoute.Login -> LoginScreen(viewModel = koinViewModel<LoginViewModel>())
//            RootRoute.Shell -> AppShell()
//        }
        // for easier tests authentication is turned off
        AppShell()
    }
}
