package com.tt.honeyDue.ui.screens

import androidx.compose.foundation.layout.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Fingerprint
import androidx.compose.material.icons.filled.Lock
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.style.TextAlign
import androidx.compose.ui.unit.dp
import com.tt.honeyDue.platform.BiometricResult
import com.tt.honeyDue.platform.rememberBiometricAuth
import com.tt.honeyDue.ui.theme.*
import honeydue.composeapp.generated.resources.*
import org.jetbrains.compose.resources.stringResource

/**
 * Lock screen shown when the app returns to foreground with biometric lock enabled.
 * Displays app logo and triggers biometric authentication.
 * Follows existing design system (OrganicDesign, theme colors).
 */
@Composable
fun BiometricLockScreen(
    onUnlocked: () -> Unit
) {
    val biometricAuth = rememberBiometricAuth()
    var authError by remember { mutableStateOf<String?>(null) }
    val promptTitle = stringResource(Res.string.biometric_prompt_title)
    val promptSubtitle = stringResource(Res.string.biometric_prompt_subtitle)

    // Auto-trigger biometric prompt on appear
    LaunchedEffect(Unit) {
        biometricAuth.authenticate(
            title = promptTitle,
            subtitle = promptSubtitle
        ) { result ->
            when (result) {
                is BiometricResult.Success -> onUnlocked()
                is BiometricResult.Failed -> authError = result.message
                is BiometricResult.NotAvailable -> onUnlocked() // Fallback: unlock if biometric unavailable
            }
        }
    }

    Surface(
        modifier = Modifier.fillMaxSize(),
        color = MaterialTheme.colorScheme.background
    ) {
        WarmGradientBackground {
            Column(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(32.dp),
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.Center
            ) {
                // Lock icon
                OrganicIconContainer(
                    icon = Icons.Default.Lock,
                    size = 96.dp,
                    iconSize = 56.dp
                )

                Spacer(modifier = Modifier.height(OrganicSpacing.xl))

                Text(
                    text = stringResource(Res.string.biometric_lock_title),
                    style = MaterialTheme.typography.headlineSmall,
                    fontWeight = FontWeight.Bold,
                    textAlign = TextAlign.Center
                )

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

                Text(
                    text = stringResource(Res.string.biometric_lock_description),
                    style = MaterialTheme.typography.bodyMedium,
                    color = MaterialTheme.colorScheme.onSurfaceVariant,
                    textAlign = TextAlign.Center
                )

                if (authError != null) {
                    Spacer(modifier = Modifier.height(OrganicSpacing.lg))

                    Text(
                        text = authError ?: "",
                        style = MaterialTheme.typography.bodySmall,
                        color = MaterialTheme.colorScheme.error,
                        textAlign = TextAlign.Center
                    )
                }

                Spacer(modifier = Modifier.height(OrganicSpacing.xl))

                // Retry button
                Button(
                    onClick = {
                        authError = null
                        biometricAuth.authenticate(
                            title = promptTitle,
                            subtitle = promptSubtitle
                        ) { result ->
                            when (result) {
                                is BiometricResult.Success -> onUnlocked()
                                is BiometricResult.Failed -> authError = result.message
                                is BiometricResult.NotAvailable -> onUnlocked()
                            }
                        }
                    },
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(56.dp),
                    shape = MaterialTheme.shapes.medium
                ) {
                    Icon(
                        imageVector = Icons.Default.Fingerprint,
                        contentDescription = null,
                        modifier = Modifier.size(24.dp)
                    )
                    Spacer(modifier = Modifier.width(OrganicSpacing.sm))
                    Text(
                        text = stringResource(Res.string.biometric_unlock_button),
                        style = MaterialTheme.typography.titleMedium,
                        fontWeight = FontWeight.SemiBold
                    )
                }
            }
        }
    }
}
