tokmd-core 1.10.0

High-level API façade for tokmd. The recommended entry point for library usage.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
//! # tokmd-core
//!
//! **Tier 4 (Library Facade)**
//!
//! This crate is the **primary library interface** for `tokmd`.
//! It coordinates scanning, aggregation, and modeling to produce code inventory receipts.
//!
//! If you are embedding `tokmd` into another Rust application, depend on this crate
//! and `tokmd-types`. Avoid depending on `tokmd-scan` or `tokmd-model` directly unless necessary.
//!
//! ## What belongs here
//! * High-level workflow coordination
//! * Simplified API for library consumers
//! * Re-exports for convenience
//! * FFI-friendly JSON entrypoint
//!
//! ## What does NOT belong here
//! * CLI argument parsing (use tokmd crate)
//! * Low-level scanning logic (use tokmd-scan)
//! * Aggregation details (use tokmd-model)
//!
//! ## Example
//!
//! ```rust
//! use tokmd_core::{lang_workflow, settings::{ScanSettings, LangSettings}};
//!
//! // Configure scan
//! let scan = ScanSettings::current_dir();
//! let lang = LangSettings {
//!     top: 10,
//!     files: true,
//!     ..Default::default()
//! };
//!
//! // Run pipeline
//! let receipt = lang_workflow(&scan, &lang).expect("Scan failed");
//! assert!(receipt.report.rows.len() > 0);
//! ```
//!
//! ## JSON API (for bindings)
//!
//! ```rust
//! use tokmd_core::ffi::run_json;
//!
//! let result = run_json("lang", r#"{"paths": ["."], "top": 10}"#);
//! let parsed: serde_json::Value = serde_json::from_str(&result).unwrap();
//! assert_eq!(parsed["ok"], true);
//! ```

#![forbid(unsafe_code)]

use std::path::{Path, PathBuf};
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
use std::time::{SystemTime, UNIX_EPOCH};

use anyhow::Result;
#[cfg(feature = "analysis")]
use tokmd_analysis as analysis;
#[cfg(feature = "analysis")]
use tokmd_analysis_types::{AnalysisArgsMeta, AnalysisSource};

// Public modules
pub mod context_git;
pub mod context_policy;
pub mod error;
pub mod ffi;
pub mod settings;
pub use tokmd_scan::InMemoryFile;
pub use tokmd_types as types;

use settings::{DiffSettings, ExportSettings, LangSettings, ModuleSettings, ScanSettings};
use tokmd_format::scan_args;
use tokmd_settings::ScanOptions;
use tokmd_types::{
    ChildIncludeMode, DiffReceipt, ExportArgsMeta, ExportData, ExportReceipt, FileRow,
    LangArgsMeta, LangReceipt, LangReport, ModuleArgsMeta, ModuleReceipt, ModuleReport, RedactMode,
    SCHEMA_VERSION, ScanStatus, ToolInfo,
};

#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
fn now_ms() -> u128 {
    // Keep wasm receipts from reusing zero as a fake wall-clock sentinel.
    js_sys::Date::now().max(1.0) as u128
}

#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
fn now_ms() -> u128 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_millis()
}

// =============================================================================
// Settings-based workflows (new API for bindings)
// =============================================================================

/// Runs the language summary workflow with pure settings types.
///
/// This is the binding-friendly API that doesn't require Clap types.
///
/// # Arguments
///
/// * `scan` - Scan settings (paths, exclusions, etc.)
/// * `lang` - Language-specific settings (top N, files, etc.)
///
/// # Returns
///
/// A `LangReceipt` containing the language summary.
pub fn lang_workflow(scan: &ScanSettings, lang: &LangSettings) -> Result<LangReceipt> {
    let scan_opts = settings_to_scan_options(scan);
    let paths = scan_paths_or_current_dir(scan);

    // Scan
    let languages = tokmd_scan::scan(&paths, &scan_opts)?;

    // Model
    let report = tokmd_model::create_lang_report(&languages, lang.top, lang.files, lang.children);

    Ok(build_lang_receipt(&paths, &scan_opts, lang, report))
}

/// Runs the language summary workflow for ordered in-memory inputs.
///
/// # Example
///
/// ```rust
/// use tokmd_core::{
///     InMemoryFile, lang_workflow_from_inputs,
///     settings::{LangSettings, ScanOptions},
/// };
///
/// let inputs = vec![InMemoryFile::new("src/main.rs", b"fn main() {}".to_vec())];
/// let scan_opts = ScanOptions::default();
/// let lang = LangSettings::default();
///
/// let receipt = lang_workflow_from_inputs(&inputs, &scan_opts, &lang).expect("Scan failed");
/// assert_eq!(receipt.report.rows.len(), 1);
/// ```
pub fn lang_workflow_from_inputs(
    inputs: &[InMemoryFile],
    scan_opts: &ScanOptions,
    lang: &LangSettings,
) -> Result<LangReceipt> {
    let scan_opts = deterministic_in_memory_scan_options(scan_opts);
    let (paths, rows) =
        collect_pure_in_memory_rows(inputs, &scan_opts, &[], 1, ChildIncludeMode::Separate)?;
    let report =
        tokmd_model::create_lang_report_from_rows(&rows, lang.top, lang.files, lang.children);

    Ok(build_lang_receipt(&paths, &scan_opts, lang, report))
}

/// Runs the module summary workflow with pure settings types.
///
/// # Arguments
///
/// * `scan` - Scan settings (paths, exclusions, etc.)
/// * `module` - Module-specific settings (roots, depth, etc.)
///
/// # Returns
///
/// A `ModuleReceipt` containing the module breakdown.
///
/// # Example
///
/// ```rust
/// use tokmd_core::{module_workflow, settings::{ScanSettings, ModuleSettings}};
///
/// let scan = ScanSettings::current_dir();
/// let module = ModuleSettings {
///     module_depth: 2,
///     ..Default::default()
/// };
///
/// let receipt = module_workflow(&scan, &module).expect("Module scan failed");
/// assert!(receipt.report.rows.len() > 0);
/// ```
pub fn module_workflow(scan: &ScanSettings, module: &ModuleSettings) -> Result<ModuleReceipt> {
    let scan_opts = settings_to_scan_options(scan);
    let paths = scan_paths_or_current_dir(scan);

    // Scan
    let languages = tokmd_scan::scan(&paths, &scan_opts)?;

    // Model
    let report = tokmd_model::create_module_report(
        &languages,
        &module.module_roots,
        module.module_depth,
        module.children,
        module.top,
    );

    Ok(build_module_receipt(&paths, &scan_opts, module, report))
}

