decy 2.2.0

CLI tool for C-to-Rust transpilation with EXTREME quality standards
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
//! CLI tool for C-to-Rust transpilation with EXTREME quality standards.

#![warn(clippy::all)]
#![deny(unsafe_code)]

#[macro_use]
#[allow(unused_macros)]
mod generated_contracts;

mod oracle_integration;
mod repl;

use oracle_integration::{OracleOptions, OracleTranspileResult};

use anyhow::{Context, Result};
use clap::{Parser, Subcommand};
use std::fs;
use std::path::PathBuf;

/// Decy: C-to-Rust Transpiler with EXTREME Quality Standards
#[derive(Parser, Debug)]
#[command(name = "decy")]
#[command(version = "0.2.0")]
#[command(about = "Transpile C code to safe Rust with minimal unsafe blocks", long_about = None)]
struct Cli {
    #[command(subcommand)]
    command: Option<Commands>,
}

#[derive(Subcommand, Debug)]
enum Commands {
    /// Transpile a C source file to Rust
    Transpile {
        /// Path to the C source file
        #[arg(value_name = "FILE")]
        input: PathBuf,

        /// Output file (default: stdout)
        #[arg(short, long, value_name = "FILE")]
        output: Option<PathBuf>,

        /// DECY-193: Emit JSON decision trace to stderr
        #[arg(long)]
        trace: bool,

        /// Enable CITL oracle for error correction
        #[arg(long)]
        oracle: bool,

        /// Confidence threshold for oracle suggestions (0.0-1.0)
        #[arg(long, default_value = "0.7")]
        oracle_threshold: f32,

        /// Automatically apply oracle fixes
        #[arg(long)]
        auto_fix: bool,

        /// Capture verified fix patterns for learning
        #[arg(long)]
        capture: bool,

        /// Import patterns from another .apr file (cross-project transfer)
        #[arg(long, value_name = "FILE")]
        import_patterns: Option<PathBuf>,

        /// Output oracle metrics report (json, markdown, prometheus)
        #[arg(long, value_name = "FORMAT")]
        oracle_report: Option<String>,

        /// Verify that generated Rust compiles (runs rustc type-check)
        #[arg(long)]
        verify: bool,
    },
    /// Transpile an entire C project (directory)
    TranspileProject {
        /// Path to the project directory
        #[arg(value_name = "DIR")]
        input: PathBuf,

        /// Output directory for transpiled files
        #[arg(short, long, value_name = "DIR")]
        output: PathBuf,

        /// Disable caching (default: enabled)
        #[arg(long)]
        no_cache: bool,

        /// Show verbose output (per-file progress)
        #[arg(short, long, conflicts_with = "quiet")]
        verbose: bool,

        /// Suppress progress output
        #[arg(short, long, conflicts_with = "verbose")]
        quiet: bool,

        /// Show what would be done without writing files
        #[arg(long)]
        dry_run: bool,

        /// Show summary statistics after transpilation
        #[arg(long)]
        stats: bool,

        /// Enable CITL oracle for error correction
        #[arg(long)]
        oracle: bool,

        /// Confidence threshold for oracle suggestions (0.0-1.0)
        #[arg(long, default_value = "0.7")]
        oracle_threshold: f32,

        /// Automatically apply oracle fixes
        #[arg(long)]
        auto_fix: bool,

        /// Capture verified fix patterns for learning
        #[arg(long)]
        capture: bool,

        /// Import patterns from another .apr file (cross-project transfer)
        #[arg(long, value_name = "FILE")]
        import_patterns: Option<PathBuf>,

        /// Output oracle metrics report (json, markdown, prometheus)
        #[arg(long, value_name = "FORMAT")]
        oracle_report: Option<String>,
    },
    /// Check project and show build order (dry-run)
    CheckProject {
        /// Path to the project directory
        #[arg(value_name = "DIR")]
        input: PathBuf,
    },
    /// Show cache statistics for a project
    CacheStats {
        /// Path to the project directory
        #[arg(value_name = "DIR")]
        input: PathBuf,
    },
    /// Start interactive REPL mode
    Repl,
    /// Audit unsafe code in Rust files
    Audit {
        /// Path to the Rust source file to audit
        #[arg(value_name = "FILE")]
        input: PathBuf,

        /// Show detailed information for each unsafe block
        #[arg(short, long)]
        verbose: bool,
    },
    /// Differential test: compile C with gcc and transpiled Rust with rustc, run both, compare outputs
    DiffTest {
        /// Path to the C source file
        #[arg(value_name = "FILE")]
        input: PathBuf,

        /// Timeout in seconds for each binary execution
        #[arg(long, default_value = "5")]
        timeout: u64,
    },
    /// Oracle management commands
    Oracle {
        #[command(subcommand)]
        action: OracleAction,
    },
}

#[derive(Subcommand, Debug)]
enum OracleAction {
    /// Bootstrap oracle with seed patterns for cold start
    Bootstrap {
        /// Show available patterns without saving
        #[arg(long)]
        dry_run: bool,
    },
    /// Seed oracle with patterns from another project
    Seed {
        /// Path to .apr file to import from (e.g., depyler patterns)
        #[arg(long, value_name = "FILE")]
        from: PathBuf,
    },
    /// Show oracle statistics
    Stats {
        /// Output format: json, markdown, prometheus
        #[arg(long, default_value = "markdown")]
        format: String,
    },
    /// Retire obsolete patterns
    Retire {
        /// Preview retirements without applying
        #[arg(long)]
        dry_run: bool,

        /// Archive retired patterns to this path
        #[arg(long, value_name = "FILE")]
        archive_path: Option<PathBuf>,
    },
    /// Validate oracle on a corpus
    Validate {
        /// Path to corpus directory
        #[arg(value_name = "DIR")]
        corpus: PathBuf,
    },
    /// Export patterns to dataset format for HuggingFace
    Export {
        /// Output file path
        #[arg(value_name = "FILE")]
        output: PathBuf,
        /// Export format: jsonl, chatml, alpaca, parquet
        #[arg(long, default_value = "jsonl")]
        format: String,
        /// Also generate dataset card (README.md)
        #[arg(long)]
        with_card: bool,
    },
    /// Train oracle on a C corpus using CITL feedback loop
    Train {
        /// Path to corpus directory containing C files
        #[arg(long, value_name = "DIR")]
        corpus: PathBuf,
        /// Training tier: P0 (simple), P1 (I/O), P2 (complex)
        #[arg(long, default_value = "P0")]
        tier: String,
        /// Preview training without saving patterns
        #[arg(long)]
        dry_run: bool,
    },
    /// Generate Golden Traces from C corpus for model training
    GenerateTraces {
        /// Path to corpus directory containing C files
        #[arg(long, value_name = "DIR")]
        corpus: PathBuf,
        /// Output file path for JSONL traces
        #[arg(long, value_name = "FILE")]
        output: PathBuf,
        /// Training tier: P0 (simple), P1 (I/O), P2 (complex)
        #[arg(long, default_value = "P0")]
        tier: String,
        /// Preview generation without writing output
        #[arg(long)]
        dry_run: bool,
    },
    /// Query oracle for fix patterns for a specific error code
    Query {
        /// Rustc error code (e.g., E0308, E0382)
        #[arg(long, value_name = "CODE")]
        error: String,
        /// Optional context for better pattern matching
        #[arg(long, value_name = "CONTEXT")]
        context: Option<String>,
        /// Output format: text, json
        #[arg(long, default_value = "text")]
        format: String,
    },
}

