peat-mesh 0.8.1

Peat mesh networking library with CRDT sync, transport security, and topology management
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
//! Selective router implementation for hierarchical data routing
//!
//! This module implements the core routing logic that determines:
//! - Whether data should be consumed (processed) by this node
//! - Whether data should be forwarded to other nodes
//! - Which peer should receive forwarded data
//!
//! ## Message Deduplication
//!
//! The router includes optional message deduplication to prevent routing loops.
//! When enabled, each packet's ID is tracked and duplicate packets are automatically
//! dropped. The deduplication cache uses a time-based eviction strategy.
//!
//! ## Lock ordering
//!
//! `SelectiveRouter` contains a single lock:
//!
//! | Lock | Type | Protects |
//! |------|------|----------|
//! | `seen_packets` | `std::sync::RwLock<HashMap<String, DeduplicationEntry>>` | Dedup cache |
//!
//! Because only one lock exists, there is no ordering constraint within this
//! module. Callers that also hold locks from other modules (e.g.,
//! `TransportManager` or `PeatMesh::state`) should acquire `seen_packets`
//! **after** releasing those outer locks to avoid contention.

use super::packet::{DataDirection, DataPacket};
use crate::beacon::HierarchyLevel;
use crate::hierarchy::NodeRole;
use crate::topology::TopologyState;
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use std::time::{Duration, Instant};
use tracing::{debug, trace, warn};

/// Routing decision result
#[derive(Debug, Clone, PartialEq)]
pub enum RoutingDecision {
    /// Consume (process) the data locally
    Consume,

    /// Forward the data to a specific peer
    Forward { next_hop: String },

    /// Consume locally AND forward to peer
    ConsumeAndForward { next_hop: String },

    /// Forward the data to multiple peers (multicast/broadcast)
    ForwardMulticast { next_hops: Vec<String> },

    /// Consume locally AND forward to multiple peers (multicast/broadcast)
    ConsumeAndForwardMulticast { next_hops: Vec<String> },

    /// Drop the packet (reached max hops or no route)
    Drop,
}

/// Configuration for message deduplication
#[derive(Debug, Clone)]
pub struct DeduplicationConfig {
    /// Whether deduplication is enabled
    pub enabled: bool,
    /// How long to remember seen packet IDs (default: 5 minutes)
    pub ttl: Duration,
    /// Maximum number of packet IDs to track (default: 10000)
    pub max_entries: usize,
}

impl Default for DeduplicationConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            ttl: Duration::from_secs(300), // 5 minutes
            max_entries: 10000,
        }
    }
}

/// Entry in the deduplication cache
#[derive(Debug, Clone)]
struct DeduplicationEntry {
    /// When this packet was first seen
    first_seen: Instant,
}

/// Selective router for hierarchical mesh networks
///
/// Makes intelligent routing decisions based on:
/// - Node's position in hierarchy (level and role)
/// - Data direction (upward/downward/lateral)
/// - Topology state (selected peer, linked peers, lateral peers)
///
/// # Message Deduplication
///
/// The router can optionally track seen packet IDs to prevent routing loops.
/// Use `new_with_deduplication()` to enable this feature.
///
/// # Example
///
/// ```ignore
/// use peat_mesh::routing::{SelectiveRouter, DataPacket, DeduplicationConfig};
/// use peat_mesh::topology::TopologyState;
///
/// // Create router with deduplication enabled
/// let router = SelectiveRouter::new_with_deduplication(DeduplicationConfig::default());
/// let state = get_topology_state();
/// let packet = DataPacket::telemetry("node-123", vec![1, 2, 3]);
///
/// // Route will automatically deduplicate
/// let decision = router.route(&packet, &state, "this-node");
///
/// // Second call with same packet returns Drop (duplicate)
/// let decision2 = router.route(&packet, &state, "this-node");
/// assert_eq!(decision2, RoutingDecision::Drop);
/// ```
pub struct SelectiveRouter {
    /// Enable verbose logging for debugging
    verbose: bool,
    /// Deduplication configuration
    dedup_config: DeduplicationConfig,
    /// Cache of seen packet IDs (packet_id -> entry)
    seen_packets: Arc<RwLock<HashMap<String, DeduplicationEntry>>>,
}

