package dev.ulfrx.recipe.ui.screens.recipesheet

import androidx.compose.runtime.Composable
import dev.ulfrx.recipe.navigation.MealPlanEditorSource
import dev.ulfrx.recipe.navigation.Screen
import dev.ulfrx.recipe.ui.components.sheet.RecipeBottomSheet
import dev.ulfrx.recipe.ui.components.sheet.RecipeBottomSheetState
import dev.ulfrx.recipe.ui.screens.mealplaneditor.MealPlanEditorScreen
import dev.ulfrx.recipe.ui.screens.mealplaneditor.MealPlanEditorViewModel
import dev.ulfrx.recipe.ui.screens.recipedetail.RecipeDetailScreen
import dev.ulfrx.recipe.ui.screens.recipedetail.RecipeDetailViewModel
import org.koin.compose.viewmodel.koinViewModel
import org.koin.core.parameter.parametersOf

@Composable
fun RecipeSheet(state: RecipeBottomSheetState<Screen>) {
    RecipeBottomSheet(state = state) {
        entry<Screen.RecipeDetail> { key ->
            val vm: RecipeDetailViewModel = koinViewModel { parametersOf(key.recipeId) }

            RecipeDetailScreen(
                viewModel = vm,
                onClose = state::dismiss,
                onPlan = { ready ->
                    state.push(
                        Screen.MealPlanEditor.Open(
                            MealPlanEditorSource.NewFromRecipe(
                                recipeId = ready.recipe.id,
                                initialServings = ready.servings,
                                initialSubstitutions = ready.substitutions,
                            ),
                        ),
                    )
                },
            )
        }

        entry<Screen.MealPlanEditor.Open> { key ->
            val vm: MealPlanEditorViewModel = koinViewModel { parametersOf(key.source) }

            MealPlanEditorScreen(
                viewModel = vm,
                onBack = state::pop,
                onConfirm = { _ ->
                    // TODO Phase 6: persist via PlannedMealsRepository
                    state.dismiss()
                },
            )
        }
    }
}
