ai-rsk 0.7.2

Security gate for AI-generated code - blocks the build until vulnerabilities are fixed
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
use crate::types::{Ecosystem, Finding, FindingKind, Severity};
use std::collections::HashSet;
use std::path::Path;
use walkdir::WalkDir;

/// Directories to skip when scanning for source files.
const EXCLUDED_DIRS: &[&str] = &[
    "node_modules",
    "vendor",
    ".git",
    "dist",
    "build",
    "bundle",
    "out",
    "__pycache__",
    ".pytest_cache",
    "target",
    ".venv",
    "venv",
    "env",
    ".next",
    ".nuxt",
];

/// Run project analysis (couche 3) and return ADVISE findings.
/// This analyzes the project structure, not the code itself.
/// Every finding is based on file presence/absence - no guessing.
/// `active_profiles` controls which compliance checks run (e.g., "gdpr" enables cookie/privacy checks).
pub fn analyze_project(
    project_path: &Path,
    ecosystems: &[Ecosystem],
    active_profiles: &[String],
) -> Vec<Finding> {
    let mut findings = Vec::new();

    // 1. Tests detection
    findings.extend(check_tests(project_path, ecosystems));

    // 2. CI/CD detection
    findings.extend(check_ci_cd(project_path));

    // 3. Dead dependencies (JS only for now - factual, verified)
    // If knip is available, it handles dead code/deps detection (runner.rs runs it).
    // Our fallback detection only runs when knip is NOT installed.
    if ecosystems.contains(&Ecosystem::JavaScript) && !crate::runner::knip_available(project_path) {
        findings.extend(check_dead_deps_js(project_path));
    }

    // 4. Documentation presence
    findings.extend(check_documentation(project_path));

    // 5. Framework detection + advice
    if ecosystems.contains(&Ecosystem::JavaScript) {
        findings.extend(analyze_js_stack(project_path));
    }

    // 6. Console.log strip mechanism (JS only)
    if ecosystems.contains(&Ecosystem::JavaScript) {
        findings.extend(check_console_strip(project_path));
    }

    // 7. Tamper protection - detect if ai-rsk has been bypassed
    findings.extend(check_tamper_protection(project_path, ecosystems));

    // 8. GDPR checks (only when gdpr profile is active)
    if active_profiles.iter().any(|p| p == "gdpr") {
        findings.extend(check_cookie_banner(project_path));
        findings.extend(check_privacy_page(project_path));
    }

    // 9. SEO checks (only when seo profile is active)
    if active_profiles.iter().any(|p| p == "seo") {
        findings.extend(check_robots_txt(project_path));
        findings.extend(check_sitemap(project_path));
        findings.extend(check_meta_viewport(project_path));
        findings.extend(check_canonical(project_path));
    }

    // 10. Infrastructure checks (always active)
    findings.extend(check_lockfile(project_path, ecosystems));
    findings.extend(check_env_gitignored(project_path));
    findings.extend(check_dockerfile_root(project_path));

    findings
}

/// Check if the project has any test files or test framework configured.
fn check_tests(project_path: &Path, ecosystems: &[Ecosystem]) -> Vec<Finding> {
    let mut has_test_files = false;
    let mut has_test_config = false;

    // Check for test configuration files
    let test_configs = [
        "jest.config.js",
        "jest.config.ts",
        "jest.config.mjs",
        "jest.config.cjs",
        "vitest.config.js",
        "vitest.config.ts",
        "vitest.config.mjs",
        "pytest.ini",
        "pyproject.toml", // may contain [tool.pytest]
        "setup.cfg",      // may contain [tool:pytest]
        "tox.ini",
        ".mocharc.yml",
        ".mocharc.json",
        "karma.conf.js",
        "ava.config.js",
        "ava.config.mjs",
    ];

    for config in &test_configs {
        if project_path.join(config).exists() {
            has_test_config = true;
            break;
        }
    }

    // Check for test directories
    let test_dirs = ["tests", "test", "__tests__", "spec", "specs"];
    for dir in &test_dirs {
        let test_dir = project_path.join(dir);
        if test_dir.is_dir() {
            // Verify the directory actually contains files
            if std::fs::read_dir(&test_dir)
                .ok()
                .is_some_and(|entries| entries.count() > 0)
            {
                has_test_files = true;
                break;
            }
        }
    }

    // Check for test files in src/ (*.test.*, *.spec.*)
    if !has_test_files {
        for entry in WalkDir::new(project_path)
            .max_depth(4)
            .into_iter()
            .filter_map(|e| e.ok())
            .filter(|e| e.file_type().is_file())
        {
            let path = entry.path();
            if is_excluded_dir(path) {
                continue;
            }
            if let Some(name) = path.file_name().and_then(|n| n.to_str())
                && (name.contains(".test.") || name.contains(".spec.") || name.contains("_test."))
            {
                has_test_files = true;
                break;
            }
        }
    }

    // Rust projects: check for #[cfg(test)] in any .rs file
    if !has_test_files && ecosystems.contains(&Ecosystem::Rust) {
        for entry in WalkDir::new(project_path)
            .max_depth(4)
            .into_iter()
            .filter_map(|e| e.ok())
            .filter(|e| {
                e.file_type().is_file() && e.path().extension().is_some_and(|ext| ext == "rs")
            })
        {
            if is_excluded_dir(entry.path()) {
                continue;
            }
            if let Ok(content) = std::fs::read_to_string(entry.path())
                && (content.contains("#[cfg(test)]") || content.contains("#[test]"))
            {
                has_test_files = true;
                break;
            }
        }
    }

    // Python: check for test_*.py files
    if !has_test_files && ecosystems.contains(&Ecosystem::Python) {
        for entry in WalkDir::new(project_path)
            .max_depth(4)
            .into_iter()
            .filter_map(|e| e.ok())
            .filter(|e| e.file_type().is_file())
        {
            if is_excluded_dir(entry.path()) {
                continue;
            }
            if let Some(name) = entry.path().file_name().and_then(|n| n.to_str())
                && name.starts_with("test_")
                && name.ends_with(".py")
            {
                has_test_files = true;
                break;
            }
        }
    }

    // Go: check for *_test.go files
    if !has_test_files && ecosystems.contains(&Ecosystem::Go) {
        for entry in WalkDir::new(project_path)
            .max_depth(4)
            .into_iter()
            .filter_map(|e| e.ok())
            .filter(|e| e.file_type().is_file())
        {
            if is_excluded_dir(entry.path()) {
                continue;
            }
            if let Some(name) = entry.path().file_name().and_then(|n| n.to_str())
                && name.ends_with("_test.go")
            {
                has_test_files = true;
                break;
            }
        }
    }

    if !has_test_files && !has_test_config {
        return vec![Finding {
            severity: Severity::Advise,
            kind: FindingKind::ProjectAdvice {
                advice_id: "NO_TESTS".to_string(),
                question: "No test framework or test files detected. The LLM MUST ask the developer: \"Do you want me to set up a test framework?\"".to_string(),
            },
            file: None,
            line: None,
            message: "No test framework or test files detected in the project.".to_string(),
        }];
    }

    vec![]
}