impl SelectiveRouter {
    /// Create a new selective router (deduplication disabled by default)
    pub fn new() -> Self {
        Self {
            verbose: false,
            dedup_config: DeduplicationConfig {
                enabled: false,
                ..Default::default()
            },
            seen_packets: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Create a new selective router with verbose logging
    pub fn new_verbose() -> Self {
        Self {
            verbose: true,
            dedup_config: DeduplicationConfig {
                enabled: false,
                ..Default::default()
            },
            seen_packets: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Create a new selective router with deduplication enabled
    pub fn new_with_deduplication(config: DeduplicationConfig) -> Self {
        Self {
            verbose: false,
            dedup_config: config,
            seen_packets: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Check if a packet has been seen before (for deduplication)
    ///
    /// Returns `true` if this is a duplicate packet that should be dropped.
    /// If the packet is new, it's added to the seen cache.
    fn is_duplicate(&self, packet_id: &str) -> bool {
        if !self.dedup_config.enabled {
            return false;
        }

        let now = Instant::now();

        // Try to insert into cache
        let mut cache = self.seen_packets.write().unwrap_or_else(|e| e.into_inner());

        // Check if already seen and not expired
        if let Some(entry) = cache.get(packet_id) {
            if now.duration_since(entry.first_seen) < self.dedup_config.ttl {
                if self.verbose {
                    debug!("Duplicate packet detected: {}", packet_id);
                }
                return true;
            }
            // Entry expired, will be replaced below
        }

        // Evict expired entries if cache is getting full
        if cache.len() >= self.dedup_config.max_entries {
            self.evict_expired(&mut cache, now);

            // If still full after eviction, remove oldest entry
            if cache.len() >= self.dedup_config.max_entries {
                if let Some(oldest_key) = cache
                    .iter()
                    .min_by_key(|(_, entry)| entry.first_seen)
                    .map(|(k, _)| k.clone())
                {
                    cache.remove(&oldest_key);
                }
            }
        }

        // Record this packet
        cache.insert(
            packet_id.to_string(),
            DeduplicationEntry { first_seen: now },
        );

        false
    }

    /// Evict expired entries from the cache
    fn evict_expired(&self, cache: &mut HashMap<String, DeduplicationEntry>, now: Instant) {
        cache.retain(|_, entry| now.duration_since(entry.first_seen) < self.dedup_config.ttl);
    }

    /// Get the number of entries in the deduplication cache
    pub fn dedup_cache_size(&self) -> usize {
        self.seen_packets
            .read()
            .unwrap_or_else(|e| e.into_inner())
            .len()
    }

    /// Clear the deduplication cache
    pub fn clear_dedup_cache(&self) {
        self.seen_packets
            .write()
            .unwrap_or_else(|e| e.into_inner())
            .clear();
    }

    /// Make a complete routing decision for a packet
    ///
    /// This is the primary entry point that combines should_consume,
    /// should_forward, and next_hop into a single decision.
    ///
    /// If deduplication is enabled, duplicate packets are automatically dropped.
    ///
    /// # Arguments
    ///
    /// * `packet` - The data packet to route
    /// * `state` - Current topology state
    /// * `this_node_id` - This node's identifier
    ///
    /// # Returns
    ///
    /// RoutingDecision indicating what to do with the packet
    pub fn route(
        &self,
        packet: &DataPacket,
        state: &TopologyState,
        this_node_id: &str,
    ) -> RoutingDecision {
        // Check for duplicate packet (if deduplication enabled)
        if self.is_duplicate(&packet.packet_id) {
            if self.verbose {
                debug!("Packet {} is a duplicate, dropping", packet.packet_id);
            }
            return RoutingDecision::Drop;
        }

        // Check if packet has reached max hops
        if packet.at_max_hops() {
            if self.verbose {
                warn!(
                    "Packet {} reached max hops ({}), dropping",
                    packet.packet_id, packet.max_hops
                );
            }
            return RoutingDecision::Drop;
        }

        // Check if we're the source (don't route our own packets back to us)
        if packet.source_node_id == this_node_id {
            if self.verbose {
                trace!(
                    "Packet {} originated from us, not routing",
                    packet.packet_id
                );
            }
            return RoutingDecision::Drop;
        }

        let should_consume = self.should_consume(packet, state, this_node_id);
        let should_forward = self.should_forward(packet, state);

        if should_consume && should_forward {
            // Both consume and forward - check if multicast needed
            let next_hops = self.next_hops(packet, state);
            if next_hops.is_empty() {
                // Can't forward without next hop, just consume
                if self.verbose {
                    debug!("Packet {}: Consume only (no next hop)", packet.packet_id);
                }
                RoutingDecision::Consume
            } else if next_hops.len() == 1 {
                // Single next hop - use unicast variant
                if self.verbose {
                    debug!(
                        "Packet {}: Consume and forward to {}",
                        packet.packet_id, next_hops[0]
                    );
                }
                RoutingDecision::ConsumeAndForward {
                    next_hop: next_hops[0].clone(),
                }
            } else {
                // Multiple next hops - use multicast variant
                if self.verbose {
                    debug!(
                        "Packet {}: Consume and multicast to {} peers",
                        packet.packet_id,
                        next_hops.len()
                    );
                }
                RoutingDecision::ConsumeAndForwardMulticast { next_hops }
            }
        } else if should_consume {
            if self.verbose {
                debug!("Packet {}: Consume only", packet.packet_id);
            }
            RoutingDecision::Consume
        } else if should_forward {
            let next_hops = self.next_hops(packet, state);
            if next_hops.is_empty() {
                if self.verbose {
                    warn!(
                        "Packet {}: Should forward but no next hop, dropping",
                        packet.packet_id
                    );
                }
                RoutingDecision::Drop
            } else if next_hops.len() == 1 {
                // Single next hop - use unicast variant
                if self.verbose {
                    debug!("Packet {}: Forward to {}", packet.packet_id, next_hops[0]);
                }
                RoutingDecision::Forward {
                    next_hop: next_hops[0].clone(),
                }
            } else {
                // Multiple next hops - use multicast variant
                if self.verbose {
                    debug!(
                        "Packet {}: Multicast to {} peers",
                        packet.packet_id,
                        next_hops.len()
                    );
                }
                RoutingDecision::ForwardMulticast { next_hops }
            }
        } else {
            if self.verbose {
                debug!("Packet {}: Drop (not for us)", packet.packet_id);
            }
            RoutingDecision::Drop
        }
    }

    /// Determine if this node should consume (process) the packet
    ///
    /// # Consumption Rules
    ///
    /// **Upward (Telemetry)**
    /// - Always consume telemetry for local processing/aggregation
    ///
    /// **Downward (Commands)**
    /// - Consume if packet is addressed to us
    /// - Leaders consume commands for their squad
    ///
    /// **Lateral (Coordination)**
    /// - Leaders consume coordination messages
    /// - Members typically don't consume lateral messages
    ///
    /// # Arguments
    ///
    /// * `packet` - The data packet
    /// * `state` - Current topology state
    /// * `this_node_id` - This node's identifier
    ///
    /// # Returns
    ///
    /// `true` if this node should process the packet
    pub fn should_consume(
        &self,
        packet: &DataPacket,
        state: &TopologyState,
        this_node_id: &str,
    ) -> bool {
        match packet.direction {
            DataDirection::Upward => {
                // Upward data (telemetry, status): Always consume for aggregation
                // Every node in the path can aggregate/process
                true
            }

            DataDirection::Downward => {
                // Downward data (commands, config): Consume if targeted at us
                if let Some(ref dest) = packet.destination_node_id {
                    if dest == this_node_id {
                        return true;
                    }
                }

                // Leaders consume commands even if not directly targeted
                // (they may need to disseminate to squad members)
                matches!(state.role, NodeRole::Leader)
            }

            DataDirection::Lateral => {
                // Lateral data (coordination): Only Leaders typically consume
                if let Some(ref dest) = packet.destination_node_id {
                    // Consume only if directly addressed to us
                    dest == this_node_id
                } else {
                    // No specific destination (broadcast): Leaders consume
                    matches!(state.role, NodeRole::Leader)
                }
            }
        }
    }

    /// Determine if this node should forward the packet
    ///
    /// # Forwarding Rules
    ///
    /// **Upward (Telemetry)**
    /// - Forward if we have a selected peer (parent in hierarchy)
    /// - Don't forward if we're at HQ level (no parent)
    ///
    /// **Downward (Commands)**
    /// - Forward if we have linked peers (children) that need this data
    /// - Don't forward if we're a leaf node (no children)
    ///
    /// **Lateral (Coordination)**
    /// - Forward if addressed to a lateral peer we track
    /// - Leaders may forward to other Leaders at same level
    ///
    /// # Arguments
    ///
    /// * `packet` - The data packet
    /// * `state` - Current topology state
    ///
    /// # Returns
    ///
    /// `true` if packet should be forwarded to another peer
    pub fn should_forward(&self, packet: &DataPacket, state: &TopologyState) -> bool {
        match packet.direction {
            DataDirection::Upward => {
                // Forward upward if we have a selected peer (parent)
                state.selected_peer.is_some()
            }

            DataDirection::Downward => {
                // Forward downward if we have linked peers (children)
                !state.linked_peers.is_empty()
            }

            DataDirection::Lateral => {
                // Forward laterally if addressed to a peer we know
                if let Some(ref dest) = packet.destination_node_id {
                    state.lateral_peers.contains_key(dest)
                } else {
                    // Broadcast lateral messages if we're a Leader with lateral peers
                    matches!(state.role, NodeRole::Leader) && !state.lateral_peers.is_empty()
                }
            }
        }
    }

    /// Determine the next hop for forwarding the packet
    ///
    /// # Next Hop Selection
    ///
    /// **Upward**: selected_peer (parent in hierarchy)
    /// **Downward**: linked_peers (children) - for now, return first child
    /// **Lateral**: lateral_peers - specific peer if addressed, or first if broadcast
    ///
    /// # Arguments
    ///
    /// * `packet` - The data packet
    /// * `state` - Current topology state
    ///
    /// # Returns
    ///
    /// Node ID of the next hop, or None if no valid next hop
    pub fn next_hop(&self, packet: &DataPacket, state: &TopologyState) -> Option<String> {
        match packet.direction {
            DataDirection::Upward => {
                // Upward: Route to selected peer (parent)
                state
                    .selected_peer
                    .as_ref()
                    .map(|peer| peer.node_id.clone())
            }

            DataDirection::Downward => {
                // Downward: Route to linked peers (children)
                // If addressed to specific child, route there
                if let Some(ref dest) = packet.destination_node_id {
                    if state.linked_peers.contains_key(dest) {
                        return Some(dest.clone());
                    }
                }

                // Otherwise, return first linked peer for backward compatibility
                // For multicast/broadcast, use next_hops() instead
                state.linked_peers.keys().next().cloned()
            }

            DataDirection::Lateral => {
                // Lateral: Route to lateral peers
                if let Some(ref dest) = packet.destination_node_id {
                    // Route to specific lateral peer if we track them
                    if state.lateral_peers.contains_key(dest) {
                        return Some(dest.clone());
                    }
                }

                // Otherwise, route to first lateral peer for backward compatibility
                state.lateral_peers.keys().next().cloned()
            }
        }
    }

    /// Determine all next hops for multicast/broadcast forwarding
    ///
    /// Returns all appropriate peers for scenarios requiring multicast:
    /// - Downward command dissemination to all children
    /// - Lateral coordination broadcast to all peers at same level
    ///
    /// # Next Hops Selection
    ///
    /// **Upward**: Returns selected_peer (parent) as single-element vector
    /// **Downward**: Returns all linked_peers (children) for broadcast
    /// **Lateral**: Returns all lateral_peers for broadcast
    ///
    /// # Arguments
    ///
    /// * `packet` - The data packet
    /// * `state` - Current topology state
    ///
    /// # Returns
    ///
    /// Vector of node IDs to forward to (empty if no valid hops)
    pub fn next_hops(&self, packet: &DataPacket, state: &TopologyState) -> Vec<String> {
        match packet.direction {
            DataDirection::Upward => {
                // Upward: Return selected peer (parent) as single-element vector
                state
                    .selected_peer
                    .as_ref()
                    .map(|peer| vec![peer.node_id.clone()])
                    .unwrap_or_default()
            }

            DataDirection::Downward => {
                // Downward: Route to all linked peers (children) for broadcast
                // If addressed to specific child, route only there
                if let Some(ref dest) = packet.destination_node_id {
                    if state.linked_peers.contains_key(dest) {
                        return vec![dest.clone()];
                    }
                }

                // Otherwise, route to ALL linked peers (multicast/broadcast)
                state.linked_peers.keys().cloned().collect()
            }

            DataDirection::Lateral => {
                // Lateral: Route to all lateral peers for broadcast
                if let Some(ref dest) = packet.destination_node_id {
                    // Route to specific lateral peer if we track them
                    if state.lateral_peers.contains_key(dest) {
                        return vec![dest.clone()];
                    }
                }

                // Otherwise, route to ALL lateral peers (broadcast)
                state.lateral_peers.keys().cloned().collect()
            }
        }
    }

    /// Check if this node is at the hierarchy level that should aggregate
    ///
    /// HQ nodes (Company level) should aggregate and consume
    /// without further forwarding.
    #[allow(dead_code)]
    fn is_hq_level(&self, level: HierarchyLevel) -> bool {
        matches!(level, HierarchyLevel::Company)
    }

    /// Check if a packet should be aggregated before forwarding
    ///
    /// Aggregation is appropriate when:
    /// - Packet data type requires aggregation (Telemetry, Status)
    /// - Routing decision is ConsumeAndForward (intermediate node)
    /// - Node is a Leader (squad leader aggregating member data)
    ///
    /// # Integration with Aggregator
    ///
    /// When this returns true, the application should:
    /// 1. Collect telemetry packets from squad members (batching)
    /// 2. Use `Aggregator::aggregate_telemetry()` to create aggregated packet
    /// 3. Route the aggregated packet upward using this router
    ///
    /// # Example
    ///
    /// ```ignore
    /// use peat_mesh::routing::{SelectiveRouter, Aggregator, DataPacket};
    ///
    /// let router = SelectiveRouter::new();
    /// // let aggregator = MyAggregator::new();
    ///
    /// // Collect telemetry from squad members
    /// let mut squad_telemetry = Vec::new();
    /// for packet in incoming_packets {
    ///     let decision = router.route(&packet, &state, "platoon-leader");
    ///     if router.should_aggregate(&packet, &decision, &state) {
    ///         squad_telemetry.push(packet);
    ///     }
    /// }
    ///
    /// // Aggregate when we have enough data
    /// if squad_telemetry.len() >= 3 {
    ///     let aggregated = aggregator.aggregate_telemetry(
    ///         "squad-1",
    ///         "platoon-leader",
    ///         squad_telemetry,
    ///     )?;
    ///
    ///     // Route aggregated packet upward
    ///     let decision = router.route(&aggregated, &state, "platoon-leader");
    ///     // ... forward to parent
    /// }
    /// ```
    ///
    /// # Arguments
    ///
    /// * `packet` - The data packet to check
    /// * `decision` - The routing decision for this packet
    /// * `state` - Current topology state
    ///
    /// # Returns
    ///
    /// `true` if this packet should be aggregated before forwarding
    pub fn should_aggregate(
        &self,
        packet: &DataPacket,
        decision: &RoutingDecision,
        state: &TopologyState,
    ) -> bool {
        // Only aggregate if we're consuming and forwarding (intermediate node)
        if !matches!(decision, RoutingDecision::ConsumeAndForward { .. }) {
            return false;
        }

        // Only aggregate data types that require it
        if !packet.data_type.requires_aggregation() {
            return false;
        }

        // Only Leaders aggregate squad member data
        matches!(state.role, NodeRole::Leader)
    }
}

impl Default for SelectiveRouter {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::beacon::{GeoPosition, GeographicBeacon};
    use crate::routing::packet::{DataDirection, DataType};
    use crate::topology::SelectedPeer;
    use std::collections::HashMap;
    use std::time::Instant;

    fn create_test_state(
        hierarchy_level: HierarchyLevel,
        role: NodeRole,
        has_selected_peer: bool,
        num_linked_peers: usize,
        num_lateral_peers: usize,
    ) -> TopologyState {
        let selected_peer = if has_selected_peer {
            Some(SelectedPeer {
                node_id: "parent-node".to_string(),
                beacon: GeographicBeacon::new(
                    "parent-node".to_string(),
                    GeoPosition::new(37.7749, -122.4194),
                    HierarchyLevel::Platoon,
                ),
                selected_at: Instant::now(),
            })
        } else {
            None
        };

        let mut linked_peers = HashMap::new();
        for i in 0..num_linked_peers {
            linked_peers.insert(format!("linked-peer-{}", i), Instant::now());
        }

        let mut lateral_peers = HashMap::new();
        for i in 0..num_lateral_peers {
            lateral_peers.insert(format!("lateral-peer-{}", i), Instant::now());
        }

        TopologyState {
            selected_peer,
            linked_peers,
            lateral_peers,
            role,
            hierarchy_level,
        }
    }

    #[test]
    fn test_upward_telemetry_leaf_node() {
        let router = SelectiveRouter::new();
        let state = create_test_state(HierarchyLevel::Squad, NodeRole::Member, true, 0, 0);
        let packet = DataPacket::telemetry("sensor-1", vec![1, 2, 3]);

        // Leaf node should consume telemetry
        assert!(router.should_consume(&packet, &state, "this-node"));

        // Leaf node with parent should forward
        assert!(router.should_forward(&packet, &state));

        // Next hop should be parent
        let next_hop = router.next_hop(&packet, &state);
        assert_eq!(next_hop, Some("parent-node".to_string()));
    }

    #[test]
    fn test_upward_telemetry_hq_node() {
        let router = SelectiveRouter::new();
        // HQ node (no selected peer = highest level)
        let state = create_test_state(HierarchyLevel::Company, NodeRole::Leader, false, 3, 0);
        let packet = DataPacket::telemetry("sensor-1", vec![1, 2, 3]);

        // HQ should consume telemetry
        assert!(router.should_consume(&packet, &state, "hq-node"));

        // HQ should NOT forward (no parent)
        assert!(!router.should_forward(&packet, &state));
    }

    #[test]
    fn test_downward_command_to_leader() {
        let router = SelectiveRouter::new();
        let state = create_test_state(HierarchyLevel::Platoon, NodeRole::Leader, true, 3, 0);
        let packet = DataPacket::command("hq", "platoon-leader", vec![4, 5, 6]);

        // Leader should consume command addressed to them
        assert!(router.should_consume(&packet, &state, "platoon-leader"));

        // Leader with children should forward
        assert!(router.should_forward(&packet, &state));

        // Next hop should be one of the linked peers (children)
        let next_hop = router.next_hop(&packet, &state);
        assert!(next_hop.is_some());
        assert!(next_hop.unwrap().starts_with("linked-peer-"));
    }

    #[test]
    fn test_downward_command_to_leaf() {
        let router = SelectiveRouter::new();
        let state = create_test_state(HierarchyLevel::Squad, NodeRole::Member, true, 0, 0);
        let packet = DataPacket::command("hq", "squad-member", vec![4, 5, 6]);

        // Member should consume command addressed to them
        assert!(router.should_consume(&packet, &state, "squad-member"));

        // Leaf node should NOT forward (no children)
        assert!(!router.should_forward(&packet, &state));
    }

    #[test]
    fn test_lateral_coordination_between_leaders() {
        let router = SelectiveRouter::new();
        let state = create_test_state(HierarchyLevel::Platoon, NodeRole::Leader, true, 2, 3);
        let packet = DataPacket::coordination("platoon-1", "lateral-peer-0", vec![7, 8, 9]);

        // Leader should NOT consume lateral coordination if not addressed to them
        assert!(!router.should_consume(&packet, &state, "platoon-3"));

        // Should forward if addressed to a lateral peer we track
        let state_with_target =
            create_test_state(HierarchyLevel::Platoon, NodeRole::Leader, true, 2, 3);
        assert!(router.should_forward(&packet, &state_with_target));
    }

    #[test]
    fn test_max_hops_drop() {
        let router = SelectiveRouter::new();
        let state = create_test_state(HierarchyLevel::Squad, NodeRole::Member, true, 0, 0);
        let mut packet = DataPacket::telemetry("sensor-1", vec![1, 2, 3]);

        // Increment hops to max
        for _ in 0..10 {
            packet.increment_hop();
        }

        // Routing should return Drop when at max hops
        let decision = router.route(&packet, &state, "this-node");
        assert_eq!(decision, RoutingDecision::Drop);
    }

    #[test]
    fn test_routing_decision_consume_and_forward() {
        let router = SelectiveRouter::new();
        // Intermediate node with parent and children
        let state = create_test_state(HierarchyLevel::Platoon, NodeRole::Leader, true, 3, 0);
        let packet = DataPacket::telemetry("sensor-1", vec![1, 2, 3]);

        let decision = router.route(&packet, &state, "platoon-leader");

        // Should consume and forward
        match decision {
            RoutingDecision::ConsumeAndForward { next_hop } => {
                assert_eq!(next_hop, "parent-node");
            }
            _ => panic!("Expected ConsumeAndForward, got {:?}", decision),
        }
    }

    #[test]
    fn test_routing_decision_consume_only() {
        let router = SelectiveRouter::new();
        // HQ node (no parent)
        let state = create_test_state(HierarchyLevel::Company, NodeRole::Leader, false, 3, 0);
        let packet = DataPacket::telemetry("sensor-1", vec![1, 2, 3]);

        let decision = router.route(&packet, &state, "hq-node");

        // Should consume only (no forwarding)
        assert_eq!(decision, RoutingDecision::Consume);
    }

    #[test]
    fn test_dont_route_own_packets() {
        let router = SelectiveRouter::new();
        let state = create_test_state(HierarchyLevel::Squad, NodeRole::Member, true, 0, 0);
        let packet = DataPacket::telemetry("this-node", vec![1, 2, 3]);

        // Should not route our own packets back to us
        let decision = router.route(&packet, &state, "this-node");
        assert_eq!(decision, RoutingDecision::Drop);
    }

    #[test]
    fn test_should_aggregate_intermediate_leader() {
        let router = SelectiveRouter::new();
        // Intermediate Leader node (has parent and children)
        let state = create_test_state(HierarchyLevel::Platoon, NodeRole::Leader, true, 3, 0);
        let packet = DataPacket::telemetry("squad-member-1", vec![1, 2, 3]);

        let decision = router.route(&packet, &state, "platoon-leader");

        // Should aggregate: Leader with ConsumeAndForward decision
        assert!(router.should_aggregate(&packet, &decision, &state));
    }

    #[test]
    fn test_should_not_aggregate_hq_node() {
        let router = SelectiveRouter::new();
        // HQ node (no parent, just consumes)
        let state = create_test_state(HierarchyLevel::Company, NodeRole::Leader, false, 3, 0);
        let packet = DataPacket::telemetry("platoon-1", vec![1, 2, 3]);

        let decision = router.route(&packet, &state, "hq-node");

        // Should NOT aggregate: Decision is Consume only (not ConsumeAndForward)
        assert!(!router.should_aggregate(&packet, &decision, &state));
    }

    #[test]
    fn test_should_not_aggregate_non_leader() {
        let router = SelectiveRouter::new();
        // Member node (not a Leader)
        let state = create_test_state(HierarchyLevel::Squad, NodeRole::Member, true, 0, 0);
        let packet = DataPacket::telemetry("sensor-1", vec![1, 2, 3]);

        let decision = router.route(&packet, &state, "squad-member");

        // Should NOT aggregate: Not a Leader
        assert!(!router.should_aggregate(&packet, &decision, &state));
    }

    #[test]
    fn test_should_not_aggregate_command_packet() {
        let router = SelectiveRouter::new();
        // Leader node
        let state = create_test_state(HierarchyLevel::Platoon, NodeRole::Leader, true, 3, 0);
        let packet = DataPacket::command("hq", "platoon-leader", vec![4, 5, 6]);

        let decision = router.route(&packet, &state, "platoon-leader");

        // Should NOT aggregate: Command packets don't require aggregation
        assert!(!router.should_aggregate(&packet, &decision, &state));
    }

    // ============================================================================
    // Week 10: Multicast/Broadcast Routing Tests
    // ============================================================================

    #[test]
    fn test_next_hops_upward_single_parent() {
        let router = SelectiveRouter::new();
        let state = create_test_state(HierarchyLevel::Squad, NodeRole::Member, true, 0, 0);
        let packet = DataPacket::telemetry("sensor-1", vec![1, 2, 3]);

        // Upward should return selected_peer as single-element vector
        let next_hops = router.next_hops(&packet, &state);
        assert_eq!(next_hops.len(), 1);
        assert_eq!(next_hops[0], "parent-node");
    }

    #[test]
    fn test_next_hops_upward_no_parent() {
        let router = SelectiveRouter::new();
        // HQ node with no parent
        let state = create_test_state(HierarchyLevel::Company, NodeRole::Leader, false, 3, 0);
        let packet = DataPacket::telemetry("sensor-1", vec![1, 2, 3]);

        // No parent means empty vector
        let next_hops = router.next_hops(&packet, &state);
        assert_eq!(next_hops.len(), 0);
    }

    #[test]
    fn test_next_hops_downward_multicast() {
        let router = SelectiveRouter::new();
        // Platoon leader with 5 linked peers (children)
        let state = create_test_state(HierarchyLevel::Platoon, NodeRole::Leader, true, 5, 0);
        // Create broadcast command packet (no specific destination)
        let packet = DataPacket {
            packet_id: uuid::Uuid::new_v4().to_string(),
            source_node_id: "hq".to_string(),
            destination_node_id: None, // Broadcast
            data_type: DataType::Command,
            direction: DataDirection::Downward,
            hop_count: 0,
            max_hops: 10,
            payload: vec![4, 5, 6],
        };

        // Downward broadcast should return ALL linked peers
        let next_hops = router.next_hops(&packet, &state);
        assert_eq!(next_hops.len(), 5);
        for i in 0..5 {
            assert!(next_hops.contains(&format!("linked-peer-{}", i)));
        }
    }

    #[test]
    fn test_next_hops_downward_targeted() {
        let router = SelectiveRouter::new();
        // Platoon leader with 3 linked peers
        let state = create_test_state(HierarchyLevel::Platoon, NodeRole::Leader, true, 3, 0);
        let packet = DataPacket::command("hq", "linked-peer-1", vec![4, 5, 6]);

        // Targeted downward should return only the specific child
        let next_hops = router.next_hops(&packet, &state);
        assert_eq!(next_hops.len(), 1);
        assert_eq!(next_hops[0], "linked-peer-1");
    }

    #[test]
    fn test_next_hops_lateral_multicast() {
        let router = SelectiveRouter::new();
        // Leader with 4 lateral peers
        let state = create_test_state(HierarchyLevel::Platoon, NodeRole::Leader, true, 2, 4);
        // Create broadcast coordination packet (no specific destination)
        let packet = DataPacket {
            packet_id: uuid::Uuid::new_v4().to_string(),
            source_node_id: "platoon-1".to_string(),
            destination_node_id: None, // Broadcast
            data_type: DataType::Coordination,
            direction: DataDirection::Lateral,
            hop_count: 0,
            max_hops: 3,
            payload: vec![7, 8, 9],
        };

        // Lateral broadcast should return ALL lateral peers
        let next_hops = router.next_hops(&packet, &state);
        assert_eq!(next_hops.len(), 4);
        for i in 0..4 {
            assert!(next_hops.contains(&format!("lateral-peer-{}", i)));
        }
    }

    #[test]
    fn test_next_hops_lateral_targeted() {
        let router = SelectiveRouter::new();
        // Leader with 3 lateral peers
        let state = create_test_state(HierarchyLevel::Platoon, NodeRole::Leader, true, 2, 3);
        let packet = DataPacket::coordination("platoon-1", "lateral-peer-2", vec![7, 8, 9]);

        // Targeted lateral should return only specific peer
        let next_hops = router.next_hops(&packet, &state);
        assert_eq!(next_hops.len(), 1);
        assert_eq!(next_hops[0], "lateral-peer-2");
    }

    #[test]
    fn test_route_downward_multicast() {
        let router = SelectiveRouter::new();
        // HQ node (Leader) with 3 children, broadcasting command
        let state = create_test_state(HierarchyLevel::Company, NodeRole::Leader, false, 3, 0);
        // Create broadcast command packet (no specific destination)
        let packet = DataPacket {
            packet_id: uuid::Uuid::new_v4().to_string(),
            source_node_id: "hq".to_string(),
            destination_node_id: None, // Broadcast
            data_type: DataType::Command,
            direction: DataDirection::Downward,
            hop_count: 0,
            max_hops: 10,
            payload: vec![4, 5, 6],
        };

        let decision = router.route(&packet, &state, "hq-node");

        // Leaders should consume broadcast commands AND multicast to all children
        match decision {
            RoutingDecision::ConsumeAndForwardMulticast { next_hops } => {
                assert_eq!(next_hops.len(), 3);
                assert!(next_hops.contains(&"linked-peer-0".to_string()));
                assert!(next_hops.contains(&"linked-peer-1".to_string()));
                assert!(next_hops.contains(&"linked-peer-2".to_string()));
            }
            _ => panic!("Expected ConsumeAndForwardMulticast, got {:?}", decision),
        }
    }

    #[test]
    fn test_route_downward_consume_and_multicast() {
        let router = SelectiveRouter::new();
        // Platoon leader with 4 children, receiving command addressed to them
        let state = create_test_state(HierarchyLevel::Platoon, NodeRole::Leader, true, 4, 0);
        let packet = DataPacket::command("hq", "platoon-leader", vec![4, 5, 6]);

        let decision = router.route(&packet, &state, "platoon-leader");

        // Should consume (addressed to us) AND multicast to all children
        match decision {
            RoutingDecision::ConsumeAndForwardMulticast { next_hops } => {
                assert_eq!(next_hops.len(), 4);
                for i in 0..4 {
                    assert!(next_hops.contains(&format!("linked-peer-{}", i)));
                }
            }
            _ => panic!("Expected ConsumeAndForwardMulticast, got {:?}", decision),
        }
    }

    #[test]
    fn test_route_lateral_multicast() {
        let router = SelectiveRouter::new();
        // Leader with 3 lateral peers, broadcasting coordination
        let state = create_test_state(HierarchyLevel::Platoon, NodeRole::Leader, true, 0, 3);
        // Create broadcast coordination packet (no specific destination)
        let packet = DataPacket {
            packet_id: uuid::Uuid::new_v4().to_string(),
            source_node_id: "platoon-1".to_string(),
            destination_node_id: None, // Broadcast
            data_type: DataType::Coordination,
            direction: DataDirection::Lateral,
            hop_count: 0,
            max_hops: 3,
            payload: vec![7, 8, 9],
        };

        let decision = router.route(&packet, &state, "platoon-3");

        // Leaders should consume broadcast coordination AND forward to all lateral peers
        match decision {
            RoutingDecision::ConsumeAndForwardMulticast { next_hops } => {
                assert_eq!(next_hops.len(), 3);
                assert!(next_hops.contains(&"lateral-peer-0".to_string()));
                assert!(next_hops.contains(&"lateral-peer-1".to_string()));
                assert!(next_hops.contains(&"lateral-peer-2".to_string()));
            }
            _ => panic!("Expected ConsumeAndForwardMulticast, got {:?}", decision),
        }
    }

    #[test]
    fn test_route_downward_single_child_unicast() {
        let router = SelectiveRouter::new();
        // Leader with only 1 child
        let state = create_test_state(HierarchyLevel::Platoon, NodeRole::Leader, false, 1, 0);
        // Create broadcast command packet (no specific destination)
        let packet = DataPacket {
            packet_id: uuid::Uuid::new_v4().to_string(),
            source_node_id: "hq".to_string(),
            destination_node_id: None, // Broadcast
            data_type: DataType::Command,
            direction: DataDirection::Downward,
            hop_count: 0,
            max_hops: 10,
            payload: vec![4, 5, 6],
        };

        let decision = router.route(&packet, &state, "platoon-leader");

        // Leaders consume broadcast commands, and with only 1 child use unicast variant
        match decision {
            RoutingDecision::ConsumeAndForward { next_hop } => {
                assert_eq!(next_hop, "linked-peer-0");
            }
            _ => panic!("Expected ConsumeAndForward (unicast), got {:?}", decision),
        }
    }

    #[test]
    fn test_route_lateral_single_peer_unicast() {
        let router = SelectiveRouter::new();
        // Leader with only 1 lateral peer
        let state = create_test_state(HierarchyLevel::Platoon, NodeRole::Leader, true, 0, 1);
        // Create broadcast coordination packet (no specific destination)
        let packet = DataPacket {
            packet_id: uuid::Uuid::new_v4().to_string(),
            source_node_id: "platoon-1".to_string(),
            destination_node_id: None, // Broadcast
            data_type: DataType::Coordination,
            direction: DataDirection::Lateral,
            hop_count: 0,
            max_hops: 3,
            payload: vec![7, 8, 9],
        };

        let decision = router.route(&packet, &state, "platoon-3");

        // Leaders consume broadcast coordination, and with only 1 lateral peer use unicast variant
        match decision {
            RoutingDecision::ConsumeAndForward { next_hop } => {
                assert_eq!(next_hop, "lateral-peer-0");
            }
            _ => panic!("Expected ConsumeAndForward (unicast), got {:?}", decision),
        }
    }

    #[test]
    fn test_route_downward_no_children_drop() {
        let router = SelectiveRouter::new();
        // Leaf node with no children
        let state = create_test_state(HierarchyLevel::Squad, NodeRole::Member, true, 0, 0);
        // Create broadcast command packet (no specific destination)
        let packet = DataPacket {
            packet_id: uuid::Uuid::new_v4().to_string(),
            source_node_id: "hq".to_string(),
            destination_node_id: None, // Broadcast
            data_type: DataType::Command,
            direction: DataDirection::Downward,
            hop_count: 0,
            max_hops: 10,
            payload: vec![4, 5, 6],
        };

        let decision = router.route(&packet, &state, "squad-member");

        // With no children, downward broadcast should drop (or consume if addressed)
        // In this case, not addressed to us, so should drop
        assert_eq!(decision, RoutingDecision::Drop);
    }

    #[test]
    fn test_multicast_preserves_backward_compatibility() {
        let router = SelectiveRouter::new();
        // Intermediate node with parent (upward routing)
        let state = create_test_state(HierarchyLevel::Platoon, NodeRole::Leader, true, 3, 0);
        let packet = DataPacket::telemetry("sensor-1", vec![1, 2, 3]);

        let decision = router.route(&packet, &state, "platoon-leader");

        // Upward routing should still use ConsumeAndForward (unicast)
        match decision {
            RoutingDecision::ConsumeAndForward { next_hop } => {
                assert_eq!(next_hop, "parent-node");
            }
            _ => panic!(
                "Expected ConsumeAndForward (backward compat), got {:?}",
                decision
            ),
        }

        // next_hop() should still work for backward compatibility
        let next_hop = router.next_hop(&packet, &state);
        assert_eq!(next_hop, Some("parent-node".to_string()));
    }

    // ============================================================================
    // Message Deduplication Tests
    // ============================================================================

    #[test]
    fn test_deduplication_disabled_by_default() {
        let router = SelectiveRouter::new();
        let state = create_test_state(HierarchyLevel::Squad, NodeRole::Member, true, 0, 0);
        let packet = DataPacket::telemetry("sensor-1", vec![1, 2, 3]);

        // Route same packet twice - should NOT be dropped (dedup disabled)
        let decision1 = router.route(&packet, &state, "this-node");
        let decision2 = router.route(&packet, &state, "this-node");

        // Both should route normally (ConsumeAndForward)
        assert!(matches!(
            decision1,
            RoutingDecision::ConsumeAndForward { .. }
        ));
        assert!(matches!(
            decision2,
            RoutingDecision::ConsumeAndForward { .. }
        ));
        assert_eq!(router.dedup_cache_size(), 0);
    }

    #[test]
    fn test_deduplication_enabled() {
        let router = SelectiveRouter::new_with_deduplication(DeduplicationConfig::default());
        let state = create_test_state(HierarchyLevel::Squad, NodeRole::Member, true, 0, 0);
        let packet = DataPacket::telemetry("sensor-1", vec![1, 2, 3]);

        // First route should succeed
        let decision1 = router.route(&packet, &state, "this-node");
        assert!(matches!(
            decision1,
            RoutingDecision::ConsumeAndForward { .. }
        ));
        assert_eq!(router.dedup_cache_size(), 1);

        // Second route of same packet should be dropped
        let decision2 = router.route(&packet, &state, "this-node");
        assert_eq!(decision2, RoutingDecision::Drop);
        assert_eq!(router.dedup_cache_size(), 1); // No new entry added
    }

    #[test]
    fn test_deduplication_different_packets() {
        let router = SelectiveRouter::new_with_deduplication(DeduplicationConfig::default());
        let state = create_test_state(HierarchyLevel::Squad, NodeRole::Member, true, 0, 0);
        let packet1 = DataPacket::telemetry("sensor-1", vec![1, 2, 3]);
        let packet2 = DataPacket::telemetry("sensor-2", vec![4, 5, 6]);

        // Route two different packets - both should succeed
        let decision1 = router.route(&packet1, &state, "this-node");
        let decision2 = router.route(&packet2, &state, "this-node");

        assert!(matches!(
            decision1,
            RoutingDecision::ConsumeAndForward { .. }
        ));
        assert!(matches!(
            decision2,
            RoutingDecision::ConsumeAndForward { .. }
        ));
        assert_eq!(router.dedup_cache_size(), 2);
    }

    #[test]
    fn test_deduplication_cache_clear() {
        let router = SelectiveRouter::new_with_deduplication(DeduplicationConfig::default());
        let state = create_test_state(HierarchyLevel::Squad, NodeRole::Member, true, 0, 0);
        let packet = DataPacket::telemetry("sensor-1", vec![1, 2, 3]);

        // Route packet
        let _ = router.route(&packet, &state, "this-node");
        assert_eq!(router.dedup_cache_size(), 1);

        // Clear cache
        router.clear_dedup_cache();
        assert_eq!(router.dedup_cache_size(), 0);

        // Should be able to route same packet again
        let decision = router.route(&packet, &state, "this-node");
        assert!(matches!(
            decision,
            RoutingDecision::ConsumeAndForward { .. }
        ));
    }

    #[test]
    fn test_deduplication_config_defaults() {
        let config = DeduplicationConfig::default();
        assert!(config.enabled);
        assert_eq!(config.ttl, Duration::from_secs(300));
        assert_eq!(config.max_entries, 10000);
    }

    #[test]
    fn test_verbose_router() {
        let router = SelectiveRouter::new_verbose();
        let state = create_test_state(HierarchyLevel::Squad, NodeRole::Member, true, 0, 0);
        let packet = DataPacket::telemetry("sensor-1", vec![1, 2, 3]);

        // Should work the same as non-verbose, just with logging
        let decision = router.route(&packet, &state, "this-node");
        assert!(matches!(
            decision,
            RoutingDecision::ConsumeAndForward { .. }
        ));
    }

    #[test]
    fn test_verbose_max_hops_drop() {
        let router = SelectiveRouter::new_verbose();
        let state = create_test_state(HierarchyLevel::Squad, NodeRole::Member, true, 0, 0);
        let mut packet = DataPacket::telemetry("sensor-1", vec![1, 2, 3]);
        for _ in 0..10 {
            packet.increment_hop();
        }
        let decision = router.route(&packet, &state, "this-node");
        assert_eq!(decision, RoutingDecision::Drop);
    }

    #[test]
    fn test_verbose_own_packet_drop() {
        let router = SelectiveRouter::new_verbose();
        let state = create_test_state(HierarchyLevel::Squad, NodeRole::Member, true, 0, 0);
        let packet = DataPacket::telemetry("this-node", vec![1, 2, 3]);
        let decision = router.route(&packet, &state, "this-node");
        assert_eq!(decision, RoutingDecision::Drop);
    }

    #[test]
    fn test_verbose_consume_only() {
        let router = SelectiveRouter::new_verbose();
        let state = create_test_state(HierarchyLevel::Company, NodeRole::Leader, false, 3, 0);
        let packet = DataPacket::telemetry("sensor-1", vec![1, 2, 3]);
        let decision = router.route(&packet, &state, "hq-node");
        assert_eq!(decision, RoutingDecision::Consume);
    }

    #[test]
    fn test_verbose_consume_and_forward() {
        let router = SelectiveRouter::new_verbose();
        let state = create_test_state(HierarchyLevel::Platoon, NodeRole::Leader, true, 3, 0);
        let packet = DataPacket::telemetry("sensor-1", vec![1, 2, 3]);
        let decision = router.route(&packet, &state, "platoon-leader");
        assert!(matches!(
            decision,
            RoutingDecision::ConsumeAndForward { .. }
        ));
    }

    #[test]
    fn test_verbose_consume_and_multicast() {
        let router = SelectiveRouter::new_verbose();
        let state = create_test_state(HierarchyLevel::Platoon, NodeRole::Leader, true, 4, 0);
        let packet = DataPacket::command("hq", "platoon-leader", vec![4, 5, 6]);
        let decision = router.route(&packet, &state, "platoon-leader");
        assert!(matches!(
            decision,
            RoutingDecision::ConsumeAndForwardMulticast { .. }
        ));
    }

    #[test]
    fn test_verbose_forward_multicast() {
        let router = SelectiveRouter::new_verbose();
        // Member (not leader) with lateral peers and broadcast lateral packet
        let state = create_test_state(HierarchyLevel::Platoon, NodeRole::Member, true, 3, 0);
        // Downward broadcast from hq - member doesn't consume, should forward to children
        let packet = DataPacket {
            packet_id: uuid::Uuid::new_v4().to_string(),
            source_node_id: "hq".to_string(),
            destination_node_id: None,
            data_type: DataType::Command,
            direction: DataDirection::Downward,
            hop_count: 0,
            max_hops: 10,
            payload: vec![4, 5, 6],
        };
        let decision = router.route(&packet, &state, "member-node");
        assert!(matches!(decision, RoutingDecision::ForwardMulticast { .. }));
    }

    #[test]
    fn test_verbose_forward_unicast() {
        let router = SelectiveRouter::new_verbose();
        let state = create_test_state(HierarchyLevel::Platoon, NodeRole::Member, true, 1, 0);
        let packet = DataPacket {
            packet_id: uuid::Uuid::new_v4().to_string(),
            source_node_id: "hq".to_string(),
            destination_node_id: None,
            data_type: DataType::Command,
            direction: DataDirection::Downward,
            hop_count: 0,
            max_hops: 10,
            payload: vec![4, 5, 6],
        };
        let decision = router.route(&packet, &state, "member-node");
        assert!(matches!(decision, RoutingDecision::Forward { .. }));
    }

    #[test]
    fn test_verbose_forward_no_next_hop_drop() {
        let router = SelectiveRouter::new_verbose();
        // Member with lateral peer but packet directed to unknown lateral
        let state = create_test_state(HierarchyLevel::Platoon, NodeRole::Member, false, 0, 1);
        let packet = DataPacket {
            packet_id: uuid::Uuid::new_v4().to_string(),
            source_node_id: "other".to_string(),
            destination_node_id: Some("lateral-peer-0".to_string()),
            data_type: DataType::Coordination,
            direction: DataDirection::Lateral,
            hop_count: 0,
            max_hops: 3,
            payload: vec![7, 8, 9],
        };
        // Member doesn't consume lateral (not addressed to us), but does forward
        let decision = router.route(&packet, &state, "member-node");
        assert!(matches!(decision, RoutingDecision::Forward { .. }));
    }

    #[test]
    fn test_verbose_drop_not_for_us() {
        let router = SelectiveRouter::new_verbose();
        let state = create_test_state(HierarchyLevel::Squad, NodeRole::Member, false, 0, 0);
        // Lateral packet not addressed to us, no lateral peers
        let packet = DataPacket {
            packet_id: uuid::Uuid::new_v4().to_string(),
            source_node_id: "other".to_string(),
            destination_node_id: Some("someone-else".to_string()),
            data_type: DataType::Coordination,
            direction: DataDirection::Lateral,
            hop_count: 0,
            max_hops: 3,
            payload: vec![7, 8, 9],
        };
        let decision = router.route(&packet, &state, "member-node");
        assert_eq!(decision, RoutingDecision::Drop);
    }

    #[test]
    fn test_verbose_dedup_drop() {
        let config = DeduplicationConfig {
            enabled: true,
            ttl: Duration::from_secs(300),
            max_entries: 100,
        };
        let router = SelectiveRouter {
            verbose: true,
            dedup_config: config,
            seen_packets: Arc::new(RwLock::new(HashMap::new())),
        };
        let state = create_test_state(HierarchyLevel::Squad, NodeRole::Member, true, 0, 0);
        let packet = DataPacket::telemetry("sensor-1", vec![1, 2, 3]);

        let _ = router.route(&packet, &state, "this-node");
        let decision2 = router.route(&packet, &state, "this-node");
        assert_eq!(decision2, RoutingDecision::Drop);
    }

    #[test]
    fn test_forward_only_no_consume_member_downward() {
        // Member with children receiving non-addressed broadcast command
        let router = SelectiveRouter::new();
        let state = create_test_state(HierarchyLevel::Platoon, NodeRole::Member, true, 2, 0);
        let packet = DataPacket {
            packet_id: uuid::Uuid::new_v4().to_string(),
            source_node_id: "hq".to_string(),
            destination_node_id: None,
            data_type: DataType::Command,
            direction: DataDirection::Downward,
            hop_count: 0,
            max_hops: 10,
            payload: vec![4, 5, 6],
        };
        let decision = router.route(&packet, &state, "member-node");
        // Member doesn't consume broadcast commands, just forwards
        assert!(matches!(decision, RoutingDecision::ForwardMulticast { .. }));
    }

    #[test]
    fn test_should_forward_no_next_hop_returns_drop() {
        let router = SelectiveRouter::new();
        // Member with no linked peers, no lateral peers, downward packet addressed to unknown
        let state = create_test_state(HierarchyLevel::Squad, NodeRole::Member, false, 0, 0);
        let packet = DataPacket {
            packet_id: uuid::Uuid::new_v4().to_string(),
            source_node_id: "hq".to_string(),
            destination_node_id: None,
            data_type: DataType::Command,
            direction: DataDirection::Downward,
            hop_count: 0,
            max_hops: 10,
            payload: vec![4, 5, 6],
        };
        let decision = router.route(&packet, &state, "squad-member");
        assert_eq!(decision, RoutingDecision::Drop);
    }

    #[test]
    fn test_next_hop_downward_targeted_not_found() {
        let router = SelectiveRouter::new();
        let state = create_test_state(HierarchyLevel::Platoon, NodeRole::Leader, true, 3, 0);
        // Addressed to a peer not in linked_peers
        let packet = DataPacket::command("hq", "unknown-child", vec![4, 5, 6]);

        let next_hop = router.next_hop(&packet, &state);
        // Should return first linked peer as fallback
        assert!(next_hop.is_some());
        assert!(next_hop.unwrap().starts_with("linked-peer-"));
    }

    #[test]
    fn test_next_hop_lateral_unknown_peer() {
        let router = SelectiveRouter::new();
        let state = create_test_state(HierarchyLevel::Platoon, NodeRole::Leader, true, 0, 3);
        let packet = DataPacket::coordination("source", "unknown-lateral", vec![7, 8, 9]);

        let next_hop = router.next_hop(&packet, &state);
        // Should return first lateral peer as fallback
        assert!(next_hop.is_some());
        assert!(next_hop.unwrap().starts_with("lateral-peer-"));
    }

    #[test]
    fn test_default_router() {
        let router = SelectiveRouter::default();
        assert_eq!(router.dedup_cache_size(), 0);
    }

    #[test]
    fn test_deduplication_max_entries_eviction() {
        let config = DeduplicationConfig {
            enabled: true,
            ttl: Duration::from_secs(300),
            max_entries: 3, // Very small for testing
        };
        let router = SelectiveRouter::new_with_deduplication(config);
        let state = create_test_state(HierarchyLevel::Squad, NodeRole::Member, true, 0, 0);

        // Route 5 packets
        for i in 0..5 {
            let packet = DataPacket::telemetry(format!("sensor-{}", i), vec![i as u8]);
            let _ = router.route(&packet, &state, "this-node");
        }

        // Cache should be limited to max_entries
        assert!(router.dedup_cache_size() <= 3);
    }
}