ant-quic 0.27.4

QUIC transport protocol with advanced NAT traversal for P2P networks
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
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
// Copyright 2024 Saorsa Labs Ltd.
//
// This Saorsa Network Software is licensed under the General Public License (GPL), version 3.
// Please see the file LICENSE-GPL, or visit <http://www.gnu.org/licenses/> for the full text.
//
// Full details available at https://saorsalabs.com/licenses

//! # Link Transport Abstraction Layer
//!
//! This module provides the [`LinkTransport`] and [`LinkConn`] traits that abstract
//! the transport layer for overlay networks like saorsa-core. This enables:
//!
//! - **Version decoupling**: Overlays can compile against a stable trait interface
//!   while ant-quic evolves underneath
//! - **Testing**: Mock transports for unit testing overlay logic
//! - **Alternative transports**: Future support for WebRTC, TCP fallback, etc.
//!
//! ## Architecture
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────────┐
//! │                      saorsa-core (overlay)                       │
//! │  DHT routing │ Record storage │ Greedy routing │ Naming         │
//! └─────────────────────────────────────────────────────────────────┘
//!//!//! ┌─────────────────────────────────────────────────────────────────┐
//! │                    LinkTransport trait                          │
//! │  local_peer() │ peer_table() │ dial() │ accept() │ subscribe()  │
//! └─────────────────────────────────────────────────────────────────┘
//!//!//! ┌─────────────────────────────────────────────────────────────────┐
//! │                  ant-quic P2pEndpoint                            │
//! │  QUIC transport │ NAT traversal │ PQC │ Connection management   │
//! └─────────────────────────────────────────────────────────────────┘
//! ```
//!
//! ## Example: Implementing an Overlay
//!
//! ```rust,ignore
//! use ant_quic::link_transport::{LinkTransport, LinkConn, LinkEvent, ProtocolId, LinkError};
//! use std::sync::Arc;
//! use futures_util::StreamExt;
//!
//! // Define your overlay's protocol identifier
//! const DHT_PROTOCOL: ProtocolId = ProtocolId::from_static(b"saorsa-dht/1.0.0");
//!
//! async fn run_overlay<T: LinkTransport>(transport: Arc<T>) -> anyhow::Result<()> {
//!     // Register our protocol so peers know we support it
//!     transport.register_protocol(DHT_PROTOCOL);
//!
//!     // Subscribe to transport events for connection lifecycle
//!     let mut events = transport.subscribe();
//!     tokio::spawn(async move {
//!         while let Ok(event) = events.recv().await {
//!             match event {
//!                 LinkEvent::PeerConnected { peer, caps } => {
//!                     println!("New peer: {:?}, relay: {}", peer, caps.supports_relay);
//!                 }
//!                 LinkEvent::PeerDisconnected { peer, reason } => {
//!                     println!("Lost peer: {:?}, reason: {:?}", peer, reason);
//!                 }
//!                 _ => {}
//!             }
//!         }
//!     });
//!
//!     // Accept incoming connections in a background task
//!     let transport_clone = transport.clone();
//!     tokio::spawn(async move {
//!         let mut incoming = transport_clone.accept(DHT_PROTOCOL);
//!         while let Some(result) = incoming.next().await {
//!             match result {
//!                 Ok(conn) => {
//!                     println!("Accepted connection from {:?}", conn.peer());
//!                     // Handle connection...
//!                 }
//!                 Err(e) => eprintln!("Accept error: {}", e),
//!             }
//!         }
//!     });
//!
//!     // Dial a peer using their PeerId (NAT traversal handled automatically)
//!     let peers = transport.peer_table();
//!     if let Some((peer_id, caps)) = peers.first() {
//!         match transport.dial(*peer_id, DHT_PROTOCOL).await {
//!             Ok(conn) => {
//!                 // Open a bidirectional stream for request/response
//!                 let (mut send, mut recv) = conn.open_bi().await?;
//!                 send.write_all(b"PING").await?;
//!                 send.finish()?;
//!
//!                 let response = recv.read_to_end(1024).await?;
//!                 println!("Response: {:?}", response);
//!             }
//!             Err(LinkError::PeerNotFound(_)) => {
//!                 println!("Peer not in table - need to bootstrap");
//!             }
//!             Err(e) => eprintln!("Dial failed: {}", e),
//!         }
//!     }
//!
//!     Ok(())
//! }
//! ```
//!
//! ## Choosing Stream Types
//!
//! - **Bidirectional (`open_bi`)**: Use for request/response patterns where both
//!   sides send and receive. Example: RPC calls, file transfers with acknowledgment.
//!
//! - **Unidirectional (`open_uni`)**: Use for one-way messages where no response
//!   is needed. Example: event notifications, log streaming, pub/sub.
//!
//! - **Datagrams (`send_datagram`)**: Use for small, unreliable messages where
//!   latency matters more than reliability. Example: heartbeats, real-time metrics.
//!
//! ## Error Handling Patterns
//!
//! ```rust,ignore
//! use ant_quic::link_transport::{LinkError, LinkResult};
//!
//! async fn connect_with_retry<T: LinkTransport>(
//!     transport: &T,
//!     peer: PeerId,
//!     proto: ProtocolId,
//! ) -> LinkResult<T::Conn> {
//!     for attempt in 1..=3 {
//!         match transport.dial(peer, proto).await {
//!             Ok(conn) => return Ok(conn),
//!             Err(LinkError::PeerNotFound(_)) => {
//!                 // Peer not in table - can't retry, need bootstrap
//!                 return Err(LinkError::PeerNotFound(format!("{:?}", peer)));
//!             }
//!             Err(LinkError::ConnectionFailed(msg)) if attempt < 3 => {
//!                 // Transient failure - retry after delay
//!                 tokio::time::sleep(Duration::from_millis(100 * attempt as u64)).await;
//!                 continue;
//!             }
//!             Err(LinkError::Timeout) if attempt < 3 => {
//!                 // NAT traversal may need multiple attempts
//!                 continue;
//!             }
//!             Err(e) => return Err(e),
//!         }
//!     }
//!     Err(LinkError::ConnectionFailed("max retries exceeded".into()))
//! }
//! ```

use std::collections::HashSet;
use std::fmt;
use std::future::Future;
use std::net::SocketAddr;
use std::pin::Pin;
use std::time::{Duration, SystemTime};

use async_trait::async_trait;
use bytes::Bytes;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use tokio::sync::broadcast;

use crate::nat_traversal_api::PeerId;
use crate::reachability::ReachabilityScope;
use crate::transport::TransportAddr;

// ============================================================================
// Stream Type Registry (Protocol Multiplexing)
// ============================================================================

/// Stream type identifier - the first byte of each QUIC stream.
///
/// This enum provides a hardcoded registry of protocol types for multiplexing
/// multiple protocols over a single QUIC connection. Each stream's first byte
/// identifies its protocol type.
///
/// # Protocol Ranges
///
/// | Range | Protocol Family | Types |
/// |-------|-----------------|-------|
/// | 0x00-0x0F | Gossip | Membership, PubSub, Bulk |
/// | 0x10-0x1F | DHT | Query, Store, Witness, Replication |
/// | 0x20-0x2F | WebRTC | Signal, Media, Data |
/// | 0xF0-0xFF | Reserved | Future use |
///
/// # Example
///
/// ```rust
/// use ant_quic::link_transport::StreamType;
///
/// // Check if a byte is a valid stream type
/// let stream_type = StreamType::from_byte(0x10);
/// assert_eq!(stream_type, Some(StreamType::DhtQuery));
///
/// // Get all gossip types
/// for st in StreamType::gossip_types() {
///     println!("Gossip type: {}", st);
/// }
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[repr(u8)]
pub enum StreamType {
    // =========================================================================
    // Gossip Protocols (0x00-0x0F)
    // =========================================================================
    /// Membership protocol messages (HyParView, SWIM).
    Membership = 0x00,