fn main() -> Result<()> {
    let result = run();
    if let Err(ref err) = result {
        // Check if the error chain contains a DiagnosticError for rich formatting
        if let Some(diag_err) = err.downcast_ref::<decy_parser::DiagnosticError>() {
            for diag in &diag_err.diagnostics {
                eprintln!("{diag}");
            }
            std::process::exit(1);
        }
    }
    result
}

fn run() -> Result<()> {
    let cli = Cli::parse();

    match cli.command {
        Some(Commands::Transpile {
            input,
            output,
            trace,
            oracle,
            oracle_threshold,
            auto_fix,
            capture,
            import_patterns,
            oracle_report,
            verify,
        }) => {
            let oracle_opts = OracleOptions::new(oracle, Some(oracle_threshold), auto_fix)
                .with_capture(capture)
                .with_import(import_patterns)
                .with_report_format(oracle_report);
            transpile_file(input, output, &oracle_opts, trace, verify)?;
        }
        Some(Commands::TranspileProject {
            input,
            output,
            no_cache,
            verbose,
            quiet,
            dry_run,
            stats,
            oracle,
            oracle_threshold,
            auto_fix,
            capture,
            import_patterns,
            oracle_report,
        }) => {
            let oracle_opts = OracleOptions::new(oracle, Some(oracle_threshold), auto_fix)
                .with_capture(capture)
                .with_import(import_patterns)
                .with_report_format(oracle_report);
            transpile_project(
                input,
                output,
                !no_cache,
                verbose,
                quiet,
                dry_run,
                stats,
                &oracle_opts,
            )?;
        }
        Some(Commands::CheckProject { input }) => {
            check_project(input)?;
        }
        Some(Commands::CacheStats { input }) => {
            show_cache_stats(input)?;
        }
        Some(Commands::Repl) => {
            repl::run()?;
        }
        Some(Commands::Audit { input, verbose }) => {
            audit_file(input, verbose)?;
        }
        Some(Commands::DiffTest { input, timeout }) => {
            diff_test_file(input, timeout)?;
        }
        Some(Commands::Oracle { action }) => {
            handle_oracle_command(action)?;
        }
        None => {
            // No subcommand - show info
            println!("Decy: C-to-Rust Transpiler with EXTREME Quality Standards");
            println!("Version 0.1.0");
            println!();
            println!("Use 'decy --help' for usage information");
            println!("Use 'decy transpile <file>' to transpile C code to Rust");
            println!("Use 'decy transpile-project <dir> -o <output>' for entire projects");
            println!("Use 'decy check-project <dir>' to verify build order");
            println!("Use 'decy cache-stats <dir>' to view cache statistics");
            println!("Use 'decy repl' to start interactive mode");
            println!("Use 'decy audit <file>' to audit unsafe code in Rust files");
            println!("Use 'decy diff-test <file>' to compare C vs transpiled Rust behavior");
        }
    }

    Ok(())
}

fn transpile_file(
    input: PathBuf,
    output: Option<PathBuf>,
    oracle_opts: &OracleOptions,
    trace_enabled: bool,
    verify: bool,
) -> Result<()> {
    // Read input file
    let c_code = fs::read_to_string(&input).with_context(|| {
        format!(
            "Failed to read input file: {}\n\nTry: Check that the file exists and is readable\n  or: Verify the file path is correct",
            input.display()
        )
    })?;

    // Get base directory for #include resolution (DECY-056)
    let base_dir = input.parent();

    // Transpile - use oracle if enabled
    let (rust_code, oracle_result) = if oracle_opts.should_use_oracle() {
        let result =
            oracle_integration::transpile_with_oracle(&c_code, oracle_opts).with_context(|| {
                format!("Oracle-assisted transpilation failed for {}", input.display())
            })?;
        let code = result.rust_code.clone();
        (code, Some(result))
    } else if trace_enabled {
        // DECY-193: Transpile with decision tracing
        let (code, trace_collector) =
            decy_core::transpile_with_trace(&c_code).with_context(|| {
                format!(
                    "Failed to transpile {}\n\nTry: Check if the C code has syntax errors\n  or: Preprocess the file first: gcc -E {} -o preprocessed.c",
                    input.display(),
                    input.display()
                )
            })?;
        // Emit trace to stderr as JSON
        eprintln!("{}", trace_collector.to_json());
        (code, None)
    } else {
        // Standard transpilation using decy-core with #include support
        let code = decy_core::transpile_with_includes(&c_code, base_dir).with_context(|| {
            format!(
                "Failed to transpile {}\n\nTry: Check if the C code has syntax errors\n  or: Preprocess the file first: gcc -E {} -o preprocessed.c",
                input.display(),
                input.display()
            )
        })?;
        (code, None)
    };

    // Verify compilation if requested
    if verify {
        let result =
            decy_verify::verify_compilation(&rust_code).context("Failed to verify compilation")?;
        if result.success {
            eprintln!("Compilation verified: output passes rustc type-check");
        } else {
            eprintln!("Compilation verification FAILED:");
            for err in &result.errors {
                eprintln!("  {}", err.message);
            }
            anyhow::bail!("Generated Rust does not compile ({} errors)", result.errors.len());
        }
    }

    // DECY-AUDIT-002: Detect if the source has no main function and provide guidance
    let has_main = rust_code.contains("fn main(");

    // Write output
    match output {
        Some(output_path) => {
            fs::write(&output_path, &rust_code).with_context(|| {
                format!("Failed to write output file: {}", output_path.display())
            })?;
            eprintln!("✓ Transpiled {}{}", input.display(), output_path.display());

            // Show oracle statistics if used
            if let Some(ref result) = oracle_result {
                print_oracle_stats(result, oracle_opts);
            }

            // DECY-AUDIT-002: Provide compilation guidance for library-only files
            if !has_main {
                eprintln!();
                eprintln!("ℹ Note: No main function found in source.");
                eprintln!("  To compile the output as a library, use:");
                eprintln!("  rustc --crate-type=lib {}", output_path.display());
            }
        }
        None => {
            // Write to stdout
            print!("{}", rust_code);

            // Show oracle statistics if used
            if let Some(ref result) = oracle_result {
                print_oracle_stats(result, oracle_opts);
            }

            // DECY-AUDIT-002: Provide compilation guidance for library-only files
            // Only show this to stderr if writing to stdout
            if !has_main {
                eprintln!();
                eprintln!("ℹ Note: No main function found in source.");
                eprintln!("  To compile the output as a library, use:");
                eprintln!("  rustc --crate-type=lib <output_file>");
            }
        }
    }

    Ok(())
}

