datasynth-cli 2.5.0

Command-line interface for synthetic enterprise data generation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
//! Comprehensive output writer for all generated data.
//!
//! Writes all generated data from the EnhancedGenerationResult to files
//! in the output directory. Uses CSV for flat tabular data (journal entry
//! lines) and JSON for types with nested structures (Vecs, sub-structs).

use std::cell::Cell;
use std::io::Write;
use std::path::Path;

use datasynth_core::documents::PaymentType;
use datasynth_runtime::enhanced_orchestrator::EnhancedGenerationResult;
use tracing::{info, warn};

thread_local! {
    /// Thread-local flat-layout flag. When true, every `write_json_safe` call
    /// routes through `write_json_flat` so nested `{header, lines}` shapes get
    /// flattened. Set by `write_all_output_with_layout` at the top of its body,
    /// reset on exit.
    static FLAT_LAYOUT_ACTIVE: Cell<bool> = const { Cell::new(false) };

    /// Thread-local JSON skip flag. When true, `write_json_safe` becomes a no-op.
    /// Set by `write_all_output_with_layout` when the requested formats don't
    /// include JSON. This avoids wrapping 190+ call sites in `if write_json`.
    static SKIP_JSON: Cell<bool> = const { Cell::new(false) };
}

/// Write a JSON file for any serializable slice. Skips empty slices.
///
/// Streams JSON directly to a buffered file writer instead of allocating
/// the entire JSON string in memory (Phase 3 I/O optimization).
/// Write a JSON array by streaming one record at a time.
///
/// Instead of serializing the entire `&[T]` in one `to_writer_pretty` call
/// (which builds a massive in-memory serde state for large arrays), this
/// writes `[\n` + per-record pretty-printed JSON with commas + `\n]`.
///
/// For 200K+ records this reduces peak memory and improves write throughput
/// by avoiding serde's internal buffering of the full array structure.
fn write_json<T: serde::Serialize>(
    data: &[T],
    path: &Path,
    label: &str,
) -> Result<(), Box<dyn std::error::Error>> {
    use std::io::Write;

    if data.is_empty() {
        return Ok(());
    }

    let file = std::fs::File::create(path)?;
    let mut writer = std::io::BufWriter::with_capacity(512 * 1024, file);

    // Stream records one at a time into a JSON array
    writer.write_all(b"[\n")?;
    for (i, item) in data.iter().enumerate() {
        if i > 0 {
            writer.write_all(b",\n")?;
        }
        serde_json::to_writer_pretty(&mut writer, item)?;
    }
    writer.write_all(b"\n]\n")?;
    writer.flush()?;

    info!(
        "  {} written: {} records -> {}",
        label,
        data.len(),
        path.display()
    );
    Ok(())
}

/// Write journal entry lines as a flat CSV file.
///
/// This extracts the key fields from both the header and each line item to
/// produce a single flat CSV that can be loaded directly into dataframes.
fn write_journal_entries_csv(
    result: &EnhancedGenerationResult,
    output_dir: &Path,
) -> Result<(), Box<dyn std::error::Error>> {
    if result.journal_entries.is_empty() {
        return Ok(());
    }

    let path = output_dir.join("journal_entries.csv");
    let file = std::fs::File::create(&path)?;
    let mut w = std::io::BufWriter::with_capacity(256 * 1024, file);

    // Write header
    writeln!(
        w,
        "document_id,company_code,fiscal_year,fiscal_period,posting_date,document_date,\
         document_type,currency,exchange_rate,reference,header_text,created_by,source,\
         business_process,ledger,is_fraud,is_anomaly,\
         line_number,gl_account,debit_amount,credit_amount,local_amount,\
         cost_center,profit_center,line_text,\
         auxiliary_account_number,auxiliary_account_label,lettrage,lettrage_date"
    )?;

    for je in &result.journal_entries {
        let h = &je.header;
        for line in &je.lines {
            let lettrage_date_str = line
                .lettrage_date
                .map(|d| d.to_string())
                .unwrap_or_default();
            writeln!(
                w,
                "{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}",
                h.document_id,
                csv_escape(&h.company_code),
                h.fiscal_year,
                h.fiscal_period,
                h.posting_date,
                h.document_date,
                csv_escape(&h.document_type),
                csv_escape(&h.currency),
                h.exchange_rate,
                csv_opt_str(&h.reference),
                csv_opt_str(&h.header_text),
                csv_escape(&h.created_by),
                h.source,
                h.business_process
                    .map(|bp| format!("{bp:?}"))
                    .unwrap_or_default(),
                csv_escape(&h.ledger),
                h.is_fraud,
                h.is_anomaly,
                line.line_number,
                csv_escape(&line.gl_account),
                line.debit_amount,
                line.credit_amount,
                line.local_amount,
                csv_opt_str(&line.cost_center),
                csv_opt_str(&line.profit_center),
                csv_opt_str(&line.line_text),
                csv_opt_str(&line.auxiliary_account_number),
                csv_opt_str(&line.auxiliary_account_label),
                csv_opt_str(&line.lettrage),
                lettrage_date_str,
            )?;
        }
    }

    w.flush()?;
    let total_lines: usize = result.journal_entries.iter().map(|je| je.lines.len()).sum();
    info!(
        "  Journal entries CSV written: {} entries, {} line items -> {}",
        result.journal_entries.len(),
        total_lines,
        path.display()
    );
    Ok(())
}

/// Write journal entries as flat JSON (header fields merged onto each line).
///
/// Each object in the output array contains all header fields plus all line fields,
/// with no nesting. This is the analytics-friendly format.
fn write_journal_entries_flat_json(
    result: &EnhancedGenerationResult,
    output_dir: &Path,
) -> Result<(), Box<dyn std::error::Error>> {
    if result.journal_entries.is_empty() {
        return Ok(());
    }

    let path = output_dir.join("journal_entries.json");
    let file = std::fs::File::create(&path)?;
    let mut writer = std::io::BufWriter::with_capacity(256 * 1024, file);

    // Write opening bracket
    writer.write_all(b"[\n")?;

    let mut first = true;
    let mut total_lines = 0usize;
    for je in &result.journal_entries {
        // Serialize header to a JSON map
        let header_value = serde_json::to_value(&je.header)?;

        for line in &je.lines {
            if !first {
                writer.write_all(b",\n")?;
            }
            first = false;
            total_lines += 1;

            // Serialize line to a JSON map, then merge header fields in
            let mut line_value = serde_json::to_value(line)?;

            if let serde_json::Value::Object(ref header_map) = header_value {
                if let serde_json::Value::Object(ref mut line_map) = line_value {
                    for (key, val) in header_map {
                        // Line fields take precedence for shared keys (e.g. document_id)
                        if !line_map.contains_key(key) {
                            line_map.insert(key.clone(), val.clone());
                        }
                    }
                }
            }

            serde_json::to_writer_pretty(&mut writer, &line_value)?;
        }
    }

    writer.write_all(b"\n]\n")?;
    writer.flush()?;
    info!(
        "  Journal entries (flat JSON) written: {} line items -> {}",
        total_lines,
        path.display()
    );
    Ok(())
}

/// Escape a string for CSV output by quoting if it contains commas or quotes.
fn csv_escape(s: &str) -> String {
    if s.contains(',') || s.contains('"') || s.contains('\n') {
        format!("\"{}\"", s.replace('"', "\"\""))
    } else {
        s.to_string()
    }
}

/// Format an Option<String> for CSV output (empty string for None).
fn csv_opt_str(opt: &Option<String>) -> String {
    match opt {
        Some(s) => csv_escape(s),
        None => String::new(),
    }
}

/// Write all generated data to the output directory.
///
/// This function exports every non-empty dataset from the generation result.
/// Journal entries are written as a flat CSV file (one row per line item)
/// and as a nested JSON file. Other data is written as JSON files since
/// many model types contain nested structures.
#[allow(dead_code)]
pub fn write_all_output(
    result: &EnhancedGenerationResult,
    output_dir: &Path,
) -> Result<(), Box<dyn std::error::Error>> {
    write_all_output_with_layout(
        result,
        output_dir,
        datasynth_config::ExportLayout::Nested,
        &[
            datasynth_config::FileFormat::Csv,
            datasynth_config::FileFormat::Json,
        ],
    )
}

