package com.syaroful.agrilinkvocpro.navigation

import androidx.compose.runtime.Composable
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import com.syaroful.agrilinkvocpro.data.UserPreferences
import com.syaroful.agrilinkvocpro.presentation.screen.detail.DetailScreen
import com.syaroful.agrilinkvocpro.presentation.screen.home.DynamicModuleViewModel
import com.syaroful.agrilinkvocpro.presentation.screen.home.HomeScreen
import com.syaroful.agrilinkvocpro.presentation.screen.login.LoginScreen
import com.syaroful.agrilinkvocpro.presentation.screen.login.LoginViewModel
import com.syaroful.agrilinkvocpro.presentation.screen.profile.ProfileScreen
import com.syaroful.agrilinkvocpro.presentation.screen.register.RegisterScreen
import com.syaroful.agrilinkvocpro.presentation.screen.splash.SplashScreen
import org.koin.androidx.compose.koinViewModel

@Composable
fun SetupNavigation(
    userPreferences: UserPreferences,
    dynamicModuleViewModel: DynamicModuleViewModel,
    onLaunchFeature: (String) -> Unit
) {

    val loginViewModel: LoginViewModel = koinViewModel()

    val navController = rememberNavController()
    NavHost(navController, startDestination = "splash") {
        composable("splash") {
            SplashScreen(
                userPreferences = userPreferences,
                onNavigate = { destination ->
                    navController.navigate(destination) {
                        popUpTo("splash") { inclusive = true }
                    }
                }
            )
        }
        composable("login") {
            LoginScreen(
                loginViewModel = loginViewModel,
                onLoginSuccess = {
                    navController.navigate("home") {
                        popUpTo("login") { inclusive = true }
                    }
                },
                onNavigateToRegister = {
                    navController.navigate("register") {
                        popUpTo("register") { inclusive = true }
                    }
                }
            )
        }
        composable("register") {
            RegisterScreen(
                onLoginSuccess = {
                    navController.navigate("login") {
                        popUpTo("login") { inclusive = true }
                    }
                },
                onNavigateToLogin = {
                    navController.navigate("login") {
                        popUpTo("login") { inclusive = true }
                    }
                }
            )
        }
        composable("home") {
            HomeScreen(
                navController = navController,
                onFeatureClick = { moduleName ->
                    dynamicModuleViewModel.onFeatureClicked(moduleName, onLaunchFeature)
                },
                dynamicModuleViewModel = dynamicModuleViewModel
            )
        }
        composable("profile") {
            ProfileScreen(
                onNavigateToLogin = {
                    navController.navigate("splash") {
                        popUpTo("splash") { inclusive = true }
                    }
                }
            )
        }
        composable(
            route = "detail-screen/{sensorId}",
            arguments = listOf(navArgument("sensorId") { type = NavType.StringType })
        ) { backStackEntry ->
            val sensorId = backStackEntry.arguments?.getString("sensorId") ?: "npk1"
            DetailScreen(sensorId = sensorId)
        }
    }
}