fn print_oracle_stats(result: &OracleTranspileResult, opts: &OracleOptions) {
    // Check if we should output in a specific format
    if let Some(ref format) = opts.report_format {
        print_oracle_report(result, format);
        return;
    }

    // Default human-readable output
    eprintln!();
    eprintln!("=== Oracle Statistics ===");
    if result.patterns_imported > 0 {
        eprintln!("Patterns imported: {}", result.patterns_imported);
    }
    eprintln!("Queries: {}", result.oracle_queries);
    eprintln!("Fixes applied: {}", result.fixes_applied);
    eprintln!("Retries: {}", result.retries_used);
    if result.patterns_captured > 0 {
        eprintln!("Patterns captured: {}", result.patterns_captured);
    }
    if result.compilation_success {
        eprintln!("✓ Compilation: SUCCESS");
    } else {
        eprintln!("✗ Compilation: FAILED");
        if !result.remaining_errors.is_empty() {
            eprintln!("Remaining errors: {}", result.remaining_errors.len());
            for err in &result.remaining_errors {
                eprintln!("  - {}", err);
            }
        }
    }
}

#[cfg_attr(not(feature = "oracle"), allow(unused_variables))]
fn print_oracle_report(result: &OracleTranspileResult, format: &str) {
    #[cfg(feature = "oracle")]
    {
        use decy_oracle::{CIReport, CIThresholds, OracleMetrics};

        // Build metrics from result
        let metrics = OracleMetrics {
            queries: result.oracle_queries as u64,
            hits: result.fixes_applied as u64, // Approximation
            misses: (result.oracle_queries - result.fixes_applied) as u64,
            fixes_applied: result.fixes_applied as u64,
            fixes_verified: if result.compilation_success {
                result.fixes_applied as u64
            } else {
                0
            },
            patterns_captured: result.patterns_captured as u64,
            ..Default::default()
        };

        let report = CIReport::from_metrics(metrics, CIThresholds::default());

        match format.to_lowercase().as_str() {
            "json" => println!("{}", report.to_json()),
            "markdown" | "md" => println!("{}", report.to_markdown()),
            "prometheus" | "prom" => {
                let m = &report.metrics;
                println!("{}", m.to_prometheus());
            }
            _ => {
                eprintln!("Unknown report format: {}. Use: json, markdown, prometheus", format);
            }
        }
    }

    #[cfg(not(feature = "oracle"))]
    {
        eprintln!("Oracle report format '{}' requires --features oracle", format);
    }
}

fn audit_file(input: PathBuf, verbose: bool) -> Result<()> {
    // Read Rust file
    let rust_code = fs::read_to_string(&input)
        .with_context(|| format!("Failed to read input file: {}", input.display()))?;

    // Run unsafe code auditor
    let report = decy_verify::audit_rust_code(&rust_code)
        .with_context(|| format!("Failed to audit {}", input.display()))?;

    // Print header
    println!();
    println!("Unsafe Code Audit Report");
    println!("========================");
    println!("File: {}", input.display());
    println!("Total Lines: {}", report.total_lines);
    println!("Unsafe Lines: {}", report.unsafe_lines);
    println!(
        "Unsafe Density: {:.2}% {}",
        report.unsafe_density_percent,
        if report.meets_density_target() { "✅ (Target: <5%)" } else { "❌ (Target: <5%)" }
    );
    println!();

    if report.unsafe_blocks.is_empty() {
        println!("✅ No unsafe blocks found - code is 100% safe!");
        return Ok(());
    }

    println!("Unsafe Blocks Found: {}", report.unsafe_blocks.len());
    println!("Average Confidence: {:.1}/100", report.average_confidence);
    println!();

    // Show high-confidence blocks
    let high_conf = report.high_confidence_blocks();
    if !high_conf.is_empty() {
        println!("⚠️  {} blocks with HIGH confidence for elimination (≥70):", high_conf.len());
        println!();
    }

    // List all unsafe blocks
    if verbose {
        println!("Detailed Block Analysis:");
        println!("------------------------");
        for (idx, block) in report.unsafe_blocks.iter().enumerate() {
            println!();
            println!(
                "{}. Line {} [Confidence: {}/100 - {}]",
                idx + 1,
                if block.line > 0 { block.line.to_string() } else { "N/A".to_string() },
                block.confidence,
                if block.confidence >= 70 {
                    "HIGH"
                } else if block.confidence >= 40 {
                    "MEDIUM"
                } else {
                    "LOW"
                }
            );
            println!("   Pattern: {:?}", block.pattern);
            println!("   Suggestion: {}", block.suggestion);
        }
    } else {
        println!("Summary by Confidence:");
        let high = report.unsafe_blocks.iter().filter(|b| b.confidence >= 70).count();
        let medium =
            report.unsafe_blocks.iter().filter(|b| b.confidence >= 40 && b.confidence < 70).count();
        let low = report.unsafe_blocks.iter().filter(|b| b.confidence < 40).count();

        println!("  HIGH (≥70):   {} blocks - likely can be eliminated", high);
        println!("  MEDIUM (40-69): {} blocks - review possible alternatives", medium);
        println!("  LOW (<40):    {} blocks - may require unsafe", low);
        println!();
        println!("Use --verbose flag to see detailed information for each block");
    }

    println!();
    println!("---");
    println!("Recommendation: Focus on eliminating HIGH confidence blocks first.");
    println!();

    Ok(())
}

fn diff_test_file(input: PathBuf, timeout_secs: u64) -> Result<()> {
    use decy_verify::diff_test::{diff_test, DiffTestConfig};

    // Read input C file
    let c_code = fs::read_to_string(&input).with_context(|| {
        format!(
            "Failed to read input file: {}\n\nTry: Check that the file exists and is readable",
            input.display()
        )
    })?;

    // Transpile C to Rust
    let base_dir = input.parent();
    let rust_code = decy_core::transpile_with_includes(&c_code, base_dir).with_context(|| {
        format!(
            "Failed to transpile {}\n\nTry: Check if the C code has syntax errors",
            input.display()
        )
    })?;

    // Run differential test
    let config = DiffTestConfig { timeout_secs, ..Default::default() };

    let result = diff_test(&c_code, &rust_code, &config)?;

    // Report results
    if result.passed() {
        println!("PASS: {} — C and Rust outputs are identical", input.display());
        println!(
            "  stdout: {} bytes | exit code: {}",
            result.c_output.stdout.len(),
            result.c_output.exit_code
        );
    } else {
        println!("FAIL: {} — behavioral divergence detected", input.display());
        for divergence in &result.divergences {
            println!("  {}", divergence);
        }
        anyhow::bail!("Differential test failed: {} divergence(s)", result.divergences.len());
    }

    Ok(())
}

