package eu.gaudian.translator.view.settings

import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.ListItem
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.rotate
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.compose.ui.tooling.preview.PreviewParameterProvider
import androidx.compose.ui.unit.dp
import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.NavController
import eu.gaudian.translator.R
import eu.gaudian.translator.model.LanguageModel
import eu.gaudian.translator.utils.findActivity
import eu.gaudian.translator.view.composable.AppButton
import eu.gaudian.translator.view.composable.AppCard
import eu.gaudian.translator.view.composable.AppDialog
import eu.gaudian.translator.view.composable.AppIcons
import eu.gaudian.translator.view.composable.AppOutlinedTextField
import eu.gaudian.translator.view.composable.AppScaffold
import eu.gaudian.translator.view.composable.AppSwitch
import eu.gaudian.translator.view.composable.AppTopAppBar
import eu.gaudian.translator.view.composable.ModelBadges
import eu.gaudian.translator.view.hints.HintDefinition
import eu.gaudian.translator.viewmodel.ApiViewModel

@Composable
fun AddModelScreen(navController: NavController, providerKey: String) {
    val activity = LocalContext.current.findActivity()
    val apiViewModel : ApiViewModel = hiltViewModel(viewModelStoreOwner = activity)
    val state by apiViewModel.apiKeyManagementState.collectAsStateWithLifecycle()
    val allProviders by apiViewModel.allProviders.collectAsStateWithLifecycle()

    val provider = remember(allProviders, providerKey) {
        allProviders.find { it.key == providerKey }
    }
    val providerName = provider?.displayName ?: stringResource(R.string.label_add_custom_model)

    var modelId by remember { mutableStateOf("") }
    var displayName by remember { mutableStateOf("") }
    var description by remember { mutableStateOf("") }
    var showScannedModelsDialog by remember { mutableStateOf(false) }

    val isLoading = state.isAddingModel
    val isScanning = state.isScanningModels
    val scannedModels = state.scannedModels
    val errorMessage = state.addModelError

    LaunchedEffect(Unit) {
        var wasAdding = state.isAddingModel
        snapshotFlow { state.isAddingModel }
            .collect { isCurrentlyAdding ->
                if (wasAdding && !isCurrentlyAdding && state.addModelError == null) {
                    navController.popBackStack()
                }
                wasAdding = isCurrentlyAdding
            }
    }

    LaunchedEffect(Unit) {
        var wasScanning = state.isScanningModels
        snapshotFlow { state.isScanningModels }
            .collect { isCurrentlyScanning ->
                if (wasScanning && !isCurrentlyScanning && state.scannedModels.isNotEmpty()) {
                    showScannedModelsDialog = true
                }
                wasScanning = isCurrentlyScanning
            }
    }

    LaunchedEffect(providerKey) {
        apiViewModel.startAddingModelForProvider(providerKey)
    }
    DisposableEffect(Unit) {
        onDispose { apiViewModel.cancelAddModel() }
    }

    if (showScannedModelsDialog) {
        EnhancedScannedModelsDialog(
            scannedModels = scannedModels,
            onDismiss = {
                @Suppress("AssignedValueIsNeverRead")
                showScannedModelsDialog = false
            },
            onModelSelected = { selectedModel ->
                modelId = selectedModel.modelId
                displayName = selectedModel.displayName.ifBlank { selectedModel.modelId }
                description = selectedModel.description
                @Suppress("AssignedValueIsNeverRead")
                showScannedModelsDialog = false
            }
        )
    }



    AppScaffold(
        topBar = {
            AppTopAppBar(
                title = providerName,
                onNavigateBack = { navController.popBackStack() },
                hintContent = HintDefinition.ADD_MODEL_SCAN.hint()
            )
        },
    ) { paddingValues ->
        LazyColumn(
            modifier = Modifier
                .fillMaxSize()
                .padding(paddingValues),
            contentPadding = PaddingValues(16.dp),
            verticalArrangement = Arrangement.spacedBy(16.dp)
        ) {
            item {
                // Scan Option - Primary
                AppCard(
                    modifier = Modifier.fillMaxWidth()
                ) {
                    Column(
                        modifier = Modifier.padding(16.dp),
                        verticalArrangement = Arrangement.spacedBy(12.dp)
                    ) {
                        Row(
                            verticalAlignment = Alignment.CenterVertically
                        ) {
                            Icon(
                                AppIcons.Search,
                                contentDescription = null,
                                tint = MaterialTheme.colorScheme.primary,
                                modifier = Modifier.size(24.dp)
                            )
                            Spacer(Modifier.width(12.dp))
                            Column(modifier = Modifier.weight(1f)) {
                                Text(
                                    stringResource(R.string.text_scan_for_available_models),
                                    style = MaterialTheme.typography.titleMedium,
                                    color = MaterialTheme.colorScheme.primary
                                )
                                Text(
                                    stringResource(
                                        R.string.text_automatically_discover_models_from,
                                        providerName
                                    ),
                                    style = MaterialTheme.typography.bodySmall,
                                    color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.7f)
                                )
                            }
                        }

                        AppButton(
                            onClick = { apiViewModel.scanModelsForProvider(providerKey) },
                            enabled = !isLoading && !isScanning,
                            modifier = Modifier.fillMaxWidth()
                        ) {
                            if (isScanning) {
                                CircularProgressIndicator(
                                    modifier = Modifier.size(20.dp),
                                    color = MaterialTheme.colorScheme.onPrimary,
                                    strokeWidth = 2.dp
                                )
                                Spacer(Modifier.width(8.dp))
                                Text(stringResource(R.string.label_scanning))
                            } else {
                                Icon(
                                    AppIcons.Search,
                                    contentDescription = null,
                                    modifier = Modifier.size(ButtonDefaults.IconSize)
                                )
                                Spacer(Modifier.size(ButtonDefaults.IconSpacing))
                                Text(stringResource(R.string.label_scan_for_models))
                            }
                        }
                    }
                    errorMessage?.let {
                        AppCard(
                            modifier = Modifier.fillMaxWidth(),
                        ) {
                            Row(
                                modifier = Modifier.padding(16.dp),
                                verticalAlignment = Alignment.CenterVertically
                            ) {
                                Icon(
                                    AppIcons.Warning,
                                    contentDescription = null,
                                    tint = MaterialTheme.colorScheme.error,
                                    modifier = Modifier.size(20.dp)
                                )
                                Spacer(Modifier.width(12.dp))
                                Text(
                                    text = it,
                                    color = MaterialTheme.colorScheme.onErrorContainer,
                                    style = MaterialTheme.typography.bodyMedium,
                                    modifier = Modifier.weight(1f)
                                )
                            }
                        }
                    }
                }
            }

            item {
                // Manual Option
                AppCard(
                    modifier = Modifier.fillMaxWidth()
                ) {
                    Column(
                        modifier = Modifier.padding(16.dp),
                        verticalArrangement = Arrangement.spacedBy(16.dp)
                    ) {
                        Row(
                            verticalAlignment = Alignment.CenterVertically
                        ) {
                            Icon(
                                AppIcons.Edit,
                                contentDescription = null,
                                tint = MaterialTheme.colorScheme.secondary,
                                modifier = Modifier.size(24.dp)
                            )
                            Spacer(Modifier.width(12.dp))
                            Column(modifier = Modifier.weight(1f)) {
                                Text(
                                    stringResource(R.string.label_add_model_manually),
                                    style = MaterialTheme.typography.titleMedium,
                                    color = MaterialTheme.colorScheme.secondary
                                )
                                Text(
                                    stringResource(R.string.text_enter_model_details_yourself),
                                    style = MaterialTheme.typography.bodySmall,
                                    color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.7f)
                                )
                            }
                        }

                        Column(verticalArrangement = Arrangement.spacedBy(12.dp)) {
                            AppOutlinedTextField(
                                value = displayName,
                                onValueChange = { displayName = it },
                                label = { Text(stringResource(R.string.label_display_name)) },
                                placeholder = { Text(stringResource(R.string.text_e_g_gpt_4_claude_3)) },
                                enabled = !isLoading,
                                modifier = Modifier.fillMaxWidth(),
                                supportingText = {
                                    if (displayName.isBlank()) {
                                        Text(
                                            stringResource(R.string.text_required_enter_a_human_readable_name),
                                            color = MaterialTheme.colorScheme.error,
                                            style = MaterialTheme.typography.bodySmall
                                        )
                                    }
                                }
                            )
                            AppOutlinedTextField(
                                value = modelId,
                                onValueChange = { modelId = it },
                                label = { Text(stringResource(R.string.label_model_id_star)) },
                                placeholder = { Text(stringResource(R.string.text_e_g_gpt_4_claude_3_sonnet)) },
                                enabled = !isLoading,
                                modifier = Modifier.fillMaxWidth(),
                                supportingText = {
                                    if (modelId.isBlank()) {
                                        Text(
                                            stringResource(R.string.text_required_enter_the_exact_model_identifier),
                                            color = MaterialTheme.colorScheme.error,
                                            style = MaterialTheme.typography.bodySmall
                                        )
                                    } else {
                                        Text(
                                            stringResource(R.string.text_this_must_match_the_provider_s_model_name_exactly),
                                            color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.6f),
                                            style = MaterialTheme.typography.bodySmall
                                        )
                                    }
                                }
                            )
                            AppOutlinedTextField(
                                value = description,
                                onValueChange = { description = it },
                                label = { Text(stringResource(R.string.label_description)) },
                                placeholder = { Text(stringResource(R.string.text_e_g_fast_and_efficient_for_simple_tasks)) },
                                enabled = !isLoading,
                                modifier = Modifier.fillMaxWidth(),
                                maxLines = 3,
                                supportingText = {
                                    Text(
                                        stringResource(R.string.text_optional_describe_what_this_model_is_good_for),
                                        color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.6f),
                                        style = MaterialTheme.typography.bodySmall
                                    )
                                }
                            )
                        }
                    }
                }
            }


            item {
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(16.dp),
                    horizontalArrangement = Arrangement.End,
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    TextButton(
                        onClick = { navController.popBackStack() },
                        enabled = !isLoading
                    ) {
                        Text(stringResource(R.string.label_cancel))
                    }
                    Spacer(modifier = Modifier.width(8.dp))
                    AppButton(
                        onClick = {
                            apiViewModel.addModelToProvider(
                                providerKey,
                                LanguageModel(
                                    modelId.trim(),
                                    displayName.trim(),
                                    providerKey,
                                    description.trim(),
                                    true
                                )
                            )
                        },
                        enabled = modelId.isNotBlank() && displayName.isNotBlank() && !isLoading
                    ) {
                        if (isLoading) {
                            CircularProgressIndicator(
                                modifier = Modifier.size(20.dp),
                                color = MaterialTheme.colorScheme.onPrimary
                            )
                        } else {
                            Text(stringResource(R.string.label_add_model))
                        }
                    }
                }
            }
        }
    }
}