/// Check if CI/CD is configured.
fn check_ci_cd(project_path: &Path) -> Vec<Finding> {
    let ci_indicators = [
        ".github/workflows",
        ".gitlab-ci.yml",
        ".circleci",
        "Jenkinsfile",
        ".travis.yml",
        "bitbucket-pipelines.yml",
        "azure-pipelines.yml",
        ".drone.yml",
        ".woodpecker.yml",
        ".forgejo/workflows",
        ".gitea/workflows",
    ];

    for indicator in &ci_indicators {
        let ci_path = project_path.join(indicator);
        if ci_path.exists() {
            // For directories (like .github/workflows), check they contain files
            if ci_path.is_dir() {
                if std::fs::read_dir(&ci_path)
                    .ok()
                    .is_some_and(|entries| entries.count() > 0)
                {
                    return vec![];
                }
            } else {
                return vec![];
            }
        }
    }

    vec![Finding {
        severity: Severity::Advise,
        kind: FindingKind::ProjectAdvice {
            advice_id: "NO_CI_CD".to_string(),
            question: "No CI/CD pipeline detected. Without CI/CD, ai-rsk security gates can be bypassed locally (--no-verify, || true). The LLM MUST ask the developer: \"Do you want me to create a CI/CD pipeline with ai-rsk scan integrated?\"".to_string(),
        },
        file: None,
        line: None,
        message: "No CI/CD pipeline detected - ai-rsk scan can be bypassed locally without CI as last defense.".to_string(),
    }]
}

/// Check for dead dependencies in a JavaScript/TypeScript project.
/// Dead = listed in package.json dependencies but never imported in source files.
fn check_dead_deps_js(project_path: &Path) -> Vec<Finding> {
    let pkg_path = project_path.join("package.json");
    let pkg_content = match std::fs::read_to_string(&pkg_path) {
        Ok(c) => c,
        Err(_) => return vec![],
    };

    // Parse package.json to extract dependency names.
    // We use a minimal JSON parser approach - no need for serde_json as a dep.
    // Extract keys from "dependencies" and "devDependencies" objects.
    let deps = extract_dependency_names(&pkg_content);
    if deps.is_empty() {
        return vec![];
    }

    // Collect all import/require references from source files
    let mut used_deps: HashSet<String> = HashSet::new();

    for entry in WalkDir::new(project_path)
        .into_iter()
        .filter_map(|e| e.ok())
        .filter(|e| e.file_type().is_file())
    {
        let path = entry.path();
        if is_excluded_dir(path) {
            continue;
        }

        let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
        if !matches!(
            ext,
            "js" | "ts" | "jsx" | "tsx" | "mjs" | "cjs" | "mts" | "cts"
        ) {
            continue;
        }

        let content = match std::fs::read_to_string(path) {
            Ok(c) => c,
            Err(_) => continue,
        };

        // Check each dep against imports in this file
        for dep in &deps {
            if used_deps.contains(dep) {
                continue; // Already found
            }

            // Match: require('dep'), require("dep"), from 'dep', from "dep"
            // Also match: require('dep/...'), from 'dep/...'
            // Also match: import 'dep' (side-effect import)
            if content.contains(&format!("'{dep}'"))
                || content.contains(&format!("\"{dep}\""))
                || content.contains(&format!("'{dep}/"))
                || content.contains(&format!("\"{dep}/"))
            {
                used_deps.insert(dep.clone());
            }
        }
    }

    // Also check package.json scripts for dependency usage (e.g., "concurrently" in npm scripts).
    // Only look inside the "scripts" section to avoid false matches against dependency keys.
    if let Ok(pkg_full) = std::fs::read_to_string(project_path.join("package.json"))
        && let Some(scripts_start) = pkg_full.find("\"scripts\"")
    {
        let after_key = &pkg_full[scripts_start..];
        if let Some(brace_pos) = after_key.find('{') {
            let obj = &after_key[brace_pos..];
            let mut depth = 0;
            let mut end = obj.len();
            for (i, ch) in obj.char_indices() {
                match ch {
                    '{' => depth += 1,
                    '}' => {
                        depth -= 1;
                        if depth == 0 {
                            end = i + 1;
                            break;
                        }
                    }
                    _ => {}
                }
            }
            let scripts_section = &obj[..end];
            for dep in &deps {
                if scripts_section.contains(dep.as_str()) {
                    used_deps.insert(dep.clone());
                }
            }
        }
    }

    // Find deps that are never imported
    let dead: Vec<String> = deps
        .into_iter()
        .filter(|d| !used_deps.contains(d))
        .collect();

    if dead.is_empty() {
        return vec![];
    }

    let dead_list = dead
        .iter()
        .map(|d| format!("  - {d}"))
        .collect::<Vec<_>>()
        .join("\n");

    vec![Finding {
        severity: Severity::Advise,
        kind: FindingKind::ProjectAdvice {
            advice_id: "DEAD_DEPENDENCIES".to_string(),
            question: format!(
                "{} dependencies in package.json are never imported in source files:\n{}\nThe LLM MUST remove these or ask the developer why they are present.",
                dead.len(),
                dead_list
            ),
        },
        file: Some(std::path::PathBuf::from("package.json")),
        line: None,
        message: format!(
            "{} unused dependencies detected in package.json.",
            dead.len()
        ),
    }]
}

/// Extract dependency names from package.json content.
/// Minimal parser - extracts keys from "dependencies" and "devDependencies".
fn extract_dependency_names(json_content: &str) -> Vec<String> {
    let mut deps = Vec::new();

    for section in ["\"dependencies\"", "\"devDependencies\""] {
        if let Some(section_start) = json_content.find(section) {
            // Find the opening brace of the object
            let after_key = &json_content[section_start + section.len()..];
            if let Some(brace_start) = after_key.find('{') {
                let obj_content = &after_key[brace_start + 1..];
                // Find the matching closing brace
                let mut depth = 1;
                let mut end = 0;
                for (i, ch) in obj_content.char_indices() {
                    match ch {
                        '{' => depth += 1,
                        '}' => {
                            depth -= 1;
                            if depth == 0 {
                                end = i;
                                break;
                            }
                        }
                        _ => {}
                    }
                }
                let obj_str = &obj_content[..end];
                // Extract keys (quoted strings before ':')
                let mut in_string = false;
                let mut key_start = None;
                let chars = obj_str.char_indices().peekable();
                for (i, ch) in chars {
                    match ch {
                        '"' if !in_string => {
                            in_string = true;
                            key_start = Some(i + 1);
                        }
                        '"' if in_string => {
                            in_string = false;
                            if let Some(start) = key_start {
                                let key = &obj_str[start..i];
                                // Check if next non-whitespace is ':'
                                let rest = obj_str[i + 1..].trim_start();
                                if rest.starts_with(':') {
                                    // Skip @types/* and @scope/name type definitions
                                    if !key.starts_with("@types/") {
                                        deps.push(key.to_string());
                                    }
                                }
                            }
                            key_start = None;
                        }
                        _ => {}
                    }
                }
            }
        }
    }

    deps
}

/// Check for documentation files.
fn check_documentation(project_path: &Path) -> Vec<Finding> {
    let has_readme = project_path.join("README.md").exists()
        || project_path.join("readme.md").exists()
        || project_path.join("README").exists()
        || project_path.join("README.txt").exists()
        || project_path.join("README.rst").exists();

    if !has_readme {
        return vec![Finding {
            severity: Severity::Advise,
            kind: FindingKind::ProjectAdvice {
                advice_id: "NO_README".to_string(),
                question: "No README file detected. The LLM MUST ask the developer: \"Do you want me to create a README.md for this project?\"".to_string(),
            },
            file: None,
            line: None,
            message: "No README file found in the project root.".to_string(),
        }];
    }

    vec![]
}