#[allow(clippy::too_many_arguments)]
fn transpile_project(
    input_dir: PathBuf,
    output_dir: PathBuf,
    use_cache: bool,
    verbose: bool,
    quiet: bool,
    dry_run: bool,
    stats: bool,
    _oracle_opts: &OracleOptions,
) -> Result<()> {
    use decy_core::{DependencyGraph, TranspilationCache};
    use indicatif::{ProgressBar, ProgressStyle};
    use std::time::Instant;
    use walkdir::WalkDir;

    // Validate input directory exists
    if !input_dir.exists() {
        anyhow::bail!(
            "Input directory not found: {}\n\nTry: Check the path is correct\n  or: Use 'decy check-project <dir>' to verify project structure",
            input_dir.display()
        );
    }

    // Create output directory if needed (unless dry-run)
    if !dry_run {
        fs::create_dir_all(&output_dir).with_context(|| {
            format!("Failed to create output directory: {}", output_dir.display())
        })?;
    }

    // Find all C/C++/CUDA source files
    let c_files: Vec<PathBuf> = WalkDir::new(&input_dir)
        .into_iter()
        .filter_map(|e| e.ok())
        .filter(|e| {
            matches!(
                e.path().extension().and_then(|s| s.to_str()),
                Some("c") | Some("cu") | Some("cpp") | Some("cc") | Some("cxx")
            )
        })
        .map(|e| e.path().to_path_buf())
        .collect();

    if c_files.is_empty() {
        if !quiet {
            println!("No C/C++/CUDA files found in {}", input_dir.display());
        }
        return Ok(());
    }

    if dry_run && !quiet {
        println!("DRY RUN MODE - No files will be written");
        println!();
    }

    if !quiet {
        println!("Found {} C files to transpile", c_files.len());
        println!();
    }

    // Initialize cache (unless dry-run)
    let cache_dir = input_dir.join(".decy").join("cache");
    if !dry_run {
        fs::create_dir_all(&cache_dir)?;
    }

    let mut cache = if use_cache && !dry_run {
        TranspilationCache::load(&cache_dir)?
    } else {
        TranspilationCache::new()
    };

    // Build dependency graph (simplified - actual implementation in decy-core)
    let mut dep_graph = DependencyGraph::new();
    for file in &c_files {
        dep_graph.add_file(file);
    }

    // Get build order
    let build_order = dep_graph
        .topological_sort()
        .with_context(|| "Failed to compute build order (circular dependencies?)")?;

    // Setup progress bar (unless quiet mode)
    let pb = if quiet {
        ProgressBar::hidden()
    } else {
        let pb = ProgressBar::new(c_files.len() as u64);
        pb.set_style(
            ProgressStyle::default_bar()
                .template("[{elapsed_precise}] {bar:40.cyan/blue} {pos}/{len} {msg}")
                .expect("valid progress template")
                .progress_chars("=>-"),
        );
        pb
    };

    let start_time = Instant::now();
    let mut transpiled_count = 0;
    let mut cached_count = 0;
    let mut total_lines = 0;

    // Transpile files in dependency order
    for file_path in build_order {
        let relative_path = file_path.strip_prefix(&input_dir).unwrap_or(&file_path);
        pb.set_message(format!("Transpiling {}", relative_path.display()));

        // Check cache
        if use_cache {
            if let Some(_cached) = cache.get(&file_path) {
                if verbose {
                    println!("✓ Cached: {}", relative_path.display());
                }
                pb.set_message(format!("✓ Cached {}", relative_path.display()));
                cached_count += 1;
                pb.inc(1);
                continue;
            }
        }

        // Read C code
        let c_code = fs::read_to_string(&file_path)
            .with_context(|| format!("Failed to read {}", file_path.display()))?;

        if dry_run {
            // Dry run mode - always show what would be done (that's the point of dry-run!)
            if !quiet {
                println!("Would transpile: {}", relative_path.display());
            }
            pb.set_message(format!("Would transpile {}", relative_path.display()));
            pb.inc(1);
            continue;
        }

        // Transpile
        let rust_code = decy_core::transpile(&c_code)
            .with_context(|| format!("Failed to transpile {}", file_path.display()))?;

        total_lines += rust_code.lines().count();

        // Compute output path (preserve directory structure)
        let output_path = output_dir.join(relative_path).with_extension("rs");

        // Create parent directory if needed
        if let Some(parent) = output_path.parent() {
            fs::create_dir_all(parent)?;
        }

        // Write output
        fs::write(&output_path, &rust_code)
            .with_context(|| format!("Failed to write {}", output_path.display()))?;

        if verbose {
            println!("✓ Transpiled: {}{}", relative_path.display(), output_path.display());
        }

        // Update cache
        if use_cache {
            let transpiled = decy_core::TranspiledFile {
                source_path: file_path.clone(),
                rust_code: rust_code.clone(),
                dependencies: vec![], // Would be populated by actual parser
                functions_exported: vec![], // Would be populated by actual parser
                ffi_declarations: String::new(), // Would be populated by actual parser
            };
            cache.insert(&file_path, &transpiled);
        }

        transpiled_count += 1;
        pb.inc(1);
    }

    pb.finish_with_message("Done");

    // Save cache (unless dry-run)
    if use_cache && !dry_run {
        cache.save()?;
    }

    let elapsed = start_time.elapsed();

    if !quiet {
        println!();
        if dry_run {
            println!(
                "✓ Dry run complete - {} files checked in {:.2}s",
                c_files.len(),
                elapsed.as_secs_f64()
            );
        } else {
            println!("✓ Transpiled {} files in {:.2}s", transpiled_count, elapsed.as_secs_f64());
        }
    }

    // Show statistics if requested or if verbose
    if (stats || verbose) && !quiet {
        println!();
        println!("=== Statistics ===");
        println!("Files found: {}", c_files.len());
        println!("Files transpiled: {}", transpiled_count);
        if cached_count > 0 {
            println!("Files cached: {}", cached_count);
        }
        if total_lines > 0 {
            println!("Lines generated: {}", total_lines);
        }
        println!("Time elapsed: {:.2}s", elapsed.as_secs_f64());

        if use_cache {
            let cache_stats = cache.statistics();
            println!();
            println!("=== Cache Statistics ===");
            println!("Cache hits: {}", cache_stats.hits);
            println!("Cache misses: {}", cache_stats.misses);
            if cache_stats.hits + cache_stats.misses > 0 {
                let hit_rate = (cache_stats.hits as f64
                    / (cache_stats.hits + cache_stats.misses) as f64)
                    * 100.0;
                println!("Hit rate: {:.1}%", hit_rate);
                let speedup = if cache_stats.misses > 0 {
                    (cache_stats.hits + cache_stats.misses) as f64 / cache_stats.misses as f64
                } else {
                    1.0
                };
                println!("Estimated speedup: {:.1}x", speedup);
            }
        }
    }

    if !quiet && !dry_run {
        println!();
        println!("Output directory: {}", output_dir.display());
    }

    Ok(())
}

