synapse-waf 0.9.1

High-performance WAF and reverse proxy with embedded intelligence — built on Cloudflare Pingora
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
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
//! Campaign Correlation Manager
//!
//! Orchestrates fingerprint indexing, campaign detection, and state management.
//! This is the main entry point for the correlation subsystem.
//!
//! # Architecture
//!
//! The `CampaignManager` coordinates three main components:
//! - **FingerprintIndex**: O(1) fingerprint→IPs lookup for efficient correlation
//! - **CampaignStore**: Campaign state storage with thread-safe access
//! - **Detectors**: SharedFingerprintDetector and Ja4RotationDetector for pattern detection
//!
//! # Usage
//!
//! ```rust,ignore
//! use synapse_pingora::correlation::{CampaignManager, ManagerConfig};
//! use std::sync::Arc;
//!
//! // Create manager with custom configuration
//! let config = ManagerConfig {
//!     shared_threshold: 3,
//!     rotation_threshold: 3,
//!     background_scanning: true,
//!     ..Default::default()
//! };
//! let manager = Arc::new(CampaignManager::with_config(config));
//!
//! // Register fingerprints during request processing (fast path)
//! let ip = "192.168.1.100".parse().unwrap();
//! manager.register_ja4(ip, "t13d1516h2_abc123".to_string());
//!
//! // Start background worker for periodic detection
//! let worker = Arc::clone(&manager).start_background_worker();
//! ```
//!
//! # Performance
//!
//! - Registration operations are O(1) and non-blocking for hot path
//! - Detection cycles are run periodically in background, not per-request
//! - All structures use lock-free DashMap for high concurrency

use std::net::IpAddr;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};

use futures::future::join_all;
use parking_lot::RwLock as ParkingLotRwLock;
use tokio::sync::{Mutex, RwLock};
use tokio::time::interval;

use crate::access::AccessListManager;
use crate::correlation::{
    Campaign, CampaignStatus, CampaignStore, CampaignStoreStats, CampaignUpdate, FingerprintGroup,
    FingerprintIndex, IndexStats,
};
use crate::telemetry::{TelemetryClient, TelemetryEvent};

use crate::correlation::detectors::{
    AttackPayload,
    AttackSequenceConfig,
    // New detectors
    AttackSequenceDetector,
    AuthTokenConfig,
    AuthTokenDetector,
    BehavioralConfig,
    BehavioralSimilarityDetector,
    Detector,
    DetectorError,
    DetectorResult,
    GraphConfig,
    GraphDetector,
    Ja4RotationDetector,
    NetworkProximityConfig,
    NetworkProximityDetector,
    RotationConfig,
    // Fingerprint detectors
    SharedFingerprintDetector,
    TimingConfig,
    TimingCorrelationDetector,
};

// ============================================================================
// Mitigation Rate Limiter (Security: Prevent mass-ban DoS)
// ============================================================================

/// Rate limiter for auto-mitigation to prevent mass-banning attacks.
///
/// Limits the number of IPs that can be banned per time window.
/// If an attacker generates many apparent campaigns, this prevents
/// legitimate users from being incorrectly blocked en masse.
#[derive(Debug)]
pub struct MitigationRateLimiter {
    /// Number of bans in current window.
    bans_in_window: AtomicU64,
    /// Window start time.
    window_start: Mutex<Instant>,
    /// Maximum bans allowed per window.
    max_bans_per_window: u64,
    /// Window duration.
    window_duration: Duration,
    /// Maximum IPs to ban per campaign.
    max_ips_per_campaign: usize,
}

impl MitigationRateLimiter {
    /// Creates a new rate limiter.
    pub fn new(
        max_bans_per_window: u64,
        window_duration: Duration,
        max_ips_per_campaign: usize,
    ) -> Self {
        Self {
            bans_in_window: AtomicU64::new(0),
            window_start: Mutex::new(Instant::now()),
            max_bans_per_window,
            window_duration,
            max_ips_per_campaign,
        }
    }

    /// Attempts to acquire a ban permit.
    ///
    /// Returns Ok(()) if the ban is allowed, Err with reason if rate limited.
    pub async fn try_ban(&self) -> Result<(), String> {
        self.maybe_reset_window().await;

        let current = self.bans_in_window.fetch_add(1, Ordering::SeqCst);
        if current >= self.max_bans_per_window {
            self.bans_in_window.fetch_sub(1, Ordering::SeqCst);
            return Err(format!(
                "Rate limit exceeded: {} bans in {:?} window",
                self.max_bans_per_window, self.window_duration
            ));
        }
        Ok(())
    }

    /// Resets the window if it has expired.
    async fn maybe_reset_window(&self) {
        let mut start = self.window_start.lock().await;

        // Double-check expiration under the lock to prevent multiple resets
        if start.elapsed() >= self.window_duration {
            *start = Instant::now();
            self.bans_in_window.store(0, Ordering::SeqCst);
        }
    }

    /// Returns the maximum IPs that can be banned per campaign.
    pub fn max_ips_per_campaign(&self) -> usize {
        self.max_ips_per_campaign
    }

    /// Returns current ban count in window.
    pub fn current_count(&self) -> u64 {
        self.bans_in_window.load(Ordering::SeqCst)
    }
}

impl Default for MitigationRateLimiter {
    fn default() -> Self {
        Self::new(
            50,                      // Max 50 bans per window
            Duration::from_secs(60), // 1 minute window
            10,                      // Max 10 IPs per campaign
        )
    }
}

// ============================================================================
// Configuration
// ============================================================================

/// Configuration for the campaign manager.
///
/// Controls detector thresholds, timing windows, and background scanning behavior.
#[derive(Debug, Clone)]
pub struct ManagerConfig {
    /// Minimum IPs sharing fingerprint to form campaign (shared FP detector).
    ///
    /// Default: 3
    pub shared_threshold: usize,

    /// Time window for rotation detection.
    ///
    /// Default: 60 seconds
    pub rotation_window: Duration,

    /// Minimum fingerprints for rotation detection.
    ///
    /// Default: 3
    pub rotation_threshold: usize,

    /// How often to run full detector scans.
    ///
    /// Default: 5 seconds
    pub scan_interval: Duration,

    /// Enable background scanning.
    ///
    /// When enabled, a background worker periodically runs detection cycles.
    /// Default: true
    pub background_scanning: bool,

    /// Track combined fingerprints (JA4+JA4H) in rotation detector.
    ///
    /// Default: true
    pub track_combined: bool,

    /// Base confidence for shared fingerprint detections.
    ///
    /// Default: 0.85
    pub shared_confidence: f64,

    // ========================================================================
    // Attack Sequence Detector Configuration (weight: 50)
    // ========================================================================
    /// Minimum IPs sharing same payload to trigger detection.
    ///
    /// Default: 2
    pub attack_sequence_min_ips: usize,

    /// Time window for attack sequence correlation.
    ///
    /// Default: 300 seconds (5 minutes)
    pub attack_sequence_window: Duration,

    // ========================================================================
    // Auth Token Detector Configuration (weight: 45)
    // ========================================================================
    /// Minimum IPs sharing token structure to trigger detection.
    ///
    /// Default: 2
    pub auth_token_min_ips: usize,

    /// Time window for auth token correlation.
    ///
    /// Default: 600 seconds (10 minutes)
    pub auth_token_window: Duration,

    // ========================================================================
    // Behavioral Similarity Detector Configuration (weight: 30)
    // ========================================================================
    /// Minimum IPs with same behavior pattern.
    ///
    /// Default: 2
    pub behavioral_min_ips: usize,

    /// Minimum sequence length to consider for behavioral analysis.
    ///
    /// Default: 3
    pub behavioral_min_sequence: usize,

    /// Time window for behavioral pattern observation.
    ///
    /// Default: 300 seconds (5 minutes)
    pub behavioral_window: Duration,

    // ========================================================================
    // Timing Correlation Detector Configuration (weight: 25)
    // ========================================================================
    /// Minimum IPs with synchronized timing.
    ///
    /// Default: 3
    pub timing_min_ips: usize,

    /// Time bucket size for synchronization detection in milliseconds.
    ///
    /// Default: 100ms
    pub timing_bucket_ms: u64,

    /// Minimum requests in same bucket to consider correlated.
    ///
    /// Default: 5
    pub timing_min_bucket_hits: usize,

    /// Time window for timing analysis.
    ///
    /// Default: 60 seconds
    pub timing_window: Duration,

    // ========================================================================
    // Network Proximity Detector Configuration (weight: 15)
    // ========================================================================
    /// Minimum IPs in same network segment.
    ///
    /// Default: 3
    pub network_min_ips: usize,

    /// Enable subnet-based correlation (/24 for IPv4).
    ///
    /// Default: true
    pub network_check_subnet: bool,

    // ========================================================================
    // Graph Correlation Detector Configuration (weight: 20)
    // ========================================================================
    /// Minimum connected component size.
    ///
    /// Default: 3
    pub graph_min_component_size: usize,

    /// Maximum traversal depth.
    ///
    /// Default: 3
    pub graph_max_depth: usize,

    /// Edge TTL.
    ///
    /// Default: 3600 seconds
    pub graph_edge_ttl: Duration,

    // ========================================================================
    // Automated Response Configuration
    // ========================================================================
    /// Enable automated mitigation (blocking) of high-confidence campaigns.
    ///
    /// Default: false
    pub auto_mitigation_enabled: bool,