    /// PubSub protocol messages (Plumtree).
    PubSub = 0x01,

    /// Bulk gossip data transfer (CRDT deltas, large payloads).
    GossipBulk = 0x02,

    // =========================================================================
    // DHT Protocols (0x10-0x1F)
    // =========================================================================
    /// DHT query operations (GET, FIND_NODE, FIND_VALUE).
    DhtQuery = 0x10,

    /// DHT store operations (PUT, STORE).
    DhtStore = 0x11,

    /// DHT witness operations (Byzantine fault tolerance).
    DhtWitness = 0x12,

    /// DHT replication operations (background repair).
    DhtReplication = 0x13,

    // =========================================================================
    // WebRTC Protocols (0x20-0x2F)
    // =========================================================================
    /// WebRTC signaling (SDP, ICE candidates via QUIC).
    WebRtcSignal = 0x20,

    /// WebRTC media streams (audio/video RTP).
    WebRtcMedia = 0x21,

    /// WebRTC data channels.
    WebRtcData = 0x22,

    // =========================================================================
    // Reserved (0xF0-0xFF)
    // =========================================================================
    /// Reserved for future protocols.
    Reserved = 0xF0,
}

impl StreamType {
    /// Parse a stream type from its byte value.
    ///
    /// Returns `None` for unknown/unassigned values.
    #[inline]
    pub fn from_byte(byte: u8) -> Option<Self> {
        match byte {
            0x00 => Some(Self::Membership),
            0x01 => Some(Self::PubSub),
            0x02 => Some(Self::GossipBulk),
            0x10 => Some(Self::DhtQuery),
            0x11 => Some(Self::DhtStore),
            0x12 => Some(Self::DhtWitness),
            0x13 => Some(Self::DhtReplication),
            0x20 => Some(Self::WebRtcSignal),
            0x21 => Some(Self::WebRtcMedia),
            0x22 => Some(Self::WebRtcData),
            0xF0 => Some(Self::Reserved),
            _ => None,
        }
    }

    /// Get the byte value for this stream type.
    #[inline]
    pub const fn as_byte(self) -> u8 {
        self as u8
    }

    /// Get the protocol family for this stream type.
    #[inline]
    pub const fn family(self) -> StreamTypeFamily {
        match self as u8 {
            0x00..=0x0F => StreamTypeFamily::Gossip,
            0x10..=0x1F => StreamTypeFamily::Dht,
            0x20..=0x2F => StreamTypeFamily::WebRtc,
            _ => StreamTypeFamily::Reserved,
        }
    }

    /// Check if this is a gossip protocol type.
    #[inline]
    pub const fn is_gossip(self) -> bool {
        matches!(self.family(), StreamTypeFamily::Gossip)
    }

    /// Check if this is a DHT protocol type.
    #[inline]
    pub const fn is_dht(self) -> bool {
        matches!(self.family(), StreamTypeFamily::Dht)
    }

    /// Check if this is a WebRTC protocol type.
    #[inline]
    pub const fn is_webrtc(self) -> bool {
        matches!(self.family(), StreamTypeFamily::WebRtc)
    }

    /// Get all gossip stream types.
    pub const fn gossip_types() -> &'static [StreamType] {
        &[Self::Membership, Self::PubSub, Self::GossipBulk]
    }

    /// Get all DHT stream types.
    pub const fn dht_types() -> &'static [StreamType] {
        &[
            Self::DhtQuery,
            Self::DhtStore,
            Self::DhtWitness,
            Self::DhtReplication,
        ]
    }

    /// Get all WebRTC stream types.
    pub const fn webrtc_types() -> &'static [StreamType] {
        &[Self::WebRtcSignal, Self::WebRtcMedia, Self::WebRtcData]
    }

    /// Get all defined stream types.
    pub const fn all_types() -> &'static [StreamType] {
        &[
            Self::Membership,
            Self::PubSub,
            Self::GossipBulk,
            Self::DhtQuery,
            Self::DhtStore,
            Self::DhtWitness,
            Self::DhtReplication,
            Self::WebRtcSignal,
            Self::WebRtcMedia,
            Self::WebRtcData,
            Self::Reserved,
        ]
    }
}

impl fmt::Display for StreamType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Membership => write!(f, "Membership"),
            Self::PubSub => write!(f, "PubSub"),
            Self::GossipBulk => write!(f, "GossipBulk"),
            Self::DhtQuery => write!(f, "DhtQuery"),
            Self::DhtStore => write!(f, "DhtStore"),
            Self::DhtWitness => write!(f, "DhtWitness"),
            Self::DhtReplication => write!(f, "DhtReplication"),
            Self::WebRtcSignal => write!(f, "WebRtcSignal"),
            Self::WebRtcMedia => write!(f, "WebRtcMedia"),
            Self::WebRtcData => write!(f, "WebRtcData"),
            Self::Reserved => write!(f, "Reserved"),
        }
    }
}

impl From<StreamType> for u8 {
    fn from(st: StreamType) -> Self {
        st as u8
    }
}

impl TryFrom<u8> for StreamType {
    type Error = LinkError;

    fn try_from(byte: u8) -> Result<Self, Self::Error> {
        Self::from_byte(byte).ok_or(LinkError::InvalidStreamType(byte))
    }
}

/// Protocol family for stream types.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum StreamTypeFamily {
    /// Gossip protocols (0x00-0x0F).
    Gossip,
    /// DHT protocols (0x10-0x1F).
    Dht,
    /// WebRTC protocols (0x20-0x2F).
    WebRtc,
    /// Reserved (0xF0-0xFF).
    Reserved,
}

impl StreamTypeFamily {
    /// Get the byte range for this protocol family.
    pub const fn byte_range(self) -> (u8, u8) {
        match self {
            Self::Gossip => (0x00, 0x0F),
            Self::Dht => (0x10, 0x1F),
            Self::WebRtc => (0x20, 0x2F),
            Self::Reserved => (0xF0, 0xFF),
        }
    }

    /// Check if a byte is in this family's range.
    pub const fn contains(self, byte: u8) -> bool {
        let (start, end) = self.byte_range();
        byte >= start && byte <= end
    }
}

/// A filter for accepting specific stream types.
///
/// Use this with `accept_bi_typed` and `accept_uni_typed` to filter
/// incoming streams by protocol type.
///
/// # Example
///
/// ```rust
/// use ant_quic::link_transport::{StreamFilter, StreamType};
///
/// // Accept only DHT streams
/// let filter = StreamFilter::new()
///     .with_types(StreamType::dht_types());
///
/// // Accept gossip and DHT
/// let filter = StreamFilter::new()
///     .with_type(StreamType::Membership)
///     .with_type(StreamType::DhtQuery);
/// ```
#[derive(Debug, Clone, Default)]
pub struct StreamFilter {
    /// Allowed stream types. Empty means accept all.
    allowed: HashSet<StreamType>,
}