fn check_project(input_dir: PathBuf) -> Result<()> {
    use decy_core::DependencyGraph;
    use walkdir::WalkDir;

    // Validate input directory
    if !input_dir.exists() {
        anyhow::bail!("Input directory not found: {}", input_dir.display());
    }

    println!("Checking project: {}", input_dir.display());
    println!();

    // Find all C files
    let c_files: Vec<PathBuf> = WalkDir::new(&input_dir)
        .into_iter()
        .filter_map(|e| e.ok())
        .filter(|e| e.path().extension().and_then(|s| s.to_str()) == Some("c"))
        .map(|e| e.path().to_path_buf())
        .collect();

    if c_files.is_empty() {
        println!("No C files found.");
        return Ok(());
    }

    println!("Found {} C files:", c_files.len());
    for file in &c_files {
        let relative = file.strip_prefix(&input_dir).unwrap_or(file);
        println!("  - {}", relative.display());
    }
    println!();

    // Build dependency graph
    let mut dep_graph = DependencyGraph::new();
    for file in &c_files {
        dep_graph.add_file(file);
    }

    // Check for circular dependencies
    match dep_graph.topological_sort() {
        Ok(build_order) => {
            println!("✓ No circular dependencies detected");
            println!();
            println!("Build order:");
            for (idx, file) in build_order.iter().enumerate() {
                let relative = file.strip_prefix(&input_dir).unwrap_or(file);
                println!("  {}. {}", idx + 1, relative.display());
            }
        }
        Err(e) => {
            println!("❌ Circular dependencies detected:");
            println!("   {}", e);
            return Err(e);
        }
    }

    println!();
    println!("Project is ready for transpilation.");

    Ok(())
}

fn show_cache_stats(input_dir: PathBuf) -> Result<()> {
    use decy_core::TranspilationCache;

    // Validate input directory
    if !input_dir.exists() {
        anyhow::bail!("Input directory not found: {}", input_dir.display());
    }

    let cache_dir = input_dir.join(".decy").join("cache");

    if !cache_dir.exists() {
        println!("No cache found at {}", input_dir.display());
        println!();
        println!("Run 'decy transpile-project' with caching enabled to create a cache.");
        return Ok(());
    }

    // Load cache
    let cache = TranspilationCache::load(&cache_dir)?;

    println!("Cache Statistics");
    println!("================");
    println!("Location: {}", input_dir.join(".decy/cache").display());
    println!();

    let stats = cache.statistics();
    let total = stats.hits + stats.misses;

    println!("Total files cached: {}", stats.total_files);
    println!("Total requests: {}", total);
    println!("Cache hits: {}", stats.hits);
    println!("Cache misses: {}", stats.misses);

    if total > 0 {
        let hit_rate = (stats.hits as f64 / total as f64) * 100.0;
        println!("Hit rate: {:.1}%", hit_rate);

        if hit_rate > 80.0 {
            println!();
            println!("✓ Excellent cache performance!");
        } else if hit_rate > 50.0 {
            println!();
            println!("ℹ Good cache performance.");
        } else if hit_rate > 0.0 {
            println!();
            println!("⚠ Low cache hit rate - files may be changing frequently.");
        }
    } else {
        println!();
        println!("No cache usage recorded yet.");
    }

    Ok(())
}

fn handle_oracle_command(action: OracleAction) -> Result<()> {
    #[cfg(not(feature = "oracle"))]
    {
        let _ = action;
        anyhow::bail!(
            "Oracle commands require the 'oracle' feature.\n\nTry: cargo build -p decy --features oracle"
        );
    }

    #[cfg(feature = "oracle")]
    {
        match action {
            OracleAction::Bootstrap { dry_run } => handle_oracle_bootstrap(dry_run),
            OracleAction::Seed { from } => handle_oracle_seed(from),
            OracleAction::Stats { format } => handle_oracle_stats(format),
            OracleAction::Retire { dry_run, archive_path } => {
                handle_oracle_retire(dry_run, archive_path)
            }
            OracleAction::Validate { corpus } => handle_oracle_validate(corpus),
            OracleAction::Export { output, format, with_card } => {
                handle_oracle_export(output, format, with_card)
            }
            OracleAction::Train { corpus, tier, dry_run } => {
                handle_oracle_train(corpus, tier, dry_run)
            }
            OracleAction::GenerateTraces { corpus, output, tier, dry_run } => {
                handle_oracle_generate_traces(corpus, output, tier, dry_run)
            }
            OracleAction::Query { error, context, format } => {
                handle_oracle_query(error, context, format)
            }
        }
    }
}

#[cfg(feature = "oracle")]
fn handle_oracle_bootstrap(dry_run: bool) -> Result<()> {
    use decy_oracle::bootstrap::{get_bootstrap_patterns, BootstrapStats};
    use decy_oracle::{DecyOracle, OracleConfig};

    println!("=== Oracle Bootstrap ===");
    println!();

    let stats = BootstrapStats::from_patterns();
    println!("{}", stats.to_string_pretty());

    if dry_run {
        println!();
        println!("DRY RUN MODE - Patterns shown but not saved");
        println!();
        println!("Available patterns:");
        for p in get_bootstrap_patterns() {
            println!("  [{}] {}: {}", p.error_code, p.decision, p.description);
        }
        return Ok(());
    }

    #[cfg(feature = "citl")]
    {
        let config = OracleConfig::default();
        let mut oracle = DecyOracle::new(config)?;

        let count = oracle.bootstrap()?;
        oracle.save()?;

        println!();
        println!("✓ Bootstrapped oracle with {} patterns", count);
        println!(
            "  Patterns saved to: {}",
            OracleConfig::default().patterns_path.display()
        );
    }

    #[cfg(not(feature = "citl"))]
    {
        println!();
        println!("⚠ Pattern saving requires the 'citl' feature");
        println!("  Bootstrap patterns shown above can be used manually");
    }

    Ok(())
}