@Suppress("HardCodedStringLiteral")
@Composable
private fun EnhancedScannedModelsDialog(
    scannedModels: List<LanguageModel>,
    onDismiss: () -> Unit,
    onModelSelected: (LanguageModel) -> Unit
) {
    var searchQuery by remember { mutableStateOf("") }
    var showOnlyFreeModels by remember { mutableStateOf(false) }

    val showSearch = remember(scannedModels) { scannedModels.size > 10 }
    val hasFreeModels = remember(scannedModels) {
        scannedModels.any { it.displayName.contains("free", true) || it.modelId.contains("free", true) }
    }

    val filteredModels = remember(scannedModels, showOnlyFreeModels, searchQuery) {
        scannedModels
            .filter { model ->
                if (showOnlyFreeModels) {
                    model.displayName.contains("free", true) || model.modelId.contains("free", true)
                } else {
                    true
                }
            }
            .filter { model ->
                model.displayName.contains(searchQuery, true) || model.modelId.contains(searchQuery, true)
            }
    }

    AppDialog(title = { Row(
        modifier = Modifier.padding(start = 4.dp, end = 4.dp, top = 8.dp, bottom = 8.dp),
        verticalAlignment = Alignment.CenterVertically
    ) {
        IconButton(onClick = onDismiss) {
            Icon(AppIcons.ArrowBack, contentDescription = stringResource(R.string.cd_back))
        }
        Text(
            text = stringResource(R.string.select_a_model),
            style = MaterialTheme.typography.headlineSmall,
            modifier = Modifier.weight(1f)
        )
    } }, onDismissRequest = onDismiss) {

            Column(modifier = Modifier.padding(0.dp)) {


                if (showSearch || hasFreeModels) {
                    Column(Modifier.padding(horizontal = 8.dp)) {
                        if (showSearch) {
                            AppOutlinedTextField(
                                value = searchQuery,
                                onValueChange = { searchQuery = it },
                                modifier = Modifier.fillMaxWidth(),
                                label = { Text(stringResource(R.string.search_models)) },
                                leadingIcon = { Icon(AppIcons.Search, contentDescription = null) },
                                singleLine = true
                            )
                            Spacer(Modifier.height(8.dp))
                        }
                        if (hasFreeModels) {
                            Row(
                                verticalAlignment = Alignment.CenterVertically,
                                modifier = Modifier
                                    .fillMaxWidth()
                                    .clickable { showOnlyFreeModels = !showOnlyFreeModels }
                                    .padding(vertical = 8.dp)
                            ) {
                                Text(
                                    stringResource(R.string.show_free_models_only),
                                    style = MaterialTheme.typography.bodyLarge,
                                    modifier = Modifier.weight(1f)
                                )
                                AppSwitch(checked = showOnlyFreeModels, onCheckedChange = null)
                            }
                        }
                    }
                }

                HorizontalDivider(modifier = Modifier.padding(top = 16.dp))

                LazyColumn(modifier = Modifier.heightIn(max = 1000.dp)) {
                    if (filteredModels.isEmpty()) {
                        item {
                            Text(
                                text = stringResource(R.string.no_models_found),
                                style = MaterialTheme.typography.bodyMedium,
                                textAlign = TextAlign.Center,
                                modifier = Modifier
                                    .fillMaxWidth()
                                    .padding(24.dp)
                            )
                        }
                    } else {
                        itemsIndexed(filteredModels, key = { index, model -> "${model.modelId}_${model.providerKey}_${model.displayName}_$index" }) { _, model ->
                            var expanded by remember { mutableStateOf(false) }
                            var canExpand by remember { mutableStateOf(false) }
                            val secondary = buildString {
                                if (model.displayName.isNotBlank()) append(model.modelId)
                                if (model.description.isNotBlank()) {
                                    if (isNotEmpty()) append(" • ")
                                    append(model.description)
                                }
                                if (model.isCustom) {
                                    if (isNotEmpty()) append(" • ")
                                    append("Custom")
                                }
                            }
                            ListItem(
                                overlineContent = { Text(model.providerKey.uppercase()) },
                                headlineContent = {
                                    Row(verticalAlignment = Alignment.CenterVertically) {
                                        Text(
                                            text = model.displayName.ifBlank { model.modelId },
                                            modifier = Modifier.weight(1f),
                                            maxLines = 1,
                                            overflow = androidx.compose.ui.text.style.TextOverflow.Ellipsis
                                        )
                                        Spacer(Modifier.width(8.dp))
                                        ModelBadges(
                                            modelDisplayOrId = model.displayName.ifBlank { model.modelId },
                                            providerKey = model.providerKey,
                                        )
                                    }
                                },
                                supportingContent = {
                                    if (secondary.isNotBlank()) {
                                        Column(verticalArrangement = Arrangement.spacedBy(4.dp)) {
                                            Text(
                                                modifier = Modifier.clickable(enabled = canExpand) { expanded = !expanded },
                                                text = secondary,
                                                maxLines = if (expanded) Int.MAX_VALUE else 1,
                                                style = MaterialTheme.typography.bodyMedium,
                                                onTextLayout = { result ->

                                                    if (!expanded) {
                                                        val overflow = result.hasVisualOverflow
                                                        if (canExpand != overflow) {
                                                            canExpand = overflow
                                                        }
                                                    }
                                                }
                                            )
                                        }
                                    }
                                },
                                trailingContent = {
                                    if (secondary.isNotBlank() && (canExpand || expanded)) {
                                        val rotation by animateFloatAsState(
                                            targetValue = if (expanded) 180f else 0f,
                                            label = "expandRotate"
                                        )
                                        IconButton(onClick = { expanded = !expanded }) {
                                            Icon(
                                                imageVector = AppIcons.ExpandMore,
                                                contentDescription = if (expanded) stringResource(R.string.cd_collapse) else stringResource(R.string.cd_expand),
                                                modifier = Modifier.rotate(rotation)
                                            )
                                        }
                                    }
                                },
                                modifier = Modifier.clickable { onModelSelected(model) }
                            )
                        }
                    }
                }
            }
        }
}