impl StreamFilter {
    /// Create a new empty filter (accepts all types).
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a filter that accepts all stream types.
    pub fn accept_all() -> Self {
        let mut filter = Self::new();
        for st in StreamType::all_types() {
            filter.allowed.insert(*st);
        }
        filter
    }

    /// Create a filter for gossip streams only.
    pub fn gossip_only() -> Self {
        Self::new().with_types(StreamType::gossip_types())
    }

    /// Create a filter for DHT streams only.
    pub fn dht_only() -> Self {
        Self::new().with_types(StreamType::dht_types())
    }

    /// Create a filter for WebRTC streams only.
    pub fn webrtc_only() -> Self {
        Self::new().with_types(StreamType::webrtc_types())
    }

    /// Add a single stream type to the filter.
    pub fn with_type(mut self, stream_type: StreamType) -> Self {
        self.allowed.insert(stream_type);
        self
    }

    /// Add multiple stream types to the filter.
    pub fn with_types(mut self, stream_types: &[StreamType]) -> Self {
        for st in stream_types {
            self.allowed.insert(*st);
        }
        self
    }

    /// Check if a stream type is accepted by this filter.
    pub fn accepts(&self, stream_type: StreamType) -> bool {
        self.allowed.is_empty() || self.allowed.contains(&stream_type)
    }

    /// Check if this filter accepts any type (is empty).
    pub fn accepts_all(&self) -> bool {
        self.allowed.is_empty()
    }

    /// Get the set of allowed types.
    pub fn allowed_types(&self) -> &HashSet<StreamType> {
        &self.allowed
    }
}

// ============================================================================
// Protocol Identifier
// ============================================================================

/// Protocol identifier for multiplexing multiple overlays on a single transport.
///
/// Protocols are identified by a 16-byte value, allowing efficient binary comparison
/// while supporting human-readable names during debugging.
///
/// # Examples
///
/// ```rust
/// use ant_quic::link_transport::ProtocolId;
///
/// // From a static string (padded/truncated to 16 bytes)
/// const DHT: ProtocolId = ProtocolId::from_static(b"saorsa-dht/1.0.0");
///
/// // From bytes
/// let proto = ProtocolId::new([0x73, 0x61, 0x6f, 0x72, 0x73, 0x61, 0x2d, 0x64,
///                              0x68, 0x74, 0x2f, 0x31, 0x2e, 0x30, 0x2e, 0x30]);
/// ```
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct ProtocolId(pub [u8; 16]);

impl ProtocolId {
    /// Create a new protocol ID from raw bytes.
    #[inline]
    pub const fn new(bytes: [u8; 16]) -> Self {
        Self(bytes)
    }

    /// Create a protocol ID from a static byte string.
    ///
    /// The string is padded with zeros if shorter than 16 bytes,
    /// or truncated if longer.
    #[inline]
    pub const fn from_static(s: &[u8]) -> Self {
        let mut bytes = [0u8; 16];
        let len = if s.len() < 16 { s.len() } else { 16 };
        let mut i = 0;
        while i < len {
            bytes[i] = s[i];
            i += 1;
        }
        Self(bytes)
    }

    /// Get the raw bytes of this protocol ID.
    #[inline]
    pub const fn as_bytes(&self) -> &[u8; 16] {
        &self.0
    }

    /// The default protocol for connections without explicit protocol negotiation.
    pub const DEFAULT: Self = Self::from_static(b"ant-quic/default");

    /// Protocol ID for NAT traversal coordination messages.
    pub const NAT_TRAVERSAL: Self = Self::from_static(b"ant-quic/nat");

    /// Protocol ID for relay traffic.
    pub const RELAY: Self = Self::from_static(b"ant-quic/relay");
}

impl Default for ProtocolId {
    fn default() -> Self {
        Self::DEFAULT
    }
}

impl fmt::Debug for ProtocolId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // Try to display as UTF-8 string, trimming null bytes
        let end = self.0.iter().position(|&b| b == 0).unwrap_or(16);
        if let Ok(s) = std::str::from_utf8(&self.0[..end]) {
            write!(f, "ProtocolId({:?})", s)
        } else {
            write!(f, "ProtocolId({:?})", hex::encode(self.0))
        }
    }
}

impl fmt::Display for ProtocolId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let end = self.0.iter().position(|&b| b == 0).unwrap_or(16);
        if let Ok(s) = std::str::from_utf8(&self.0[..end]) {
            write!(f, "{}", s)
        } else {
            write!(f, "{}", hex::encode(self.0))
        }
    }
}

impl From<&str> for ProtocolId {
    fn from(s: &str) -> Self {
        Self::from_static(s.as_bytes())
    }
}

impl From<[u8; 16]> for ProtocolId {
    fn from(bytes: [u8; 16]) -> Self {
        Self(bytes)
    }
}

// ============================================================================
// Peer Capabilities
// ============================================================================

/// NAT type classification hint for connection strategy selection.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum NatHint {
    /// No NAT detected (public IP, direct connectivity)
    None,
    /// Full cone NAT (easiest to traverse)
    FullCone,
    /// Address-restricted cone NAT
    AddressRestrictedCone,
    /// Port-restricted cone NAT
    PortRestrictedCone,
    /// Symmetric NAT (hardest to traverse, may require relay)
    Symmetric,
    /// Unknown NAT type
    #[default]
    Unknown,
}

/// Capabilities and quality metrics for a connected peer.
///
/// This struct captures both static capabilities (what the peer can do)
/// and dynamic metrics (how well the peer is performing).
#[derive(Debug, Clone)]
pub struct Capabilities {
    /// Whether this peer is a generally reusable relay helper.
    ///
    /// This is conservative and only flips true for fresh global-scope direct
    /// evidence. Scoped local/loopback evidence is preserved separately in
    /// `direct_reachability_scope`.
    pub supports_relay: bool,

    /// Whether this peer is a generally reusable NAT hole-punch coordinator.
    ///
    /// Like `supports_relay`, this is intentionally conservative and global-only.
    pub supports_coordination: bool,

    /// Observed external addresses for this peer.
    pub observed_addrs: Vec<SocketAddr>,

    /// Broadest direct reachability scope verified for this connected peer.
    pub direct_reachability_scope: Option<ReachabilityScope>,

    /// Protocols this peer advertises support for.
    pub protocols: Vec<ProtocolId>,

    /// Last time we successfully communicated with this peer.
    pub last_seen: SystemTime,

    /// Median round-trip time in milliseconds (p50).
    pub rtt_ms_p50: u32,

    /// Estimated RTT jitter in milliseconds.
    pub rtt_jitter_ms: u32,

    /// Packet loss rate (0.0 to 1.0).
    pub packet_loss: f32,

    /// Inferred NAT type for connection strategy hints.
    pub nat_type_hint: Option<NatHint>,

    /// Peer's advertised bandwidth limit (bytes/sec), if any.
    pub bandwidth_limit: Option<u64>,

    /// Number of successful connections to this peer.
    pub successful_connections: u32,

    /// Number of failed connection attempts to this peer.
    pub failed_connections: u32,

    /// Whether this peer is currently connected.
    pub is_connected: bool,
}

