@file:Suppress("AssignedValueIsNeverRead")

package eu.gaudian.translator.view.dictionary

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Card
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.ExposedDropdownMenuAnchorType
import androidx.compose.material3.ExposedDropdownMenuBox
import androidx.compose.material3.ExposedDropdownMenuDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ModalBottomSheet
import androidx.compose.material3.ProgressIndicatorDefaults
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.rememberModalBottomSheetState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import eu.gaudian.translator.R
import eu.gaudian.translator.model.DictionaryEntry
import eu.gaudian.translator.model.Language
import eu.gaudian.translator.model.communication.FileInfo
import eu.gaudian.translator.view.composable.AppButton
import eu.gaudian.translator.view.composable.AppCard
import eu.gaudian.translator.view.composable.AppIcons
import eu.gaudian.translator.view.composable.AppOutlinedButton
import eu.gaudian.translator.view.composable.AppOutlinedTextField
import eu.gaudian.translator.view.composable.DictionaryLanguageDropDown
import eu.gaudian.translator.view.composable.OptionItemSwitch
import eu.gaudian.translator.viewmodel.DictionaryViewModel
import eu.gaudian.translator.viewmodel.LanguageViewModel
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

// Main Dictionary Screen Component
@Composable
fun DictionaryScreenContent(
    navController: NavController,
    onEntryClick: (DictionaryEntry) -> Unit,
    dictionaryViewModel: DictionaryViewModel,
    languageViewModel: LanguageViewModel,
    onNavigateToOptions: () -> Unit
) {
    // Navigation effect
    val entryToNavigate by dictionaryViewModel.navigateToEntry.collectAsState()
    LaunchedEffect(entryToNavigate) {
        entryToNavigate?.let { entry ->
            onEntryClick(entry)
            dictionaryViewModel.onNavigationDone()
        }
    }

    LaunchedEffect(Unit) {
        dictionaryViewModel.fetchManifest()
    }

    var showHistorySheet by remember { mutableStateOf(false) }
    val sheetState = rememberModalBottomSheetState()
    var searchQuery by remember { mutableStateOf("") }
    val searchHistory by dictionaryViewModel.searchHistoryEntries.collectAsState(initial = emptyList())
    val uiState by dictionaryViewModel.uiState.collectAsState()
    val wordOfTheDay by dictionaryViewModel.wordOfTheDay.collectAsState()
    val selectedLanguage by languageViewModel.selectedDictionaryLanguage.collectAsState()
    var useDownloaded by remember { mutableStateOf(false) }

    // Update local dictionary availability when selected language changes
    LaunchedEffect(selectedLanguage) {
        selectedLanguage?.let { lang ->
            dictionaryViewModel.updateLocalDictAvailability(lang.code.lowercase())
        }
    }

    LazyColumn(
        modifier = Modifier.padding(8.dp),
        verticalArrangement = Arrangement.spacedBy(8.dp)
    ) {
        item {
            DictionarySearchCard(
                searchQuery = searchQuery,
                onSearchQueryChange = { searchQuery = it },
                selectedLanguage = selectedLanguage,
                languageViewModel = languageViewModel,
                dictionaryViewModel = dictionaryViewModel,
                useDownloaded = useDownloaded,
                onUseDownloadedChange = { useDownloaded = it },
                uiState = uiState,
                onNavigateToOptions = onNavigateToOptions,
                onShowHistory = { showHistorySheet = true },
                onOriginClick = {
                    if (searchQuery.isNotBlank() && selectedLanguage != null) {
                        @Suppress("HardCodedStringLiteral")
                        navController.navigate("etymology_result/${searchQuery}/${selectedLanguage!!.nameResId}")
                    }
                },
                onDefinitionClick = {
                    if (searchQuery.isNotBlank() && selectedLanguage != null) {
                        dictionaryViewModel.performSearch(searchQuery, selectedLanguage!!, useDownloaded = useDownloaded)
                    }
                }
            )
        }

        wordOfTheDay?.let { entry ->
            item {
                WordOfTheDayCard(
                    entry = entry,
                    isLoading = uiState.isLoading,
                    onEntryClick = { onEntryClick(it) },
                    onRefresh = dictionaryViewModel::refreshWordOfTheDay
                )
            }
        }

        item {
            DictionaryManagerSection(
                dictionaryViewModel = dictionaryViewModel
            )
        }
    }

    if (showHistorySheet) {
        ModalBottomSheet(
            onDismissRequest = { showHistorySheet = false },
            sheetState = sheetState
        ) {
            DictionaryHistorySheetContent(
                historyEntries = searchHistory,
                onEntryClick = {
                    onEntryClick(it)
                    showHistorySheet = false
                }
            )
        }
    }
}