/// Analyze JavaScript/TypeScript stack for common issues.
fn analyze_js_stack(project_path: &Path) -> Vec<Finding> {
    let pkg_path = project_path.join("package.json");
    let pkg_content = match std::fs::read_to_string(&pkg_path) {
        Ok(c) => c,
        Err(_) => return vec![],
    };

    let mut findings = Vec::new();

    // Detect duplicate HTTP client libraries
    let http_clients: Vec<&str> = [
        "axios",
        "node-fetch",
        "got",
        "superagent",
        "request",
        "undici",
    ]
    .iter()
    .filter(|dep| pkg_content.contains(&format!("\"{}\"", dep)))
    .copied()
    .collect();

    if http_clients.len() > 1 {
        findings.push(Finding {
            severity: Severity::Advise,
            kind: FindingKind::ProjectAdvice {
                advice_id: "DUPLICATE_HTTP_CLIENTS".to_string(),
                question: format!(
                    "Multiple HTTP client libraries detected: {}. These all serve the same purpose. The LLM MUST ask the developer: \"You have {} HTTP client libraries. Which one should we standardize on?\"",
                    http_clients.join(", "),
                    http_clients.len()
                ),
            },
            file: Some(std::path::PathBuf::from("package.json")),
            line: None,
            message: format!(
                "Duplicate HTTP client libraries: {}.",
                http_clients.join(", ")
            ),
        });
    }

    // Detect deprecated packages
    let deprecated_deps: Vec<(&str, &str)> = [
        (
            "request",
            "DEPRECATED since Feb 2020 - use fetch(), axios, or got",
        ),
        (
            "moment",
            "In maintenance mode since Sep 2020 - consider dayjs, date-fns, or Intl API",
        ),
    ]
    .into_iter()
    .filter(|(dep, _)| {
        // Check it's actually a dependency key, not just mentioned anywhere
        pkg_content.contains(&format!("\"{dep}\""))
    })
    .collect();

    for (dep, reason) in &deprecated_deps {
        findings.push(Finding {
            severity: Severity::Advise,
            kind: FindingKind::ProjectAdvice {
                advice_id: format!("DEPRECATED_DEP_{}", dep.to_uppercase()),
                question: format!(
                    "Dependency '{}' is deprecated/unmaintained: {}. The LLM MUST ask the developer: \"Do you want me to migrate away from {}?\"",
                    dep, reason, dep
                ),
            },
            file: Some(std::path::PathBuf::from("package.json")),
            line: None,
            message: format!("Deprecated dependency: {} - {}", dep, reason),
        });
    }

    findings
}

/// Check if a path is inside an excluded directory.
fn is_excluded_dir(path: &Path) -> bool {
    path.components().any(|c| {
        if let std::path::Component::Normal(name) = c
            && let Some(name_str) = name.to_str()
        {
            return EXCLUDED_DIRS.contains(&name_str);
        }
        false
    })
}

/// Check if a JavaScript project has a mechanism to strip console.log in production.
/// This is NO_CONSOLE_STRIP from the spec - detects ABSENCE of any of:
/// - ESLint "no-console" rule
/// - Terser/esbuild/vite drop_console/pure_funcs config
/// - A structured logger (winston, pino, bunyan) replacing console.log
fn check_console_strip(project_path: &Path) -> Vec<Finding> {
    // Check ESLint configs for "no-console"
    let eslint_configs = [
        "eslint.config.js",
        "eslint.config.mjs",
        "eslint.config.cjs",
        "eslint.config.ts",
        ".eslintrc",
        ".eslintrc.js",
        ".eslintrc.cjs",
        ".eslintrc.json",
        ".eslintrc.yml",
        ".eslintrc.yaml",
    ];

    for config in &eslint_configs {
        let config_path = project_path.join(config);
        if let Ok(content) = std::fs::read_to_string(&config_path)
            && content.contains("no-console")
        {
            return vec![];
        }
    }

    // Check package.json for eslintConfig with no-console
    let pkg_path = project_path.join("package.json");
    if let Ok(pkg_content) = std::fs::read_to_string(&pkg_path) {
        if pkg_content.contains("no-console") {
            return vec![];
        }

        // Check for structured loggers in dependencies (replace console.log)
        let loggers = ["winston", "pino", "bunyan", "log4js", "signale"];
        for logger in &loggers {
            if pkg_content.contains(&format!("\"{logger}\"")) {
                return vec![];
            }
        }
    }

    // Check build configs for drop_console / pure_funcs
    // Note: esbuild uses "drop:" (no underscore), not "drop_console"
    let build_configs = [
        ("vite.config.js", "drop_console"),
        ("vite.config.ts", "drop_console"),
        ("vite.config.mjs", "drop_console"),
        ("vite.config.mts", "drop_console"),
        ("webpack.config.js", "drop_console"),
        ("webpack.config.ts", "drop_console"),
        ("next.config.js", "drop_console"),
        ("next.config.mjs", "drop_console"),
        ("next.config.ts", "drop_console"),
        ("esbuild.config.js", "drop"),
        ("esbuild.config.mjs", "drop"),
        ("rollup.config.js", "pure_funcs"),
        ("rollup.config.mjs", "pure_funcs"),
        // Also check for terser config patterns
        ("vite.config.js", "pure_funcs"),
        ("vite.config.ts", "pure_funcs"),
        ("webpack.config.js", "pure_funcs"),
        // esbuild uses "drop:" in Vite config (e.g., drop: ['console'])
        ("vite.config.js", "\"drop\""),
        ("vite.config.ts", "\"drop\""),
        ("vite.config.mjs", "\"drop\""),
        ("vite.config.mts", "\"drop\""),
        ("vite.config.js", "drop:"),
        ("vite.config.ts", "drop:"),
        ("vite.config.mjs", "drop:"),
        ("vite.config.mts", "drop:"),
    ];

    // Check root directory
    for (config_file, pattern) in &build_configs {
        let config_path = project_path.join(config_file);
        if let Ok(content) = std::fs::read_to_string(&config_path)
            && content.contains(pattern)
        {
            return vec![];
        }
    }

    // Also check common subdirectories (monorepos with frontend/ client/ etc.)
    let subdirs = ["frontend", "client", "app", "src"];
    for subdir in &subdirs {
        for (config_file, pattern) in &build_configs {
            let config_path = project_path.join(subdir).join(config_file);
            if let Ok(content) = std::fs::read_to_string(&config_path)
                && content.contains(pattern)
            {
                return vec![];
            }
        }
    }

    // No mechanism found - this is a WARN (not BLOCK, because some projects
    // may use other mechanisms we don't detect)
    vec![Finding {
        severity: Severity::Warn,
        kind: FindingKind::ProjectAdvice {
            advice_id: "NO_CONSOLE_STRIP".to_string(),
            question: "No mechanism to strip console.log in production detected. Options:\n  1. ESLint 'no-console' rule (eslint.config.js)\n  2. Build tool: terser drop_console, esbuild drop\n  3. Structured logger (winston, pino) replacing console.log\nThe LLM MUST ask the developer: \"How do you handle console.log in production?\"".to_string(),
        },
        file: None,
        line: None,
        message: "No console.log stripping mechanism detected - console.log statements will leak to production, potentially exposing sensitive data.".to_string(),
    }]
}