    /// Confidence threshold for automated mitigation (0.0 - 1.0).
    ///
    /// Default: 0.90
    pub auto_mitigation_threshold: f64,
}

impl Default for ManagerConfig {
    fn default() -> Self {
        Self {
            shared_threshold: 3,
            rotation_window: Duration::from_secs(60),
            rotation_threshold: 3,
            scan_interval: Duration::from_secs(5),
            background_scanning: true,
            track_combined: true,
            shared_confidence: 0.85,
            // Attack sequence detector (weight: 50)
            attack_sequence_min_ips: 2,
            attack_sequence_window: Duration::from_secs(300),
            // Auth token detector (weight: 45)
            auth_token_min_ips: 2,
            auth_token_window: Duration::from_secs(600),
            // Behavioral similarity detector (weight: 30)
            behavioral_min_ips: 2,
            behavioral_min_sequence: 3,
            behavioral_window: Duration::from_secs(300),
            // Timing correlation detector (weight: 25)
            timing_min_ips: 3,
            timing_bucket_ms: 100,
            timing_min_bucket_hits: 5,
            timing_window: Duration::from_secs(60),
            // Network proximity detector (weight: 15)
            network_min_ips: 3,
            network_check_subnet: true,
            // Graph correlation detector (weight: 20)
            graph_min_component_size: 3,
            graph_max_depth: 3,
            graph_edge_ttl: Duration::from_secs(3600),
            // Automated Response
            auto_mitigation_enabled: false,
            auto_mitigation_threshold: 0.90,
        }
    }
}

impl ManagerConfig {
    /// Create a new configuration with default values.
    pub fn new() -> Self {
        Self::default()
    }

    /// Builder method to set shared threshold.
    pub fn with_shared_threshold(mut self, threshold: usize) -> Self {
        self.shared_threshold = threshold;
        self
    }

    /// Builder method to set rotation window.
    pub fn with_rotation_window(mut self, window: Duration) -> Self {
        self.rotation_window = window;
        self
    }

    /// Builder method to set rotation threshold.
    pub fn with_rotation_threshold(mut self, threshold: usize) -> Self {
        self.rotation_threshold = threshold;
        self
    }

    /// Builder method to set scan interval.
    pub fn with_scan_interval(mut self, interval: Duration) -> Self {
        self.scan_interval = interval;
        self
    }

    /// Builder method to enable/disable background scanning.
    pub fn with_background_scanning(mut self, enabled: bool) -> Self {
        self.background_scanning = enabled;
        self
    }

    /// Builder method to enable/disable combined fingerprint tracking.
    pub fn with_track_combined(mut self, enabled: bool) -> Self {
        self.track_combined = enabled;
        self
    }

    /// Builder method to set shared confidence.
    pub fn with_shared_confidence(mut self, confidence: f64) -> Self {
        self.shared_confidence = confidence.clamp(0.0, 1.0);
        self
    }

    /// Builder method to enable/disable automated mitigation.
    pub fn with_auto_mitigation(mut self, enabled: bool) -> Self {
        self.auto_mitigation_enabled = enabled;
        self
    }

    /// Builder method to set automated mitigation threshold.
    pub fn with_auto_mitigation_threshold(mut self, threshold: f64) -> Self {
        self.auto_mitigation_threshold = threshold.clamp(0.0, 1.0);
        self
    }

    /// Validate the configuration.
    ///
    /// Returns an error message if configuration is invalid.
    pub fn validate(&self) -> Result<(), String> {
        if self.shared_threshold < 2 {
            return Err("shared_threshold must be at least 2".to_string());
        }
        if self.rotation_threshold < 2 {
            return Err("rotation_threshold must be at least 2".to_string());
        }
        if self.rotation_window.is_zero() {
            return Err("rotation_window must be positive".to_string());
        }
        if self.scan_interval.is_zero() {
            return Err("scan_interval must be positive".to_string());
        }
        // Security: Auto-mitigation threshold must be high to prevent false positives
        if self.auto_mitigation_enabled && self.auto_mitigation_threshold < 0.7 {
            return Err(
                "auto_mitigation_threshold must be >= 0.7 when auto_mitigation is enabled to prevent false positives"
                    .to_string(),
            );
        }
        // Security: Graph bounds must be reasonable
        if self.graph_min_component_size < 2 {
            return Err("graph_min_component_size must be at least 2".to_string());
        }
        Ok(())
    }
}

// ============================================================================
// Statistics
// ============================================================================

/// Statistics for the campaign manager.
///
/// Provides observability into manager operations including registration counts,
/// detection cycles, and campaign creation.
#[derive(Debug, Clone, Default)]
pub struct ManagerStats {
    /// Total fingerprints registered since start.
    pub fingerprints_registered: u64,

    /// Total detection cycles run.
    pub detections_run: u64,

    /// Total campaigns created.
    pub campaigns_created: u64,

    /// Last successful scan timestamp.
    pub last_scan: Option<Instant>,

    /// Statistics from the fingerprint index.
    pub index_stats: IndexStats,

    /// Statistics from the campaign store.
    pub campaign_stats: CampaignStoreStats,

    /// Detections count by detector type.
    pub detections_by_type: std::collections::HashMap<String, u64>,
}

// ============================================================================
// Fingerprint Group Cache
// ============================================================================

/// TTL for cached fingerprint groups (100ms).
const GROUP_CACHE_TTL: Duration = Duration::from_millis(100);

/// Cache for fingerprint groups to avoid repeated expensive scans.
///
/// The `get_groups_above_threshold()` method on FingerprintIndex is O(n) and
/// can be called by multiple detectors during a single detection cycle. This
/// cache provides a short-lived (100ms) cached result to amortize the cost.
struct GroupCache {
    /// Cached fingerprint groups.
    groups: Vec<FingerprintGroup>,
    /// Timestamp when the cache was populated.
    cached_at: Instant,
    /// The threshold that was used to generate this cache.
    threshold: usize,
}

impl GroupCache {
    /// Create a new cache entry.
    fn new(groups: Vec<FingerprintGroup>, threshold: usize) -> Self {
        Self {
            groups,
            cached_at: Instant::now(),
            threshold,
        }
    }

    /// Check if the cache is still valid.
    fn is_valid(&self, threshold: usize) -> bool {
        self.threshold == threshold && self.cached_at.elapsed() < GROUP_CACHE_TTL
    }
}

// ============================================================================
// Campaign Manager
// ============================================================================

/// Main orchestrator for campaign correlation.
///
/// Coordinates fingerprint indexing, campaign detection, and state management.
/// This is the entry point for the correlation subsystem.
///
/// # Thread Safety
///
/// All methods are thread-safe and can be called concurrently. The manager uses
/// lock-free data structures (DashMap) and atomic counters for high-performance
/// concurrent access.
///
/// # Registration vs Detection
///
/// - **Registration** (`register_*` methods): Called per-request, must be FAST.
///   Only updates indexes, no detection logic.
/// - **Detection** (`run_detection_cycle`): Called periodically by background
///   worker or on-demand. Processes all detectors and applies campaign updates.
///
/// # Detectors (ordered by weight)
///
/// 1. Attack Sequence (50) - Same attack payloads across actors
/// 2. Auth Token (45) - Same JWT structure/issuer across IPs
/// 3. HTTP Fingerprint (40) - Identical browser fingerprint (JA4H)
/// 4. TLS Fingerprint (35) - Same TLS signature (JA4)
/// 5. Behavioral Similarity (30) - Identical navigation/timing patterns
/// 6. Timing Correlation (25) - Coordinated request timing (botnets)
/// 7. Network Proximity (15) - Same ASN or /24 subnet
pub struct CampaignManager {
    /// Manager configuration.
    config: ManagerConfig,

    /// Fingerprint index for O(1) lookups.
    index: Arc<FingerprintIndex>,

    /// Campaign state storage.
    store: Arc<CampaignStore>,

    /// Access List Manager for automated mitigation (optional).
    access_list_manager: Option<Arc<ParkingLotRwLock<AccessListManager>>>,

    /// Telemetry client for cross-tenant correlation (optional).
    telemetry_client: Option<Arc<TelemetryClient>>,

    // ========================================================================
    // All 7 Detectors (ordered by weight)
    // ========================================================================
    /// Attack sequence detector (weight: 50 - highest signal).
    attack_sequence_detector: AttackSequenceDetector,

    /// Auth token detector (weight: 45).
    auth_token_detector: AuthTokenDetector,

    /// HTTP fingerprint detector (weight: 40).
    http_fingerprint_detector: SharedFingerprintDetector,

    /// TLS fingerprint / JA4 rotation detector (weight: 35).
    tls_fingerprint_detector: Ja4RotationDetector,

    /// Behavioral similarity detector (weight: 30).
    behavioral_detector: BehavioralSimilarityDetector,

    /// Timing correlation detector (weight: 25).
    timing_detector: TimingCorrelationDetector,

    /// Network proximity detector (weight: 15 - lowest signal).
    network_detector: NetworkProximityDetector,

    /// Graph correlation detector (weight: 20).
    graph_detector: GraphDetector,

    /// Internal statistics (atomic counters for thread safety).
    stats_fingerprints_registered: AtomicU64,
    stats_detections_run: AtomicU64,
    stats_campaigns_created: AtomicU64,

