synapse 1.1.0

Neural Communication Network with Federated Identity and Blockchain Trust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
//! Unified Transport Abstraction Layer for Synapse
//! 
//! This module provides a unified interface that sits above all transport mechanisms,
//! making applications transport-agnostic while providing intelligent transport selection,
//! failover, and optimization capabilities.

use crate::{
    types::SecureMessage,
    error::Result,
};
use async_trait::async_trait;
use std::{
    time::{Duration, Instant},
    collections::HashMap,
};
use dashmap::DashMap;
use serde::{Serialize, Deserialize};
use super::router::ConnectionOffer;


/// Unified transport interface that all transport mechanisms must implement
#[async_trait]
pub trait Transport: Send + Sync {
    /// Get the transport type identifier
    fn transport_type(&self) -> TransportType;
    
    /// Get transport capabilities
    fn capabilities(&self) -> TransportCapabilities;
    
    /// Check if this transport can reach a specific target
    async fn can_reach(&self, target: &TransportTarget) -> bool;
    
    /// Get estimated metrics for reaching a target
    async fn estimate_metrics(&self, target: &TransportTarget) -> Result<TransportEstimate>;
    
    /// Send a message via this transport
    async fn send_message(&self, target: &TransportTarget, message: &SecureMessage) -> Result<DeliveryReceipt>;
    
    /// Receive messages from this transport
    async fn receive_messages(&self) -> Result<Vec<IncomingMessage>>;
    
    /// Test connectivity to a target
    async fn test_connectivity(&self, target: &TransportTarget) -> Result<ConnectivityResult>;
    
    /// Start the transport (if needed)
    async fn start(&self) -> Result<()>;
    
    /// Stop the transport gracefully
    async fn stop(&self) -> Result<()>;
    
    /// Get current transport status
    async fn status(&self) -> TransportStatus;
    
    /// Get transport metrics
    async fn metrics(&self) -> TransportMetrics;

    /// Send a connection offer to a target
    async fn send_connection_offer(&self, target: &str, offer: ConnectionOffer) -> Result<String> {
        let _ = (target, offer);
        Err(crate::error::SynapseError::TransportError("Connection offers not supported by this transport".to_string()))
    }
}

/// Transport types supported by the system
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum TransportType {
    Tcp,
    Udp,
    WebSocket,
    Http,
    Email,
    AutoDiscovery, // Replaces Mdns with more comprehensive discovery
    Quic,
    Custom(u32), // For extensibility
}

impl std::fmt::Display for TransportType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            TransportType::Tcp => write!(f, "TCP"),
            TransportType::Udp => write!(f, "UDP"),
            TransportType::WebSocket => write!(f, "WebSocket"),
            TransportType::Http => write!(f, "HTTP"),
            TransportType::Email => write!(f, "Email"),
            TransportType::AutoDiscovery => write!(f, "Auto-Discovery"),
            TransportType::Quic => write!(f, "QUIC"),
            TransportType::Custom(id) => write!(f, "Custom({})", id),
        }
    }
}

/// Transport capabilities
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransportCapabilities {
    /// Maximum message size supported
    pub max_message_size: usize,
    /// Whether transport supports reliable delivery
    pub reliable: bool,
    /// Whether transport supports real-time communication
    pub real_time: bool,
    /// Whether transport supports broadcast/multicast
    pub broadcast: bool,
    /// Whether transport supports bidirectional communication
    pub bidirectional: bool,
    /// Whether transport supports encryption
    pub encrypted: bool,
    /// Whether transport works across networks (not just local)
    pub network_spanning: bool,
    /// Supported message urgency levels
    pub supported_urgencies: Vec<MessageUrgency>,
    /// Transport-specific features
    pub features: Vec<String>,
}

/// Message urgency levels for transport selection
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum MessageUrgency {
    /// Immediate delivery required (< 100ms)
    Critical,
    /// Real-time delivery preferred (< 1s)
    RealTime,
    /// Interactive response time (< 5s)
    Interactive,
    /// Background processing acceptable
    Background,
    /// Store and forward acceptable
    Batch,
}