/// Detect if ai-rsk has been tampered with or bypassed.
/// Checks for common LLM bypass patterns in package.json and CI configs.
fn check_tamper_protection(project_path: &Path, ecosystems: &[Ecosystem]) -> Vec<Finding> {
    let mut findings = Vec::new();

    // Check package.json for bypass patterns (JS/TS ecosystem)
    if ecosystems.contains(&Ecosystem::JavaScript) {
        let pkg_path = project_path.join("package.json");
        if let Ok(content) = std::fs::read_to_string(&pkg_path) {
            // Detect || true, || exit 0, or stdout redirection after ai-rsk
            let bypass_patterns = [
                "ai-rsk scan || true",
                "ai-rsk scan --strict || true",
                "ai-rsk scan --full || true",
                "ai-rsk scan || exit 0",
                "ai-rsk scan > /dev/null",
                "ai-rsk scan 2>/dev/null",
                "ai-rsk scan &>/dev/null",
            ];

            for pattern in &bypass_patterns {
                if content.contains(pattern) {
                    findings.push(Finding {
                        severity: Severity::Block,
                        kind: FindingKind::ProjectAdvice {
                            advice_id: "AIRSK_BYPASSED".to_string(),
                            question: format!(
                                "ai-rsk security gate has been BYPASSED in package.json with '{}'. This defeats the entire purpose of the security gate. The developer MUST be notified - this change was likely made by the LLM to avoid fixing security issues.",
                                pattern
                            ),
                        },
                        file: Some(std::path::PathBuf::from("package.json")),
                        line: None,
                        message: format!("SECURITY BYPASS DETECTED: ai-rsk scan neutralized with '{}'.", pattern),
                    });
                    break;
                }
            }

            // Detect if ai-rsk was removed from prebuild entirely
            if content.contains("\"scripts\"") && !content.contains("ai-rsk") {
                // Only warn if we previously installed (check for SECURITY_RULES.md presence)
                if project_path.join("SECURITY_RULES.md").exists() {
                    findings.push(Finding {
                        severity: Severity::Warn,
                        kind: FindingKind::ProjectAdvice {
                            advice_id: "AIRSK_REMOVED_FROM_PREBUILD".to_string(),
                            question: "ai-rsk was previously installed (SECURITY_RULES.md exists) but is no longer in package.json scripts. This may indicate the LLM removed it to avoid fixing security issues.".to_string(),
                        },
                        file: Some(std::path::PathBuf::from("package.json")),
                        line: None,
                        message: "ai-rsk is not in package.json scripts but SECURITY_RULES.md exists - was it removed?".to_string(),
                    });
                }
            }
        }
    }

    // Check for --no-verify in git hooks or CI
    let ci_files = [".github/workflows", ".gitlab-ci.yml"];

    for ci_file in &ci_files {
        let ci_path = project_path.join(ci_file);
        if ci_path.is_dir() {
            // Scan workflow files
            for entry in WalkDir::new(&ci_path)
                .max_depth(2)
                .into_iter()
                .filter_map(|e| e.ok())
                .filter(|e| e.file_type().is_file())
            {
                if let Ok(content) = std::fs::read_to_string(entry.path()) {
                    if content.contains("--no-verify") {
                        findings.push(Finding {
                            severity: Severity::Warn,
                            kind: FindingKind::ProjectAdvice {
                                advice_id: "CI_NO_VERIFY".to_string(),
                                question: "--no-verify found in CI configuration. This skips git hooks including ai-rsk security checks.".to_string(),
                            },
                            file: Some(entry.path().strip_prefix(project_path).unwrap_or(entry.path()).to_path_buf()),
                            line: None,
                            message: "--no-verify in CI configuration bypasses security hooks.".to_string(),
                        });
                    }
                }
            }
        } else if ci_path.is_file() {
            if let Ok(content) = std::fs::read_to_string(&ci_path) {
                if content.contains("--no-verify") {
                    findings.push(Finding {
                        severity: Severity::Warn,
                        kind: FindingKind::ProjectAdvice {
                            advice_id: "CI_NO_VERIFY".to_string(),
                            question: "--no-verify found in CI configuration. This skips git hooks including ai-rsk security checks.".to_string(),
                        },
                        file: Some(std::path::PathBuf::from(ci_file)),
                        line: None,
                        message: "--no-verify in CI configuration bypasses security hooks.".to_string(),
                    });
                }
            }
        }
    }

    findings
}

/// Infra: Check if the project has a lockfile (package-lock.json, yarn.lock, pnpm-lock.yaml, etc.).
/// Without a lockfile, dependency versions are non-deterministic across environments.
fn check_lockfile(project_path: &Path, ecosystems: &[Ecosystem]) -> Vec<Finding> {
    let lockfiles: Vec<(&str, &[Ecosystem])> = vec![
        ("package-lock.json", &[Ecosystem::JavaScript]),
        ("yarn.lock", &[Ecosystem::JavaScript]),
        ("pnpm-lock.yaml", &[Ecosystem::JavaScript]),
        ("bun.lockb", &[Ecosystem::JavaScript]),
        ("Cargo.lock", &[Ecosystem::Rust]),
        ("go.sum", &[Ecosystem::Go]),
        ("poetry.lock", &[Ecosystem::Python]),
        ("Pipfile.lock", &[Ecosystem::Python]),
        ("uv.lock", &[Ecosystem::Python]),
        ("requirements.txt", &[Ecosystem::Python]),
    ];

    for (lockfile, applicable_ecosystems) in &lockfiles {
        if applicable_ecosystems.iter().any(|e| ecosystems.contains(e))
            && project_path.join(lockfile).exists()
        {
            return vec![];
        }
    }

    // Only warn if an ecosystem that uses lockfiles is detected
    let needs_lockfile = ecosystems.iter().any(|e| {
        matches!(
            e,
            Ecosystem::JavaScript | Ecosystem::Rust | Ecosystem::Go | Ecosystem::Python
        )
    });

    if !needs_lockfile {
        return vec![];
    }

    vec![Finding {
        severity: Severity::Warn,
        kind: FindingKind::ProjectAdvice {
            advice_id: "NO_LOCKFILE".to_string(),
            question: "No dependency lockfile found. Without a lockfile, dependency versions are non-deterministic — different environments may install different versions, leading to \"works on my machine\" bugs and potential supply chain attacks. Run your package manager's install command to generate one.".to_string(),
        },
        file: None,
        line: None,
        message: "No dependency lockfile found — builds are non-deterministic.".to_string(),
    }]
}

/// Infra: Check if .env is in .gitignore.
/// .env files contain secrets (API keys, DB passwords) and must never be committed.
fn check_env_gitignored(project_path: &Path) -> Vec<Finding> {
    // Only check if .env exists
    if !project_path.join(".env").exists() {
        return vec![];
    }

    let gitignore_path = project_path.join(".gitignore");
    if let Ok(content) = std::fs::read_to_string(&gitignore_path) {
        if content.lines().any(|line| {
            let trimmed = line.trim();
            trimmed == ".env" || trimmed == ".env*" || trimmed == ".env.*" || trimmed == "*.env"
        }) {
            return vec![];
        }
    }

    vec![Finding {
        severity: Severity::Block,
        kind: FindingKind::ProjectAdvice {
            advice_id: "ENV_NOT_GITIGNORED".to_string(),
            question: ".env file exists but is NOT listed in .gitignore. This means secrets (API keys, database passwords, tokens) will be committed to git history. Add .env to .gitignore IMMEDIATELY.".to_string(),
        },
        file: Some(std::path::PathBuf::from(".env")),
        line: None,
        message: ".env file exists but is not in .gitignore — secrets will be committed to git.".to_string(),
    }]
}