    /// Per-detector detection counts.
    stats_detections_by_type: RwLock<std::collections::HashMap<String, u64>>,

    /// Last scan timestamp (protected by RwLock for safe concurrent access).
    last_scan: RwLock<Option<Instant>>,

    /// Flag to signal background worker shutdown.
    shutdown: AtomicBool,

    /// Cache for fingerprint groups (100ms TTL).
    /// Reduces repeated expensive scans during detection cycles.
    group_cache: RwLock<Option<GroupCache>>,

    /// Rate limiter for auto-mitigation to prevent mass-banning.
    mitigation_rate_limiter: MitigationRateLimiter,

    /// Track mitigated campaigns to prevent re-mitigation.
    mitigated_campaigns: dashmap::DashSet<String>,
}

impl CampaignManager {
    /// Create a new campaign manager with default configuration.
    pub fn new() -> Self {
        Self::with_config(ManagerConfig::default())
    }

    /// Create a new campaign manager with custom configuration.
    pub fn with_config(config: ManagerConfig) -> Self {
        // ====================================================================
        // Initialize all 7 detectors from config
        // ====================================================================

        // 1. Attack Sequence Detector (weight: 50)
        let attack_sequence_config = AttackSequenceConfig {
            min_ips: config.attack_sequence_min_ips,
            window: config.attack_sequence_window,
            similarity_threshold: 0.95, // Default similarity threshold
            ..Default::default()
        };
        let attack_sequence_detector = AttackSequenceDetector::new(attack_sequence_config);

        // 2. Auth Token Detector (weight: 45)
        let auth_token_config = AuthTokenConfig {
            min_ips: config.auth_token_min_ips,
            window: config.auth_token_window,
            ..Default::default()
        };
        let auth_token_detector = AuthTokenDetector::new(auth_token_config);

        // 3. HTTP Fingerprint Detector (weight: 40)
        let http_fingerprint_detector = SharedFingerprintDetector::with_config(
            config.shared_threshold,
            config.shared_confidence,
            config.scan_interval.as_millis() as u64,
        );

        // 4. TLS Fingerprint / JA4 Rotation Detector (weight: 35)
        let rotation_config = RotationConfig {
            min_fingerprints: config.rotation_threshold,
            window: config.rotation_window,
            track_combined: config.track_combined,
            ..Default::default()
        };
        let tls_fingerprint_detector = Ja4RotationDetector::new(rotation_config);

        // 5. Behavioral Similarity Detector (weight: 30)
        let behavioral_config = BehavioralConfig {
            min_ips: config.behavioral_min_ips,
            min_sequence_length: config.behavioral_min_sequence,
            window: config.behavioral_window,
            ..Default::default()
        };
        let behavioral_detector = BehavioralSimilarityDetector::new(behavioral_config);

        // 6. Timing Correlation Detector (weight: 25)
        let timing_config = TimingConfig {
            min_ips: config.timing_min_ips,
            bucket_size: Duration::from_millis(config.timing_bucket_ms),
            min_bucket_hits: config.timing_min_bucket_hits,
            window: config.timing_window,
            ..Default::default()
        };
        let timing_detector = TimingCorrelationDetector::new(timing_config);

        // 7. Network Proximity Detector (weight: 15)
        let network_config = NetworkProximityConfig {
            min_ips: config.network_min_ips,
            check_subnet: config.network_check_subnet,
            check_asn: false, // ASN lookup requires external data
            ..Default::default()
        };
        let network_detector = NetworkProximityDetector::new(network_config);

        // 8. Graph Correlation Detector (weight: 20)
        let graph_config = GraphConfig {
            min_component_size: config.graph_min_component_size,
            max_traversal_depth: config.graph_max_depth,
            edge_ttl: config.graph_edge_ttl,
            ..Default::default()
        };
        let graph_detector = GraphDetector::new(graph_config);

        Self {
            config,
            index: Arc::new(FingerprintIndex::new()),
            store: Arc::new(CampaignStore::new()),
            access_list_manager: None,
            telemetry_client: None,
            // All 7 detectors
            attack_sequence_detector,
            auth_token_detector,
            http_fingerprint_detector,
            tls_fingerprint_detector,
            behavioral_detector,
            timing_detector,
            network_detector,
            graph_detector,
            // Statistics
            stats_fingerprints_registered: AtomicU64::new(0),
            stats_detections_run: AtomicU64::new(0),
            stats_campaigns_created: AtomicU64::new(0),
            stats_detections_by_type: RwLock::new(std::collections::HashMap::new()),
            last_scan: RwLock::new(None),
            shutdown: AtomicBool::new(false),
            // Cache for fingerprint groups (starts empty)
            group_cache: RwLock::new(None),
            // Mitigation rate limiter and tracking
            mitigation_rate_limiter: MitigationRateLimiter::default(),
            mitigated_campaigns: dashmap::DashSet::new(),
        }
    }

    /// Set the AccessListManager for automated mitigation.
    pub fn set_access_list_manager(&mut self, manager: Arc<ParkingLotRwLock<AccessListManager>>) {
        self.access_list_manager = Some(manager);
    }

    /// Set the TelemetryClient for cross-tenant correlation.
    pub fn set_telemetry_client(&mut self, client: Arc<TelemetryClient>) {
        self.telemetry_client = Some(client);
    }

    /// Register a JA4 fingerprint for an IP address.
    ///
    /// Called during request processing - must be fast.
    /// Only updates indexes, no detection logic is run.
    ///
    /// # Arguments
    /// * `ip` - The IP address of the client
    /// * `fingerprint` - The JA4 TLS fingerprint
    pub fn register_ja4(&self, ip: IpAddr, fingerprint: String) {
        if fingerprint.is_empty() {
            return;
        }

        let ip_str = ip.to_string();

        // Update fingerprint index
        self.index.update_entity(&ip_str, Some(&fingerprint), None);

        // Record in rotation detector
        self.tls_fingerprint_detector
            .record_fingerprint(ip, fingerprint);

        // Increment stats
        self.stats_fingerprints_registered
            .fetch_add(1, Ordering::Relaxed);
    }

    /// Register a JA4 fingerprint using Arc<str> to reduce allocations.
    ///
    /// Optimized version for callers who already have an Arc<str> fingerprint.
    /// This avoids cloning the fingerprint string when it's already reference-counted.
    ///
    /// # Arguments
    /// * `ip` - The IP address of the client
    /// * `fingerprint` - The JA4 TLS fingerprint as Arc<str>
    pub fn register_ja4_arc(&self, ip: IpAddr, fingerprint: Arc<str>) {
        if fingerprint.is_empty() {
            return;
        }

        let ip_str = ip.to_string();

        // Update fingerprint index (uses &str reference, no allocation needed)
        self.index.update_entity(&ip_str, Some(&fingerprint), None);

        // Record in rotation detector (requires String, but Arc<str> → String is cheap clone)
        self.tls_fingerprint_detector
            .record_fingerprint(ip, fingerprint.to_string());

        // Increment stats
        self.stats_fingerprints_registered
            .fetch_add(1, Ordering::Relaxed);
    }

    /// Register a combined (JA4+JA4H) fingerprint for an IP address.
    ///
    /// Combined fingerprints provide higher confidence correlation due to
    /// increased specificity.
    ///
    /// # Arguments
    /// * `ip` - The IP address of the client
    /// * `fingerprint` - The combined fingerprint hash
    pub fn register_combined(&self, ip: IpAddr, fingerprint: String) {
        if fingerprint.is_empty() {
            return;
        }

        let ip_str = ip.to_string();

        // Update fingerprint index (combined only)
        self.index.update_entity(&ip_str, None, Some(&fingerprint));

        // Record in rotation detector if tracking combined
        if self.config.track_combined {
            self.tls_fingerprint_detector
                .record_fingerprint(ip, fingerprint);
        }

        // Increment stats
        self.stats_fingerprints_registered
            .fetch_add(1, Ordering::Relaxed);
    }

    /// Register a combined fingerprint using Arc<str> to reduce allocations.
    ///
    /// Optimized version for callers who already have an Arc<str> fingerprint.
    /// This avoids cloning the fingerprint string when it's already reference-counted.
    ///
    /// # Arguments
    /// * `ip` - The IP address of the client
    /// * `fingerprint` - The combined fingerprint hash as Arc<str>
    pub fn register_combined_arc(&self, ip: IpAddr, fingerprint: Arc<str>) {
        if fingerprint.is_empty() {
            return;
        }

        let ip_str = ip.to_string();

        // Update fingerprint index (combined only, uses &str reference)
        self.index.update_entity(&ip_str, None, Some(&fingerprint));

        // Record in rotation detector if tracking combined
        if self.config.track_combined {
            self.tls_fingerprint_detector
                .record_fingerprint(ip, fingerprint.to_string());
        }

        // Increment stats
        self.stats_fingerprints_registered
            .fetch_add(1, Ordering::Relaxed);
    }