@Suppress("HardCodedStringLiteral")
@Preview(showBackground = true, name = "AddModelScreen - Default")
@Composable
fun AddModelScreenPreview() {
    val navController = NavController(LocalContext.current)
    AddModelScreen(navController = navController, providerKey = "openai")
}

@Suppress("HardCodedStringLiteral")
@Preview(showBackground = true, name = "AddModelScreen - Custom Provider")
@Composable
fun AddModelScreenCustomProviderPreview() {
    val navController = NavController(LocalContext.current)
    AddModelScreen(navController = navController, providerKey = "custom-local")
}

@Suppress("HardCodedStringLiteral")
@Preview(showBackground = true, name = "AddModelScreen - Anthropic")
@Composable
fun AddModelScreenAnthropicPreview() {
    val navController = NavController(LocalContext.current)
    AddModelScreen(navController = navController, providerKey = "anthropic")
}

// Preview data providers
@Suppress("HardCodedStringLiteral")
class ScannedModelsPreviewProvider : PreviewParameterProvider<List<LanguageModel>> {
    override val values = sequenceOf(
        // Small set for basic preview
        listOf(
            LanguageModel(modelId = "gpt-3.5-turbo", displayName = "GPT-3.5 Turbo", providerKey = "openai", description = "Fast and efficient model for most tasks", isCustom = false),
            LanguageModel(modelId = "gpt-4", displayName = "GPT-4", providerKey = "openai", description = "Most capable model for complex tasks", isCustom = false),
            LanguageModel(modelId = "claude-instant", displayName = "Claude Instant", providerKey = "anthropic", description = "Fast and responsive model", isCustom = false)
        ),
        // Large set for comprehensive preview
        listOf(
            LanguageModel(modelId = "gpt-3.5-turbo", displayName = "GPT-3.5 Turbo", providerKey = "openai", description = "OpenAI's most capable model", isCustom = false),
            LanguageModel(modelId = "gpt-4", displayName = "GPT-4", providerKey = "openai", description = "OpenAI's newest model", isCustom = false),
            LanguageModel(modelId = "gpt-4-turbo", displayName = "GPT-4 Turbo", providerKey = "openai", description = "Faster version of GPT-4", isCustom = false),
            LanguageModel(modelId = "claude-1", displayName = "Claude 1", providerKey = "anthropic", description = "Anthropic's largest model", isCustom = false),
            LanguageModel(modelId = "claude-instant-1", displayName = "Claude Instant 1", providerKey = "anthropic", description = "Anthropic's fastest model", isCustom = false),
            LanguageModel(modelId = "claude-2", displayName = "Claude 2", providerKey = "anthropic", description = "Improved Claude model", isCustom = false),
            LanguageModel(modelId = "palm-2", displayName = "PaLM 2", providerKey = "google", description = "Google's latest model", isCustom = false),
            LanguageModel(modelId = "gemini-pro", displayName = "Gemini Pro", providerKey = "google", description = "Google's advanced model", isCustom = false),
            LanguageModel(modelId = "custom-model-1", displayName = "My Custom Model", providerKey = "custom", description = "A custom model added by the user", isCustom = true),
            LanguageModel(modelId = "free-model-1", displayName = "Free Model Alpha", providerKey = "community", description = "A free model from the community", isCustom = false),
            LanguageModel(modelId = "experimental-model-x", displayName = "Experimental Model X", providerKey = "labs", description = "An experimental model with latest features", isCustom = false),
            LanguageModel(modelId = "legacy-model", displayName = "Legacy Model", providerKey = "archive", description = "An older model for compatibility", isCustom = false),
            LanguageModel(modelId = "specialized-model-finance", displayName = "Finance Analyzer", providerKey = "financeLLM", description = "Model specialized in financial data", isCustom = false),
            LanguageModel(modelId = "code-gen-master", displayName = "CodeGen Master", providerKey = "devTools", description = "Advanced code generation model", isCustom = false),
            LanguageModel(modelId = "story-writer-pro", displayName = "Story Writer Pro", providerKey = "creativeAI", description = "Model for creative writing and storytelling", isCustom = false)
        )
    )
}