/// Transport target specification
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransportTarget {
    /// Primary identifier (entity ID, email, IP, etc.)
    pub identifier: String,
    /// Optional specific address/endpoint
    pub address: Option<String>,
    /// Preferred transport types (in order of preference)
    pub preferred_transports: Vec<TransportType>,
    /// Required capabilities
    pub required_capabilities: Vec<String>,
    /// Urgency level for this target
    pub urgency: MessageUrgency,
}

impl TransportTarget {
    pub fn new(identifier: String) -> Self {
        Self {
            identifier,
            address: None,
            preferred_transports: Vec::new(),
            required_capabilities: Vec::new(),
            urgency: MessageUrgency::Interactive,
        }
    }
    
    pub fn with_address(mut self, address: String) -> Self {
        self.address = Some(address);
        self
    }
    
    pub fn with_urgency(mut self, urgency: MessageUrgency) -> Self {
        self.urgency = urgency;
        self
    }
    
    pub fn prefer_transport(mut self, transport: TransportType) -> Self {
        self.preferred_transports.push(transport);
        self
    }
    
    pub fn require_capability(mut self, capability: String) -> Self {
        self.required_capabilities.push(capability);
        self
    }
}

/// Transport performance estimate
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransportEstimate {
    /// Estimated latency
    pub latency: Duration,
    /// Estimated reliability (0.0-1.0)
    pub reliability: f64,
    /// Estimated bandwidth (bytes/sec)
    pub bandwidth: u64,
    /// Estimated cost (arbitrary units)
    pub cost: f64,
    /// Whether the transport is currently available
    pub available: bool,
    /// Confidence in these estimates (0.0-1.0)
    pub confidence: f64,
}

/// Delivery receipt from successful message send
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeliveryReceipt {
    /// Unique message ID
    pub message_id: String,
    /// Transport used for delivery
    pub transport_used: TransportType,
    /// Actual delivery time
    pub delivery_time: Duration,
    /// Target that was reached
    pub target_reached: String,
    /// Delivery confirmation level
    pub confirmation: DeliveryConfirmation,
    /// Additional metadata
    pub metadata: HashMap<String, String>,
}

/// Levels of delivery confirmation
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum DeliveryConfirmation {
    /// Message was sent to transport layer
    Sent,
    /// Message was delivered to target network
    Delivered,
    /// Message was received by target application
    Received,
    /// Message was acknowledged by target
    Acknowledged,
}

/// Incoming message with transport context
#[derive(Debug, Clone)]
pub struct IncomingMessage {
    /// The message content
    pub message: SecureMessage,
    /// Transport that received the message
    pub transport_type: TransportType,
    /// Source address/identifier
    pub source: String,
    /// When the message was received (as timestamp)
    pub received_timestamp: u64,
    /// Additional transport-specific metadata
    pub metadata: HashMap<String, String>,
}

impl IncomingMessage {
    /// Create a new incoming message with current timestamp
    pub fn new(message: SecureMessage, transport_type: TransportType, source: String) -> Self {
        Self {
            message,
            transport_type,
            source,
            received_timestamp: std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs(),
            metadata: HashMap::new(),
        }
    }
    
    /// Get received time as Duration since epoch
    pub fn received_at(&self) -> Duration {
        Duration::from_secs(self.received_timestamp)
    }
}

/// Result of connectivity test
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConnectivityResult {
    /// Whether connection was successful
    pub connected: bool,
    /// Round-trip time if successful
    pub rtt: Option<Duration>,
    /// Error message if failed
    pub error: Option<String>,
    /// Connection quality score (0.0-1.0)
    pub quality: f64,
    /// Additional test results
    pub details: HashMap<String, String>,
}

/// Current transport status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TransportStatus {
    /// Transport is not yet started
    Stopped,
    /// Transport is starting up
    Starting,
    /// Transport is running normally
    Running,
    /// Transport is experiencing issues but functional
    Degraded,
    /// Transport is not functional
    Failed,
    /// Transport is shutting down
    Stopping,
}