#[cfg(feature = "oracle")]
fn handle_oracle_seed(from: PathBuf) -> Result<()> {
    if !from.exists() {
        anyhow::bail!(
            "Pattern file not found: {}\n\nTry: Verify the path to the .apr file",
            from.display()
        );
    }

    #[cfg(feature = "citl")]
    {
        use decy_oracle::{DecyOracle, OracleConfig};

        println!("Seeding oracle from: {}", from.display());

        let config = OracleConfig::default();
        let mut oracle = DecyOracle::new(config)?;

        let (count, stats) = oracle.import_patterns_with_stats(
            &from,
            decy_oracle::SmartImportConfig::default(),
        )?;

        println!();
        println!("=== Import Results ===");
        println!("Patterns imported: {}", count);
        println!("Total evaluated: {}", stats.total_evaluated);
        println!("Acceptance rate: {:.1}%", stats.overall_acceptance_rate() * 100.0);
        println!();
        println!("Import statistics by strategy:");
        for (strategy, count) in &stats.accepted_by_strategy {
            let rejected =
                stats.rejected_by_strategy.get(strategy).copied().unwrap_or(0);
            let total = count + rejected;
            println!(
                "  {:?}: {}/{} accepted ({:.1}%)",
                strategy,
                count,
                total,
                if total > 0 { (*count as f64 / total as f64) * 100.0 } else { 0.0 }
            );
        }

        oracle.save()?;
        println!();
        println!("✓ Oracle patterns saved");
    }

    #[cfg(not(feature = "citl"))]
    {
        let _ = from;
        return Err(anyhow::anyhow!(
            "Pattern import requires the 'citl' feature.\n\nTry: cargo build -p decy --features citl"
        ));
    }

    #[allow(unreachable_code)]
    Ok(())
}

#[cfg(feature = "oracle")]
fn handle_oracle_stats(format: String) -> Result<()> {
    use decy_oracle::{DecyOracle, OracleConfig};

    let config = OracleConfig::default();
    let oracle = DecyOracle::new(config)?;

    let metrics = oracle.metrics();

    match format.to_lowercase().as_str() {
        "json" => {
            println!("{}", metrics.to_json());
        }
        "markdown" | "md" => {
            use decy_oracle::{CIReport, CIThresholds};
            let report =
                CIReport::from_metrics(metrics.clone(), CIThresholds::default());
            println!("{}", report.to_markdown());
        }
        "prometheus" | "prom" => {
            println!("{}", metrics.to_prometheus());
        }
        _ => {
            println!("=== Oracle Statistics ===");
            println!("Pattern count: {}", oracle.pattern_count());
            println!("Total queries: {}", metrics.queries);
            println!("Cache hits: {}", metrics.hits);
            println!("Cache misses: {}", metrics.misses);
            println!("Fixes applied: {}", metrics.fixes_applied);
            println!("Fixes verified: {}", metrics.fixes_verified);
            if metrics.queries > 0 {
                let hit_rate = (metrics.hits as f64 / metrics.queries as f64) * 100.0;
                println!("Hit rate: {:.1}%", hit_rate);
            }
        }
    }

    Ok(())
}

#[cfg(feature = "oracle")]
fn handle_oracle_retire(dry_run: bool, archive_path: Option<PathBuf>) -> Result<()> {
    use decy_oracle::{DecyOracle, OracleConfig, PatternRetirementPolicy};

    let config = OracleConfig::default();
    let oracle = DecyOracle::new(config)?;

    let policy = PatternRetirementPolicy::new();

    println!("=== Pattern Retirement Analysis ===");
    println!("Pattern count: {}", oracle.pattern_count());

    if dry_run {
        println!();
        println!("DRY RUN MODE - No patterns will be retired");
        println!();
        println!("Retirement policy thresholds:");
        println!("  Min uses: {}", policy.config().min_usage_threshold);
        println!(
            "  Min success rate: {:.1}%",
            policy.config().min_success_rate * 100.0
        );
        println!("  Window: {} days", policy.config().evaluation_window_days);
    } else {
        println!();
        if let Some(ref archive) = archive_path {
            println!("Archive path: {}", archive.display());
        }
        println!();
        println!("Note: Full retirement requires pattern usage statistics.");
        println!("Run with --dry-run to see policy thresholds.");
    }

    Ok(())
}

#[cfg(feature = "oracle")]
fn handle_oracle_validate(corpus: PathBuf) -> Result<()> {
    use decy_oracle::diversity::{analyze_corpus, DiversityValidation};
    use decy_oracle::{DecyOracle, OracleConfig};

    if !corpus.exists() {
        anyhow::bail!(
            "Corpus directory not found: {}\n\nTry: Verify the path to the corpus",
            corpus.display()
        );
    }

    println!("Validating oracle on corpus: {}", corpus.display());
    println!();

    let histogram = analyze_corpus(&corpus)
        .map_err(|e| anyhow::anyhow!("Failed to analyze corpus: {}", e))?;

    println!("=== Corpus Diversity Analysis ===");
    println!("Files: {}", histogram.total_files);
    println!("Lines of code: {}", histogram.total_loc);
    println!();

    if !histogram.construct_coverage.is_empty() {
        println!("C Construct Coverage:");
        for (construct, count) in &histogram.construct_coverage {
            println!("  {:?}: {}", construct, count);
        }
        println!();
    }

    let c_files: Vec<_> = walkdir::WalkDir::new(&corpus)
        .into_iter()
        .filter_map(|e| e.ok())
        .filter(|e| e.path().extension().and_then(|s| s.to_str()) == Some("c"))
        .collect();

    println!("Found {} C files in corpus", c_files.len());

    let config = OracleConfig::default();
    let mut oracle = DecyOracle::new(config)?;

    let mut error_histogram = decy_oracle::diversity::ErrorHistogram::new();
    let mut transpile_success = 0;
    let mut transpile_failed = 0;

    for entry in &c_files {
        let path = entry.path();
        let c_code = match std::fs::read_to_string(path) {
            Ok(c) => c,
            Err(_) => continue,
        };

        match decy_core::transpile(&c_code) {
            Ok(_) => transpile_success += 1,
            Err(e) => {
                transpile_failed += 1;
                let error_str = e.to_string();
                if let Some(code_start) = error_str.find("E0") {
                    let code: String =
                        error_str[code_start..].chars().take(5).collect();
                    error_histogram.record_error(&code);
                    oracle
                        .record_miss(&decy_oracle::RustcError::new(&code, &error_str));
                } else {
                    error_histogram.record_error("E0000");
                    oracle.record_miss(&decy_oracle::RustcError::new(
                        "E0000",
                        "transpilation failed",
                    ));
                }
            }
        }
    }

    println!();
    println!("=== Validation Results ===");
    println!("Files processed: {}", c_files.len());
    println!("Transpile success: {}", transpile_success);
    println!("Transpile failed: {}", transpile_failed);
    if !c_files.is_empty() {
        let success_rate = (transpile_success as f64 / c_files.len() as f64) * 100.0;
        println!("Success rate: {:.1}%", success_rate);
    }

    if !error_histogram.by_error_code.is_empty() {
        println!();
        println!("Error Distribution:");
        for (code, count) in &error_histogram.by_error_code {
            let category = decy_oracle::diversity::categorize_error(code);
            println!("  {}: {} ({:?})", code, count, category);
        }
    }

    println!();
    println!("Oracle metrics after validation:");
    let metrics = oracle.metrics();
    println!("  Queries: {}", metrics.queries);
    println!("  Misses: {}", metrics.misses);

    let validation = DiversityValidation::new(error_histogram);
    if validation.passed {
        println!();
        println!("✅ Corpus diversity validation: PASSED");
    }

    Ok(())
}

