import SwiftUI

/// Strong, reusable confirmation for service-affecting device resets: a red
/// warning, an explicit acknowledgement the user must switch on, and a prominent
/// red action button that stays disabled until then. Presented as a sheet so it
/// can't be dismissed by an accidental background tap into a destructive action.
struct ResetConfirmSheet: View {
    let deviceKind: String       // "ONU" / "OLT"
    let deviceName: String
    let onConfirm: () -> Void

    @Environment(\.dismiss) private var dismiss
    @State private var acknowledged = false

    var body: some View {
        NavigationStack {
            Form {
                Section {
                    HStack(spacing: 12) {
                        Image(systemName: "exclamationmark.triangle.fill")
                            .font(.title2)
                            .foregroundStyle(.red)
                        Text("Service-affecting").font(.headline)
                    }
                    Text("Resetting \(deviceKind) “\(deviceName)” reboots it and interrupts service for everything connected to it. This can take several minutes and can't be undone.")
                        .font(.subheadline)
                }

                Section {
                    Toggle("I understand this will interrupt service", isOn: $acknowledged)
                        .tint(.red)
                }

                Section {
                    Button {
                        onConfirm()
                        dismiss()
                    } label: {
                        HStack {
                            Spacer()
                            Label("Reset \(deviceKind)", systemImage: "arrow.clockwise")
                                .fontWeight(.semibold)
                            Spacer()
                        }
                    }
                    .buttonStyle(.borderedProminent)
                    .tint(.red)
                    .disabled(!acknowledged)
                }
                .listRowBackground(Color.clear)
            }
            .navigationTitle("Reset \(deviceKind)?")
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .cancellationAction) {
                    Button("Cancel") { dismiss() }
                }
            }
        }
        .presentationDetents([.medium])
    }
}
