package eu.geyskens.pdfscan

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.ui.Modifier
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import eu.geyskens.pdfscan.ui.screens.CameraScreen
import eu.geyskens.pdfscan.ui.screens.CropScreen
import eu.geyskens.pdfscan.ui.screens.ExportScreen
import eu.geyskens.pdfscan.ui.screens.PagesScreen
import eu.geyskens.pdfscan.ui.screens.SettingsScreen
import eu.geyskens.pdfscan.ui.theme.PaperScanTheme

object Routes {
    const val CAMERA = "camera"
    const val PAGES = "pages"
    const val CROP = "crop/{pageId}"
    const val EXPORT = "export"
    const val SETTINGS = "settings"
    fun crop(pageId: String) = "crop/$pageId"
}

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            PaperScanTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background,
                ) {
                    // Activity-scoped so every destination shares one scan session.
                    val vm: ScanViewModel = viewModel()
                    val navController = rememberNavController()

                    NavHost(navController = navController, startDestination = Routes.CAMERA) {
                        composable(Routes.CAMERA) { CameraScreen(vm, navController) }
                        composable(Routes.PAGES) { PagesScreen(vm, navController) }
                        composable(Routes.CROP) { backStack ->
                            CropScreen(
                                vm = vm,
                                navController = navController,
                                pageId = backStack.arguments?.getString("pageId").orEmpty(),
                            )
                        }
                        composable(Routes.EXPORT) { ExportScreen(vm, navController) }
                        composable(Routes.SETTINGS) { SettingsScreen(vm, navController) }
                    }
                }
            }
        }
    }
}
