import SwiftUI

struct ForgotPasswordView: View {
    @ObservedObject var viewModel: PasswordResetViewModel
    @FocusState private var isEmailFocused: Bool
    @Environment(\.dismiss) var dismiss

    var body: some View {
            ZStack {
                WarmGradientBackground()

                ScrollView(showsIndicators: false) {
                    VStack(spacing: OrganicSpacing.spacious) {
                        Spacer()
                            .frame(height: OrganicSpacing.comfortable)

                        // Hero Section
                        VStack(spacing: OrganicSpacing.comfortable) {
                            ZStack {
                                Circle()
                                    .fill(
                                        RadialGradient(
                                            colors: [
                                                Color.appPrimary.opacity(0.15),
                                                Color.appPrimary.opacity(0.05),
                                                Color.clear
                                            ],
                                            center: .center,
                                            startRadius: 0,
                                            endRadius: 60
                                        )
                                    )
                                    .frame(width: 120, height: 120)

                                Image(systemName: "key.fill")
                                    .font(.system(size: 48, weight: .medium))
                                    .foregroundColor(Color.appPrimary)
                            }

                            VStack(spacing: 8) {
                                Text("Forgot Password?")
                                    .font(.system(size: 26, weight: .bold, design: .rounded))
                                    .foregroundColor(Color.appTextPrimary)
                                    .a11yHeader()

                                Text("Enter your email address and we'll send you a verification code")
                                    .font(.system(size: 15, weight: .medium))
                                    .foregroundColor(Color.appTextSecondary)
                                    .multilineTextAlignment(.center)
                                    .padding(.horizontal)
                            }
                        }

                        // Form Card
                        VStack(spacing: 20) {
                            // Email Field
                            VStack(alignment: .leading, spacing: 8) {
                                Text("EMAIL")
                                    .font(.system(size: 11, weight: .semibold, design: .rounded))
                                    .foregroundColor(Color.appTextSecondary)
                                    .tracking(1.2)

                                HStack(spacing: 12) {
                                    ZStack {
                                        Circle()
                                            .fill(Color.appPrimary.opacity(0.1))
                                            .frame(width: 32, height: 32)
                                        Image(systemName: "envelope.fill")
                                            .font(.system(size: 14, weight: .medium))
                                            .foregroundColor(Color.appPrimary)
                                    }

                                    TextField("Email Address", text: $viewModel.email)
                                        .font(.system(size: 16, weight: .medium))
                                        .textInputAutocapitalization(.never)
                                        .autocorrectionDisabled()
                                        .keyboardType(.emailAddress)
                                        .focused($isEmailFocused)
                                        .submitLabel(.go)
                                        .onSubmit {
                                            viewModel.requestPasswordReset()
                                        }
                                        .onChange(of: viewModel.email) { _, _ in
                                            viewModel.clearError()
                                        }
                                        .accessibilityIdentifier(AccessibilityIdentifiers.PasswordReset.emailField)
                                        .accessibilityHint("Enter your account email address")
                                }
                                .padding(16)
                                .background(Color.appBackgroundPrimary.opacity(0.5))
                                .clipShape(RoundedRectangle(cornerRadius: 16, style: .continuous))
                                .overlay(
                                    RoundedRectangle(cornerRadius: 16, style: .continuous)
                                        .stroke(isEmailFocused ? Color.appPrimary : Color.appTextSecondary.opacity(0.15), lineWidth: 1.5)
                                )
                                .animation(.easeInOut(duration: 0.2), value: isEmailFocused)

                                Text("We'll send a 6-digit verification code to this address")
                                    .font(.system(size: 12, weight: .medium))
                                    .foregroundColor(Color.appTextSecondary)
                            }

                            // Error Message
                            if let errorMessage = viewModel.errorMessage {
                                HStack(spacing: 10) {
                                    Image(systemName: "exclamationmark.circle.fill")
                                        .foregroundColor(Color.appError)
                                    Text(errorMessage)
                                        .font(.system(size: 14, weight: .medium))
                                        .foregroundColor(Color.appError)
                                    Spacer()
                                }
                                .padding(16)
                                .background(Color.appError.opacity(0.1))
                                .clipShape(RoundedRectangle(cornerRadius: 16, style: .continuous))
                            }

                            // Success Message
                            if let successMessage = viewModel.successMessage {
                                HStack(spacing: 10) {
                                    Image(systemName: "checkmark.circle.fill")
                                        .foregroundColor(Color.appAccent)
                                    Text(successMessage)
                                        .font(.system(size: 14, weight: .medium))
                                        .foregroundColor(Color.appAccent)
                                    Spacer()
                                }
                                .padding(16)
                                .background(Color.appAccent.opacity(0.1))
                                .clipShape(RoundedRectangle(cornerRadius: 16, style: .continuous))
                            }

                            // Send Code Button
                            Button(action: {
                                viewModel.requestPasswordReset()
                            }) {
                                HStack(spacing: 8) {
                                    if viewModel.isLoading {
                                        ProgressView()
                                            .progressViewStyle(CircularProgressViewStyle(tint: .white))
                                    } else {
                                        Image(systemName: "envelope.fill")
                                    }
                                    Text(viewModel.isLoading ? "Sending..." : "Send Reset Code")
                                        .font(.headline)
                                        .fontWeight(.semibold)
                                }
                                .frame(maxWidth: .infinity)
                                .frame(height: 56)
                                .foregroundColor(Color.appTextOnPrimary)
                                .background(
                                    !viewModel.email.isEmpty && !viewModel.isLoading
                                        ? AnyShapeStyle(LinearGradient(colors: [Color.appPrimary, Color.appPrimary.opacity(0.8)], startPoint: .topLeading, endPoint: .bottomTrailing))
                                        : AnyShapeStyle(Color.appTextSecondary)
                                )
                                .clipShape(RoundedRectangle(cornerRadius: 12, style: .continuous))
                                .shadow(
                                    color: !viewModel.email.isEmpty && !viewModel.isLoading ? Color.appPrimary.opacity(0.3) : .clear,
                                    radius: 10,
                                    y: 5
                                )
                            }
                            .disabled(viewModel.email.isEmpty || viewModel.isLoading)
                            .accessibilityIdentifier(AccessibilityIdentifiers.PasswordReset.sendCodeButton)
                            .accessibilityHint("Double tap to send a verification code to your email")

                            // Back to Login
                            Button(action: { dismiss() }) {
                                Text("Back to Login")
                                    .font(.system(size: 15, weight: .semibold, design: .rounded))
                                    .foregroundColor(Color.appTextSecondary)
                            }
                            .padding(.top, 8)
                            .accessibilityIdentifier(AccessibilityIdentifiers.PasswordReset.backToLoginButton)
                        }
                        .padding(OrganicSpacing.cozy)
                        .background(OrganicFormCardBackground())
                        .clipShape(RoundedRectangle(cornerRadius: 32, style: .continuous))
                        .naturalShadow(.pronounced)
                        .padding(.horizontal, 16)

                        Spacer()
                    }
                }
            }
            .navigationBarTitleDisplayMode(.inline)
            .onAppear {
                isEmailFocused = true
            }
    }
}

// MARK: - Organic Form Card Background

private struct OrganicFormCardBackground: View {
    @Environment(\.colorScheme) var colorScheme

    var body: some View {
        ZStack {
            Color.appBackgroundSecondary

            GeometryReader { geo in
                OrganicBlobShape(variation: 0)
                    .fill(
                        RadialGradient(
                            colors: [
                                Color.appPrimary.opacity(colorScheme == .dark ? 0.08 : 0.05),
                                Color.appPrimary.opacity(0.01)
                            ],
                            center: .center,
                            startRadius: 0,
                            endRadius: geo.size.width * 0.5
                        )
                    )
                    .frame(width: geo.size.width * 0.6, height: geo.size.height * 0.5)
                    .offset(x: geo.size.width * 0.45, y: -geo.size.height * 0.1)
                    .blur(radius: 20)
            }

            GrainTexture(opacity: 0.015)
        }
    }
}

#Preview {
    ForgotPasswordView(viewModel: PasswordResetViewModel())
}
