package com.diegocayo.organizame.presentation.navigation

import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController

@Composable
fun RootNavHost(isAuthenticated: Boolean) {
    val navController = rememberNavController()
    val showBottomBar = rememberSaveable { mutableStateOf(isAuthenticated) }
    val navBackStackEntry by navController.currentBackStackEntryAsState()
    val currentRoute = navBackStackEntry?.destination?.route

    showBottomBar.value = currentRoute in listOf(
        AppScreen.Main.Tasks.route,
        AppScreen.Main.Events.route,
        AppScreen.Main.Subjects.route,
        AppScreen.Main.Profile.route
    )

    Scaffold(
        bottomBar = {
            if (showBottomBar.value) {
                BottomNavigationBar(navController)
            }
        }
    ) { innerPadding ->
        NavHost(
            navController = navController,
            startDestination = if (isAuthenticated) AppScreen.Main.route else AppScreen.Welcome.route,
            modifier = androidx.compose.ui.Modifier.padding(innerPadding)
        ) {
            firstStepNavGraph(navController)
            mainNavGraph(navController)
        }
    }
}