/// Write all generated data with a configurable export layout and format set.
///
/// Only writes files for formats present in `formats`. If `formats` is empty,
/// writes both CSV and JSON (backward compatible). This allows skipping JSON
/// when only CSV is needed, which halves output time for large datasets.
pub fn write_all_output_with_layout(
    result: &EnhancedGenerationResult,
    output_dir: &Path,
    export_layout: datasynth_config::ExportLayout,
    formats: &[datasynth_config::FileFormat],
) -> Result<(), Box<dyn std::error::Error>> {
    let csv_enabled = formats.is_empty()
        || formats.contains(&datasynth_config::FileFormat::Csv)
        || formats.contains(&datasynth_config::FileFormat::Parquet);
    let json_enabled = formats.is_empty()
        || formats.contains(&datasynth_config::FileFormat::Json)
        || formats.contains(&datasynth_config::FileFormat::JsonLines);
    std::fs::create_dir_all(output_dir)?;
    info!("Writing comprehensive output to: {}", output_dir.display());

    // Set flat-layout flag for all `write_json_safe` calls in this pass.
    // Scope guard ensures we reset on return (including error paths).
    struct FlatLayoutGuard;
    impl Drop for FlatLayoutGuard {
        fn drop(&mut self) {
            FLAT_LAYOUT_ACTIVE.with(|c| c.set(false));
        }
    }
    let _flat_guard = if export_layout == datasynth_config::ExportLayout::Flat {
        FLAT_LAYOUT_ACTIVE.with(|c| c.set(true));
        Some(FlatLayoutGuard)
    } else {
        None
    };

    // Set JSON skip flag so `write_json_safe` becomes a no-op when JSON not requested.
    struct SkipJsonGuard;
    impl Drop for SkipJsonGuard {
        fn drop(&mut self) {
            SKIP_JSON.with(|c| c.set(false));
        }
    }
    let _skip_json_guard = if !json_enabled {
        SKIP_JSON.with(|c| c.set(true));
        info!("JSON output skipped (not in requested formats)");
        Some(SkipJsonGuard)
    } else {
        None
    };

    // ========================================================================
    // Journal Entries (CSV + JSON in parallel when both enabled)
    // ========================================================================
    if !result.journal_entries.is_empty() {
        let do_csv = csv_enabled;
        let do_json = json_enabled;
        let is_flat = export_layout == datasynth_config::ExportLayout::Flat;

        std::thread::scope(|s| {
            if do_csv {
                s.spawn(|| {
                    if let Err(e) = write_journal_entries_csv(result, output_dir) {
                        warn!("Failed to write journal_entries.csv: {}", e);
                    }
                });
            }
            if do_json {
                s.spawn(|| {
                    if is_flat {
                        if let Err(e) = write_journal_entries_flat_json(result, output_dir) {
                            warn!("Failed to write flat journal_entries.json: {}", e);
                        }
                    } else if let Err(e) = write_json(
                        &result.journal_entries,
                        &output_dir.join("journal_entries.json"),
                        "Journal entries (JSON)",
                    ) {
                        warn!("Failed to write journal_entries.json: {}", e);
                    }
                });
            }
        });
    }

    // ========================================================================
    // Master Data
    // ========================================================================
    let md_dir = output_dir.join("master_data");
    if !result.master_data.vendors.is_empty()
        || !result.master_data.customers.is_empty()
        || !result.master_data.materials.is_empty()
        || !result.master_data.assets.is_empty()
        || !result.master_data.employees.is_empty()
        || !result.master_data.cost_centers.is_empty()
    {
        std::fs::create_dir_all(&md_dir)?;
        info!("Writing master data...");

        write_json_safe(
            &result.master_data.vendors,
            &md_dir.join("vendors.json"),
            "Vendors",
        );
        write_json_safe(
            &result.master_data.customers,
            &md_dir.join("customers.json"),
            "Customers",
        );
        write_json_safe(
            &result.master_data.materials,
            &md_dir.join("materials.json"),
            "Materials",
        );
        write_json_safe(
            &result.master_data.assets,
            &md_dir.join("fixed_assets.json"),
            "Fixed assets",
        );
        write_json_safe(
            &result.master_data.employees,
            &md_dir.join("employees.json"),
            "Employees",
        );
        write_json_safe(
            &result.master_data.cost_centers,
            &md_dir.join("cost_centers.json"),
            "Cost centers",
        );
    }

    // ========================================================================
    // Document Flows
    // ========================================================================
    let df_dir = output_dir.join("document_flows");
    let flat_mode = export_layout == datasynth_config::ExportLayout::Flat;
    if !result.document_flows.purchase_orders.is_empty()
        || !result.document_flows.sales_orders.is_empty()
    {
        std::fs::create_dir_all(&df_dir)?;
        info!("Writing document flows...");

        write_json_auto(
            &result.document_flows.purchase_orders,
            &df_dir.join("purchase_orders.json"),
            "Purchase orders",
            flat_mode,
        );
        write_json_auto(
            &result.document_flows.goods_receipts,
            &df_dir.join("goods_receipts.json"),
            "Goods receipts",
            flat_mode,
        );
        write_json_auto(
            &result.document_flows.vendor_invoices,
            &df_dir.join("vendor_invoices.json"),
            "Vendor invoices",
            flat_mode,
        );
        write_json_auto(
            &result.document_flows.payments,
            &df_dir.join("payments.json"),
            "Payments",
            flat_mode,
        );
        let customer_receipts: Vec<_> = result
            .document_flows
            .payments
            .iter()
            .filter(|p| p.payment_type == PaymentType::ArReceipt)
            .collect();
        write_json_auto(
            &customer_receipts,
            &df_dir.join("customer_receipts.json"),
            "Customer receipts",
            flat_mode,
        );
        write_json_auto(
            &result.document_flows.sales_orders,
            &df_dir.join("sales_orders.json"),
            "Sales orders",
            flat_mode,
        );
        write_json_auto(
            &result.document_flows.deliveries,
            &df_dir.join("deliveries.json"),
            "Deliveries",
            flat_mode,
        );
        write_json_auto(
            &result.document_flows.customer_invoices,
            &df_dir.join("customer_invoices.json"),
            "Customer invoices",
            flat_mode,
        );

        // Document cross-references (PO→GR, GR→Invoice, Invoice→Payment, etc.)
        write_json_safe(
            &result.document_flows.document_references,
            &df_dir.join("document_references.json"),
            "Document references",
        );

        // Note: P2P/O2C chain types do not implement Serialize, so we log
        // their counts instead. The individual documents above capture all data.
        if !result.document_flows.p2p_chains.is_empty() {
            info!(
                "  P2P chains: {} (data exported via individual document files)",
                result.document_flows.p2p_chains.len()
            );
        }
        if !result.document_flows.o2c_chains.is_empty() {
            info!(
                "  O2C chains: {} (data exported via individual document files)",
                result.document_flows.o2c_chains.len()
            );
        }
    }

    // ========================================================================
    // Subledger
    // ========================================================================
    let sl_dir = output_dir.join("subledger");
    if !result.subledger.ap_invoices.is_empty()
        || !result.subledger.ar_invoices.is_empty()
        || !result.subledger.fa_records.is_empty()
        || !result.subledger.inventory_positions.is_empty()
    {
        std::fs::create_dir_all(&sl_dir)?;
        info!("Writing subledger data...");

        write_json_safe(
            &result.subledger.ap_invoices,
            &sl_dir.join("ap_invoices.json"),
            "AP invoices",
        );
        write_json_safe(
            &result.subledger.ar_invoices,
            &sl_dir.join("ar_invoices.json"),
            "AR invoices",
        );
        write_json_safe(
            &result.subledger.fa_records,
            &sl_dir.join("fa_records.json"),
            "FA records",
        );
        write_json_safe(
            &result.subledger.inventory_positions,
            &sl_dir.join("inventory_positions.json"),
            "Inventory positions",
        );
        write_json_safe(
            &result.subledger.inventory_movements,
            &sl_dir.join("inventory_movements.json"),
            "Inventory movements",
        );
        write_json_safe(
            &result.subledger.ar_aging_reports,
            &sl_dir.join("ar_aging.json"),
            "AR aging reports",
        );
        write_json_safe(
            &result.subledger.ap_aging_reports,
            &sl_dir.join("ap_aging.json"),
            "AP aging reports",
        );
        write_json_safe(
            &result.subledger.depreciation_runs,
            &sl_dir.join("depreciation_runs.json"),
            "Depreciation runs",
        );
        write_json_safe(
            &result.subledger.inventory_valuations,
            &sl_dir.join("inventory_valuation.json"),
            "Inventory valuations",
        );
        // Dunning runs and letters (generated after AR aging)
        write_json_safe(
            &result.subledger.dunning_runs,
            &sl_dir.join("dunning_runs.json"),
            "Dunning runs",
        );
        write_json_safe(
            &result.subledger.dunning_letters,
            &sl_dir.join("dunning_letters.json"),
            "Dunning letters",
        );
    }

    // ========================================================================
    // Audit
    // ========================================================================
    let audit_dir = output_dir.join("audit");
    if !result.audit.engagements.is_empty() {
        std::fs::create_dir_all(&audit_dir)?;
        info!("Writing audit data...");

        write_json_safe(
            &result.audit.engagements,
            &audit_dir.join("audit_engagements.json"),
            "Audit engagements",
        );
        write_json_safe(
            &result.audit.audit_scopes,
            &audit_dir.join("audit_scopes.json"),
            "Audit scopes (ISA 220 / ISA 300)",
        );
        write_json_safe(
            &result.audit.workpapers,
            &audit_dir.join("audit_workpapers.json"),
            "Audit workpapers",
        );
        write_json_safe(
            &result.audit.evidence,
            &audit_dir.join("audit_evidence.json"),
            "Audit evidence",
        );
        write_json_safe(
            &result.audit.risk_assessments,
            &audit_dir.join("audit_risk_assessments.json"),
            "Audit risk assessments",
        );
        write_json_safe(
            &result.audit.findings,
            &audit_dir.join("audit_findings.json"),
            "Audit findings",
        );
        write_json_safe(
            &result.audit.judgments,
            &audit_dir.join("audit_judgments.json"),
            "Audit judgments",
        );
        write_json_safe(
            &result.audit.confirmations,
            &audit_dir.join("audit_confirmations.json"),
            "Audit confirmations",
        );
        write_json_safe(
            &result.audit.confirmation_responses,
            &audit_dir.join("audit_confirmation_responses.json"),
            "Audit confirmation responses",
        );
        write_json_safe(
            &result.audit.procedure_steps,
            &audit_dir.join("audit_procedure_steps.json"),
            "Audit procedure steps",
        );
        write_json_safe(
            &result.audit.samples,
            &audit_dir.join("audit_samples.json"),
            "Audit samples",
        );
        write_json_safe(
            &result.audit.analytical_results,
            &audit_dir.join("audit_analytical_results.json"),
            "Audit analytical results",
        );
        write_json_safe(
            &result.audit.ia_functions,
            &audit_dir.join("audit_ia_functions.json"),
            "Audit IA functions",
        );
        write_json_safe(
            &result.audit.ia_reports,
            &audit_dir.join("audit_ia_reports.json"),
            "Audit IA reports",
        );
        write_json_safe(
            &result.audit.related_parties,
            &audit_dir.join("audit_related_parties.json"),
            "Audit related parties",
        );
        write_json_safe(
            &result.audit.related_party_transactions,
            &audit_dir.join("audit_related_party_transactions.json"),
            "Audit related party transactions",
        );
        // ISA 600: Group audit artefacts
        if !result.audit.component_auditors.is_empty() {
            write_json_safe(
                &result.audit.component_auditors,
                &audit_dir.join("component_auditors.json"),
                "Component auditors (ISA 600)",
            );
            if let Some(plan) = &result.audit.group_audit_plan {
                write_json_single_safe(
                    plan,
                    &audit_dir.join("group_audit_plan.json"),
                    "Group audit plan (ISA 600)",
                );
            }
            write_json_safe(
                &result.audit.component_instructions,
                &audit_dir.join("component_instructions.json"),
                "Component instructions (ISA 600)",
            );
            write_json_safe(
                &result.audit.component_reports,
                &audit_dir.join("component_reports.json"),
                "Component auditor reports (ISA 600)",
            );
        }
        // ISA 210: Engagement letters
        write_json_safe(
            &result.audit.engagement_letters,
            &audit_dir.join("engagement_letters.json"),
            "Engagement letters (ISA 210)",
        );
        // ISA 560 / IAS 10: Subsequent events
        write_json_safe(
            &result.audit.subsequent_events,
            &audit_dir.join("subsequent_events.json"),
            "Subsequent events (ISA 560 / IAS 10)",
        );
        // ISA 402: Service organization controls
        write_json_safe(
            &result.audit.service_organizations,
            &audit_dir.join("service_organizations.json"),
            "Service organizations (ISA 402)",
        );
        write_json_safe(
            &result.audit.soc_reports,
            &audit_dir.join("soc_reports.json"),
            "SOC reports (ISA 402)",
        );
        write_json_safe(
            &result.audit.user_entity_controls,
            &audit_dir.join("user_entity_controls.json"),
            "User entity controls (ISA 402)",
        );

        // ISA 570: Going concern assessments
        write_json_safe(
            &result.audit.going_concern_assessments,
            &audit_dir.join("going_concern_assessments.json"),
            "Going concern assessments (ISA 570)",
        );

        // ISA 540: Accounting estimates
        write_json_safe(
            &result.audit.accounting_estimates,
            &audit_dir.join("accounting_estimates.json"),
            "Accounting estimates (ISA 540)",
        );

        // ISA 700/701/705/706: Audit opinions and Key Audit Matters
        if !result.audit.audit_opinions.is_empty() {
            write_json_safe(
                &result.audit.audit_opinions,
                &audit_dir.join("audit_opinions.json"),
                "Audit opinions (ISA 700/705/706)",
            );
            write_json_safe(
                &result.audit.key_audit_matters,
                &audit_dir.join("key_audit_matters.json"),
                "Key Audit Matters (ISA 701)",
            );
        }

        // SOX 302 / 404
        if !result.audit.sox_302_certifications.is_empty() {
            write_json_safe(
                &result.audit.sox_302_certifications,
                &audit_dir.join("sox_302_certifications.json"),
                "SOX 302 certifications",
            );
            write_json_safe(
                &result.audit.sox_404_assessments,
                &audit_dir.join("sox_404_assessments.json"),
                "SOX 404 ICFR assessments",
            );
        }

        // ISA 320: Materiality calculations
        if !result.audit.materiality_calculations.is_empty() {
            write_json_safe(
                &result.audit.materiality_calculations,
                &audit_dir.join("materiality_calculations.json"),
                "Materiality calculations (ISA 320)",
            );
        }

        // ISA 315: Combined Risk Assessments
        if !result.audit.combined_risk_assessments.is_empty() {
            write_json_safe(
                &result.audit.combined_risk_assessments,
                &audit_dir.join("combined_risk_assessments.json"),
                "Combined Risk Assessments (ISA 315)",
            );
        }

        // ISA 530: Sampling Plans and Sampled Items
        if !result.audit.sampling_plans.is_empty() {
            write_json_safe(
                &result.audit.sampling_plans,
                &audit_dir.join("sampling_plans.json"),
                "Sampling plans (ISA 530)",
            );
            write_json_safe(
                &result.audit.sampled_items,
                &audit_dir.join("sampled_items.json"),
                "Sampled items (ISA 530)",
            );
        }

        // ISA 315: Significant Classes of Transactions (SCOTS)
        if !result.audit.significant_transaction_classes.is_empty() {
            write_json_safe(
                &result.audit.significant_transaction_classes,
                &audit_dir.join("significant_transaction_classes.json"),
                "Significant Classes of Transactions / SCOTS (ISA 315)",
            );
        }

        // ISA 520: Unusual Item Markers
        if !result.audit.unusual_items.is_empty() {
            write_json_safe(
                &result.audit.unusual_items,
                &audit_dir.join("unusual_items.json"),
                "Unusual item flags (ISA 520)",
            );
        }

        // ISA 520: Analytical Relationships
        if !result.audit.analytical_relationships.is_empty() {
            write_json_safe(
                &result.audit.analytical_relationships,
                &audit_dir.join("analytical_relationships.json"),
                "Analytical relationships (ISA 520)",
            );
        }

        // PCAOB-ISA cross-reference mappings
        if !result.audit.isa_pcaob_mappings.is_empty() {
            write_json_safe(
                &result.audit.isa_pcaob_mappings,
                &audit_dir.join("isa_pcaob_mappings.json"),
                "PCAOB-ISA standard mappings",
            );
        }

        // ISA standard reference (number, title, series for all 34 ISA standards)
        if !result.audit.isa_mappings.is_empty() {
            write_json_safe(
                &result.audit.isa_mappings,
                &audit_dir.join("isa_mappings.json"),
                "ISA standard reference mappings",
            );
        }

        // FSM event trail (when audit.fsm.enabled: true)
        if let Some(ref event_trail) = result.audit.fsm_event_trail {
            if !event_trail.is_empty() {
                write_json_safe(
                    event_trail,
                    &audit_dir.join("fsm_event_trail.json"),
                    "FSM audit event trail",
                );
            }
        }
    }

    // ========================================================================
    // Banking (JSON - keep existing format for backward compat)
    // ========================================================================
    let banking_dir = output_dir.join("banking");
    if !result.banking.customers.is_empty() {
        std::fs::create_dir_all(&banking_dir)?;
        info!("Writing banking data...");

        write_json_safe(
            &result.banking.customers,
            &banking_dir.join("banking_customers.json"),
            "Banking customers",
        );
        write_json_safe(
            &result.banking.accounts,
            &banking_dir.join("banking_accounts.json"),
            "Banking accounts",
        );
        write_json_safe(
            &result.banking.transactions,
            &banking_dir.join("banking_transactions.json"),
            "Banking transactions",
        );
        write_json_safe(
            &result.banking.transaction_labels,
            &banking_dir.join("aml_transaction_labels.json"),
            "AML transaction labels",
        );
        write_json_safe(
            &result.banking.customer_labels,
            &banking_dir.join("aml_customer_labels.json"),
            "AML customer labels",
        );
        write_json_safe(
            &result.banking.account_labels,
            &banking_dir.join("aml_account_labels.json"),
            "AML account labels",
        );
        write_json_safe(
            &result.banking.relationship_labels,
            &banking_dir.join("aml_relationship_labels.json"),
            "AML relationship labels",
        );
        write_json_safe(
            &result.banking.narratives,
            &banking_dir.join("aml_narratives.json"),
            "AML narratives",
        );
    }

    // ========================================================================
    // Sourcing (S2C)
    // ========================================================================
    let s2c_dir = output_dir.join("sourcing");
    if !result.sourcing.spend_analyses.is_empty() || !result.sourcing.sourcing_projects.is_empty() {
        std::fs::create_dir_all(&s2c_dir)?;
        info!("Writing sourcing (S2C) data...");

        write_json_safe(
            &result.sourcing.spend_analyses,
            &s2c_dir.join("spend_analyses.json"),
            "Spend analyses",
        );
        write_json_safe(
            &result.sourcing.sourcing_projects,
            &s2c_dir.join("sourcing_projects.json"),
            "Sourcing projects",
        );
        write_json_safe(
            &result.sourcing.qualifications,
            &s2c_dir.join("supplier_qualifications.json"),
            "Supplier qualifications",
        );
        write_json_safe(
            &result.sourcing.rfx_events,
            &s2c_dir.join("rfx_events.json"),
            "RFx events",
        );
        write_json_safe(
            &result.sourcing.bids,
            &s2c_dir.join("supplier_bids.json"),
            "Supplier bids",
        );
        write_json_safe(
            &result.sourcing.bid_evaluations,
            &s2c_dir.join("bid_evaluations.json"),
            "Bid evaluations",
        );
        write_json_safe(
            &result.sourcing.contracts,
            &s2c_dir.join("procurement_contracts.json"),
            "Procurement contracts",
        );
        write_json_safe(
            &result.sourcing.catalog_items,
            &s2c_dir.join("catalog_items.json"),
            "Catalog items",
        );
        write_json_safe(
            &result.sourcing.scorecards,
            &s2c_dir.join("supplier_scorecards.json"),
            "Supplier scorecards",
        );
    }

    // ========================================================================
    // Intercompany
    // ========================================================================
    let ic_dir = output_dir.join("intercompany");
    if result.intercompany.group_structure.is_some()
        || !result.intercompany.matched_pairs.is_empty()
    {
        std::fs::create_dir_all(&ic_dir)?;
        info!("Writing intercompany data...");

        // Always write group structure when present (independent of IC transactions).
        if let Some(gs) = &result.intercompany.group_structure {
            write_json_single_safe(gs, &ic_dir.join("group_structure.json"), "Group structure");
        }

        write_json_safe(
            &result.intercompany.matched_pairs,
            &ic_dir.join("ic_matched_pairs.json"),
            "IC matched pairs",
        );
        write_json_safe(
            &result.intercompany.seller_journal_entries,
            &ic_dir.join("ic_seller_journal_entries.json"),
            "IC seller journal entries",
        );
        write_json_safe(
            &result.intercompany.buyer_journal_entries,
            &ic_dir.join("ic_buyer_journal_entries.json"),
            "IC buyer journal entries",
        );
        write_json_safe(
            &result.intercompany.elimination_entries,
            &ic_dir.join("ic_elimination_entries.json"),
            "IC elimination entries",
        );

        // NCI measurements from group structure ownership percentages
        if !result.intercompany.nci_measurements.is_empty() {
            write_json_safe(
                &result.intercompany.nci_measurements,
                &ic_dir.join("nci_measurements.json"),
                "NCI measurements",
            );
        }
    }

    // ========================================================================
    // Financial Reporting
    // ========================================================================
    let fin_dir = output_dir.join("financial_reporting");
    if !result.financial_reporting.financial_statements.is_empty()
        || !result.financial_reporting.bank_reconciliations.is_empty()
        || !result
            .financial_reporting
            .consolidated_statements
            .is_empty()
    {
        std::fs::create_dir_all(&fin_dir)?;
        info!("Writing financial reporting data...");

        // Legacy flat file (all standalone statements combined)
        write_json_safe(
            &result.financial_reporting.financial_statements,
            &fin_dir.join("financial_statements.json"),
            "Financial statements",
        );

        // Per-entity standalone statements
        if !result.financial_reporting.standalone_statements.is_empty() {
            let standalone_dir = fin_dir.join("standalone");
            std::fs::create_dir_all(&standalone_dir)?;
            for (entity_code, stmts) in &result.financial_reporting.standalone_statements {
                let file_name = format!("{}_financial_statements.json", entity_code);
                write_json_safe(
                    stmts,
                    &standalone_dir.join(&file_name),
                    &format!("Standalone statements for {}", entity_code),
                );
            }
        }

        // Consolidated statements + schedule
        if !result
            .financial_reporting
            .consolidated_statements
            .is_empty()
            || !result
                .financial_reporting
                .consolidation_schedules
                .is_empty()
        {
            let consolidated_dir = fin_dir.join("consolidated");
            std::fs::create_dir_all(&consolidated_dir)?;
            write_json_safe(
                &result.financial_reporting.consolidated_statements,
                &consolidated_dir.join("consolidated_financial_statements.json"),
                "Consolidated financial statements",
            );
            write_json_safe(
                &result.financial_reporting.consolidation_schedules,
                &consolidated_dir.join("consolidation_schedule.json"),
                "Consolidation schedule",
            );
        }

        write_json_safe(
            &result.financial_reporting.bank_reconciliations,
            &fin_dir.join("bank_reconciliations.json"),
            "Bank reconciliations",
        );

        // IFRS 8 / ASC 280 Segment Reporting
        if !result.financial_reporting.segment_reports.is_empty()
            || !result
                .financial_reporting
                .segment_reconciliations
                .is_empty()
        {
            let seg_dir = fin_dir.join("segment_reporting");
            std::fs::create_dir_all(&seg_dir)?;
            write_json_safe(
                &result.financial_reporting.segment_reports,
                &seg_dir.join("segment_reports.json"),
                "Segment reports",
            );
            write_json_safe(
                &result.financial_reporting.segment_reconciliations,
                &seg_dir.join("segment_reconciliations.json"),
                "Segment reconciliations",
            );
        }

        // IAS 1 / ASC 235: Notes to financial statements
        write_json_safe(
            &result.financial_reporting.notes_to_financial_statements,
            &fin_dir.join("notes_to_financial_statements.json"),
            "Notes to financial statements",
        );
    }

    // ========================================================================
    // Period-Close Trial Balances
    // ========================================================================
    if !result.financial_reporting.trial_balances.is_empty() {
        let pc_dir = output_dir.join("period_close");
        std::fs::create_dir_all(&pc_dir)?;
        info!(
            "Writing {} period-close trial balances...",
            result.financial_reporting.trial_balances.len()
        );
        write_json_safe(
            &result.financial_reporting.trial_balances,
            &pc_dir.join("trial_balances.json"),
            "Period-close trial balances",
        );
    }

    // ========================================================================
    // Balance: Opening Balances + GL-Subledger Reconciliation
    // ========================================================================
    if !result.opening_balances.is_empty() || !result.subledger_reconciliation.is_empty() {
        let balance_dir = output_dir.join("balance");
        std::fs::create_dir_all(&balance_dir)?;
        info!("Writing balance data...");

        write_json_safe(
            &result.opening_balances,
            &balance_dir.join("opening_balances.json"),
            "Opening balances",
        );
        write_json_safe(
            &result.subledger_reconciliation,
            &balance_dir.join("subledger_reconciliation.json"),
            "Subledger reconciliation",
        );
    }

    // ========================================================================
    // HR (Payroll, Time Entries, Expense Reports, Benefit Enrollments, Pensions)
    // ========================================================================
    let hr_dir = output_dir.join("hr");
    if !result.hr.payroll_runs.is_empty()
        || !result.hr.time_entries.is_empty()
        || !result.hr.expense_reports.is_empty()
        || !result.hr.benefit_enrollments.is_empty()
        || !result.hr.pension_plans.is_empty()
        || !result.hr.stock_grants.is_empty()
        || !result.master_data.employee_change_history.is_empty()
    {
        std::fs::create_dir_all(&hr_dir)?;
        info!("Writing HR data...");

        write_json_safe(
            &result.hr.payroll_runs,
            &hr_dir.join("payroll_runs.json"),
            "Payroll runs",
        );
        write_json_safe(
            &result.hr.payroll_line_items,
            &hr_dir.join("payroll_line_items.json"),
            "Payroll line items",
        );
        write_json_safe(
            &result.hr.time_entries,
            &hr_dir.join("time_entries.json"),
            "Time entries",
        );
        write_json_safe(
            &result.hr.expense_reports,
            &hr_dir.join("expense_reports.json"),
            "Expense reports",
        );
        write_json_safe(
            &result.hr.benefit_enrollments,
            &hr_dir.join("benefit_enrollments.json"),
            "Benefit enrollments",
        );
        write_json_safe(
            &result.hr.pension_plans,
            &hr_dir.join("pension_plans.json"),
            "Pension plans",
        );
        write_json_safe(
            &result.hr.pension_obligations,
            &hr_dir.join("pension_obligations.json"),
            "Pension obligations",
        );
        write_json_safe(
            &result.hr.pension_plan_assets,
            &hr_dir.join("plan_assets.json"),
            "Plan assets",
        );
        write_json_safe(
            &result.hr.pension_disclosures,
            &hr_dir.join("pension_disclosures.json"),
            "Pension disclosures",
        );
        write_json_safe(
            &result.hr.stock_grants,
            &hr_dir.join("stock_grants.json"),
            "Stock grants",
        );
        write_json_safe(
            &result.hr.stock_comp_expenses,
            &hr_dir.join("stock_comp_expense.json"),
            "Stock comp expense",
        );
        write_json_safe(
            &result.master_data.employee_change_history,
            &hr_dir.join("employee_change_history.json"),
            "Employee change history",
        );
    }

    // ========================================================================
    // Manufacturing
    // ========================================================================
    let mfg_dir = output_dir.join("manufacturing");
    if !result.manufacturing.production_orders.is_empty()
        || !result.manufacturing.quality_inspections.is_empty()
        || !result.manufacturing.cycle_counts.is_empty()
        || !result.manufacturing.bom_components.is_empty()
        || !result.manufacturing.inventory_movements.is_empty()
    {
        std::fs::create_dir_all(&mfg_dir)?;
        info!("Writing manufacturing data...");

        write_json_safe(
            &result.manufacturing.production_orders,
            &mfg_dir.join("production_orders.json"),
            "Production orders",
        );
        write_json_safe(
            &result.manufacturing.quality_inspections,
            &mfg_dir.join("quality_inspections.json"),
            "Quality inspections",
        );
        write_json_safe(
            &result.manufacturing.cycle_counts,
            &mfg_dir.join("cycle_counts.json"),
            "Cycle counts",
        );
        write_json_safe(
            &result.manufacturing.bom_components,
            &mfg_dir.join("bom_components.json"),
            "BOM components",
        );
        write_json_safe(
            &result.manufacturing.inventory_movements,
            &mfg_dir.join("inventory_movements.json"),
            "Inventory movements",
        );
    }

    // ========================================================================
    // Sales, KPIs, Budgets
    // ========================================================================
    let sales_dir = output_dir.join("sales_kpi_budgets");
    if !result.sales_kpi_budgets.sales_quotes.is_empty()
        || !result.sales_kpi_budgets.kpis.is_empty()
        || !result.sales_kpi_budgets.budgets.is_empty()
    {
        std::fs::create_dir_all(&sales_dir)?;
        info!("Writing sales, KPI, and budget data...");

        write_json_safe(
            &result.sales_kpi_budgets.sales_quotes,
            &sales_dir.join("sales_quotes.json"),
            "Sales quotes",
        );
        write_json_safe(
            &result.sales_kpi_budgets.kpis,
            &sales_dir.join("management_kpis.json"),
            "Management KPIs",
        );
        write_json_safe(
            &result.sales_kpi_budgets.budgets,
            &sales_dir.join("budgets.json"),
            "Budgets",
        );
    }

    // ========================================================================
    // Tax
    // ========================================================================
    let tax_dir = output_dir.join("tax");
    if !result.tax.jurisdictions.is_empty()
        || !result.tax.codes.is_empty()
        || !result.tax.tax_provisions.is_empty()
    {
        std::fs::create_dir_all(&tax_dir)?;
        info!("Writing tax data...");

        write_json_safe(
            &result.tax.jurisdictions,
            &tax_dir.join("tax_jurisdictions.json"),
            "Tax jurisdictions",
        );
        write_json_safe(
            &result.tax.codes,
            &tax_dir.join("tax_codes.json"),
            "Tax codes",
        );
        write_json_safe(
            &result.tax.tax_provisions,
            &tax_dir.join("tax_provisions.json"),
            "Tax provisions",
        );
        write_json_safe(
            &result.tax.tax_lines,
            &tax_dir.join("tax_lines.json"),
            "Tax lines",
        );
        write_json_safe(
            &result.tax.tax_returns,
            &tax_dir.join("tax_returns.json"),
            "Tax returns",
        );
        write_json_safe(
            &result.tax.withholding_records,
            &tax_dir.join("withholding_records.json"),
            "Withholding tax records",
        );
        if !result.tax.tax_anomaly_labels.is_empty() {
            write_json_safe(
                &result.tax.tax_anomaly_labels,
                &tax_dir.join("tax_anomaly_labels.json"),
                "Tax anomaly labels",
            );
        }
        // Deferred tax engine output (IAS 12 / ASC 740)
        if !result.tax.deferred_tax.temporary_differences.is_empty() {
            write_json_safe(
                &result.tax.deferred_tax.temporary_differences,
                &tax_dir.join("temporary_differences.json"),
                "Temporary differences",
            );
            write_json_safe(
                &result.tax.deferred_tax.etr_reconciliations,
                &tax_dir.join("etr_reconciliation.json"),
                "ETR reconciliation",
            );
            write_json_safe(
                &result.tax.deferred_tax.rollforwards,
                &tax_dir.join("deferred_tax_rollforward.json"),
                "Deferred tax rollforward",
            );
            write_json_safe(
                &result.tax.deferred_tax.journal_entries,
                &tax_dir.join("deferred_tax_journal_entries.json"),
                "Deferred tax journal entries",
            );
        }
    }

    // ========================================================================
    // ESG
    // ========================================================================
    let esg_dir = output_dir.join("esg");
    if !result.esg.emissions.is_empty()
        || !result.esg.energy.is_empty()
        || !result.esg.diversity.is_empty()
        || !result.esg.governance.is_empty()
    {
        std::fs::create_dir_all(&esg_dir)?;
        info!("Writing ESG data...");

        write_json_safe(
            &result.esg.emissions,
            &esg_dir.join("emission_records.json"),
            "Emission records",
        );
        write_json_safe(
            &result.esg.energy,
            &esg_dir.join("energy_consumption.json"),
            "Energy consumption",
        );
        write_json_safe(
            &result.esg.water,
            &esg_dir.join("water_usage.json"),
            "Water usage",
        );
        write_json_safe(
            &result.esg.waste,
            &esg_dir.join("waste_records.json"),
            "Waste records",
        );
        write_json_safe(
            &result.esg.diversity,
            &esg_dir.join("workforce_diversity.json"),
            "Workforce diversity",
        );
        write_json_safe(
            &result.esg.pay_equity,
            &esg_dir.join("pay_equity.json"),
            "Pay equity",
        );
        write_json_safe(
            &result.esg.safety_incidents,
            &esg_dir.join("safety_incidents.json"),
            "Safety incidents",
        );
        write_json_safe(
            &result.esg.safety_metrics,
            &esg_dir.join("safety_metrics.json"),
            "Safety metrics",
        );
        write_json_safe(
            &result.esg.governance,
            &esg_dir.join("governance_metrics.json"),
            "Governance metrics",
        );
        write_json_safe(
            &result.esg.supplier_assessments,
            &esg_dir.join("supplier_esg_assessments.json"),
            "Supplier ESG assessments",
        );
        write_json_safe(
            &result.esg.materiality,
            &esg_dir.join("materiality_assessments.json"),
            "Materiality assessments",
        );
        write_json_safe(
            &result.esg.disclosures,
            &esg_dir.join("esg_disclosures.json"),
            "ESG disclosures",
        );
        write_json_safe(
            &result.esg.climate_scenarios,
            &esg_dir.join("climate_scenarios.json"),
            "Climate scenarios",
        );
        write_json_safe(
            &result.esg.anomaly_labels,
            &esg_dir.join("esg_anomaly_labels.json"),
            "ESG anomaly labels",
        );
    }

    // ========================================================================
    // Process Mining (OCPM)
    // ========================================================================
    if let Some(ref event_log) = result.ocpm.event_log {
        if !event_log.events.is_empty() || !event_log.objects.is_empty() {
            let pm_dir = output_dir.join("process_mining");
            std::fs::create_dir_all(&pm_dir)?;
            info!("Writing process mining (OCPM) data...");

            // Write the full OCEL 2.0 event log
            match serde_json::to_string_pretty(event_log) {
                Ok(json) => {
                    if let Err(e) = std::fs::write(pm_dir.join("event_log.json"), json) {
                        warn!("Failed to write OCPM event log: {}", e);
                    } else {
                        info!(
                            "  Event log written: {} events, {} objects",
                            result.ocpm.event_count, result.ocpm.object_count
                        );
                    }
                }
                Err(e) => warn!("Failed to serialize OCPM event log: {}", e),
            }

            // Write events separately for easy consumption
            if !event_log.events.is_empty() {
                match serde_json::to_string_pretty(&event_log.events) {
                    Ok(json) => {
                        if let Err(e) = std::fs::write(pm_dir.join("events.json"), json) {
                            warn!("Failed to write OCPM events: {}", e);
                        } else {
                            info!("  Events written: {} records", event_log.events.len());
                        }
                    }
                    Err(e) => warn!("Failed to serialize OCPM events: {}", e),
                }
            }

            // Write objects separately for easy consumption
            if !event_log.objects.is_empty() {
                let objects: Vec<&_> = event_log.objects.iter().collect();
                match serde_json::to_string_pretty(&objects) {
                    Ok(json) => {
                        if let Err(e) = std::fs::write(pm_dir.join("objects.json"), json) {
                            warn!("Failed to write OCPM objects: {}", e);
                        } else {
                            info!("  Objects written: {} records", event_log.objects.len());
                        }
                    }
                    Err(e) => warn!("Failed to serialize OCPM objects: {}", e),
                }
            }

            // Write process variants if any were computed
            if !event_log.variants.is_empty() {
                let variants: Vec<&_> = event_log.variants.values().collect();
                match serde_json::to_string_pretty(&variants) {
                    Ok(json) => {
                        if let Err(e) = std::fs::write(pm_dir.join("process_variants.json"), json) {
                            warn!("Failed to write process variants: {}", e);
                        } else {
                            info!(
                                "  Process variants written: {} variants",
                                event_log.variants.len()
                            );
                        }
                    }
                    Err(e) => warn!("Failed to serialize process variants: {}", e),
                }
            }
        }
    }

    // ========================================================================
    // Chart of Accounts
    // ========================================================================
    // Write accounts as a flat array for consistency with other entity files.
    // CoA metadata (coa_id, country, industry) is preserved in the generation manifest.
    match serde_json::to_string_pretty(&result.chart_of_accounts.accounts) {
        Ok(json) => {
            if let Err(e) = std::fs::write(output_dir.join("chart_of_accounts.json"), json) {
                warn!("Failed to write chart of accounts: {}", e);
            } else {
                info!("  Chart of accounts written");
            }
        }
        Err(e) => warn!("Failed to serialize chart of accounts: {}", e),
    }

    // ========================================================================
    // Balance Validation Summary
    // ========================================================================
    if result.balance_validation.validated {
        match serde_json::to_string_pretty(&BalanceValidationSummary::from(
            &result.balance_validation,
        )) {
            Ok(json) => {
                if let Err(e) = std::fs::write(output_dir.join("balance_validation.json"), json) {
                    warn!("Failed to write balance validation: {}", e);
                } else {
                    info!("  Balance validation summary written");
                }
            }
            Err(e) => warn!("Failed to serialize balance validation: {}", e),
        }
    }

    // ========================================================================
    // Data Quality Statistics (now serializable directly via Serialize derives)
    // ========================================================================
    {
        match serde_json::to_string_pretty(&result.data_quality_stats) {
            Ok(json) => {
                if let Err(e) = std::fs::write(output_dir.join("data_quality_stats.json"), json) {
                    warn!("Failed to write data quality stats: {}", e);
                } else {
                    info!("  Data quality stats written (full detail)");
                }
            }
            Err(e) => warn!("Failed to serialize data quality stats: {}", e),
        }
    }

    // ========================================================================
    // Pre-built Analytics (Benford, amount distribution, process variants)
    // ========================================================================
    {
        let analytics_dir = output_dir.join("analytics");

        // Collect non-zero amounts from journal entry lines
        let amounts: Vec<_> = result
            .journal_entries
            .iter()
            .flat_map(|je| je.lines.iter())
            .flat_map(|line| {
                let d = (!line.debit_amount.is_zero()).then_some(line.debit_amount);
                let c = (!line.credit_amount.is_zero()).then_some(line.credit_amount);
                d.into_iter().chain(c)
            })
            .collect();

        if amounts.len() >= 10 {
            std::fs::create_dir_all(&analytics_dir)?;
            info!("Writing pre-built analytics ({} amounts)...", amounts.len());

            // Benford's Law analysis
            let benford_analyzer = datasynth_eval::BenfordAnalyzer::default();
            match benford_analyzer.analyze(&amounts) {
                Ok(ref benford_result) => {
                    if let Ok(json) = serde_json::to_string_pretty(benford_result) {
                        if let Err(e) =
                            std::fs::write(analytics_dir.join("benford_analysis.json"), json)
                        {
                            warn!("Failed to write Benford analysis: {}", e);
                        } else {
                            info!(
                                "  Benford analysis written (conformity: {:?}, MAD: {:.4})",
                                benford_result.conformity, benford_result.mad
                            );
                        }
                    }
                }
                Err(e) => warn!("Benford analysis skipped: {}", e),
            }

            // Amount distribution analysis
            let amount_analyzer = datasynth_eval::AmountDistributionAnalyzer::new();
            match amount_analyzer.analyze(&amounts) {
                Ok(ref dist_result) => {
                    if let Ok(json) = serde_json::to_string_pretty(dist_result) {
                        if let Err(e) =
                            std::fs::write(analytics_dir.join("amount_distribution.json"), json)
                        {
                            warn!("Failed to write amount distribution: {}", e);
                        } else {
                            info!(
                                "  Amount distribution written (skewness: {:.2}, kurtosis: {:.2})",
                                dist_result.skewness, dist_result.kurtosis
                            );
                        }
                    }
                }
                Err(e) => warn!("Amount distribution analysis skipped: {}", e),
            }
        }

        // Process variant summary (from OCPM event log)
        if let Some(ref event_log) = result.ocpm.event_log {
            if !event_log.variants.is_empty() {
                std::fs::create_dir_all(&analytics_dir)?;
                let variant_data: Vec<datasynth_eval::VariantData> = event_log
                    .variants
                    .values()
                    .map(|v| datasynth_eval::VariantData {
                        variant_id: v.variant_id.clone(),
                        case_count: v.frequency as usize,
                        is_happy_path: v.is_happy_path,
                    })
                    .collect();

                let variant_analyzer = datasynth_eval::VariantAnalyzer::new();
                match variant_analyzer.analyze(&variant_data) {
                    Ok(ref variant_result) => {
                        if let Ok(json) = serde_json::to_string_pretty(variant_result) {
                            if let Err(e) = std::fs::write(
                                analytics_dir.join("process_variant_summary.json"),
                                json,
                            ) {
                                warn!("Failed to write variant summary: {}", e);
                            } else {
                                info!(
                                    "  Process variant summary written ({} variants, entropy: {:.2})",
                                    variant_result.variant_count, variant_result.variant_entropy
                                );
                            }
                        }
                    }
                    Err(e) => warn!("Variant analysis skipped: {}", e),
                }
            }
        }
    }

    // ========================================================================
    // Data Quality Issue Records + Quality Labels
    // ========================================================================
    if !result.quality_issues.is_empty() {
        let labels_dir = output_dir.join("labels");
        std::fs::create_dir_all(&labels_dir)?;
        info!("Writing data quality issue records...");
        write_json_safe(
            &result.quality_issues,
            &labels_dir.join("quality_issues.json"),
            "Data quality issues",
        );

        // Derive quality_labels.json from quality_issues: maps each QualityIssue
        // to a QualityIssueLabel with the corresponding LabeledIssueType and severity.
        use datasynth_generators::{
            LabeledIssueType, QualityIssueLabel, QualityIssueType, QualityLabels,
        };
        let mut quality_labels = QualityLabels::with_capacity(result.quality_issues.len());
        for issue in &result.quality_issues {
            let labeled_type = match issue.issue_type {
                QualityIssueType::MissingValue => LabeledIssueType::MissingValue,
                QualityIssueType::Typo => LabeledIssueType::Typo,
                QualityIssueType::DateFormatVariation
                | QualityIssueType::AmountFormatVariation
                | QualityIssueType::IdentifierFormatVariation
                | QualityIssueType::TextFormatVariation => LabeledIssueType::FormatVariation,
                QualityIssueType::ExactDuplicate
                | QualityIssueType::NearDuplicate
                | QualityIssueType::FuzzyDuplicate => LabeledIssueType::Duplicate,
                QualityIssueType::EncodingIssue => LabeledIssueType::EncodingIssue,
            };
            let mut label = QualityIssueLabel::new(
                labeled_type,
                issue.record_id.clone(),
                issue.field.clone().unwrap_or_else(|| "_record".to_string()),
                "data_quality_injector",
            );
            if let Some(ref orig) = issue.original_value {
                label = label.with_original(orig.clone());
            }
            if let Some(ref modified) = issue.modified_value {
                label = label.with_modified(modified.clone());
            }
            quality_labels.add(label);
        }
        if let Ok(json) = serde_json::to_string_pretty(&quality_labels) {
            if let Err(e) = std::fs::write(labels_dir.join("quality_labels.json"), json.as_bytes())
            {
                warn!("Failed to write quality labels: {}", e);
            } else {
                info!(
                    "  Quality labels written: {} labels -> labels/quality_labels.json",
                    quality_labels.len()
                );
            }
        }
    }

    // ========================================================================
    // Internal Controls
    // ========================================================================
    if !result.internal_controls.is_empty() || !result.sod_violations.is_empty() {
        let ctrl_dir = output_dir.join("internal_controls");
        std::fs::create_dir_all(&ctrl_dir)?;
        info!("Writing internal controls data...");

        write_json_safe(
            &result.internal_controls,
            &ctrl_dir.join("internal_controls.json"),
            "Internal controls",
        );
        // SoD violations extracted from control-annotated journal entries
        write_json_safe(
            &result.sod_violations,
            &ctrl_dir.join("sod_violations.json"),
            "SoD violations",
        );

        // SoD conflict pairs, SoD rules, control mappings, and COSO control mapping
        // are static reference data — export via ControlExporter regardless of whether
        // individual violations were generated so the master catalog is always present.
        let exporter = datasynth_output::ControlExporter::new(&ctrl_dir);
        match exporter.export_standard() {
            Ok(summary) => {
                info!(
                    "  Control master data written: {} controls, {} SoD conflicts, {} SoD rules, {} COSO mappings, {} account mappings",
                    summary.controls_count,
                    summary.sod_conflicts_count,
                    summary.sod_rules_count,
                    summary.coso_mappings_count,
                    summary.account_mappings_count,
                );
            }
            Err(e) => warn!("Failed to write control master data: {}", e),
        }
    }

    // ========================================================================
    // Accounting Standards
    // ========================================================================
    if !result.accounting_standards.contracts.is_empty()
        || !result.accounting_standards.impairment_tests.is_empty()
        || !result.accounting_standards.business_combinations.is_empty()
        || !result.accounting_standards.ecl_models.is_empty()
        || !result.accounting_standards.provisions.is_empty()
        || !result
            .accounting_standards
            .currency_translation_results
            .is_empty()
    {
        let acct_dir = output_dir.join("accounting_standards");
        std::fs::create_dir_all(&acct_dir)?;
        info!("Writing accounting standards data...");

        write_json_safe(
            &result.accounting_standards.contracts,
            &acct_dir.join("customer_contracts.json"),
            "Customer contracts",
        );
        write_json_safe(
            &result.accounting_standards.impairment_tests,
            &acct_dir.join("impairment_tests.json"),
            "Impairment tests",
        );
        write_json_safe(
            &result.accounting_standards.business_combinations,
            &acct_dir.join("business_combinations.json"),
            "Business combinations",
        );
        write_json_safe(
            &result
                .accounting_standards
                .business_combination_journal_entries,
            &acct_dir.join("business_combination_journal_entries.json"),
            "Business combination journal entries",
        );
        write_json_safe(
            &result.accounting_standards.ecl_models,
            &acct_dir.join("ecl_models.json"),
            "ECL models",
        );
        write_json_safe(
            &result.accounting_standards.ecl_provision_movements,
            &acct_dir.join("ecl_provision_movements.json"),
            "ECL provision movements",
        );
        write_json_safe(
            &result.accounting_standards.ecl_journal_entries,
            &acct_dir.join("ecl_journal_entries.json"),
            "ECL journal entries",
        );
        write_json_safe(
            &result.accounting_standards.provisions,
            &acct_dir.join("provisions.json"),
            "Provisions (IAS 37 / ASC 450)",
        );
        write_json_safe(
            &result.accounting_standards.provision_movements,
            &acct_dir.join("provision_movements.json"),
            "Provision movements",
        );
        write_json_safe(
            &result.accounting_standards.contingent_liabilities,
            &acct_dir.join("contingent_liabilities.json"),
            "Contingent liabilities",
        );
        write_json_safe(
            &result.accounting_standards.provision_journal_entries,
            &acct_dir.join("provision_journal_entries.json"),
            "Provision journal entries",
        );

        // IAS 21 — write under accounting_standards/fx/
        if !result
            .accounting_standards
            .currency_translation_results
            .is_empty()
        {
            let fx_dir = acct_dir.join("fx");
            std::fs::create_dir_all(&fx_dir)?;
            write_json_safe(
                &result.accounting_standards.currency_translation_results,
                &fx_dir.join("currency_translation_results.json"),
                "IAS 21 currency translation results",
            );
        }
    }

    // ========================================================================
    // Quality Gate Results
    // ========================================================================
    if let Some(ref gate_result) = result.gate_result {
        match serde_json::to_string_pretty(gate_result) {
            Ok(json) => {
                if let Err(e) = std::fs::write(output_dir.join("quality_gate_result.json"), json) {
                    warn!("Failed to write quality gate result: {}", e);
                } else {
                    info!(
                        "  Quality gate result written (passed={})",
                        gate_result.passed
                    );
                }
            }
            Err(e) => warn!("Failed to serialize quality gate result: {}", e),
        }
    }

    // ========================================================================
    // Treasury
    // ========================================================================
    if !result.treasury.debt_instruments.is_empty()
        || !result.treasury.cash_positions.is_empty()
        || !result.treasury.hedging_instruments.is_empty()
    {
        let treasury_dir = output_dir.join("treasury");
        std::fs::create_dir_all(&treasury_dir)?;
        info!("Writing treasury data...");

        write_json_safe(
            &result.treasury.debt_instruments,
            &treasury_dir.join("debt_instruments.json"),
            "Debt instruments",
        );
        write_json_safe(
            &result.treasury.hedging_instruments,
            &treasury_dir.join("hedging_instruments.json"),
            "Hedging instruments",
        );
        write_json_safe(
            &result.treasury.hedge_relationships,
            &treasury_dir.join("hedge_relationships.json"),
            "Hedge relationships",
        );
        write_json_safe(
            &result.treasury.cash_positions,
            &treasury_dir.join("cash_positions.json"),
            "Cash positions",
        );
        write_json_safe(
            &result.treasury.cash_forecasts,
            &treasury_dir.join("cash_forecasts.json"),
            "Cash forecasts",
        );
        write_json_safe(
            &result.treasury.cash_pools,
            &treasury_dir.join("cash_pools.json"),
            "Cash pools",
        );
        write_json_safe(
            &result.treasury.cash_pool_sweeps,
            &treasury_dir.join("cash_pool_sweeps.json"),
            "Cash pool sweeps",
        );
        write_json_safe(
            &result.treasury.bank_guarantees,
            &treasury_dir.join("bank_guarantees.json"),
            "Bank guarantees",
        );
        write_json_safe(
            &result.treasury.netting_runs,
            &treasury_dir.join("netting_runs.json"),
            "Netting runs",
        );
        if !result.treasury.treasury_anomaly_labels.is_empty() {
            write_json_safe(
                &result.treasury.treasury_anomaly_labels,
                &treasury_dir.join("treasury_anomaly_labels.json"),
                "Treasury anomaly labels",
            );
        }
    }

    // ========================================================================
    // Project Accounting
    // ========================================================================
    if !result.project_accounting.projects.is_empty() {
        let pa_dir = output_dir.join("project_accounting");
        std::fs::create_dir_all(&pa_dir)?;
        info!("Writing project accounting data...");

        write_json_safe(
            &result.project_accounting.projects,
            &pa_dir.join("projects.json"),
            "Projects",
        );
        write_json_safe(
            &result.project_accounting.cost_lines,
            &pa_dir.join("cost_lines.json"),
            "Project cost lines",
        );
        write_json_safe(
            &result.project_accounting.revenue_records,
            &pa_dir.join("revenue_records.json"),
            "Project revenue records",
        );
        write_json_safe(
            &result.project_accounting.earned_value_metrics,
            &pa_dir.join("earned_value_metrics.json"),
            "Earned value metrics",
        );
        write_json_safe(
            &result.project_accounting.change_orders,
            &pa_dir.join("change_orders.json"),
            "Change orders",
        );
        write_json_safe(
            &result.project_accounting.milestones,
            &pa_dir.join("milestones.json"),
            "Project milestones",
        );
    }

    // ========================================================================
    // Evolution Events (Process Evolution + Organizational Events)
    // ========================================================================
    if !result.process_evolution.is_empty()
        || !result.organizational_events.is_empty()
        || !result.disruption_events.is_empty()
    {
        let events_dir = output_dir.join("events");
        std::fs::create_dir_all(&events_dir)?;
        info!("Writing evolution events...");

        write_json_safe(
            &result.process_evolution,
            &events_dir.join("process_evolution_events.json"),
            "Process evolution events",
        );
        write_json_safe(
            &result.organizational_events,
            &events_dir.join("organizational_events.json"),
            "Organizational events",
        );
        write_json_safe(
            &result.disruption_events,
            &events_dir.join("disruption_events.json"),
            "Disruption events",
        );
    }

    // ========================================================================
    // ML Training: Counterfactual Pairs
    // ========================================================================
    if !result.counterfactual_pairs.is_empty() {
        let ml_dir = output_dir.join("ml_training");
        std::fs::create_dir_all(&ml_dir)?;
        info!("Writing ML training data...");

        write_json_safe(
            &result.counterfactual_pairs,
            &ml_dir.join("counterfactual_pairs.json"),
            "Counterfactual pairs",
        );
    }

    // ========================================================================
    // Fraud Red-Flag Indicators
    // ========================================================================
    if !result.red_flags.is_empty() {
        let labels_dir = output_dir.join("labels");
        std::fs::create_dir_all(&labels_dir)?;
        info!("Writing fraud red-flag indicators...");

        write_json_safe(
            &result.red_flags,
            &labels_dir.join("fraud_red_flags.json"),
            "Fraud red flags",
        );
    }

    // ========================================================================
    // Collusion Rings
    // ========================================================================
    if !result.collusion_rings.is_empty() {
        let labels_dir = output_dir.join("labels");
        std::fs::create_dir_all(&labels_dir)?;
        info!("Writing collusion rings...");

        write_json_safe(
            &result.collusion_rings,
            &labels_dir.join("collusion_rings.json"),
            "Collusion rings",
        );
    }

    // ========================================================================
    // Temporal Vendor Version Chains
    // ========================================================================
    if !result.temporal_vendor_chains.is_empty() {
        let temporal_dir = output_dir.join("temporal");
        std::fs::create_dir_all(&temporal_dir)?;
        info!("Writing temporal vendor version chains...");

        write_json_safe(
            &result.temporal_vendor_chains,
            &temporal_dir.join("vendor_version_chains.json"),
            "Vendor version chains",
        );
    }

    // ========================================================================
    // Entity Relationship Graph + Cross-Process Links
    // ========================================================================
    if result.entity_relationship_graph.is_some() || !result.cross_process_links.is_empty() {
        let rel_dir = output_dir.join("relationships");
        std::fs::create_dir_all(&rel_dir)?;
        info!("Writing entity relationship data...");

        if let Some(ref graph) = result.entity_relationship_graph {
            match serde_json::to_string_pretty(graph) {
                Ok(json) => {
                    let path = rel_dir.join("entity_relationship_graph.json");
                    if let Err(e) = std::fs::write(&path, json) {
                        warn!("Failed to write entity relationship graph: {}", e);
                    } else {
                        info!(
                            "  Entity relationship graph written: {} nodes, {} edges -> {}",
                            graph.nodes.len(),
                            graph.edges.len(),
                            path.display()
                        );
                    }
                }
                Err(e) => warn!("Failed to serialize entity relationship graph: {}", e),
            }
        }

        write_json_safe(
            &result.cross_process_links,
            &rel_dir.join("cross_process_links.json"),
            "Cross-process links",
        );
    }

    // ========================================================================
    // Industry-Specific Data
    // ========================================================================
    if let Some(ref industry_output) = result.industry_output {
        if !industry_output.gl_accounts.is_empty() {
            let industry_dir = output_dir.join("industry");
            std::fs::create_dir_all(&industry_dir).ok();
            info!("Writing industry-specific data...");
            match serde_json::to_string_pretty(industry_output) {
                Ok(json) => {
                    if let Err(e) = std::fs::write(industry_dir.join("industry_data.json"), json) {
                        warn!("Failed to write industry data: {}", e);
                    } else {
                        info!(
                            "  Industry data written: {} GL accounts for {}",
                            industry_output.gl_accounts.len(),
                            industry_output.industry
                        );
                    }
                }
                Err(e) => warn!("Failed to serialize industry data: {}", e),
            }
        }
    }

    // ========================================================================
    // Graph Export Summary
    // ========================================================================
    if result.graph_export.exported {
        let graph_dir = output_dir.join("graph_export");
        std::fs::create_dir_all(&graph_dir).ok();
        match serde_json::to_string_pretty(&result.graph_export) {
            Ok(json) => {
                if let Err(e) = std::fs::write(graph_dir.join("graph_export_summary.json"), json) {
                    warn!("Failed to write graph export summary: {}", e);
                } else {
                    info!("  Graph export summary written");
                }
            }
            Err(e) => warn!("Failed to serialize graph export summary: {}", e),
        }
    }

    // ========================================================================
    // Compliance Regulations
    // ========================================================================
    let cr = &result.compliance_regulations;
    let has_compliance_data = !cr.standard_records.is_empty()
        || !cr.audit_procedures.is_empty()
        || !cr.findings.is_empty()
        || !cr.filings.is_empty();
    if has_compliance_data {
        let cr_dir = output_dir.join("compliance_regulations");
        std::fs::create_dir_all(&cr_dir)?;
        info!("Writing compliance regulations data...");

        write_json_safe(
            &cr.standard_records,
            &cr_dir.join("compliance_standards.json"),
            "Compliance standards",
        );
        write_json_safe(
            &cr.cross_reference_records,
            &cr_dir.join("cross_references.json"),
            "Cross-references",
        );
        write_json_safe(
            &cr.jurisdiction_records,
            &cr_dir.join("jurisdiction_profiles.json"),
            "Jurisdiction profiles",
        );
        write_json_safe(
            &cr.audit_procedures,
            &cr_dir.join("audit_procedures.json"),
            "Audit procedures",
        );
        write_json_safe(
            &cr.findings,
            &cr_dir.join("compliance_findings.json"),
            "Compliance findings",
        );
        write_json_safe(
            &cr.filings,
            &cr_dir.join("regulatory_filings.json"),
            "Regulatory filings",
        );

        if let Some(ref graph) = cr.compliance_graph {
            match serde_json::to_string_pretty(graph) {
                Ok(json) => {
                    if let Err(e) = std::fs::write(cr_dir.join("compliance_graph.json"), json) {
                        warn!("Failed to write compliance graph: {}", e);
                    } else {
                        info!(
                            "  Compliance graph written: {} nodes, {} edges",
                            graph.nodes.len(),
                            graph.edges.len()
                        );
                    }
                }
                Err(e) => warn!("Failed to serialize compliance graph: {}", e),
            }
        }
    }

    // ========================================================================
    // Generation Statistics
    // ========================================================================
    match serde_json::to_string_pretty(&result.statistics) {
        Ok(json) => {
            if let Err(e) = std::fs::write(output_dir.join("generation_statistics.json"), json) {
                warn!("Failed to write generation statistics: {}", e);
            } else {
                info!("  Generation statistics written");
            }
        }
        Err(e) => warn!("Failed to serialize generation statistics: {}", e),
    }

    info!("Output writing complete.");
    Ok(())
}