/// Runs the module summary workflow for ordered in-memory inputs.
///
/// # Example
///
/// ```rust
/// use tokmd_core::{
///     InMemoryFile, module_workflow_from_inputs,
///     settings::{ModuleSettings, ScanOptions},
/// };
///
/// let inputs = vec![InMemoryFile::new("src/main.rs", b"fn main() {}".to_vec())];
/// let scan_opts = ScanOptions::default();
/// let module = ModuleSettings::default();
///
/// let receipt =
///     module_workflow_from_inputs(&inputs, &scan_opts, &module).expect("Module scan failed");
/// assert_eq!(receipt.report.rows.len(), 1);
/// ```
pub fn module_workflow_from_inputs(
    inputs: &[InMemoryFile],
    scan_opts: &ScanOptions,
    module: &ModuleSettings,
) -> Result<ModuleReceipt> {
    let scan_opts = deterministic_in_memory_scan_options(scan_opts);
    let (paths, rows) = collect_pure_in_memory_rows(
        inputs,
        &scan_opts,
        &module.module_roots,
        module.module_depth,
        module.children,
    )?;
    let report = tokmd_model::create_module_report_from_rows(
        &rows,
        &module.module_roots,
        module.module_depth,
        module.children,
        module.top,
    );

    Ok(build_module_receipt(&paths, &scan_opts, module, report))
}

/// Runs the export workflow with pure settings types.
///
/// # Arguments
///
/// * `scan` - Scan settings (paths, exclusions, etc.)
/// * `export` - Export-specific settings (format, min_code, etc.)
///
/// # Returns
///
/// An `ExportReceipt` containing file-level data.
///
/// # Example
///
/// ```rust
/// use tokmd_core::{export_workflow, settings::{ScanSettings, ExportSettings}};
///
/// let scan = ScanSettings::current_dir();
/// let export = ExportSettings::default();
///
/// let receipt = export_workflow(&scan, &export).expect("Export scan failed");
/// assert!(receipt.data.rows.len() > 0);
/// ```
pub fn export_workflow(scan: &ScanSettings, export: &ExportSettings) -> Result<ExportReceipt> {
    let scan_opts = settings_to_scan_options(scan);
    let paths = scan_paths_or_current_dir(scan);
    let strip_prefix = export.strip_prefix.as_deref();

    // Scan
    let languages = tokmd_scan::scan(&paths, &scan_opts)?;

    // Model
    let data = tokmd_model::create_export_data(
        &languages,
        &export.module_roots,
        export.module_depth,
        export.children,
        strip_prefix.map(std::path::Path::new),
        export.min_code,
        export.max_rows,
    );

    Ok(build_export_receipt(&paths, &scan_opts, export, data))
}

/// Runs the file export workflow for ordered in-memory inputs.
///
/// # Example
///
/// ```rust
/// use tokmd_core::{
///     InMemoryFile, export_workflow_from_inputs,
///     settings::{ExportSettings, ScanOptions},
/// };
///
/// let inputs = vec![InMemoryFile::new("src/main.rs", b"fn main() {}".to_vec())];
/// let scan_opts = ScanOptions::default();
/// let export = ExportSettings::default();
///
/// let receipt =
///     export_workflow_from_inputs(&inputs, &scan_opts, &export).expect("Export scan failed");
/// assert_eq!(receipt.data.rows.len(), 1);
/// ```
pub fn export_workflow_from_inputs(
    inputs: &[InMemoryFile],
    scan_opts: &ScanOptions,
    export: &ExportSettings,
) -> Result<ExportReceipt> {
    let scan_opts = deterministic_in_memory_scan_options(scan_opts);
    let (paths, mut rows) = collect_pure_in_memory_rows(
        inputs,
        &scan_opts,
        &export.module_roots,
        export.module_depth,
        export.children,
    )?;
    if let Some(strip_prefix) = export.strip_prefix.as_deref() {
        rows = strip_virtual_export_prefix(
            rows,
            strip_prefix,
            &export.module_roots,
            export.module_depth,
        );
    }
    let data = tokmd_model::create_export_data_from_rows(
        rows,
        &export.module_roots,
        export.module_depth,
        export.children,
        export.min_code,
        export.max_rows,
    );

    Ok(build_export_receipt(&paths, &scan_opts, export, data))
}

/// Runs the diff workflow comparing two receipts or paths.
///
/// # Arguments
///
/// * `settings` - Diff settings (from, to references)
///
/// # Returns
///
/// A `DiffReceipt` showing changes between the two states.
///
/// # Example
///
/// ```rust
/// use tokmd_core::{diff_workflow, settings::DiffSettings};
///
/// let settings = DiffSettings {
///     from: ".".to_string(), // compare current dir to itself as a quick test
///     to: ".".to_string(),
///     ..Default::default()
/// };
///
/// let receipt = diff_workflow(&settings).expect("Diff failed");
/// assert!(receipt.totals.delta_code == 0); // delta is zero
/// ```
pub fn diff_workflow(settings: &DiffSettings) -> Result<DiffReceipt> {
    // Load or scan the "from" state
    let from_report = load_lang_report(&settings.from)?;

    // Load or scan the "to" state
    let to_report = load_lang_report(&settings.to)?;

    // Compute diff
    let rows = tokmd_format::compute_diff_rows(&from_report, &to_report);
    let totals = tokmd_format::compute_diff_totals(&rows);

    Ok(tokmd_format::create_diff_receipt(
        &settings.from,
        &settings.to,
        rows,
        totals,
    ))
}

/// Analyze workflow (requires `analysis` feature).
///
/// Runs export + analysis workflows and returns an `AnalysisReceipt`.
///
/// # Example
///
/// ```rust
/// use tokmd_core::{analyze_workflow, settings::{ScanSettings, AnalyzeSettings}};
///
/// let scan = ScanSettings::current_dir();
/// let analyze = AnalyzeSettings {
///     preset: "receipt".to_string(),
///     ..Default::default()
/// };
///
/// let receipt = analyze_workflow(&scan, &analyze).expect("Analyze scan failed");
/// assert!(receipt.derived.is_some());
/// ```
#[cfg(feature = "analysis")]
pub fn analyze_workflow(
    scan: &ScanSettings,
    analyze: &settings::AnalyzeSettings,
) -> Result<tokmd_analysis_types::AnalysisReceipt> {
    let export_receipt = export_workflow(scan, &ExportSettings::default())?;
    let root = derive_analysis_root(scan)
        .or_else(|| std::env::current_dir().ok())
        .unwrap_or_else(|| PathBuf::from("."));

    analyze_with_export_receipt(export_receipt, scan.paths.clone(), root, analyze)
}