/// Transport performance metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransportMetrics {
    /// Transport type
    pub transport_type: TransportType,
    /// Number of messages sent
    pub messages_sent: u64,
    /// Number of messages received
    pub messages_received: u64,
    /// Number of send failures
    pub send_failures: u64,
    /// Number of receive failures
    pub receive_failures: u64,
    /// Total bytes sent
    pub bytes_sent: u64,
    /// Total bytes received
    pub bytes_received: u64,
    /// Average latency in milliseconds
    pub average_latency_ms: u64,
    /// Current reliability score (0.0-1.0)
    pub reliability_score: f64,
    /// Number of active connections
    pub active_connections: u32,
    /// Last update time as Unix timestamp
    pub last_updated_timestamp: u64,
    /// Transport-specific metrics
    pub custom_metrics: HashMap<String, f64>,
}

impl Default for TransportMetrics {
    fn default() -> Self {
        Self {
            transport_type: TransportType::Tcp,
            messages_sent: 0,
            messages_received: 0,
            send_failures: 0,
            receive_failures: 0,
            bytes_sent: 0,
            bytes_received: 0,
            average_latency_ms: 0,
            reliability_score: 1.0,
            active_connections: 0,
            last_updated_timestamp: std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs(),
            custom_metrics: HashMap::new(),
        }
    }
}

impl TransportMetrics {
    /// Get average latency as Duration
    pub fn average_latency(&self) -> Duration {
        Duration::from_millis(self.average_latency_ms)
    }
    
    /// Set average latency from Duration
    pub fn set_average_latency(&mut self, latency: Duration) {
        self.average_latency_ms = latency.as_millis() as u64;
    }
    
    /// Get last updated time as Instant (approximate)
    pub fn last_updated(&self) -> Instant {
        // This is approximate since we can't perfectly convert back to Instant
        Instant::now() // For now, just return current time
    }
    
    /// Update the last updated timestamp to now
    pub fn touch(&mut self) {
        self.last_updated_timestamp = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();
    }
}

/// Delivery estimate for UI display
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeliveryEstimate {
    /// Estimated latency
    pub latency: Duration,
    /// Estimated reliability (0.0-1.0)  
    pub reliability: f64,
    /// Estimated throughput in bytes per second
    pub throughput_estimate: u64,
    /// Cost score (lower is better)
    pub cost_score: f64,
}

/// Helper implementations for common capability sets
impl TransportCapabilities {
    /// TCP transport capabilities
    pub fn tcp() -> Self {
        Self {
            max_message_size: 64 * 1024 * 1024, // 64MB
            reliable: true,
            real_time: false,
            broadcast: false,
            bidirectional: true,
            encrypted: false, // Depends on TLS
            network_spanning: true,
            supported_urgencies: vec![
                MessageUrgency::Interactive,
                MessageUrgency::Background,
                MessageUrgency::Batch,
            ],
            features: vec![
                "connection_oriented".to_string(),
                "stream_based".to_string(),
                "flow_control".to_string(),
            ],
        }
    }
    
    /// UDP transport capabilities
    pub fn udp() -> Self {
        Self {
            max_message_size: 65507, // Max UDP payload
            reliable: false,
            real_time: true,
            broadcast: true,
            bidirectional: true,
            encrypted: false,
            network_spanning: true,
            supported_urgencies: vec![
                MessageUrgency::Critical,
                MessageUrgency::RealTime,
                MessageUrgency::Interactive,
            ],
            features: vec![
                "connectionless".to_string(),
                "datagram_based".to_string(),
                "low_overhead".to_string(),
                "multicast".to_string(),
            ],
        }
    }
    
    /// Email transport capabilities
    pub fn email() -> Self {
        Self {
            max_message_size: 25 * 1024 * 1024, // 25MB typical limit
            reliable: true,
            real_time: false,
            broadcast: true,
            bidirectional: true,
            encrypted: true, // Usually TLS
            network_spanning: true,
            supported_urgencies: vec![
                MessageUrgency::Background,
                MessageUrgency::Batch,
            ],
            features: vec![
                "store_and_forward".to_string(),
                "federation".to_string(),
                "authentication".to_string(),
                "persistent".to_string(),
            ],
        }
    }
    