    /// Register both JA4 and JA4H fingerprints.
    ///
    /// Convenience method for registering both fingerprint types in one call.
    ///
    /// # Arguments
    /// * `ip` - The IP address of the client
    /// * `ja4` - Optional JA4 TLS fingerprint
    /// * `ja4h` - Optional JA4H HTTP fingerprint (used in combined hash)
    pub fn register_fingerprints(&self, ip: IpAddr, ja4: Option<String>, ja4h: Option<String>) {
        let ip_str = ip.to_string();
        let mut registered = false;

        // Update fingerprint index
        let ja4_ref = ja4.as_deref();
        let combined = ja4h.as_ref().map(|h| {
            // Create combined hash from JA4+JA4H
            format!("{}_{}", ja4.as_deref().unwrap_or(""), h)
        });
        let combined_ref = combined.as_deref();

        self.index.update_entity(&ip_str, ja4_ref, combined_ref);

        // Record JA4 in rotation detector
        if let Some(ref fp) = ja4 {
            if !fp.is_empty() {
                self.tls_fingerprint_detector
                    .record_fingerprint(ip, fp.clone());
                registered = true;
            }
        }

        // Record combined in rotation detector if tracking
        if self.config.track_combined {
            if let Some(ref fp) = combined {
                if !fp.is_empty() {
                    self.tls_fingerprint_detector
                        .record_fingerprint(ip, fp.clone());
                    registered = true;
                }
            }
        }

        if registered {
            self.stats_fingerprints_registered
                .fetch_add(1, Ordering::Relaxed);
        }
    }

    // ========================================================================
    // New Detector Registration Methods
    // ========================================================================

    /// Record an attack payload observation for campaign correlation.
    ///
    /// Called when an attack is detected (SQLi, XSS, etc.) to correlate
    /// identical payloads across different IPs. Weight: 50 (highest signal).
    ///
    /// # Arguments
    /// * `ip` - The IP address of the attacker
    /// * `payload_hash` - Hash of the normalized attack payload
    /// * `attack_type` - Classification (sqli, xss, path_traversal, etc.)
    /// * `path` - Target path of the attack
    pub fn record_attack(
        &self,
        ip: IpAddr,
        payload_hash: String,
        attack_type: String,
        path: String,
    ) {
        self.attack_sequence_detector.record_attack(
            ip,
            AttackPayload {
                payload_hash,
                attack_type,
                target_path: path,
                timestamp: std::time::Instant::now(),
            },
        );
    }

    /// Record a JWT token observation for campaign correlation.
    ///
    /// Called when a JWT is seen in request headers. Correlates IPs
    /// using tokens with identical structure or issuer. Weight: 45.
    ///
    /// # Arguments
    /// * `ip` - The IP address of the client
    /// * `jwt` - The raw JWT string (header.payload.signature)
    pub fn record_token(&self, ip: IpAddr, jwt: &str) {
        self.auth_token_detector.record_jwt(ip, jwt);
    }

    /// Record a request for behavioral and timing analysis.
    ///
    /// Should be called for every request to build behavioral patterns
    /// and detect timing correlations. Updates multiple detectors:
    /// - Behavioral detector (weight: 30) - navigation patterns
    /// - Timing detector (weight: 25) - request synchronization
    /// - Network detector (weight: 15) - subnet correlation
    ///
    /// # Arguments
    /// * `ip` - The IP address of the client
    /// * `method` - HTTP method (GET, POST, etc.)
    /// * `path` - Request path
    pub fn record_request(&self, ip: IpAddr, method: &str, path: &str) {
        self.behavioral_detector.record_request(ip, method, path);
        self.timing_detector.record_request(ip);
        self.network_detector.register_ip(ip);
    }

    /// Record a request with full context for all applicable detectors.
    ///
    /// Convenience method that records data to multiple detectors at once.
    /// Call this during request processing to capture all correlation signals.
    ///
    /// # Arguments
    /// * `ip` - The IP address of the client
    /// * `method` - HTTP method
    /// * `path` - Request path
    /// * `ja4` - Optional JA4 TLS fingerprint
    /// * `jwt` - Optional JWT from Authorization header
    pub fn record_request_full(
        &self,
        ip: IpAddr,
        method: &str,
        path: &str,
        ja4: Option<&str>,
        jwt: Option<&str>,
    ) {
        // Record for behavioral/timing/network
        self.record_request(ip, method, path);

        let ip_id = GraphDetector::ip_id(&ip.to_string());

        // Record JA4 fingerprint
        if let Some(fp) = ja4 {
            if !fp.is_empty() {
                self.register_ja4(ip, fp.to_string());
                // Graph correlation: Link IP to Fingerprint
                self.record_relation(&ip_id, &GraphDetector::fp_id(fp));
            }
        }

        // Record JWT token
        if let Some(token) = jwt {
            if !token.is_empty() {
                self.record_token(ip, token);
                // Graph correlation: Link IP to Token (use hash or prefix for ID)
                // Using first 16 chars of token as ID to avoid sensitive data in graph keys
                let token_id = if token.len() > 16 {
                    &token[..16]
                } else {
                    token
                };
                self.record_relation(&ip_id, &GraphDetector::token_id(token_id));
            }
        }
    }

    /// Record a relationship for graph correlation.
    ///
    /// Records a connection between two entities (e.g., IP and Fingerprint)
    /// to build the correlation graph.
    ///
    /// # Arguments
    /// * `entity_a` - First entity ID (e.g., "ip:1.2.3.4")
    /// * `entity_b` - Second entity ID (e.g., "fp:abc12345")
    pub fn record_relation(&self, entity_a: &str, entity_b: &str) {
        self.graph_detector.record_relation(entity_a, entity_b);
    }

    // ========================================================================
    // Campaign Scoring
    // ========================================================================

    /// Calculate weighted campaign score from all correlation reasons.
    ///
    /// The score is computed as the weighted average of all correlation
    /// reasons, where each reason's contribution is:
    /// `weight * confidence / total_reasons`
    ///
    /// # Arguments
    /// * `campaign` - The campaign to score
    ///
    /// # Returns
    /// A score between 0.0 and 50.0 (max weight * max confidence)
    pub fn calculate_campaign_score(&self, campaign: &Campaign) -> f64 {
        if campaign.correlation_reasons.is_empty() {
            return 0.0;
        }

        let total_weighted: f64 = campaign
            .correlation_reasons
            .iter()
            .map(|r| r.correlation_type.weight() as f64 * r.confidence)
            .sum();

        total_weighted / campaign.correlation_reasons.len() as f64
    }

    /// Run all 7 detectors in parallel and process updates with weighted scoring.
    ///
    /// Called periodically by background worker or on-demand.
    /// Detectors run concurrently for improved performance (~70ms savings at scale):
    /// 1. Attack Sequence (50) - Same attack payloads
    /// 2. Auth Token (45) - Same JWT structure/issuer
    /// 3. HTTP Fingerprint (40) - Identical JA4H
    /// 4. TLS Fingerprint (35) - Same JA4
    /// 5. Behavioral Similarity (30) - Navigation patterns
    /// 6. Timing Correlation (25) - Synchronized requests
    /// 7. Network Proximity (15) - Same ASN/subnet
    ///
    /// # Returns
    /// Number of campaign updates processed.
    ///
    /// # Errors
    /// Returns an error if any detector fails critically.
    pub async fn run_detection_cycle(&self) -> DetectorResult<usize> {
        // Create futures for each detector using trait objects for dynamic dispatch
        // This allows heterogeneous detectors to be run in parallel via join_all
        let detectors: Vec<(&dyn Detector, &'static str)> = vec![
            (
                &self.attack_sequence_detector as &dyn Detector,
                "attack_sequence",
            ),
            (&self.auth_token_detector as &dyn Detector, "auth_token"),
            (
                &self.http_fingerprint_detector as &dyn Detector,
                "http_fingerprint",
            ),
            (
                &self.tls_fingerprint_detector as &dyn Detector,
                "tls_fingerprint",
            ),
            (&self.behavioral_detector as &dyn Detector, "behavioral"),
            (&self.timing_detector as &dyn Detector, "timing"),
            (&self.network_detector as &dyn Detector, "network"),
            (&self.graph_detector as &dyn Detector, "graph"),
        ];

        // Run all detectors in parallel using join_all
        // Each future wraps the synchronous analyze() call
        let detector_futures: Vec<_> = detectors
            .into_iter()
            .map(|(detector, name)| {
                let index = &self.index;
                // Wrap each detector in an async block
                async move {
                    let result = detector.analyze(index);
                    (name, result)
                }
            })
            .collect();

        let results = join_all(detector_futures).await;

        // Process all results and collect updates
        let mut total_updates = 0;
        let mut stats_updates: std::collections::HashMap<String, u64> =
            std::collections::HashMap::new();

        for (name, result) in results {
            match result {
                Ok(updates) => {
                    let update_count = updates.len();
                    for update in updates {
                        self.process_campaign_update(update).await;
                        total_updates += 1;
                    }
                    // Collect per-detector stats
                    if update_count > 0 {
                        *stats_updates.entry(name.to_string()).or_insert(0) += update_count as u64;
                    }
                }
                Err(e) => {
                    tracing::warn!("Detector {} failed: {}", name, e);
                }
            }
        }

        // Batch update stats (single lock acquisition)
        if !stats_updates.is_empty() {
            let mut stats = self.stats_detections_by_type.write().await;
            for (name, count) in stats_updates {
                *stats.entry(name).or_insert(0) += count;
            }
        }

        // Update global stats
        self.stats_detections_run.fetch_add(1, Ordering::Relaxed);
        {
            let mut last_scan = self.last_scan.write().await;
            *last_scan = Some(Instant::now());
        }

        Ok(total_updates)
    }

