import SwiftUI

struct ErrorView: View {
    let error: NetworkError
    let onRetry: (() -> Void)?
    let onDismiss: (() -> Void)?

    init(error: NetworkError, onRetry: (() -> Void)? = nil, onDismiss: (() -> Void)? = nil) {
        self.error = error
        self.onRetry = onRetry
        self.onDismiss = onDismiss
    }

    var body: some View {
        VStack(spacing: 40) {
            Image(systemName: errorIcon)
                .font(.system(size: 100))
                .foregroundColor(.red)

            VStack(spacing: 20) {
                Text("Error")
                    .font(.system(size: 60, weight: .bold))
                    .foregroundColor(.white)

                Text(error.localizedDescription)
                    .font(.title2)
                    .foregroundColor(.gray)
                    .multilineTextAlignment(.center)
                    .padding(.horizontal, 100)

                if let suggestion = error.recoverySuggestion {
                    Text(suggestion)
                        .font(.title3)
                        .foregroundColor(.gray.opacity(0.8))
                        .multilineTextAlignment(.center)
                        .padding(.horizontal, 100)
                }
            }

            HStack(spacing: 30) {
                if let onRetry = onRetry {
                    Button(action: onRetry) {
                        HStack {
                            Image(systemName: "arrow.clockwise")
                            Text("Retry")
                        }
                        .font(.title2)
                        .foregroundColor(.white)
                        .padding()
                        .frame(width: 250)
                        .background(Color.blue)
                        .cornerRadius(10)
                    }
                }

                if let onDismiss = onDismiss {
                    Button(action: onDismiss) {
                        Text("Dismiss")
                            .font(.title2)
                            .foregroundColor(.white)
                            .padding()
                            .frame(width: 250)
                            .background(Color.gray.opacity(0.5))
                            .cornerRadius(10)
                    }
                }
            }
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(Color.black)
    }

    private var errorIcon: String {
        switch error {
        case .noInternetConnection:
            return "wifi.slash"
        case .timeout:
            return "clock.badge.exclamationmark"
        case .authenticationFailed:
            return "lock.slash"
        case .serverError:
            return "server.rack"
        default:
            return "exclamationmark.triangle"
        }
    }
}

#Preview {
    ErrorView(
        error: .noInternetConnection,
        onRetry: {},
        onDismiss: {}
    )
}

#Preview("Authentication Failed") {
    ErrorView(
        error: .authenticationFailed,
        onRetry: {}
    )
}