    /// Auto-Discovery transport capabilities
    pub fn auto_discovery() -> Self {
        Self {
            max_message_size: 1024, // Smallest for discovery
            reliable: false,
            real_time: true,
            broadcast: true,
            bidirectional: true,
            encrypted: false,
            network_spanning: false, // Local network only
            supported_urgencies: vec![
                MessageUrgency::Critical,
                MessageUrgency::RealTime,
            ],
            features: vec![
                "service_discovery".to_string(),
                "zero_configuration".to_string(),
                "local_network".to_string(),
                "multicast".to_string(),
            ],
        }
    }
    
    /// WebSocket transport capabilities
    pub fn websocket() -> Self {
        Self {
            max_message_size: 16 * 1024 * 1024, // 16MB typical
            reliable: true,
            real_time: true,
            broadcast: false,
            bidirectional: true,
            encrypted: true, // WSS
            network_spanning: true,
            supported_urgencies: vec![
                MessageUrgency::Critical,
                MessageUrgency::RealTime,
                MessageUrgency::Interactive,
            ],
            features: vec![
                "web_compatible".to_string(),
                "full_duplex".to_string(),
                "frame_based".to_string(),
                "http_upgrade".to_string(),
            ],
        }
    }
    
    /// QUIC transport capabilities
    pub fn quic() -> Self {
        Self {
            max_message_size: 1024 * 1024 * 1024, // 1GB theoretical
            reliable: true,
            real_time: true,
            broadcast: false,
            bidirectional: true,
            encrypted: true, // Built-in TLS 1.3
            network_spanning: true,
            supported_urgencies: vec![
                MessageUrgency::Critical,
                MessageUrgency::RealTime,
                MessageUrgency::Interactive,
                MessageUrgency::Background,
            ],
            features: vec![
                "multiplexed_streams".to_string(),
                "zero_rtt".to_string(),
                "connection_migration".to_string(),
                "modern_crypto".to_string(),
                "congestion_control".to_string(),
            ],
        }
    }
    
    /// HTTP transport capabilities
    pub fn http() -> Self {
        Self {
            max_message_size: 10 * 1024 * 1024, // 10MB typical limit
            reliable: true,
            real_time: false, // HTTP has higher latency
            broadcast: false,
            bidirectional: true,
            encrypted: false, // Depends on HTTPS
            network_spanning: true,
            supported_urgencies: vec![
                MessageUrgency::Interactive,
                MessageUrgency::Background,
                MessageUrgency::Batch,
            ],
            features: vec![
                "firewall_friendly".to_string(),
                "web_compatible".to_string(),
                "request_response".to_string(),
                "standard_protocol".to_string(),
            ],
        }
    }
    
    /// HTTPS transport capabilities
    pub fn https() -> Self {
        let mut caps = Self::http();
        caps.encrypted = true;
        caps.features.push("encrypted".to_string());
        caps.features.push("authenticated".to_string());
        caps
    }
}

/// Transport factory trait for creating transport instances
#[async_trait]
pub trait TransportFactory: Send + Sync {
    /// Create a new transport instance
    async fn create_transport(&self, config: &HashMap<String, String>) -> Result<Box<dyn Transport>>;
    
    /// Get the transport type this factory creates
    fn transport_type(&self) -> TransportType;
    
    /// Get default configuration for this transport
    fn default_config(&self) -> HashMap<String, String>;
    
    /// Validate configuration for this transport
    fn validate_config(&self, config: &HashMap<String, String>) -> Result<()>;
}

// Factory implementations for unified transports

/// TCP Transport Factory
pub struct TcpTransportFactory;

#[async_trait]
impl TransportFactory for TcpTransportFactory {
    async fn create_transport(&self, _config: &HashMap<String, String>) -> Result<Box<dyn Transport>> {
        let transport = crate::transport::tcp_simple::SimpleTcpTransport::new();
        Ok(Box::new(transport))
    }
    
    fn transport_type(&self) -> TransportType {
        TransportType::Tcp
    }
    
    fn default_config(&self) -> HashMap<String, String> {
        let mut config = HashMap::new();
        config.insert("listen_port".to_string(), "0".to_string());
        config.insert("connection_timeout_ms".to_string(), "30000".to_string());
        config.insert("max_message_size".to_string(), "1048576".to_string()); // 1MB
        config
    }
    