impl Default for Capabilities {
    fn default() -> Self {
        Self {
            supports_relay: false,
            supports_coordination: false,
            observed_addrs: Vec::new(),
            direct_reachability_scope: None,
            protocols: Vec::new(),
            last_seen: SystemTime::UNIX_EPOCH,
            rtt_ms_p50: 0,
            rtt_jitter_ms: 0,
            packet_loss: 0.0,
            nat_type_hint: None,
            bandwidth_limit: None,
            successful_connections: 0,
            failed_connections: 0,
            is_connected: false,
        }
    }
}

impl Capabilities {
    /// Create capabilities for a newly connected peer.
    pub fn new_connected(addr: SocketAddr) -> Self {
        Self {
            observed_addrs: vec![addr],
            direct_reachability_scope: None,
            last_seen: SystemTime::now(),
            is_connected: true,
            ..Default::default()
        }
    }

    /// Calculate a quality score for peer selection (0.0 to 1.0).
    ///
    /// Higher scores indicate better peers for connection.
    pub fn quality_score(&self) -> f32 {
        let mut score = 0.5; // Base score

        // RTT component (lower is better, max 300ms considered)
        let rtt_score = 1.0 - (self.rtt_ms_p50 as f32 / 300.0).min(1.0);
        score += rtt_score * 0.3;

        // Packet loss component
        let loss_score = 1.0 - self.packet_loss;
        score += loss_score * 0.2;

        // Connection success rate
        let total = self.successful_connections + self.failed_connections;
        if total > 0 {
            let success_rate = self.successful_connections as f32 / total as f32;
            score += success_rate * 0.2;
        }

        // Capability bonus
        if self.supports_relay {
            score += 0.05;
        }
        if self.supports_coordination {
            score += 0.05;
        }

        // NAT type penalty
        if let Some(nat) = self.nat_type_hint {
            match nat {
                NatHint::None | NatHint::FullCone => {}
                NatHint::AddressRestrictedCone | NatHint::PortRestrictedCone => {
                    score -= 0.05;
                }
                NatHint::Symmetric => {
                    score -= 0.15;
                }
                NatHint::Unknown => {
                    score -= 0.02;
                }
            }
        }

        score.clamp(0.0, 1.0)
    }

    /// Check if this peer supports a specific protocol.
    pub fn supports_protocol(&self, proto: &ProtocolId) -> bool {
        self.protocols.contains(proto)
    }
}

// ============================================================================
// Link Events
// ============================================================================

/// Reason for peer disconnection.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DisconnectReason {
    /// Clean shutdown initiated by local side.
    LocalClose,
    /// Clean shutdown initiated by remote side.
    RemoteClose,
    /// Connection timed out.
    Timeout,
    /// Transport error occurred.
    TransportError(String),
    /// Application-level error code.
    ApplicationError(u64),
    /// Connection was reset.
    Reset,
}

/// Events emitted by the link transport layer.
///
/// These events notify the overlay about significant transport-level changes.
#[derive(Debug, Clone)]
pub enum LinkEvent {
    /// A new peer has connected.
    PeerConnected {
        /// The connected peer's ID.
        peer: PeerId,
        /// Initial capabilities (may be updated later).
        caps: Capabilities,
    },

    /// A peer has disconnected.
    PeerDisconnected {
        /// The disconnected peer's ID.
        peer: PeerId,
        /// Reason for disconnection.
        reason: DisconnectReason,
    },

    /// Our observed external address has been updated.
    ExternalAddressUpdated {
        /// The new external address (supports all transport types).
        addr: TransportAddr,
    },

    /// A peer's capabilities have been updated.
    CapabilityUpdated {
        /// The peer whose capabilities changed.
        peer: PeerId,
        /// Updated capabilities.
        caps: Capabilities,
    },

    /// A relay request has been received.
    RelayRequest {
        /// Peer requesting the relay.
        from: PeerId,
        /// Target peer for the relay.
        to: PeerId,
        /// Bytes remaining in relay budget.
        budget_bytes: u64,
    },

    /// A NAT traversal coordination request has been received.
    CoordinationRequest {
        /// First peer in the coordination.
        peer_a: PeerId,
        /// Second peer in the coordination.
        peer_b: PeerId,
        /// Coordination round number.
        round: u64,
    },

    /// The bootstrap cache has been updated.
    BootstrapCacheUpdated {
        /// Number of peers in the cache.
        peer_count: usize,
    },
}

// ============================================================================
// Protocol Handler Abstraction
// ============================================================================

/// Handler for specific protocol stream types.
///
/// Implement this trait to handle incoming streams by protocol type.
/// Each handler declares which [`StreamType`]s it processes and receives
/// matching streams via [`Self::handle_stream`].
///
/// # Example
///
/// ```rust,ignore
/// use ant_quic::link_transport::{ProtocolHandler, StreamType, LinkResult, PeerId};
/// use async_trait::async_trait;
/// use bytes::Bytes;
///
/// struct GossipHandler;
///
/// #[async_trait]
/// impl ProtocolHandler for GossipHandler {
///     fn stream_types(&self) -> &[StreamType] {
///         StreamType::gossip_types()
///     }
///
///     async fn handle_stream(
///         &self,
///         peer: PeerId,
///         stream_type: StreamType,
///         data: Bytes,
///     ) -> LinkResult<Option<Bytes>> {
///         // Process incoming gossip message, optionally return response
///         Ok(None)
///     }
/// }
/// ```
#[async_trait]
pub trait ProtocolHandler: Send + Sync {
    /// Get the stream types this handler processes.
    fn stream_types(&self) -> &[StreamType];

    /// Handle an incoming stream.
    ///
    /// # Arguments
    ///
    /// * `peer` - The peer that sent the stream
    /// * `stream_type` - The type of stream received
    /// * `data` - The stream payload data
    ///
    /// # Returns
    ///
    /// * `Ok(Some(response))` - Send response back to the peer
    /// * `Ok(None)` - No response (close stream gracefully)
    /// * `Err(e)` - Handler error (stream closed with error)
    async fn handle_stream(
        &self,
        peer: PeerId,
        stream_type: StreamType,
        data: Bytes,
    ) -> LinkResult<Option<Bytes>>;

    /// Handle an incoming datagram.
    ///
    /// Default implementation does nothing. Override for unreliable messaging.
    async fn handle_datagram(
        &self,
        _peer: PeerId,
        _stream_type: StreamType,
        _data: Bytes,
    ) -> LinkResult<()> {
        Ok(())
    }

    /// Called when the handler is being shut down.
    ///
    /// Default implementation does nothing. Override for cleanup.
    async fn shutdown(&self) -> LinkResult<()> {
        Ok(())
    }

    /// Get a human-readable name for this handler.
    ///
    /// Used in logging and debugging.
    fn name(&self) -> &str {
        "ProtocolHandler"
    }
}

/// A boxed protocol handler for dynamic dispatch.
pub type BoxedHandler = Box<dyn ProtocolHandler>;

/// Extension trait for creating boxed handlers.
pub trait ProtocolHandlerExt: ProtocolHandler + Sized + 'static {
    /// Box this handler for use with [`crate::SharedTransport`].
    fn boxed(self) -> BoxedHandler {
        Box::new(self)
    }
}

impl<T: ProtocolHandler + 'static> ProtocolHandlerExt for T {}

// ============================================================================
// Link Transport Errors
// ============================================================================

/// Errors that can occur during link transport operations.
#[derive(Debug, Error, Clone)]
pub enum LinkError {
    /// The connection was closed.
    #[error("connection closed")]
    ConnectionClosed,

    /// Failed to establish connection.
    #[error("connection failed: {0}")]
    ConnectionFailed(String),