/// Analyze workflow for ordered in-memory inputs (requires `analysis` feature).
///
/// Runs the in-memory export + analysis pipeline and returns an `AnalysisReceipt`.
///
/// `preset = "receipt"` and `preset = "estimate"` stay on the pure row path
/// and do not borrow the host repository as a fake root. Richer presets still
/// materialize a temporary scan root until the remaining analysis seams are
/// moved off the filesystem.
///
/// # Example
///
/// ```rust
/// use tokmd_core::{analyze_workflow_from_inputs, settings::{AnalyzeSettings, ScanOptions}, InMemoryFile};
///
/// let inputs = vec![
///     InMemoryFile {
///         path: "src/main.rs".into(),
///         bytes: b"fn main() { println!(\"hello world\"); }".to_vec(),
///     }
/// ];
///
/// let scan_opts = ScanOptions::default();
/// let analyze_opts = AnalyzeSettings {
///     preset: "receipt".to_string(),
///     ..Default::default()
/// };
///
/// let receipt = analyze_workflow_from_inputs(&inputs, &scan_opts, &analyze_opts)
///     .expect("analyze_workflow_from_inputs failed");
/// assert!(receipt.derived.is_some());
/// ```
#[cfg(feature = "analysis")]
pub fn analyze_workflow_from_inputs(
    inputs: &[InMemoryFile],
    scan_opts: &ScanOptions,
    analyze: &settings::AnalyzeSettings,
) -> Result<tokmd_analysis_types::AnalysisReceipt> {
    let export = ExportSettings::default();
    let scan_opts = deterministic_in_memory_scan_options(scan_opts);
    if supports_rootless_in_memory_analyze_preset(&analyze.preset) {
        let (paths, rows) = collect_pure_in_memory_rows(
            inputs,
            &scan_opts,
            &export.module_roots,
            export.module_depth,
            export.children,
        )?;
        let data = tokmd_model::create_export_data_from_rows(
            rows,
            &export.module_roots,
            export.module_depth,
            export.children,
            export.min_code,
            export.max_rows,
        );
        let logical_inputs: Vec<String> = paths
            .iter()
            .map(|path| tokmd_model::normalize_path(path, None))
            .collect();
        let export_receipt = build_export_receipt(&paths, &scan_opts, &export, data);

        return analyze_with_export_receipt(
            export_receipt,
            logical_inputs,
            PathBuf::new(),
            analyze,
        );
    }

    let scan = tokmd_scan::scan_in_memory(inputs, &scan_opts)?;
    let data = collect_materialized_export_data(&scan, &export);
    let logical_inputs: Vec<String> = scan
        .logical_paths()
        .iter()
        .map(|path| tokmd_model::normalize_path(path, None))
        .collect();
    let root = scan.strip_prefix().to_path_buf();
    let export_receipt = build_export_receipt(scan.logical_paths(), &scan_opts, &export, data);

    analyze_with_export_receipt(export_receipt, logical_inputs, root, analyze)
}

#[cfg(feature = "analysis")]
#[doc(hidden)]
pub fn supports_rootless_in_memory_analyze_preset(preset: &str) -> bool {
    let preset = preset.trim();
    preset.eq_ignore_ascii_case("receipt") || preset.eq_ignore_ascii_case("estimate")
}

#[cfg(feature = "analysis")]
fn analyze_with_export_receipt(
    export_receipt: ExportReceipt,
    inputs: Vec<String>,
    root: PathBuf,
    analyze: &settings::AnalyzeSettings,
) -> Result<tokmd_analysis_types::AnalysisReceipt> {
    let request = build_analysis_request(analyze)?;
    let source = AnalysisSource {
        inputs,
        export_path: None,
        base_receipt_path: None,
        export_schema_version: Some(export_receipt.schema_version),
        export_generated_at_ms: Some(export_receipt.generated_at_ms),
        base_signature: None,
        module_roots: export_receipt.data.module_roots.clone(),
        module_depth: export_receipt.data.module_depth,
        children: child_include_mode_to_string(export_receipt.data.children),
    };

    let ctx = analysis::AnalysisContext {
        export: export_receipt.data,
        root,
        source,
    };

    analysis::analyze(ctx, request)
}

#[cfg(feature = "analysis")]
fn build_analysis_request(
    analyze: &settings::AnalyzeSettings,
) -> Result<analysis::AnalysisRequest> {
    let (preset, preset_meta) = parse_analysis_preset(&analyze.preset)?;
    let (granularity, granularity_meta) = parse_import_granularity(&analyze.granularity)?;
    let effort = parse_effort_request(analyze, &preset_meta)?;

    Ok(analysis::AnalysisRequest {
        preset,
        args: AnalysisArgsMeta {
            preset: preset_meta,
            format: "json".to_string(),
            window_tokens: analyze.window,
            git: analyze.git,
            max_files: analyze.max_files,
            max_bytes: analyze.max_bytes,
            max_file_bytes: analyze.max_file_bytes,
            max_commits: analyze.max_commits,
            max_commit_files: analyze.max_commit_files,
            import_granularity: granularity_meta,
        },
        limits: analysis::AnalysisLimits {
            max_files: analyze.max_files,
            max_bytes: analyze.max_bytes,
            max_file_bytes: analyze.max_file_bytes,
            max_commits: analyze.max_commits,
            max_commit_files: analyze.max_commit_files,
        },
        window_tokens: analyze.window,
        git: analyze.git,
        import_granularity: granularity,
        detail_functions: false,
        near_dup: false,
        near_dup_threshold: 0.80,
        near_dup_max_files: 2000,
        near_dup_scope: analysis::NearDupScope::Module,
        near_dup_max_pairs: None,
        near_dup_exclude: Vec::new(),
        effort,
    })
}

// =============================================================================
// Cockpit workflow (requires `cockpit` feature)
// =============================================================================

/// Cockpit workflow: compute PR metrics and evidence gates.
///
/// Runs the cockpit analysis pipeline using pure settings types.
///
/// # Arguments
///
/// * `settings` - Cockpit settings (base/head refs, range mode, baseline)
///
/// # Returns
///
/// A `CockpitReceipt` containing PR metrics, evidence gates, and review plan.
///
/// # Example
///
/// ```rust,no_run
/// use tokmd_core::{cockpit_workflow, settings::CockpitSettings};
///
/// let settings = CockpitSettings {
///     base: "HEAD~1".to_string(),
///     head: "HEAD".to_string(),
///     range_mode: "2dot".to_string(),
///     ..Default::default()
/// };
///
/// let receipt = cockpit_workflow(&settings).expect("Cockpit scan failed");
/// assert!(!receipt.review_plan.is_empty());
/// ```
#[cfg(feature = "cockpit")]
pub fn cockpit_workflow(
    settings: &settings::CockpitSettings,
) -> Result<tokmd_types::cockpit::CockpitReceipt> {
    use tokmd_types::cockpit::CockpitReceipt;

    if !tokmd_git::git_available() {
        anyhow::bail!("git is not available on PATH");
    }

    let cwd = std::env::current_dir().context("Failed to resolve current directory")?;
    let repo_root =
        tokmd_git::repo_root(&cwd).ok_or_else(|| anyhow::anyhow!("not inside a git repository"))?;

    let range_mode = parse_cockpit_range_mode(&settings.range_mode)?;

    let resolved_base =
        tokmd_git::resolve_base_ref(&repo_root, &settings.base).ok_or_else(|| {
            anyhow::anyhow!(
                "base ref '{}' not found and no fallback resolved",
                settings.base
            )
        })?;

    let baseline_path = settings.baseline.as_deref();

    let mut receipt: CockpitReceipt = tokmd_cockpit::compute_cockpit(
        &repo_root,
        &resolved_base,
        &settings.head,
        range_mode,
        baseline_path.map(std::path::Path::new),
    )?;

    // Load baseline and compute trend if provided
    if let Some(baseline_path) = baseline_path {
        receipt.trend = Some(tokmd_cockpit::load_and_compute_trend(
            std::path::Path::new(baseline_path),
            &receipt,
        )?);
    }

    Ok(receipt)
}