@Suppress("HardCodedStringLiteral")
@Preview(showBackground = true, name = "ScannedModelsDialog - Small List")
@Composable
fun EnhancedScannedModelsDialogSmallPreview() {
    val scannedModels = listOf(
        LanguageModel(modelId = "gpt-3.5-turbo", displayName = "GPT-3.5 Turbo", providerKey = "openai", description = "Fast and efficient model for most tasks", isCustom = false),
        LanguageModel(modelId = "gpt-4", displayName = "GPT-4", providerKey = "openai", description = "Most capable model for complex tasks", isCustom = false),
        LanguageModel(modelId = "claude-instant", displayName = "Claude Instant", providerKey = "anthropic", description = "Fast and responsive model", isCustom = false)
    )
    EnhancedScannedModelsDialog(
        scannedModels = scannedModels,
        onDismiss = {},
        onModelSelected = {}
    )
}

@Suppress("HardCodedStringLiteral")
@Preview(showBackground = true, name = "ScannedModelsDialog - Large List")
@Composable
fun EnhancedScannedModelsDialogLargePreview(
    @PreviewParameter(ScannedModelsPreviewProvider::class) scannedModels: List<LanguageModel>
) {
    EnhancedScannedModelsDialog(
        scannedModels = scannedModels,
        onDismiss = {},
        onModelSelected = {}
    )
}