    /// Get fingerprint groups above threshold with caching.
    ///
    /// This method caches the results of `get_groups_above_threshold()` for 100ms
    /// to avoid repeated expensive O(n) scans during a single detection cycle.
    /// Multiple detectors can use the same cached result within the TTL window.
    ///
    /// # Arguments
    /// * `threshold` - Minimum number of IPs required for a group
    ///
    /// # Returns
    /// Vector of fingerprint groups above the threshold.
    pub async fn get_cached_groups(&self, threshold: usize) -> Vec<FingerprintGroup> {
        // Check cache first
        {
            let cache_guard = self.group_cache.read().await;
            if let Some(ref cache) = *cache_guard {
                if cache.is_valid(threshold) {
                    return cache.groups.clone();
                }
            }
        }

        // Cache miss or expired - compute fresh groups
        let groups = self.index.get_groups_above_threshold(threshold);

        // Update cache
        {
            let mut cache_guard = self.group_cache.write().await;
            *cache_guard = Some(GroupCache::new(groups.clone(), threshold));
        }

        groups
    }

    /// Invalidate the fingerprint groups cache.
    ///
    /// Called when significant changes occur that would affect group composition.
    pub async fn invalidate_group_cache(&self) {
        let mut cache_guard = self.group_cache.write().await;
        *cache_guard = None;
    }

    /// Process a campaign update from a detector.
    ///
    /// If the update contains a correlation reason with IPs, we try to:
    /// 1. Find existing campaign for any of those IPs
    /// 2. If found, update the existing campaign
    /// 3. If not found, create a new campaign
    async fn process_campaign_update(&self, update: CampaignUpdate) {
        // Extract IPs from correlation reason if present
        let ips: Vec<String> = update
            .add_correlation_reason
            .as_ref()
            .map(|reason| reason.evidence.clone())
            .unwrap_or_default();

        if ips.is_empty() {
            return;
        }

        // Check if any IP is already in a campaign
        let existing_campaign_id = ips.iter().find_map(|ip| self.store.get_campaign_for_ip(ip));

        // Use a variable to track if we need to check for mitigation
        let mut check_mitigation = false;
        let mut target_campaign_id = String::new();

        match existing_campaign_id {
            Some(campaign_id) => {
                // Update existing campaign
                let _ = self.store.update_campaign(&campaign_id, update);

                // Add any new IPs to the campaign
                for ip in &ips {
                    let _ = self.store.add_actor_to_campaign(&campaign_id, ip);
                }

                check_mitigation = true;
                target_campaign_id = campaign_id;
            }
            None => {
                // Create new campaign
                let confidence = update.confidence.unwrap_or(0.5);

                // Generate a unique ID, retrying if collision occurs (rare edge case)
                // ID collisions can happen if two campaigns are created in the same millisecond
                let mut campaign_id = Campaign::generate_id();
                let mut retry_count = 0;
                while self.store.get_campaign(&campaign_id).is_some() && retry_count < 10 {
                    // Add random suffix to handle collision
                    campaign_id = format!("{}-{:x}", Campaign::generate_id(), fastrand::u32(..));
                    retry_count += 1;
                }

                let mut campaign = Campaign::new(campaign_id.clone(), ips, confidence);

                // Apply update fields to new campaign
                if let Some(status) = update.status {
                    campaign.status = status;
                }
                if let Some(ref attack_types) = update.attack_types {
                    campaign.attack_types = attack_types.clone();
                }
                if let Some(reason) = update.add_correlation_reason {
                    campaign.correlation_reasons.push(reason);
                }
                if let Some(risk_score) = update.risk_score {
                    campaign.risk_score = risk_score;
                }

                // Store the campaign
                if self.store.create_campaign(campaign).is_ok() {
                    self.stats_campaigns_created.fetch_add(1, Ordering::Relaxed);
                    check_mitigation = true;
                    target_campaign_id = campaign_id;
                }
            }
        }

        // Check for automated mitigation if enabled
        if check_mitigation {
            if let Some(campaign) = self.store.get_campaign(&target_campaign_id) {
                // Auto-mitigation (Block)
                if self.config.auto_mitigation_enabled
                    && campaign.confidence >= self.config.auto_mitigation_threshold
                    && campaign.status != CampaignStatus::Resolved
                {
                    self.mitigate_campaign(&campaign).await;
                }

                // Cross-Tenant Reporting (Fleet Intelligence)
                // Report high-confidence campaigns (>= 0.8) to Signal Horizon
                if campaign.confidence >= 0.8 {
                    self.report_campaign(&campaign);
                }
            }
        }
    }

    /// Report a high-confidence campaign to Signal Horizon telemetry.
    fn report_campaign(&self, campaign: &Campaign) {
        if let Some(ref client) = self.telemetry_client {
            // Only report if client is enabled
            if !client.is_enabled() {
                return;
            }

            let event = TelemetryEvent::CampaignReport {
                campaign_id: campaign.id.clone(),
                confidence: campaign.confidence,
                attack_types: campaign
                    .attack_types
                    .iter()
                    .map(|at| format!("{:?}", at))
                    .collect(),
                actor_count: campaign.actor_count,
                correlation_reasons: campaign
                    .correlation_reasons
                    .iter()
                    .map(|r| r.description.clone())
                    .collect(),
                timestamp_ms: std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap_or_default()
                    .as_millis() as u64,
            };

            // Fire and forget - runs in background
            let client = Arc::clone(client);
            tokio::spawn(async move {
                if let Err(e) = client.report(event).await {
                    tracing::debug!("Failed to report campaign telemetry: {}", e);
                }
            });
        }
    }

    /// Apply automated mitigation to a high-confidence campaign.
    ///
    /// Adds campaign actors to the deny list via AccessListManager.
    /// Implements rate limiting and batch blocking for safety.
    async fn mitigate_campaign(&self, campaign: &Campaign) {
        // Check if already mitigated
        if self.mitigated_campaigns.contains(&campaign.id) {
            tracing::debug!(campaign_id = %campaign.id, "Campaign already mitigated, skipping");
            return;
        }

        let access_list = match &self.access_list_manager {
            Some(al) => al,
            None => {
                tracing::debug!("No AccessListManager configured, skipping mitigation");
                return;
            }
        };

        // Collect IPs to block (limit per campaign)
        let max_ips = self.mitigation_rate_limiter.max_ips_per_campaign();
        let ips_to_block: Vec<IpAddr> = campaign
            .actors
            .iter()
            .filter_map(|ip_str| ip_str.parse::<IpAddr>().ok())
            .take(max_ips)
            .collect();

        if ips_to_block.is_empty() {
            tracing::debug!(campaign_id = %campaign.id, "No valid IPs to block");
            return;
        }

        // Rate limit check - acquire permits for all IPs
        let mut blocked_count = 0;
        let mut rate_limited = false;

        for ip in &ips_to_block {
            if let Err(reason) = self.mitigation_rate_limiter.try_ban().await {
                tracing::warn!(
                    campaign_id = %campaign.id,
                    reason = %reason,
                    blocked = blocked_count,
                    remaining = ips_to_block.len() - blocked_count,
                    "Mitigation rate limited"
                );
                rate_limited = true;
                break;
            }

            // Add deny rule
            let comment = format!(
                "Campaign {} (confidence: {:.2})",
                campaign.id, campaign.confidence
            );
            {
                let mut al = access_list.write();
                if let Err(e) = al.add_deny_ip(ip, Some(&comment)) {
                    tracing::error!(ip = %ip, error = %e, "Failed to add deny rule");
                    continue;
                }
            }
            blocked_count += 1;
        }

        // Log audit event
        let attack_types: Vec<String> = campaign
            .attack_types
            .iter()
            .map(|at| format!("{:?}", at))
            .collect();
        tracing::info!(
            campaign_id = %campaign.id,
            confidence = campaign.confidence,
            total_actors = campaign.actors.len(),
            blocked = blocked_count,
            rate_limited = rate_limited,
            attack_types = ?attack_types,
            "Auto-mitigation applied"
        );

        // Mark as mitigated
        self.mitigated_campaigns.insert(campaign.id.clone());

        // Report mitigation event to telemetry
        if let Some(ref client) = self.telemetry_client {
            if client.is_enabled() {
                let event = TelemetryEvent::CampaignReport {
                    campaign_id: format!("mitigation:{}", campaign.id),
                    confidence: campaign.confidence,
                    attack_types,
                    actor_count: blocked_count,
                    correlation_reasons: vec![format!(
                        "Auto-mitigation applied: {} IPs blocked",
                        blocked_count
                    )],
                    timestamp_ms: std::time::SystemTime::now()
                        .duration_since(std::time::UNIX_EPOCH)
                        .unwrap_or_default()
                        .as_millis() as u64,
                };

                let client = Arc::clone(client);
                tokio::spawn(async move {
                    if let Err(e) = client.report(event).await {
                        tracing::debug!("Failed to report mitigation telemetry: {}", e);
                    }
                });
            }
        }
    }