/// Write JSON with error handling - logs a warning on failure but does not abort.
///
/// When the `FLAT_LAYOUT_ACTIVE` thread-local is true (set by
/// `write_all_output_with_layout` when `export_layout: flat`), this routes
/// through `write_json_flat` so nested `{header, lines|items|allocations}`
/// shapes are automatically flattened. For structures without that shape,
/// `write_json_flat` passes through unchanged.
fn write_json_safe<T: serde::Serialize>(data: &[T], path: &Path, label: &str) {
    // Skip JSON entirely when not in requested output formats
    if SKIP_JSON.with(|c| c.get()) {
        return;
    }
    if FLAT_LAYOUT_ACTIVE.with(|c| c.get()) {
        write_json_flat(data, path, label);
    } else if let Err(e) = write_json(data, path, label) {
        warn!("Failed to write {}: {}", label, e);
    }
}

/// Write JSON, choosing flat or nested layout based on the flag.
fn write_json_auto<T: serde::Serialize>(data: &[T], path: &Path, label: &str, flat: bool) {
    if flat {
        write_json_flat(data, path, label);
    } else {
        write_json_safe(data, path, label);
    }
}

/// Write a flat JSON file by merging nested `header` fields onto each top-level item.
///
/// For structures like `{"header": {...}, "items": [...], "field": val}`, this
/// merges header fields and top-level scalar fields onto each item. If the struct
/// has no `header` key, it writes the data as-is (passthrough).
///
/// Uses heap-allocated intermediates to avoid stack overflow with large records
/// in constrained environments (e.g., distroless containers with glibc 2.36).
/// Fixes #116.
fn write_json_flat<T: serde::Serialize>(data: &[T], path: &Path, label: &str) {
    if data.is_empty() {
        return;
    }

    // Pre-allocate on heap — avoid flat_map closure accumulating on the stack
    let mut flat: Vec<serde_json::Value> = Vec::with_capacity(data.len());

    for item in data {
        let val = match serde_json::to_value(item) {
            Ok(v) => v,
            Err(e) => {
                warn!("Failed to serialize record for flat export: {}", e);
                continue;
            }
        };

        let serde_json::Value::Object(map) = val else {
            flat.push(val);
            continue;
        };

        // Find the header object and items array key
        let items_key = ["items", "lines", "allocations", "line_items"]
            .iter()
            .find(|k| map.contains_key(**k))
            .copied();

        let header = map.get("header");
        let has_structure =
            matches!(header, Some(serde_json::Value::Object(_))) && items_key.is_some();

        if !has_structure {
            // Passthrough: no header/items structure
            flat.push(serde_json::Value::Object(map));
            continue;
        }

        let items_key = items_key.expect("checked above");
        // Extract header map (borrow, don't clone the whole thing)
        let header_map = match map.get("header") {
            Some(serde_json::Value::Object(h)) => h,
            _ => {
                flat.push(serde_json::Value::Object(map));
                continue;
            }
        };

        // Collect top-level scalar field keys+values (small — just scalars)
        let top_fields: Vec<(&String, &serde_json::Value)> = map
            .iter()
            .filter(|(k, v)| {
                k.as_str() != "header" && k.as_str() != items_key && !v.is_array() && !v.is_object()
            })
            .collect();

        if let Some(serde_json::Value::Array(items)) = map.get(items_key) {
            flat.reserve(items.len());
            for item_val in items {
                let mut merged = serde_json::Map::new();
                // Line/item fields first (take precedence)
                if let serde_json::Value::Object(m) = item_val {
                    merged.extend(m.iter().map(|(k, v)| (k.clone(), v.clone())));
                }
                // Header fields (don't overwrite line fields)
                for (k, v) in header_map {
                    if !merged.contains_key(k) {
                        merged.insert(k.clone(), v.clone());
                    }
                }
                // Top-level scalars
                for &(k, v) in &top_fields {
                    if !merged.contains_key(k) {
                        merged.insert(k.clone(), v.clone());
                    }
                }
                flat.push(serde_json::Value::Object(merged));
            }
        } else {
            flat.push(serde_json::Value::Object(map));
        }
    }

    if flat.is_empty() {
        return;
    }

    // Stream-write each flattened record instead of serializing the whole Vec
    let count = flat.len();
    match std::fs::File::create(path) {
        Ok(file) => {
            use std::io::Write;
            let mut writer = std::io::BufWriter::with_capacity(512 * 1024, file);
            if let Err(e) = (|| -> Result<(), Box<dyn std::error::Error>> {
                writer.write_all(b"[\n")?;
                for (i, item) in flat.iter().enumerate() {
                    if i > 0 {
                        writer.write_all(b",\n")?;
                    }
                    serde_json::to_writer_pretty(&mut writer, item)?;
                }
                writer.write_all(b"\n]\n")?;
                writer.flush()?;
                Ok(())
            })() {
                warn!("Failed to write {}: {}", label, e);
            } else {
                info!(
                    "  {} written (flat): {} records -> {}",
                    label,
                    count,
                    path.display()
                );
            }
        }
        Err(e) => warn!("Failed to create {}: {}", label, e),
    }
}