    fn validate_config(&self, config: &HashMap<String, String>) -> Result<()> {
        if let Some(port_str) = config.get("listen_port") {
            if port_str.parse::<u16>().is_err() {
                return Err(crate::error::SynapseError::Config(
                    "Invalid port number".to_string()
                ));
            }
        }
        Ok(())
    }
}

/// UDP Transport Factory (temporarily disabled)
// pub struct UdpTransportFactory;
// 
// #[async_trait]
// impl TransportFactory for UdpTransportFactory {
//     async fn create_transport(&self, config: &HashMap<String, String>) -> Result<Box<dyn Transport>> {
//         // TODO: Implement simple UDP transport
//         Err(crate::error::SynapseError::TransportError("UDP transport not yet implemented".to_string()))
//     }
//     
//     fn transport_type(&self) -> TransportType {
//         TransportType::Udp
//     }
//     
//     fn default_config(&self) -> HashMap<String, String> {
//         HashMap::new()
//     }
//
//     fn validate_config(&self, config: &HashMap<String, String>) -> Result<()> {
//         Ok(())
//     }
// }

/// Email Transport Factory (temporarily disabled)
pub struct EmailTransportFactory;

#[async_trait]
impl TransportFactory for EmailTransportFactory {
    async fn create_transport(&self, _config: &HashMap<String, String>) -> Result<Box<dyn Transport>> {
        Err(crate::error::SynapseError::TransportError("Email transport not yet implemented".to_string()))
    }
    
    fn transport_type(&self) -> TransportType {
        TransportType::Email
    }
    
    fn default_config(&self) -> HashMap<String, String> {
        HashMap::new()
    }
    
    fn validate_config(&self, _config: &HashMap<String, String>) -> Result<()> {
        Ok(())
    }
}
// }

// mDNS Transport Factory (COMMENTED OUT - TO BE FIXED)
pub struct MdnsTransportFactory;

#[async_trait]
impl TransportFactory for MdnsTransportFactory {
    async fn create_transport(&self, _config: &HashMap<String, String>) -> Result<Box<dyn Transport>> {
        // TODO: Re-enable mDNS transport after trait compatibility is fixed
        Err(crate::error::SynapseError::TransportError("mDNS transport temporarily disabled".to_string()))
    }
    
    fn transport_type(&self) -> TransportType {
        TransportType::AutoDiscovery
    }
    
    fn default_config(&self) -> HashMap<String, String> {
        let mut config = HashMap::new();
        config.insert("service_name".to_string(), "_synapse._tcp.local".to_string());
        config.insert("local_port".to_string(), "0".to_string());
        config.insert("discovery_timeout_ms".to_string(), "5000".to_string());
        config.insert("max_message_size".to_string(), "65507".to_string()); // Max UDP
        config
    }
    
    fn validate_config(&self, config: &HashMap<String, String>) -> Result<()> {
        if let Some(service_name) = config.get("service_name") {
            if !service_name.contains("._tcp.") && !service_name.contains("._udp.") {
                return Err(crate::error::SynapseError::Config(
                    "Service name must include protocol (_tcp. or _udp.)".to_string()
                ));
            }
        }
        Ok(())
    }
}

// WebSocket Transport Factory (COMMENTED OUT - TO BE FIXED)
/*
pub struct WebSocketTransportFactory;

#[async_trait]
impl TransportFactory for WebSocketTransportFactory {
    async fn create_transport(&self, config: &HashMap<String, String>) -> Result<Box<dyn Transport>> {
        let transport = crate::transport::websocket_unified::WebSocketTransportImpl::new(config).await?;
        Ok(Box::new(transport))
    }
    
    fn transport_type(&self) -> TransportType {
        TransportType::WebSocket
    }
    
    fn default_config(&self) -> HashMap<String, String> {
        let mut config = HashMap::new();
        config.insert("local_port".to_string(), "0".to_string());
        config.insert("connection_timeout_ms".to_string(), "30000".to_string());
        config.insert("max_message_size".to_string(), "16777216".to_string()); // 16MB
        config
    }
    
    fn validate_config(&self, config: &HashMap<String, String>) -> Result<()> {
        if let Some(port_str) = config.get("local_port") {
            if port_str.parse::<u16>().is_err() {
                return Err(crate::error::SynapseError::Config(
                    "Invalid port number".to_string()
                )));
            }
        }
        Ok(())
    }
}
*/