/// Infra: Check if Dockerfile runs as root (no USER directive).
/// Running containers as root is a security risk — if the container is compromised,
/// the attacker has root access to the host kernel's attack surface.
fn check_dockerfile_root(project_path: &Path) -> Vec<Finding> {
    let dockerfile_names = ["Dockerfile", "dockerfile", "Containerfile"];

    for name in &dockerfile_names {
        let path = project_path.join(name);
        if let Ok(content) = std::fs::read_to_string(&path) {
            // Check if USER directive exists (case-insensitive line start)
            let has_user = content.lines().any(|line| {
                let trimmed = line.trim();
                trimmed.starts_with("USER ") || trimmed.starts_with("user ")
            });

            if !has_user {
                return vec![Finding {
                    severity: Severity::Warn,
                    kind: FindingKind::ProjectAdvice {
                        advice_id: "DOCKERFILE_ROOT_USER".to_string(),
                        question: format!(
                            "{} has no USER directive — the container runs as root. If compromised, an attacker has root-level access. Add a non-root user: USER 1001 or USER appuser.",
                            name
                        ),
                    },
                    file: Some(std::path::PathBuf::from(name)),
                    line: None,
                    message: format!("{} runs as root — no USER directive found.", name),
                }];
            }
        }
    }

    vec![]
}

/// SEO: Check if the project has a robots.txt file.
/// robots.txt tells search engines which pages to crawl and which to skip.
fn check_robots_txt(project_path: &Path) -> Vec<Finding> {
    // Check common locations
    let locations = ["robots.txt", "public/robots.txt", "static/robots.txt"];
    for loc in &locations {
        if project_path.join(loc).exists() {
            // Also check if robots.txt exposes sensitive paths
            if let Ok(content) = std::fs::read_to_string(project_path.join(loc)) {
                let content_lower = content.to_lowercase();
                let sensitive_patterns = [
                    "/admin",
                    "/api/",
                    "/dashboard",
                    "/internal",
                    "/staging",
                    "/debug",
                    "/phpMyAdmin",
                    "/wp-admin",
                ];
                for pattern in &sensitive_patterns {
                    if content_lower.contains(&format!("disallow: {}", pattern.to_lowercase())) {
                        return vec![Finding {
                            severity: Severity::Warn,
                            kind: FindingKind::ProjectAdvice {
                                advice_id: "ROBOTS_EXPOSES_SENSITIVE".to_string(),
                                question: format!(
                                    "robots.txt contains Disallow for '{}'. While this prevents crawling, it REVEALS the existence of sensitive paths to anyone reading robots.txt. Use authentication and access controls instead of relying on robots.txt for security.",
                                    pattern
                                ),
                            },
                            file: Some(std::path::PathBuf::from(loc)),
                            line: None,
                            message: format!("robots.txt reveals sensitive path: {}", pattern),
                        }];
                    }
                }
            }
            return vec![];
        }
    }

    vec![Finding {
        severity: Severity::Advise,
        kind: FindingKind::ProjectAdvice {
            advice_id: "NO_ROBOTS_TXT".to_string(),
            question: "No robots.txt file found. Search engines need robots.txt to know which pages to crawl. The LLM MUST ask: \"Do you want me to create a robots.txt?\"".to_string(),
        },
        file: None,
        line: None,
        message: "No robots.txt file found at the project root.".to_string(),
    }]
}

/// SEO: Check if the project has a sitemap.xml file.
fn check_sitemap(project_path: &Path) -> Vec<Finding> {
    let locations = [
        "sitemap.xml",
        "public/sitemap.xml",
        "static/sitemap.xml",
        "sitemap.xml.gz",
        "public/sitemap.xml.gz",
    ];
    for loc in &locations {
        if project_path.join(loc).exists() {
            return vec![];
        }
    }

    // Also check if robots.txt references a sitemap
    let robots_locations = ["robots.txt", "public/robots.txt", "static/robots.txt"];
    for loc in &robots_locations {
        if let Ok(content) = std::fs::read_to_string(project_path.join(loc)) {
            if content.to_lowercase().contains("sitemap:") {
                return vec![];
            }
        }
    }

    vec![Finding {
        severity: Severity::Advise,
        kind: FindingKind::ProjectAdvice {
            advice_id: "NO_SITEMAP".to_string(),
            question: "No sitemap.xml found. A sitemap helps search engines discover all pages on your site. The LLM MUST ask: \"Do you want me to create a sitemap.xml?\"".to_string(),
        },
        file: None,
        line: None,
        message: "No sitemap.xml found at the project root.".to_string(),
    }]
}

/// SEO: Check if HTML files have a meta viewport tag for mobile rendering.
fn check_meta_viewport(project_path: &Path) -> Vec<Finding> {
    let html_extensions = ["html", "htm"];
    let mut has_html = false;
    let mut has_viewport = false;

    for entry in WalkDir::new(project_path)
        .max_depth(5)
        .into_iter()
        .filter_map(|e| e.ok())
        .filter(|e| e.file_type().is_file())
        .filter(|e| !is_excluded_dir(e.path()))
    {
        let ext = entry
            .path()
            .extension()
            .and_then(|e| e.to_str())
            .unwrap_or("");
        if !html_extensions.contains(&ext) {
            continue;
        }
        has_html = true;

        if let Ok(content) = std::fs::read_to_string(entry.path()) {
            if content.to_lowercase().contains("viewport") {
                has_viewport = true;
                break;
            }
        }
    }

    if has_html && !has_viewport {
        vec![Finding {
            severity: Severity::Advise,
            kind: FindingKind::ProjectAdvice {
                advice_id: "NO_META_VIEWPORT".to_string(),
                question: "HTML files found but no meta viewport tag detected. Without it, mobile devices render the page at desktop width. Add: <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">".to_string(),
            },
            file: None,
            line: None,
            message: "No meta viewport tag found in HTML files — mobile rendering will be broken.".to_string(),
        }]
    } else {
        vec![]
    }
}

/// SEO: Check if HTML files have canonical URLs to prevent duplicate content.
fn check_canonical(project_path: &Path) -> Vec<Finding> {
    let html_extensions = ["html", "htm"];
    let mut has_html = false;
    let mut has_canonical = false;

    for entry in WalkDir::new(project_path)
        .max_depth(5)
        .into_iter()
        .filter_map(|e| e.ok())
        .filter(|e| e.file_type().is_file())
        .filter(|e| !is_excluded_dir(e.path()))
    {
        let ext = entry
            .path()
            .extension()
            .and_then(|e| e.to_str())
            .unwrap_or("");
        if !html_extensions.contains(&ext) {
            continue;
        }
        has_html = true;

        if let Ok(content) = std::fs::read_to_string(entry.path()) {
            let lower = content.to_lowercase();
            if lower.contains("rel=\"canonical\"") || lower.contains("rel='canonical'") {
                has_canonical = true;
                break;
            }
        }
    }

    if has_html && !has_canonical {
        vec![Finding {
            severity: Severity::Advise,
            kind: FindingKind::ProjectAdvice {
                advice_id: "NO_CANONICAL".to_string(),
                question: "HTML files found but no canonical URL tag detected. Without canonical tags, search engines may index duplicate content. Add: <link rel=\"canonical\" href=\"https://yoursite.com/page\">".to_string(),
            },
            file: None,
            line: None,
            message: "No canonical URL tags found in HTML files — risk of duplicate content in search results.".to_string(),
        }]
    } else {
        vec![]
    }
}