#[cfg(feature = "cockpit")]
fn parse_cockpit_range_mode(value: &str) -> Result<tokmd_git::GitRangeMode> {
    let normalized = value.trim().to_ascii_lowercase();
    match normalized.as_str() {
        "two-dot" | "2dot" => Ok(tokmd_git::GitRangeMode::TwoDot),
        "three-dot" | "3dot" => Ok(tokmd_git::GitRangeMode::ThreeDot),
        _ => Err(error::TokmdError::invalid_field(
            "range_mode",
            "'two-dot', '2dot', 'three-dot', or '3dot'",
        )
        .into()),
    }
}

#[cfg(feature = "cockpit")]
use anyhow::Context as _;

// =============================================================================
// Analysis formatting facade (requires `analysis` feature)
// =============================================================================

/// Analysis formatting re-exports for Tier 5 products.
///
/// This module provides Tier 4 facade access to Tier 3 analysis formatting,
/// maintaining tier boundary compliance for tokmd CLI and other products.
///
/// ## Example
///
/// ```rust
/// use tokmd_core::analysis_facade::{render, RenderedOutput};
/// use tokmd_types::AnalysisFormat;
/// use tokmd_analysis_types::AnalysisReceipt;
///
/// fn format_analysis(receipt: &AnalysisReceipt, format: AnalysisFormat) -> anyhow::Result<String> {
///     match render(receipt, format)? {
///         RenderedOutput::Text(text) => Ok(text),
///         RenderedOutput::Binary(_) => Err(anyhow::anyhow!("Binary output not supported")),
///     }
/// }
/// ```
#[cfg(feature = "analysis")]
pub mod analysis_facade {
    /// Render an analysis receipt to the specified format.
    ///
    /// # Arguments
    /// * `receipt` — The analysis receipt to render (from `tokmd_analysis_types`)
    /// * `format` — Target output format (from `tokmd_types::AnalysisFormat`)
    ///
    /// # Returns
    /// `RenderedOutput` enum containing either text or binary data
    ///
    /// # Errors
    /// Returns error if:
    /// - JSON/XML serialization fails
    /// - `fun` feature is disabled but OBJ/MIDI format requested
    pub use tokmd_format::analysis::render;

    /// Output container for rendered analysis.
    ///
    /// ## Variants
    /// - `Text(String)` — Textual formats: Markdown, JSON, XML, SVG, Mermaid, Tree, HTML
    /// - `Binary(Vec<u8>)` — Binary formats: MIDI (requires `fun` feature)
    pub use tokmd_format::analysis::RenderedOutput;
}

// =============================================================================
// Helper functions
// =============================================================================

/// Convert ScanSettings to ScanOptions for lower-tier crates.
fn settings_to_scan_options(scan: &ScanSettings) -> ScanOptions {
    scan.options.clone()
}

fn scan_paths_or_current_dir(scan: &ScanSettings) -> Vec<PathBuf> {
    if scan.paths.is_empty() {
        vec![PathBuf::from(".")]
    } else {
        scan.paths.iter().map(PathBuf::from).collect()
    }
}

fn deterministic_in_memory_scan_options(scan_opts: &ScanOptions) -> ScanOptions {
    let mut effective = scan_opts.clone();
    // Explicit in-memory inputs are authoritative; they should not depend on
    // host cwd config discovery or be filtered back out by hidden/exclude rules.
    effective.config = tokmd_types::ConfigMode::None;
    effective.hidden = true;
    effective.excluded.clear();
    effective
}

fn collect_pure_in_memory_rows(
    inputs: &[InMemoryFile],
    scan_opts: &ScanOptions,
    module_roots: &[String],
    module_depth: usize,
    children: ChildIncludeMode,
) -> Result<(Vec<PathBuf>, Vec<FileRow>)> {
    let paths = tokmd_scan::normalize_in_memory_paths(inputs)?;
    let config = tokmd_scan::config_from_scan_options(scan_opts);
    let row_inputs: Vec<tokmd_model::InMemoryRowInput<'_>> = paths
        .iter()
        .zip(inputs)
        .map(|(path, input)| {
            tokmd_model::InMemoryRowInput::new(path.as_path(), input.bytes.as_slice())
        })
        .collect();
    let rows = tokmd_model::collect_in_memory_file_rows(
        &row_inputs,
        module_roots,
        module_depth,
        children,
        &config,
    );
    Ok((paths, rows))
}

#[cfg(feature = "analysis")]
fn collect_materialized_rows(
    scan: &tokmd_scan::MaterializedScan,
    module_roots: &[String],
    module_depth: usize,
    children: ChildIncludeMode,
) -> Vec<FileRow> {
    tokmd_model::collect_file_rows(
        scan.languages(),
        module_roots,
        module_depth,
        children,
        Some(scan.strip_prefix()),
    )
}

fn strip_virtual_export_prefix(
    rows: Vec<FileRow>,
    strip_prefix: &str,
    module_roots: &[String],
    module_depth: usize,
) -> Vec<FileRow> {
    rows.into_iter()
        .map(|mut row| {
            let normalized =
                tokmd_model::normalize_path(Path::new(&row.path), Some(Path::new(strip_prefix)));
            row.path = normalized.clone();
            row.module = tokmd_model::module_key(&normalized, module_roots, module_depth);
            row
        })
        .collect()
}

#[cfg(feature = "analysis")]
fn collect_materialized_export_data(
    scan: &tokmd_scan::MaterializedScan,
    export: &ExportSettings,
) -> ExportData {
    let mut rows = collect_materialized_rows(
        scan,
        &export.module_roots,
        export.module_depth,
        export.children,
    );

    if let Some(strip_prefix) = export.strip_prefix.as_deref() {
        rows = strip_virtual_export_prefix(
            rows,
            strip_prefix,
            &export.module_roots,
            export.module_depth,
        );
    }

    tokmd_model::create_export_data_from_rows(
        rows,
        &export.module_roots,
        export.module_depth,
        export.children,
        export.min_code,
        export.max_rows,
    )
}

