import SwiftUI

struct ErrorBanner: View {
    let error: BookStackError
    var onRetry: (() -> Void)? = nil
    var onSettings: (() -> Void)? = nil

    var body: some View {
        HStack(spacing: 12) {
            Image(systemName: "exclamationmark.triangle.fill")
                .foregroundStyle(.orange)
                .font(.title3)

            Text(error.errorDescription ?? "An unknown error occurred.")
                .font(.callout)
                .foregroundStyle(.primary)
                .fixedSize(horizontal: false, vertical: true)

            Spacer()

            if case .unauthorized = error, let onSettings {
                Button("Settings", action: onSettings)
                    .buttonStyle(.bordered)
                    .controlSize(.small)
            } else if let onRetry {
                Button("Retry", action: onRetry)
                    .buttonStyle(.bordered)
                    .controlSize(.small)
            }
        }
        .padding()
        .background(.regularMaterial, in: RoundedRectangle(cornerRadius: 12))
        .accessibilityElement(children: .combine)
        .accessibilityLabel("Error: \(error.errorDescription ?? "Unknown error")")
    }
}

#Preview {
    VStack(spacing: 16) {
        ErrorBanner(error: .networkUnavailable, onRetry: {})
        ErrorBanner(error: .unauthorized)
        ErrorBanner(error: .timeout, onRetry: {})
    }
    .padding()
}