    /// The peer is not known/reachable.
    #[error("peer not found: {0}")]
    PeerNotFound(String),

    /// Protocol negotiation failed.
    #[error("protocol not supported: {0}")]
    ProtocolNotSupported(ProtocolId),

    /// A timeout occurred.
    #[error("operation timed out")]
    Timeout,

    /// The stream was reset by the peer.
    #[error("stream reset: error code {0}")]
    StreamReset(u64),

    /// An I/O error occurred.
    #[error("I/O error: {0}")]
    Io(String),

    /// The transport is shutting down.
    #[error("transport shutdown")]
    Shutdown,

    /// Rate limit exceeded.
    #[error("rate limit exceeded")]
    RateLimited,

    /// Internal error.
    #[error("internal error: {0}")]
    Internal(String),

    /// Invalid stream type byte.
    #[error("invalid stream type byte: 0x{0:02x}")]
    InvalidStreamType(u8),

    /// Stream type not accepted by filter.
    #[error("stream type {0} not accepted")]
    StreamTypeFiltered(StreamType),

    /// Handler already registered for stream type.
    #[error("handler already exists for stream type: {0}")]
    HandlerExists(StreamType),

    /// No handler registered for stream type.
    #[error("no handler for stream type: {0}")]
    NoHandler(StreamType),

    /// Transport not running.
    #[error("transport not running")]
    NotRunning,

    /// Transport already running.
    #[error("transport already running")]
    AlreadyRunning,
}

impl From<std::io::Error> for LinkError {
    fn from(e: std::io::Error) -> Self {
        Self::Io(e.to_string())
    }
}

/// Result type for link transport operations.
pub type LinkResult<T> = Result<T, LinkError>;

// ============================================================================
// Link Connection Trait
// ============================================================================

/// A boxed future for async operations.
pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;

/// A boxed stream for async iteration.
pub type BoxStream<'a, T> = Pin<Box<dyn futures_util::Stream<Item = T> + Send + 'a>>;

/// A connection to a remote peer.
///
/// This trait abstracts a single QUIC connection, providing methods to
/// open streams and send/receive datagrams. Connections are obtained via
/// [`LinkTransport::dial`] or [`LinkTransport::accept`].
///
/// # Stream Types
///
/// - **Bidirectional streams** (`open_bi`): Both endpoints can send and receive.
///   Use for request/response patterns.
/// - **Unidirectional streams** (`open_uni`): Only the opener can send.
///   Use for notifications or one-way data transfer.
/// - **Datagrams** (`send_datagram`): Unreliable, unordered messages.
///   Use for real-time data where latency > reliability.
///
/// # Connection Lifecycle
///
/// 1. Connection established (via dial or accept)
/// 2. Open streams as needed
/// 3. Close gracefully with `close()` or let it drop
pub trait LinkConn: Send + Sync {
    /// Get the remote peer's cryptographic identity.
    ///
    /// This is stable across reconnections and network changes.
    fn peer(&self) -> PeerId;

    /// Get the remote peer's current network address.
    ///
    /// Note: This may change during the connection lifetime due to
    /// NAT rebinding or connection migration.
    fn remote_addr(&self) -> SocketAddr;

    /// Open a unidirectional stream (send only).
    ///
    /// The remote peer will receive this stream via their `accept_uni()`.
    /// Use for one-way messages like notifications or log streams.
    ///
    /// # Example
    /// ```rust,ignore
    /// let mut stream = conn.open_uni().await?;
    /// stream.write_all(b"notification").await?;
    /// stream.finish()?; // Signal end of stream
    /// ```
    fn open_uni(&self) -> BoxFuture<'_, LinkResult<Box<dyn LinkSendStream>>>;

    /// Open a bidirectional stream for request/response communication.
    ///
    /// Returns a (send, recv) pair. Both sides can write and read.
    /// Use for RPC, file transfers, or any interactive protocol.
    ///
    /// # Example
    /// ```rust,ignore
    /// let (mut send, mut recv) = conn.open_bi().await?;
    /// send.write_all(b"request").await?;
    /// send.finish()?;
    /// let response = recv.read_to_end(4096).await?;
    /// ```
    fn open_bi(
        &self,
    ) -> BoxFuture<'_, LinkResult<(Box<dyn LinkSendStream>, Box<dyn LinkRecvStream>)>>;

    /// Open a typed unidirectional stream.
    ///
    /// The stream type byte is automatically prepended to the stream.
    /// The remote peer should use `accept_uni_typed` to receive.
    ///
    /// # Example
    /// ```rust,ignore
    /// let mut stream = conn.open_uni_typed(StreamType::Membership).await?;
    /// stream.write_all(b"membership update").await?;
    /// stream.finish()?;
    /// ```
    fn open_uni_typed(
        &self,
        stream_type: StreamType,
    ) -> BoxFuture<'_, LinkResult<Box<dyn LinkSendStream>>>;

    /// Open a typed bidirectional stream.
    ///
    /// The stream type byte is automatically prepended to the stream.
    /// The remote peer should use `accept_bi_typed` to receive.
    ///
    /// # Example
    /// ```rust,ignore
    /// let (mut send, mut recv) = conn.open_bi_typed(StreamType::DhtQuery).await?;
    /// send.write_all(b"query request").await?;
    /// send.finish()?;
    /// let response = recv.read_to_end(4096).await?;
    /// ```
    fn open_bi_typed(
        &self,
        stream_type: StreamType,
    ) -> BoxFuture<'_, LinkResult<(Box<dyn LinkSendStream>, Box<dyn LinkRecvStream>)>>;

    /// Accept incoming unidirectional streams with type filtering.
    ///
    /// Returns a stream of (type, recv_stream) pairs for streams
    /// matching the filter. Use `StreamFilter::new()` to accept all types.
    ///
    /// # Example
    /// ```rust,ignore
    /// let filter = StreamFilter::gossip_only();
    /// let mut incoming = conn.accept_uni_typed(filter);
    /// while let Some(result) = incoming.next().await {
    ///     let (stream_type, recv) = result?;
    ///     println!("Got {} stream", stream_type);
    /// }
    /// ```
    fn accept_uni_typed(
        &self,
        filter: StreamFilter,
    ) -> BoxStream<'_, LinkResult<(StreamType, Box<dyn LinkRecvStream>)>>;

    /// Accept incoming bidirectional streams with type filtering.
    ///
    /// Returns a stream of (type, send_stream, recv_stream) tuples for
    /// streams matching the filter. Use `StreamFilter::new()` to accept all types.
    ///
    /// # Example
    /// ```rust,ignore
    /// let filter = StreamFilter::dht_only();
    /// let mut incoming = conn.accept_bi_typed(filter);
    /// while let Some(result) = incoming.next().await {
    ///     let (stream_type, send, recv) = result?;
    ///     // Handle DHT request/response
    /// }
    /// ```
    fn accept_bi_typed(
        &self,
        filter: StreamFilter,
    ) -> BoxStream<'_, LinkResult<(StreamType, Box<dyn LinkSendStream>, Box<dyn LinkRecvStream>)>>;

    /// Send an unreliable datagram to the peer.
    ///
    /// Datagrams are:
    /// - **Unreliable**: May be dropped without notification
    /// - **Unordered**: May arrive out of order
    /// - **Size-limited**: Must fit in a single QUIC packet (~1200 bytes)
    ///
    /// Use for heartbeats, metrics, or real-time data where occasional
    /// loss is acceptable.
    fn send_datagram(&self, data: Bytes) -> LinkResult<()>;