@Suppress("HardCodedStringLiteral")
@Preview(showBackground = true, name = "ScannedModelsDialog - Custom Models Only")
@Composable
fun EnhancedScannedModelsDialogCustomPreview() {
    val scannedModels = listOf(
        LanguageModel(modelId = "custom-gpt", displayName = "Custom GPT", providerKey = "custom", description = "My custom GPT model", isCustom = true),
        LanguageModel(modelId = "local-llama", displayName = "Local LLaMA", providerKey = "local", description = "Local LLaMA instance", isCustom = true),
        LanguageModel(modelId = "fine-tuned-model", displayName = "Fine-Tuned Model", providerKey = "custom", description = "Model fine-tuned on my data", isCustom = true)
    )
    EnhancedScannedModelsDialog(
        scannedModels = scannedModels,
        onDismiss = {},
        onModelSelected = {}
    )
}

@Suppress("HardCodedStringLiteral")
@Preview(showBackground = true, name = "ScannedModelsDialog - Free Models Only")
@Composable
fun EnhancedScannedModelsDialogFreePreview() {
    val scannedModels = listOf(
        LanguageModel(modelId = "free-gpt", displayName = "Free GPT", providerKey = "free", description = "Free GPT model", isCustom = false),
        LanguageModel(modelId = "free-claude", displayName = "Free Claude", providerKey = "free", description = "Free Claude model", isCustom = false),
        LanguageModel(modelId = "community-model", displayName = "Community Model", providerKey = "community", description = "Community-supported model", isCustom = false)
    )
    EnhancedScannedModelsDialog(
        scannedModels = scannedModels,
        onDismiss = {},
        onModelSelected = {}
    )
}