// --- Component 1: Search Card ---

@Composable
fun DictionarySearchCard(
    searchQuery: String,
    onSearchQueryChange: (String) -> Unit,
    selectedLanguage: Language?,
    languageViewModel: LanguageViewModel,
    dictionaryViewModel: DictionaryViewModel,
    useDownloaded: Boolean,
    onUseDownloadedChange: (Boolean) -> Unit,
    uiState: DictionaryViewModel.DictionaryUiState,
    onNavigateToOptions: () -> Unit,
    onShowHistory: () -> Unit,
    onOriginClick: () -> Unit,
    onDefinitionClick: () -> Unit
) {
    AppCard {
        Column(
            modifier = Modifier.padding(16.dp),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            SearchFieldWithSuggestions(
                searchQuery = searchQuery,
                selectedLanguage = selectedLanguage,
                onSearchQueryChange = onSearchQueryChange,
                dictionaryViewModel = dictionaryViewModel
            )

            Spacer(modifier = Modifier.height(8.dp))

            // Using ViewModel's isLocalDictAvailable StateFlow
            val isLocalDictAvailable by dictionaryViewModel.isLocalDictAvailable.collectAsState()

            LanguageSelectionRow(
                languageViewModel = languageViewModel,
                hasDictionary = isLocalDictAvailable,
                useDownloaded = useDownloaded,
                onUseDownloadedChange = onUseDownloadedChange
            )

            Spacer(modifier = Modifier.height(8.dp))

            SearchActionRow(
                onNavigateToOptions = onNavigateToOptions,
                onShowHistory = onShowHistory,
                onOriginClick = onOriginClick,
                onDefinitionClick = onDefinitionClick,
                isSearchEnabled = searchQuery.isNotBlank(),
                isLoading = uiState.isLoading
            )
        }
    }
}

@Composable
private fun SearchFieldWithSuggestions(
    searchQuery: String,
    selectedLanguage: Language?,
    onSearchQueryChange: (String) -> Unit,
    dictionaryViewModel: DictionaryViewModel
) {
    var expanded by remember { mutableStateOf(false) }
    var suggestions by remember { mutableStateOf(emptyList<String>()) }
    val scope = rememberCoroutineScope()
    var lastJob by remember { mutableStateOf<Job?>(null) }

    // Observe suggestions from ViewModel
    val vmSuggestions by dictionaryViewModel.suggestions.collectAsState()
    LaunchedEffect(vmSuggestions) {
        suggestions = vmSuggestions
        expanded = suggestions.isNotEmpty()
    }

    ExposedDropdownMenuBox(
        expanded = expanded && suggestions.isNotEmpty(),
        onExpandedChange = { expanded = !expanded }
    ) {
        AppOutlinedTextField(
            value = searchQuery,
            onValueChange = {
                onSearchQueryChange(it)
                lastJob?.cancel()
                lastJob = scope.launch {
                    delay(100)
                    if (searchQuery.length >= 3 && selectedLanguage != null) {
                        dictionaryViewModel.fetchSuggestions(searchQuery, selectedLanguage.code.lowercase(), 5)
                    } else {
                        dictionaryViewModel.clearSuggestions()
                    }
                }
            },
            label = { Text(stringResource(R.string.cd_search)) },
            modifier = Modifier
                .fillMaxWidth()
                .menuAnchor(ExposedDropdownMenuAnchorType.PrimaryEditable),
            trailingIcon = { ExposedDropdownMenuDefaults.TrailingIcon(expanded = expanded) }
        )
        if (suggestions.isNotEmpty()) {
            ExposedDropdownMenu(
                expanded = expanded,
                onDismissRequest = { expanded = false }
            ) {
                suggestions.forEach { word ->
                    DropdownMenuItem(
                        text = { Text(word) },
                        onClick = {
                            dictionaryViewModel.setNavigatingFromDictionaryResult(false)
                            if (selectedLanguage != null) {
                                dictionaryViewModel.performSearch(word, selectedLanguage)
                            }
                        }
                    )
                }
            }
        }
    }
}