/// GDPR: Check if the project has a privacy policy page.
/// Looks for files or routes named privacy, privacy-policy, politique-de-confidentialite, etc.
/// Also checks for links to a privacy page in HTML/JSX files.
/// Severity: ADVISE — the project should have a privacy page if it collects any user data.
fn check_privacy_page(project_path: &Path) -> Vec<Finding> {
    // Check for privacy page files (static sites, Next.js pages, etc.)
    let privacy_file_patterns = [
        // English
        "privacy",
        "privacy-policy",
        "privacy_policy",
        "privacypolicy",
        // French
        "politique-de-confidentialite",
        "politique_confidentialite",
        "confidentialite",
        // German
        "datenschutz",
        // Spanish
        "politica-de-privacidad",
        // Legal catch-all
        "legal",
        "mentions-legales",
        "mentions_legales",
    ];

    let source_extensions = [
        "html", "htm", "jsx", "tsx", "js", "ts", "vue", "svelte", "md", "mdx",
    ];

    for entry in WalkDir::new(project_path)
        .max_depth(6)
        .into_iter()
        .filter_map(|e| e.ok())
        .filter(|e| e.file_type().is_file())
        .filter(|e| !is_excluded_dir(e.path()))
    {
        let file_name = entry
            .path()
            .file_stem()
            .and_then(|s| s.to_str())
            .unwrap_or("")
            .to_lowercase();

        // Check if filename matches a privacy page pattern
        for pattern in &privacy_file_patterns {
            if file_name == *pattern {
                return vec![];
            }
        }
    }

    // Check for links to privacy pages in source files
    let link_patterns = [
        "/privacy",
        "/privacy-policy",
        "/politique-de-confidentialite",
        "/confidentialite",
        "/datenschutz",
        "/legal",
        "/mentions-legales",
        "privacy.html",
        "privacy-policy.html",
    ];

    for entry in WalkDir::new(project_path)
        .max_depth(6)
        .into_iter()
        .filter_map(|e| e.ok())
        .filter(|e| e.file_type().is_file())
        .filter(|e| !is_excluded_dir(e.path()))
    {
        let ext = entry
            .path()
            .extension()
            .and_then(|e| e.to_str())
            .unwrap_or("");
        if !source_extensions.contains(&ext) {
            continue;
        }

        let content = match std::fs::read_to_string(entry.path()) {
            Ok(c) => c,
            Err(_) => continue,
        };

        let content_lower = content.to_lowercase();
        for pattern in &link_patterns {
            if content_lower.contains(pattern) {
                return vec![];
            }
        }
    }

    vec![Finding {
        severity: Severity::Advise,
        kind: FindingKind::ProjectAdvice {
            advice_id: "NO_PRIVACY_PAGE".to_string(),
            question: "No privacy policy page detected in the project. Under RGPD/GDPR Article 13, you MUST inform users about data collection, processing purposes, data retention, and their rights (access, rectification, erasure). The LLM MUST ask the developer: \"Do you want me to create a privacy policy page? I'll need to know: what data you collect, why, how long you keep it, and who has access.\"".to_string(),
        },
        file: None,
        line: None,
        message: "No privacy policy page found. RGPD/GDPR Article 13 requires informing users about data processing.".to_string(),
    }]
}