@Suppress("HardCodedStringLiteral")
@Preview(showBackground = true, name = "ScannedModelsDialog - Mixed Providers")
@Composable
fun EnhancedScannedModelsDialogMixedPreview() {
    val scannedModels = listOf(
        LanguageModel(modelId = "gpt-3.5-turbo", displayName = "GPT-3.5 Turbo", providerKey = "openai", description = "OpenAI's fast model", isCustom = false),
        LanguageModel(modelId = "claude-instant", displayName = "Claude Instant", providerKey = "anthropic", description = "Anthropic's fast model", isCustom = false),
        LanguageModel(modelId = "custom-local", displayName = "Local Custom", providerKey = "custom", description = "My local model", isCustom = true),
        LanguageModel(modelId = "free-basic", displayName = "Free Basic", providerKey = "free", description = "Free basic model", isCustom = false)
    )
    EnhancedScannedModelsDialog(
        scannedModels = scannedModels,
        onDismiss = {},
        onModelSelected = {}
    )
}

@Suppress("HardCodedStringLiteral")
@Preview(showBackground = true, name = "ScannedModelsDialog - Empty State")
@Composable
fun EnhancedScannedModelsDialogEmptyPreview() {
    EnhancedScannedModelsDialog(
        scannedModels = emptyList(),
        onDismiss = {},
        onModelSelected = {}
    )
}

@Suppress("HardCodedStringLiteral")
@Preview(showBackground = true, name = "ScannedModelsDialog - Single Model")
@Composable
fun EnhancedScannedModelsDialogSinglePreview() {
    val scannedModels = listOf(
        LanguageModel(modelId = "gpt-4", displayName = "GPT-4", providerKey = "openai", description = "OpenAI's most advanced model", isCustom = false)
    )
    EnhancedScannedModelsDialog(
        scannedModels = scannedModels,
        onDismiss = {},
        onModelSelected = {}
    )
}