    /// Check if an IP should trigger immediate detection.
    ///
    /// Used for event-driven detection on new requests. Checks all 7 detectors
    /// to see if any threshold has been reached that warrants immediate analysis.
    ///
    /// # Arguments
    /// * `ip` - The IP address to check
    ///
    /// # Returns
    /// `true` if immediate detection should be triggered.
    pub fn should_trigger_detection(&self, ip: &IpAddr) -> bool {
        // Check detectors in order of weight (short-circuit on first match)
        self.attack_sequence_detector
            .should_trigger(ip, &self.index)
            || self.auth_token_detector.should_trigger(ip, &self.index)
            || self
                .http_fingerprint_detector
                .should_trigger(ip, &self.index)
            || self
                .tls_fingerprint_detector
                .should_trigger(ip, &self.index)
            || self.behavioral_detector.should_trigger(ip, &self.index)
            || self.timing_detector.should_trigger(ip, &self.index)
            || self.network_detector.should_trigger(ip, &self.index)
            || self.graph_detector.should_trigger(ip, &self.index)
    }

    /// Get all active campaigns for API response.
    ///
    /// Returns campaigns with Detected or Active status.
    pub fn get_campaigns(&self) -> Vec<Campaign> {
        self.store.list_active_campaigns()
    }

    /// Get all campaigns (including resolved/dormant).
    pub fn get_all_campaigns(&self) -> Vec<Campaign> {
        self.store.list_campaigns(None)
    }

    /// Create a snapshot of all campaigns for persistence.
    ///
    /// Returns all campaigns regardless of status.
    pub fn snapshot(&self) -> Vec<Campaign> {
        self.store.list_campaigns(None)
    }

    /// Restore campaigns from a snapshot.
    ///
    /// Clears existing state and loads the provided campaigns.
    pub fn restore(&self, campaigns: Vec<Campaign>) {
        // Clear existing state
        self.store.clear();
        self.index.clear();

        // Restore campaigns
        for campaign in campaigns {
            // Re-add IP mappings
            for ip_str in &campaign.actors {
                // Update fingerprint index with a placeholder to re-establish the IP entry
                self.index.update_entity(ip_str, None, None);
            }

            // Create the campaign in the store
            let _ = self.store.create_campaign(campaign);
        }
    }

    /// Get a specific campaign by ID.
    ///
    /// # Arguments
    /// * `id` - The campaign ID to retrieve
    ///
    /// # Returns
    /// The campaign if found, None otherwise.
    pub fn get_campaign(&self, id: &str) -> Option<Campaign> {
        self.store.get_campaign(id)
    }

    /// Get IPs that are members of a campaign.
    ///
    /// # Arguments
    /// * `campaign_id` - The campaign ID to query
    ///
    /// # Returns
    /// Vector of IP addresses in the campaign.
    pub fn get_campaign_actors(&self, campaign_id: &str) -> Vec<IpAddr> {
        self.store
            .get_campaign(campaign_id)
            .map(|campaign| {
                campaign
                    .actors
                    .iter()
                    .filter_map(|ip_str| ip_str.parse().ok())
                    .collect()
            })
            .unwrap_or_default()
    }

    /// Get the correlation graph for a campaign.
    pub fn get_campaign_graph(&self, campaign_id: &str) -> serde_json::Value {
        let ips = self.get_campaign_actors(campaign_id);
        let ips_str: Vec<String> = ips.into_iter().map(|ip| ip.to_string()).collect();

        self.graph_detector.get_cytoscape_data(&ips_str)
    }

    /// Get the correlation graph for a campaign with pagination and identifier hashing.
    /// P1 fix: Supports pagination to prevent memory exhaustion and hashes identifiers
    /// to prevent information disclosure.
    pub fn get_campaign_graph_paginated(
        &self,
        campaign_id: &str,
        limit: Option<usize>,
        offset: Option<usize>,
        hash_identifiers: bool,
    ) -> crate::correlation::detectors::graph::PaginatedGraph {
        use crate::correlation::detectors::graph::GraphExportOptions;

        let ips = self.get_campaign_actors(campaign_id);
        let ips_str: Vec<String> = ips.into_iter().map(|ip| ip.to_string()).collect();

        let options = GraphExportOptions {
            limit,
            offset,
            hash_identifiers,
        };

        self.graph_detector
            .get_cytoscape_data_paginated(&ips_str, options)
    }

    /// Get current statistics.
    ///
    /// Returns a snapshot of manager statistics including index and store stats.
    pub fn stats(&self) -> ManagerStats {
        let last_scan = {
            // Use try_read to avoid blocking; if locked, use None
            self.last_scan
                .try_read()
                .map(|guard| *guard)
                .unwrap_or(None)
        };

        let detections_by_type = self
            .stats_detections_by_type
            .try_read()
            .map(|guard| guard.clone())
            .unwrap_or_default();

        ManagerStats {
            fingerprints_registered: self.stats_fingerprints_registered.load(Ordering::Relaxed),
            detections_run: self.stats_detections_run.load(Ordering::Relaxed),
            campaigns_created: self.stats_campaigns_created.load(Ordering::Relaxed),
            last_scan,
            index_stats: self.index.stats(),
            campaign_stats: self.store.stats(),
            detections_by_type,
        }
    }

    /// Start background detection worker.
    ///
    /// Returns a handle that can be used to await worker completion.
    /// The worker runs detection cycles at the configured interval until
    /// the manager is dropped or shutdown is signaled.
    ///
    /// # Returns
    /// JoinHandle for the background task.
    pub fn start_background_worker(self: Arc<Self>) -> tokio::task::JoinHandle<()> {
        let manager = self;
        let scan_interval = manager.config.scan_interval;

        tokio::spawn(async move {
            let mut ticker = interval(scan_interval);

            loop {
                ticker.tick().await;

                // Check for shutdown signal
                if manager.shutdown.load(Ordering::Relaxed) {
                    log::info!("Campaign manager background worker shutting down");
                    break;
                }

                // Run detection cycle
                match manager.run_detection_cycle().await {
                    Ok(updates) => {
                        if updates > 0 {
                            log::debug!("Detection cycle processed {} updates", updates);
                        }
                    }
                    Err(e) => {
                        log::warn!("Detection cycle error: {}", e);
                    }
                }
            }
        })
    }

    /// Signal the background worker to shut down.
    pub fn shutdown(&self) {
        self.shutdown.store(true, Ordering::Relaxed);
    }

    /// Check if shutdown has been signaled.
    pub fn is_shutdown(&self) -> bool {
        self.shutdown.load(Ordering::Relaxed)
    }

    /// Remove an IP from tracking (called when entity is evicted).
    ///
    /// Cleans up the IP from:
    /// - Fingerprint index
    /// - Any associated campaigns
    ///
    /// # Arguments
    /// * `ip` - The IP address to remove
    pub fn remove_ip(&self, ip: &IpAddr) {
        let ip_str = ip.to_string();

        // Remove from fingerprint index
        self.index.remove_entity(&ip_str);

        // Remove from any campaign
        if let Some(campaign_id) = self.store.get_campaign_for_ip(&ip_str) {
            let _ = self.store.remove_actor_from_campaign(&campaign_id, &ip_str);
        }
    }

    /// Get the fingerprint index (for integration with EntityManager).
    ///
    /// Allows direct access to the index for advanced use cases.
    pub fn index(&self) -> &Arc<FingerprintIndex> {
        &self.index
    }

    /// Get the campaign store (for integration).
    ///
    /// Allows direct access to the store for advanced use cases.
    pub fn store(&self) -> &Arc<CampaignStore> {
        &self.store
    }

    /// Get the current configuration.
    pub fn config(&self) -> &ManagerConfig {
        &self.config
    }

    /// Resolve a campaign.
    ///
    /// # Arguments
    /// * `campaign_id` - The campaign ID to resolve
    /// * `reason` - The reason for resolution
    ///
    /// # Returns
    /// Ok(()) if successful, Err if campaign not found or already resolved.
    pub fn resolve_campaign(&self, campaign_id: &str, reason: &str) -> Result<(), DetectorError> {
        self.store
            .resolve_campaign(campaign_id, reason)
            .map_err(|e| DetectorError::DetectionFailed(e.to_string()))
    }

    /// Clear all state (primarily for testing).
    ///
    /// Clears fingerprint index, campaign store, and detector state.
    pub fn clear(&self) {
        self.index.clear();
        self.store.clear();
        self.http_fingerprint_detector.clear_processed();
        self.tls_fingerprint_detector.cleanup_old_observations();
    }
}

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

