go-brrr 0.1.0

Token-efficient code analysis for LLMs - Rust implementation
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
//! Hardcoded secret and credential detection.
//!
//! Detects secrets embedded in source code including:
//! - API keys from major providers (AWS, GitHub, Slack, Stripe, Google, etc.)
//! - Private keys (RSA, EC, PGP)
//! - JWTs (JSON Web Tokens)
//! - Connection strings (database URLs with credentials)
//! - Generic patterns (password = "...", api_key = "...", etc.)
//! - High-entropy strings assigned to sensitive variables
//!
//! # False Positive Mitigation
//!
//! The detector ignores:
//! - Environment variable reads (`os.environ['API_KEY']`, `process.env.API_KEY`)
//! - Config file reads (`config.get('password')`)
//! - Placeholder values (`YOUR_API_KEY_HERE`, `changeme`, `xxx`, `TODO`)
//! - Test files (reduced severity)
//! - Example/documentation strings
//! - Empty or obviously fake values
//!
//! # Architecture
//!
//! The detector uses a multi-layer approach:
//! 1. **Provider Patterns**: High-confidence regex for known API key formats
//! 2. **Generic Patterns**: Assignment patterns for sensitive variable names
//! 3. **Entropy Analysis**: Shannon entropy for detecting random strings
//! 4. **Context Analysis**: AST context to filter false positives
//!
//! # Example
//!
//! ```ignore
//! use go_brrr::security::secrets::{SecretsDetector, scan_secrets};
//!
//! // Scan a directory
//! let result = scan_secrets("./src", None)?;
//! for finding in result.findings {
//!     println!("[{}] {} at {}:{}",
//!         finding.severity,
//!         finding.secret_type,
//!         finding.location.file,
//!         finding.location.line
//!     );
//! }
//! ```

use std::collections::{HashMap, HashSet};
use std::path::Path;

use once_cell::sync::Lazy;
use regex::Regex;
use serde::{Deserialize, Serialize};

use crate::error::{Result, BrrrError};

// =============================================================================
// Types
// =============================================================================

/// Type of secret detected.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum SecretType {
    /// AWS Access Key ID (AKIA...)
    AwsAccessKey,
    /// AWS Secret Access Key
    AwsSecretKey,
    /// GitHub Personal Access Token (ghp_..., gho_..., ghu_..., ghs_..., ghr_...)
    GitHubToken,
    /// GitLab Personal Access Token (glpat-...)
    GitLabToken,
    /// Slack token (xox[baprs]-...)
    SlackToken,
    /// Stripe API key (sk_live_..., sk_test_..., rk_live_..., rk_test_...)
    StripeKey,
    /// Google API key (AIza...)
    GoogleApiKey,
    /// Google Cloud service account key (JSON with private_key)
    GoogleServiceAccount,
    /// Twilio API key or Auth token
    TwilioKey,
    /// SendGrid API key (SG....)
    SendGridKey,
    /// Mailgun API key
    MailgunKey,
    /// Heroku API key
    HerokuKey,
    /// npm token
    NpmToken,
    /// PyPI token
    PyPiToken,
    /// Discord token
    DiscordToken,
    /// Telegram Bot token
    TelegramToken,
    /// Firebase/FCM server key
    FirebaseKey,
    /// Azure storage/service key
    AzureKey,
    /// DigitalOcean token
    DigitalOceanToken,
    /// Datadog API key
    DatadogKey,
    /// OpenAI API key (sk-...)
    OpenAiKey,
    /// Anthropic API key (sk-ant-...)
    AnthropicKey,
    /// RSA/EC/DSA private key (-----BEGIN ... PRIVATE KEY-----)
    PrivateKey,
    /// PGP private key block
    PgpPrivateKey,
    /// SSH private key
    SshPrivateKey,
    /// JSON Web Token (eyJ...)
    Jwt,
    /// Database connection string with embedded credentials
    ConnectionString,
    /// Generic password assignment
    Password,
    /// Generic API key assignment
    ApiKey,
    /// Generic secret/token assignment
    GenericSecret,
    /// High-entropy string (potential secret)
    HighEntropyString,
}

impl std::fmt::Display for SecretType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SecretType::AwsAccessKey => write!(f, "AWS Access Key"),
            SecretType::AwsSecretKey => write!(f, "AWS Secret Access Key"),
            SecretType::GitHubToken => write!(f, "GitHub Token"),
            SecretType::GitLabToken => write!(f, "GitLab Token"),
            SecretType::SlackToken => write!(f, "Slack Token"),
            SecretType::StripeKey => write!(f, "Stripe API Key"),
            SecretType::GoogleApiKey => write!(f, "Google API Key"),
            SecretType::GoogleServiceAccount => write!(f, "Google Service Account Key"),
            SecretType::TwilioKey => write!(f, "Twilio Key"),
            SecretType::SendGridKey => write!(f, "SendGrid API Key"),
            SecretType::MailgunKey => write!(f, "Mailgun API Key"),
            SecretType::HerokuKey => write!(f, "Heroku API Key"),
            SecretType::NpmToken => write!(f, "npm Token"),
            SecretType::PyPiToken => write!(f, "PyPI Token"),
            SecretType::DiscordToken => write!(f, "Discord Token"),
            SecretType::TelegramToken => write!(f, "Telegram Bot Token"),
            SecretType::FirebaseKey => write!(f, "Firebase/FCM Key"),
            SecretType::AzureKey => write!(f, "Azure Key"),
            SecretType::DigitalOceanToken => write!(f, "DigitalOcean Token"),
            SecretType::DatadogKey => write!(f, "Datadog API Key"),
            SecretType::OpenAiKey => write!(f, "OpenAI API Key"),
            SecretType::AnthropicKey => write!(f, "Anthropic API Key"),
            SecretType::PrivateKey => write!(f, "Private Key"),
            SecretType::PgpPrivateKey => write!(f, "PGP Private Key"),
            SecretType::SshPrivateKey => write!(f, "SSH Private Key"),
            SecretType::Jwt => write!(f, "JSON Web Token"),
            SecretType::ConnectionString => write!(f, "Connection String"),
            SecretType::Password => write!(f, "Password"),
            SecretType::ApiKey => write!(f, "API Key"),
            SecretType::GenericSecret => write!(f, "Generic Secret"),
            SecretType::HighEntropyString => write!(f, "High-Entropy String"),
        }
    }
}

/// Severity level for secret findings.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum Severity {
    /// Informational only (possible false positive)
    Info,
    /// Low severity (test files, examples, low entropy)
    Low,
    /// Medium severity (generic patterns, moderate confidence)
    Medium,
    /// High severity (provider-specific patterns, high confidence)
    High,
    /// Critical severity (private keys, production credentials)
    Critical,
}

impl std::fmt::Display for Severity {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Severity::Info => write!(f, "INFO"),
            Severity::Low => write!(f, "LOW"),
            Severity::Medium => write!(f, "MEDIUM"),
            Severity::High => write!(f, "HIGH"),
            Severity::Critical => write!(f, "CRITICAL"),
        }
    }
}

/// Confidence level for the detection.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum Confidence {
    /// Low confidence (heuristic match, may be false positive)
    Low,
    /// Medium confidence (pattern match with some validation)
    Medium,
    /// High confidence (provider-specific pattern with format validation)
    High,
}

impl std::fmt::Display for Confidence {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Confidence::Low => write!(f, "LOW"),
            Confidence::Medium => write!(f, "MEDIUM"),
            Confidence::High => write!(f, "HIGH"),
        }
    }
}

/// Location of a finding in source code.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Location {
    /// File path
    pub file: String,
    /// Line number (1-indexed)
    pub line: usize,
    /// Column number (1-indexed)
    pub column: usize,
    /// End line number (1-indexed)
    pub end_line: usize,
    /// End column number (1-indexed)
    pub end_column: usize,
}

/// A secret finding.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SecretFinding {
    /// Location in source code
    pub location: Location,
    /// Type of secret detected
    pub secret_type: SecretType,
    /// Severity level
    pub severity: Severity,
    /// Confidence level
    pub confidence: Confidence,
    /// Masked value (first and last few chars visible)
    pub masked_value: String,
    /// Variable name if detected via assignment
    pub variable_name: Option<String>,
    /// Human-readable description
    pub description: String,
    /// Suggested remediation
    pub remediation: String,
    /// Whether this is in a test file
    pub is_test_file: bool,
    /// Shannon entropy of the secret value
    pub entropy: Option<f64>,
}

/// Summary of secret scan results.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScanResult {
    /// All findings
    pub findings: Vec<SecretFinding>,
    /// Number of files scanned
    pub files_scanned: usize,
    /// Counts by secret type
    pub type_counts: HashMap<String, usize>,
    /// Counts by severity
    pub severity_counts: HashMap<String, usize>,
}

// =============================================================================
// Pattern Definitions
// =============================================================================

/// Provider-specific pattern with metadata.
struct ProviderPattern {
    regex: Regex,
    secret_type: SecretType,
    severity: Severity,
    confidence: Confidence,
    description: &'static str,
}

/// Generic assignment pattern.
struct GenericPattern {
    var_regex: Regex,
    secret_type: SecretType,
    min_value_length: usize,
    description: &'static str,
}

