package com.garfiec.librechat.shared.navigation

import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.slideInHorizontally
import androidx.compose.animation.slideOutHorizontally
import androidx.compose.animation.togetherWith
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.lifecycle.compose.collectAsStateWithLifecycle

/**
 * Sidebar scaffold that switches between Conversations and Settings modes.
 * Uses [AnimatedContent] for a smooth horizontal slide transition.
 */
@Composable
fun SidebarScaffold(
    viewModel: NavHostViewModel,
    onNewChat: () -> Unit,
    onConversationClick: (String) -> Unit,
    onSettingsClick: () -> Unit,
    onSettingsCategorySelected: (SettingsCategory) -> Unit,
    onAgentsClick: () -> Unit,
    onFilesClick: () -> Unit,
    modifier: Modifier = Modifier,
) {
    val sidebarMode by viewModel.sidebarMode.collectAsStateWithLifecycle()
    val selectedCategory by viewModel.selectedSettingsCategory.collectAsStateWithLifecycle()

    AnimatedContent(
        targetState = sidebarMode,
        modifier = modifier,
        transitionSpec = {
            if (targetState is SidebarMode.Settings) {
                slideInHorizontally { fullWidth -> fullWidth } togetherWith
                    slideOutHorizontally { fullWidth -> -fullWidth }
            } else {
                slideInHorizontally { fullWidth -> -fullWidth } togetherWith
                    slideOutHorizontally { fullWidth -> fullWidth }
            }
        },
        label = "SidebarModeTransition",
    ) { mode ->
        when (mode) {
            is SidebarMode.Conversations -> {
                DrawerContent(
                    viewModel = viewModel,
                    onNewChat = onNewChat,
                    onConversationClick = onConversationClick,
                    onSettingsClick = onSettingsClick,
                    onAgentsClick = onAgentsClick,
                    onFilesClick = onFilesClick,
                )
            }
            is SidebarMode.Settings -> {
                SettingsSidebarContent(
                    selectedCategory = selectedCategory,
                    onBackToConversations = {
                        viewModel.setSidebarMode(SidebarMode.Conversations)
                    },
                    onCategorySelected = { category ->
                        viewModel.selectSettingsCategory(category)
                        onSettingsCategorySelected(category)
                    },
                )
            }
        }
    }
}