    /// Receive datagrams from the peer.
    ///
    /// Returns a stream of datagrams. Each datagram is delivered as-is
    /// (no framing). The stream ends when the connection closes.
    fn recv_datagrams(&self) -> BoxStream<'_, Bytes>;

    /// Close the connection gracefully.
    ///
    /// # Parameters
    /// - `error_code`: Application-defined error code (0 = normal close)
    /// - `reason`: Human-readable reason for debugging
    fn close(&self, error_code: u64, reason: &str);

    /// Check if the connection is still open.
    ///
    /// Returns false after the connection has been closed (locally or remotely)
    /// or if a fatal error occurred.
    fn is_open(&self) -> bool;

    /// Get current connection statistics.
    ///
    /// Useful for monitoring connection health and debugging performance.
    fn stats(&self) -> ConnectionStats;
}

/// Statistics for a connection.
///
/// Updated in real-time as the connection handles data. Use for:
/// - Monitoring connection health
/// - Detecting congestion (high RTT, packet loss)
/// - Debugging performance issues
///
/// # Typical Values
///
/// | Metric | Good | Concerning | Critical |
/// |--------|------|------------|----------|
/// | RTT | <50ms | 50-200ms | >500ms |
/// | Packet loss | <0.1% | 0.1-1% | >5% |
///
/// # Example
/// ```rust,ignore
/// let stats = conn.stats();
/// if stats.rtt > Duration::from_millis(200) {
///     log::warn!("High latency: {:?}", stats.rtt);
/// }
/// if stats.packets_lost > stats.bytes_sent / 100 {
///     log::warn!("Significant packet loss detected");
/// }
/// ```
#[derive(Debug, Clone, Default)]
pub struct ConnectionStats {
    /// Total bytes sent on this connection (including retransmits).
    pub bytes_sent: u64,
    /// Total bytes received on this connection.
    pub bytes_received: u64,
    /// Current smoothed round-trip time estimate.
    /// Calculated using QUIC's RTT estimation algorithm.
    pub rtt: Duration,
    /// How long this connection has been established.
    pub connected_duration: Duration,
    /// Total number of streams opened (bidirectional + unidirectional).
    pub streams_opened: u64,
    /// Estimated packets lost during transmission.
    /// High values indicate congestion or poor network conditions.
    pub packets_lost: u64,
}

/// A send stream for writing data to a peer.
pub trait LinkSendStream: Send + Sync {
    /// Write data to the stream.
    fn write<'a>(&'a mut self, data: &'a [u8]) -> BoxFuture<'a, LinkResult<usize>>;

    /// Write all data to the stream.
    fn write_all<'a>(&'a mut self, data: &'a [u8]) -> BoxFuture<'a, LinkResult<()>>;

    /// Finish the stream (signal end of data).
    fn finish(&mut self) -> LinkResult<()>;

    /// Reset the stream with an error code.
    fn reset(&mut self, error_code: u64) -> LinkResult<()>;

    /// Get the stream ID.
    fn id(&self) -> u64;
}

/// A receive stream for reading data from a peer.
pub trait LinkRecvStream: Send + Sync {
    /// Read data from the stream.
    fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> BoxFuture<'a, LinkResult<Option<usize>>>;

    /// Read all data until the stream ends.
    fn read_to_end(&mut self, size_limit: usize) -> BoxFuture<'_, LinkResult<Vec<u8>>>;

    /// Stop receiving data (signal we don't want more).
    fn stop(&mut self, error_code: u64) -> LinkResult<()>;

    /// Get the stream ID.
    fn id(&self) -> u64;
}

// ============================================================================
// Link Transport Trait
// ============================================================================

/// Incoming connection stream.
pub type Incoming<C> = BoxStream<'static, LinkResult<C>>;

/// The primary transport abstraction for overlay networks.
///
/// This trait provides everything an overlay needs to establish connections,
/// send/receive data, and monitor the transport layer.
///
/// # Implementation Notes
///
/// Implementors should:
/// - Handle NAT traversal transparently
/// - Maintain a peer table with capabilities
/// - Emit events for connection state changes
/// - Support protocol multiplexing
///
/// # Example Implementation
///
/// The default implementation wraps `P2pEndpoint`:
///
/// ```rust,ignore
/// let config = P2pConfig::builder()
///     .bind_addr("0.0.0.0:0".parse()?)
///     .build()?;
/// let endpoint = P2pEndpoint::new(config).await?;
/// let transport: Arc<dyn LinkTransport<Conn = P2pLinkConn>> = Arc::new(endpoint);
/// ```
pub trait LinkTransport: Send + Sync + 'static {
    /// The connection type returned by this transport.
    type Conn: LinkConn + 'static;

    /// Get our local peer identity.
    ///
    /// This is our stable cryptographic identity, derived from our key pair.
    /// It remains constant across restarts and network changes.
    fn local_peer(&self) -> PeerId;

    /// Get our externally observed address, if known.
    ///
    /// Returns the address other peers see when we connect to them.
    /// This is discovered via:
    /// - OBSERVED_ADDRESS frames from connected peers
    /// - NAT traversal address discovery
    ///
    /// Returns `None` if we haven't connected to any peers yet or
    /// if we're behind a symmetric NAT that changes our external port.
    fn external_address(&self) -> Option<SocketAddr>;

    /// Get all known peers with their capabilities.
    ///
    /// Includes:
    /// - Currently connected peers (`caps.is_connected = true`)
    /// - Previously connected peers still in bootstrap cache
    /// - Peers learned from relay/coordination traffic
    ///
    /// Use `Capabilities::quality_score()` to rank peers for selection.
    fn peer_table(&self) -> Vec<(PeerId, Capabilities)>;

    /// Get capabilities for a specific peer.
    ///
    /// Returns `None` if the peer is not known.
    fn peer_capabilities(&self, peer: &PeerId) -> Option<Capabilities>;

    /// Subscribe to transport-level events.
    ///
    /// Events include peer connections/disconnections, address changes,
    /// and capability updates. Use for maintaining overlay state.
    ///
    /// Multiple subscribers are supported via broadcast channel.
    fn subscribe(&self) -> broadcast::Receiver<LinkEvent>;

    /// Accept incoming connections for a specific protocol.
    ///
    /// Returns a stream of connections from peers that want to speak
    /// the specified protocol. Register your protocol first with
    /// `register_protocol()`.
    ///
    /// # Example
    /// ```rust,ignore
    /// let mut incoming = transport.accept(MY_PROTOCOL);
    /// while let Some(result) = incoming.next().await {
    ///     if let Ok(conn) = result {
    ///         tokio::spawn(handle_connection(conn));
    ///     }
    /// }
    /// ```
    fn accept(&self, proto: ProtocolId) -> Incoming<Self::Conn>;

    /// Dial a peer by their PeerId (preferred method).
    ///
    /// Uses the peer table to find known addresses for this peer.
    /// NAT traversal is handled automatically - if direct connection
    /// fails, coordination and hole-punching are attempted.
    ///
    /// # Errors
    /// - `PeerNotFound`: Peer not in table (need to bootstrap)
    /// - `ConnectionFailed`: Network error (may be transient)
    /// - `Timeout`: NAT traversal timed out (retry may succeed)
    fn dial(&self, peer: PeerId, proto: ProtocolId) -> BoxFuture<'_, LinkResult<Self::Conn>>;

    /// Dial a peer by direct address (for bootstrapping).
    ///
    /// Use when you don't know the peer's ID yet, such as when
    /// connecting to a known seed address to join the network.
    ///
    /// After connection, the peer's ID will be available via
    /// `conn.peer()`.
    fn dial_addr(
        &self,
        addr: SocketAddr,
        proto: ProtocolId,
    ) -> BoxFuture<'_, LinkResult<Self::Conn>>;

    /// Get protocols we advertise as supported.
    fn supported_protocols(&self) -> Vec<ProtocolId>;

    /// Register a protocol as supported.
    ///
    /// Call this before `accept()` to receive connections for the protocol.
    /// Registered protocols are advertised to connected peers.
    fn register_protocol(&self, proto: ProtocolId);

    /// Unregister a protocol.
    ///
    /// Stops accepting new connections for this protocol. Existing
    /// connections are not affected.
    fn unregister_protocol(&self, proto: ProtocolId);

    /// Check if we have an active connection to a peer.
    fn is_connected(&self, peer: &PeerId) -> bool;

    /// Get the count of active connections.
    fn active_connections(&self) -> usize;

    /// Gracefully shutdown the transport.
    ///
    /// Closes all connections, stops accepting new ones, and flushes
    /// the bootstrap cache to disk. Pending operations will complete
    /// or error.
    ///
    /// Call this before exiting to ensure clean shutdown.
    fn shutdown(&self) -> BoxFuture<'_, ()>;
}