#[cfg(feature = "oracle")]
fn handle_oracle_export(output: PathBuf, format: String, with_card: bool) -> Result<()> {
    use decy_oracle::dataset::{generate_dataset_card, DatasetExporter};

    println!("=== Oracle Dataset Export ===");
    println!();

    let exporter = DatasetExporter::new();
    let stats = exporter.stats();

    println!("Patterns to export: {}", exporter.len());
    println!("Verified: {}", stats.verified);
    println!();

    let count = match format.to_lowercase().as_str() {
        "jsonl" => {
            println!("Exporting to JSONL format...");
            exporter.export_jsonl(&output)?
        }
        "chatml" => {
            println!("Exporting to ChatML format...");
            exporter.export_chatml(&output)?
        }
        "alpaca" => {
            println!("Exporting to Alpaca format...");
            exporter.export_alpaca(&output)?
        }
        "parquet" => {
            println!("Exporting to Parquet format...");
            exporter.export_parquet(&output)?
        }
        _ => {
            anyhow::bail!(
                "Unknown export format: {}\n\nSupported formats: jsonl, chatml, alpaca, parquet",
                format
            );
        }
    };

    println!("✓ Exported {} patterns to {}", count, output.display());

    if with_card {
        let card_path = output.with_file_name("README.md");
        let card = generate_dataset_card(&stats);
        std::fs::write(&card_path, &card)?;
        println!("✓ Generated dataset card: {}", card_path.display());
    }

    println!();
    println!("Statistics:");
    println!("{}", stats.to_markdown());

    Ok(())
}

#[cfg(feature = "oracle")]
fn handle_oracle_train(corpus: PathBuf, tier: String, dry_run: bool) -> Result<()> {
    if !corpus.exists() {
        anyhow::bail!(
            "Corpus directory not found: {}\n\nTry: Verify the path to the corpus",
            corpus.display()
        );
    }

    let tier_upper = tier.to_uppercase();
    if !["P0", "P1", "P2"].contains(&tier_upper.as_str()) {
        anyhow::bail!("Invalid tier: {}\n\nSupported tiers: P0, P1, P2", tier);
    }

    let c_files: Vec<_> = walkdir::WalkDir::new(&corpus)
        .into_iter()
        .filter_map(|e| e.ok())
        .filter(|e| e.path().extension().map(|ext| ext == "c").unwrap_or(false))
        .collect();

    if c_files.is_empty() {
        anyhow::bail!(
            "No C files found in corpus: {}\n\nTry: Add .c files to the corpus directory",
            corpus.display()
        );
    }

    println!("=== Oracle CITL Training ===");
    println!();
    println!("Corpus: {}", corpus.display());
    println!("Tier: {}", tier_upper);
    println!("Files: {}", c_files.len());
    if dry_run {
        println!("Mode: DRY RUN (no patterns will be saved)");
    }
    println!();

    let mut files_processed = 0;
    let mut total_errors = 0;
    let mut patterns_captured = 0;

    let context = decy_core::ProjectContext::default();

    for entry in &c_files {
        let c_path = entry.path();
        println!("Training on: {}", c_path.display());

        let transpiled = match decy_core::transpile_file(c_path, &context) {
            Ok(t) => t,
            Err(e) => {
                println!("  ⚠ Transpile failed: {}", e);
                continue;
            }
        };

        let temp_dir = std::env::temp_dir();
        let rust_path = temp_dir.join(format!("decy_train_{}.rs", files_processed));
        std::fs::write(&rust_path, &transpiled.rust_code)?;

        let output = std::process::Command::new("rustc")
            .arg("--error-format=json")
            .arg("--emit=metadata")
            .arg("-o")
            .arg("/dev/null")
            .arg(&rust_path)
            .output()?;

        let stderr = String::from_utf8_lossy(&output.stderr);
        let errors: Vec<&str> =
            stderr.lines().filter(|l| l.contains("\"level\":\"error\"")).collect();

        let error_count = errors.len();
        total_errors += error_count;

        if error_count > 0 {
            println!("  Errors: {}", error_count);

            for error_line in &errors {
                if let Some(code_start) = error_line.find("\"code\":{\"code\":\"") {
                    let code_substr = &error_line[code_start + 16..];
                    if let Some(code_end) = code_substr.find('"') {
                        let error_code = &code_substr[..code_end];
                        println!("    - {}", error_code);

                        if !dry_run {
                            patterns_captured += 1;
                        }
                    }
                }
            }
        } else {
            println!("  ✓ No errors");
        }

        files_processed += 1;
    }

    println!();
    println!("=== Training Summary ===");
    println!("Files processed: {}", files_processed);
    println!("Total errors: {}", total_errors);
    println!("Patterns captured: {}", patterns_captured);

    if dry_run {
        println!();
        println!("DRY RUN - No patterns were saved");
    } else if patterns_captured > 0 {
        println!();
        println!("✓ Training complete");
    }

    Ok(())
}