fn build_lang_receipt(
    paths: &[PathBuf],
    scan_opts: &ScanOptions,
    lang: &LangSettings,
    report: LangReport,
) -> LangReceipt {
    LangReceipt {
        schema_version: SCHEMA_VERSION,
        generated_at_ms: now_ms(),
        tool: ToolInfo::current(),
        mode: "lang".to_string(),
        status: ScanStatus::Complete,
        warnings: vec![],
        scan: scan_args(paths, scan_opts, lang.redact),
        args: LangArgsMeta {
            format: "json".to_string(),
            top: lang.top,
            with_files: lang.files,
            children: lang.children,
        },
        report,
    }
}

fn build_module_receipt(
    paths: &[PathBuf],
    scan_opts: &ScanOptions,
    module: &ModuleSettings,
    report: ModuleReport,
) -> ModuleReceipt {
    ModuleReceipt {
        schema_version: SCHEMA_VERSION,
        generated_at_ms: now_ms(),
        tool: ToolInfo::current(),
        mode: "module".to_string(),
        status: ScanStatus::Complete,
        warnings: vec![],
        scan: scan_args(paths, scan_opts, module.redact),
        args: ModuleArgsMeta {
            format: "json".to_string(),
            top: module.top,
            module_roots: module.module_roots.clone(),
            module_depth: module.module_depth,
            children: module.children,
        },
        report,
    }
}

fn build_export_receipt(
    paths: &[PathBuf],
    scan_opts: &ScanOptions,
    export: &ExportSettings,
    data: ExportData,
) -> ExportReceipt {
    let should_redact = export.redact == RedactMode::Paths || export.redact == RedactMode::All;
    let strip_prefix_redacted = should_redact && export.strip_prefix.is_some();

    ExportReceipt {
        schema_version: SCHEMA_VERSION,
        generated_at_ms: now_ms(),
        tool: ToolInfo::current(),
        mode: "export".to_string(),
        status: ScanStatus::Complete,
        warnings: vec![],
        scan: scan_args(paths, scan_opts, Some(export.redact)),
        args: ExportArgsMeta {
            format: export.format,
            module_roots: export.module_roots.clone(),
            module_depth: export.module_depth,
            children: export.children,
            min_code: export.min_code,
            max_rows: export.max_rows,
            redact: export.redact,
            strip_prefix: if should_redact {
                export
                    .strip_prefix
                    .as_ref()
                    .map(|p| tokmd_format::redact_path(p))
            } else {
                export.strip_prefix.clone()
            },
            strip_prefix_redacted,
        },
        data: redact_export_data(data, export.redact),
    }
}

#[cfg(feature = "analysis")]
fn parse_analysis_preset(value: &str) -> Result<(analysis::AnalysisPreset, String)> {
    let normalized = value.trim().to_ascii_lowercase();
    let preset = match normalized.as_str() {
        "receipt" => analysis::AnalysisPreset::Receipt,
        "estimate" => analysis::AnalysisPreset::Estimate,
        "health" => analysis::AnalysisPreset::Health,
        "risk" => analysis::AnalysisPreset::Risk,
        "supply" => analysis::AnalysisPreset::Supply,
        "architecture" => analysis::AnalysisPreset::Architecture,
        "topics" => analysis::AnalysisPreset::Topics,
        "security" => analysis::AnalysisPreset::Security,
        "identity" => analysis::AnalysisPreset::Identity,
        "git" => analysis::AnalysisPreset::Git,
        "deep" => analysis::AnalysisPreset::Deep,
        "fun" => analysis::AnalysisPreset::Fun,
        _ => {
            return Err(error::TokmdError::invalid_field(
                "preset",
                "'receipt', 'estimate', 'health', 'risk', 'supply', 'architecture', 'topics', 'security', 'identity', 'git', 'deep', or 'fun'",
            )
            .into());
        }
    };
    Ok((preset, normalized))
}

#[cfg(feature = "analysis")]
fn parse_import_granularity(value: &str) -> Result<(analysis::ImportGranularity, String)> {
    let normalized = value.trim().to_ascii_lowercase();
    let granularity = match normalized.as_str() {
        "module" => analysis::ImportGranularity::Module,
        "file" => analysis::ImportGranularity::File,
        _ => {
            return Err(
                error::TokmdError::invalid_field("granularity", "'module' or 'file'").into(),
            );
        }
    };
    Ok((granularity, normalized))
}

#[cfg(feature = "analysis")]
fn parse_effort_request(
    analyze: &settings::AnalyzeSettings,
    preset: &str,
) -> Result<Option<analysis::EffortRequest>> {
    let request = analysis::EffortRequest::default();
    let requested = preset == "estimate"
        || analyze.effort_model.is_some()
        || analyze.effort_layer.is_some()
        || analyze.effort_base_ref.is_some()
        || analyze.effort_head_ref.is_some()
        || analyze.effort_monte_carlo.unwrap_or(false)
        || analyze.effort_mc_iterations.is_some()
        || analyze.effort_mc_seed.is_some();

    if !requested {
        return Ok(None);
    }

    if (analyze.effort_base_ref.is_some() && analyze.effort_head_ref.is_none())
        || (analyze.effort_base_ref.is_none() && analyze.effort_head_ref.is_some())
    {
        return Err(error::TokmdError::invalid_field(
            "effort_base_ref/effort_head_ref",
            "both effort_base_ref and effort_head_ref must be provided together",
        )
        .into());
    }

    let model = analyze
        .effort_model
        .as_deref()
        .map(parse_effort_model)
        .transpose()?
        .unwrap_or(request.model);
    let layer = analyze
        .effort_layer
        .as_deref()
        .map(parse_effort_layer)
        .transpose()?
        .unwrap_or(request.layer);

    let monte_carlo = analyze.effort_monte_carlo.unwrap_or(false);

    let mc_iterations = analyze
        .effort_mc_iterations
        .unwrap_or(request.mc_iterations);

    if mc_iterations == 0 {
        return Err(error::TokmdError::invalid_field(
            "effort_mc_iterations",
            "must be greater than 0",
        )
        .into());
    }

    Ok(Some(analysis::EffortRequest {
        model,
        layer,
        base_ref: analyze.effort_base_ref.clone(),
        head_ref: analyze.effort_head_ref.clone(),
        monte_carlo,
        mc_iterations,
        mc_seed: analyze.effort_mc_seed,
    }))
}