@Composable
private fun LanguageSelectionRow(
    languageViewModel: LanguageViewModel,
    hasDictionary: Boolean,
    useDownloaded: Boolean,
    onUseDownloadedChange: (Boolean) -> Unit
) {
    Column {
        Row(
            modifier = Modifier.fillMaxWidth(),
            verticalAlignment = Alignment.CenterVertically
        ) {
            DictionaryLanguageDropDown(
                languageViewModel = languageViewModel,
                modifier = Modifier.weight(1f)
            )
        }
        if (hasDictionary) {
            Row(
                modifier = Modifier.fillMaxWidth(),
                verticalAlignment = Alignment.CenterVertically
            ) {
                OptionItemSwitch(
                    title = stringResource(R.string.text_use_downloaded_dictionary),
                    checked = useDownloaded,
                    onCheckedChange = onUseDownloadedChange,
                )
            }
        }
    }
}

@Composable
private fun SearchActionRow(
    onNavigateToOptions: () -> Unit,
    onShowHistory: () -> Unit,
    onOriginClick: () -> Unit,
    onDefinitionClick: () -> Unit,
    isSearchEnabled: Boolean,
    isLoading: Boolean
) {
    Row(
        modifier = Modifier.fillMaxWidth(),
        verticalAlignment = Alignment.CenterVertically,
    ) {
        IconButton(onClick = onNavigateToOptions, modifier = Modifier.size(35.dp)) {
            Icon(AppIcons.Settings, contentDescription = stringResource(R.string.label_dictionary_options), tint = MaterialTheme.colorScheme.primary)
        }
        IconButton(onClick = onShowHistory, modifier = Modifier.size(35.dp)) {
            Icon(AppIcons.History, contentDescription = stringResource(R.string.cd_translation_history), tint = MaterialTheme.colorScheme.primary)
        }
        Spacer(Modifier.weight(1f))
        Row(
            horizontalArrangement = Arrangement.spacedBy(8.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            AppButton(onClick = onOriginClick, enabled = isSearchEnabled && !isLoading) {
                Text(stringResource(R.string.origin))
            }
            AppButton(onClick = onDefinitionClick, enabled = isSearchEnabled && !isLoading) {
                Text(text = stringResource(R.string.definition), maxLines = 1, overflow = TextOverflow.Ellipsis)
            }
            if (isLoading) {
                CircularProgressIndicator(
                    modifier = Modifier.size(24.dp),
                    color = MaterialTheme.colorScheme.primary,
                    strokeWidth = 2.dp
                )
            }
        }
    }
}

// --- Component 2: Manager Section ---

@Composable
fun DictionaryManagerSection(
    dictionaryViewModel: DictionaryViewModel,
) {
    val manifest by dictionaryViewModel.manifest.collectAsState()
    val downloadedDictionaries by dictionaryViewModel.downloadedDictionaries.collectAsState()
    val orphanedFiles by dictionaryViewModel.orphanedFiles.collectAsState()
    val downloadProgress by dictionaryViewModel.downloadProgress.collectAsState()
    val isDownloading by dictionaryViewModel.isDownloading.collectAsState()

    var showDeleteAllDialog by remember { mutableStateOf(false) }

    if (manifest == null) return

    AppCard(
        title = stringResource(R.string.label_dictionary_manager),
        text = stringResource(R.string.text_dictionary_manager_description),
        expandable = true
    ) {
        Column(modifier = Modifier.fillMaxWidth()) {
            if (manifest!!.files.isNotEmpty()) {
                DictionaryDownloadList(
                    files = manifest!!.files,
                    downloadedDictionaries = downloadedDictionaries,
                    downloadProgress = downloadProgress,
                    isDownloading = isDownloading,
                    dictionaryViewModel = dictionaryViewModel
                )
            }

            if (orphanedFiles.isNotEmpty()) {
                OrphanedFilesList(
                    files = orphanedFiles,
                    dictionaryViewModel = dictionaryViewModel
                )
            }

            if ((downloadedDictionaries.isNotEmpty() || orphanedFiles.isNotEmpty())) {
                AppButton(
                    onClick = { showDeleteAllDialog = true },
                    enabled = !isDownloading,
                    colors = ButtonDefaults.buttonColors(
                        containerColor = MaterialTheme.colorScheme.error,
                        contentColor = MaterialTheme.colorScheme.onError
                    ),
                    modifier = Modifier.fillMaxWidth().padding(top = 8.dp)
                ) {
                    Text(stringResource(R.string.label_delete_all))
                }
            }

            if (manifest!!.files.isEmpty() && orphanedFiles.isEmpty()) {
                Text(
                    text = stringResource(R.string.text_no_dictionaries_available),
                    style = MaterialTheme.typography.bodyMedium,
                    modifier = Modifier.padding(top = 16.dp)
                )
            }
        }
    }

    if (showDeleteAllDialog) {
        DeleteAllDialog(
            onConfirm = {
                dictionaryViewModel.deleteAllDictionaries()
                showDeleteAllDialog = false
            },
            onDismiss = { showDeleteAllDialog = false }
        )
    }
}

@Composable
private fun DictionaryDownloadList(
    files: List<FileInfo>,
    downloadedDictionaries: List<FileInfo>,
    downloadProgress: Float?,
    isDownloading: Boolean,
    dictionaryViewModel: DictionaryViewModel
) {
    // Local state to track which ID is currently being processed
    var currentDownloadingId by remember { mutableStateOf<String?>(null) }

    // Reset when progress clears
    LaunchedEffect(downloadProgress) {
        if (downloadProgress == null) currentDownloadingId = null
    }

    Text(
        text = stringResource(R.string.text_available_dictionaries),
        style = MaterialTheme.typography.titleMedium,
        fontWeight = FontWeight.Bold,
        modifier = Modifier.padding(top = 16.dp, bottom = 8.dp)
    )

    Column {
        files.forEach { fileInfo ->
            val isDownloaded = downloadedDictionaries.any { it.id == fileInfo.id }
            // Using ViewModel's getDictionaryUiItems to get pre-calculated values
            val uiItems = dictionaryViewModel.getDictionaryUiItems(files, downloadedDictionaries)
            val uiItem = uiItems.find { it.fileInfo.id == fileInfo.id }

            DictionaryManagerItem(
                fileInfo = fileInfo,
                isDownloaded = uiItem?.isDownloaded ?: isDownloaded,
                hasUpdate = uiItem?.hasUpdate ?: false,
                size = uiItem?.size ?: fileInfo.assets.sumOf { it.sizeBytes },
                downloadProgress = if (fileInfo.id == currentDownloadingId) downloadProgress else null,
                isDownloading = isDownloading,
                onDownload = {
                    currentDownloadingId = fileInfo.id
                    dictionaryViewModel.downloadDictionary(fileInfo)
                },
                onUpdate = {
                    currentDownloadingId = fileInfo.id
                    dictionaryViewModel.downloadDictionary(fileInfo)
                },
                onDelete = { dictionaryViewModel.deleteDictionary(fileInfo) }
            )
        }
    }
}

@Composable
private fun OrphanedFilesList(
    files: List<FileInfo>,
    dictionaryViewModel: DictionaryViewModel
) {
    Text(
        text = stringResource(R.string.label_orphaned_files),
        style = MaterialTheme.typography.titleMedium,
        fontWeight = FontWeight.Bold,
        modifier = Modifier.padding(top = 16.dp, bottom = 8.dp)
    )
    Text(
        text = stringResource(R.string.text_these_files_exist_locally),
        style = MaterialTheme.typography.bodySmall,
        modifier = Modifier.padding(bottom = 8.dp)
    )

    Column {
        files.forEach { fileInfo ->
            OrphanedFileItem(
                fileInfo = fileInfo,
                size = dictionaryViewModel.getDictionarySize(fileInfo),
                onDelete = { dictionaryViewModel.deleteOrphanedFile(fileInfo) }
            )
        }
    }
}

@Composable
fun DeleteAllDialog(onConfirm: () -> Unit, onDismiss: () -> Unit) {
    AlertDialog(
        onDismissRequest = onDismiss,
        title = { Text(stringResource(R.string.delete_all_dictionaries_title)) },
        text = { Text(stringResource(R.string.delete_all_dictionaries_confirmation)) },
        confirmButton = {
            TextButton(onClick = onConfirm) { Text(stringResource(android.R.string.ok)) }
        },
        dismissButton = {
            TextButton(onClick = onDismiss) { Text(stringResource(android.R.string.cancel)) }
        }
    )
}

// --- Helper Items ---

@Composable
fun DictionaryManagerItem(
    fileInfo: FileInfo,
    isDownloaded: Boolean,
    hasUpdate: Boolean,
    size: Long,
    downloadProgress: Float?,
    isDownloading: Boolean,
    onDownload: () -> Unit,
    onUpdate: () -> Unit,
    onDelete: () -> Unit
) {
    Card(
        modifier = Modifier.fillMaxWidth().padding(vertical = 4.dp)
    ) {
        Column(modifier = Modifier.padding(16.dp)) {
            Text(fileInfo.name, style = MaterialTheme.typography.titleMedium, fontWeight = FontWeight.Bold)
            Text(fileInfo.description, style = MaterialTheme.typography.bodyMedium)
            Text(stringResource(R.string.label_version_2d, fileInfo.version), style = MaterialTheme.typography.bodySmall)
            Text(stringResource(R.string.label_size_2d_mb, size / 1024 / 1024), style = MaterialTheme.typography.bodySmall)

            if (downloadProgress != null) {
                LinearProgressIndicator(
                    progress = { downloadProgress },
                    modifier = Modifier.fillMaxWidth(),
                    color = ProgressIndicatorDefaults.linearColor,
                    trackColor = ProgressIndicatorDefaults.linearTrackColor,
                    strokeCap = ProgressIndicatorDefaults.LinearStrokeCap,
                )
            } else {
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.spacedBy(8.dp)
                ) {
                    if (!isDownloaded) {
                        AppButton(onClick = onDownload, enabled = !isDownloading) {
                            Text(stringResource(R.string.label_download))
                        }
                    } else {
                        if (hasUpdate) {
                            AppOutlinedButton(onClick = onUpdate, enabled = !isDownloading) {
                                Text(stringResource(R.string.label_update))
                            }
                        }
                        AppOutlinedButton(onClick = onDelete, enabled = !isDownloading, borderColor = MaterialTheme.colorScheme.error) {
                            Text(stringResource(R.string.label_delete))
                        }
                    }
                }
            }
        }
    }
}