/// Write a single serializable value as a JSON file.
fn write_json_single<T: serde::Serialize>(
    data: &T,
    path: &Path,
    label: &str,
) -> Result<(), Box<dyn std::error::Error>> {
    let file = std::fs::File::create(path)?;
    let writer = std::io::BufWriter::with_capacity(256 * 1024, file);
    serde_json::to_writer_pretty(writer, data)?;
    info!("  {} written -> {}", label, path.display());
    Ok(())
}

/// Write a single serializable value as a JSON file, logging a warning on failure.
fn write_json_single_safe<T: serde::Serialize>(data: &T, path: &Path, label: &str) {
    if SKIP_JSON.with(|c| c.get()) {
        return;
    }
    if let Err(e) = write_json_single(data, path, label) {
        warn!("Failed to write {}: {}", label, e);
    }
}

/// Serializable summary of balance validation (avoids serializing the full
/// `BalanceValidationResult` which has non-Serialize validation error types).
#[derive(serde::Serialize)]
struct BalanceValidationSummary {
    validated: bool,
    is_balanced: bool,
    entries_processed: u64,
    total_debits: String,
    total_credits: String,
    accounts_tracked: usize,
    companies_tracked: usize,
    has_unbalanced_entries: bool,
    validation_error_count: usize,
}

impl BalanceValidationSummary {
    fn from(v: &datasynth_runtime::enhanced_orchestrator::BalanceValidationResult) -> Self {
        Self {
            validated: v.validated,
            is_balanced: v.is_balanced,
            entries_processed: v.entries_processed,
            total_debits: v.total_debits.to_string(),
            total_credits: v.total_credits.to_string(),
            accounts_tracked: v.accounts_tracked,
            companies_tracked: v.companies_tracked,
            has_unbalanced_entries: v.has_unbalanced_entries,
            validation_error_count: v.validation_errors.len(),
        }
    }
}