#[cfg(feature = "analysis")]
fn parse_effort_model(value: &str) -> Result<analysis::EffortModelKind> {
    match value.trim().to_ascii_lowercase().as_str() {
        "cocomo81-basic" => Ok(analysis::EffortModelKind::Cocomo81Basic),
        "cocomo2-early" | "ensemble" => Err(error::TokmdError::invalid_field(
            "effort_model",
            "only 'cocomo81-basic' is currently supported",
        )
        .into()),
        _ => Err(error::TokmdError::invalid_field("effort_model", "'cocomo81-basic'").into()),
    }
}

#[cfg(feature = "analysis")]
fn parse_effort_layer(value: &str) -> Result<analysis::EffortLayer> {
    match value.trim().to_ascii_lowercase().as_str() {
        "headline" => Ok(analysis::EffortLayer::Headline),
        "why" => Ok(analysis::EffortLayer::Why),
        "full" => Ok(analysis::EffortLayer::Full),
        _ => Err(
            error::TokmdError::invalid_field("effort_layer", "'headline', 'why', or 'full'").into(),
        ),
    }
}

#[cfg(feature = "analysis")]
fn child_include_mode_to_string(mode: tokmd_types::ChildIncludeMode) -> String {
    match mode {
        tokmd_types::ChildIncludeMode::Separate => "separate".to_string(),
        tokmd_types::ChildIncludeMode::ParentsOnly => "parents-only".to_string(),
    }
}

#[cfg(feature = "analysis")]
fn derive_analysis_root(scan: &ScanSettings) -> Option<PathBuf> {
    let first = scan.paths.first()?;
    if first.trim().is_empty() {
        return None;
    }

    let candidate = PathBuf::from(first);
    let absolute = if candidate.is_absolute() {
        candidate
    } else {
        std::env::current_dir().ok()?.join(candidate)
    };

    if absolute.is_dir() {
        Some(absolute)
    } else {
        absolute.parent().map(|p| p.to_path_buf())
    }
}

/// Load a LangReport from a file path or scan a directory.
fn load_lang_report(source: &str) -> Result<LangReport> {
    let path = std::path::Path::new(source);

    if path.exists() && path.is_file() {
        // Try to load as a receipt file
        let content = std::fs::read_to_string(path)?;
        if let Ok(receipt) = serde_json::from_str::<LangReceipt>(&content) {
            return Ok(receipt.report);
        }
        // Fall through to scanning if not a valid receipt
    }

    // Scan the path
    let scan = ScanSettings::for_paths(vec![source.to_string()]);
    let lang = LangSettings::default();
    let receipt = lang_workflow(&scan, &lang)?;
    Ok(receipt.report)
}

/// Apply redaction to export data.
fn redact_export_data(data: ExportData, mode: RedactMode) -> ExportData {
    if mode == RedactMode::None {
        return data;
    }

    let rows = data
        .rows
        .into_iter()
        .map(|mut row| {
            if mode == RedactMode::Paths || mode == RedactMode::All {
                row.path = tokmd_format::redact_path(&row.path);
            }
            if mode == RedactMode::All {
                row.module = tokmd_format::short_hash(&row.module);
            }
            row
        })
        .collect();

    ExportData {
        rows,
        module_roots: data.module_roots,
        module_depth: data.module_depth,
        children: data.children,
    }
}

// =============================================================================
// Re-exports for binding convenience
// =============================================================================

/// Re-export schema version for bindings.
pub const CORE_SCHEMA_VERSION: u32 = SCHEMA_VERSION;

/// Re-export analysis schema version for bindings.
#[cfg(feature = "analysis")]
pub const CORE_ANALYSIS_SCHEMA_VERSION: u32 = tokmd_analysis_types::ANALYSIS_SCHEMA_VERSION;

/// Get the current tokmd version.
pub fn version() -> &'static str {
    env!("CARGO_PKG_VERSION")
}

#[cfg(test)]
mod tests {
    use super::*;
    #[cfg(feature = "analysis")]
    use crate::settings::AnalyzeSettings;
    #[cfg(feature = "analysis")]
    use std::fs;
    #[cfg(feature = "analysis")]
    use std::path::{Path, PathBuf};
    #[cfg(feature = "analysis")]
    use std::time::{SystemTime, UNIX_EPOCH};

    #[cfg(feature = "analysis")]
    #[derive(Debug)]
    struct TempDirGuard(PathBuf);

    #[cfg(feature = "analysis")]
    impl Drop for TempDirGuard {
        fn drop(&mut self) {
            let _ = fs::remove_dir_all(&self.0);
        }
    }

    #[test]
    fn version_not_empty() {
        assert!(!version().is_empty());
    }

    #[test]
    fn settings_to_scan_options_preserves_values() {
        let scan = ScanSettings {
            paths: vec!["src".to_string()],
            options: ScanOptions {
                excluded: vec!["target".to_string()],
                hidden: true,
                no_ignore: true,
                ..Default::default()
            },
        };

        let opts = settings_to_scan_options(&scan);
        assert_eq!(opts.excluded, vec!["target"]);
        assert!(opts.hidden);
        assert!(opts.no_ignore);
    }

    #[test]
    fn scan_settings_current_dir() {
        let settings = ScanSettings::current_dir();
        assert_eq!(settings.paths, vec!["."]);
    }

    #[test]
    fn scan_settings_for_paths() {
        let settings = ScanSettings::for_paths(vec!["src".to_string(), "lib".to_string()]);
        assert_eq!(settings.paths, vec!["src", "lib"]);
    }

    #[cfg(feature = "analysis")]
    #[test]
    fn effort_request_defaults_to_estimate_preset() {
        let analyze = AnalyzeSettings {
            preset: "estimate".to_string(),
            ..Default::default()
        };
        let req = parse_effort_request(&analyze, "estimate").expect("parse effort request");
        let req = req.expect("estimate should imply effort request");
        assert_eq!(
            req.model.as_str(),
            analysis::EffortModelKind::Cocomo81Basic.as_str()
        );
        assert_eq!(req.layer.as_str(), analysis::EffortLayer::Full.as_str());
    }

    #[cfg(feature = "analysis")]
    #[test]
    fn effort_request_not_implied_for_non_estimate_without_flags() {
        let analyze = AnalyzeSettings {
            preset: "receipt".to_string(),
            ..Default::default()
        };
        let req = parse_effort_request(&analyze, "receipt").expect("parse effort request");
        assert!(req.is_none());
    }

    #[cfg(feature = "analysis")]
    #[test]
    fn effort_request_rejects_unsupported_model() {
        let analyze = AnalyzeSettings {
            preset: "estimate".to_string(),
            effort_model: Some("cocomo2-early".to_string()),
            ..Default::default()
        };
        let err =
            parse_effort_request(&analyze, "estimate").expect_err("unsupported model should fail");
        assert!(err.to_string().contains("only 'cocomo81-basic'"));
    }

    #[cfg(feature = "analysis")]
    fn mk_temp_dir(prefix: &str) -> PathBuf {
        let timestamp = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_nanos();
        let mut root = std::env::temp_dir();
        root.push(format!("{prefix}-{timestamp}-{}", std::process::id()));
        root
    }