// QUIC Transport Factory (COMMENTED OUT - TO BE FIXED)
/*
pub struct QuicTransportFactory;

#[async_trait]
impl TransportFactory for QuicTransportFactory {
    async fn create_transport(&self, config: &HashMap<String, String>) -> Result<Box<dyn Transport>> {
        let transport = crate::transport::quic_unified::QuicTransportImpl::new(config).await?;
        Ok(Box::new(transport))
    }
    
    fn transport_type(&self) -> TransportType {
        TransportType::Quic
    }
    
    fn default_config(&self) -> HashMap<String, String> {
        let mut config = HashMap::new();
        config.insert("bind_address".to_string(), "0.0.0.0:0".to_string());
        config.insert("connection_timeout_ms".to_string(), "10000".to_string());
        config.insert("max_concurrent_streams".to_string(), "1000".to_string());
        config.insert("max_message_size".to_string(), "10485760".to_string()); // 10MB
        config
    }
    
    fn validate_config(&self, config: &HashMap<String, String>) -> Result<()> {
        if let Some(addr_str) = config.get("bind_address") {
            if addr_str.parse::<SocketAddr>().is_err() {
                return Err(crate::error::SynapseError::Config(
                    "Invalid socket address".to_string()
                )));
            }
        }
        Ok(())
    }
}
*/

/// HTTP Transport Factory
#[cfg(feature = "http")]
pub struct HttpTransportFactory;

#[cfg(feature = "http")]
#[async_trait]
impl TransportFactory for HttpTransportFactory {
    async fn create_transport(&self, config: &HashMap<String, String>) -> Result<Box<dyn Transport>> {
        let transport = crate::transport::http_unified::HttpTransportImpl::new(config).await?;
        Ok(Box::new(transport))
    }
    
    fn transport_type(&self) -> TransportType {
        TransportType::Http
    }
    
    fn default_config(&self) -> HashMap<String, String> {
        let mut config = HashMap::new();
        config.insert("use_https".to_string(), "true".to_string());
        config.insert("server_port".to_string(), "0".to_string()); // Disabled by default
        config.insert("server_address".to_string(), "127.0.0.1".to_string());
        config.insert("timeout_ms".to_string(), "30000".to_string());
        config.insert("max_message_size".to_string(), "10485760".to_string()); // 10MB
        config.insert("user_agent".to_string(), "Synapse-HTTP-Transport/1.0".to_string());
        config
    }
    
    fn validate_config(&self, config: &HashMap<String, String>) -> Result<()> {
        // Validate server port
        if let Some(port_str) = config.get("server_port") {
            if port_str.parse::<u16>().is_err() {
                return Err(crate::error::SynapseError::Config(
                    "Invalid server port number".to_string()
                ));
            }
        }
        
        // Validate timeout
        if let Some(timeout_str) = config.get("timeout_ms") {
            if timeout_str.parse::<u64>().is_err() {
                return Err(crate::error::SynapseError::Config(
                    "Invalid timeout value".to_string()
                ));
            }
        }
        
        // Validate max message size
        if let Some(size_str) = config.get("max_message_size") {
            if size_str.parse::<usize>().is_err() {
                return Err(crate::error::SynapseError::Config(
                    "Invalid max message size".to_string()
                ));
            }
        }
        
        Ok(())
    }
}

/// Unified Transport Manager
/// Manages multiple transport mechanisms and provides intelligent routing
pub struct UnifiedTransportManager {
    transports: HashMap<TransportType, Box<dyn Transport>>,
    target_preferences: HashMap<String, TransportType>, // Target ID -> preferred transport
    metrics_cache: DashMap<String, HashMap<TransportType, TransportEstimate>>, // Performance metrics cache
    failover_policies: HashMap<TransportType, Vec<TransportType>>, // Failover order
    config: UnifiedTransportConfig,
}

