package com.tt.honeyDue.ui.components.dialogs

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Warning
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import com.tt.honeyDue.ui.theme.AppRadius
import com.tt.honeyDue.ui.theme.AppSpacing
import honeydue.composeapp.generated.resources.*
import org.jetbrains.compose.resources.stringResource

/**
 * Dialog for confirming account deletion.
 *
 * For email auth users: shows a password field for confirmation.
 * For social auth users: shows a "type DELETE to confirm" field.
 *
 * @param isSocialAuthUser Whether the user signed in via social auth (Apple/Google)
 * @param onConfirm Callback with password (for email users) or confirmation text (for social users)
 * @param onDismiss Callback when dialog is dismissed
 * @param isLoading Whether the delete request is in progress
 */
@Composable
fun DeleteAccountDialog(
    isSocialAuthUser: Boolean,
    onConfirm: (password: String?, confirmation: String?) -> Unit,
    onDismiss: () -> Unit,
    isLoading: Boolean
) {
    var inputValue by remember { mutableStateOf("") }

    val isConfirmEnabled = if (isSocialAuthUser) {
        inputValue.equals("DELETE", ignoreCase = false)
    } else {
        inputValue.isNotBlank()
    }

    Dialog(onDismissRequest = { if (!isLoading) onDismiss() }) {
        Card(
            shape = RoundedCornerShape(AppRadius.lg),
            colors = CardDefaults.cardColors(
                containerColor = MaterialTheme.colorScheme.background
            ),
            modifier = Modifier
                .fillMaxWidth()
                .padding(AppSpacing.lg)
        ) {
            Column(
                modifier = Modifier.padding(AppSpacing.xl),
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.spacedBy(AppSpacing.md)
            ) {
                // Warning Icon
                Icon(
                    imageVector = Icons.Default.Warning,
                    contentDescription = "Warning",
                    tint = MaterialTheme.colorScheme.error,
                    modifier = Modifier.size(48.dp)
                )

                // Title
                Text(
                    text = stringResource(Res.string.delete_account_title),
                    style = MaterialTheme.typography.headlineSmall,
                    fontWeight = FontWeight.Bold,
                    color = MaterialTheme.colorScheme.error
                )

                // Warning text
                Text(
                    text = stringResource(Res.string.delete_account_warning),
                    style = MaterialTheme.typography.bodyMedium,
                    color = MaterialTheme.colorScheme.onSurfaceVariant
                )

                // Shared residence warning
                Text(
                    text = stringResource(Res.string.delete_account_shared_warning),
                    style = MaterialTheme.typography.bodyMedium,
                    fontWeight = FontWeight.SemiBold,
                    color = MaterialTheme.colorScheme.error
                )

                Spacer(modifier = Modifier.height(AppSpacing.sm))

                // Input field
                if (isSocialAuthUser) {
                    OutlinedTextField(
                        value = inputValue,
                        onValueChange = { inputValue = it },
                        label = { Text(stringResource(Res.string.delete_account_confirm_type)) },
                        modifier = Modifier.fillMaxWidth(),
                        singleLine = true,
                        enabled = !isLoading,
                        shape = RoundedCornerShape(12.dp)
                    )
                } else {
                    OutlinedTextField(
                        value = inputValue,
                        onValueChange = { inputValue = it },
                        label = { Text(stringResource(Res.string.delete_account_confirm_password)) },
                        modifier = Modifier.fillMaxWidth(),
                        singleLine = true,
                        enabled = !isLoading,
                        visualTransformation = PasswordVisualTransformation(),
                        shape = RoundedCornerShape(12.dp)
                    )
                }

                Spacer(modifier = Modifier.height(AppSpacing.sm))

                // Buttons
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.spacedBy(AppSpacing.md)
                ) {
                    // Cancel button
                    OutlinedButton(
                        onClick = onDismiss,
                        modifier = Modifier.weight(1f),
                        enabled = !isLoading
                    ) {
                        Text(stringResource(Res.string.delete_account_cancel))
                    }

                    // Delete button
                    Button(
                        onClick = {
                            if (isSocialAuthUser) {
                                onConfirm(null, inputValue)
                            } else {
                                onConfirm(inputValue, null)
                            }
                        },
                        modifier = Modifier.weight(1f),
                        enabled = isConfirmEnabled && !isLoading,
                        colors = ButtonDefaults.buttonColors(
                            containerColor = MaterialTheme.colorScheme.error
                        )
                    ) {
                        if (isLoading) {
                            CircularProgressIndicator(
                                modifier = Modifier.size(20.dp),
                                color = MaterialTheme.colorScheme.onError,
                                strokeWidth = 2.dp
                            )
                        } else {
                            Text(
                                stringResource(Res.string.delete_account_button),
                                fontWeight = FontWeight.SemiBold
                            )
                        }
                    }
                }
            }
        }
    }
}