    #[cfg(feature = "analysis")]
    fn write_file(path: &Path, contents: &str) {
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent).unwrap();
        }
        fs::write(path, contents).unwrap();
    }

    #[cfg(feature = "analysis")]
    #[test]
    fn analyze_workflow_estimate_preset_populates_effort_and_size_basis_breakdown() {
        let root = mk_temp_dir("tokmd-core-estimate-preset");
        let _guard = TempDirGuard(root.clone());
        write_file(&root.join("src/main.rs"), "fn main() {}\n");
        write_file(
            &root.join("target/generated/bundle.min.js"),
            "console.log(1);\n",
        );
        write_file(
            &root.join("vendor/lib/external.rs"),
            "pub fn external() {}\n",
        );

        let scan = settings::ScanSettings::for_paths(vec![root.display().to_string()]);
        let analyze = AnalyzeSettings {
            preset: "estimate".to_string(),
            ..Default::default()
        };

        let receipt = analyze_workflow(&scan, &analyze).expect("estimate analyze failed");
        let effort = receipt
            .effort
            .as_ref()
            .expect("estimate preset should produce effort");

        assert!(effort.results.effort_pm_p50 > 0.0);
        assert_eq!(
            effort.size_basis.total_lines,
            effort.size_basis.authored_lines
                + effort.size_basis.generated_lines
                + effort.size_basis.vendored_lines
        );
        assert!(effort.size_basis.authored_lines > 0);
        assert!(
            effort.size_basis.generated_lines + effort.size_basis.vendored_lines > 0,
            "expected deterministic generated or vendored lines"
        );
    }
}

// =============================================================================
// Mutation-killing tests for private functions
// These target surviving mutants identified in conveyor verification run.
// =============================================================================

#[cfg(test)]
mod mutation_tests {
    use super::*;
    use tokmd_settings::ExportSettings;
    use tokmd_types::ExportData;
    use tokmd_types::RedactMode;

    // Helper to create minimal ExportData
    fn empty_export_data() -> ExportData {
        ExportData {
            rows: vec![],
            module_roots: vec![],
            module_depth: 3,
            children: tokmd_types::ChildIncludeMode::Separate,
        }
    }

    // Helper to create minimal ScanOptions
    fn minimal_scan_opts() -> ScanOptions {
        ScanOptions {
            excluded: vec![],
            config: tokmd_types::ConfigMode::Auto,
            hidden: false,
            no_ignore: false,
            no_ignore_parent: false,
            no_ignore_dot: false,
            no_ignore_vcs: false,
            treat_doc_strings_as_comments: false,
        }
    }

    // Helper to create ExportSettings with specific redact/strip_prefix
    fn export_settings(redact: RedactMode, strip_prefix: Option<String>) -> ExportSettings {
        ExportSettings {
            format: tokmd_settings::ExportFormat::Json,
            module_roots: vec![],
            module_depth: 3,
            children: tokmd_types::ChildIncludeMode::Separate,
            min_code: 1,
            max_rows: 1000,
            redact,
            meta: true,
            strip_prefix,
        }
    }

    // =============================================================================
    // parse_analysis_preset — Kill 9/12 untested match arms
    // =============================================================================

    #[test]
    #[cfg(feature = "analysis")]
    fn parse_analysis_preset_all_twelve_variants() {
        #[cfg(feature = "analysis")]
        use tokmd_analysis::AnalysisPreset;

        let variants = [
            ("receipt", AnalysisPreset::Receipt),
            ("estimate", AnalysisPreset::Estimate),
            ("health", AnalysisPreset::Health),
            ("risk", AnalysisPreset::Risk),
            ("supply", AnalysisPreset::Supply),
            ("architecture", AnalysisPreset::Architecture),
            ("topics", AnalysisPreset::Topics),
            ("security", AnalysisPreset::Security),
            ("identity", AnalysisPreset::Identity),
            ("git", AnalysisPreset::Git),
            ("deep", AnalysisPreset::Deep),
            ("fun", AnalysisPreset::Fun),
        ];

        for (input, expected) in &variants {
            // Test exact lowercase
            let (preset, normalized) = parse_analysis_preset(input).unwrap();
            assert_eq!(preset, *expected, "Exact match failed for: {}", input);
            assert_eq!(normalized, *input, "Normalization failed for: {}", input);

            // Test uppercase (normalization)
            let upper = input.to_uppercase();
            let (preset, normalized) = parse_analysis_preset(&upper).unwrap();
            assert_eq!(preset, *expected, "Uppercase match failed for: {}", upper);
            assert_eq!(
                normalized, *input,
                "Uppercase normalization failed for: {}",
                upper
            );

            // Test mixed case with whitespace (normalization)
            let mixed = format!("  {}  ", input);
            let (preset, normalized) = parse_analysis_preset(&mixed).unwrap();
            assert_eq!(preset, *expected, "Mixed case match failed for: {}", mixed);
            assert_eq!(
                normalized, *input,
                "Mixed case normalization failed for: {}",
                mixed
            );
        }
    }

    #[test]
    #[cfg(feature = "analysis")]
    fn parse_analysis_preset_invalid_variants_fail() {
        let invalid = [
            "unknown",
            "invalid",
            "",
            "receipts",         // typo
            "healthh",          // typo
            "ARCH",             // partial match
            "receipt_estimate", // combined
        ];

        for input in &invalid {
            assert!(
                parse_analysis_preset(input).is_err(),
                "Should fail for invalid input: {}",
                input
            );
        }
    }

    // =============================================================================
    // build_export_receipt — Kill && → || mutation on strip_prefix_redacted
    // =============================================================================

    #[test]
    fn build_export_receipt_redact_paths_with_strip_prefix() {
        let settings = export_settings(RedactMode::Paths, Some("/project".to_string()));
        let data = empty_export_data();
        let paths = vec![PathBuf::from("/project/src/main.rs")];

        let receipt = build_export_receipt(&paths, &minimal_scan_opts(), &settings, data);

        // strip_prefix_redacted = should_redact && strip_prefix.is_some()
        // = true && true = true
        assert!(
            receipt.args.strip_prefix_redacted,
            "strip_prefix_redacted should be true when redact=Paths and strip_prefix=Some"
        );
    }

    #[test]
    fn build_export_receipt_redact_paths_without_strip_prefix() {
        let settings = export_settings(RedactMode::Paths, None);
        let data = empty_export_data();
        let paths = vec![PathBuf::from("/project/src/main.rs")];

        let receipt = build_export_receipt(&paths, &minimal_scan_opts(), &settings, data);

        // strip_prefix_redacted = should_redact && strip_prefix.is_some()
        // = true && false = false
        // This kills the && → || mutation (|| would give true)
        assert!(
            !receipt.args.strip_prefix_redacted,
            "strip_prefix_redacted should be false when strip_prefix=None (kills &&→||)"
        );
    }