@Composable
fun OrphanedFileItem(fileInfo: FileInfo, size: Long, onDelete: () -> Unit) {
    Card(modifier = Modifier.fillMaxWidth().padding(vertical = 4.dp)) {
        Column(modifier = Modifier.padding(16.dp)) {
            Text(fileInfo.name, style = MaterialTheme.typography.titleMedium, fontWeight = FontWeight.Bold)
            Text(fileInfo.description, style = MaterialTheme.typography.bodyMedium)
            Text(stringResource(R.string.label_version_2d, fileInfo.version), style = MaterialTheme.typography.bodySmall)
            Text(stringResource(R.string.label_size_2d_mb, size / 1024 / 1024), style = MaterialTheme.typography.bodySmall)
            AppButton(onClick = onDelete) { Text(stringResource(R.string.label_delete)) }
        }
    }
}

@Composable
fun WordOfTheDayCard(
    entry: DictionaryEntry,
    isLoading: Boolean,
    onEntryClick: (DictionaryEntry) -> Unit,
    onRefresh: () -> Unit,
    modifier: Modifier = Modifier
) {
    AppCard(modifier = modifier.fillMaxWidth().clickable { onEntryClick(entry) }) {
        Column(modifier = Modifier.padding(16.dp)) {
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceBetween,
                verticalAlignment = Alignment.CenterVertically
            ) {
                Text(stringResource(R.string.text_word_of_the_day), style = MaterialTheme.typography.titleLarge, fontWeight = FontWeight.Bold)
                IconButton(onClick = onRefresh, enabled = !isLoading) {
                    Icon(AppIcons.Refresh, contentDescription = stringResource(R.string.refresh_word_of_the_day))
                }
            }
            Spacer(modifier = Modifier.height(8.dp))
            Text(text = "${entry.word}: ${entry.definition.firstOrNull()?.content ?: ""}")
        }
    }
}

