package com.tt.honeyDue.ui.screens.onboarding

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowForward
import androidx.compose.material.icons.filled.LocationOn
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.KeyboardType
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.tt.honeyDue.ui.theme.*
import com.tt.honeyDue.viewmodel.OnboardingViewModel
import honeydue.composeapp.generated.resources.*
import org.jetbrains.compose.resources.stringResource

/**
 * Onboarding step that collects the user's ZIP code to suggest
 * region-specific maintenance tasks. The backend handles all
 * ZIP → state → climate zone resolution.
 */
@Composable
fun OnboardingLocationContent(
    viewModel: OnboardingViewModel,
    onLocationDetected: (String) -> Unit,
    onSkip: () -> Unit
) {
    var zipCode by remember { mutableStateOf("") }

    WarmGradientBackground(
        modifier = Modifier.fillMaxSize()
    ) {
        Column(
            modifier = Modifier
                .fillMaxSize()
                .imePadding()
                .padding(horizontal = OrganicSpacing.xl),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Spacer(modifier = Modifier.weight(1f))

            // Header with icon
            Column(
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.spacedBy(OrganicSpacing.lg)
            ) {
                OrganicIconContainer(
                    icon = Icons.Default.LocationOn,
                    size = 100.dp,
                    iconSize = 50.dp,
                    contentDescription = null // decorative
                )

                Column(
                    horizontalAlignment = Alignment.CenterHorizontally,
                    verticalArrangement = Arrangement.spacedBy(OrganicSpacing.sm)
                ) {
                    Text(
                        text = stringResource(Res.string.onboarding_location_title),
                        style = MaterialTheme.typography.headlineMedium,
                        fontWeight = FontWeight.Bold,
                        color = MaterialTheme.colorScheme.onBackground,
                        textAlign = TextAlign.Center
                    )

                    Text(
                        text = stringResource(Res.string.onboarding_location_subtitle),
                        style = MaterialTheme.typography.bodyLarge,
                        color = MaterialTheme.colorScheme.onSurfaceVariant,
                        textAlign = TextAlign.Center
                    )
                }
            }

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

            // ZIP code input
            Text(
                text = stringResource(Res.string.onboarding_location_enter_zip_prompt),
                style = MaterialTheme.typography.bodyMedium,
                color = MaterialTheme.colorScheme.onSurfaceVariant,
                fontWeight = FontWeight.Medium
            )

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

            OutlinedTextField(
                value = zipCode,
                onValueChange = { newValue ->
                    // Only allow digits, max 5 characters
                    if (newValue.length <= 5 && newValue.all { it.isDigit() }) {
                        zipCode = newValue
                    }
                },
                placeholder = {
                    Text(
                        stringResource(Res.string.onboarding_location_zip_placeholder),
                        color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.6f)
                    )
                },
                modifier = Modifier.width(180.dp),
                shape = RoundedCornerShape(OrganicRadius.md),
                singleLine = true,
                textStyle = MaterialTheme.typography.headlineSmall.copy(
                    fontWeight = FontWeight.SemiBold,
                    textAlign = TextAlign.Center
                ),
                keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
                colors = OutlinedTextFieldDefaults.colors(
                    focusedBorderColor = MaterialTheme.colorScheme.primary,
                    unfocusedBorderColor = MaterialTheme.colorScheme.outline.copy(alpha = 0.5f)
                )
            )

            Spacer(modifier = Modifier.weight(1f))

            // Continue button — passes ZIP directly to backend for resolution
            OrganicPrimaryButton(
                text = stringResource(Res.string.onboarding_continue),
                onClick = { onLocationDetected(zipCode) },
                modifier = Modifier.fillMaxWidth(),
                enabled = zipCode.length == 5,
                icon = Icons.AutoMirrored.Filled.ArrowForward
            )

            Spacer(modifier = Modifier.height(OrganicSpacing.xl * 2))
        }
    }
}