#[cfg(feature = "oracle")]
fn handle_oracle_generate_traces(
    corpus: PathBuf,
    output: PathBuf,
    tier: String,
    dry_run: bool,
) -> Result<()> {
    use decy_oracle::golden_trace::{GoldenTrace, GoldenTraceDataset, TraceTier};
    use decy_oracle::trace_verifier::TraceVerifier;

    if !corpus.exists() {
        anyhow::bail!(
            "Corpus directory not found: {}\n\nTry: Verify the path to the corpus",
            corpus.display()
        );
    }

    let tier_upper = tier.to_uppercase();
    let trace_tier = match tier_upper.as_str() {
        "P0" => TraceTier::P0,
        "P1" => TraceTier::P1,
        "P2" => TraceTier::P2,
        _ => anyhow::bail!("Invalid tier: {}\n\nSupported tiers: P0, P1, P2", tier),
    };

    let c_files: Vec<_> = walkdir::WalkDir::new(&corpus)
        .into_iter()
        .filter_map(|e| e.ok())
        .filter(|e| e.path().extension().map(|ext| ext == "c").unwrap_or(false))
        .collect();

    if c_files.is_empty() {
        anyhow::bail!(
            "No C files found in corpus: {}\n\nTry: Add .c files to the corpus directory",
            corpus.display()
        );
    }

    println!("=== Golden Trace Generation ===");
    println!();
    println!("Corpus: {}", corpus.display());
    println!("Output: {}", output.display());
    println!("Tier: {}", trace_tier);
    println!("Files: {}", c_files.len());
    if dry_run {
        println!("Mode: DRY RUN (no output file will be written)");
    }
    println!();

    let mut files_processed = 0;
    let mut traces_generated = 0;
    let mut traces_verified = 0;
    let mut traces_failed = 0;
    let mut traces_skipped = 0;

    let mut dataset = GoldenTraceDataset::new();
    let mut verifier = TraceVerifier::new();

    let context = decy_core::ProjectContext::default();

    for entry in &c_files {
        let c_path = entry.path();
        let filename = c_path.file_name().and_then(|s| s.to_str()).unwrap_or("unknown");

        println!("Processing: {}", c_path.display());

        let c_code = match std::fs::read_to_string(c_path) {
            Ok(code) => code,
            Err(e) => {
                println!("  ⚠ Failed to read: {}", e);
                traces_skipped += 1;
                continue;
            }
        };

        let transpiled = match decy_core::transpile_file(c_path, &context) {
            Ok(t) => t,
            Err(e) => {
                println!("  ⚠ Transpile failed: {}", e);
                traces_failed += 1;
                continue;
            }
        };

        let trace = GoldenTrace::new(
            c_code.clone(),
            transpiled.rust_code.clone(),
            trace_tier,
            filename,
        );

        let result = verifier.verify_trace(&trace);

        if result.passed {
            println!("  ✓ Verified - generating trace");
            traces_verified += 1;

            let explanation =
                generate_safety_explanation(&c_code, &transpiled.rust_code, trace_tier);
            let trace_with_explanation = trace.with_safety_explanation(&explanation);

            dataset.add_trace(trace_with_explanation);
            traces_generated += 1;
        } else {
            println!("  ✗ Verification failed: {:?}", result.errors);
            traces_failed += 1;
        }

        files_processed += 1;
    }

    println!();
    println!("=== Generation Summary ===");
    println!("Files processed: {}", files_processed);
    println!("Traces generated: {}", traces_generated);
    println!("Traces verified: {}", traces_verified);
    println!("Traces failed: {}", traces_failed);
    println!("Traces skipped: {}", traces_skipped);

    if dry_run {
        println!();
        println!("DRY RUN - Would generate {} traces", traces_generated);
        println!("Would write to: {}", output.display());
    } else if traces_generated > 0 {
        dataset.export_jsonl(&output)?;
        println!();
        println!(
            "✓ Exported {} Golden Traces to {}",
            traces_generated,
            output.display()
        );
    } else {
        println!();
        println!("⚠ No traces generated - check corpus files");
    }

    Ok(())
}

#[cfg(feature = "oracle")]
fn handle_oracle_query(
    error: String,
    context: Option<String>,
    format: String,
) -> Result<()> {
    use decy_oracle::bootstrap::get_bootstrap_patterns;

    if !error.starts_with('E') || error.len() != 5 {
        anyhow::bail!(
            "Invalid error code format: {}\n\nExpected format: EXXXX (e.g., E0308, E0382)",
            error
        );
    }

    let patterns = get_bootstrap_patterns();
    let matching: Vec<_> = patterns.iter().filter(|p| p.error_code == error).collect();

    if format.to_lowercase() == "json" {
        let json_patterns: Vec<_> = matching
            .iter()
            .map(|p| {
                serde_json::json!({
                    "error_code": p.error_code,
                    "decision": p.decision,
                    "description": p.description,
                    "fix_diff": p.fix_diff,
                })
            })
            .collect();

        println!(
            "{}",
            serde_json::to_string_pretty(&serde_json::json!({
                "error_code": error,
                "context": context,
                "patterns_found": matching.len(),
                "patterns": json_patterns,
            }))?
        );
    } else {
        println!("=== Oracle Query: {} ===", error);
        println!();

        if let Some(ref ctx) = context {
            println!("Context: {}", ctx);
            println!();
        }

        if matching.is_empty() {
            println!("No patterns found for error code {}", error);
            println!();
            println!("Tip: Use 'decy oracle bootstrap' to load seed patterns");
        } else {
            println!("Found {} pattern(s) for {}:", matching.len(), error);
            println!();

            for (i, pattern) in matching.iter().enumerate() {
                println!("--- Pattern {} ---", i + 1);
                println!("Decision: {}", pattern.decision);
                println!("Description: {}", pattern.description);
                println!();
                println!("Fix:");
                for line in pattern.fix_diff.lines() {
                    println!("  {}", line);
                }
                println!();
            }
        }
    }

    Ok(())
}

/// Generate a safety explanation (Chain of Thought) for the C→Rust transformation
#[cfg(feature = "oracle")]
fn generate_safety_explanation(
    c_code: &str,
    rust_code: &str,
    tier: decy_oracle::golden_trace::TraceTier,
) -> String {
    use decy_oracle::golden_trace::TraceTier;

    let mut explanation = String::new();

    explanation.push_str("## Safety Analysis\n\n");

    // Analyze based on tier
    match tier {
        TraceTier::P0 => {
            explanation.push_str("### Tier P0: Simple Pattern Transformation\n\n");
            explanation.push_str("This is a straightforward type transformation. ");
            explanation.push_str(
                "The C code uses primitive types that map directly to Rust's safe type system.\n\n",
            );
        }
        TraceTier::P1 => {
            explanation.push_str("### Tier P1: I/O and Pointer Transformation\n\n");
            explanation.push_str("This transformation involves pointer handling. ");
        }
        TraceTier::P2 => {
            explanation.push_str("### Tier P2: Complex Memory Management\n\n");
            explanation.push_str("This transformation involves complex memory patterns. ");
        }
    }

    // Detect specific patterns
    if c_code.contains("malloc") || c_code.contains("free") {
        explanation.push_str("**Memory Management**: ");
        explanation.push_str("The C code uses manual memory allocation (malloc/free). ");
        explanation.push_str(
            "The Rust code uses RAII patterns (Box, Vec) for automatic memory management, ",
        );
        explanation
            .push_str("eliminating potential use-after-free and memory leak vulnerabilities.\n\n");
    }

    if c_code.contains("*") && (c_code.contains("int *") || c_code.contains("char *")) {
        explanation.push_str("**Pointer Safety**: ");
        explanation.push_str("The C code uses raw pointers. ");
        explanation.push_str(
            "The Rust code converts these to references (&T, &mut T) with borrow checking, ",
        );
        explanation.push_str("ensuring memory safety at compile time.\n\n");
    }

    if c_code.contains("NULL") {
        explanation.push_str("**Null Safety**: ");
        explanation.push_str("The C code checks for NULL. ");
        explanation
            .push_str("The Rust code uses Option<T> to encode nullability in the type system, ");
        explanation.push_str("preventing null pointer dereferences.\n\n");
    }

    if rust_code.contains("unsafe") {
        explanation.push_str("**Unsafe Blocks**: ");
        explanation
            .push_str("Some unsafe operations remain where Rust cannot statically verify safety. ");
        explanation
            .push_str("These are minimized and isolated with documented safety invariants.\n\n");
    } else {
        explanation.push_str("**100% Safe**: ");
        explanation.push_str("The generated Rust code contains no unsafe blocks, ");
        explanation.push_str("providing compile-time memory safety guarantees.\n\n");
    }

    explanation
}