import SwiftUI

struct PasswordEntryView: View {
    @EnvironmentObject var appState: AppState
    @StateObject private var viewModel = AuthenticationViewModel()
    @FocusState private var isPasswordFieldFocused: Bool

    var body: some View {
        ZStack {
            Color.black.ignoresSafeArea()

            VStack(spacing: 60) {
                VStack(spacing: 20) {
                    Text(Constants.App.name)
                        .font(.system(size: 80, weight: .bold))
                        .foregroundColor(.white)

                    Text("Enter your password to continue")
                        .font(.title2)
                        .foregroundColor(.gray)
                }

                VStack(spacing: 40) {
                    HStack {
                        if viewModel.isPasswordVisible {
                            TextField("Password", text: $viewModel.password)
                                .font(.title)
                                .padding()
                                .frame(width: 600)
                                .background(Color.white.opacity(0.1))
                                .cornerRadius(10)
                                .textInputAutocapitalization(.never)
                                .autocorrectionDisabled()
                                .focused($isPasswordFieldFocused)
                        } else {
                            SecureField("Password", text: $viewModel.password)
                                .font(.title)
                                .padding()
                                .frame(width: 600)
                                .background(Color.white.opacity(0.1))
                                .cornerRadius(10)
                                .textInputAutocapitalization(.never)
                                .autocorrectionDisabled()
                                .focused($isPasswordFieldFocused)
                        }

                        Button(action: {
                            viewModel.isPasswordVisible.toggle()
                        }) {
                            Image(systemName: viewModel.isPasswordVisible ? "eye.slash.fill" : "eye.fill")
                                .font(.title)
                                .foregroundColor(.white)
                        }
                        .buttonStyle(.plain)
                    }

                    Button(action: {
                        Task {
                            await appState.authenticate(password: viewModel.password)
                        }
                    }) {
                        Text("Sign In")
                            .font(.title2)
                            .fontWeight(.semibold)
                            .foregroundColor(.white)
                            .frame(width: 300, height: 80)
                            .background(viewModel.isPasswordValid ? Color.blue : Color.gray)
                            .cornerRadius(15)
                    }
                    .disabled(!viewModel.isPasswordValid || appState.isLoading)
                }

                if appState.isLoading {
                    ProgressView()
                        .scaleEffect(2.0)
                        .tint(.white)
                }

                if let error = appState.error {
                    VStack(spacing: 15) {
                        Text(error.localizedDescription)
                            .font(.title3)
                            .foregroundColor(.red)
                            .multilineTextAlignment(.center)

                        if let suggestion = error.recoverySuggestion {
                            Text(suggestion)
                                .font(.body)
                                .foregroundColor(.gray)
                                .multilineTextAlignment(.center)
                        }

                        Button("Dismiss") {
                            appState.clearError()
                        }
                        .buttonStyle(.plain)
                        .foregroundColor(.white)
                    }
                    .padding()
                    .frame(maxWidth: 800)
                }
            }
        }
        .onAppear {
            isPasswordFieldFocused = true
        }
    }
}

#Preview {
    PasswordEntryView()
        .environmentObject(AppState(developmentMode: false))
}