@Composable
fun DictionaryHistorySheetContent(
    historyEntries: List<DictionaryEntry>,
    onEntryClick: (DictionaryEntry) -> Unit
) {
    Column(modifier = Modifier.padding(16.dp)) {
        Text(stringResource(R.string.text_search_history), style = MaterialTheme.typography.titleLarge, fontWeight = FontWeight.Bold, modifier = Modifier.padding(bottom = 8.dp))
        LazyColumn {
            items(historyEntries) { entry ->
                DictionaryHistoryItem(entry = entry, onEntryClick = onEntryClick)
            }
        }
    }
}

@Composable
fun DictionaryHistoryItem(entry: DictionaryEntry, onEntryClick: (DictionaryEntry) -> Unit) {
    Card(modifier = Modifier.fillMaxWidth().padding(vertical = 4.dp), onClick = { onEntryClick(entry) }) {
        Row(modifier = Modifier.fillMaxWidth().padding(16.dp), horizontalArrangement = Arrangement.SpaceBetween) {
            Text(text = entry.word)
            Text(text = entry.languageName, style = MaterialTheme.typography.bodySmall)
        }
    }
}


@Suppress("HardCodedStringLiteral")
@Composable
@androidx.compose.ui.tooling.preview.Preview
fun DictionarySearchCardPreview() {
    // Preview with mock values - ViewModels are null for preview
    Box(modifier = Modifier.padding(16.dp)) {
        Text("DictionarySearchCard Preview")
    }
}