/// Compile provider-specific patterns.
static PROVIDER_PATTERNS: Lazy<Vec<ProviderPattern>> = Lazy::new(|| {
    vec![
        // AWS Access Key ID
        ProviderPattern {
            regex: Regex::new(r"(?i)\b(AKIA[0-9A-Z]{16})\b").expect("Invalid regex"),
            secret_type: SecretType::AwsAccessKey,
            severity: Severity::Critical,
            confidence: Confidence::High,
            description: "AWS Access Key ID detected",
        },
        // AWS Secret Access Key (40 chars, base64-like)
        ProviderPattern {
            regex: Regex::new(r#"(?i)(?:aws_secret_access_key|aws_secret_key|secret_access_key)\s*[=:]\s*["']?([A-Za-z0-9/+=]{40})["']?"#).expect("Invalid regex"),
            secret_type: SecretType::AwsSecretKey,
            severity: Severity::Critical,
            confidence: Confidence::High,
            description: "AWS Secret Access Key detected",
        },
        // GitHub tokens (multiple types)
        ProviderPattern {
            regex: Regex::new(r"\b(ghp_[a-zA-Z0-9]{36})\b").expect("Invalid regex"),
            secret_type: SecretType::GitHubToken,
            severity: Severity::High,
            confidence: Confidence::High,
            description: "GitHub Personal Access Token (classic) detected",
        },
        ProviderPattern {
            regex: Regex::new(r"\b(gho_[a-zA-Z0-9]{36})\b").expect("Invalid regex"),
            secret_type: SecretType::GitHubToken,
            severity: Severity::High,
            confidence: Confidence::High,
            description: "GitHub OAuth Access Token detected",
        },
        ProviderPattern {
            regex: Regex::new(r"\b(ghu_[a-zA-Z0-9]{36})\b").expect("Invalid regex"),
            secret_type: SecretType::GitHubToken,
            severity: Severity::High,
            confidence: Confidence::High,
            description: "GitHub User-to-Server Token detected",
        },
        ProviderPattern {
            regex: Regex::new(r"\b(ghs_[a-zA-Z0-9]{36})\b").expect("Invalid regex"),
            secret_type: SecretType::GitHubToken,
            severity: Severity::High,
            confidence: Confidence::High,
            description: "GitHub Server-to-Server Token detected",
        },
        ProviderPattern {
            regex: Regex::new(r"\b(ghr_[a-zA-Z0-9]{36})\b").expect("Invalid regex"),
            secret_type: SecretType::GitHubToken,
            severity: Severity::High,
            confidence: Confidence::High,
            description: "GitHub Refresh Token detected",
        },
        ProviderPattern {
            regex: Regex::new(r"\b(github_pat_[a-zA-Z0-9]{22}_[a-zA-Z0-9]{59})\b").expect("Invalid regex"),
            secret_type: SecretType::GitHubToken,
            severity: Severity::High,
            confidence: Confidence::High,
            description: "GitHub Fine-Grained Personal Access Token detected",
        },
        // GitLab tokens
        ProviderPattern {
            regex: Regex::new(r"\b(glpat-[a-zA-Z0-9_-]{20,})\b").expect("Invalid regex"),
            secret_type: SecretType::GitLabToken,
            severity: Severity::High,
            confidence: Confidence::High,
            description: "GitLab Personal Access Token detected",
        },
        // Slack tokens
        ProviderPattern {
            regex: Regex::new(r"\b(xox[baprs]-[0-9a-zA-Z-]{10,})\b").expect("Invalid regex"),
            secret_type: SecretType::SlackToken,
            severity: Severity::High,
            confidence: Confidence::High,
            description: "Slack Token detected",
        },
        // Stripe keys
        ProviderPattern {
            regex: Regex::new(r"\b(sk_live_[0-9a-zA-Z]{24,})\b").expect("Invalid regex"),
            secret_type: SecretType::StripeKey,
            severity: Severity::Critical,
            confidence: Confidence::High,
            description: "Stripe Live Secret Key detected",
        },
        ProviderPattern {
            regex: Regex::new(r"\b(sk_test_[0-9a-zA-Z]{24,})\b").expect("Invalid regex"),
            secret_type: SecretType::StripeKey,
            severity: Severity::Medium,
            confidence: Confidence::High,
            description: "Stripe Test Secret Key detected",
        },
        ProviderPattern {
            regex: Regex::new(r"\b(rk_live_[0-9a-zA-Z]{24,})\b").expect("Invalid regex"),
            secret_type: SecretType::StripeKey,
            severity: Severity::Critical,
            confidence: Confidence::High,
            description: "Stripe Live Restricted Key detected",
        },
        // Google API key
        ProviderPattern {
            regex: Regex::new(r"\b(AIza[0-9A-Za-z_-]{35})\b").expect("Invalid regex"),
            secret_type: SecretType::GoogleApiKey,
            severity: Severity::High,
            confidence: Confidence::High,
            description: "Google API Key detected",
        },
        // Twilio
        ProviderPattern {
            regex: Regex::new(r"\b(SK[0-9a-fA-F]{32})\b").expect("Invalid regex"),
            secret_type: SecretType::TwilioKey,
            severity: Severity::High,
            confidence: Confidence::Medium,
            description: "Twilio API Key detected",
        },
        // SendGrid
        ProviderPattern {
            regex: Regex::new(r"\b(SG\.[a-zA-Z0-9_-]{22}\.[a-zA-Z0-9_-]{43})\b").expect("Invalid regex"),
            secret_type: SecretType::SendGridKey,
            severity: Severity::High,
            confidence: Confidence::High,
            description: "SendGrid API Key detected",
        },
        // npm tokens
        ProviderPattern {
            regex: Regex::new(r"\b(npm_[a-zA-Z0-9]{36})\b").expect("Invalid regex"),
            secret_type: SecretType::NpmToken,
            severity: Severity::High,
            confidence: Confidence::High,
            description: "npm Access Token detected",
        },
        // PyPI tokens
        ProviderPattern {
            regex: Regex::new(r"\b(pypi-[a-zA-Z0-9_-]{50,})\b").expect("Invalid regex"),
            secret_type: SecretType::PyPiToken,
            severity: Severity::High,
            confidence: Confidence::High,
            description: "PyPI API Token detected",
        },
        // Discord tokens (bot tokens and webhooks)
        ProviderPattern {
            regex: Regex::new(r"\b([MN][A-Za-z0-9]{23,}\.[A-Za-z0-9_-]{6}\.[A-Za-z0-9_-]{27,})\b").expect("Invalid regex"),
            secret_type: SecretType::DiscordToken,
            severity: Severity::High,
            confidence: Confidence::Medium,
            description: "Discord Bot Token detected",
        },
        // Telegram Bot tokens
        ProviderPattern {
            regex: Regex::new(r"\b(\d{8,10}:[a-zA-Z0-9_-]{35})\b").expect("Invalid regex"),
            secret_type: SecretType::TelegramToken,
            severity: Severity::High,
            confidence: Confidence::Medium,
            description: "Telegram Bot Token detected",
        },
        // Firebase/FCM
        ProviderPattern {
            regex: Regex::new(r"\b(AAAA[a-zA-Z0-9_-]{140,})\b").expect("Invalid regex"),
            secret_type: SecretType::FirebaseKey,
            severity: Severity::High,
            confidence: Confidence::Medium,
            description: "Firebase Cloud Messaging Server Key detected",
        },
        // Azure
        ProviderPattern {
            regex: Regex::new(r#"(?i)(?:AccountKey|SharedAccessKey)\s*=\s*([A-Za-z0-9+/=]{44,})"#).expect("Invalid regex"),
            secret_type: SecretType::AzureKey,
            severity: Severity::Critical,
            confidence: Confidence::High,
            description: "Azure Storage/Service Key detected",
        },
        // DigitalOcean
        ProviderPattern {
            regex: Regex::new(r"\b(dop_v1_[a-f0-9]{64})\b").expect("Invalid regex"),
            secret_type: SecretType::DigitalOceanToken,
            severity: Severity::High,
            confidence: Confidence::High,
            description: "DigitalOcean Personal Access Token detected",
        },
        ProviderPattern {
            regex: Regex::new(r"\b(doo_v1_[a-f0-9]{64})\b").expect("Invalid regex"),
            secret_type: SecretType::DigitalOceanToken,
            severity: Severity::High,
            confidence: Confidence::High,
            description: "DigitalOcean OAuth Token detected",
        },
        // Datadog
        ProviderPattern {
            regex: Regex::new(r"\b([a-f0-9]{32})\b").expect("Invalid regex"),
            secret_type: SecretType::DatadogKey,
            severity: Severity::Medium,
            confidence: Confidence::Low,
            description: "Possible Datadog API Key detected (32-char hex)",
        },
        // OpenAI
        ProviderPattern {
            regex: Regex::new(r"\b(sk-[a-zA-Z0-9]{20}T3BlbkFJ[a-zA-Z0-9]{20})\b").expect("Invalid regex"),
            secret_type: SecretType::OpenAiKey,
            severity: Severity::High,
            confidence: Confidence::High,
            description: "OpenAI API Key detected",
        },
        ProviderPattern {
            regex: Regex::new(r"\b(sk-proj-[a-zA-Z0-9_-]{40,})\b").expect("Invalid regex"),
            secret_type: SecretType::OpenAiKey,
            severity: Severity::High,
            confidence: Confidence::High,
            description: "OpenAI Project API Key detected",
        },
        // Anthropic
        ProviderPattern {
            regex: Regex::new(r"\b(sk-ant-api[0-9]{2}-[a-zA-Z0-9_-]{90,})\b").expect("Invalid regex"),
            secret_type: SecretType::AnthropicKey,
            severity: Severity::High,
            confidence: Confidence::High,
            description: "Anthropic API Key detected",
        },
        // Heroku
        ProviderPattern {
            regex: Regex::new(r#"(?i)heroku[a-z_-]*\s*[=:]\s*["']?([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})["']?"#).expect("Invalid regex"),
            secret_type: SecretType::HerokuKey,
            severity: Severity::High,
            confidence: Confidence::Medium,
            description: "Heroku API Key detected",
        },
        // Private keys (RSA, EC, DSA, etc.)
        ProviderPattern {
            regex: Regex::new(r"-----BEGIN\s+(?:RSA\s+)?PRIVATE\s+KEY-----").expect("Invalid regex"),
            secret_type: SecretType::PrivateKey,
            severity: Severity::Critical,
            confidence: Confidence::High,
            description: "RSA Private Key detected",
        },
        ProviderPattern {
            regex: Regex::new(r"-----BEGIN\s+EC\s+PRIVATE\s+KEY-----").expect("Invalid regex"),
            secret_type: SecretType::PrivateKey,
            severity: Severity::Critical,
            confidence: Confidence::High,
            description: "EC Private Key detected",
        },
        ProviderPattern {
            regex: Regex::new(r"-----BEGIN\s+DSA\s+PRIVATE\s+KEY-----").expect("Invalid regex"),
            secret_type: SecretType::PrivateKey,
            severity: Severity::Critical,
            confidence: Confidence::High,
            description: "DSA Private Key detected",
        },
        ProviderPattern {
            regex: Regex::new(r"-----BEGIN\s+ENCRYPTED\s+PRIVATE\s+KEY-----").expect("Invalid regex"),
            secret_type: SecretType::PrivateKey,
            severity: Severity::High,
            confidence: Confidence::High,
            description: "Encrypted Private Key detected (still sensitive)",
        },
        ProviderPattern {
            regex: Regex::new(r"-----BEGIN\s+OPENSSH\s+PRIVATE\s+KEY-----").expect("Invalid regex"),
            secret_type: SecretType::SshPrivateKey,
            severity: Severity::Critical,
            confidence: Confidence::High,
            description: "OpenSSH Private Key detected",
        },
        // PGP private key
        ProviderPattern {
            regex: Regex::new(r"-----BEGIN\s+PGP\s+PRIVATE\s+KEY\s+BLOCK-----").expect("Invalid regex"),
            secret_type: SecretType::PgpPrivateKey,
            severity: Severity::Critical,
            confidence: Confidence::High,
            description: "PGP Private Key Block detected",
        },
        // JWT (three base64url-encoded parts separated by dots)
        ProviderPattern {
            regex: Regex::new(r"\b(eyJ[A-Za-z0-9_-]{10,}\.eyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]+)\b").expect("Invalid regex"),
            secret_type: SecretType::Jwt,
            severity: Severity::Medium,
            confidence: Confidence::High,
            description: "JSON Web Token detected",
        },
        // Connection strings with credentials
        ProviderPattern {
            regex: Regex::new(r##"(?i)(postgres(?:ql)?|mysql|mongodb(?:\+srv)?|redis|amqp)://[^:]+:[^@]+@[^\s"'`]+"##).expect("Invalid regex"),
            secret_type: SecretType::ConnectionString,
            severity: Severity::Critical,
            confidence: Confidence::High,
            description: "Database connection string with embedded credentials detected",
        },
    ]
});

/// Compile generic assignment patterns.
static GENERIC_PATTERNS: Lazy<Vec<GenericPattern>> = Lazy::new(|| {
    vec![
        GenericPattern {
            var_regex: Regex::new(r#"(?i)(?:^|[^a-zA-Z0-9_])(?:password|passwd|pwd)\s*[=:]\s*["']([^"']+)["']"#).expect("Invalid regex"),
            secret_type: SecretType::Password,
            min_value_length: 4,
            description: "Hardcoded password detected",
        },
        GenericPattern {
            var_regex: Regex::new(r#"(?i)(?:^|[^a-zA-Z0-9_])(?:api_?key|apikey|api_?token)\s*[=:]\s*["']([^"']+)["']"#).expect("Invalid regex"),
            secret_type: SecretType::ApiKey,
            min_value_length: 8,
            description: "Hardcoded API key detected",
        },
        GenericPattern {
            var_regex: Regex::new(r#"(?i)(?:^|[^a-zA-Z0-9_])(?:secret|secret_?key|private_?key|auth_?key|access_?key)\s*[=:]\s*["']([^"']+)["']"#).expect("Invalid regex"),
            secret_type: SecretType::GenericSecret,
            min_value_length: 8,
            description: "Hardcoded secret detected",
        },
        GenericPattern {
            var_regex: Regex::new(r#"(?i)(?:^|[^a-zA-Z0-9_])(?:token|auth_?token|access_?token|bearer_?token)\s*[=:]\s*["']([^"']+)["']"#).expect("Invalid regex"),
            secret_type: SecretType::GenericSecret,
            min_value_length: 8,
            description: "Hardcoded token detected",
        },
        GenericPattern {
            var_regex: Regex::new(r#"(?i)(?:^|[^a-zA-Z0-9_])(?:credentials?|creds?)\s*[=:]\s*["']([^"']+)["']"#).expect("Invalid regex"),
            secret_type: SecretType::GenericSecret,
            min_value_length: 6,
            description: "Hardcoded credentials detected",
        },
    ]
});

/// Placeholder values that should be ignored.
static PLACEHOLDER_PATTERNS: Lazy<Vec<Regex>> = Lazy::new(|| {
    vec![
        // Explicit placeholders
        Regex::new(r"(?i)^(your[_-]?api[_-]?key|your[_-]?secret|your[_-]?token|your[_-]?password)").expect("Invalid regex"),
        Regex::new(r"(?i)(changeme|change[_-]?me|replace[_-]?me|insert[_-]?here|put[_-]?here|add[_-]?here)").expect("Invalid regex"),
        Regex::new(r"(?i)^(xxx+|placeholder|dummy|fake|test|example|sample|demo)").expect("Invalid regex"),
        Regex::new(r"(?i)(todo|fixme|tbd|none|null|undefined|empty)").expect("Invalid regex"),
        // Common example patterns
        Regex::new(r"(?i)^<[^>]+>$").expect("Invalid regex"),  // <your-api-key>
        Regex::new(r"(?i)^\$\{[^}]+\}$").expect("Invalid regex"),  // ${API_KEY}
        Regex::new(r"(?i)^\{\{[^}]+\}\}$").expect("Invalid regex"),  // {{api_key}}
        Regex::new(r"(?i)^%[^%]+%$").expect("Invalid regex"),  // %API_KEY%
        // Repeating characters (detected via entropy check in is_placeholder)
        // Note: backreferences not supported in Rust regex, handled programmatically
        // Obvious fakes
        Regex::new(r"^(1234|abcd|pass|password|secret|token|key)").expect("Invalid regex"),
    ]
});

/// Patterns indicating environment variable reads (not hardcoded secrets).
static ENV_VAR_PATTERNS: Lazy<Vec<Regex>> = Lazy::new(|| {
    vec![
        // Python
        Regex::new(r#"os\.environ\s*\["#).expect("Invalid regex"),
        Regex::new(r#"os\.environ\.get\s*\("#).expect("Invalid regex"),
        Regex::new(r#"os\.getenv\s*\("#).expect("Invalid regex"),
        Regex::new(r#"environ\s*\["#).expect("Invalid regex"),
        // JavaScript/TypeScript
        Regex::new(r#"process\.env\."#).expect("Invalid regex"),
        Regex::new(r#"process\.env\["#).expect("Invalid regex"),
        Regex::new(r#"Deno\.env\.get\("#).expect("Invalid regex"),
        Regex::new(r#"import\.meta\.env\."#).expect("Invalid regex"),
        // Go
        Regex::new(r#"os\.Getenv\s*\("#).expect("Invalid regex"),
        Regex::new(r#"os\.LookupEnv\s*\("#).expect("Invalid regex"),
        // Rust
        Regex::new(r#"std::env::var\s*\("#).expect("Invalid regex"),
        Regex::new(r#"env::var\s*\("#).expect("Invalid regex"),
        Regex::new(r#"env!\s*\("#).expect("Invalid regex"),
        // Java
        Regex::new(r#"System\.getenv\s*\("#).expect("Invalid regex"),
        Regex::new(r#"System\.getProperty\s*\("#).expect("Invalid regex"),
        // Ruby
        Regex::new(r#"ENV\s*\["#).expect("Invalid regex"),
        Regex::new(r#"ENV\.fetch\s*\("#).expect("Invalid regex"),
        // C/C++
        Regex::new(r#"getenv\s*\("#).expect("Invalid regex"),
        // .NET
        Regex::new(r#"Environment\.GetEnvironmentVariable\s*\("#).expect("Invalid regex"),
    ]
});

/// Patterns indicating config file reads.
static CONFIG_READ_PATTERNS: Lazy<Vec<Regex>> = Lazy::new(|| {
    vec![
        Regex::new(r#"config\s*\.\s*get\s*\("#).expect("Invalid regex"),
        Regex::new(r#"config\s*\["#).expect("Invalid regex"),
        Regex::new(r#"settings\s*\.\s*get\s*\("#).expect("Invalid regex"),
        Regex::new(r#"settings\s*\["#).expect("Invalid regex"),
        Regex::new(r#"\.config\("#).expect("Invalid regex"),
        Regex::new(r#"viper\.Get"#).expect("Invalid regex"),
        Regex::new(r#"configuration\."#).expect("Invalid regex"),
    ]
});

// =============================================================================
// Entropy Calculation
// =============================================================================

/// Calculate Shannon entropy of a string using SIMD-optimized byte histogram.
///
/// Entropy is a measure of randomness. High entropy strings (> 4.5 bits/char)
/// are likely to be secrets like API keys or passwords.
///
/// # Algorithm
///
/// Uses a 256-element byte histogram for O(1) frequency lookups instead of HashMap.
/// On x86_64 with AVX2, uses SIMD to accelerate histogram building by processing
/// 32 bytes at a time with vectorized comparisons.
fn calculate_entropy(s: &str) -> f64 {
    let bytes = s.as_bytes();
    if bytes.is_empty() {
        return 0.0;
    }

    // Build byte histogram using SIMD when available
    let histogram = build_byte_histogram(bytes);

    // Calculate entropy from histogram
    let len = bytes.len() as f64;
    let mut entropy = 0.0;

    for &count in &histogram {
        if count > 0 {
            let probability = f64::from(count) / len;
            entropy -= probability * probability.log2();
        }
    }

    entropy
}

/// Build a 256-element byte histogram.
///
/// Dispatches to SIMD or scalar implementation based on platform and CPU features.
#[inline]
fn build_byte_histogram(bytes: &[u8]) -> [u32; 256] {
    #[cfg(target_arch = "x86_64")]
    {
        if is_x86_feature_detected!("avx2") {
            // SAFETY: AVX2 feature check ensures the intrinsics are available
            return unsafe { build_byte_histogram_avx2(bytes) };
        }
    }

    build_byte_histogram_scalar(bytes)
}

/// Scalar implementation of byte histogram building.
///
/// Uses a fixed 256-element array instead of HashMap for O(1) updates.
#[inline]
fn build_byte_histogram_scalar(bytes: &[u8]) -> [u32; 256] {
    let mut histogram = [0u32; 256];

    // Process 4 bytes at a time for better instruction-level parallelism
    let chunks = bytes.chunks_exact(4);
    let remainder = chunks.remainder();

    for chunk in chunks {
        histogram[chunk[0] as usize] += 1;
        histogram[chunk[1] as usize] += 1;
        histogram[chunk[2] as usize] += 1;
        histogram[chunk[3] as usize] += 1;
    }

    for &b in remainder {
        histogram[b as usize] += 1;
    }

    histogram
}

/// AVX2 SIMD implementation of byte histogram building.
///
/// Processes 32 bytes at a time using SIMD comparisons (u8x32).
/// For each unique byte value, uses vectorized comparison and popcount
/// to count occurrences across 32-byte chunks simultaneously.
///
/// # Safety
///
/// Caller must ensure AVX2 is available on the CPU.
#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "avx2")]
unsafe fn build_byte_histogram_avx2(bytes: &[u8]) -> [u32; 256] {
    use std::arch::x86_64::{
        __m256i, _mm256_cmpeq_epi8, _mm256_loadu_si256, _mm256_movemask_epi8, _mm256_set1_epi8,
    };

    let mut histogram = [0u32; 256];
    let len = bytes.len();

    // SIMD setup overhead makes scalar faster for short strings
    if len < 128 {
        return build_byte_histogram_scalar(bytes);
    }

    let ptr = bytes.as_ptr();
    let aligned_len = len & !31; // Round down to multiple of 32

    // Process 32 bytes at a time
    for offset in (0..aligned_len).step_by(32) {
        // Load 32 bytes into AVX2 register (u8x32)
        let data = _mm256_loadu_si256(ptr.add(offset).cast::<__m256i>());

        // Count occurrences of each byte value using SIMD comparisons
        // Each comparison checks 32 bytes in parallel
        for byte_val in 0u8..=255u8 {
            // Broadcast single byte to all 32 lanes
            let pattern = _mm256_set1_epi8(byte_val as i8);
            // Compare: each lane becomes 0xFF if match, 0x00 otherwise
            let cmp = _mm256_cmpeq_epi8(data, pattern);
            // Extract MSB of each byte into 32-bit mask
            let mask = _mm256_movemask_epi8(cmp) as u32;
            // Count set bits = number of matches in this 32-byte chunk
            histogram[byte_val as usize] += mask.count_ones();
        }
    }

    // Handle remaining bytes with scalar code
    for &b in bytes.get_unchecked(aligned_len..) {
        histogram[b as usize] += 1;
    }

    histogram
}

/// Minimum entropy threshold for high-entropy detection.
const MIN_ENTROPY_THRESHOLD: f64 = 4.5;

/// Minimum length for high-entropy string detection.
const MIN_ENTROPY_STRING_LENGTH: usize = 16;

// =============================================================================
// Detector Implementation
// =============================================================================

/// Secrets detector for multiple languages.
pub struct SecretsDetector {
    /// Whether to skip test files
    skip_test_files: bool,
    /// Whether to include high-entropy detection
    include_entropy: bool,
    /// Minimum entropy threshold
    entropy_threshold: f64,
}

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

// =============================================================================
// SIMD Utilities
// =============================================================================

/// Check if all bytes in a slice are identical.
///
/// Uses u8x32 to compare 32 bytes at a time with early exit on first mismatch.
/// Falls back to scalar comparison for the remainder.
///
/// # Performance
/// - O(n/32) SIMD comparisons for the main loop
/// - Early exit on first mismatch avoids unnecessary work
/// - Zero allocations
#[inline]
#[allow(clippy::manual_is_ascii_check)] // SIMD optimization is intentional
fn is_all_same_byte(bytes: &[u8]) -> bool {
    use std::simd::{cmp::SimdPartialEq, u8x32};

    if bytes.is_empty() {
        return true;
    }

    let first = bytes[0];
    let len = bytes.len();

    // For very short inputs, scalar is faster (no SIMD setup overhead)
    if len < 32 {
        return bytes.iter().all(|&b| b == first);
    }

    // SIMD: splat first byte across all 32 lanes
    let first_vec = u8x32::splat(first);
    let mut offset = 0_usize;

    // Process 32 bytes at a time
    while offset + 32 <= len {
        // SAFETY: bounds check guarantees bytes[offset..offset+32] is valid
        let chunk = u8x32::from_slice(&bytes[offset..offset + 32]);
        let mask = chunk.simd_eq(first_vec);

        // Early exit: if not all lanes match, bytes are not uniform
        // All lanes equal means lower 32 bits are all 1s (0xFFFF_FFFF)
        // to_bitmask() returns u64, but only lower 32 bits are meaningful for u8x32
        if mask.to_bitmask() != 0xFFFF_FFFF_u64 {
            return false;
        }
        offset += 32;
    }

    // Scalar: handle remaining bytes (0..31)
    bytes[offset..].iter().all(|&b| b == first)
}

/// Count non-whitespace bytes in a string using SIMD.
///
/// Uses u8x32 to check 32 bytes at a time for ASCII whitespace characters:
/// space (0x20), tab (0x09), newline (0x0A), carriage return (0x0D).
/// Subtracts whitespace count from total length to get non-whitespace count.
///
/// # Performance
/// - O(n/32) SIMD operations for the main loop
/// - Processes 32 bytes per iteration using parallel comparison
/// - Zero allocations
///
/// # Note
/// This counts bytes, not Unicode code points. For ASCII-heavy content like
/// source code and secrets, this is equivalent and significantly faster than
/// iterating over chars.
#[inline]
fn count_non_whitespace_simd(s: &str) -> usize {
    use std::simd::{cmp::SimdPartialEq, u8x32};

    let bytes = s.as_bytes();
    let len = bytes.len();

    // For short inputs, scalar is faster (no SIMD setup overhead)
    if len < 32 {
        return bytes
            .iter()
            .filter(|&&b| !matches!(b, b' ' | b'\t' | b'\n' | b'\r'))
            .count();
    }

    // SIMD constants: splat each whitespace byte across all 32 lanes
    let space = u8x32::splat(b' '); // 0x20
    let tab = u8x32::splat(b'\t'); // 0x09
    let newline = u8x32::splat(b'\n'); // 0x0A
    let cr = u8x32::splat(b'\r'); // 0x0D

    let mut whitespace_count: usize = 0;
    let mut offset = 0_usize;

    // Process 32 bytes at a time
    while offset + 32 <= len {
        let chunk = u8x32::from_slice(&bytes[offset..offset + 32]);

        // Create masks for each whitespace character (parallel comparison)
        let is_space = chunk.simd_eq(space);
        let is_tab = chunk.simd_eq(tab);
        let is_newline = chunk.simd_eq(newline);
        let is_cr = chunk.simd_eq(cr);

        // Combine masks: true if byte matches any whitespace character
        let is_whitespace = is_space | is_tab | is_newline | is_cr;

        // Count set bits in bitmask (each set bit = one whitespace byte)
        // to_bitmask() returns u64, lower 32 bits are meaningful for u8x32
        whitespace_count += is_whitespace.to_bitmask().count_ones() as usize;

        offset += 32;
    }

    // Scalar: handle remaining bytes (0..31)
    for &b in &bytes[offset..] {
        if matches!(b, b' ' | b'\t' | b'\n' | b'\r') {
            whitespace_count += 1;
        }
    }

    // Return non-whitespace count by subtracting whitespace from total
    len - whitespace_count
}

impl SecretsDetector {
    /// Create a new secrets detector with default settings.
    pub fn new() -> Self {
        Self {
            skip_test_files: false,
            include_entropy: true,
            entropy_threshold: MIN_ENTROPY_THRESHOLD,
        }
    }

    /// Set whether to skip test files.
    #[must_use]
    pub fn skip_test_files(mut self, skip: bool) -> Self {
        self.skip_test_files = skip;
        self
    }

    /// Set whether to include high-entropy detection.
    #[must_use]
    pub fn include_entropy(mut self, include: bool) -> Self {
        self.include_entropy = include;
        self
    }

    /// Set the entropy threshold for high-entropy detection.
    #[must_use]
    pub fn entropy_threshold(mut self, threshold: f64) -> Self {
        self.entropy_threshold = threshold;
        self
    }

    /// Check if a file is a test file.
    fn is_test_file(path: &Path) -> bool {
        let path_str = path.to_string_lossy().to_lowercase();

        // Check directory names
        if path_str.contains("/test/")
            || path_str.contains("/tests/")
            || path_str.contains("/__tests__/")
            || path_str.contains("/spec/")
            || path_str.contains("/specs/")
            || path_str.contains("/_test/")
            || path_str.contains("/test_")
            || path_str.contains("/testdata/")
            || path_str.contains("/fixtures/")
            || path_str.contains("/mocks/")
        {
            return true;
        }

        // Check file names
        if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
            let name_lower = name.to_lowercase();
            if name_lower.starts_with("test_")
                || name_lower.ends_with("_test.py")
                || name_lower.ends_with("_test.go")
                || name_lower.ends_with("_test.rs")
                || name_lower.ends_with("_test.ts")
                || name_lower.ends_with("_test.js")
                || name_lower.ends_with(".test.ts")
                || name_lower.ends_with(".test.js")
                || name_lower.ends_with(".test.tsx")
                || name_lower.ends_with(".test.jsx")
                || name_lower.ends_with(".spec.ts")
                || name_lower.ends_with(".spec.js")
                || name_lower.ends_with("_spec.rb")
                || name_lower.ends_with("test.java")
            {
                return true;
            }
        }

        false
    }

    /// Check if a value is a placeholder (not a real secret).
    fn is_placeholder(value: &str) -> bool {
        // Empty or very short values
        if value.len() < 4 {
            return true;
        }

        // Check for repeating characters (e.g., "xxxxxxxx", "00000000")
        // This replaces the backreference pattern ^(.)\1{7,}$ which Rust regex doesn't support
        // Uses SIMD for efficient 32-byte-at-a-time comparison with early exit
        if value.len() >= 8 && is_all_same_byte(value.as_bytes()) {
            return true;
        }

        // Check against placeholder patterns
        for pattern in PLACEHOLDER_PATTERNS.iter() {
            if pattern.is_match(value) {
                return true;
            }
        }

        false
    }

    /// Check if a line is reading from environment or config.
    fn is_env_or_config_read(line: &str) -> bool {
        for pattern in ENV_VAR_PATTERNS.iter() {
            if pattern.is_match(line) {
                return true;
            }
        }

        for pattern in CONFIG_READ_PATTERNS.iter() {
            if pattern.is_match(line) {
                return true;
            }
        }

        false
    }

    /// Mask a secret value, showing only first and last few characters.
    fn mask_value(value: &str) -> String {
        let len = value.len();
        if len <= 8 {
            return "*".repeat(len);
        }

        let visible = std::cmp::min(4, len / 4);
        let masked_len = len - (visible * 2);

        format!(
            "{}{}{}",
            &value[..visible],
            "*".repeat(masked_len),
            &value[len - visible..]
        )
    }

    /// Generate remediation advice for a secret type.
    fn get_remediation(secret_type: SecretType) -> String {
        match secret_type {
            SecretType::AwsAccessKey | SecretType::AwsSecretKey => {
                "1. Immediately rotate the AWS credentials in IAM console\n\
                 2. Use environment variables or AWS Secrets Manager\n\
                 3. Consider using IAM roles for EC2/Lambda/ECS instead of keys"
                    .to_string()
            }
            SecretType::GitHubToken | SecretType::GitLabToken => {
                "1. Revoke the token immediately in repository settings\n\
                 2. Generate a new token with minimal required permissions\n\
                 3. Use GITHUB_TOKEN in Actions or environment variables"
                    .to_string()
            }
            SecretType::PrivateKey | SecretType::SshPrivateKey | SecretType::PgpPrivateKey => {
                "1. Remove the private key from source control\n\
                 2. Generate new key pair and distribute new public key\n\
                 3. Store private keys in secure key management systems\n\
                 4. Use ssh-agent or similar for authentication"
                    .to_string()
            }
            SecretType::ConnectionString => {
                "1. Remove credentials from connection string\n\
                 2. Use environment variables for database credentials\n\
                 3. Consider using managed identity or IAM authentication\n\
                 4. Use connection pooling with credential injection"
                    .to_string()
            }
            SecretType::Jwt => {
                "1. Determine if JWT is a hardcoded test token\n\
                 2. If production: rotate signing keys and invalidate tokens\n\
                 3. Generate JWTs dynamically, never hardcode"
                    .to_string()
            }
            SecretType::Password | SecretType::ApiKey | SecretType::GenericSecret => {
                "1. Remove hardcoded credential from source code\n\
                 2. Store in environment variables or secrets manager\n\
                 3. Rotate the credential if it was exposed in VCS history\n\
                 4. Consider using a .env file (not committed to VCS)"
                    .to_string()
            }
            SecretType::HighEntropyString => {
                "1. Review if this is actually a secret\n\
                 2. If secret: move to environment variable or secrets manager\n\
                 3. If not a secret: consider adding to ignore list"
                    .to_string()
            }
            _ => {
                "1. Remove hardcoded credential from source code\n\
                 2. Store securely using environment variables or secrets manager\n\
                 3. Rotate the credential if exposed in VCS history"
                    .to_string()
            }
        }
    }

    /// Scan a single file for secrets.
    pub fn scan_file(&self, file_path: &str) -> Result<Vec<SecretFinding>> {
        let path = Path::new(file_path);
        let is_test = Self::is_test_file(path);

        // Skip test files if configured
        if self.skip_test_files && is_test {
            return Ok(vec![]);
        }

        let source = std::fs::read_to_string(path).map_err(|e| BrrrError::io_with_path(e, path))?;

        let mut findings = Vec::new();

        for (line_num, line) in source.lines().enumerate() {
            let line_number = line_num + 1;

            // Skip lines that are reading from environment or config
            if Self::is_env_or_config_read(line) {
                continue;
            }

            // Check provider-specific patterns
            for pattern in PROVIDER_PATTERNS.iter() {
                if let Some(captures) = pattern.regex.captures(line) {
                    let matched = captures.get(1).or_else(|| captures.get(0));
                    if let Some(m) = matched {
                        let value = m.as_str();

                        // Skip placeholders
                        if Self::is_placeholder(value) {
                            continue;
                        }

                        // Skip very short matches for low-confidence patterns
                        if pattern.confidence == Confidence::Low && value.len() < 20 {
                            continue;
                        }

                        // Adjust severity for test files
                        let severity = if is_test {
                            match pattern.severity {
                                Severity::Critical => Severity::Medium,
                                Severity::High => Severity::Low,
                                _ => Severity::Info,
                            }
                        } else {
                            pattern.severity
                        };

                        let column = m.start() + 1;
                        let end_column = m.end() + 1;

                        findings.push(SecretFinding {
                            location: Location {
                                file: file_path.to_string(),
                                line: line_number,
                                column,
                                end_line: line_number,
                                end_column,
                            },
                            secret_type: pattern.secret_type,
                            severity,
                            confidence: pattern.confidence,
                            masked_value: Self::mask_value(value),
                            variable_name: None,
                            description: pattern.description.to_string(),
                            remediation: Self::get_remediation(pattern.secret_type),
                            is_test_file: is_test,
                            entropy: Some(calculate_entropy(value)),
                        });
                    }
                }
            }

            // Check generic assignment patterns
            for pattern in GENERIC_PATTERNS.iter() {
                if let Some(captures) = pattern.var_regex.captures(line) {
                    if let Some(value_match) = captures.get(1) {
                        let value = value_match.as_str();

                        // Skip placeholders
                        if Self::is_placeholder(value) {
                            continue;
                        }

                        // Skip short values
                        if value.len() < pattern.min_value_length {
                            continue;
                        }

                        // Calculate entropy
                        let entropy = calculate_entropy(value);

                        // Skip low-entropy values for generic patterns
                        if entropy < 3.0 {
                            continue;
                        }

                        // Determine confidence based on entropy
                        let confidence = if entropy > 4.5 {
                            Confidence::High
                        } else if entropy > 3.5 {
                            Confidence::Medium
                        } else {
                            Confidence::Low
                        };

                        // Determine severity
                        let base_severity = if entropy > 4.5 {
                            Severity::High
                        } else if entropy > 3.5 {
                            Severity::Medium
                        } else {
                            Severity::Low
                        };

                        let severity = if is_test {
                            match base_severity {
                                Severity::High => Severity::Low,
                                Severity::Medium => Severity::Info,
                                _ => Severity::Info,
                            }
                        } else {
                            base_severity
                        };

                        let column = value_match.start() + 1;
                        let end_column = value_match.end() + 1;

                        findings.push(SecretFinding {
                            location: Location {
                                file: file_path.to_string(),
                                line: line_number,
                                column,
                                end_line: line_number,
                                end_column,
                            },
                            secret_type: pattern.secret_type,
                            severity,
                            confidence,
                            masked_value: Self::mask_value(value),
                            variable_name: None,
                            description: pattern.description.to_string(),
                            remediation: Self::get_remediation(pattern.secret_type),
                            is_test_file: is_test,
                            entropy: Some(entropy),
                        });
                    }
                }
            }

            // High-entropy detection (if enabled)
            if self.include_entropy {
                self.detect_high_entropy_strings(
                    line,
                    line_number,
                    file_path,
                    is_test,
                    &mut findings,
                );
            }
        }

        Ok(findings)
    }

    /// Detect high-entropy strings that might be secrets.
    fn detect_high_entropy_strings(
        &self,
        line: &str,
        line_number: usize,
        file_path: &str,
        is_test: bool,
        findings: &mut Vec<SecretFinding>,
    ) {
        // Pattern to find quoted strings
        static STRING_PATTERN: Lazy<Regex> = Lazy::new(|| {
            Regex::new(r#"["']([^"']{16,})["']"#).expect("Invalid regex")
        });

        // Skip comments (simple heuristic)
        let trimmed = line.trim();
        if trimmed.starts_with('#')
            || trimmed.starts_with("//")
            || trimmed.starts_with('*')
            || trimmed.starts_with("/*")
        {
            return;
        }

        for captures in STRING_PATTERN.captures_iter(line) {
            if let Some(value_match) = captures.get(1) {
                let value = value_match.as_str();

                // Skip if too short
                if value.len() < MIN_ENTROPY_STRING_LENGTH {
                    continue;
                }

                // Skip placeholders
                if Self::is_placeholder(value) {
                    continue;
                }

                // Skip URLs, paths, and common patterns
                if value.contains("http://")
                    || value.contains("https://")
                    || value.contains("file://")
                    || value.starts_with('/')
                    || value.contains("\\\\")
                    || value.contains(".com")
                    || value.contains(".org")
                    || value.contains(".io")
                {
                    continue;
                }

                // Skip if mostly spaces or newlines (SIMD-accelerated)
                let non_space: usize = count_non_whitespace_simd(value);
                if (non_space as f64) / (value.len() as f64) < 0.8 {
                    continue;
                }

                let entropy = calculate_entropy(value);

                if entropy >= self.entropy_threshold {
                    // Check if already matched by a provider pattern
                    let already_matched = findings.iter().any(|f| {
                        f.location.line == line_number
                            && f.location.column <= value_match.start() + 1
                            && f.location.end_column >= value_match.end() + 1
                    });

                    if already_matched {
                        continue;
                    }

                    let severity = if is_test {
                        Severity::Info
                    } else if entropy > 5.0 {
                        Severity::Medium
                    } else {
                        Severity::Low
                    };

                    let column = value_match.start() + 1;
                    let end_column = value_match.end() + 1;

                    findings.push(SecretFinding {
                        location: Location {
                            file: file_path.to_string(),
                            line: line_number,
                            column,
                            end_line: line_number,
                            end_column,
                        },
                        secret_type: SecretType::HighEntropyString,
                        severity,
                        confidence: Confidence::Low,
                        masked_value: Self::mask_value(value),
                        variable_name: None,
                        description: format!(
                            "High-entropy string detected (entropy: {:.2} bits/char)",
                            entropy
                        ),
                        remediation: Self::get_remediation(SecretType::HighEntropyString),
                        is_test_file: is_test,
                        entropy: Some(entropy),
                    });
                }
            }
        }
    }

    /// Scan a directory for secrets.
    pub fn scan_directory(&self, dir_path: &str, language: Option<&str>) -> Result<ScanResult> {
        let path = Path::new(dir_path);
        if !path.is_dir() {
            return Err(BrrrError::InvalidArgument(format!(
                "Not a directory: {}",
                dir_path
            )));
        }

        let mut findings = Vec::new();
        let mut files_scanned = 0;

        // Walk directory respecting .gitignore and .brrrignore
        let mut builder = ignore::WalkBuilder::new(path);
        builder.add_custom_ignore_filename(".brrrignore");
        builder.hidden(true);

        // Define extensions to scan based on language
        let extensions: HashSet<&str> = match language {
            Some("python") => ["py"].iter().copied().collect(),
            Some("typescript") | Some("javascript") => ["ts", "tsx", "js", "jsx", "mjs", "cjs"]
                .iter()
                .copied()
                .collect(),
            Some("go") => ["go"].iter().copied().collect(),
            Some("rust") => ["rs"].iter().copied().collect(),
            Some("java") => ["java"].iter().copied().collect(),
            Some("c") => ["c", "h"].iter().copied().collect(),
            Some("cpp") => ["cpp", "cc", "cxx", "hpp", "h"].iter().copied().collect(),
            Some("ruby") => ["rb", "erb"].iter().copied().collect(),
            Some("php") => ["php"].iter().copied().collect(),
            Some("csharp") => ["cs"].iter().copied().collect(),
            _ => [
                "py", "ts", "tsx", "js", "jsx", "mjs", "cjs", "go", "rs", "java", "c", "h", "cpp",
                "cc", "cxx", "hpp", "rb", "erb", "php", "cs", "yaml", "yml", "json", "toml", "xml",
                "properties", "ini", "cfg", "conf", "env", "sh", "bash", "zsh", "ps1", "bat", "cmd",
            ]
            .iter()
            .copied()
            .collect(),
        };

        for entry in builder.build().flatten() {
            let entry_path = entry.path();
            if !entry_path.is_file() {
                continue;
            }

            let ext = entry_path
                .extension()
                .and_then(|e| e.to_str())
                .unwrap_or("");

            // Special handling for dotenv files
            let file_name = entry_path
                .file_name()
                .and_then(|n| n.to_str())
                .unwrap_or("");

            let should_scan = extensions.contains(ext)
                || file_name.starts_with(".env")
                || file_name.ends_with(".env")
                || file_name == "Dockerfile"
                || file_name == "docker-compose.yml"
                || file_name == "docker-compose.yaml";

            if !should_scan {
                continue;
            }

            files_scanned += 1;

            if let Ok(file_findings) = self.scan_file(entry_path.to_str().unwrap_or("")) {
                findings.extend(file_findings);
            }
        }

        // Count by type and severity
        let mut type_counts: HashMap<String, usize> = HashMap::new();
        let mut severity_counts: HashMap<String, usize> = HashMap::new();

        for finding in &findings {
            *type_counts
                .entry(finding.secret_type.to_string())
                .or_insert(0) += 1;
            *severity_counts
                .entry(finding.severity.to_string())
                .or_insert(0) += 1;
        }

        Ok(ScanResult {
            findings,
            files_scanned,
            type_counts,
            severity_counts,
        })
    }
}

// =============================================================================
// Public API Functions
// =============================================================================

/// Scan a file or directory for hardcoded secrets.
///
/// # Arguments
///
/// * `path` - Path to file or directory to scan
/// * `language` - Optional language filter (python, typescript, etc.)
///
/// # Returns
///
/// Scan result with all findings
pub fn scan_secrets(path: &str, language: Option<&str>) -> Result<ScanResult> {
    let detector = SecretsDetector::new();
    let path_obj = Path::new(path);

    if path_obj.is_file() {
        let findings = detector.scan_file(path)?;

        let mut type_counts: HashMap<String, usize> = HashMap::new();
        let mut severity_counts: HashMap<String, usize> = HashMap::new();

        for finding in &findings {
            *type_counts
                .entry(finding.secret_type.to_string())
                .or_insert(0) += 1;
            *severity_counts
                .entry(finding.severity.to_string())
                .or_insert(0) += 1;
        }

        Ok(ScanResult {
            findings,
            files_scanned: 1,
            type_counts,
            severity_counts,
        })
    } else {
        detector.scan_directory(path, language)
    }
}

/// Scan a single file for hardcoded secrets.
pub fn scan_file_secrets(path: &Path, _language: Option<&str>) -> Result<Vec<SecretFinding>> {
    let detector = SecretsDetector::new();
    detector.scan_file(path.to_str().unwrap_or(""))
}

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

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

    fn create_temp_file(content: &str, extension: &str) -> tempfile::NamedTempFile {
        let mut file = tempfile::Builder::new()
            .suffix(extension)
            .tempfile()
            .expect("Failed to create temp file");
        file.write_all(content.as_bytes())
            .expect("Failed to write temp file");
        file
    }

    // =========================================================================
    // AWS Tests
    // =========================================================================

    #[test]
    fn test_detect_aws_access_key() {
        let source = r#"
AWS_ACCESS_KEY_ID = "AKIAIOSFODNN7EXAMPLE"
aws_secret_key = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
        "#;
        let file = create_temp_file(source, ".py");
        let detector = SecretsDetector::new();
        let findings = detector
            .scan_file(file.path().to_str().unwrap())
            .expect("Scan should succeed");

        assert!(!findings.is_empty(), "Should detect AWS credentials");

        let aws_key = findings.iter().find(|f| f.secret_type == SecretType::AwsAccessKey);
        assert!(aws_key.is_some(), "Should detect AWS Access Key");
    }

    // =========================================================================
    // GitHub Token Tests
    // =========================================================================

    #[test]
    fn test_detect_github_token_classic() {
        let source = r#"
GITHUB_TOKEN = "ghp_1234567890abcdefghijklmnopqrstuvwxyz"
        "#;
        let file = create_temp_file(source, ".py");
        let detector = SecretsDetector::new();
        let findings = detector
            .scan_file(file.path().to_str().unwrap())
            .expect("Scan should succeed");

        assert!(!findings.is_empty(), "Should detect GitHub token");
        assert_eq!(findings[0].secret_type, SecretType::GitHubToken);
        assert_eq!(findings[0].confidence, Confidence::High);
    }

    #[test]
    fn test_detect_github_fine_grained_token() {
        // Fine-grained PAT format: github_pat_[base62]{22}_[base62]{59}
        // 22 chars + 59 chars
        let source = r#"
token = "github_pat_11ABCD1234567890abcdef_1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklm"
        "#;
        let file = create_temp_file(source, ".py");
        let detector = SecretsDetector::new();
        let findings = detector
            .scan_file(file.path().to_str().unwrap())
            .expect("Scan should succeed");

        let gh_token = findings.iter().find(|f| f.secret_type == SecretType::GitHubToken);
        assert!(gh_token.is_some(), "Should detect fine-grained GitHub token");
    }

    // =========================================================================
    // Slack Token Tests
    // =========================================================================

    #[test]
    fn test_detect_slack_token() {
        let source = r#"
SLACK_TOKEN = "xoxb-FAKE-TEST-TOKEN-FOR-UNIT-TESTS-ONLY-abcd"
        "#;
        let file = create_temp_file(source, ".ts");
        let detector = SecretsDetector::new();
        let findings = detector
            .scan_file(file.path().to_str().unwrap())
            .expect("Scan should succeed");

        assert!(!findings.is_empty(), "Should detect Slack token");
        assert_eq!(findings[0].secret_type, SecretType::SlackToken);
    }

    // =========================================================================
    // Stripe Key Tests
    // =========================================================================

    #[test]
    fn test_detect_stripe_live_key() {
        let source = r#"
stripe.api_key = "sk_test_FAKE_TEST_KEY_FOR_UNIT_TESTS"
        "#;
        let file = create_temp_file(source, ".py");
        let detector = SecretsDetector::new();
        let findings = detector
            .scan_file(file.path().to_str().unwrap())
            .expect("Scan should succeed");

        let stripe_key = findings.iter().find(|f| f.secret_type == SecretType::StripeKey);
        assert!(stripe_key.is_some(), "Should detect Stripe live key");
        assert_eq!(stripe_key.unwrap().severity, Severity::Critical);
    }

    #[test]
    fn test_detect_stripe_test_key_lower_severity() {
        let source = r#"
stripe.api_key = "sk_test_1234567890abcdefghijklmnop"
        "#;
        let file = create_temp_file(source, ".py");
        let detector = SecretsDetector::new();
        let findings = detector
            .scan_file(file.path().to_str().unwrap())
            .expect("Scan should succeed");

        let stripe_key = findings.iter().find(|f| f.secret_type == SecretType::StripeKey);
        assert!(stripe_key.is_some(), "Should detect Stripe test key");
        assert_eq!(stripe_key.unwrap().severity, Severity::Medium);
    }

    // =========================================================================
    // Google API Key Tests
    // =========================================================================

    #[test]
    fn test_detect_google_api_key() {
        let source = r#"
const apiKey = "AIzaSyDaGmWKa4JsXZ-HjGw7ISLn_3namBGewQe"
        "#;
        let file = create_temp_file(source, ".ts");
        let detector = SecretsDetector::new();
        let findings = detector
            .scan_file(file.path().to_str().unwrap())
            .expect("Scan should succeed");

        let google_key = findings.iter().find(|f| f.secret_type == SecretType::GoogleApiKey);
        assert!(google_key.is_some(), "Should detect Google API key");
    }

    // =========================================================================
    // Private Key Tests
    // =========================================================================

    #[test]
    fn test_detect_rsa_private_key() {
        let source = r#"
-----BEGIN RSA PRIVATE KEY-----
MIIEpQIBAAKCAQEA2Z3qX2BTLS4e...
-----END RSA PRIVATE KEY-----
        "#;
        let file = create_temp_file(source, ".pem");
        let detector = SecretsDetector::new();
        let findings = detector
            .scan_file(file.path().to_str().unwrap())
            .expect("Scan should succeed");

        let priv_key = findings.iter().find(|f| f.secret_type == SecretType::PrivateKey);
        assert!(priv_key.is_some(), "Should detect RSA private key");
        assert_eq!(priv_key.unwrap().severity, Severity::Critical);
    }

    #[test]
    fn test_detect_openssh_private_key() {
        let source = r#"
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAA...
-----END OPENSSH PRIVATE KEY-----
        "#;
        let file = create_temp_file(source, ".key");
        let detector = SecretsDetector::new();
        let findings = detector
            .scan_file(file.path().to_str().unwrap())
            .expect("Scan should succeed");

        let ssh_key = findings.iter().find(|f| f.secret_type == SecretType::SshPrivateKey);
        assert!(ssh_key.is_some(), "Should detect OpenSSH private key");
    }

    // =========================================================================
    // JWT Tests
    // =========================================================================

    #[test]
    fn test_detect_jwt() {
        let source = r#"
const token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
        "#;
        let file = create_temp_file(source, ".js");
        let detector = SecretsDetector::new();
        let findings = detector
            .scan_file(file.path().to_str().unwrap())
            .expect("Scan should succeed");

        let jwt = findings.iter().find(|f| f.secret_type == SecretType::Jwt);
        assert!(jwt.is_some(), "Should detect JWT");
    }

    // =========================================================================
    // Connection String Tests
    // =========================================================================

    #[test]
    fn test_detect_postgres_connection_string() {
        let source = r#"
DATABASE_URL = "postgres://user:password123@localhost:5432/mydb"
        "#;
        let file = create_temp_file(source, ".env");
        let detector = SecretsDetector::new();
        let findings = detector
            .scan_file(file.path().to_str().unwrap())
            .expect("Scan should succeed");

        let conn_str = findings
            .iter()
            .find(|f| f.secret_type == SecretType::ConnectionString);
        assert!(conn_str.is_some(), "Should detect Postgres connection string");
        assert_eq!(conn_str.unwrap().severity, Severity::Critical);
    }

    #[test]
    fn test_detect_mongodb_connection_string() {
        let source = r#"
MONGO_URI = "mongodb+srv://admin:secretpass@cluster0.abc123.mongodb.net/mydb"
        "#;
        let file = create_temp_file(source, ".env");
        let detector = SecretsDetector::new();
        let findings = detector
            .scan_file(file.path().to_str().unwrap())
            .expect("Scan should succeed");

        let conn_str = findings
            .iter()
            .find(|f| f.secret_type == SecretType::ConnectionString);
        assert!(conn_str.is_some(), "Should detect MongoDB connection string");
    }

    // =========================================================================
    // Generic Pattern Tests
    // =========================================================================

    #[test]
    fn test_detect_hardcoded_password() {
        let source = r#"
password = "MyS3cr3tP@ssw0rd!"
        "#;
        let file = create_temp_file(source, ".py");
        let detector = SecretsDetector::new();
        let findings = detector
            .scan_file(file.path().to_str().unwrap())
            .expect("Scan should succeed");

        let password = findings.iter().find(|f| f.secret_type == SecretType::Password);
        assert!(password.is_some(), "Should detect hardcoded password");
    }

    #[test]
    fn test_detect_hardcoded_api_key() {
        let source = r#"
api_key = "abc123xyz789secretkey456"
        "#;
        let file = create_temp_file(source, ".py");
        let detector = SecretsDetector::new();
        let findings = detector
            .scan_file(file.path().to_str().unwrap())
            .expect("Scan should succeed");

        let api_key = findings.iter().find(|f| f.secret_type == SecretType::ApiKey);
        assert!(api_key.is_some(), "Should detect hardcoded API key");
    }

    // =========================================================================
    // False Positive Tests
    // =========================================================================

    #[test]
    fn test_ignore_environment_variable_read_python() {
        let source = r#"
API_KEY = os.environ["API_KEY"]
SECRET = os.getenv("SECRET_KEY")
TOKEN = os.environ.get("AUTH_TOKEN")
        "#;
        let file = create_temp_file(source, ".py");
        let detector = SecretsDetector::new();
        let findings = detector
            .scan_file(file.path().to_str().unwrap())
            .expect("Scan should succeed");

        assert!(findings.is_empty(), "Should NOT flag environment variable reads");
    }

    #[test]
    fn test_ignore_environment_variable_read_typescript() {
        let source = r#"
const apiKey = process.env.API_KEY;
const secret = process.env["SECRET_KEY"];
        "#;
        let file = create_temp_file(source, ".ts");
        let detector = SecretsDetector::new();
        let findings = detector
            .scan_file(file.path().to_str().unwrap())
            .expect("Scan should succeed");

        assert!(findings.is_empty(), "Should NOT flag process.env reads");
    }

    #[test]
    fn test_ignore_placeholder_values() {
        let source = r#"
API_KEY = "YOUR_API_KEY_HERE"
password = "changeme"
secret = "xxxxxxxxxxxxxxxx"
token = "<your-token>"
key = "placeholder"
        "#;
        let file = create_temp_file(source, ".py");
        let detector = SecretsDetector::new();
        let findings = detector
            .scan_file(file.path().to_str().unwrap())
            .expect("Scan should succeed");

        assert!(findings.is_empty(), "Should NOT flag placeholder values");
    }

    #[test]
    fn test_ignore_config_reads() {
        let source = r#"
password = config.get("password")
api_key = settings["api_key"]
        "#;
        let file = create_temp_file(source, ".py");
        let detector = SecretsDetector::new();
        let findings = detector
            .scan_file(file.path().to_str().unwrap())
            .expect("Scan should succeed");

        assert!(findings.is_empty(), "Should NOT flag config reads");
    }

    #[test]
    fn test_reduced_severity_in_test_files() {
        let source = r#"
GITHUB_TOKEN = "ghp_1234567890abcdefghijklmnopqrstuvwxyz"
        "#;
        let mut file = tempfile::Builder::new()
            .suffix("_test.py")
            .tempfile()
            .expect("Failed to create temp file");
        file.write_all(source.as_bytes())
            .expect("Failed to write temp file");

        let detector = SecretsDetector::new();
        let findings = detector
            .scan_file(file.path().to_str().unwrap())
            .expect("Scan should succeed");

        assert!(!findings.is_empty(), "Should detect token in test file");
        assert!(findings[0].is_test_file, "Should mark as test file");
        // Severity should be reduced
        assert!(
            findings[0].severity < Severity::High,
            "Severity should be reduced for test files"
        );
    }

    // =========================================================================
    // Entropy Tests
    // =========================================================================

    #[test]
    fn test_entropy_calculation() {
        // Low entropy (repeating characters)
        let low_entropy = calculate_entropy("aaaaaaaaaaaaaaaaaaaa");
        assert!(low_entropy < 1.0, "Repeating chars should have low entropy");

        // Medium entropy (simple text)
        let medium_entropy = calculate_entropy("hello world test");
        assert!(
            medium_entropy > 2.0 && medium_entropy < 4.0,
            "Simple text should have medium entropy"
        );

        // High entropy (random-looking)
        let high_entropy = calculate_entropy("aB3$kL9#mN2@pQ5&rT8");
        assert!(high_entropy > 4.0, "Random-looking string should have high entropy");
    }

    #[test]
    fn test_detect_high_entropy_string() {
        let source = r#"
const key = "aB3kL9mN2pQ5rT8xY1cD4eF7gH0iJ"
        "#;
        let file = create_temp_file(source, ".js");
        let detector = SecretsDetector::new();
        let findings = detector
            .scan_file(file.path().to_str().unwrap())
            .expect("Scan should succeed");

        let high_entropy = findings
            .iter()
            .find(|f| f.secret_type == SecretType::HighEntropyString);
        assert!(
            high_entropy.is_some(),
            "Should detect high-entropy string as potential secret"
        );
    }

    // =========================================================================
    // Masking Tests
    // =========================================================================

    #[test]
    fn test_mask_value() {
        assert_eq!(SecretsDetector::mask_value("short"), "*****");
        assert_eq!(SecretsDetector::mask_value("12345678"), "********");
        assert_eq!(
            SecretsDetector::mask_value("1234567890123456"),
            "1234********3456"
        );
        assert_eq!(
            SecretsDetector::mask_value("ghp_1234567890abcdefghijklmnopqrstuvwxyz"),
            "ghp_********************************wxyz"
        );
    }

    // =========================================================================
    // OpenAI / Anthropic Tests
    // =========================================================================

    #[test]
    fn test_detect_openai_key() {
        let source = r#"
OPENAI_API_KEY = "sk-proj-abcdefghijklmnopqrstuvwxyz1234567890ABCD"
        "#;
        let file = create_temp_file(source, ".env");
        let detector = SecretsDetector::new();
        let findings = detector
            .scan_file(file.path().to_str().unwrap())
            .expect("Scan should succeed");

        let openai_key = findings.iter().find(|f| f.secret_type == SecretType::OpenAiKey);
        assert!(openai_key.is_some(), "Should detect OpenAI API key");
    }

    // =========================================================================
    // SendGrid Tests
    // =========================================================================

    #[test]
    fn test_detect_sendgrid_key() {
        // SendGrid format: SG.[22 chars].[43 chars]
        let source = r#"
SENDGRID_API_KEY = "SG.FAKE_TEST_KEY_ONLY.THIS_IS_NOT_A_REAL_SENDGRID_API_KEY_FOR_TESTS"
        "#;
        let file = create_temp_file(source, ".env");
        let detector = SecretsDetector::new();
        let findings = detector
            .scan_file(file.path().to_str().unwrap())
            .expect("Scan should succeed");

        let sg_key = findings.iter().find(|f| f.secret_type == SecretType::SendGridKey);
        assert!(sg_key.is_some(), "Should detect SendGrid API key");
    }

    // =========================================================================
    // Severity / Display Tests
    // =========================================================================

    #[test]
    fn test_severity_display() {
        assert_eq!(Severity::Critical.to_string(), "CRITICAL");
        assert_eq!(Severity::High.to_string(), "HIGH");
        assert_eq!(Severity::Medium.to_string(), "MEDIUM");
        assert_eq!(Severity::Low.to_string(), "LOW");
        assert_eq!(Severity::Info.to_string(), "INFO");
    }

    #[test]
    fn test_confidence_display() {
        assert_eq!(Confidence::High.to_string(), "HIGH");
        assert_eq!(Confidence::Medium.to_string(), "MEDIUM");
        assert_eq!(Confidence::Low.to_string(), "LOW");
    }

    #[test]
    fn test_secret_type_display() {
        assert_eq!(SecretType::AwsAccessKey.to_string(), "AWS Access Key");
        assert_eq!(SecretType::GitHubToken.to_string(), "GitHub Token");
        assert_eq!(SecretType::PrivateKey.to_string(), "Private Key");
        assert_eq!(SecretType::ConnectionString.to_string(), "Connection String");
    }

    // =========================================================================
    // Scan Result Tests
    // =========================================================================

    #[test]
    fn test_scan_result_counts() {
        let result = ScanResult {
            findings: vec![],
            files_scanned: 10,
            type_counts: [("AWS Access Key".to_string(), 2)]
                .into_iter()
                .collect(),
            severity_counts: [("CRITICAL".to_string(), 2), ("HIGH".to_string(), 3)]
                .into_iter()
                .collect(),
        };

        assert_eq!(result.files_scanned, 10);
        assert_eq!(result.type_counts.get("AWS Access Key"), Some(&2));
        assert_eq!(result.severity_counts.get("CRITICAL"), Some(&2));
    }

    // =========================================================================
    // SIMD Utility Tests
    // =========================================================================

    #[test]
    fn test_count_non_whitespace_simd() {
        // Empty string
        assert_eq!(count_non_whitespace_simd(""), 0);

        // All non-whitespace
        assert_eq!(count_non_whitespace_simd("hello"), 5);
        assert_eq!(count_non_whitespace_simd("abc123!@#"), 9);

        // All whitespace
        assert_eq!(count_non_whitespace_simd("    "), 0);
        assert_eq!(count_non_whitespace_simd("\t\t\t"), 0);
        assert_eq!(count_non_whitespace_simd("\n\n\n"), 0);
        assert_eq!(count_non_whitespace_simd("\r\n\r\n"), 0);
        assert_eq!(count_non_whitespace_simd(" \t\n\r"), 0);

        // Mixed
        assert_eq!(count_non_whitespace_simd("hello world"), 10);
        assert_eq!(count_non_whitespace_simd("  hello  "), 5);
        assert_eq!(count_non_whitespace_simd("\thello\n"), 5);
        assert_eq!(count_non_whitespace_simd("a b c d e"), 5);

        // Exactly 32 bytes (one SIMD iteration)
        let s32 = "abcdefghijklmnopqrstuvwxyz123456";
        assert_eq!(s32.len(), 32);
        assert_eq!(count_non_whitespace_simd(s32), 32);

        // 33 bytes with whitespace (6 spaces, 27 non-whitespace)
        let s33_ws = "abcd efgh ijkl mnop qrst uvwx yz1";
        assert_eq!(s33_ws.len(), 33);
        assert_eq!(count_non_whitespace_simd(s33_ws), 27);

        // Larger than 32 bytes (multiple SIMD iterations)
        let long = "a".repeat(100);
        assert_eq!(count_non_whitespace_simd(&long), 100);

        let long_ws = "a ".repeat(50); // 100 bytes, 50 'a' and 50 spaces
        assert_eq!(count_non_whitespace_simd(&long_ws), 50);

        // Edge case: 31 bytes (scalar path)
        let s31 = "abcdefghijklmnopqrstuvwxyz12345";
        assert_eq!(s31.len(), 31);
        assert_eq!(count_non_whitespace_simd(s31), 31);

        // Edge case: 33 bytes (SIMD + 1 scalar)
        let s33 = "abcdefghijklmnopqrstuvwxyz1234567";
        assert_eq!(s33.len(), 33);
        assert_eq!(count_non_whitespace_simd(s33), 33);

        // High-entropy secret-like string with no whitespace
        let secret = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY";
        assert_eq!(count_non_whitespace_simd(secret), secret.len());
    }
}