package com.prodhack.moscow2025.presentation.navigation

import android.content.Context
import android.os.Bundle
import androidx.compose.material3.SnackbarHostState
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import com.prodhack.moscow2025.presentation.screens.main.MainScreen
import com.prodhack.moscow2025.domain.utils.NetworkError
import com.prodhack.moscow2025.presentation.screens.createResume.CreateResumeScreen
import com.prodhack.moscow2025.presentation.screens.fillProfile.FillProfileScreen
import com.prodhack.moscow2025.presentation.screens.login.LoginScreen
import com.prodhack.moscow2025.presentation.screens.profile.ProfileScreen
import com.prodhack.moscow2025.presentation.screens.register.RegisterScreen
import com.prodhack.moscow2025.presentation.screens.resumeDetails.ResumeDetailsScreen
import com.prodhack.moscow2025.presentation.screens.editResume.EditResumeScreen
import com.prodhack.moscow2025.presentation.screens.resumeHistory.ResumeHistoryScreen
import com.prodhack.moscow2025.presentation.screens.diffScreen.ResumeDiffScreen
import com.prodhack.moscow2025.presentation.utils.ErrorCallbacks
import com.prodhack.moscow2025.presentation.utils.ErrorCollectorScope

@Composable
fun TTasksNavHost(
	navController: NavHostController,
	modifier: Modifier = Modifier,
	sessionDestination: AppDestination? = null,
	context: Context,
	requestNotifyPermissions: () -> Unit,
	snackbarHostState: SnackbarHostState
) {
	val startDestination = sessionDestination?.route ?: AppDestination.Login.route

	ErrorCollectorScope(context, navController, object : ErrorCallbacks {
		override fun processConnectionError(networkError: NetworkError.Connection) {

		}

		override fun processUnexpectedError(networkError: NetworkError.Unexpected) {

		}

	}) {
		NavHost(
			navController = navController,
			startDestination = startDestination,
			modifier = modifier
		) {
			composable(AppDestination.Login.route) {
				LoginScreen(
					snackbarHostState = snackbarHostState,
					onRegisterClick = {
						navController.navigate(AppDestination.Register.route)
					},
					onSuccess = {
						navController.navigate(AppDestination.Main.route) {
							popUpTo(AppDestination.Login.route) {
								inclusive = true
							}
						}
					}
				)
			}

			composable(AppDestination.Register.route) {
				RegisterScreen(
					snackbarHostState = snackbarHostState,
					onLoginClick = {
						navController.popBackStack()
					},
					onSuccess = {
						navController.navigate(AppDestination.FillProfile.route) {
							popUpTo(AppDestination.Register.route) {
								inclusive = true
							}
						}
					}
				)
			}

			composable(AppDestination.FillProfile.route) {
				FillProfileScreen(
					snackbarHostState = snackbarHostState,
					onSuccess = {
						navController.navigate(AppDestination.Main.route) {
							popUpTo(AppDestination.FillProfile.route) {
								inclusive = true
							}
						}
					}
				)
			}

			composable(AppDestination.Main.route) {
				MainScreen(
					openResumeDetails = { id ->
						navController.navigate(AppDestination.ResumeDetails.route, Bundle().apply {
							putString(AppDestination.ResumeDetails.ARG_ID, id)
						})
					}, openCreateResume = {
						navController.navigate(AppDestination.ResumeCreation.route)
					},
					requestNotifyPermissions = requestNotifyPermissions
				)
			}

			composable(AppDestination.Profile.route)
			{
				ProfileScreen(
					snackbarHostState = snackbarHostState,
					navigateToLoginScreen = {
						navController.navigate(AppDestination.Login.route)
					}
				)
			}

			composable(AppDestination.ResumeDetails.route) {
				ResumeDetailsScreen(navBackStackEntry = it)
			}

			composable(AppDestination.ResumeCreation.route) {
				CreateResumeScreen({ navController.popBackStack() }, openResumeDetails = { id ->
					navController.navigate(AppDestination.ResumeDetails.route, Bundle().apply {
						putString(AppDestination.ResumeDetails.ARG_ID, id)
					})
				})
			}

			composable(AppDestination.ResumeEdit.route) {
				EditResumeScreen(navBackStackEntry = it, goBack = {
					navController.popBackStack()
				}, openResumeDetails = { id ->
					navController.navigate(AppDestination.ResumeDetails.route, Bundle().apply {
						putString(AppDestination.ResumeDetails.ARG_ID, id)
					})
				})
			}

			composable(AppDestination.ResumeHistory.route) {
				ResumeHistoryScreen(navBackStackEntry = it) {
					navController.popBackStack()
				}
			}

			composable(AppDestination.ResumeDiff.route) {
				ResumeDiffScreen(navBackStackEntry = it)
			}
		}
	}
}