/// Configuration for the unified transport manager
#[derive(Debug, Clone)]
pub struct UnifiedTransportConfig {
    pub default_transport: TransportType,
    pub enable_automatic_failover: bool,
    pub prefer_real_time_for_sync: bool,
    pub metrics_cache_seconds: u64,
    pub optimize_for_bandwidth: bool,
}

impl Default for UnifiedTransportConfig {
    fn default() -> Self {
        Self {
            default_transport: TransportType::WebSocket,
            enable_automatic_failover: true,
            prefer_real_time_for_sync: true,
            metrics_cache_seconds: 60,
            optimize_for_bandwidth: false,
        }
    }
}

impl UnifiedTransportManager {
    /// Create new transport manager
    pub async fn new(config: UnifiedTransportConfig) -> Result<Self> {
        let mut manager = Self {
            transports: HashMap::new(),
            target_preferences: HashMap::new(),
            metrics_cache: DashMap::new(),
            failover_policies: HashMap::new(),
            config,
        };
        
        // Set up default failover policies
        manager.setup_default_failover_policies();
        
        Ok(manager)
    }
    
    /// Register a transport implementation
    pub fn register_transport(&mut self, transport: Box<dyn Transport>) -> Result<()> {
        let transport_type = transport.transport_type();
        
        if self.transports.contains_key(&transport_type) {
            return Err(crate::error::SynapseError::TransportError(format!("Transport already registered: {}", transport_type)));
        }
        
        self.transports.insert(transport_type, transport);
        
        Ok(())
    }
    
    /// Send a message using the best available transport
    pub async fn send_message(&self, target: &TransportTarget, message: &SecureMessage) -> Result<DeliveryReceipt> {
        // Check if we have a preferred transport for this target
        let transport_type = if let Some(preferred) = self.target_preferences.get(&target.identifier) {
            *preferred
        } else {
            // No preference, try to determine best transport
            self.determine_best_transport(target, message).await?
        };
        
        // Try to send with selected transport
        if let Some(transport) = self.transports.get(&transport_type) {
            match transport.send_message(target, message).await {
                Ok(receipt) => {
                    return Ok(receipt);
                }
                Err(err) => {
                    // Transport failed, try failover if enabled
                    if self.config.enable_automatic_failover {
                        return self.try_failover_transports(target, message, transport_type).await;
                    }
                    return Err(err);
                }
            }
        }
        
        // No suitable transport found
        Err(crate::error::SynapseError::TransportError(format!("No suitable transport found for target: {}", target.identifier)))
    }
    
    /// Try to send using failover transports
    async fn try_failover_transports(
        &self, 
        target: &TransportTarget, 
        message: &SecureMessage,
        failed_transport: TransportType,
    ) -> Result<DeliveryReceipt> {
        if let Some(failover_list) = self.failover_policies.get(&failed_transport) {
            for transport_type in failover_list {
                if let Some(transport) = self.transports.get(transport_type) {
                    if transport.can_reach(target).await {
                        match transport.send_message(target, message).await {
                            Ok(receipt) => return Ok(receipt),
                            Err(_) => continue, // Try next failover transport
                        }
                    }
                }
            }
        }
        
        Err(crate::error::SynapseError::TransportError(format!("All transports failed for target: {}", target.identifier)))
    }
    