// ============================================================================
// Tests
// ============================================================================

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

    // ========================================================================
    // Helper Functions
    // ========================================================================

    fn create_test_manager() -> CampaignManager {
        let config = ManagerConfig {
            shared_threshold: 3,
            rotation_threshold: 3,
            rotation_window: Duration::from_secs(60),
            scan_interval: Duration::from_millis(100),
            background_scanning: false,
            ..Default::default()
        };
        CampaignManager::with_config(config)
    }

    fn create_test_ip(last_octet: u8) -> IpAddr {
        format!("192.168.1.{}", last_octet).parse().unwrap()
    }

    // ========================================================================
    // Configuration Tests
    // ========================================================================

    #[test]
    fn test_config_default() {
        let config = ManagerConfig::default();

        assert_eq!(config.shared_threshold, 3);
        assert_eq!(config.rotation_threshold, 3);
        assert_eq!(config.rotation_window, Duration::from_secs(60));
        assert_eq!(config.scan_interval, Duration::from_secs(5));
        assert!(config.background_scanning);
        assert!(config.track_combined);
        assert!((config.shared_confidence - 0.85).abs() < 0.001);
    }

    #[test]
    fn test_config_builder() {
        let config = ManagerConfig::new()
            .with_shared_threshold(5)
            .with_rotation_threshold(4)
            .with_rotation_window(Duration::from_secs(120))
            .with_scan_interval(Duration::from_secs(10))
            .with_background_scanning(false)
            .with_track_combined(false)
            .with_shared_confidence(0.9);

        assert_eq!(config.shared_threshold, 5);
        assert_eq!(config.rotation_threshold, 4);
        assert_eq!(config.rotation_window, Duration::from_secs(120));
        assert_eq!(config.scan_interval, Duration::from_secs(10));
        assert!(!config.background_scanning);
        assert!(!config.track_combined);
        assert!((config.shared_confidence - 0.9).abs() < 0.001);
    }

    #[tokio::test]
    async fn test_mitigation_rate_limiter_limits() {
        let limiter = MitigationRateLimiter::new(2, Duration::from_secs(60), 10);

        assert!(limiter.try_ban().await.is_ok());
        assert!(limiter.try_ban().await.is_ok());
        assert!(limiter.try_ban().await.is_err());
    }

    #[test]
    fn test_config_validation() {
        // Valid config
        let config = ManagerConfig::default();
        assert!(config.validate().is_ok());

        // Invalid shared_threshold
        let config = ManagerConfig::new().with_shared_threshold(1);
        assert!(config.validate().is_err());

        // Invalid rotation_threshold
        let config = ManagerConfig::new().with_rotation_threshold(1);
        assert!(config.validate().is_err());

        // Invalid rotation_window
        let config = ManagerConfig {
            rotation_window: Duration::ZERO,
            ..Default::default()
        };
        assert!(config.validate().is_err());

        // Invalid scan_interval
        let config = ManagerConfig {
            scan_interval: Duration::ZERO,
            ..Default::default()
        };
        assert!(config.validate().is_err());

        // Auto-mitigation with threshold too low (security risk)
        let config = ManagerConfig {
            auto_mitigation_enabled: true,
            auto_mitigation_threshold: 0.5, // Below 0.7 minimum
            ..Default::default()
        };
        assert!(config.validate().is_err());

        // Auto-mitigation with valid threshold
        let config = ManagerConfig {
            auto_mitigation_enabled: true,
            auto_mitigation_threshold: 0.9,
            ..Default::default()
        };
        assert!(config.validate().is_ok());

        // Auto-mitigation disabled ignores threshold
        let config = ManagerConfig {
            auto_mitigation_enabled: false,
            auto_mitigation_threshold: 0.5, // Would be invalid if enabled
            ..Default::default()
        };
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_config_confidence_clamping() {
        let config = ManagerConfig::new().with_shared_confidence(1.5);
        assert!((config.shared_confidence - 1.0).abs() < 0.001);

        let config = ManagerConfig::new().with_shared_confidence(-0.5);
        assert!(config.shared_confidence >= 0.0);
    }

    // ========================================================================
    // Registration Flow Tests
    // ========================================================================

    #[test]
    fn test_register_ja4() {
        let manager = create_test_manager();
        let ip = create_test_ip(1);

        manager.register_ja4(ip, "t13d1516h2_abc123".to_string());

        let stats = manager.stats();
        assert_eq!(stats.fingerprints_registered, 1);
        assert_eq!(stats.index_stats.total_ips, 1);
        assert_eq!(stats.index_stats.ja4_fingerprints, 1);
    }

    #[test]
    fn test_register_ja4_empty_skipped() {
        let manager = create_test_manager();
        let ip = create_test_ip(1);

        manager.register_ja4(ip, "".to_string());

        let stats = manager.stats();
        assert_eq!(stats.fingerprints_registered, 0);
        assert_eq!(stats.index_stats.total_ips, 0);
    }

    #[test]
    fn test_register_combined() {
        let manager = create_test_manager();
        let ip = create_test_ip(1);

        manager.register_combined(ip, "combined_hash_xyz".to_string());

        let stats = manager.stats();
        assert_eq!(stats.fingerprints_registered, 1);
        assert_eq!(stats.index_stats.total_ips, 1);
        assert_eq!(stats.index_stats.combined_fingerprints, 1);
    }

    #[test]
    fn test_register_fingerprints_both() {
        let manager = create_test_manager();
        let ip = create_test_ip(1);

        manager.register_fingerprints(
            ip,
            Some("ja4_test".to_string()),
            Some("ja4h_test".to_string()),
        );

        let stats = manager.stats();
        assert_eq!(stats.fingerprints_registered, 1);
        assert_eq!(stats.index_stats.ja4_fingerprints, 1);
        assert_eq!(stats.index_stats.combined_fingerprints, 1);
    }

    #[test]
    fn test_register_fingerprints_ja4_only() {
        let manager = create_test_manager();
        let ip = create_test_ip(1);

        manager.register_fingerprints(ip, Some("ja4_only".to_string()), None);

        let stats = manager.stats();
        assert_eq!(stats.fingerprints_registered, 1);
        assert_eq!(stats.index_stats.ja4_fingerprints, 1);
        assert_eq!(stats.index_stats.combined_fingerprints, 0);
    }

    // ========================================================================
    // Detection Cycle Tests
    // ========================================================================

    #[tokio::test]
    async fn test_detection_cycle_empty() {
        let manager = create_test_manager();

        let updates = manager.run_detection_cycle().await.unwrap();

        assert_eq!(updates, 0);
        assert_eq!(manager.stats().detections_run, 1);
    }

    #[tokio::test]
    async fn test_detection_cycle_creates_campaign() {
        let manager = create_test_manager();

        // Register 3 IPs with same fingerprint (threshold)
        for i in 1..=3 {
            let ip = create_test_ip(i);
            manager.register_ja4(ip, "shared_fingerprint".to_string());
        }

        let updates = manager.run_detection_cycle().await.unwrap();

        assert!(updates >= 1);
        assert_eq!(manager.stats().campaigns_created, 1);

        let campaigns = manager.get_campaigns();
        assert_eq!(campaigns.len(), 1);
    }

    #[tokio::test]
    async fn test_detection_cycle_no_duplicate_campaigns() {
        let manager = create_test_manager();

        // Register 3 IPs with same fingerprint
        for i in 1..=3 {
            let ip = create_test_ip(i);
            manager.register_ja4(ip, "shared_fp".to_string());
        }

        // First detection cycle
        manager.run_detection_cycle().await.unwrap();
        let first_count = manager.stats().campaigns_created;

        // Second detection cycle - should not create duplicate
        manager.run_detection_cycle().await.unwrap();
        let second_count = manager.stats().campaigns_created;

        assert_eq!(first_count, second_count);
    }

    // ========================================================================
    // Campaign Retrieval Tests
    // ========================================================================

    #[tokio::test]
    async fn test_get_campaigns() {
        let manager = create_test_manager();

        // Create a campaign
        for i in 1..=3 {
            let ip = create_test_ip(i);
            manager.register_ja4(ip, "test_fp".to_string());
        }
        manager.run_detection_cycle().await.unwrap();

        let campaigns = manager.get_campaigns();
        assert!(!campaigns.is_empty());

        // Verify campaign has the expected actors
        let campaign = &campaigns[0];
        assert_eq!(campaign.actor_count, 3);
    }

    #[tokio::test]
    async fn test_get_campaign_by_id() {
        let manager = create_test_manager();

        // Create a campaign
        for i in 1..=3 {
            let ip = create_test_ip(i);
            manager.register_ja4(ip, "get_by_id_fp".to_string());
        }
        manager.run_detection_cycle().await.unwrap();

        let campaigns = manager.get_campaigns();
        let campaign_id = &campaigns[0].id;

        let retrieved = manager.get_campaign(campaign_id);
        assert!(retrieved.is_some());
        assert_eq!(retrieved.unwrap().id, *campaign_id);

        // Non-existent ID
        let not_found = manager.get_campaign("nonexistent");
        assert!(not_found.is_none());
    }

    #[tokio::test]
    async fn test_get_campaign_actors() {
        let manager = create_test_manager();

        // Create a campaign
        for i in 1..=3 {
            let ip = create_test_ip(i);
            manager.register_ja4(ip, "actors_fp".to_string());
        }
        manager.run_detection_cycle().await.unwrap();

        let campaigns = manager.get_campaigns();
        let campaign_id = &campaigns[0].id;

        let actors = manager.get_campaign_actors(campaign_id);
        assert_eq!(actors.len(), 3);

        // Non-existent campaign
        let no_actors = manager.get_campaign_actors("nonexistent");
        assert!(no_actors.is_empty());
    }

    // ========================================================================
    // Stats Tracking Tests
    // ========================================================================

    #[tokio::test]
    async fn test_stats_tracking() {
        let manager = create_test_manager();

        // Initial stats
        let initial = manager.stats();
        assert_eq!(initial.fingerprints_registered, 0);
        assert_eq!(initial.detections_run, 0);
        assert_eq!(initial.campaigns_created, 0);
        assert!(initial.last_scan.is_none());

        // Register some fingerprints
        for i in 1..=5 {
            let ip = create_test_ip(i);
            manager.register_ja4(ip, "stats_test_fp".to_string());
        }

        let after_register = manager.stats();
        assert_eq!(after_register.fingerprints_registered, 5);
        assert_eq!(after_register.index_stats.total_ips, 5);

        // Run detection
        manager.run_detection_cycle().await.unwrap();

        let after_detect = manager.stats();
        assert_eq!(after_detect.detections_run, 1);
        assert!(after_detect.last_scan.is_some());
        assert!(after_detect.campaigns_created >= 1);
    }

    // ========================================================================
    // Remove IP Cleanup Tests
    // ========================================================================

    #[tokio::test]
    async fn test_remove_ip_cleanup() {
        let manager = create_test_manager();

        // Create a campaign
        for i in 1..=3 {
            let ip = create_test_ip(i);
            manager.register_ja4(ip, "remove_test_fp".to_string());
        }
        manager.run_detection_cycle().await.unwrap();

        // Verify campaign exists
        let campaigns = manager.get_campaigns();
        assert_eq!(campaigns[0].actor_count, 3);

        // Remove one IP
        let ip_to_remove = create_test_ip(1);
        manager.remove_ip(&ip_to_remove);

        // Verify IP was removed from index
        assert_eq!(manager.index.len(), 2);

        // Verify IP was removed from campaign
        let updated_campaigns = manager.get_campaigns();
        assert_eq!(updated_campaigns[0].actor_count, 2);
    }

    // ========================================================================
    // Concurrent Registration Tests
    // ========================================================================

    #[test]
    fn test_concurrent_registration() {
        let manager = Arc::new(create_test_manager());
        let mut handles = vec![];

        // Spawn multiple threads registering fingerprints
        for thread_id in 0..10 {
            let manager = Arc::clone(&manager);
            handles.push(thread::spawn(move || {
                for i in 0..100 {
                    let ip: IpAddr = format!("10.{}.0.{}", thread_id, i % 256).parse().unwrap();
                    manager.register_ja4(ip, format!("fp_t{}_{}", thread_id, i % 5));
                }
            }));
        }

        for handle in handles {
            handle.join().unwrap();
        }

        // Verify no panics and reasonable state
        let stats = manager.stats();
        assert_eq!(stats.fingerprints_registered, 1000);
        assert!(stats.index_stats.total_ips > 0);
    }

    // ========================================================================
    // Trigger Detection Logic Tests
    // ========================================================================

    #[test]
    fn test_should_trigger_detection_below_threshold() {
        let manager = create_test_manager();

        // Register only 2 IPs (below threshold of 3)
        for i in 1..=2 {
            let ip = create_test_ip(i);
            manager.register_ja4(ip, "trigger_test_fp".to_string());
        }

        let ip = create_test_ip(1);
        assert!(!manager.should_trigger_detection(&ip));
    }

    #[test]
    fn test_should_trigger_detection_at_threshold() {
        let manager = create_test_manager();

        // Register 3 IPs (at threshold)
        for i in 1..=3 {
            let ip = create_test_ip(i);
            manager.register_ja4(ip, "trigger_threshold_fp".to_string());
        }

        let ip = create_test_ip(1);
        assert!(manager.should_trigger_detection(&ip));
    }

    // ========================================================================
    // Background Worker Lifecycle Tests
    // ========================================================================

    #[tokio::test]
    async fn test_background_worker_lifecycle() {
        let config = ManagerConfig {
            scan_interval: Duration::from_millis(50),
            background_scanning: true,
            shared_threshold: 3,
            ..Default::default()
        };
        let manager = Arc::new(CampaignManager::with_config(config));

        // Register some fingerprints
        for i in 1..=3 {
            let ip = create_test_ip(i);
            manager.register_ja4(ip, "worker_test_fp".to_string());
        }

        // Start worker
        let worker = Arc::clone(&manager).start_background_worker();

        // Wait for a few cycles
        tokio::time::sleep(Duration::from_millis(200)).await;

        // Verify detection ran
        let stats = manager.stats();
        assert!(stats.detections_run >= 1);

        // Signal shutdown
        manager.shutdown();

        // Worker should complete
        let timeout = tokio::time::timeout(Duration::from_millis(500), worker).await;
        assert!(timeout.is_ok(), "Worker should shut down gracefully");
    }

    #[tokio::test]
    async fn test_shutdown_flag() {
        let manager = CampaignManager::new();

        assert!(!manager.is_shutdown());

        manager.shutdown();

        assert!(manager.is_shutdown());
    }

    // ========================================================================
    // Integration Tests
    // ========================================================================

    #[tokio::test]
    async fn test_full_flow() {
        let manager = create_test_manager();

        // Phase 1: Register fingerprints from multiple IPs
        let fingerprint = "t13d1516h2_full_flow_test";
        for i in 1..=5 {
            let ip = create_test_ip(i);
            manager.register_ja4(ip, fingerprint.to_string());
        }

        // Phase 2: Run detection
        let updates = manager.run_detection_cycle().await.unwrap();
        assert!(updates >= 1);

        // Phase 3: Verify campaign was created
        let campaigns = manager.get_campaigns();
        assert_eq!(campaigns.len(), 1);

        let campaign = &campaigns[0];
        assert_eq!(campaign.actor_count, 5);
        assert!(campaign.confidence >= 0.8);
        assert!(!campaign.correlation_reasons.is_empty());

        // Phase 4: Get campaign by ID
        let retrieved = manager.get_campaign(&campaign.id).unwrap();
        assert_eq!(retrieved.actors.len(), 5);

        // Phase 5: Get actors
        let actors = manager.get_campaign_actors(&campaign.id);
        assert_eq!(actors.len(), 5);

        // Phase 6: Remove an IP
        manager.remove_ip(&create_test_ip(1));
        let updated = manager.get_campaign(&campaign.id).unwrap();
        assert_eq!(updated.actors.len(), 4);

        // Phase 7: Verify stats
        let stats = manager.stats();
        assert_eq!(stats.fingerprints_registered, 5);
        assert_eq!(stats.campaigns_created, 1);
        assert_eq!(stats.campaign_stats.total_campaigns, 1);
    }

    #[test]
    fn test_clear() {
        let manager = create_test_manager();

        // Add some data
        for i in 1..=5 {
            let ip = create_test_ip(i);
            manager.register_ja4(ip, "clear_test_fp".to_string());
        }

        assert_eq!(manager.index.len(), 5);

        // Clear
        manager.clear();

        assert_eq!(manager.index.len(), 0);
        assert!(manager.store.is_empty());
    }

    #[tokio::test]
    async fn test_resolve_campaign() {
        let manager = create_test_manager();

        // Create a campaign
        for i in 1..=3 {
            let ip = create_test_ip(i);
            manager.register_ja4(ip, "resolve_test_fp".to_string());
        }
        manager.run_detection_cycle().await.unwrap();

        let campaigns = manager.get_campaigns();
        let campaign_id = campaigns[0].id.clone();

        // Resolve the campaign
        let result = manager.resolve_campaign(&campaign_id, "Threat mitigated");
        assert!(result.is_ok());

        // Verify campaign is resolved
        let resolved = manager.get_campaign(&campaign_id).unwrap();
        assert_eq!(resolved.status, CampaignStatus::Resolved);

        // Active campaigns should now be empty
        let active = manager.get_campaigns();
        assert!(active.is_empty());
    }

    #[test]
    fn test_index_and_store_access() {
        let manager = create_test_manager();

        // Verify we can access internal components
        let _index = manager.index();
        let _store = manager.store();
        let _config = manager.config();

        // These should not panic
        assert!(manager.index().is_empty());
        assert!(manager.store().is_empty());
    }

    // ========================================================================
    // Edge Cases
    // ========================================================================

    #[test]
    fn test_ipv6_addresses() {
        let manager = create_test_manager();

        let ipv6_1: IpAddr = "2001:db8::1".parse().unwrap();
        let ipv6_2: IpAddr = "2001:db8::2".parse().unwrap();
        let ipv6_3: IpAddr = "2001:db8::3".parse().unwrap();

        manager.register_ja4(ipv6_1, "ipv6_fp".to_string());
        manager.register_ja4(ipv6_2, "ipv6_fp".to_string());
        manager.register_ja4(ipv6_3, "ipv6_fp".to_string());

        let stats = manager.stats();
        assert_eq!(stats.fingerprints_registered, 3);
        assert_eq!(stats.index_stats.total_ips, 3);
    }

    #[test]
    fn test_default_trait() {
        let manager = CampaignManager::default();

        assert!(manager.index.is_empty());
        assert!(manager.store.is_empty());
        assert!(!manager.is_shutdown());
    }

    #[tokio::test]
    async fn test_multiple_fingerprint_groups() {
        let manager = create_test_manager();

        // Group 1: 3 IPs with fingerprint A
        for i in 1..=3 {
            let ip = create_test_ip(i);
            manager.register_ja4(ip, "group_a_fp".to_string());
        }

        // Group 2: 4 IPs with fingerprint B
        for i in 10..=13 {
            let ip = create_test_ip(i);
            manager.register_ja4(ip, "group_b_fp".to_string());
        }

        manager.run_detection_cycle().await.unwrap();

        let campaigns = manager.get_campaigns();
        assert_eq!(campaigns.len(), 2);

        // Verify both groups created campaigns
        let actor_counts: Vec<usize> = campaigns.iter().map(|c| c.actor_count).collect();
        assert!(actor_counts.contains(&3));
        assert!(actor_counts.contains(&4));
    }
}