import SwiftUI
import Bedrock

/// A sheet for recording contact details when sharing a card.
struct RecordContactSheet: View {
    @Environment(\.dismiss) private var dismiss
    @Binding var recipientName: String
    @Binding var recipientRole: String
    @Binding var recipientCompany: String
    let onSave: () -> Void
    
    private var isValid: Bool {
        !recipientName.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
    }
    
    var body: some View {
        NavigationStack {
            Form {
                Section(String.localized("Recipient Details")) {
                    TextField(String.localized("Name"), text: $recipientName)
                        .textContentType(.name)
                    
                    TextField(String.localized("Role (optional)"), text: $recipientRole)
                        .textContentType(.jobTitle)
                    
                    TextField(String.localized("Company (optional)"), text: $recipientCompany)
                        .textContentType(.organizationName)
                }
                
                Section {
                    Text("This person will appear in your Contacts tab so you can track who has your card.")
                        .font(.footnote)
                        .foregroundStyle(Color.Text.secondary)
                }
            }
            .navigationTitle(String.localized("Track Share"))
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .cancellationAction) {
                    Button(String.localized("Cancel")) {
                        dismiss()
                    }
                }
                
                ToolbarItem(placement: .confirmationAction) {
                    Button(String.localized("Save")) {
                        onSave()
                        dismiss()
                    }
                    .disabled(!isValid)
                }
            }
        }
        .presentationDetents([.medium])
    }
}

#Preview {
    RecordContactSheet(
        recipientName: .constant(""),
        recipientRole: .constant(""),
        recipientCompany: .constant("")
    ) {
        Design.debugLog("Saved")
    }
}
