package com.carmaintenance.ui.navigation

import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.navArgument
import com.carmaintenance.ui.screen.LoginScreen
import com.carmaintenance.ui.screen.admin.AdminDashboardScreen
import com.carmaintenance.ui.screen.customer.CustomerDashboardScreen
import com.carmaintenance.ui.screen.customer.CustomerAppointmentsScreen
import com.carmaintenance.ui.screen.customer.CustomerOrdersScreen
import com.carmaintenance.ui.screen.customer.CustomerVehiclesScreen
import com.carmaintenance.ui.viewmodel.AuthViewModel
import com.carmaintenance.ui.viewmodel.AuthUiState

@Composable
fun CarMaintenanceNavGraph(
    navController: NavHostController,
    authViewModel: AuthViewModel = hiltViewModel()
) {
    val uiState by authViewModel.uiState.collectAsState()

    LaunchedEffect(uiState) {
        when (uiState) {
            is AuthUiState.LoggedIn -> {
                val user = (uiState as AuthUiState.LoggedIn).user
                val route = when (user.role) {
                    "admin" -> Screen.AdminDashboard.route
                    "staff" -> Screen.StaffDashboard.route
                    "customer" -> Screen.CustomerDashboard.route
                    else -> Screen.Login.route
                }
                navController.navigate(route) {
                    popUpTo(Screen.Login.route) { inclusive = true }
                }
            }
            is AuthUiState.NotLoggedIn -> {
                navController.navigate(Screen.Login.route) {
                    popUpTo(0) { inclusive = true }
                }
            }
            else -> {}
        }
    }

    NavHost(
        navController = navController,
        startDestination = Screen.Login.route
    ) {
        composable(Screen.Login.route) {
            LoginScreen(
                onLoginSuccess = {
                    // 导航由LaunchedEffect处理
                }
            )
        }

        composable(Screen.CustomerDashboard.route) {
            CustomerDashboardScreen(
                onNavigateBack = { authViewModel.logout() }
            )
        }

        composable(Screen.CustomerVehicles.route) {
            CustomerVehiclesScreen(
                onNavigateBack = { navController.popBackStack() }
            )
        }

        composable(Screen.CustomerOrders.route) {
            CustomerOrdersScreen(
                onNavigateBack = { navController.popBackStack() }
            )
        }

        composable(Screen.CustomerAppointments.route) {
            CustomerAppointmentsScreen(
                onNavigateBack = { navController.popBackStack() }
            )
        }

        composable(Screen.AdminDashboard.route) {
            AdminDashboardScreen(
                onNavigateBack = { authViewModel.logout() }
            )
        }

        composable(Screen.StaffDashboard.route) {
            // TODO: 实现工作人员仪表板
        }
    }
}