    /// Determine best transport for a target and message
    async fn determine_best_transport(&self, target: &TransportTarget, _message: &SecureMessage) -> Result<TransportType> {
        // If target specifies preferred transports and we have one, use the first available
        if !target.preferred_transports.is_empty() {
            for preferred in &target.preferred_transports {
                if self.transports.contains_key(preferred) {
                    return Ok(*preferred);
                }
            }
        }
        
        // Collect metrics for all transports that can reach this target
        let mut available_transports = Vec::new();
        
        for (transport_type, transport) in &self.transports {
            if transport.can_reach(target).await {
                let metrics = transport.estimate_metrics(target).await?;
                available_transports.push((*transport_type, metrics));
            }
        }
        
        if available_transports.is_empty() {
            return Err(crate::error::SynapseError::TransportError(format!("No transports can reach target: {}", target.identifier)));
        }
        
        // Sort by best metrics based on configuration and message
        available_transports.sort_by(|(_, a), (_, b)| {
            // For now, we'll use latency as the primary sorting criterion
            // since we don't have synchronous response field on SecureMessage
            // and real_time_capability field on TransportEstimate
            
            // First compare by availability
            match (a.available, b.available) {
                (true, false) => return std::cmp::Ordering::Less,
                (false, true) => return std::cmp::Ordering::Greater,
                _ => {} // Both same availability, continue with other metrics
            }
            
            // Then by reliability
            let reliability_cmp = b.reliability.partial_cmp(&a.reliability).unwrap_or(std::cmp::Ordering::Equal);
            if reliability_cmp != std::cmp::Ordering::Equal {
                return reliability_cmp;
            }
            
            // Finally by latency (lower is better)
            a.latency.partial_cmp(&b.latency).unwrap_or(std::cmp::Ordering::Equal)
        });
        
        // Return the best transport
        Ok(available_transports[0].0)
    }
    
    /// Set up default failover policies
    fn setup_default_failover_policies(&mut self) {
        // WebSocket failover
        self.failover_policies.insert(
            TransportType::WebSocket,
            vec![TransportType::Http, TransportType::Email]
        );
        
        // HTTP failover
        self.failover_policies.insert(
            TransportType::Http,
            vec![TransportType::WebSocket, TransportType::Email]
        );
        
        // Email failover (last resort)
        self.failover_policies.insert(
            TransportType::Email,
            vec![TransportType::Http, TransportType::WebSocket]
        );
        
        // QUIC failover
        self.failover_policies.insert(
            TransportType::Quic,
            vec![TransportType::WebSocket, TransportType::Http, TransportType::Email]
        );
    }
    
    /// Receive messages from all transports
    pub async fn receive_messages(&self) -> Result<Vec<IncomingMessage>> {
        let mut all_messages = Vec::new();
        
        for transport in self.transports.values() {
            match transport.receive_messages().await {
                Ok(mut messages) => all_messages.append(&mut messages),
                Err(_) => continue, // Skip transports with errors
            }
        }
        
        Ok(all_messages)
    }
    
    /// Receive messages from a specific transport
    pub async fn receive_from_transport(&self, transport_type: TransportType) -> Result<Vec<IncomingMessage>> {
        if let Some(transport) = self.transports.get(&transport_type) {
            return transport.receive_messages().await;
        }
        
        Err(crate::error::SynapseError::TransportError(format!("Transport not found: {}", transport_type)))
    }
    
    /// Start all transports
    pub async fn start_all_transports(&self) -> Result<()> {
        for transport in self.transports.values() {
            if let Err(e) = transport.start().await {
                // Log the error but continue with other transports
                tracing::error!("Failed to start transport {}: {}", transport.transport_type(), e);
            }
        }
        
        Ok(())
    }
    
    /// Stop all transports
    pub async fn stop_all_transports(&self) -> Result<()> {
        for transport in self.transports.values() {
            if let Err(e) = transport.stop().await {
                // Log the error but continue stopping other transports
                tracing::error!("Failed to stop transport {}: {}", transport.transport_type(), e);
            }
        }
        
        Ok(())
    }
}

/// Convenience function to create all standard transport factories
pub fn create_standard_factories() -> Vec<Box<dyn TransportFactory>> {
    let factories: Vec<Box<dyn TransportFactory>> = vec![
        Box::new(TcpTransportFactory),
        Box::new(super::udp_unified::UdpTransportFactory),
        // Enhanced transports now available
        Box::new(EmailTransportFactory),
        Box::new(MdnsTransportFactory),
        // WebSocket and QUIC transports available for future enablement
        // Box::new(WebSocketTransportFactory),
        // Box::new(QuicTransportFactory),
    ];
    
    #[cfg(feature = "http")]
    {
        let mut factories = factories;
        factories.push(Box::new(HttpTransportFactory));
        factories
    }
    #[cfg(not(feature = "http"))]
    {
        factories
    }
}