    #[test]
    fn build_export_receipt_no_redact_with_strip_prefix() {
        let settings = export_settings(RedactMode::None, Some("/project".to_string()));
        let data = empty_export_data();
        let paths = vec![PathBuf::from("/project/src/main.rs")];

        let receipt = build_export_receipt(&paths, &minimal_scan_opts(), &settings, data);

        // strip_prefix_redacted = should_redact && strip_prefix.is_some()
        // = false && true = false
        assert!(
            !receipt.args.strip_prefix_redacted,
            "strip_prefix_redacted should be false when redact=None"
        );
    }

    #[test]
    fn build_export_receipt_redact_all_with_strip_prefix() {
        let settings = export_settings(RedactMode::All, Some("/project".to_string()));
        let data = empty_export_data();
        let paths = vec![PathBuf::from("/project/src/main.rs")];

        let receipt = build_export_receipt(&paths, &minimal_scan_opts(), &settings, data);

        // strip_prefix_redacted = should_redact && strip_prefix.is_some()
        // = true && true = true (All also triggers should_redact)
        assert!(
            receipt.args.strip_prefix_redacted,
            "strip_prefix_redacted should be true when redact=All and strip_prefix=Some"
        );
    }

    #[test]
    fn build_export_receipt_redact_all_without_strip_prefix() {
        let settings = export_settings(RedactMode::All, None);
        let data = empty_export_data();
        let paths = vec![PathBuf::from("/project/src/main.rs")];

        let receipt = build_export_receipt(&paths, &minimal_scan_opts(), &settings, data);

        // strip_prefix_redacted = should_redact && strip_prefix.is_some()
        // = true && false = false
        // This kills the && → || mutation
        assert!(
            !receipt.args.strip_prefix_redacted,
            "strip_prefix_redacted should be false when strip_prefix=None (kills &&→||)"
        );
    }

    #[test]
    fn build_export_receipt_strip_prefix_redaction_logic() {
        // Test the ternary logic: strip_prefix redaction in ExportArgsMeta
        // Kills mutations that change the if/else logic on strip_prefix

        // Case 1: redact=Paths → strip_prefix should be redacted
        let settings = export_settings(RedactMode::Paths, Some("/project".to_string()));
        let data = empty_export_data();
        let paths = vec![PathBuf::from("/project/src/main.rs")];
        let receipt = build_export_receipt(&paths, &minimal_scan_opts(), &settings, data);

        // When redacted, strip_prefix should be transformed (not the original)
        assert!(receipt.args.strip_prefix.is_some());
        assert_ne!(
            receipt.args.strip_prefix,
            Some("/project".to_string()),
            "strip_prefix should be redacted/transformed when redact=Paths"
        );

        // Case 2: redact=None → strip_prefix should pass through unchanged
        let settings = export_settings(RedactMode::None, Some("/project".to_string()));
        let data = empty_export_data();
        let receipt = build_export_receipt(&paths, &minimal_scan_opts(), &settings, data);

        assert_eq!(
            receipt.args.strip_prefix,
            Some("/project".to_string()),
            "strip_prefix should pass through unchanged when redact=None"
        );

        // Case 3: redact=All → strip_prefix should be redacted
        let settings = export_settings(RedactMode::All, Some("/project".to_string()));
        let data = empty_export_data();
        let receipt = build_export_receipt(&paths, &minimal_scan_opts(), &settings, data);

        assert!(receipt.args.strip_prefix.is_some());
        assert_ne!(
            receipt.args.strip_prefix,
            Some("/project".to_string()),
            "strip_prefix should be redacted when redact=All"
        );
    }

    #[test]
    #[cfg(feature = "analysis")]
    fn parse_analysis_preset_normalization_edge_cases() {
        // Kills mutations that remove .trim() or .to_ascii_lowercase()

        // Test trim removal
        let (preset, _) = parse_analysis_preset("  receipt  ").unwrap();
        assert_eq!(
            preset,
            tokmd_analysis::AnalysisPreset::Receipt,
            "Leading/trailing whitespace should be trimmed"
        );

        let (preset, _) = parse_analysis_preset("\tHEALTH\n").unwrap();
        assert_eq!(
            preset,
            tokmd_analysis::AnalysisPreset::Health,
            "Tabs and newlines should be trimmed, case normalized"
        );

        // Test to_ascii_lowercase removal
        let (preset, _) = parse_analysis_preset("ReCeIpT").unwrap();
        assert_eq!(
            preset,
            tokmd_analysis::AnalysisPreset::Receipt,
            "Mixed case should be normalized to lowercase"
        );

        let (preset, _) = parse_analysis_preset("ESTIMATE").unwrap();
        assert_eq!(
            preset,
            tokmd_analysis::AnalysisPreset::Estimate,
            "Uppercase should be normalized"
        );

        // Test combined trim + lowercase
        let (preset, normalized) = parse_analysis_preset("  DeEp  ").unwrap();
        assert_eq!(preset, tokmd_analysis::AnalysisPreset::Deep);
        assert_eq!(normalized, "deep", "Should be trimmed and lowercased");
    }

    // =============================================================================
    // cockpit_workflow — Kill boolean logic mutations (requires git + cockpit feature)
    // =============================================================================

    #[cfg(feature = "cockpit")]
    #[test]
    fn cockpit_workflow_range_mode_parsing() {
        assert!(matches!(
            parse_cockpit_range_mode("three-dot").expect("three-dot should parse"),
            tokmd_git::GitRangeMode::ThreeDot
        ));
        assert!(matches!(
            parse_cockpit_range_mode("3dot").expect("3dot should parse"),
            tokmd_git::GitRangeMode::ThreeDot
        ));
        assert!(matches!(
            parse_cockpit_range_mode("two-dot").expect("two-dot should parse"),
            tokmd_git::GitRangeMode::TwoDot
        ));
        assert!(matches!(
            parse_cockpit_range_mode("2dot").expect("2dot should parse"),
            tokmd_git::GitRangeMode::TwoDot
        ));
        assert!(matches!(
            parse_cockpit_range_mode("  THREE-DOT  ").expect("trimmed/case-insensitive parse"),
            tokmd_git::GitRangeMode::ThreeDot
        ));
    }

    #[cfg(feature = "cockpit")]
    #[test]
    fn cockpit_workflow_range_mode_invalid_rejected() {
        let err = parse_cockpit_range_mode("invalid").expect_err("invalid mode should fail");
        let msg = err.to_string();
        assert!(
            msg.contains("range_mode"),
            "Error should reference range_mode field; got: {msg}"
        );
    }
}

#[cfg(doctest)]
#[doc = include_str!("../README.md")]
pub mod readme_doctests {}