@Suppress("HardCodedStringLiteral")
@Composable
@androidx.compose.ui.tooling.preview.Preview
fun LanguageSelectionRowPreview() {
    // Preview with mock values
    Box(modifier = Modifier.padding(16.dp)) {
        Text("LanguageSelectionRow Preview")
    }
}

@Suppress("HardCodedStringLiteral")
@Composable
@androidx.compose.ui.tooling.preview.Preview
fun SearchActionRowPreview() {
    // Preview with mock values
    Box(modifier = Modifier.padding(16.dp)) {
        Text("SearchActionRow Preview")
    }
}

@Suppress("HardCodedStringLiteral")
@Composable
@androidx.compose.ui.tooling.preview.Preview
fun DictionaryManagerItemPreview() {
    val mockFileInfo = FileInfo(
        id = "test_id",
        name = "German Dictionary",
        description = "German to English dictionary",
        version = "1.0.0",
        assets = emptyList()
    )
    DictionaryManagerItem(
        fileInfo = mockFileInfo,
        isDownloaded = false,
        hasUpdate = false,
        size = 1024 * 1024 * 50,
        downloadProgress = null,
        isDownloading = false,
        onDownload = {},
        onUpdate = {},
        onDelete = {}
    )
}

@Suppress("HardCodedStringLiteral")
@Composable
@androidx.compose.ui.tooling.preview.Preview
fun WordOfTheDayCardPreview() {
    val mockEntry = DictionaryEntry(
        id = 1,
        word = "Apfel",
        definition = emptyList(),
        languageCode = 1,
        languageName = "German"
    )
    WordOfTheDayCard(
        entry = mockEntry,
        isLoading = false,
        onEntryClick = {},
        onRefresh = {}
    )
}

@Suppress("HardCodedStringLiteral")
@Composable
@androidx.compose.ui.tooling.preview.Preview
fun DictionaryHistoryItemPreview() {
    val mockEntry = DictionaryEntry(
        id = 1,
        word = "Hello",
        definition = emptyList(),
        languageCode = 1,
        languageName = "English"
    )
    DictionaryHistoryItem(entry = mockEntry, onEntryClick = {})
}

@Suppress("HardCodedStringLiteral")
@Composable
@androidx.compose.ui.tooling.preview.Preview
fun DictionaryHistorySheetContentPreview() {
    val mockEntries = listOf(
        DictionaryEntry(id = 1, word = "Hello", definition = emptyList(), languageCode = 1, languageName = "English"),
        DictionaryEntry(id = 2, word = "Guten Tag", definition = emptyList(), languageCode = 1, languageName = "German")
    )
    DictionaryHistorySheetContent(historyEntries = mockEntries, onEntryClick = {})
}