// ============================================================================
// P2pEndpoint Implementation
// ============================================================================

// The implementation of LinkTransport for P2pEndpoint is in a separate file
// to keep this module focused on the trait definitions.

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_protocol_id_from_string() {
        let proto = ProtocolId::from("saorsa-dht/1.0");
        assert_eq!(&proto.0[..14], b"saorsa-dht/1.0");
        assert_eq!(proto.0[14], 0);
        assert_eq!(proto.0[15], 0);
    }

    #[test]
    fn test_protocol_id_truncation() {
        let proto = ProtocolId::from("this-is-a-very-long-protocol-name");
        assert_eq!(&proto.0, b"this-is-a-very-l");
    }

    #[test]
    fn test_protocol_id_display() {
        let proto = ProtocolId::from("test/1.0");
        assert_eq!(format!("{}", proto), "test/1.0");
    }

    #[test]
    fn test_capabilities_quality_score() {
        let mut caps = Capabilities::default();

        // Default has perfect RTT (0ms) and no packet loss, so score should be high
        // Score = 0.5 (base) + 0.3 (RTT: 1.0*0.3) + 0.2 (loss: 1.0*0.2) = 1.0
        let base_score = caps.quality_score();
        assert!(
            (0.9..=1.0).contains(&base_score),
            "base_score = {}",
            base_score
        );

        // Worse RTT should reduce score
        caps.rtt_ms_p50 = 150; // 50% of max
        let worse_rtt_score = caps.quality_score();
        assert!(
            worse_rtt_score < base_score,
            "worse RTT should reduce score"
        );

        // Very bad RTT should reduce score more
        caps.rtt_ms_p50 = 500;
        let bad_rtt_score = caps.quality_score();
        assert!(
            bad_rtt_score < worse_rtt_score,
            "bad RTT should reduce score more"
        );

        // Symmetric NAT should reduce score
        caps.rtt_ms_p50 = 50;
        caps.nat_type_hint = Some(NatHint::Symmetric);
        let nat_score = caps.quality_score();
        // Reset RTT for fair comparison
        caps.nat_type_hint = None;
        caps.rtt_ms_p50 = 50;
        let no_nat_score = caps.quality_score();
        assert!(
            nat_score < no_nat_score,
            "symmetric NAT should reduce score"
        );
    }

    #[test]
    fn test_capabilities_supports_protocol() {
        let mut caps = Capabilities::default();
        let dht = ProtocolId::from("dht/1.0");
        let gossip = ProtocolId::from("gossip/1.0");

        caps.protocols.push(dht);

        assert!(caps.supports_protocol(&dht));
        assert!(!caps.supports_protocol(&gossip));
    }

    // =========================================================================
    // Stream Type Tests
    // =========================================================================

    #[test]
    fn test_stream_type_bytes() {
        assert_eq!(StreamType::Membership.as_byte(), 0x00);
        assert_eq!(StreamType::PubSub.as_byte(), 0x01);
        assert_eq!(StreamType::GossipBulk.as_byte(), 0x02);
        assert_eq!(StreamType::DhtQuery.as_byte(), 0x10);
        assert_eq!(StreamType::DhtStore.as_byte(), 0x11);
        assert_eq!(StreamType::DhtWitness.as_byte(), 0x12);
        assert_eq!(StreamType::DhtReplication.as_byte(), 0x13);
        assert_eq!(StreamType::WebRtcSignal.as_byte(), 0x20);
        assert_eq!(StreamType::WebRtcMedia.as_byte(), 0x21);
        assert_eq!(StreamType::WebRtcData.as_byte(), 0x22);
        assert_eq!(StreamType::Reserved.as_byte(), 0xF0);
    }

    #[test]
    fn test_stream_type_from_byte() {
        assert_eq!(StreamType::from_byte(0x00), Some(StreamType::Membership));
        assert_eq!(StreamType::from_byte(0x10), Some(StreamType::DhtQuery));
        assert_eq!(StreamType::from_byte(0x20), Some(StreamType::WebRtcSignal));
        assert_eq!(StreamType::from_byte(0xF0), Some(StreamType::Reserved));
        assert_eq!(StreamType::from_byte(0x99), None); // Unassigned
        assert_eq!(StreamType::from_byte(0xFF), None); // Unassigned
    }

    #[test]
    fn test_stream_type_families() {
        assert!(StreamType::Membership.is_gossip());
        assert!(StreamType::PubSub.is_gossip());
        assert!(StreamType::GossipBulk.is_gossip());

        assert!(StreamType::DhtQuery.is_dht());
        assert!(StreamType::DhtStore.is_dht());
        assert!(StreamType::DhtWitness.is_dht());
        assert!(StreamType::DhtReplication.is_dht());

        assert!(StreamType::WebRtcSignal.is_webrtc());
        assert!(StreamType::WebRtcMedia.is_webrtc());
        assert!(StreamType::WebRtcData.is_webrtc());
    }

    #[test]
    fn test_stream_type_family_ranges() {
        assert!(StreamTypeFamily::Gossip.contains(0x00));
        assert!(StreamTypeFamily::Gossip.contains(0x0F));
        assert!(!StreamTypeFamily::Gossip.contains(0x10));

        assert!(StreamTypeFamily::Dht.contains(0x10));
        assert!(StreamTypeFamily::Dht.contains(0x1F));
        assert!(!StreamTypeFamily::Dht.contains(0x20));

        assert!(StreamTypeFamily::WebRtc.contains(0x20));
        assert!(StreamTypeFamily::WebRtc.contains(0x2F));
        assert!(!StreamTypeFamily::WebRtc.contains(0x30));
    }

    #[test]
    fn test_stream_filter_accepts() {
        let filter = StreamFilter::new()
            .with_type(StreamType::Membership)
            .with_type(StreamType::DhtQuery);

        assert!(filter.accepts(StreamType::Membership));
        assert!(filter.accepts(StreamType::DhtQuery));
        assert!(!filter.accepts(StreamType::PubSub));
        assert!(!filter.accepts(StreamType::WebRtcMedia));
    }

    #[test]
    fn test_stream_filter_empty_accepts_all() {
        let filter = StreamFilter::new();
        assert!(filter.accepts_all());
        assert!(filter.accepts(StreamType::Membership));
        assert!(filter.accepts(StreamType::DhtQuery));
        assert!(filter.accepts(StreamType::WebRtcMedia));
    }

    #[test]
    fn test_stream_filter_presets() {
        let gossip = StreamFilter::gossip_only();
        assert!(gossip.accepts(StreamType::Membership));
        assert!(gossip.accepts(StreamType::PubSub));
        assert!(gossip.accepts(StreamType::GossipBulk));
        assert!(!gossip.accepts(StreamType::DhtQuery));

        let dht = StreamFilter::dht_only();
        assert!(dht.accepts(StreamType::DhtQuery));
        assert!(dht.accepts(StreamType::DhtStore));
        assert!(!dht.accepts(StreamType::Membership));

        let webrtc = StreamFilter::webrtc_only();
        assert!(webrtc.accepts(StreamType::WebRtcSignal));
        assert!(webrtc.accepts(StreamType::WebRtcMedia));
        assert!(!webrtc.accepts(StreamType::DhtQuery));
    }

    #[test]
    fn test_stream_type_display() {
        assert_eq!(format!("{}", StreamType::Membership), "Membership");
        assert_eq!(format!("{}", StreamType::DhtQuery), "DhtQuery");
        assert_eq!(format!("{}", StreamType::WebRtcMedia), "WebRtcMedia");
    }

    // =========================================================================
    // Phase 1: ProtocolHandler Tests (TDD RED)
    // =========================================================================

    mod protocol_handler_tests {
        use super::*;
        use std::sync::Arc;
        use std::sync::atomic::{AtomicUsize, Ordering};

        /// Test handler implementation for testing
        struct TestHandler {
            types: Vec<StreamType>,
            call_count: Arc<AtomicUsize>,
        }

        impl TestHandler {
            fn new(types: Vec<StreamType>) -> Self {
                Self {
                    types,
                    call_count: Arc::new(AtomicUsize::new(0)),
                }
            }

            fn with_counter(types: Vec<StreamType>, counter: Arc<AtomicUsize>) -> Self {
                Self {
                    types,
                    call_count: counter,
                }
            }
        }

        #[async_trait]
        impl ProtocolHandler for TestHandler {
            fn stream_types(&self) -> &[StreamType] {
                &self.types
            }

            async fn handle_stream(
                &self,
                _peer: PeerId,
                _stream_type: StreamType,
                data: Bytes,
            ) -> LinkResult<Option<Bytes>> {
                self.call_count.fetch_add(1, Ordering::SeqCst);
                Ok(Some(data)) // Echo back
            }

            fn name(&self) -> &str {
                "TestHandler"
            }
        }

        #[test]
        fn test_handler_stream_types() {
            let handler = TestHandler::new(vec![StreamType::Membership, StreamType::PubSub]);
            assert_eq!(handler.stream_types().len(), 2);
            assert!(handler.stream_types().contains(&StreamType::Membership));
            assert!(handler.stream_types().contains(&StreamType::PubSub));
        }

        #[tokio::test]
        async fn test_handler_returns_response() {
            let handler = TestHandler::new(vec![StreamType::DhtQuery]);
            let peer = PeerId::from([0u8; 32]);

            let result = handler
                .handle_stream(peer, StreamType::DhtQuery, Bytes::from_static(b"test"))
                .await;

            assert!(result.is_ok());
            assert_eq!(result.unwrap(), Some(Bytes::from_static(b"test")));
        }

        #[tokio::test]
        async fn test_handler_no_response() {
            struct SinkHandler;

            #[async_trait]
            impl ProtocolHandler for SinkHandler {
                fn stream_types(&self) -> &[StreamType] {
                    &[StreamType::GossipBulk]
                }

                async fn handle_stream(
                    &self,
                    _peer: PeerId,
                    _stream_type: StreamType,
                    _data: Bytes,
                ) -> LinkResult<Option<Bytes>> {
                    Ok(None)
                }
            }

            let handler = SinkHandler;
            let peer = PeerId::from([0u8; 32]);

            let result = handler
                .handle_stream(peer, StreamType::GossipBulk, Bytes::from_static(b"data"))
                .await;

            assert!(result.is_ok());
            assert!(result.unwrap().is_none());
        }

        #[tokio::test]
        async fn test_handler_tracks_calls() {
            let count = Arc::new(AtomicUsize::new(0));
            let handler = TestHandler::with_counter(vec![StreamType::Membership], count.clone());
            let peer = PeerId::from([0u8; 32]);

            assert_eq!(handler.name(), "TestHandler");
            assert_eq!(count.load(Ordering::SeqCst), 0);

            let _ = handler
                .handle_stream(peer, StreamType::Membership, Bytes::new())
                .await;
            assert_eq!(count.load(Ordering::SeqCst), 1);

            let _ = handler
                .handle_stream(peer, StreamType::Membership, Bytes::new())
                .await;
            assert_eq!(count.load(Ordering::SeqCst), 2);
        }

        #[test]
        fn test_boxed_handler() {
            let handler: BoxedHandler = TestHandler::new(vec![StreamType::DhtStore]).boxed();
            assert_eq!(handler.stream_types(), &[StreamType::DhtStore]);
            assert_eq!(handler.name(), "TestHandler");
        }

        #[tokio::test]
        async fn test_default_datagram_handler() {
            let handler = TestHandler::new(vec![StreamType::Membership]);
            let peer = PeerId::from([0u8; 32]);

            // Default implementation should succeed silently
            let result = handler
                .handle_datagram(peer, StreamType::Membership, Bytes::from_static(b"dgram"))
                .await;
            assert!(result.is_ok());
        }

        #[tokio::test]
        async fn test_default_shutdown() {
            let handler = TestHandler::new(vec![StreamType::Membership]);

            // Default shutdown implementation should succeed
            let result = handler.shutdown().await;
            assert!(result.is_ok());
        }
    }

    // =========================================================================
    // Phase 2: Handler Error Tests (TDD RED)
    // =========================================================================

    mod handler_error_tests {
        use super::*;

        #[test]
        fn test_handler_exists_error() {
            let err = LinkError::HandlerExists(StreamType::Membership);
            let msg = err.to_string();
            assert!(msg.contains("Membership"), "Error message: {}", msg);
            assert!(
                msg.to_lowercase().contains("handler"),
                "Error message: {}",
                msg
            );
        }

        #[test]
        fn test_no_handler_error() {
            let err = LinkError::NoHandler(StreamType::DhtQuery);
            let msg = err.to_string();
            assert!(msg.contains("DhtQuery"), "Error message: {}", msg);
        }

        #[test]
        fn test_not_running_error() {
            let err = LinkError::NotRunning;
            let msg = err.to_string();
            assert!(
                msg.to_lowercase().contains("not running"),
                "Error message: {}",
                msg
            );
        }

        #[test]
        fn test_already_running_error() {
            let err = LinkError::AlreadyRunning;
            let msg = err.to_string();
            assert!(
                msg.to_lowercase().contains("already running"),
                "Error message: {}",
                msg
            );
        }
    }
}