package com.syncflow.ui.navigation

import androidx.compose.runtime.Composable
import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.navArgument
import com.syncflow.ui.addpair.AddPairScreen
import com.syncflow.ui.auth.AccountSetupScreen
import com.syncflow.ui.conflict.ConflictScreen
import com.syncflow.ui.main.MainShell
import com.syncflow.ui.pairdetail.PairDetailScreen

sealed class Screen(val route: String) {
    object Main : Screen("main")
    object AddPair : Screen("add_pair?pairId={pairId}") {
        fun route(pairId: Long? = null) = if (pairId != null) "add_pair?pairId=$pairId" else "add_pair"
    }
    object PairDetail : Screen("pair/{pairId}") {
        fun route(pairId: Long) = "pair/$pairId"
    }
    object Conflicts : Screen("conflicts/{pairId}") {
        fun route(pairId: Long) = "conflicts/$pairId"
    }
    object AddAccount : Screen("add_account")
}

@Composable
fun SyncFlowNavGraph(navController: NavHostController) {
    NavHost(navController = navController, startDestination = Screen.Main.route) {
        composable(Screen.Main.route) {
            MainShell(
                onAddPair = { navController.navigate(Screen.AddPair.route()) },
                onPairClick = { id -> navController.navigate(Screen.PairDetail.route(id)) },
                onAddAccount = { navController.navigate(Screen.AddAccount.route) },
            )
        }
        composable(
            route = "add_pair?pairId={pairId}",
            arguments = listOf(navArgument("pairId") { type = NavType.LongType; defaultValue = -1L }),
        ) {
            AddPairScreen(onDone = { navController.popBackStack() })
        }
        composable(
            route = "pair/{pairId}",
            arguments = listOf(navArgument("pairId") { type = NavType.LongType }),
        ) {
            PairDetailScreen(
                onBack = { navController.popBackStack() },
                onEdit = { id -> navController.navigate(Screen.AddPair.route(id)) },
                onConflicts = { id -> navController.navigate(Screen.Conflicts.route(id)) },
            )
        }
        composable(
            route = "conflicts/{pairId}",
            arguments = listOf(navArgument("pairId") { type = NavType.LongType }),
        ) {
            ConflictScreen(onBack = { navController.popBackStack() })
        }
        composable(Screen.AddAccount.route) {
            AccountSetupScreen(onDone = { navController.popBackStack() })
        }
    }
}
