// CreateGroupView.swift
import SwiftUI
struct CreateGroupView: View {
@EnvironmentObject var appState: AppState
@Environment(\.dismiss) private var dismiss
@State private var groupName = ""
@State private var selectedPeerIDs: Set<String> = []
/// Seuls les pairs avec une clé connue peuvent être ajoutés au groupe
private var eligiblePeers: [PeerInfo] {
appState.peers.filter { $0.isKeyKnown }
}
private var canCreate: Bool {
!groupName.trimmingCharacters(in: .whitespaces).isEmpty && !selectedPeerIDs.isEmpty
}
var body: some View {
NavigationStack {
Form {
Section("Nom du groupe") {
TextField("Ex: Les amis, Équipe…", text: $groupName)
}
Section("Membres") {
if eligiblePeers.isEmpty {
Text("Aucun pair avec clé échangée disponible.\nConnecte-toi à au moins un pair d'abord.")
.font(.caption)
.foregroundColor(.secondary)
} else {
ForEach(eligiblePeers) { peer in
Button(action: { toggle(peer.id) }) {
HStack {
Image(systemName: selectedPeerIDs.contains(peer.id)
? "checkmark.circle.fill" : "circle")
.foregroundColor(selectedPeerIDs.contains(peer.id) ? .blue : .secondary)
Text(String(peer.displayName.prefix(8)).uppercased())
.foregroundColor(.primary)
Spacer()
Image(systemName: "lock.fill")
.font(.caption)
.foregroundColor(.green)
}
}
}
}
}
if !selectedPeerIDs.isEmpty {
Section {
Text("\(selectedPeerIDs.count + 1) membre(s) au total (toi inclus)")
.font(.caption)
.foregroundColor(.secondary)
Text("Chiffrement : AES-256-GCM avec clé de groupe partagée via XWing PQC")
.font(.caption)
.foregroundColor(.secondary)
}
}
}
.navigationTitle("Nouveau groupe")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Annuler") { dismiss() }
}
ToolbarItem(placement: .confirmationAction) {
Button("Créer") {
appState.createGroup(
name: groupName.trimmingCharacters(in: .whitespaces),
memberIDs: Array(selectedPeerIDs)
)
dismiss()
}
.disabled(!canCreate)
}
}
}
}
private func toggle(_ peerID: String) {
if selectedPeerIDs.contains(peerID) {
selectedPeerIDs.remove(peerID)
} else {
selectedPeerIDs.insert(peerID)
}
}
}