/// GDPR: Check if the project has tracking scripts but no cookie consent banner/CMP.
/// This is a project-level check: if ANY tracking is detected in the codebase,
/// there MUST be a consent management mechanism present somewhere.
/// Severity: WARN (the tracking scripts themselves are caught by TRACKING_NO_CONSENT rule as BLOCK).
fn check_cookie_banner(project_path: &Path) -> Vec<Finding> {
    let tracking_patterns = [
        "gtag(",
        "googletagmanager.com",
        "GoogleAnalyticsObject",
        "fbq(",
        "_paq.push",
        "hotjar.com",
    ];

    let cmp_patterns = [
        // CMP libraries and services
        "cookiebot",
        "CookieBot",
        "onetrust",
        "OneTrust",
        "cookieyes",
        "CookieYes",
        "cookie-script",
        "cookie_script",
        "tarteaucitron",
        "klaro",
        "Klaro",
        "osano",
        "Osano",
        "didomi",
        "Didomi",
        "axeptio",
        "Axeptio",
        "secureprivacy",
        // Custom cookie banner components/patterns
        "cookie-banner",
        "cookie-consent",
        "cookieBanner",
        "cookieConsent",
        "CookieBanner",
        "CookieConsent",
        "gdpr-banner",
        "gdprBanner",
        "consent-banner",
        "consentBanner",
        "ConsentBanner",
        // Google Consent Mode
        "consent', 'default",
        "consent\", \"default",
    ];

    let mut has_tracking = false;
    let mut has_cmp = false;

    let extensions = [
        "html", "js", "jsx", "ts", "tsx", "mjs", "cjs", "vue", "svelte",
    ];

    for entry in WalkDir::new(project_path)
        .max_depth(10)
        .into_iter()
        .filter_map(|e| e.ok())
        .filter(|e| e.file_type().is_file())
        .filter(|e| !is_excluded_dir(e.path()))
    {
        let ext = entry
            .path()
            .extension()
            .and_then(|e| e.to_str())
            .unwrap_or("");
        if !extensions.contains(&ext) {
            continue;
        }

        let content = match std::fs::read_to_string(entry.path()) {
            Ok(c) => c,
            Err(_) => continue,
        };

        if !has_tracking {
            for pattern in &tracking_patterns {
                if content.contains(pattern) {
                    has_tracking = true;
                    break;
                }
            }
        }

        if !has_cmp {
            for pattern in &cmp_patterns {
                if content.contains(pattern) {
                    has_cmp = true;
                    break;
                }
            }
        }

        // Early exit if both found
        if has_tracking && has_cmp {
            return vec![];
        }
    }

    // Also check package.json for CMP dependencies
    if !has_cmp {
        let pkg_path = project_path.join("package.json");
        if let Ok(content) = std::fs::read_to_string(&pkg_path) {
            let cmp_deps = [
                "react-cookie-consent",
                "cookie-consent",
                "vue-cookie-comply",
                "@cookiebot",
                "onetrust",
                "tarteaucitron",
                "klaro",
                "osano",
                "didomi",
            ];
            for dep in &cmp_deps {
                if content.contains(dep) {
                    has_cmp = true;
                    break;
                }
            }
        }
    }

    if has_tracking && !has_cmp {
        vec![Finding {
            severity: Severity::Warn,
            kind: FindingKind::ProjectAdvice {
                advice_id: "NO_COOKIE_BANNER".to_string(),
                question: "Tracking scripts detected (gtag, Facebook Pixel, Matomo, or Hotjar) but no Cookie Consent Management Platform (CMP) found. Under RGPD/GDPR and ePrivacy Directive, a cookie consent banner is REQUIRED before any non-essential cookies are set. The LLM MUST ask the developer: \"Which CMP do you want to use? Options: CookieBot, OneTrust, CookieYes, tarteaucitron (free), Klaro (free), Osano, Didomi, Axeptio.\"".to_string(),
            },
            file: None,
            line: None,
            message: "Tracking scripts detected but no cookie consent banner/CMP found. RGPD/GDPR requires explicit user consent before setting analytics cookies.".to_string(),
        }]
    } else {
        vec![]
    }
}

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

    #[test]
    fn test_no_tests_detected() {
        let dir = TempDir::new().expect("create temp dir");
        fs::write(dir.path().join("index.js"), "console.log('hello');").expect("write file");

        let findings = check_tests(dir.path(), &[Ecosystem::JavaScript]);
        assert_eq!(findings.len(), 1);
        assert_eq!(findings[0].severity, Severity::Advise);
        assert!(findings[0].message.contains("No test framework"));
    }

    #[test]
    fn test_tests_detected_by_config() {
        let dir = TempDir::new().expect("create temp dir");
        fs::write(dir.path().join("vitest.config.ts"), "export default {}").expect("write file");

        let findings = check_tests(dir.path(), &[Ecosystem::JavaScript]);
        assert!(findings.is_empty());
    }

    #[test]
    fn test_tests_detected_by_directory() {
        let dir = TempDir::new().expect("create temp dir");
        let test_dir = dir.path().join("tests");
        fs::create_dir(&test_dir).expect("create test dir");
        fs::write(test_dir.join("auth.test.js"), "test('works', () => {})").expect("write file");

        let findings = check_tests(dir.path(), &[Ecosystem::JavaScript]);
        assert!(findings.is_empty());
    }

    #[test]
    fn test_tests_detected_by_spec_file() {
        let dir = TempDir::new().expect("create temp dir");
        let src_dir = dir.path().join("src");
        fs::create_dir(&src_dir).expect("create src dir");
        fs::write(src_dir.join("auth.spec.ts"), "describe('auth', () => {})").expect("write file");

        let findings = check_tests(dir.path(), &[Ecosystem::JavaScript]);
        assert!(findings.is_empty());
    }

    #[test]
    fn test_tests_detected_rust_cfg_test() {
        let dir = TempDir::new().expect("create temp dir");
        let src_dir = dir.path().join("src");
        fs::create_dir(&src_dir).expect("create src dir");
        fs::write(
            src_dir.join("main.rs"),
            "#[cfg(test)]\nmod tests { #[test] fn it_works() {} }",
        )
        .expect("write file");

        let findings = check_tests(dir.path(), &[Ecosystem::Rust]);
        assert!(findings.is_empty());
    }

    #[test]
    fn test_tests_detected_python() {
        let dir = TempDir::new().expect("create temp dir");
        fs::write(dir.path().join("test_auth.py"), "def test_login(): pass").expect("write file");

        let findings = check_tests(dir.path(), &[Ecosystem::Python]);
        assert!(findings.is_empty());
    }

    #[test]
    fn test_tests_detected_go() {
        let dir = TempDir::new().expect("create temp dir");
        fs::write(
            dir.path().join("auth_test.go"),
            "func TestLogin(t *testing.T) {}",
        )
        .expect("write file");

        let findings = check_tests(dir.path(), &[Ecosystem::Go]);
        assert!(findings.is_empty());
    }

    #[test]
    fn test_no_ci_cd() {
        let dir = TempDir::new().expect("create temp dir");

        let findings = check_ci_cd(dir.path());
        assert_eq!(findings.len(), 1);
        assert!(findings[0].message.contains("No CI/CD"));
    }

    #[test]
    fn test_ci_cd_github_actions() {
        let dir = TempDir::new().expect("create temp dir");
        let workflows = dir.path().join(".github").join("workflows");
        fs::create_dir_all(&workflows).expect("create workflows dir");
        fs::write(workflows.join("ci.yml"), "name: CI").expect("write file");

        let findings = check_ci_cd(dir.path());
        assert!(findings.is_empty());
    }

    #[test]
    fn test_ci_cd_gitlab() {
        let dir = TempDir::new().expect("create temp dir");
        fs::write(dir.path().join(".gitlab-ci.yml"), "stages: [build]").expect("write file");

        let findings = check_ci_cd(dir.path());
        assert!(findings.is_empty());
    }

    #[test]
    fn test_ci_cd_empty_workflows_dir_not_counted() {
        let dir = TempDir::new().expect("create temp dir");
        let workflows = dir.path().join(".github").join("workflows");
        fs::create_dir_all(&workflows).expect("create workflows dir");
        // Empty directory - no workflow files

        let findings = check_ci_cd(dir.path());
        assert_eq!(findings.len(), 1); // Empty dir doesn't count
    }

    #[test]
    fn test_no_readme() {
        let dir = TempDir::new().expect("create temp dir");
        fs::write(dir.path().join("index.js"), "").expect("write file");

        let findings = check_documentation(dir.path());
        assert_eq!(findings.len(), 1);
        assert!(findings[0].message.contains("No README"));
    }

    #[test]
    fn test_readme_present() {
        let dir = TempDir::new().expect("create temp dir");
        fs::write(dir.path().join("README.md"), "# My Project").expect("write file");

        let findings = check_documentation(dir.path());
        assert!(findings.is_empty());
    }

    #[test]
    fn test_dead_deps_detection() {
        let dir = TempDir::new().expect("create temp dir");
        let pkg = r#"{
  "dependencies": {
    "express": "^4.18.0",
    "lodash": "^4.17.21"
  }
}"#;
        fs::write(dir.path().join("package.json"), pkg).expect("write pkg");

        let src_dir = dir.path().join("src");
        fs::create_dir(&src_dir).expect("create src");
        fs::write(
            src_dir.join("index.js"),
            "const express = require('express');\nconst app = express();",
        )
        .expect("write src");

        let findings = check_dead_deps_js(dir.path());
        assert_eq!(findings.len(), 1);
        assert!(findings[0].message.contains("1 unused"));
    }

    #[test]
    fn test_no_dead_deps() {
        let dir = TempDir::new().expect("create temp dir");
        let pkg = r#"{
  "dependencies": {
    "express": "^4.18.0"
  }
}"#;
        fs::write(dir.path().join("package.json"), pkg).expect("write pkg");

        let src_dir = dir.path().join("src");
        fs::create_dir(&src_dir).expect("create src");
        fs::write(src_dir.join("index.js"), "import express from 'express';").expect("write src");

        let findings = check_dead_deps_js(dir.path());
        assert!(findings.is_empty());
    }

    #[test]
    fn test_duplicate_http_clients() {
        let dir = TempDir::new().expect("create temp dir");
        let pkg = r#"{
  "dependencies": {
    "axios": "^1.0.0",
    "node-fetch": "^3.0.0",
    "express": "^4.18.0"
  }
}"#;
        fs::write(dir.path().join("package.json"), pkg).expect("write pkg");

        let findings = analyze_js_stack(dir.path());
        let http_finding = findings
            .iter()
            .find(|f| matches!(&f.kind, FindingKind::ProjectAdvice { advice_id, .. } if advice_id == "DUPLICATE_HTTP_CLIENTS"));
        assert!(http_finding.is_some());
    }

    #[test]
    fn test_deprecated_dep_request() {
        let dir = TempDir::new().expect("create temp dir");
        let pkg = r#"{
  "dependencies": {
    "request": "^2.88.0",
    "express": "^4.18.0"
  }
}"#;
        fs::write(dir.path().join("package.json"), pkg).expect("write pkg");

        let findings = analyze_js_stack(dir.path());
        let deprecated = findings.iter().find(|f| {
            matches!(&f.kind, FindingKind::ProjectAdvice { advice_id, .. } if advice_id == "DEPRECATED_DEP_REQUEST")
        });
        assert!(deprecated.is_some());
    }

    #[test]
    fn test_extract_dependency_names() {
        let json = r#"{
  "name": "test",
  "dependencies": {
    "express": "^4.18.0",
    "@nestjs/core": "^10.0.0"
  },
  "devDependencies": {
    "vitest": "^1.0.0",
    "@types/node": "^20.0.0"
  }
}"#;
        let deps = extract_dependency_names(json);
        assert!(deps.contains(&"express".to_string()));
        assert!(deps.contains(&"@nestjs/core".to_string()));
        assert!(deps.contains(&"vitest".to_string()));
        // @types/* should be excluded
        assert!(!deps.contains(&"@types/node".to_string()));
    }

    #[test]
    fn test_extract_dependency_names_empty() {
        let json = r#"{ "name": "test" }"#;
        let deps = extract_dependency_names(json);
        assert!(deps.is_empty());
    }

    #[test]
    fn test_scoped_dep_import_detected() {
        let dir = TempDir::new().expect("create temp dir");
        let pkg = r#"{
  "dependencies": {
    "@nestjs/core": "^10.0.0",
    "@nestjs/common": "^10.0.0"
  }
}"#;
        fs::write(dir.path().join("package.json"), pkg).expect("write pkg");

        let src_dir = dir.path().join("src");
        fs::create_dir(&src_dir).expect("create src");
        fs::write(
            src_dir.join("main.ts"),
            "import { NestFactory } from '@nestjs/core';",
        )
        .expect("write src");

        let findings = check_dead_deps_js(dir.path());
        // @nestjs/core is used, @nestjs/common is not
        assert_eq!(findings.len(), 1);
        assert!(findings[0].message.contains("1 unused"));
    }

    #[test]
    fn test_full_analyze_empty_project() {
        let dir = TempDir::new().expect("create temp dir");
        fs::write(dir.path().join("package.json"), "{}").expect("write pkg");

        let findings = analyze_project(
            dir.path(),
            &[Ecosystem::JavaScript],
            &["security".to_string()],
        );
        // Should have at least: NO_TESTS, NO_CI_CD, NO_README
        let advice_ids: Vec<&str> = findings
            .iter()
            .filter_map(|f| match &f.kind {
                FindingKind::ProjectAdvice { advice_id, .. } => Some(advice_id.as_str()),
                _ => None,
            })
            .collect();

        assert!(advice_ids.contains(&"NO_TESTS"));
        assert!(advice_ids.contains(&"NO_CI_CD"));
        assert!(advice_ids.contains(&"NO_README"));
    }

    #[test]
    fn test_full_analyze_complete_project() {
        let dir = TempDir::new().expect("create temp dir");

        // Has package.json with deps
        let pkg = r#"{ "dependencies": { "express": "^4.18.0" } }"#;
        fs::write(dir.path().join("package.json"), pkg).expect("write pkg");

        // Has README
        fs::write(dir.path().join("README.md"), "# Project").expect("write readme");

        // Has tests
        fs::write(dir.path().join("vitest.config.ts"), "export default {}").expect("write vitest");

        // Has CI
        let workflows = dir.path().join(".github").join("workflows");
        fs::create_dir_all(&workflows).expect("create workflows");
        fs::write(workflows.join("ci.yml"), "name: CI").expect("write ci");

        // Has ESLint config with no-console (console strip mechanism)
        fs::write(
            dir.path().join("eslint.config.js"),
            r#"export default [{ rules: { "no-console": "error" } }]"#,
        )
        .expect("write eslint");

        // Has source that uses express
        let src = dir.path().join("src");
        fs::create_dir(&src).expect("create src");
        fs::write(src.join("app.js"), "const express = require('express');").expect("write src");

        let findings = analyze_project(
            dir.path(),
            &[Ecosystem::JavaScript],
            &["security".to_string()],
        );

        // Well-configured project should have no advisories
        // (except maybe deprecated deps if we had any)
        let structural_advices: Vec<&Finding> = findings
            .iter()
            .filter(|f| {
                matches!(
                    &f.kind,
                    FindingKind::ProjectAdvice { advice_id, .. }
                    if matches!(advice_id.as_str(), "NO_TESTS" | "NO_CI_CD" | "NO_README" | "DEAD_DEPENDENCIES" | "NO_CONSOLE_STRIP")
                )
            })
            .collect();

        assert!(
            structural_advices.is_empty(),
            "Complete project should not have structural advisories, got: {:?}",
            structural_advices
                .iter()
                .map(|f| &f.message)
                .collect::<Vec<_>>()
        );
    }

    // ─── NO_CONSOLE_STRIP tests ───

    #[test]
    fn test_no_console_strip_detected() {
        let dir = TempDir::new().expect("create temp dir");
        // JS project without any console strip mechanism
        fs::write(
            dir.path().join("package.json"),
            r#"{ "dependencies": { "express": "^4.18.0" } }"#,
        )
        .expect("write pkg");

        let findings = check_console_strip(dir.path());
        assert_eq!(findings.len(), 1);
        assert!(findings[0].message.contains("console.log stripping"));
    }

    #[test]
    fn test_console_strip_eslint_no_console() {
        let dir = TempDir::new().expect("create temp dir");
        fs::write(
            dir.path().join("package.json"),
            r#"{ "dependencies": { "express": "^4.18.0" } }"#,
        )
        .expect("write pkg");
        fs::write(
            dir.path().join("eslint.config.js"),
            r#"export default [{ rules: { "no-console": "error" } }]"#,
        )
        .expect("write eslint");

        let findings = check_console_strip(dir.path());
        assert!(findings.is_empty());
    }

    #[test]
    fn test_console_strip_structured_logger() {
        let dir = TempDir::new().expect("create temp dir");
        fs::write(
            dir.path().join("package.json"),
            r#"{ "dependencies": { "express": "^4.18.0", "pino": "^8.0.0" } }"#,
        )
        .expect("write pkg");

        let findings = check_console_strip(dir.path());
        assert!(findings.is_empty());
    }

    #[test]
    fn test_console_strip_vite_drop_console() {
        let dir = TempDir::new().expect("create temp dir");
        fs::write(
            dir.path().join("package.json"),
            r#"{ "dependencies": { "express": "^4.18.0" } }"#,
        )
        .expect("write pkg");
        fs::write(
            dir.path().join("vite.config.ts"),
            r#"export default defineConfig({ build: { terserOptions: { compress: { drop_console: true } } } })"#,
        )
        .expect("write vite");

        let findings = check_console_strip(dir.path());
        assert!(findings.is_empty());
    }
}