datasynth-core 2.3.0

Core domain models, traits, and distributions for synthetic enterprise data generation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
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
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
//! Anomaly types and labels for synthetic data generation.
//!
//! This module provides comprehensive anomaly classification for:
//! - Fraud detection training
//! - Error detection systems
//! - Process compliance monitoring
//! - Statistical anomaly detection
//! - Graph-based anomaly detection

use chrono::{NaiveDate, NaiveDateTime};
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Causal reason explaining why an anomaly was injected.
///
/// This enables provenance tracking for understanding the "why" behind each anomaly.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum AnomalyCausalReason {
    /// Injected due to random rate selection.
    RandomRate {
        /// Base rate used for selection.
        base_rate: f64,
    },
    /// Injected due to temporal pattern matching.
    TemporalPattern {
        /// Name of the temporal pattern (e.g., "year_end_spike", "month_end").
        pattern_name: String,
    },
    /// Injected based on entity targeting rules.
    EntityTargeting {
        /// Type of entity targeted (e.g., "vendor", "user", "account").
        target_type: String,
        /// ID of the targeted entity.
        target_id: String,
    },
    /// Part of an anomaly cluster.
    ClusterMembership {
        /// ID of the cluster this anomaly belongs to.
        cluster_id: String,
    },
    /// Part of a multi-step scenario.
    ScenarioStep {
        /// Type of scenario (e.g., "kickback_scheme", "round_tripping").
        scenario_type: String,
        /// Step number within the scenario.
        step_number: u32,
    },
    /// Injected based on data quality profile.
    DataQualityProfile {
        /// Profile name (e.g., "noisy", "legacy", "clean").
        profile: String,
    },
    /// Injected for ML training balance.
    MLTrainingBalance {
        /// Target class being balanced.
        target_class: String,
    },
}

/// Structured injection strategy with captured parameters.
///
/// Unlike the string-based `injection_strategy` field, this enum captures
/// the exact parameters used during injection for full reproducibility.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum InjectionStrategy {
    /// Amount was manipulated by a factor.
    AmountManipulation {
        /// Original amount before manipulation.
        original: Decimal,
        /// Multiplication factor applied.
        factor: f64,
    },
    /// Amount adjusted to avoid a threshold.
    ThresholdAvoidance {
        /// Threshold being avoided.
        threshold: Decimal,
        /// Final amount after adjustment.
        adjusted_amount: Decimal,
    },
    /// Date was backdated or forward-dated.
    DateShift {
        /// Number of days shifted (negative = backdated).
        days_shifted: i32,
        /// Original date before shift.
        original_date: NaiveDate,
    },
    /// User approved their own transaction.
    SelfApproval {
        /// User who created and approved.
        user_id: String,
    },
    /// Segregation of duties violation.
    SoDViolation {
        /// First duty involved.
        duty1: String,
        /// Second duty involved.
        duty2: String,
        /// User who performed both duties.
        violating_user: String,
    },
    /// Exact duplicate of another document.
    ExactDuplicate {
        /// ID of the original document.
        original_doc_id: String,
    },
    /// Near-duplicate with small variations.
    NearDuplicate {
        /// ID of the original document.
        original_doc_id: String,
        /// Fields that were varied.
        varied_fields: Vec<String>,
    },
    /// Circular flow of funds/goods.
    CircularFlow {
        /// Chain of entities involved.
        entity_chain: Vec<String>,
    },
    /// Split transaction to avoid threshold.
    SplitTransaction {
        /// Original total amount.
        original_amount: Decimal,
        /// Number of splits.
        split_count: u32,
        /// IDs of the split documents.
        split_doc_ids: Vec<String>,
    },
    /// Round number manipulation.
    RoundNumbering {
        /// Original precise amount.
        original_amount: Decimal,
        /// Rounded amount.
        rounded_amount: Decimal,
    },
    /// Timing manipulation (weekend, after-hours, etc.).
    TimingManipulation {
        /// Type of timing issue.
        timing_type: String,
        /// Original timestamp.
        original_time: Option<NaiveDateTime>,
    },
    /// Account misclassification.
    AccountMisclassification {
        /// Correct account.
        correct_account: String,
        /// Incorrect account used.
        incorrect_account: String,
    },
    /// Missing required field.
    MissingField {
        /// Name of the missing field.
        field_name: String,
    },
    /// Custom injection strategy.
    Custom {
        /// Strategy name.
        name: String,
        /// Additional parameters.
        parameters: HashMap<String, String>,
    },
}

impl InjectionStrategy {
    /// Returns a human-readable description of the strategy.
    pub fn description(&self) -> String {
        match self {
            InjectionStrategy::AmountManipulation { factor, .. } => {
                format!("Amount multiplied by {factor:.2}")
            }
            InjectionStrategy::ThresholdAvoidance { threshold, .. } => {
                format!("Amount adjusted to avoid {threshold} threshold")
            }
            InjectionStrategy::DateShift { days_shifted, .. } => {
                if *days_shifted < 0 {
                    format!("Date backdated by {} days", days_shifted.abs())
                } else {
                    format!("Date forward-dated by {days_shifted} days")
                }
            }
            InjectionStrategy::SelfApproval { user_id } => {
                format!("Self-approval by user {user_id}")
            }
            InjectionStrategy::SoDViolation { duty1, duty2, .. } => {
                format!("SoD violation: {duty1} and {duty2}")
            }
            InjectionStrategy::ExactDuplicate { original_doc_id } => {
                format!("Exact duplicate of {original_doc_id}")
            }
            InjectionStrategy::NearDuplicate {
                original_doc_id,
                varied_fields,
            } => {
                format!("Near-duplicate of {original_doc_id} (varied: {varied_fields:?})")
            }
            InjectionStrategy::CircularFlow { entity_chain } => {
                format!("Circular flow through {} entities", entity_chain.len())
            }
            InjectionStrategy::SplitTransaction { split_count, .. } => {
                format!("Split into {split_count} transactions")
            }
            InjectionStrategy::RoundNumbering { .. } => "Amount rounded to even number".to_string(),
            InjectionStrategy::TimingManipulation { timing_type, .. } => {
                format!("Timing manipulation: {timing_type}")
            }
            InjectionStrategy::AccountMisclassification {
                correct_account,
                incorrect_account,
            } => {
                format!("Misclassified from {correct_account} to {incorrect_account}")
            }
            InjectionStrategy::MissingField { field_name } => {
                format!("Missing required field: {field_name}")
            }
            InjectionStrategy::Custom { name, .. } => format!("Custom: {name}"),
        }
    }

    /// Returns the strategy type name.
    pub fn strategy_type(&self) -> &'static str {
        match self {
            InjectionStrategy::AmountManipulation { .. } => "AmountManipulation",
            InjectionStrategy::ThresholdAvoidance { .. } => "ThresholdAvoidance",
            InjectionStrategy::DateShift { .. } => "DateShift",
            InjectionStrategy::SelfApproval { .. } => "SelfApproval",
            InjectionStrategy::SoDViolation { .. } => "SoDViolation",
            InjectionStrategy::ExactDuplicate { .. } => "ExactDuplicate",
            InjectionStrategy::NearDuplicate { .. } => "NearDuplicate",
            InjectionStrategy::CircularFlow { .. } => "CircularFlow",
            InjectionStrategy::SplitTransaction { .. } => "SplitTransaction",
            InjectionStrategy::RoundNumbering { .. } => "RoundNumbering",
            InjectionStrategy::TimingManipulation { .. } => "TimingManipulation",
            InjectionStrategy::AccountMisclassification { .. } => "AccountMisclassification",
            InjectionStrategy::MissingField { .. } => "MissingField",
            InjectionStrategy::Custom { .. } => "Custom",
        }
    }
}

/// Primary anomaly classification.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum AnomalyType {
    /// Fraudulent activity.
    Fraud(FraudType),
    /// Data entry or processing error.
    Error(ErrorType),
    /// Process or control issue.
    ProcessIssue(ProcessIssueType),
    /// Statistical anomaly.
    Statistical(StatisticalAnomalyType),
    /// Relational/graph anomaly.
    Relational(RelationalAnomalyType),
    /// Custom anomaly type.
    Custom(String),
}

impl AnomalyType {
    /// Returns the category name.
    pub fn category(&self) -> &'static str {
        match self {
            AnomalyType::Fraud(_) => "Fraud",
            AnomalyType::Error(_) => "Error",
            AnomalyType::ProcessIssue(_) => "ProcessIssue",
            AnomalyType::Statistical(_) => "Statistical",
            AnomalyType::Relational(_) => "Relational",
            AnomalyType::Custom(_) => "Custom",
        }
    }

    /// Returns the specific type name.
    pub fn type_name(&self) -> String {
        match self {
            AnomalyType::Fraud(t) => format!("{t:?}"),
            AnomalyType::Error(t) => format!("{t:?}"),
            AnomalyType::ProcessIssue(t) => format!("{t:?}"),
            AnomalyType::Statistical(t) => format!("{t:?}"),
            AnomalyType::Relational(t) => format!("{t:?}"),
            AnomalyType::Custom(s) => s.clone(),
        }
    }

    /// Returns the severity level (1-5, 5 being most severe).
    pub fn severity(&self) -> u8 {
        match self {
            AnomalyType::Fraud(t) => t.severity(),
            AnomalyType::Error(t) => t.severity(),
            AnomalyType::ProcessIssue(t) => t.severity(),
            AnomalyType::Statistical(t) => t.severity(),
            AnomalyType::Relational(t) => t.severity(),
            AnomalyType::Custom(_) => 3,
        }
    }

    /// Returns whether this anomaly is typically intentional.
    pub fn is_intentional(&self) -> bool {
        matches!(self, AnomalyType::Fraud(_))
    }
}

/// Fraud types for detection training.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum FraudType {
    // Journal Entry Fraud
    /// Fictitious journal entry with no business purpose.
    FictitiousEntry,
    /// Fictitious transaction (alias for FictitiousEntry).
    FictitiousTransaction,
    /// Round-dollar amounts suggesting manual manipulation.
    RoundDollarManipulation,
    /// Entry posted just below approval threshold.
    JustBelowThreshold,
    /// Revenue recognition manipulation.
    RevenueManipulation,
    /// Expense capitalization fraud.
    ImproperCapitalization,
    /// Improperly capitalizing expenses as assets.
    ExpenseCapitalization,
    /// Cookie jar reserves manipulation.
    ReserveManipulation,
    /// Round-tripping funds through suspense/clearing accounts.
    SuspenseAccountAbuse,
    /// Splitting transactions to stay below approval thresholds.
    SplitTransaction,
    /// Unusual timing (weekend, holiday, after-hours postings).
    TimingAnomaly,
    /// Posting to unauthorized accounts.
    UnauthorizedAccess,

    // Approval Fraud
    /// User approving their own request.
    SelfApproval,
    /// Approval beyond authorized limit.
    ExceededApprovalLimit,
    /// Segregation of duties violation.
    SegregationOfDutiesViolation,
    /// Approval by unauthorized user.
    UnauthorizedApproval,
    /// Collusion between approver and requester.
    CollusiveApproval,

    // Vendor/Payment Fraud
    /// Fictitious vendor.
    FictitiousVendor,
    /// Duplicate payment to vendor.
    DuplicatePayment,
    /// Payment to shell company.
    ShellCompanyPayment,
    /// Kickback scheme.
    Kickback,
    /// Kickback scheme (alias).
    KickbackScheme,
    /// Invoice manipulation.
    InvoiceManipulation,

    // Asset Fraud
    /// Misappropriation of assets.
    AssetMisappropriation,
    /// Inventory theft.
    InventoryTheft,
    /// Ghost employee.
    GhostEmployee,

    // Financial Statement Fraud
    /// Premature revenue recognition.
    PrematureRevenue,
    /// Understated liabilities.
    UnderstatedLiabilities,
    /// Overstated assets.
    OverstatedAssets,
    /// Channel stuffing.
    ChannelStuffing,

    // Accounting Standards Violations (ASC 606 / IFRS 15 - Revenue)
    /// Improper revenue recognition timing (ASC 606/IFRS 15).
    ImproperRevenueRecognition,
    /// Multiple performance obligations not properly separated.
    ImproperPoAllocation,
    /// Variable consideration not properly estimated.
    VariableConsiderationManipulation,
    /// Contract modifications not properly accounted for.
    ContractModificationMisstatement,

    // Accounting Standards Violations (ASC 842 / IFRS 16 - Leases)
    /// Lease classification manipulation (operating vs finance).
    LeaseClassificationManipulation,
    /// Off-balance sheet lease fraud.
    OffBalanceSheetLease,
    /// Lease liability understatement.
    LeaseLiabilityUnderstatement,
    /// ROU asset misstatement.
    RouAssetMisstatement,

    // Accounting Standards Violations (ASC 820 / IFRS 13 - Fair Value)
    /// Fair value hierarchy misclassification.
    FairValueHierarchyManipulation,
    /// Level 3 input manipulation.
    Level3InputManipulation,
    /// Valuation technique manipulation.
    ValuationTechniqueManipulation,

    // Accounting Standards Violations (ASC 360 / IAS 36 - Impairment)
    /// Delayed impairment recognition.
    DelayedImpairment,
    /// Improperly avoiding impairment testing.
    ImpairmentTestAvoidance,
    /// Cash flow projection manipulation for impairment.
    CashFlowProjectionManipulation,
    /// Improper impairment reversal (IFRS only).
    ImproperImpairmentReversal,

    // Sourcing/Procurement Fraud (S2C)
    /// Bid rigging or collusion among bidders.
    BidRigging,
    /// Contracts with phantom/shell vendors.
    PhantomVendorContract,
    /// Splitting contracts to avoid approval thresholds.
    SplitContractThreshold,
    /// Conflict of interest in sourcing decisions.
    ConflictOfInterestSourcing,

    // HR/Payroll Fraud (H2R)
    /// Ghost employee on payroll.
    GhostEmployeePayroll,
    /// Payroll inflation/unauthorized raises.
    PayrollInflation,
    /// Duplicate expense report submission.
    DuplicateExpenseReport,
    /// Fictitious expense claims.
    FictitiousExpense,
    /// Splitting expenses to avoid approval threshold.
    SplitExpenseToAvoidApproval,

    // O2C Fraud
    /// Revenue timing manipulation via quotes.
    RevenueTimingManipulation,
    /// Overriding quote prices without authorization.
    QuotePriceOverride,
}

impl FraudType {
    /// Returns severity level (1-5).
    pub fn severity(&self) -> u8 {
        match self {
            FraudType::RoundDollarManipulation => 2,
            FraudType::JustBelowThreshold => 3,
            FraudType::SelfApproval => 3,
            FraudType::ExceededApprovalLimit => 3,
            FraudType::DuplicatePayment => 3,
            FraudType::FictitiousEntry => 4,
            FraudType::RevenueManipulation => 5,
            FraudType::FictitiousVendor => 5,
            FraudType::ShellCompanyPayment => 5,
            FraudType::AssetMisappropriation => 5,
            FraudType::SegregationOfDutiesViolation => 4,
            FraudType::CollusiveApproval => 5,
            // Accounting Standards Violations (Revenue - ASC 606/IFRS 15)
            FraudType::ImproperRevenueRecognition => 5,
            FraudType::ImproperPoAllocation => 4,
            FraudType::VariableConsiderationManipulation => 4,
            FraudType::ContractModificationMisstatement => 3,
            // Accounting Standards Violations (Leases - ASC 842/IFRS 16)
            FraudType::LeaseClassificationManipulation => 4,
            FraudType::OffBalanceSheetLease => 5,
            FraudType::LeaseLiabilityUnderstatement => 4,
            FraudType::RouAssetMisstatement => 3,
            // Accounting Standards Violations (Fair Value - ASC 820/IFRS 13)
            FraudType::FairValueHierarchyManipulation => 4,
            FraudType::Level3InputManipulation => 5,
            FraudType::ValuationTechniqueManipulation => 4,
            // Accounting Standards Violations (Impairment - ASC 360/IAS 36)
            FraudType::DelayedImpairment => 4,
            FraudType::ImpairmentTestAvoidance => 4,
            FraudType::CashFlowProjectionManipulation => 5,
            FraudType::ImproperImpairmentReversal => 3,
            _ => 4,
        }
    }
}

/// Error types for error detection.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ErrorType {
    // Data Entry Errors
    /// Duplicate document entry.
    DuplicateEntry,
    /// Reversed debit/credit amounts.
    ReversedAmount,
    /// Transposed digits in amount.
    TransposedDigits,
    /// Wrong decimal placement.
    DecimalError,
    /// Missing required field.
    MissingField,
    /// Invalid account code.
    InvalidAccount,

    // Timing Errors
    /// Posted to wrong period.
    WrongPeriod,
    /// Backdated entry.
    BackdatedEntry,
    /// Future-dated entry.
    FutureDatedEntry,
    /// Cutoff error.
    CutoffError,

    // Classification Errors
    /// Wrong account classification.
    MisclassifiedAccount,
    /// Wrong cost center.
    WrongCostCenter,
    /// Wrong company code.
    WrongCompanyCode,

    // Calculation Errors
    /// Unbalanced journal entry.
    UnbalancedEntry,
    /// Rounding error.
    RoundingError,
    /// Currency conversion error.
    CurrencyError,
    /// Tax calculation error.
    TaxCalculationError,

    // Accounting Standards Errors (Non-Fraudulent)
    /// Wrong revenue recognition timing (honest mistake).
    RevenueTimingError,
    /// Performance obligation allocation error.
    PoAllocationError,
    /// Lease classification error (operating vs finance).
    LeaseClassificationError,
    /// Lease calculation error (PV, amortization).
    LeaseCalculationError,
    /// Fair value measurement error.
    FairValueError,
    /// Impairment calculation error.
    ImpairmentCalculationError,
    /// Discount rate error.
    DiscountRateError,
    /// Framework application error (IFRS vs GAAP).
    FrameworkApplicationError,
}

impl ErrorType {
    /// Returns severity level (1-5).
    pub fn severity(&self) -> u8 {
        match self {
            ErrorType::RoundingError => 1,
            ErrorType::MissingField => 2,
            ErrorType::TransposedDigits => 2,
            ErrorType::DecimalError => 3,
            ErrorType::DuplicateEntry => 3,
            ErrorType::ReversedAmount => 3,
            ErrorType::WrongPeriod => 4,
            ErrorType::UnbalancedEntry => 5,
            ErrorType::CurrencyError => 4,
            // Accounting Standards Errors
            ErrorType::RevenueTimingError => 4,
            ErrorType::PoAllocationError => 3,
            ErrorType::LeaseClassificationError => 3,
            ErrorType::LeaseCalculationError => 3,
            ErrorType::FairValueError => 4,
            ErrorType::ImpairmentCalculationError => 4,
            ErrorType::DiscountRateError => 3,
            ErrorType::FrameworkApplicationError => 4,
            _ => 3,
        }
    }
}

/// Process issue types.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ProcessIssueType {
    // Approval Issues
    /// Approval skipped entirely.
    SkippedApproval,
    /// Late approval (after posting).
    LateApproval,
    /// Missing supporting documentation.
    MissingDocumentation,
    /// Incomplete approval chain.
    IncompleteApprovalChain,

    // Timing Issues
    /// Late posting.
    LatePosting,
    /// Posting outside business hours.
    AfterHoursPosting,
    /// Weekend/holiday posting.
    WeekendPosting,
    /// Rushed period-end posting.
    RushedPeriodEnd,

    // Control Issues
    /// Manual override of system control.
    ManualOverride,
    /// Unusual user access pattern.
    UnusualAccess,
    /// System bypass.
    SystemBypass,
    /// Batch processing anomaly.
    BatchAnomaly,

    // Documentation Issues
    /// Vague or missing description.
    VagueDescription,
    /// Changed after posting.
    PostFactoChange,
    /// Incomplete audit trail.
    IncompleteAuditTrail,

    // Sourcing/Procurement Issues (S2C)
    /// Purchasing outside of contracts (maverick spend).
    MaverickSpend,
    /// Purchasing against an expired contract.
    ExpiredContractPurchase,
    /// Overriding contracted price without authorization.
    ContractPriceOverride,
    /// Award given with only a single bid received.
    SingleBidAward,
    /// Bypassing supplier qualification requirements.
    QualificationBypass,

    // O2C Issues
    /// Converting an expired quote to a sales order.
    ExpiredQuoteConversion,
}

impl ProcessIssueType {
    /// Returns severity level (1-5).
    pub fn severity(&self) -> u8 {
        match self {
            ProcessIssueType::VagueDescription => 1,
            ProcessIssueType::LatePosting => 2,
            ProcessIssueType::AfterHoursPosting => 2,
            ProcessIssueType::WeekendPosting => 2,
            ProcessIssueType::SkippedApproval => 4,
            ProcessIssueType::ManualOverride => 4,
            ProcessIssueType::SystemBypass => 5,
            ProcessIssueType::IncompleteAuditTrail => 4,
            _ => 3,
        }
    }
}

/// Statistical anomaly types.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum StatisticalAnomalyType {
    // Amount Anomalies
    /// Amount significantly above normal.
    UnusuallyHighAmount,
    /// Amount significantly below normal.
    UnusuallyLowAmount,
    /// Violates Benford's Law distribution.
    BenfordViolation,
    /// Exact duplicate amount (suspicious).
    ExactDuplicateAmount,
    /// Repeating pattern in amounts.
    RepeatingAmount,

    // Frequency Anomalies
    /// Unusual transaction frequency.
    UnusualFrequency,
    /// Burst of transactions.
    TransactionBurst,
    /// Unusual time of day.
    UnusualTiming,

    // Trend Anomalies
    /// Break in historical trend.
    TrendBreak,
    /// Sudden level shift.
    LevelShift,
    /// Seasonal pattern violation.
    SeasonalAnomaly,

    // Distribution Anomalies
    /// Outlier in distribution.
    StatisticalOutlier,
    /// Change in variance.
    VarianceChange,
    /// Distribution shift.
    DistributionShift,

    // Sourcing/Contract Anomalies
    /// Pattern of SLA breaches from a vendor.
    SlaBreachPattern,
    /// Contract with zero utilization.
    UnusedContract,

    // HR/Payroll Anomalies
    /// Anomalous overtime patterns.
    OvertimeAnomaly,
}

impl StatisticalAnomalyType {
    /// Returns severity level (1-5).
    pub fn severity(&self) -> u8 {
        match self {
            StatisticalAnomalyType::UnusualTiming => 1,
            StatisticalAnomalyType::UnusualFrequency => 2,
            StatisticalAnomalyType::BenfordViolation => 2,
            StatisticalAnomalyType::UnusuallyHighAmount => 3,
            StatisticalAnomalyType::TrendBreak => 3,
            StatisticalAnomalyType::TransactionBurst => 4,
            StatisticalAnomalyType::ExactDuplicateAmount => 3,
            _ => 3,
        }
    }
}

/// Relational/graph anomaly types.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum RelationalAnomalyType {
    // Transaction Pattern Anomalies
    /// Circular transaction pattern.
    CircularTransaction,
    /// Unusual account combination.
    UnusualAccountPair,
    /// New trading partner.
    NewCounterparty,
    /// Dormant account suddenly active.
    DormantAccountActivity,

    // Network Anomalies
    /// Unusual network centrality.
    CentralityAnomaly,
    /// Isolated transaction cluster.
    IsolatedCluster,
    /// Bridge node anomaly.
    BridgeNodeAnomaly,
    /// Community structure change.
    CommunityAnomaly,

    // Relationship Anomalies
    /// Missing expected relationship.
    MissingRelationship,
    /// Unexpected relationship.
    UnexpectedRelationship,
    /// Relationship strength change.
    RelationshipStrengthChange,

    // Intercompany Anomalies
    /// Unmatched intercompany transaction.
    UnmatchedIntercompany,
    /// Circular intercompany flow.
    CircularIntercompany,
    /// Transfer pricing anomaly.
    TransferPricingAnomaly,
}

impl RelationalAnomalyType {
    /// Returns severity level (1-5).
    pub fn severity(&self) -> u8 {
        match self {
            RelationalAnomalyType::NewCounterparty => 1,
            RelationalAnomalyType::DormantAccountActivity => 2,
            RelationalAnomalyType::UnusualAccountPair => 2,
            RelationalAnomalyType::CircularTransaction => 4,
            RelationalAnomalyType::CircularIntercompany => 4,
            RelationalAnomalyType::TransferPricingAnomaly => 4,
            RelationalAnomalyType::UnmatchedIntercompany => 3,
            _ => 3,
        }
    }
}

/// A labeled anomaly for supervised learning.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LabeledAnomaly {
    /// Unique anomaly identifier.
    pub anomaly_id: String,
    /// Type of anomaly.
    pub anomaly_type: AnomalyType,
    /// Document or entity that contains the anomaly.
    pub document_id: String,
    /// Document type (JE, PO, Invoice, etc.).
    pub document_type: String,
    /// Company code.
    pub company_code: String,
    /// Date the anomaly occurred.
    pub anomaly_date: NaiveDate,
    /// Timestamp when detected/injected.
    #[serde(with = "crate::serde_timestamp::naive")]
    pub detection_timestamp: NaiveDateTime,
    /// Confidence score (0.0 - 1.0) for injected anomalies.
    pub confidence: f64,
    /// Severity (1-5).
    pub severity: u8,
    /// Description of the anomaly.
    pub description: String,
    /// Related entities (user IDs, account codes, etc.).
    pub related_entities: Vec<String>,
    /// Monetary impact if applicable.
    pub monetary_impact: Option<Decimal>,
    /// Additional metadata.
    pub metadata: HashMap<String, String>,
    /// Whether this was injected (true) or naturally occurring (false).
    pub is_injected: bool,
    /// Injection strategy used (if injected) - legacy string field.
    pub injection_strategy: Option<String>,
    /// Cluster ID if part of an anomaly cluster.
    pub cluster_id: Option<String>,

    // ========================================
    // PROVENANCE TRACKING FIELDS (Phase 1.2)
    // ========================================
    /// Hash of the original document before modification.
    /// Enables tracking what the document looked like pre-injection.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub original_document_hash: Option<String>,

    /// Causal reason explaining why this anomaly was injected.
    /// Provides "why" tracking for each anomaly.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub causal_reason: Option<AnomalyCausalReason>,

    /// Structured injection strategy with parameters.
    /// More detailed than the legacy string-based injection_strategy field.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub structured_strategy: Option<InjectionStrategy>,

    /// Parent anomaly ID if this was derived from another anomaly.
    /// Enables anomaly transformation chains.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub parent_anomaly_id: Option<String>,

    /// Child anomaly IDs that were derived from this anomaly.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub child_anomaly_ids: Vec<String>,

    /// Scenario ID if this anomaly is part of a multi-step scenario.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub scenario_id: Option<String>,

    /// Generation run ID that produced this anomaly.
    /// Enables tracing anomalies back to their generation run.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub run_id: Option<String>,

    /// Seed used for RNG during generation.
    /// Enables reproducibility.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub generation_seed: Option<u64>,
}

impl LabeledAnomaly {
    /// Creates a new labeled anomaly.
    pub fn new(
        anomaly_id: String,
        anomaly_type: AnomalyType,
        document_id: String,
        document_type: String,
        company_code: String,
        anomaly_date: NaiveDate,
    ) -> Self {
        let severity = anomaly_type.severity();
        let description = format!(
            "{} - {} in document {}",
            anomaly_type.category(),
            anomaly_type.type_name(),
            document_id
        );

        Self {
            anomaly_id,
            anomaly_type,
            document_id,
            document_type,
            company_code,
            anomaly_date,
            detection_timestamp: chrono::Local::now().naive_local(),
            confidence: 1.0,
            severity,
            description,
            related_entities: Vec::new(),
            monetary_impact: None,
            metadata: HashMap::new(),
            is_injected: true,
            injection_strategy: None,
            cluster_id: None,
            // Provenance fields
            original_document_hash: None,
            causal_reason: None,
            structured_strategy: None,
            parent_anomaly_id: None,
            child_anomaly_ids: Vec::new(),
            scenario_id: None,
            run_id: None,
            generation_seed: None,
        }
    }

    /// Sets the description.
    pub fn with_description(mut self, description: &str) -> Self {
        self.description = description.to_string();
        self
    }

    /// Sets the monetary impact.
    pub fn with_monetary_impact(mut self, impact: Decimal) -> Self {
        self.monetary_impact = Some(impact);
        self
    }

    /// Adds a related entity.
    pub fn with_related_entity(mut self, entity: &str) -> Self {
        self.related_entities.push(entity.to_string());
        self
    }

    /// Adds metadata.
    pub fn with_metadata(mut self, key: &str, value: &str) -> Self {
        self.metadata.insert(key.to_string(), value.to_string());
        self
    }

    /// Sets the injection strategy (legacy string).
    pub fn with_injection_strategy(mut self, strategy: &str) -> Self {
        self.injection_strategy = Some(strategy.to_string());
        self
    }

    /// Sets the cluster ID.
    pub fn with_cluster(mut self, cluster_id: &str) -> Self {
        self.cluster_id = Some(cluster_id.to_string());
        self
    }

    // ========================================
    // PROVENANCE BUILDER METHODS (Phase 1.2)
    // ========================================

    /// Sets the original document hash for provenance tracking.
    pub fn with_original_document_hash(mut self, hash: &str) -> Self {
        self.original_document_hash = Some(hash.to_string());
        self
    }

    /// Sets the causal reason for this anomaly.
    pub fn with_causal_reason(mut self, reason: AnomalyCausalReason) -> Self {
        self.causal_reason = Some(reason);
        self
    }

    /// Sets the structured injection strategy.
    pub fn with_structured_strategy(mut self, strategy: InjectionStrategy) -> Self {
        // Also set the legacy string field for backward compatibility
        self.injection_strategy = Some(strategy.strategy_type().to_string());
        self.structured_strategy = Some(strategy);
        self
    }

    /// Sets the parent anomaly ID (for anomaly derivation chains).
    pub fn with_parent_anomaly(mut self, parent_id: &str) -> Self {
        self.parent_anomaly_id = Some(parent_id.to_string());
        self
    }

    /// Adds a child anomaly ID.
    pub fn with_child_anomaly(mut self, child_id: &str) -> Self {
        self.child_anomaly_ids.push(child_id.to_string());
        self
    }

    /// Sets the scenario ID for multi-step scenario tracking.
    pub fn with_scenario(mut self, scenario_id: &str) -> Self {
        self.scenario_id = Some(scenario_id.to_string());
        self
    }

    /// Sets the generation run ID.
    pub fn with_run_id(mut self, run_id: &str) -> Self {
        self.run_id = Some(run_id.to_string());
        self
    }

    /// Sets the generation seed for reproducibility.
    pub fn with_generation_seed(mut self, seed: u64) -> Self {
        self.generation_seed = Some(seed);
        self
    }

    /// Sets multiple provenance fields at once for convenience.
    pub fn with_provenance(
        mut self,
        run_id: Option<&str>,
        seed: Option<u64>,
        causal_reason: Option<AnomalyCausalReason>,
    ) -> Self {
        if let Some(id) = run_id {
            self.run_id = Some(id.to_string());
        }
        self.generation_seed = seed;
        self.causal_reason = causal_reason;
        self
    }

    /// Converts to a feature vector for ML.
    ///
    /// Returns a vector of 15 features:
    /// - 6 features: Category one-hot encoding (Fraud, Error, ProcessIssue, Statistical, Relational, Custom)
    /// - 1 feature: Severity (normalized 0-1)
    /// - 1 feature: Confidence
    /// - 1 feature: Has monetary impact (0/1)
    /// - 1 feature: Monetary impact (log-scaled)
    /// - 1 feature: Is intentional (0/1)
    /// - 1 feature: Number of related entities
    /// - 1 feature: Is part of cluster (0/1)
    /// - 1 feature: Is part of scenario (0/1)
    /// - 1 feature: Has parent anomaly (0/1) - indicates derivation
    pub fn to_features(&self) -> Vec<f64> {
        let mut features = Vec::new();

        // Category one-hot encoding
        let categories = [
            "Fraud",
            "Error",
            "ProcessIssue",
            "Statistical",
            "Relational",
            "Custom",
        ];
        for cat in &categories {
            features.push(if self.anomaly_type.category() == *cat {
                1.0
            } else {
                0.0
            });
        }

        // Severity (normalized)
        features.push(self.severity as f64 / 5.0);

        // Confidence
        features.push(self.confidence);

        // Has monetary impact
        features.push(if self.monetary_impact.is_some() {
            1.0
        } else {
            0.0
        });

        // Monetary impact (log-scaled)
        if let Some(impact) = self.monetary_impact {
            let impact_f64: f64 = impact.try_into().unwrap_or(0.0);
            features.push((impact_f64.abs() + 1.0).ln());
        } else {
            features.push(0.0);
        }

        // Is intentional
        features.push(if self.anomaly_type.is_intentional() {
            1.0
        } else {
            0.0
        });

        // Number of related entities
        features.push(self.related_entities.len() as f64);

        // Is part of cluster
        features.push(if self.cluster_id.is_some() { 1.0 } else { 0.0 });

        // Provenance features
        // Is part of scenario
        features.push(if self.scenario_id.is_some() { 1.0 } else { 0.0 });

        // Has parent anomaly (indicates this is a derived anomaly)
        features.push(if self.parent_anomaly_id.is_some() {
            1.0
        } else {
            0.0
        });

        features
    }

    /// Returns the number of features in the feature vector.
    pub fn feature_count() -> usize {
        15 // 6 category + 9 other features
    }

    /// Returns feature names for documentation/ML metadata.
    pub fn feature_names() -> Vec<&'static str> {
        vec![
            "category_fraud",
            "category_error",
            "category_process_issue",
            "category_statistical",
            "category_relational",
            "category_custom",
            "severity_normalized",
            "confidence",
            "has_monetary_impact",
            "monetary_impact_log",
            "is_intentional",
            "related_entity_count",
            "is_clustered",
            "is_scenario_part",
            "is_derived",
        ]
    }
}

/// Summary of anomalies for reporting.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct AnomalySummary {
    /// Total anomaly count.
    pub total_count: usize,
    /// Count by category.
    pub by_category: HashMap<String, usize>,
    /// Count by specific type.
    pub by_type: HashMap<String, usize>,
    /// Count by severity.
    pub by_severity: HashMap<u8, usize>,
    /// Count by company.
    pub by_company: HashMap<String, usize>,
    /// Total monetary impact.
    pub total_monetary_impact: Decimal,
    /// Date range.
    pub date_range: Option<(NaiveDate, NaiveDate)>,
    /// Number of clusters.
    pub cluster_count: usize,
}

impl AnomalySummary {
    /// Creates a summary from a list of anomalies.
    pub fn from_anomalies(anomalies: &[LabeledAnomaly]) -> Self {
        let mut summary = AnomalySummary {
            total_count: anomalies.len(),
            ..Default::default()
        };

        let mut min_date: Option<NaiveDate> = None;
        let mut max_date: Option<NaiveDate> = None;
        let mut clusters = std::collections::HashSet::new();

        for anomaly in anomalies {
            // By category
            *summary
                .by_category
                .entry(anomaly.anomaly_type.category().to_string())
                .or_insert(0) += 1;

            // By type
            *summary
                .by_type
                .entry(anomaly.anomaly_type.type_name())
                .or_insert(0) += 1;

            // By severity
            *summary.by_severity.entry(anomaly.severity).or_insert(0) += 1;

            // By company
            *summary
                .by_company
                .entry(anomaly.company_code.clone())
                .or_insert(0) += 1;

            // Monetary impact
            if let Some(impact) = anomaly.monetary_impact {
                summary.total_monetary_impact += impact;
            }

            // Date range
            match min_date {
                None => min_date = Some(anomaly.anomaly_date),
                Some(d) if anomaly.anomaly_date < d => min_date = Some(anomaly.anomaly_date),
                _ => {}
            }
            match max_date {
                None => max_date = Some(anomaly.anomaly_date),
                Some(d) if anomaly.anomaly_date > d => max_date = Some(anomaly.anomaly_date),
                _ => {}
            }

            // Clusters
            if let Some(cluster_id) = &anomaly.cluster_id {
                clusters.insert(cluster_id.clone());
            }
        }

        summary.date_range = min_date.zip(max_date);
        summary.cluster_count = clusters.len();

        summary
    }
}

// ============================================================================
// ENHANCED ANOMALY TAXONOMY (FR-003)
// ============================================================================

/// High-level anomaly category for multi-class classification.
///
/// These categories provide a more granular classification than the base
/// AnomalyType enum, enabling better ML model training and audit reporting.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum AnomalyCategory {
    // Vendor-related anomalies
    /// Fictitious or shell vendor.
    FictitiousVendor,
    /// Kickback or collusion with vendor.
    VendorKickback,
    /// Related party vendor transactions.
    RelatedPartyVendor,

    // Transaction-related anomalies
    /// Duplicate payment or invoice.
    DuplicatePayment,
    /// Unauthorized transaction.
    UnauthorizedTransaction,
    /// Structured transactions to avoid thresholds.
    StructuredTransaction,

    // Pattern-based anomalies
    /// Circular flow of funds.
    CircularFlow,
    /// Behavioral anomaly (deviation from normal patterns).
    BehavioralAnomaly,
    /// Timing-based anomaly.
    TimingAnomaly,

    // Journal entry anomalies
    /// Manual journal entry anomaly.
    JournalAnomaly,
    /// Manual override of controls.
    ManualOverride,
    /// Missing approval in chain.
    MissingApproval,

    // Statistical anomalies
    /// Statistical outlier.
    StatisticalOutlier,
    /// Distribution anomaly (Benford, etc.).
    DistributionAnomaly,

    // Custom category
    /// User-defined category.
    Custom(String),
}

impl AnomalyCategory {
    /// Derives an AnomalyCategory from an AnomalyType.
    pub fn from_anomaly_type(anomaly_type: &AnomalyType) -> Self {
        match anomaly_type {
            AnomalyType::Fraud(fraud_type) => match fraud_type {
                FraudType::FictitiousVendor | FraudType::ShellCompanyPayment => {
                    AnomalyCategory::FictitiousVendor
                }
                FraudType::Kickback | FraudType::KickbackScheme => AnomalyCategory::VendorKickback,
                FraudType::DuplicatePayment => AnomalyCategory::DuplicatePayment,
                FraudType::SplitTransaction | FraudType::JustBelowThreshold => {
                    AnomalyCategory::StructuredTransaction
                }
                FraudType::SelfApproval
                | FraudType::UnauthorizedApproval
                | FraudType::CollusiveApproval => AnomalyCategory::UnauthorizedTransaction,
                FraudType::TimingAnomaly
                | FraudType::RoundDollarManipulation
                | FraudType::SuspenseAccountAbuse => AnomalyCategory::JournalAnomaly,
                _ => AnomalyCategory::BehavioralAnomaly,
            },
            AnomalyType::Error(error_type) => match error_type {
                ErrorType::DuplicateEntry => AnomalyCategory::DuplicatePayment,
                ErrorType::WrongPeriod
                | ErrorType::BackdatedEntry
                | ErrorType::FutureDatedEntry => AnomalyCategory::TimingAnomaly,
                _ => AnomalyCategory::JournalAnomaly,
            },
            AnomalyType::ProcessIssue(process_type) => match process_type {
                ProcessIssueType::SkippedApproval | ProcessIssueType::IncompleteApprovalChain => {
                    AnomalyCategory::MissingApproval
                }
                ProcessIssueType::ManualOverride | ProcessIssueType::SystemBypass => {
                    AnomalyCategory::ManualOverride
                }
                ProcessIssueType::AfterHoursPosting | ProcessIssueType::WeekendPosting => {
                    AnomalyCategory::TimingAnomaly
                }
                _ => AnomalyCategory::BehavioralAnomaly,
            },
            AnomalyType::Statistical(stat_type) => match stat_type {
                StatisticalAnomalyType::BenfordViolation
                | StatisticalAnomalyType::DistributionShift => AnomalyCategory::DistributionAnomaly,
                _ => AnomalyCategory::StatisticalOutlier,
            },
            AnomalyType::Relational(rel_type) => match rel_type {
                RelationalAnomalyType::CircularTransaction
                | RelationalAnomalyType::CircularIntercompany => AnomalyCategory::CircularFlow,
                _ => AnomalyCategory::BehavioralAnomaly,
            },
            AnomalyType::Custom(s) => AnomalyCategory::Custom(s.clone()),
        }
    }

    /// Returns the category name as a string.
    pub fn name(&self) -> &str {
        match self {
            AnomalyCategory::FictitiousVendor => "fictitious_vendor",
            AnomalyCategory::VendorKickback => "vendor_kickback",
            AnomalyCategory::RelatedPartyVendor => "related_party_vendor",
            AnomalyCategory::DuplicatePayment => "duplicate_payment",
            AnomalyCategory::UnauthorizedTransaction => "unauthorized_transaction",
            AnomalyCategory::StructuredTransaction => "structured_transaction",
            AnomalyCategory::CircularFlow => "circular_flow",
            AnomalyCategory::BehavioralAnomaly => "behavioral_anomaly",
            AnomalyCategory::TimingAnomaly => "timing_anomaly",
            AnomalyCategory::JournalAnomaly => "journal_anomaly",
            AnomalyCategory::ManualOverride => "manual_override",
            AnomalyCategory::MissingApproval => "missing_approval",
            AnomalyCategory::StatisticalOutlier => "statistical_outlier",
            AnomalyCategory::DistributionAnomaly => "distribution_anomaly",
            AnomalyCategory::Custom(s) => s.as_str(),
        }
    }

    /// Returns the ordinal value for ML encoding.
    pub fn ordinal(&self) -> u8 {
        match self {
            AnomalyCategory::FictitiousVendor => 0,
            AnomalyCategory::VendorKickback => 1,
            AnomalyCategory::RelatedPartyVendor => 2,
            AnomalyCategory::DuplicatePayment => 3,
            AnomalyCategory::UnauthorizedTransaction => 4,
            AnomalyCategory::StructuredTransaction => 5,
            AnomalyCategory::CircularFlow => 6,
            AnomalyCategory::BehavioralAnomaly => 7,
            AnomalyCategory::TimingAnomaly => 8,
            AnomalyCategory::JournalAnomaly => 9,
            AnomalyCategory::ManualOverride => 10,
            AnomalyCategory::MissingApproval => 11,
            AnomalyCategory::StatisticalOutlier => 12,
            AnomalyCategory::DistributionAnomaly => 13,
            AnomalyCategory::Custom(_) => 14,
        }
    }

    /// Returns the total number of categories (excluding Custom).
    pub fn category_count() -> usize {
        15 // 14 fixed categories + Custom
    }
}

/// Type of contributing factor for anomaly confidence/severity calculation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum FactorType {
    /// Amount deviation from expected value.
    AmountDeviation,
    /// Proximity to approval/reporting threshold.
    ThresholdProximity,
    /// Timing-related anomaly indicator.
    TimingAnomaly,
    /// Entity risk score contribution.
    EntityRisk,
    /// Pattern match confidence.
    PatternMatch,
    /// Frequency deviation from normal.
    FrequencyDeviation,
    /// Relationship-based anomaly indicator.
    RelationshipAnomaly,
    /// Control bypass indicator.
    ControlBypass,
    /// Benford's Law violation.
    BenfordViolation,
    /// Duplicate indicator.
    DuplicateIndicator,
    /// Approval chain issue.
    ApprovalChainIssue,
    /// Documentation gap.
    DocumentationGap,
    /// Custom factor type.
    Custom,
}

impl FactorType {
    /// Returns the factor type name.
    pub fn name(&self) -> &'static str {
        match self {
            FactorType::AmountDeviation => "amount_deviation",
            FactorType::ThresholdProximity => "threshold_proximity",
            FactorType::TimingAnomaly => "timing_anomaly",
            FactorType::EntityRisk => "entity_risk",
            FactorType::PatternMatch => "pattern_match",
            FactorType::FrequencyDeviation => "frequency_deviation",
            FactorType::RelationshipAnomaly => "relationship_anomaly",
            FactorType::ControlBypass => "control_bypass",
            FactorType::BenfordViolation => "benford_violation",
            FactorType::DuplicateIndicator => "duplicate_indicator",
            FactorType::ApprovalChainIssue => "approval_chain_issue",
            FactorType::DocumentationGap => "documentation_gap",
            FactorType::Custom => "custom",
        }
    }
}

/// Evidence supporting a contributing factor.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FactorEvidence {
    /// Source of the evidence (e.g., "transaction_history", "entity_registry").
    pub source: String,
    /// Raw evidence data.
    pub data: HashMap<String, String>,
}

/// A contributing factor to anomaly confidence/severity.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContributingFactor {
    /// Type of factor.
    pub factor_type: FactorType,
    /// Observed value.
    pub value: f64,
    /// Threshold or expected value.
    pub threshold: f64,
    /// Direction of comparison (true = value > threshold is anomalous).
    pub direction_greater: bool,
    /// Weight of this factor in overall calculation (0.0 - 1.0).
    pub weight: f64,
    /// Human-readable description.
    pub description: String,
    /// Optional supporting evidence.
    pub evidence: Option<FactorEvidence>,
}

impl ContributingFactor {
    /// Creates a new contributing factor.
    pub fn new(
        factor_type: FactorType,
        value: f64,
        threshold: f64,
        direction_greater: bool,
        weight: f64,
        description: &str,
    ) -> Self {
        Self {
            factor_type,
            value,
            threshold,
            direction_greater,
            weight,
            description: description.to_string(),
            evidence: None,
        }
    }

    /// Adds evidence to the factor.
    pub fn with_evidence(mut self, source: &str, data: HashMap<String, String>) -> Self {
        self.evidence = Some(FactorEvidence {
            source: source.to_string(),
            data,
        });
        self
    }

    /// Calculates the factor's contribution to anomaly score.
    pub fn contribution(&self) -> f64 {
        let deviation = if self.direction_greater {
            (self.value - self.threshold).max(0.0)
        } else {
            (self.threshold - self.value).max(0.0)
        };

        // Normalize by threshold to get relative deviation
        let relative_deviation = if self.threshold.abs() > 0.001 {
            deviation / self.threshold.abs()
        } else {
            deviation
        };

        // Apply weight and cap at 1.0
        (relative_deviation * self.weight).min(1.0)
    }
}

/// Enhanced anomaly label with dynamic confidence and severity.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EnhancedAnomalyLabel {
    /// Base labeled anomaly (backward compatible).
    pub base: LabeledAnomaly,
    /// Enhanced category classification.
    pub category: AnomalyCategory,
    /// Dynamically calculated confidence (0.0 - 1.0).
    pub enhanced_confidence: f64,
    /// Contextually calculated severity (0.0 - 1.0).
    pub enhanced_severity: f64,
    /// Factors contributing to confidence/severity.
    pub contributing_factors: Vec<ContributingFactor>,
    /// Secondary categories (for multi-label classification).
    pub secondary_categories: Vec<AnomalyCategory>,
}

impl EnhancedAnomalyLabel {
    /// Creates an enhanced label from a base labeled anomaly.
    pub fn from_base(base: LabeledAnomaly) -> Self {
        let category = AnomalyCategory::from_anomaly_type(&base.anomaly_type);
        let enhanced_confidence = base.confidence;
        let enhanced_severity = base.severity as f64 / 5.0;

        Self {
            base,
            category,
            enhanced_confidence,
            enhanced_severity,
            contributing_factors: Vec::new(),
            secondary_categories: Vec::new(),
        }
    }

    /// Sets the enhanced confidence.
    pub fn with_confidence(mut self, confidence: f64) -> Self {
        self.enhanced_confidence = confidence.clamp(0.0, 1.0);
        self
    }

    /// Sets the enhanced severity.
    pub fn with_severity(mut self, severity: f64) -> Self {
        self.enhanced_severity = severity.clamp(0.0, 1.0);
        self
    }

    /// Adds a contributing factor.
    pub fn with_factor(mut self, factor: ContributingFactor) -> Self {
        self.contributing_factors.push(factor);
        self
    }

    /// Adds a secondary category.
    pub fn with_secondary_category(mut self, category: AnomalyCategory) -> Self {
        if !self.secondary_categories.contains(&category) && category != self.category {
            self.secondary_categories.push(category);
        }
        self
    }

    /// Converts to an extended feature vector.
    ///
    /// Returns base features (15) + enhanced features (10) = 25 features.
    pub fn to_features(&self) -> Vec<f64> {
        let mut features = self.base.to_features();

        // Enhanced features
        features.push(self.enhanced_confidence);
        features.push(self.enhanced_severity);
        features.push(self.category.ordinal() as f64 / AnomalyCategory::category_count() as f64);
        features.push(self.secondary_categories.len() as f64);
        features.push(self.contributing_factors.len() as f64);

        // Max factor weight
        let max_weight = self
            .contributing_factors
            .iter()
            .map(|f| f.weight)
            .fold(0.0, f64::max);
        features.push(max_weight);

        // Factor type indicators (binary flags for key factor types)
        let has_control_bypass = self
            .contributing_factors
            .iter()
            .any(|f| f.factor_type == FactorType::ControlBypass);
        features.push(if has_control_bypass { 1.0 } else { 0.0 });

        let has_amount_deviation = self
            .contributing_factors
            .iter()
            .any(|f| f.factor_type == FactorType::AmountDeviation);
        features.push(if has_amount_deviation { 1.0 } else { 0.0 });

        let has_timing = self
            .contributing_factors
            .iter()
            .any(|f| f.factor_type == FactorType::TimingAnomaly);
        features.push(if has_timing { 1.0 } else { 0.0 });

        let has_pattern_match = self
            .contributing_factors
            .iter()
            .any(|f| f.factor_type == FactorType::PatternMatch);
        features.push(if has_pattern_match { 1.0 } else { 0.0 });

        features
    }

    /// Returns the number of features in the enhanced feature vector.
    pub fn feature_count() -> usize {
        25 // 15 base + 10 enhanced
    }

    /// Returns feature names for the enhanced feature vector.
    pub fn feature_names() -> Vec<&'static str> {
        let mut names = LabeledAnomaly::feature_names();
        names.extend(vec![
            "enhanced_confidence",
            "enhanced_severity",
            "category_ordinal",
            "secondary_category_count",
            "contributing_factor_count",
            "max_factor_weight",
            "has_control_bypass",
            "has_amount_deviation",
            "has_timing_factor",
            "has_pattern_match",
        ]);
        names
    }
}

// ============================================================================
// MULTI-DIMENSIONAL LABELING (Anomaly Pattern Enhancements)
// ============================================================================

/// Severity level classification for anomalies.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
pub enum SeverityLevel {
    /// Minor issue, low impact.
    Low,
    /// Moderate issue, noticeable impact.
    #[default]
    Medium,
    /// Significant issue, substantial impact.
    High,
    /// Critical issue, severe impact requiring immediate attention.
    Critical,
}

impl SeverityLevel {
    /// Returns the numeric value (1-4) for the severity level.
    pub fn numeric(&self) -> u8 {
        match self {
            SeverityLevel::Low => 1,
            SeverityLevel::Medium => 2,
            SeverityLevel::High => 3,
            SeverityLevel::Critical => 4,
        }
    }

    /// Creates a severity level from a numeric value.
    pub fn from_numeric(value: u8) -> Self {
        match value {
            1 => SeverityLevel::Low,
            2 => SeverityLevel::Medium,
            3 => SeverityLevel::High,
            _ => SeverityLevel::Critical,
        }
    }

    /// Creates a severity level from a normalized score (0.0-1.0).
    pub fn from_score(score: f64) -> Self {
        match score {
            s if s < 0.25 => SeverityLevel::Low,
            s if s < 0.50 => SeverityLevel::Medium,
            s if s < 0.75 => SeverityLevel::High,
            _ => SeverityLevel::Critical,
        }
    }

    /// Returns a normalized score (0.0-1.0) for this severity level.
    pub fn to_score(&self) -> f64 {
        match self {
            SeverityLevel::Low => 0.125,
            SeverityLevel::Medium => 0.375,
            SeverityLevel::High => 0.625,
            SeverityLevel::Critical => 0.875,
        }
    }
}

/// Structured severity scoring for anomalies.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnomalySeverity {
    /// Severity level classification.
    pub level: SeverityLevel,
    /// Continuous severity score (0.0-1.0).
    pub score: f64,
    /// Absolute financial impact amount.
    pub financial_impact: Decimal,
    /// Whether this exceeds materiality threshold.
    pub is_material: bool,
    /// Materiality threshold used for determination.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub materiality_threshold: Option<Decimal>,
}

impl AnomalySeverity {
    /// Creates a new severity assessment.
    pub fn new(level: SeverityLevel, financial_impact: Decimal) -> Self {
        Self {
            level,
            score: level.to_score(),
            financial_impact,
            is_material: false,
            materiality_threshold: None,
        }
    }

    /// Creates severity from a score, auto-determining level.
    pub fn from_score(score: f64, financial_impact: Decimal) -> Self {
        Self {
            level: SeverityLevel::from_score(score),
            score: score.clamp(0.0, 1.0),
            financial_impact,
            is_material: false,
            materiality_threshold: None,
        }
    }

    /// Sets the materiality assessment.
    pub fn with_materiality(mut self, threshold: Decimal) -> Self {
        self.materiality_threshold = Some(threshold);
        self.is_material = self.financial_impact.abs() >= threshold;
        self
    }
}

impl Default for AnomalySeverity {
    fn default() -> Self {
        Self {
            level: SeverityLevel::Medium,
            score: 0.5,
            financial_impact: Decimal::ZERO,
            is_material: false,
            materiality_threshold: None,
        }
    }
}

/// Detection difficulty classification for anomalies.
///
/// Categorizes how difficult an anomaly is to detect, which is useful
/// for ML model benchmarking and audit procedure selection.
///
/// Note: This is distinct from `drift_events::AnomalyDetectionDifficulty` which
/// is used for drift event classification and has different variants.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
pub enum AnomalyDetectionDifficulty {
    /// Obvious anomaly, easily caught by basic rules (expected detection rate: 99%).
    Trivial,
    /// Relatively easy to detect with standard procedures (expected detection rate: 90%).
    Easy,
    /// Requires moderate effort or specialized analysis (expected detection rate: 70%).
    #[default]
    Moderate,
    /// Difficult to detect, requires advanced techniques (expected detection rate: 40%).
    Hard,
    /// Expert-level difficulty, requires forensic analysis (expected detection rate: 15%).
    Expert,
}

impl AnomalyDetectionDifficulty {
    /// Returns the expected detection rate for this difficulty level.
    pub fn expected_detection_rate(&self) -> f64 {
        match self {
            AnomalyDetectionDifficulty::Trivial => 0.99,
            AnomalyDetectionDifficulty::Easy => 0.90,
            AnomalyDetectionDifficulty::Moderate => 0.70,
            AnomalyDetectionDifficulty::Hard => 0.40,
            AnomalyDetectionDifficulty::Expert => 0.15,
        }
    }

    /// Returns a numeric difficulty score (0.0-1.0).
    pub fn difficulty_score(&self) -> f64 {
        match self {
            AnomalyDetectionDifficulty::Trivial => 0.05,
            AnomalyDetectionDifficulty::Easy => 0.25,
            AnomalyDetectionDifficulty::Moderate => 0.50,
            AnomalyDetectionDifficulty::Hard => 0.75,
            AnomalyDetectionDifficulty::Expert => 0.95,
        }
    }

    /// Creates a difficulty level from a score (0.0-1.0).
    pub fn from_score(score: f64) -> Self {
        match score {
            s if s < 0.15 => AnomalyDetectionDifficulty::Trivial,
            s if s < 0.35 => AnomalyDetectionDifficulty::Easy,
            s if s < 0.55 => AnomalyDetectionDifficulty::Moderate,
            s if s < 0.75 => AnomalyDetectionDifficulty::Hard,
            _ => AnomalyDetectionDifficulty::Expert,
        }
    }

    /// Returns the name of this difficulty level.
    pub fn name(&self) -> &'static str {
        match self {
            AnomalyDetectionDifficulty::Trivial => "trivial",
            AnomalyDetectionDifficulty::Easy => "easy",
            AnomalyDetectionDifficulty::Moderate => "moderate",
            AnomalyDetectionDifficulty::Hard => "hard",
            AnomalyDetectionDifficulty::Expert => "expert",
        }
    }
}

/// Ground truth certainty level for anomaly labels.
///
/// Indicates how certain we are that the label is correct.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
pub enum GroundTruthCertainty {
    /// Definitively known (injected anomaly with full provenance).
    #[default]
    Definite,
    /// Highly probable based on strong evidence.
    Probable,
    /// Possibly an anomaly based on indirect evidence.
    Possible,
}

impl GroundTruthCertainty {
    /// Returns a certainty score (0.0-1.0).
    pub fn certainty_score(&self) -> f64 {
        match self {
            GroundTruthCertainty::Definite => 1.0,
            GroundTruthCertainty::Probable => 0.8,
            GroundTruthCertainty::Possible => 0.5,
        }
    }

    /// Returns the name of this certainty level.
    pub fn name(&self) -> &'static str {
        match self {
            GroundTruthCertainty::Definite => "definite",
            GroundTruthCertainty::Probable => "probable",
            GroundTruthCertainty::Possible => "possible",
        }
    }
}

/// Detection method classification.
///
/// Indicates which detection methods are recommended or effective for an anomaly.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum DetectionMethod {
    /// Simple rule-based detection (thresholds, filters).
    RuleBased,
    /// Statistical analysis (distributions, outlier detection).
    Statistical,
    /// Machine learning models (classification, anomaly detection).
    MachineLearning,
    /// Graph-based analysis (network patterns, relationships).
    GraphBased,
    /// Manual forensic audit procedures.
    ForensicAudit,
    /// Combination of multiple methods.
    Hybrid,
}

impl DetectionMethod {
    /// Returns the name of this detection method.
    pub fn name(&self) -> &'static str {
        match self {
            DetectionMethod::RuleBased => "rule_based",
            DetectionMethod::Statistical => "statistical",
            DetectionMethod::MachineLearning => "machine_learning",
            DetectionMethod::GraphBased => "graph_based",
            DetectionMethod::ForensicAudit => "forensic_audit",
            DetectionMethod::Hybrid => "hybrid",
        }
    }

    /// Returns a description of this detection method.
    pub fn description(&self) -> &'static str {
        match self {
            DetectionMethod::RuleBased => "Simple threshold and filter rules",
            DetectionMethod::Statistical => "Statistical distribution analysis",
            DetectionMethod::MachineLearning => "ML classification models",
            DetectionMethod::GraphBased => "Network and relationship analysis",
            DetectionMethod::ForensicAudit => "Manual forensic procedures",
            DetectionMethod::Hybrid => "Combined multi-method approach",
        }
    }
}

/// Extended anomaly label with comprehensive multi-dimensional classification.
///
/// This extends the base `EnhancedAnomalyLabel` with additional fields for
/// severity scoring, detection difficulty, recommended methods, and ground truth.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExtendedAnomalyLabel {
    /// Base labeled anomaly.
    pub base: LabeledAnomaly,
    /// Enhanced category classification.
    pub category: AnomalyCategory,
    /// Structured severity assessment.
    pub severity: AnomalySeverity,
    /// Detection difficulty classification.
    pub detection_difficulty: AnomalyDetectionDifficulty,
    /// Recommended detection methods for this anomaly.
    pub recommended_methods: Vec<DetectionMethod>,
    /// Key indicators that should trigger detection.
    pub key_indicators: Vec<String>,
    /// Ground truth certainty level.
    pub ground_truth_certainty: GroundTruthCertainty,
    /// Contributing factors to confidence/severity.
    pub contributing_factors: Vec<ContributingFactor>,
    /// Related entity IDs (vendors, customers, employees, etc.).
    pub related_entity_ids: Vec<String>,
    /// Secondary categories for multi-label classification.
    pub secondary_categories: Vec<AnomalyCategory>,
    /// Scheme ID if part of a multi-stage fraud scheme.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub scheme_id: Option<String>,
    /// Stage number within a scheme (1-indexed).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub scheme_stage: Option<u32>,
    /// Whether this is a near-miss (suspicious but legitimate).
    #[serde(default)]
    pub is_near_miss: bool,
    /// Explanation if this is a near-miss.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub near_miss_explanation: Option<String>,
}

impl ExtendedAnomalyLabel {
    /// Creates an extended label from a base labeled anomaly.
    pub fn from_base(base: LabeledAnomaly) -> Self {
        let category = AnomalyCategory::from_anomaly_type(&base.anomaly_type);
        let severity = AnomalySeverity {
            level: SeverityLevel::from_numeric(base.severity),
            score: base.severity as f64 / 5.0,
            financial_impact: base.monetary_impact.unwrap_or(Decimal::ZERO),
            is_material: false,
            materiality_threshold: None,
        };

        Self {
            base,
            category,
            severity,
            detection_difficulty: AnomalyDetectionDifficulty::Moderate,
            recommended_methods: vec![DetectionMethod::RuleBased],
            key_indicators: Vec::new(),
            ground_truth_certainty: GroundTruthCertainty::Definite,
            contributing_factors: Vec::new(),
            related_entity_ids: Vec::new(),
            secondary_categories: Vec::new(),
            scheme_id: None,
            scheme_stage: None,
            is_near_miss: false,
            near_miss_explanation: None,
        }
    }

    /// Sets the severity assessment.
    pub fn with_severity(mut self, severity: AnomalySeverity) -> Self {
        self.severity = severity;
        self
    }

    /// Sets the detection difficulty.
    pub fn with_difficulty(mut self, difficulty: AnomalyDetectionDifficulty) -> Self {
        self.detection_difficulty = difficulty;
        self
    }

    /// Adds a recommended detection method.
    pub fn with_method(mut self, method: DetectionMethod) -> Self {
        if !self.recommended_methods.contains(&method) {
            self.recommended_methods.push(method);
        }
        self
    }

    /// Sets the recommended detection methods.
    pub fn with_methods(mut self, methods: Vec<DetectionMethod>) -> Self {
        self.recommended_methods = methods;
        self
    }

    /// Adds a key indicator.
    pub fn with_indicator(mut self, indicator: impl Into<String>) -> Self {
        self.key_indicators.push(indicator.into());
        self
    }

    /// Sets the ground truth certainty.
    pub fn with_certainty(mut self, certainty: GroundTruthCertainty) -> Self {
        self.ground_truth_certainty = certainty;
        self
    }

    /// Adds a contributing factor.
    pub fn with_factor(mut self, factor: ContributingFactor) -> Self {
        self.contributing_factors.push(factor);
        self
    }

    /// Adds a related entity ID.
    pub fn with_entity(mut self, entity_id: impl Into<String>) -> Self {
        self.related_entity_ids.push(entity_id.into());
        self
    }

    /// Adds a secondary category.
    pub fn with_secondary_category(mut self, category: AnomalyCategory) -> Self {
        if category != self.category && !self.secondary_categories.contains(&category) {
            self.secondary_categories.push(category);
        }
        self
    }

    /// Sets scheme information.
    pub fn with_scheme(mut self, scheme_id: impl Into<String>, stage: u32) -> Self {
        self.scheme_id = Some(scheme_id.into());
        self.scheme_stage = Some(stage);
        self
    }

    /// Marks this as a near-miss with explanation.
    pub fn as_near_miss(mut self, explanation: impl Into<String>) -> Self {
        self.is_near_miss = true;
        self.near_miss_explanation = Some(explanation.into());
        self
    }

    /// Converts to an extended feature vector for ML.
    ///
    /// Returns base features (15) + extended features (15) = 30 features.
    pub fn to_features(&self) -> Vec<f64> {
        let mut features = self.base.to_features();

        // Extended features
        features.push(self.severity.score);
        features.push(self.severity.level.to_score());
        features.push(if self.severity.is_material { 1.0 } else { 0.0 });
        features.push(self.detection_difficulty.difficulty_score());
        features.push(self.detection_difficulty.expected_detection_rate());
        features.push(self.ground_truth_certainty.certainty_score());
        features.push(self.category.ordinal() as f64 / AnomalyCategory::category_count() as f64);
        features.push(self.secondary_categories.len() as f64);
        features.push(self.contributing_factors.len() as f64);
        features.push(self.key_indicators.len() as f64);
        features.push(self.recommended_methods.len() as f64);
        features.push(self.related_entity_ids.len() as f64);
        features.push(if self.scheme_id.is_some() { 1.0 } else { 0.0 });
        features.push(self.scheme_stage.unwrap_or(0) as f64);
        features.push(if self.is_near_miss { 1.0 } else { 0.0 });

        features
    }

    /// Returns the number of features in the extended feature vector.
    pub fn feature_count() -> usize {
        30 // 15 base + 15 extended
    }

    /// Returns feature names for the extended feature vector.
    pub fn feature_names() -> Vec<&'static str> {
        let mut names = LabeledAnomaly::feature_names();
        names.extend(vec![
            "severity_score",
            "severity_level_score",
            "is_material",
            "difficulty_score",
            "expected_detection_rate",
            "ground_truth_certainty",
            "category_ordinal",
            "secondary_category_count",
            "contributing_factor_count",
            "key_indicator_count",
            "recommended_method_count",
            "related_entity_count",
            "is_part_of_scheme",
            "scheme_stage",
            "is_near_miss",
        ]);
        names
    }
}

// ============================================================================
// MULTI-STAGE FRAUD SCHEME TYPES
// ============================================================================

/// Type of multi-stage fraud scheme.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum SchemeType {
    /// Gradual embezzlement over time.
    GradualEmbezzlement,
    /// Revenue manipulation across periods.
    RevenueManipulation,
    /// Vendor kickback scheme.
    VendorKickback,
    /// Round-tripping funds through multiple entities.
    RoundTripping,
    /// Ghost employee scheme.
    GhostEmployee,
    /// Expense reimbursement fraud.
    ExpenseReimbursement,
    /// Inventory theft scheme.
    InventoryTheft,
    /// Custom scheme type.
    Custom,
}

impl SchemeType {
    /// Returns the name of this scheme type.
    pub fn name(&self) -> &'static str {
        match self {
            SchemeType::GradualEmbezzlement => "gradual_embezzlement",
            SchemeType::RevenueManipulation => "revenue_manipulation",
            SchemeType::VendorKickback => "vendor_kickback",
            SchemeType::RoundTripping => "round_tripping",
            SchemeType::GhostEmployee => "ghost_employee",
            SchemeType::ExpenseReimbursement => "expense_reimbursement",
            SchemeType::InventoryTheft => "inventory_theft",
            SchemeType::Custom => "custom",
        }
    }

    /// Returns the typical number of stages for this scheme type.
    pub fn typical_stages(&self) -> u32 {
        match self {
            SchemeType::GradualEmbezzlement => 4, // testing, escalation, acceleration, desperation
            SchemeType::RevenueManipulation => 4, // Q4->Q1->Q2->Q4
            SchemeType::VendorKickback => 4,      // setup, inflation, kickback, concealment
            SchemeType::RoundTripping => 3,       // setup, execution, reversal
            SchemeType::GhostEmployee => 3,       // creation, payroll, concealment
            SchemeType::ExpenseReimbursement => 3, // submission, approval, payment
            SchemeType::InventoryTheft => 3,      // access, theft, cover-up
            SchemeType::Custom => 4,
        }
    }
}

/// Status of detection for a fraud scheme.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
pub enum SchemeDetectionStatus {
    /// Scheme is undetected.
    #[default]
    Undetected,
    /// Under investigation but not confirmed.
    UnderInvestigation,
    /// Partially detected (some transactions flagged).
    PartiallyDetected,
    /// Fully detected and confirmed.
    FullyDetected,
}

/// Reference to a transaction within a scheme.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SchemeTransactionRef {
    /// Document ID of the transaction.
    pub document_id: String,
    /// Transaction date.
    pub date: chrono::NaiveDate,
    /// Transaction amount.
    pub amount: Decimal,
    /// Stage this transaction belongs to.
    pub stage: u32,
    /// Anomaly ID if labeled.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub anomaly_id: Option<String>,
}

/// Concealment technique used in fraud.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ConcealmentTechnique {
    /// Document manipulation or forgery.
    DocumentManipulation,
    /// Circumventing approval processes.
    ApprovalCircumvention,
    /// Exploiting timing (period-end, holidays).
    TimingExploitation,
    /// Transaction splitting to avoid thresholds.
    TransactionSplitting,
    /// Account misclassification.
    AccountMisclassification,
    /// Collusion with other employees.
    Collusion,
    /// Data alteration or deletion.
    DataAlteration,
    /// Creating false documentation.
    FalseDocumentation,
}

impl ConcealmentTechnique {
    /// Returns the difficulty bonus this technique adds.
    pub fn difficulty_bonus(&self) -> f64 {
        match self {
            ConcealmentTechnique::DocumentManipulation => 0.20,
            ConcealmentTechnique::ApprovalCircumvention => 0.15,
            ConcealmentTechnique::TimingExploitation => 0.10,
            ConcealmentTechnique::TransactionSplitting => 0.15,
            ConcealmentTechnique::AccountMisclassification => 0.10,
            ConcealmentTechnique::Collusion => 0.25,
            ConcealmentTechnique::DataAlteration => 0.20,
            ConcealmentTechnique::FalseDocumentation => 0.15,
        }
    }
}

// ============================================================================
// ACFE-ALIGNED FRAUD TAXONOMY
// ============================================================================
//
// Based on the Association of Certified Fraud Examiners (ACFE) Report to the
// Nations: Occupational Fraud Classification System. This taxonomy provides
// ACFE-aligned categories, schemes, and calibration data.

/// ACFE-aligned fraud categories based on the Occupational Fraud Tree.
///
/// ACFE Report to the Nations statistics (typical):
/// - Asset Misappropriation: 86% of cases, $100k median loss
/// - Corruption: 33% of cases, $150k median loss
/// - Financial Statement Fraud: 10% of cases, $954k median loss
///
/// Note: Percentages sum to >100% because some schemes fall into multiple categories.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
pub enum AcfeFraudCategory {
    /// Theft of organizational assets (cash, inventory, equipment).
    /// Most common (86% of cases) but typically lowest median loss ($100k).
    #[default]
    AssetMisappropriation,
    /// Abuse of position for personal gain through bribery, kickbacks, conflicts of interest.
    /// Medium frequency (33% of cases), medium median loss ($150k).
    Corruption,
    /// Intentional misstatement of financial statements.
    /// Least common (10% of cases) but highest median loss ($954k).
    FinancialStatementFraud,
}

impl AcfeFraudCategory {
    /// Returns the name of this category.
    pub fn name(&self) -> &'static str {
        match self {
            AcfeFraudCategory::AssetMisappropriation => "asset_misappropriation",
            AcfeFraudCategory::Corruption => "corruption",
            AcfeFraudCategory::FinancialStatementFraud => "financial_statement_fraud",
        }
    }

    /// Returns the typical percentage of occupational fraud cases (from ACFE reports).
    pub fn typical_occurrence_rate(&self) -> f64 {
        match self {
            AcfeFraudCategory::AssetMisappropriation => 0.86,
            AcfeFraudCategory::Corruption => 0.33,
            AcfeFraudCategory::FinancialStatementFraud => 0.10,
        }
    }

    /// Returns the typical median loss amount (from ACFE reports).
    pub fn typical_median_loss(&self) -> Decimal {
        match self {
            AcfeFraudCategory::AssetMisappropriation => Decimal::new(100_000, 0),
            AcfeFraudCategory::Corruption => Decimal::new(150_000, 0),
            AcfeFraudCategory::FinancialStatementFraud => Decimal::new(954_000, 0),
        }
    }

    /// Returns the typical detection time in months (from ACFE reports).
    pub fn typical_detection_months(&self) -> u32 {
        match self {
            AcfeFraudCategory::AssetMisappropriation => 12,
            AcfeFraudCategory::Corruption => 18,
            AcfeFraudCategory::FinancialStatementFraud => 24,
        }
    }
}

/// Cash-based fraud schemes under Asset Misappropriation.
///
/// Organized according to the ACFE Fraud Tree:
/// - Theft of Cash on Hand
/// - Theft of Cash Receipts
/// - Fraudulent Disbursements
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum CashFraudScheme {
    // ========== Theft of Cash on Hand ==========
    /// Stealing cash from cash drawers or safes after it has been recorded.
    Larceny,
    /// Stealing cash before it is recorded in the books (intercepts receipts).
    Skimming,

    // ========== Theft of Cash Receipts ==========
    /// Skimming from sales transactions before recording.
    SalesSkimming,
    /// Intercepting customer payments on accounts receivable.
    ReceivablesSkimming,
    /// Creating false refunds to pocket the difference.
    RefundSchemes,

    // ========== Fraudulent Disbursements - Billing Schemes ==========
    /// Creating fictitious vendors to invoice and pay.
    ShellCompany,
    /// Manipulating payments to legitimate vendors for personal gain.
    NonAccompliceVendor,
    /// Using company funds for personal purchases.
    PersonalPurchases,

    // ========== Fraudulent Disbursements - Payroll Schemes ==========
    /// Creating fake employees to collect wages.
    GhostEmployee,
    /// Falsifying hours worked, sales commissions, or salary rates.
    FalsifiedWages,
    /// Manipulating commission calculations.
    CommissionSchemes,

    // ========== Fraudulent Disbursements - Expense Reimbursement ==========
    /// Claiming non-business expenses as business expenses.
    MischaracterizedExpenses,
    /// Inflating legitimate expense amounts.
    OverstatedExpenses,
    /// Creating completely fictitious expenses.
    FictitiousExpenses,

    // ========== Fraudulent Disbursements - Check/Payment Tampering ==========
    /// Forging the signature of an authorized check signer.
    ForgedMaker,
    /// Intercepting and altering the endorsement on legitimate checks.
    ForgedEndorsement,
    /// Altering the payee on a legitimate check.
    AlteredPayee,
    /// Authorized signer writing checks for personal benefit.
    AuthorizedMaker,

    // ========== Fraudulent Disbursements - Register/POS Schemes ==========
    /// Creating false voided transactions.
    FalseVoids,
    /// Processing fictitious refunds.
    FalseRefunds,
}

impl CashFraudScheme {
    /// Returns the ACFE category this scheme belongs to.
    pub fn category(&self) -> AcfeFraudCategory {
        AcfeFraudCategory::AssetMisappropriation
    }

    /// Returns the subcategory within the ACFE Fraud Tree.
    pub fn subcategory(&self) -> &'static str {
        match self {
            CashFraudScheme::Larceny | CashFraudScheme::Skimming => "theft_of_cash_on_hand",
            CashFraudScheme::SalesSkimming
            | CashFraudScheme::ReceivablesSkimming
            | CashFraudScheme::RefundSchemes => "theft_of_cash_receipts",
            CashFraudScheme::ShellCompany
            | CashFraudScheme::NonAccompliceVendor
            | CashFraudScheme::PersonalPurchases => "billing_schemes",
            CashFraudScheme::GhostEmployee
            | CashFraudScheme::FalsifiedWages
            | CashFraudScheme::CommissionSchemes => "payroll_schemes",
            CashFraudScheme::MischaracterizedExpenses
            | CashFraudScheme::OverstatedExpenses
            | CashFraudScheme::FictitiousExpenses => "expense_reimbursement",
            CashFraudScheme::ForgedMaker
            | CashFraudScheme::ForgedEndorsement
            | CashFraudScheme::AlteredPayee
            | CashFraudScheme::AuthorizedMaker => "check_tampering",
            CashFraudScheme::FalseVoids | CashFraudScheme::FalseRefunds => "register_schemes",
        }
    }

    /// Returns the typical severity (1-5) for this scheme.
    pub fn severity(&self) -> u8 {
        match self {
            // Lower severity - often small amounts, easier to detect
            CashFraudScheme::FalseVoids
            | CashFraudScheme::FalseRefunds
            | CashFraudScheme::MischaracterizedExpenses => 3,
            // Medium severity
            CashFraudScheme::OverstatedExpenses
            | CashFraudScheme::Skimming
            | CashFraudScheme::Larceny
            | CashFraudScheme::PersonalPurchases
            | CashFraudScheme::FalsifiedWages => 4,
            // Higher severity - larger amounts, harder to detect
            CashFraudScheme::ShellCompany
            | CashFraudScheme::GhostEmployee
            | CashFraudScheme::FictitiousExpenses
            | CashFraudScheme::ForgedMaker
            | CashFraudScheme::AuthorizedMaker => 5,
            _ => 4,
        }
    }

    /// Returns the typical detection difficulty.
    pub fn detection_difficulty(&self) -> AnomalyDetectionDifficulty {
        match self {
            // Easy to detect with basic controls
            CashFraudScheme::FalseVoids | CashFraudScheme::FalseRefunds => {
                AnomalyDetectionDifficulty::Easy
            }
            // Moderate - requires reconciliation
            CashFraudScheme::Larceny | CashFraudScheme::OverstatedExpenses => {
                AnomalyDetectionDifficulty::Moderate
            }
            // Hard - requires sophisticated analysis
            CashFraudScheme::Skimming
            | CashFraudScheme::ShellCompany
            | CashFraudScheme::GhostEmployee => AnomalyDetectionDifficulty::Hard,
            // Expert level
            CashFraudScheme::SalesSkimming | CashFraudScheme::ReceivablesSkimming => {
                AnomalyDetectionDifficulty::Expert
            }
            _ => AnomalyDetectionDifficulty::Moderate,
        }
    }

    /// Returns all variants for iteration.
    pub fn all_variants() -> &'static [CashFraudScheme] {
        &[
            CashFraudScheme::Larceny,
            CashFraudScheme::Skimming,
            CashFraudScheme::SalesSkimming,
            CashFraudScheme::ReceivablesSkimming,
            CashFraudScheme::RefundSchemes,
            CashFraudScheme::ShellCompany,
            CashFraudScheme::NonAccompliceVendor,
            CashFraudScheme::PersonalPurchases,
            CashFraudScheme::GhostEmployee,
            CashFraudScheme::FalsifiedWages,
            CashFraudScheme::CommissionSchemes,
            CashFraudScheme::MischaracterizedExpenses,
            CashFraudScheme::OverstatedExpenses,
            CashFraudScheme::FictitiousExpenses,
            CashFraudScheme::ForgedMaker,
            CashFraudScheme::ForgedEndorsement,
            CashFraudScheme::AlteredPayee,
            CashFraudScheme::AuthorizedMaker,
            CashFraudScheme::FalseVoids,
            CashFraudScheme::FalseRefunds,
        ]
    }
}

/// Inventory and Other Asset fraud schemes under Asset Misappropriation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum AssetFraudScheme {
    // ========== Inventory Schemes ==========
    /// Misusing or converting inventory for personal benefit.
    InventoryMisuse,
    /// Stealing physical inventory items.
    InventoryTheft,
    /// Manipulating purchasing to facilitate theft.
    InventoryPurchasingScheme,
    /// Manipulating receiving/shipping to steal inventory.
    InventoryReceivingScheme,

    // ========== Other Asset Schemes ==========
    /// Misusing company equipment or vehicles.
    EquipmentMisuse,
    /// Theft of company equipment, tools, or supplies.
    EquipmentTheft,
    /// Unauthorized access to or theft of intellectual property.
    IntellectualPropertyTheft,
    /// Using company time/resources for personal business.
    TimeTheft,
}

impl AssetFraudScheme {
    /// Returns the ACFE category this scheme belongs to.
    pub fn category(&self) -> AcfeFraudCategory {
        AcfeFraudCategory::AssetMisappropriation
    }

    /// Returns the subcategory within the ACFE Fraud Tree.
    pub fn subcategory(&self) -> &'static str {
        match self {
            AssetFraudScheme::InventoryMisuse
            | AssetFraudScheme::InventoryTheft
            | AssetFraudScheme::InventoryPurchasingScheme
            | AssetFraudScheme::InventoryReceivingScheme => "inventory",
            _ => "other_assets",
        }
    }

    /// Returns the typical severity (1-5) for this scheme.
    pub fn severity(&self) -> u8 {
        match self {
            AssetFraudScheme::TimeTheft | AssetFraudScheme::EquipmentMisuse => 2,
            AssetFraudScheme::InventoryMisuse | AssetFraudScheme::EquipmentTheft => 3,
            AssetFraudScheme::InventoryTheft
            | AssetFraudScheme::InventoryPurchasingScheme
            | AssetFraudScheme::InventoryReceivingScheme => 4,
            AssetFraudScheme::IntellectualPropertyTheft => 5,
        }
    }
}

/// Corruption schemes under the ACFE Fraud Tree.
///
/// Corruption schemes involve the wrongful use of influence in a business
/// transaction to procure personal benefit.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum CorruptionScheme {
    // ========== Conflicts of Interest ==========
    /// Employee has undisclosed financial interest in purchasing decisions.
    PurchasingConflict,
    /// Employee has undisclosed relationship with customer/vendor.
    SalesConflict,
    /// Employee owns or has interest in competing business.
    OutsideBusinessInterest,
    /// Employee makes decisions benefiting family members.
    NepotismConflict,

    // ========== Bribery ==========
    /// Kickback payments from vendors for favorable treatment.
    InvoiceKickback,
    /// Collusion among vendors to inflate prices.
    BidRigging,
    /// Other cash payments for favorable decisions.
    CashBribery,
    /// Bribery of government officials.
    PublicOfficial,

    // ========== Illegal Gratuities ==========
    /// Gifts given after favorable decisions (not agreed in advance).
    IllegalGratuity,

    // ========== Economic Extortion ==========
    /// Demanding payment under threat of adverse action.
    EconomicExtortion,
}

impl CorruptionScheme {
    /// Returns the ACFE category this scheme belongs to.
    pub fn category(&self) -> AcfeFraudCategory {
        AcfeFraudCategory::Corruption
    }

    /// Returns the subcategory within the ACFE Fraud Tree.
    pub fn subcategory(&self) -> &'static str {
        match self {
            CorruptionScheme::PurchasingConflict
            | CorruptionScheme::SalesConflict
            | CorruptionScheme::OutsideBusinessInterest
            | CorruptionScheme::NepotismConflict => "conflicts_of_interest",
            CorruptionScheme::InvoiceKickback
            | CorruptionScheme::BidRigging
            | CorruptionScheme::CashBribery
            | CorruptionScheme::PublicOfficial => "bribery",
            CorruptionScheme::IllegalGratuity => "illegal_gratuities",
            CorruptionScheme::EconomicExtortion => "economic_extortion",
        }
    }

    /// Returns the typical severity (1-5) for this scheme.
    pub fn severity(&self) -> u8 {
        match self {
            // Lower severity conflicts of interest
            CorruptionScheme::NepotismConflict => 3,
            // Medium severity
            CorruptionScheme::PurchasingConflict
            | CorruptionScheme::SalesConflict
            | CorruptionScheme::OutsideBusinessInterest
            | CorruptionScheme::IllegalGratuity => 4,
            // High severity - active corruption
            CorruptionScheme::InvoiceKickback
            | CorruptionScheme::BidRigging
            | CorruptionScheme::CashBribery
            | CorruptionScheme::EconomicExtortion => 5,
            // Highest severity - involves public officials
            CorruptionScheme::PublicOfficial => 5,
        }
    }

    /// Returns the typical detection difficulty.
    pub fn detection_difficulty(&self) -> AnomalyDetectionDifficulty {
        match self {
            // Easier to detect with proper disclosure requirements
            CorruptionScheme::NepotismConflict | CorruptionScheme::OutsideBusinessInterest => {
                AnomalyDetectionDifficulty::Moderate
            }
            // Hard - requires transaction pattern analysis
            CorruptionScheme::PurchasingConflict
            | CorruptionScheme::SalesConflict
            | CorruptionScheme::BidRigging => AnomalyDetectionDifficulty::Hard,
            // Expert level - deliberate concealment
            CorruptionScheme::InvoiceKickback
            | CorruptionScheme::CashBribery
            | CorruptionScheme::PublicOfficial
            | CorruptionScheme::IllegalGratuity
            | CorruptionScheme::EconomicExtortion => AnomalyDetectionDifficulty::Expert,
        }
    }

    /// Returns all variants for iteration.
    pub fn all_variants() -> &'static [CorruptionScheme] {
        &[
            CorruptionScheme::PurchasingConflict,
            CorruptionScheme::SalesConflict,
            CorruptionScheme::OutsideBusinessInterest,
            CorruptionScheme::NepotismConflict,
            CorruptionScheme::InvoiceKickback,
            CorruptionScheme::BidRigging,
            CorruptionScheme::CashBribery,
            CorruptionScheme::PublicOfficial,
            CorruptionScheme::IllegalGratuity,
            CorruptionScheme::EconomicExtortion,
        ]
    }
}

/// Financial Statement Fraud schemes under the ACFE Fraud Tree.
///
/// Financial statement fraud involves the intentional misstatement or omission
/// of material information in financial reports.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum FinancialStatementScheme {
    // ========== Asset/Revenue Overstatement ==========
    /// Recording revenue before it is earned.
    PrematureRevenue,
    /// Deferring expenses to future periods.
    DelayedExpenses,
    /// Recording revenue for transactions that never occurred.
    FictitiousRevenues,
    /// Failing to record known liabilities.
    ConcealedLiabilities,
    /// Overstating the value of assets.
    ImproperAssetValuations,
    /// Omitting or misstating required disclosures.
    ImproperDisclosures,
    /// Manipulating timing of revenue recognition (channel stuffing).
    ChannelStuffing,
    /// Recognizing bill-and-hold revenue improperly.
    BillAndHold,
    /// Capitalizing expenses that should be expensed.
    ImproperCapitalization,

    // ========== Asset/Revenue Understatement ==========
    /// Understating revenue (often for tax purposes).
    UnderstatedRevenues,
    /// Recording excessive expenses.
    OverstatedExpenses,
    /// Recording excessive liabilities or reserves.
    OverstatedLiabilities,
    /// Undervaluing assets for writedowns/reserves.
    ImproperAssetWritedowns,
}

impl FinancialStatementScheme {
    /// Returns the ACFE category this scheme belongs to.
    pub fn category(&self) -> AcfeFraudCategory {
        AcfeFraudCategory::FinancialStatementFraud
    }

    /// Returns the subcategory within the ACFE Fraud Tree.
    pub fn subcategory(&self) -> &'static str {
        match self {
            FinancialStatementScheme::UnderstatedRevenues
            | FinancialStatementScheme::OverstatedExpenses
            | FinancialStatementScheme::OverstatedLiabilities
            | FinancialStatementScheme::ImproperAssetWritedowns => "understatement",
            _ => "overstatement",
        }
    }

    /// Returns the typical severity (1-5) for this scheme.
    pub fn severity(&self) -> u8 {
        // All financial statement fraud is high severity
        5
    }

    /// Returns the typical detection difficulty.
    pub fn detection_difficulty(&self) -> AnomalyDetectionDifficulty {
        match self {
            // Easier to detect with good analytics
            FinancialStatementScheme::ChannelStuffing
            | FinancialStatementScheme::DelayedExpenses => AnomalyDetectionDifficulty::Moderate,
            // Hard - requires deep analysis
            FinancialStatementScheme::PrematureRevenue
            | FinancialStatementScheme::ImproperCapitalization
            | FinancialStatementScheme::ImproperAssetWritedowns => AnomalyDetectionDifficulty::Hard,
            // Expert level
            FinancialStatementScheme::FictitiousRevenues
            | FinancialStatementScheme::ConcealedLiabilities
            | FinancialStatementScheme::ImproperAssetValuations
            | FinancialStatementScheme::ImproperDisclosures
            | FinancialStatementScheme::BillAndHold => AnomalyDetectionDifficulty::Expert,
            _ => AnomalyDetectionDifficulty::Hard,
        }
    }

    /// Returns all variants for iteration.
    pub fn all_variants() -> &'static [FinancialStatementScheme] {
        &[
            FinancialStatementScheme::PrematureRevenue,
            FinancialStatementScheme::DelayedExpenses,
            FinancialStatementScheme::FictitiousRevenues,
            FinancialStatementScheme::ConcealedLiabilities,
            FinancialStatementScheme::ImproperAssetValuations,
            FinancialStatementScheme::ImproperDisclosures,
            FinancialStatementScheme::ChannelStuffing,
            FinancialStatementScheme::BillAndHold,
            FinancialStatementScheme::ImproperCapitalization,
            FinancialStatementScheme::UnderstatedRevenues,
            FinancialStatementScheme::OverstatedExpenses,
            FinancialStatementScheme::OverstatedLiabilities,
            FinancialStatementScheme::ImproperAssetWritedowns,
        ]
    }
}

/// Unified ACFE scheme type that encompasses all fraud schemes.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum AcfeScheme {
    /// Cash-based fraud schemes.
    Cash(CashFraudScheme),
    /// Inventory and other asset fraud schemes.
    Asset(AssetFraudScheme),
    /// Corruption schemes.
    Corruption(CorruptionScheme),
    /// Financial statement fraud schemes.
    FinancialStatement(FinancialStatementScheme),
}

impl AcfeScheme {
    /// Returns the ACFE category this scheme belongs to.
    pub fn category(&self) -> AcfeFraudCategory {
        match self {
            AcfeScheme::Cash(s) => s.category(),
            AcfeScheme::Asset(s) => s.category(),
            AcfeScheme::Corruption(s) => s.category(),
            AcfeScheme::FinancialStatement(s) => s.category(),
        }
    }

    /// Returns the severity (1-5) for this scheme.
    pub fn severity(&self) -> u8 {
        match self {
            AcfeScheme::Cash(s) => s.severity(),
            AcfeScheme::Asset(s) => s.severity(),
            AcfeScheme::Corruption(s) => s.severity(),
            AcfeScheme::FinancialStatement(s) => s.severity(),
        }
    }

    /// Returns the detection difficulty for this scheme.
    pub fn detection_difficulty(&self) -> AnomalyDetectionDifficulty {
        match self {
            AcfeScheme::Cash(s) => s.detection_difficulty(),
            AcfeScheme::Asset(_) => AnomalyDetectionDifficulty::Moderate,
            AcfeScheme::Corruption(s) => s.detection_difficulty(),
            AcfeScheme::FinancialStatement(s) => s.detection_difficulty(),
        }
    }
}

/// How a fraud was detected (from ACFE statistics).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum AcfeDetectionMethod {
    /// Tip from employee, customer, vendor, or anonymous source.
    Tip,
    /// Internal audit procedures.
    InternalAudit,
    /// Management review and oversight.
    ManagementReview,
    /// External audit procedures.
    ExternalAudit,
    /// Account reconciliation discrepancies.
    AccountReconciliation,
    /// Document examination.
    DocumentExamination,
    /// Discovered by accident.
    ByAccident,
    /// Automated monitoring/IT controls.
    ItControls,
    /// Surveillance or investigation.
    Surveillance,
    /// Confession by perpetrator.
    Confession,
    /// Law enforcement notification.
    LawEnforcement,
    /// Other detection method.
    Other,
}

impl AcfeDetectionMethod {
    /// Returns the typical percentage of frauds detected by this method (from ACFE reports).
    pub fn typical_detection_rate(&self) -> f64 {
        match self {
            AcfeDetectionMethod::Tip => 0.42,
            AcfeDetectionMethod::InternalAudit => 0.16,
            AcfeDetectionMethod::ManagementReview => 0.12,
            AcfeDetectionMethod::ExternalAudit => 0.04,
            AcfeDetectionMethod::AccountReconciliation => 0.05,
            AcfeDetectionMethod::DocumentExamination => 0.04,
            AcfeDetectionMethod::ByAccident => 0.06,
            AcfeDetectionMethod::ItControls => 0.03,
            AcfeDetectionMethod::Surveillance => 0.02,
            AcfeDetectionMethod::Confession => 0.02,
            AcfeDetectionMethod::LawEnforcement => 0.01,
            AcfeDetectionMethod::Other => 0.03,
        }
    }

    /// Returns all variants for iteration.
    pub fn all_variants() -> &'static [AcfeDetectionMethod] {
        &[
            AcfeDetectionMethod::Tip,
            AcfeDetectionMethod::InternalAudit,
            AcfeDetectionMethod::ManagementReview,
            AcfeDetectionMethod::ExternalAudit,
            AcfeDetectionMethod::AccountReconciliation,
            AcfeDetectionMethod::DocumentExamination,
            AcfeDetectionMethod::ByAccident,
            AcfeDetectionMethod::ItControls,
            AcfeDetectionMethod::Surveillance,
            AcfeDetectionMethod::Confession,
            AcfeDetectionMethod::LawEnforcement,
            AcfeDetectionMethod::Other,
        ]
    }
}

/// Department/position of perpetrator (from ACFE statistics).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum PerpetratorDepartment {
    /// Accounting, finance, or bookkeeping.
    Accounting,
    /// Operations or manufacturing.
    Operations,
    /// Executive/upper management.
    Executive,
    /// Sales.
    Sales,
    /// Customer service.
    CustomerService,
    /// Purchasing/procurement.
    Purchasing,
    /// Information technology.
    It,
    /// Human resources.
    HumanResources,
    /// Administrative/clerical.
    Administrative,
    /// Warehouse/inventory.
    Warehouse,
    /// Board of directors.
    BoardOfDirectors,
    /// Other department.
    Other,
}

impl PerpetratorDepartment {
    /// Returns the typical percentage of frauds by department (from ACFE reports).
    pub fn typical_occurrence_rate(&self) -> f64 {
        match self {
            PerpetratorDepartment::Accounting => 0.21,
            PerpetratorDepartment::Operations => 0.17,
            PerpetratorDepartment::Executive => 0.12,
            PerpetratorDepartment::Sales => 0.11,
            PerpetratorDepartment::CustomerService => 0.07,
            PerpetratorDepartment::Purchasing => 0.06,
            PerpetratorDepartment::It => 0.05,
            PerpetratorDepartment::HumanResources => 0.04,
            PerpetratorDepartment::Administrative => 0.04,
            PerpetratorDepartment::Warehouse => 0.03,
            PerpetratorDepartment::BoardOfDirectors => 0.02,
            PerpetratorDepartment::Other => 0.08,
        }
    }

    /// Returns the typical median loss by perpetrator department.
    pub fn typical_median_loss(&self) -> Decimal {
        match self {
            PerpetratorDepartment::Executive => Decimal::new(600_000, 0),
            PerpetratorDepartment::BoardOfDirectors => Decimal::new(500_000, 0),
            PerpetratorDepartment::Sales => Decimal::new(150_000, 0),
            PerpetratorDepartment::Accounting => Decimal::new(130_000, 0),
            PerpetratorDepartment::Purchasing => Decimal::new(120_000, 0),
            PerpetratorDepartment::Operations => Decimal::new(100_000, 0),
            PerpetratorDepartment::It => Decimal::new(100_000, 0),
            _ => Decimal::new(80_000, 0),
        }
    }
}

/// Perpetrator position level (from ACFE statistics).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum PerpetratorLevel {
    /// Entry-level employee.
    Employee,
    /// Manager or supervisor.
    Manager,
    /// Owner, executive, or C-level.
    OwnerExecutive,
}

impl PerpetratorLevel {
    /// Returns the typical percentage of frauds by position level.
    pub fn typical_occurrence_rate(&self) -> f64 {
        match self {
            PerpetratorLevel::Employee => 0.42,
            PerpetratorLevel::Manager => 0.36,
            PerpetratorLevel::OwnerExecutive => 0.22,
        }
    }

    /// Returns the typical median loss by position level.
    pub fn typical_median_loss(&self) -> Decimal {
        match self {
            PerpetratorLevel::Employee => Decimal::new(50_000, 0),
            PerpetratorLevel::Manager => Decimal::new(125_000, 0),
            PerpetratorLevel::OwnerExecutive => Decimal::new(337_000, 0),
        }
    }
}

/// ACFE Calibration data for fraud generation.
///
/// Contains statistical parameters based on ACFE Report to the Nations
/// for realistic fraud pattern generation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AcfeCalibration {
    /// Overall median loss for occupational fraud ($117,000 typical).
    pub median_loss: Decimal,
    /// Median duration in months before detection (12 months typical).
    pub median_duration_months: u32,
    /// Distribution of fraud by category.
    pub category_distribution: HashMap<String, f64>,
    /// Distribution of detection methods.
    pub detection_method_distribution: HashMap<String, f64>,
    /// Distribution by perpetrator department.
    pub department_distribution: HashMap<String, f64>,
    /// Distribution by perpetrator level.
    pub level_distribution: HashMap<String, f64>,
    /// Average number of red flags per fraud case.
    pub avg_red_flags_per_case: f64,
    /// Percentage of frauds involving collusion.
    pub collusion_rate: f64,
}

impl Default for AcfeCalibration {
    fn default() -> Self {
        let mut category_distribution = HashMap::new();
        category_distribution.insert("asset_misappropriation".to_string(), 0.86);
        category_distribution.insert("corruption".to_string(), 0.33);
        category_distribution.insert("financial_statement_fraud".to_string(), 0.10);

        let mut detection_method_distribution = HashMap::new();
        for method in AcfeDetectionMethod::all_variants() {
            detection_method_distribution.insert(
                format!("{method:?}").to_lowercase(),
                method.typical_detection_rate(),
            );
        }

        let mut department_distribution = HashMap::new();
        department_distribution.insert("accounting".to_string(), 0.21);
        department_distribution.insert("operations".to_string(), 0.17);
        department_distribution.insert("executive".to_string(), 0.12);
        department_distribution.insert("sales".to_string(), 0.11);
        department_distribution.insert("customer_service".to_string(), 0.07);
        department_distribution.insert("purchasing".to_string(), 0.06);
        department_distribution.insert("other".to_string(), 0.26);

        let mut level_distribution = HashMap::new();
        level_distribution.insert("employee".to_string(), 0.42);
        level_distribution.insert("manager".to_string(), 0.36);
        level_distribution.insert("owner_executive".to_string(), 0.22);

        Self {
            median_loss: Decimal::new(117_000, 0),
            median_duration_months: 12,
            category_distribution,
            detection_method_distribution,
            department_distribution,
            level_distribution,
            avg_red_flags_per_case: 2.8,
            collusion_rate: 0.50,
        }
    }
}

impl AcfeCalibration {
    /// Creates a new ACFE calibration with the given parameters.
    pub fn new(median_loss: Decimal, median_duration_months: u32) -> Self {
        Self {
            median_loss,
            median_duration_months,
            ..Self::default()
        }
    }

    /// Returns the median loss for a specific category.
    pub fn median_loss_for_category(&self, category: AcfeFraudCategory) -> Decimal {
        category.typical_median_loss()
    }

    /// Returns the median duration for a specific category.
    pub fn median_duration_for_category(&self, category: AcfeFraudCategory) -> u32 {
        category.typical_detection_months()
    }

    /// Validates the calibration data.
    pub fn validate(&self) -> Result<(), String> {
        if self.median_loss <= Decimal::ZERO {
            return Err("Median loss must be positive".to_string());
        }
        if self.median_duration_months == 0 {
            return Err("Median duration must be at least 1 month".to_string());
        }
        if self.collusion_rate < 0.0 || self.collusion_rate > 1.0 {
            return Err("Collusion rate must be between 0.0 and 1.0".to_string());
        }
        Ok(())
    }
}

/// Fraud Triangle components (Pressure, Opportunity, Rationalization).
///
/// The fraud triangle is a model for explaining the factors that cause
/// someone to commit occupational fraud.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FraudTriangle {
    /// Pressure or incentive to commit fraud.
    pub pressure: PressureType,
    /// Opportunity factors that enable fraud.
    pub opportunities: Vec<OpportunityFactor>,
    /// Rationalization used to justify the fraud.
    pub rationalization: Rationalization,
}

impl FraudTriangle {
    /// Creates a new fraud triangle.
    pub fn new(
        pressure: PressureType,
        opportunities: Vec<OpportunityFactor>,
        rationalization: Rationalization,
    ) -> Self {
        Self {
            pressure,
            opportunities,
            rationalization,
        }
    }

    /// Returns a risk score based on the fraud triangle components.
    pub fn risk_score(&self) -> f64 {
        let pressure_score = self.pressure.risk_weight();
        let opportunity_score: f64 = self
            .opportunities
            .iter()
            .map(OpportunityFactor::risk_weight)
            .sum::<f64>()
            / self.opportunities.len().max(1) as f64;
        let rationalization_score = self.rationalization.risk_weight();

        (pressure_score + opportunity_score + rationalization_score) / 3.0
    }
}

/// Types of pressure/incentive that can lead to fraud.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum PressureType {
    // Financial Pressures
    /// Personal financial difficulties (debt, lifestyle beyond means).
    PersonalFinancialDifficulties,
    /// Pressure to meet financial targets/earnings expectations.
    FinancialTargets,
    /// Market or analyst expectations.
    MarketExpectations,
    /// Debt covenant compliance requirements.
    CovenantCompliance,
    /// Credit rating maintenance.
    CreditRatingMaintenance,
    /// Acquisition/merger valuation pressure.
    AcquisitionValuation,

    // Non-Financial Pressures
    /// Fear of job loss.
    JobSecurity,
    /// Pressure to maintain status or image.
    StatusMaintenance,
    /// Gambling addiction.
    GamblingAddiction,
    /// Substance abuse issues.
    SubstanceAbuse,
    /// Family pressure or obligations.
    FamilyPressure,
    /// Greed or desire for more.
    Greed,
}

impl PressureType {
    /// Returns the risk weight (0.0-1.0) for this pressure type.
    pub fn risk_weight(&self) -> f64 {
        match self {
            PressureType::PersonalFinancialDifficulties => 0.80,
            PressureType::FinancialTargets => 0.75,
            PressureType::MarketExpectations => 0.70,
            PressureType::CovenantCompliance => 0.85,
            PressureType::CreditRatingMaintenance => 0.70,
            PressureType::AcquisitionValuation => 0.75,
            PressureType::JobSecurity => 0.65,
            PressureType::StatusMaintenance => 0.55,
            PressureType::GamblingAddiction => 0.90,
            PressureType::SubstanceAbuse => 0.85,
            PressureType::FamilyPressure => 0.60,
            PressureType::Greed => 0.70,
        }
    }
}

/// Opportunity factors that enable fraud.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum OpportunityFactor {
    /// Weak internal controls.
    WeakInternalControls,
    /// Lack of segregation of duties.
    LackOfSegregation,
    /// Override capability.
    ManagementOverride,
    /// Complex or unusual transactions.
    ComplexTransactions,
    /// Related party transactions.
    RelatedPartyTransactions,
    /// Poor tone at the top.
    PoorToneAtTop,
    /// Inadequate supervision.
    InadequateSupervision,
    /// Access to assets without accountability.
    AssetAccess,
    /// Inadequate record keeping.
    PoorRecordKeeping,
    /// Failure to discipline fraud perpetrators.
    LackOfDiscipline,
    /// Lack of independent checks.
    LackOfIndependentChecks,
}

impl OpportunityFactor {
    /// Returns the risk weight (0.0-1.0) for this opportunity factor.
    pub fn risk_weight(&self) -> f64 {
        match self {
            OpportunityFactor::WeakInternalControls => 0.85,
            OpportunityFactor::LackOfSegregation => 0.80,
            OpportunityFactor::ManagementOverride => 0.90,
            OpportunityFactor::ComplexTransactions => 0.70,
            OpportunityFactor::RelatedPartyTransactions => 0.75,
            OpportunityFactor::PoorToneAtTop => 0.85,
            OpportunityFactor::InadequateSupervision => 0.75,
            OpportunityFactor::AssetAccess => 0.70,
            OpportunityFactor::PoorRecordKeeping => 0.65,
            OpportunityFactor::LackOfDiscipline => 0.60,
            OpportunityFactor::LackOfIndependentChecks => 0.75,
        }
    }
}

/// Rationalizations used by fraud perpetrators.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Rationalization {
    /// "I'm just borrowing; I'll pay it back."
    TemporaryBorrowing,
    /// "Everyone does it."
    EveryoneDoesIt,
    /// "It's for the good of the company."
    ForTheCompanyGood,
    /// "I deserve this; the company owes me."
    Entitlement,
    /// "I was just following orders."
    FollowingOrders,
    /// "They won't miss it; they have plenty."
    TheyWontMissIt,
    /// "I need it more than they do."
    NeedItMore,
    /// "It's not really stealing."
    NotReallyStealing,
    /// "I'm underpaid for what I do."
    Underpaid,
    /// "It's a victimless crime."
    VictimlessCrime,
}

impl Rationalization {
    /// Returns the risk weight (0.0-1.0) for this rationalization.
    pub fn risk_weight(&self) -> f64 {
        match self {
            // More dangerous rationalizations
            Rationalization::Entitlement => 0.85,
            Rationalization::EveryoneDoesIt => 0.80,
            Rationalization::NotReallyStealing => 0.80,
            Rationalization::TheyWontMissIt => 0.75,
            // Medium risk
            Rationalization::Underpaid => 0.70,
            Rationalization::ForTheCompanyGood => 0.65,
            Rationalization::NeedItMore => 0.65,
            // Lower risk (still indicates fraud)
            Rationalization::TemporaryBorrowing => 0.60,
            Rationalization::FollowingOrders => 0.55,
            Rationalization::VictimlessCrime => 0.60,
        }
    }
}

// ============================================================================
// NEAR-MISS TYPES
// ============================================================================

/// Type of near-miss pattern (suspicious but legitimate).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum NearMissPattern {
    /// Transaction very similar to another (possible duplicate but legitimate).
    NearDuplicate {
        /// Date difference from similar transaction.
        date_difference_days: u32,
        /// Original transaction ID.
        similar_transaction_id: String,
    },
    /// Amount just below approval threshold (but legitimate).
    ThresholdProximity {
        /// The threshold being approached.
        threshold: Decimal,
        /// Percentage of threshold (0.0-1.0).
        proximity: f64,
    },
    /// Unusual but legitimate business pattern.
    UnusualLegitimate {
        /// Type of legitimate pattern.
        pattern_type: LegitimatePatternType,
        /// Business justification.
        justification: String,
    },
    /// Error that was caught and corrected.
    CorrectedError {
        /// Days until correction.
        correction_lag_days: u32,
        /// Correction document ID.
        correction_document_id: String,
    },
}

/// Types of unusual but legitimate business patterns.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum LegitimatePatternType {
    /// Year-end bonus payment.
    YearEndBonus,
    /// Contract prepayment.
    ContractPrepayment,
    /// Settlement payment.
    SettlementPayment,
    /// Insurance claim.
    InsuranceClaim,
    /// One-time vendor payment.
    OneTimePayment,
    /// Asset disposal.
    AssetDisposal,
    /// Seasonal inventory buildup.
    SeasonalInventory,
    /// Promotional spending.
    PromotionalSpending,
}

impl LegitimatePatternType {
    /// Returns a description of this pattern type.
    pub fn description(&self) -> &'static str {
        match self {
            LegitimatePatternType::YearEndBonus => "Year-end bonus payment",
            LegitimatePatternType::ContractPrepayment => "Contract prepayment per terms",
            LegitimatePatternType::SettlementPayment => "Legal settlement payment",
            LegitimatePatternType::InsuranceClaim => "Insurance claim reimbursement",
            LegitimatePatternType::OneTimePayment => "One-time vendor payment",
            LegitimatePatternType::AssetDisposal => "Fixed asset disposal",
            LegitimatePatternType::SeasonalInventory => "Seasonal inventory buildup",
            LegitimatePatternType::PromotionalSpending => "Promotional campaign spending",
        }
    }
}

/// What might trigger a false positive for this near-miss.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum FalsePositiveTrigger {
    /// Amount is near threshold.
    AmountNearThreshold,
    /// Timing is unusual.
    UnusualTiming,
    /// Similar to existing transaction.
    SimilarTransaction,
    /// New counterparty.
    NewCounterparty,
    /// Account combination unusual.
    UnusualAccountCombination,
    /// Volume spike.
    VolumeSpike,
    /// Round amount.
    RoundAmount,
}

/// Label for a near-miss case.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NearMissLabel {
    /// Document ID.
    pub document_id: String,
    /// The near-miss pattern.
    pub pattern: NearMissPattern,
    /// How suspicious it appears (0.0-1.0).
    pub suspicion_score: f64,
    /// What would trigger a false positive.
    pub false_positive_trigger: FalsePositiveTrigger,
    /// Why this is actually legitimate.
    pub explanation: String,
}

impl NearMissLabel {
    /// Creates a new near-miss label.
    pub fn new(
        document_id: impl Into<String>,
        pattern: NearMissPattern,
        suspicion_score: f64,
        trigger: FalsePositiveTrigger,
        explanation: impl Into<String>,
    ) -> Self {
        Self {
            document_id: document_id.into(),
            pattern,
            suspicion_score: suspicion_score.clamp(0.0, 1.0),
            false_positive_trigger: trigger,
            explanation: explanation.into(),
        }
    }
}

/// Configuration for anomaly rates.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnomalyRateConfig {
    /// Overall anomaly rate (0.0 - 1.0).
    pub total_rate: f64,
    /// Fraud rate as proportion of anomalies.
    pub fraud_rate: f64,
    /// Error rate as proportion of anomalies.
    pub error_rate: f64,
    /// Process issue rate as proportion of anomalies.
    pub process_issue_rate: f64,
    /// Statistical anomaly rate as proportion of anomalies.
    pub statistical_rate: f64,
    /// Relational anomaly rate as proportion of anomalies.
    pub relational_rate: f64,
}

impl Default for AnomalyRateConfig {
    fn default() -> Self {
        Self {
            total_rate: 0.02,         // 2% of transactions are anomalous
            fraud_rate: 0.25,         // 25% of anomalies are fraud
            error_rate: 0.35,         // 35% of anomalies are errors
            process_issue_rate: 0.20, // 20% are process issues
            statistical_rate: 0.15,   // 15% are statistical
            relational_rate: 0.05,    // 5% are relational
        }
    }
}

impl AnomalyRateConfig {
    /// Validates that rates sum to approximately 1.0.
    pub fn validate(&self) -> Result<(), String> {
        let sum = self.fraud_rate
            + self.error_rate
            + self.process_issue_rate
            + self.statistical_rate
            + self.relational_rate;

        if (sum - 1.0).abs() > 0.01 {
            return Err(format!("Anomaly category rates must sum to 1.0, got {sum}"));
        }

        if self.total_rate < 0.0 || self.total_rate > 1.0 {
            return Err(format!(
                "Total rate must be between 0.0 and 1.0, got {}",
                self.total_rate
            ));
        }

        Ok(())
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;
    use rust_decimal_macros::dec;

    #[test]
    fn test_anomaly_type_category() {
        let fraud = AnomalyType::Fraud(FraudType::SelfApproval);
        assert_eq!(fraud.category(), "Fraud");
        assert!(fraud.is_intentional());

        let error = AnomalyType::Error(ErrorType::DuplicateEntry);
        assert_eq!(error.category(), "Error");
        assert!(!error.is_intentional());
    }

    #[test]
    fn test_labeled_anomaly() {
        let anomaly = LabeledAnomaly::new(
            "ANO001".to_string(),
            AnomalyType::Fraud(FraudType::SelfApproval),
            "JE001".to_string(),
            "JE".to_string(),
            "1000".to_string(),
            NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(),
        )
        .with_description("User approved their own expense report")
        .with_related_entity("USER001");

        assert_eq!(anomaly.severity, 3);
        assert!(anomaly.is_injected);
        assert_eq!(anomaly.related_entities.len(), 1);
    }

    #[test]
    fn test_labeled_anomaly_with_provenance() {
        let anomaly = LabeledAnomaly::new(
            "ANO001".to_string(),
            AnomalyType::Fraud(FraudType::SelfApproval),
            "JE001".to_string(),
            "JE".to_string(),
            "1000".to_string(),
            NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(),
        )
        .with_run_id("run-123")
        .with_generation_seed(42)
        .with_causal_reason(AnomalyCausalReason::RandomRate { base_rate: 0.02 })
        .with_structured_strategy(InjectionStrategy::SelfApproval {
            user_id: "USER001".to_string(),
        })
        .with_scenario("scenario-001")
        .with_original_document_hash("abc123");

        assert_eq!(anomaly.run_id, Some("run-123".to_string()));
        assert_eq!(anomaly.generation_seed, Some(42));
        assert!(anomaly.causal_reason.is_some());
        assert!(anomaly.structured_strategy.is_some());
        assert_eq!(anomaly.scenario_id, Some("scenario-001".to_string()));
        assert_eq!(anomaly.original_document_hash, Some("abc123".to_string()));

        // Check that legacy injection_strategy is also set
        assert_eq!(anomaly.injection_strategy, Some("SelfApproval".to_string()));
    }

    #[test]
    fn test_labeled_anomaly_derivation_chain() {
        let parent = LabeledAnomaly::new(
            "ANO001".to_string(),
            AnomalyType::Fraud(FraudType::DuplicatePayment),
            "JE001".to_string(),
            "JE".to_string(),
            "1000".to_string(),
            NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(),
        );

        let child = LabeledAnomaly::new(
            "ANO002".to_string(),
            AnomalyType::Error(ErrorType::DuplicateEntry),
            "JE002".to_string(),
            "JE".to_string(),
            "1000".to_string(),
            NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(),
        )
        .with_parent_anomaly(&parent.anomaly_id);

        assert_eq!(child.parent_anomaly_id, Some("ANO001".to_string()));
    }

    #[test]
    fn test_injection_strategy_description() {
        let strategy = InjectionStrategy::AmountManipulation {
            original: dec!(1000),
            factor: 2.5,
        };
        assert_eq!(strategy.description(), "Amount multiplied by 2.50");
        assert_eq!(strategy.strategy_type(), "AmountManipulation");

        let strategy = InjectionStrategy::ThresholdAvoidance {
            threshold: dec!(10000),
            adjusted_amount: dec!(9999),
        };
        assert_eq!(
            strategy.description(),
            "Amount adjusted to avoid 10000 threshold"
        );

        let strategy = InjectionStrategy::DateShift {
            days_shifted: -5,
            original_date: NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(),
        };
        assert_eq!(strategy.description(), "Date backdated by 5 days");

        let strategy = InjectionStrategy::DateShift {
            days_shifted: 3,
            original_date: NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(),
        };
        assert_eq!(strategy.description(), "Date forward-dated by 3 days");
    }

    #[test]
    fn test_causal_reason_variants() {
        let reason = AnomalyCausalReason::RandomRate { base_rate: 0.02 };
        if let AnomalyCausalReason::RandomRate { base_rate } = reason {
            assert!((base_rate - 0.02).abs() < 0.001);
        }

        let reason = AnomalyCausalReason::TemporalPattern {
            pattern_name: "year_end_spike".to_string(),
        };
        if let AnomalyCausalReason::TemporalPattern { pattern_name } = reason {
            assert_eq!(pattern_name, "year_end_spike");
        }

        let reason = AnomalyCausalReason::ScenarioStep {
            scenario_type: "kickback".to_string(),
            step_number: 3,
        };
        if let AnomalyCausalReason::ScenarioStep {
            scenario_type,
            step_number,
        } = reason
        {
            assert_eq!(scenario_type, "kickback");
            assert_eq!(step_number, 3);
        }
    }

    #[test]
    fn test_feature_vector_length() {
        let anomaly = LabeledAnomaly::new(
            "ANO001".to_string(),
            AnomalyType::Fraud(FraudType::SelfApproval),
            "JE001".to_string(),
            "JE".to_string(),
            "1000".to_string(),
            NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(),
        );

        let features = anomaly.to_features();
        assert_eq!(features.len(), LabeledAnomaly::feature_count());
        assert_eq!(features.len(), LabeledAnomaly::feature_names().len());
    }

    #[test]
    fn test_feature_vector_with_provenance() {
        let anomaly = LabeledAnomaly::new(
            "ANO001".to_string(),
            AnomalyType::Fraud(FraudType::SelfApproval),
            "JE001".to_string(),
            "JE".to_string(),
            "1000".to_string(),
            NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(),
        )
        .with_scenario("scenario-001")
        .with_parent_anomaly("ANO000");

        let features = anomaly.to_features();

        // Last two features should be 1.0 (has scenario, has parent)
        assert_eq!(features[features.len() - 2], 1.0); // is_scenario_part
        assert_eq!(features[features.len() - 1], 1.0); // is_derived
    }

    #[test]
    fn test_anomaly_summary() {
        let anomalies = vec![
            LabeledAnomaly::new(
                "ANO001".to_string(),
                AnomalyType::Fraud(FraudType::SelfApproval),
                "JE001".to_string(),
                "JE".to_string(),
                "1000".to_string(),
                NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(),
            ),
            LabeledAnomaly::new(
                "ANO002".to_string(),
                AnomalyType::Error(ErrorType::DuplicateEntry),
                "JE002".to_string(),
                "JE".to_string(),
                "1000".to_string(),
                NaiveDate::from_ymd_opt(2024, 1, 16).unwrap(),
            ),
        ];

        let summary = AnomalySummary::from_anomalies(&anomalies);

        assert_eq!(summary.total_count, 2);
        assert_eq!(summary.by_category.get("Fraud"), Some(&1));
        assert_eq!(summary.by_category.get("Error"), Some(&1));
    }

    #[test]
    fn test_rate_config_validation() {
        let config = AnomalyRateConfig::default();
        assert!(config.validate().is_ok());

        let bad_config = AnomalyRateConfig {
            fraud_rate: 0.5,
            error_rate: 0.5,
            process_issue_rate: 0.5, // Sum > 1.0
            ..Default::default()
        };
        assert!(bad_config.validate().is_err());
    }

    #[test]
    fn test_injection_strategy_serialization() {
        let strategy = InjectionStrategy::SoDViolation {
            duty1: "CreatePO".to_string(),
            duty2: "ApprovePO".to_string(),
            violating_user: "USER001".to_string(),
        };

        let json = serde_json::to_string(&strategy).unwrap();
        let deserialized: InjectionStrategy = serde_json::from_str(&json).unwrap();

        assert_eq!(strategy, deserialized);
    }

    #[test]
    fn test_labeled_anomaly_serialization_with_provenance() {
        let anomaly = LabeledAnomaly::new(
            "ANO001".to_string(),
            AnomalyType::Fraud(FraudType::SelfApproval),
            "JE001".to_string(),
            "JE".to_string(),
            "1000".to_string(),
            NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(),
        )
        .with_run_id("run-123")
        .with_generation_seed(42)
        .with_causal_reason(AnomalyCausalReason::RandomRate { base_rate: 0.02 });

        let json = serde_json::to_string(&anomaly).unwrap();
        let deserialized: LabeledAnomaly = serde_json::from_str(&json).unwrap();

        assert_eq!(anomaly.run_id, deserialized.run_id);
        assert_eq!(anomaly.generation_seed, deserialized.generation_seed);
    }

    // ========================================
    // FR-003 ENHANCED TAXONOMY TESTS
    // ========================================

    #[test]
    fn test_anomaly_category_from_anomaly_type() {
        // Fraud mappings
        let fraud_vendor = AnomalyType::Fraud(FraudType::FictitiousVendor);
        assert_eq!(
            AnomalyCategory::from_anomaly_type(&fraud_vendor),
            AnomalyCategory::FictitiousVendor
        );

        let fraud_kickback = AnomalyType::Fraud(FraudType::KickbackScheme);
        assert_eq!(
            AnomalyCategory::from_anomaly_type(&fraud_kickback),
            AnomalyCategory::VendorKickback
        );

        let fraud_structured = AnomalyType::Fraud(FraudType::SplitTransaction);
        assert_eq!(
            AnomalyCategory::from_anomaly_type(&fraud_structured),
            AnomalyCategory::StructuredTransaction
        );

        // Error mappings
        let error_duplicate = AnomalyType::Error(ErrorType::DuplicateEntry);
        assert_eq!(
            AnomalyCategory::from_anomaly_type(&error_duplicate),
            AnomalyCategory::DuplicatePayment
        );

        // Process issue mappings
        let process_skip = AnomalyType::ProcessIssue(ProcessIssueType::SkippedApproval);
        assert_eq!(
            AnomalyCategory::from_anomaly_type(&process_skip),
            AnomalyCategory::MissingApproval
        );

        // Relational mappings
        let relational_circular =
            AnomalyType::Relational(RelationalAnomalyType::CircularTransaction);
        assert_eq!(
            AnomalyCategory::from_anomaly_type(&relational_circular),
            AnomalyCategory::CircularFlow
        );
    }

    #[test]
    fn test_anomaly_category_ordinal() {
        assert_eq!(AnomalyCategory::FictitiousVendor.ordinal(), 0);
        assert_eq!(AnomalyCategory::VendorKickback.ordinal(), 1);
        assert_eq!(AnomalyCategory::Custom("test".to_string()).ordinal(), 14);
    }

    #[test]
    fn test_contributing_factor() {
        let factor = ContributingFactor::new(
            FactorType::AmountDeviation,
            15000.0,
            10000.0,
            true,
            0.5,
            "Amount exceeds threshold",
        );

        assert_eq!(factor.factor_type, FactorType::AmountDeviation);
        assert_eq!(factor.value, 15000.0);
        assert_eq!(factor.threshold, 10000.0);
        assert!(factor.direction_greater);

        // Contribution: (15000 - 10000) / 10000 * 0.5 = 0.25
        let contribution = factor.contribution();
        assert!((contribution - 0.25).abs() < 0.01);
    }

    #[test]
    fn test_contributing_factor_with_evidence() {
        let mut data = HashMap::new();
        data.insert("expected".to_string(), "10000".to_string());
        data.insert("actual".to_string(), "15000".to_string());

        let factor = ContributingFactor::new(
            FactorType::AmountDeviation,
            15000.0,
            10000.0,
            true,
            0.5,
            "Amount deviation detected",
        )
        .with_evidence("transaction_history", data);

        assert!(factor.evidence.is_some());
        let evidence = factor.evidence.unwrap();
        assert_eq!(evidence.source, "transaction_history");
        assert_eq!(evidence.data.get("expected"), Some(&"10000".to_string()));
    }

    #[test]
    fn test_enhanced_anomaly_label() {
        let base = LabeledAnomaly::new(
            "ANO001".to_string(),
            AnomalyType::Fraud(FraudType::DuplicatePayment),
            "JE001".to_string(),
            "JE".to_string(),
            "1000".to_string(),
            NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(),
        );

        let enhanced = EnhancedAnomalyLabel::from_base(base)
            .with_confidence(0.85)
            .with_severity(0.7)
            .with_factor(ContributingFactor::new(
                FactorType::DuplicateIndicator,
                1.0,
                0.5,
                true,
                0.4,
                "Duplicate payment detected",
            ))
            .with_secondary_category(AnomalyCategory::StructuredTransaction);

        assert_eq!(enhanced.category, AnomalyCategory::DuplicatePayment);
        assert_eq!(enhanced.enhanced_confidence, 0.85);
        assert_eq!(enhanced.enhanced_severity, 0.7);
        assert_eq!(enhanced.contributing_factors.len(), 1);
        assert_eq!(enhanced.secondary_categories.len(), 1);
    }

    #[test]
    fn test_enhanced_anomaly_label_features() {
        let base = LabeledAnomaly::new(
            "ANO001".to_string(),
            AnomalyType::Fraud(FraudType::SelfApproval),
            "JE001".to_string(),
            "JE".to_string(),
            "1000".to_string(),
            NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(),
        );

        let enhanced = EnhancedAnomalyLabel::from_base(base)
            .with_confidence(0.9)
            .with_severity(0.8)
            .with_factor(ContributingFactor::new(
                FactorType::ControlBypass,
                1.0,
                0.0,
                true,
                0.5,
                "Control bypass detected",
            ));

        let features = enhanced.to_features();

        // Should have 25 features (15 base + 10 enhanced)
        assert_eq!(features.len(), EnhancedAnomalyLabel::feature_count());
        assert_eq!(features.len(), 25);

        // Check enhanced confidence is in features
        assert_eq!(features[15], 0.9); // enhanced_confidence

        // Check has_control_bypass flag
        assert_eq!(features[21], 1.0); // has_control_bypass
    }

    #[test]
    fn test_enhanced_anomaly_label_feature_names() {
        let names = EnhancedAnomalyLabel::feature_names();
        assert_eq!(names.len(), 25);
        assert!(names.contains(&"enhanced_confidence"));
        assert!(names.contains(&"enhanced_severity"));
        assert!(names.contains(&"has_control_bypass"));
    }

    #[test]
    fn test_factor_type_names() {
        assert_eq!(FactorType::AmountDeviation.name(), "amount_deviation");
        assert_eq!(FactorType::ThresholdProximity.name(), "threshold_proximity");
        assert_eq!(FactorType::ControlBypass.name(), "control_bypass");
    }

    #[test]
    fn test_anomaly_category_serialization() {
        let category = AnomalyCategory::CircularFlow;
        let json = serde_json::to_string(&category).unwrap();
        let deserialized: AnomalyCategory = serde_json::from_str(&json).unwrap();
        assert_eq!(category, deserialized);

        let custom = AnomalyCategory::Custom("custom_type".to_string());
        let json = serde_json::to_string(&custom).unwrap();
        let deserialized: AnomalyCategory = serde_json::from_str(&json).unwrap();
        assert_eq!(custom, deserialized);
    }

    #[test]
    fn test_enhanced_label_secondary_category_dedup() {
        let base = LabeledAnomaly::new(
            "ANO001".to_string(),
            AnomalyType::Fraud(FraudType::DuplicatePayment),
            "JE001".to_string(),
            "JE".to_string(),
            "1000".to_string(),
            NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(),
        );

        let enhanced = EnhancedAnomalyLabel::from_base(base)
            // Try to add the primary category as secondary (should be ignored)
            .with_secondary_category(AnomalyCategory::DuplicatePayment)
            // Add a valid secondary
            .with_secondary_category(AnomalyCategory::TimingAnomaly)
            // Try to add duplicate secondary (should be ignored)
            .with_secondary_category(AnomalyCategory::TimingAnomaly);

        // Should only have 1 secondary category (TimingAnomaly)
        assert_eq!(enhanced.secondary_categories.len(), 1);
        assert_eq!(
            enhanced.secondary_categories[0],
            AnomalyCategory::TimingAnomaly
        );
    }

    // ==========================================================================
    // Accounting Standards Fraud Type Tests
    // ==========================================================================

    #[test]
    fn test_revenue_recognition_fraud_types() {
        // Test ASC 606/IFRS 15 related fraud types
        let fraud_types = [
            FraudType::ImproperRevenueRecognition,
            FraudType::ImproperPoAllocation,
            FraudType::VariableConsiderationManipulation,
            FraudType::ContractModificationMisstatement,
        ];

        for fraud_type in fraud_types {
            let anomaly_type = AnomalyType::Fraud(fraud_type);
            assert_eq!(anomaly_type.category(), "Fraud");
            assert!(anomaly_type.is_intentional());
            assert!(anomaly_type.severity() >= 3);
        }
    }

    #[test]
    fn test_lease_accounting_fraud_types() {
        // Test ASC 842/IFRS 16 related fraud types
        let fraud_types = [
            FraudType::LeaseClassificationManipulation,
            FraudType::OffBalanceSheetLease,
            FraudType::LeaseLiabilityUnderstatement,
            FraudType::RouAssetMisstatement,
        ];

        for fraud_type in fraud_types {
            let anomaly_type = AnomalyType::Fraud(fraud_type);
            assert_eq!(anomaly_type.category(), "Fraud");
            assert!(anomaly_type.is_intentional());
            assert!(anomaly_type.severity() >= 3);
        }

        // Off-balance sheet lease fraud should be high severity
        assert_eq!(FraudType::OffBalanceSheetLease.severity(), 5);
    }

    #[test]
    fn test_fair_value_fraud_types() {
        // Test ASC 820/IFRS 13 related fraud types
        let fraud_types = [
            FraudType::FairValueHierarchyManipulation,
            FraudType::Level3InputManipulation,
            FraudType::ValuationTechniqueManipulation,
        ];

        for fraud_type in fraud_types {
            let anomaly_type = AnomalyType::Fraud(fraud_type);
            assert_eq!(anomaly_type.category(), "Fraud");
            assert!(anomaly_type.is_intentional());
            assert!(anomaly_type.severity() >= 4);
        }

        // Level 3 manipulation is highest severity (unobservable inputs)
        assert_eq!(FraudType::Level3InputManipulation.severity(), 5);
    }

    #[test]
    fn test_impairment_fraud_types() {
        // Test ASC 360/IAS 36 related fraud types
        let fraud_types = [
            FraudType::DelayedImpairment,
            FraudType::ImpairmentTestAvoidance,
            FraudType::CashFlowProjectionManipulation,
            FraudType::ImproperImpairmentReversal,
        ];

        for fraud_type in fraud_types {
            let anomaly_type = AnomalyType::Fraud(fraud_type);
            assert_eq!(anomaly_type.category(), "Fraud");
            assert!(anomaly_type.is_intentional());
            assert!(anomaly_type.severity() >= 3);
        }

        // Cash flow manipulation has highest severity
        assert_eq!(FraudType::CashFlowProjectionManipulation.severity(), 5);
    }

    // ==========================================================================
    // Accounting Standards Error Type Tests
    // ==========================================================================

    #[test]
    fn test_standards_error_types() {
        // Test non-fraudulent accounting standards errors
        let error_types = [
            ErrorType::RevenueTimingError,
            ErrorType::PoAllocationError,
            ErrorType::LeaseClassificationError,
            ErrorType::LeaseCalculationError,
            ErrorType::FairValueError,
            ErrorType::ImpairmentCalculationError,
            ErrorType::DiscountRateError,
            ErrorType::FrameworkApplicationError,
        ];

        for error_type in error_types {
            let anomaly_type = AnomalyType::Error(error_type);
            assert_eq!(anomaly_type.category(), "Error");
            assert!(!anomaly_type.is_intentional());
            assert!(anomaly_type.severity() >= 3);
        }
    }

    #[test]
    fn test_framework_application_error() {
        // Test IFRS vs GAAP confusion errors
        let error_type = ErrorType::FrameworkApplicationError;
        assert_eq!(error_type.severity(), 4);

        let anomaly = LabeledAnomaly::new(
            "ERR001".to_string(),
            AnomalyType::Error(error_type),
            "JE100".to_string(),
            "JE".to_string(),
            "1000".to_string(),
            NaiveDate::from_ymd_opt(2024, 6, 30).unwrap(),
        )
        .with_description("LIFO inventory method used under IFRS (not permitted)")
        .with_metadata("framework", "IFRS")
        .with_metadata("standard_violated", "IAS 2");

        assert_eq!(anomaly.anomaly_type.category(), "Error");
        assert_eq!(
            anomaly.metadata.get("standard_violated"),
            Some(&"IAS 2".to_string())
        );
    }

    #[test]
    fn test_standards_anomaly_serialization() {
        // Test that new fraud types serialize/deserialize correctly
        let fraud_types = [
            FraudType::ImproperRevenueRecognition,
            FraudType::LeaseClassificationManipulation,
            FraudType::FairValueHierarchyManipulation,
            FraudType::DelayedImpairment,
        ];

        for fraud_type in fraud_types {
            let json = serde_json::to_string(&fraud_type).expect("Failed to serialize");
            let deserialized: FraudType =
                serde_json::from_str(&json).expect("Failed to deserialize");
            assert_eq!(fraud_type, deserialized);
        }

        // Test error types
        let error_types = [
            ErrorType::RevenueTimingError,
            ErrorType::LeaseCalculationError,
            ErrorType::FairValueError,
            ErrorType::FrameworkApplicationError,
        ];

        for error_type in error_types {
            let json = serde_json::to_string(&error_type).expect("Failed to serialize");
            let deserialized: ErrorType =
                serde_json::from_str(&json).expect("Failed to deserialize");
            assert_eq!(error_type, deserialized);
        }
    }

    #[test]
    fn test_standards_labeled_anomaly() {
        // Test creating a labeled anomaly for a standards violation
        let anomaly = LabeledAnomaly::new(
            "STD001".to_string(),
            AnomalyType::Fraud(FraudType::ImproperRevenueRecognition),
            "CONTRACT-2024-001".to_string(),
            "Revenue".to_string(),
            "1000".to_string(),
            NaiveDate::from_ymd_opt(2024, 12, 31).unwrap(),
        )
        .with_description("Revenue recognized before performance obligation satisfied (ASC 606)")
        .with_monetary_impact(dec!(500000))
        .with_metadata("standard", "ASC 606")
        .with_metadata("paragraph", "606-10-25-1")
        .with_metadata("contract_id", "C-2024-001")
        .with_related_entity("CONTRACT-2024-001")
        .with_related_entity("CUSTOMER-500");

        assert_eq!(anomaly.severity, 5); // ImproperRevenueRecognition has severity 5
        assert!(anomaly.is_injected);
        assert_eq!(anomaly.monetary_impact, Some(dec!(500000)));
        assert_eq!(anomaly.related_entities.len(), 2);
        assert_eq!(
            anomaly.metadata.get("standard"),
            Some(&"ASC 606".to_string())
        );
    }

    // ==========================================================================
    // Multi-Dimensional Labeling Tests
    // ==========================================================================

    #[test]
    fn test_severity_level() {
        assert_eq!(SeverityLevel::Low.numeric(), 1);
        assert_eq!(SeverityLevel::Critical.numeric(), 4);

        assert_eq!(SeverityLevel::from_numeric(1), SeverityLevel::Low);
        assert_eq!(SeverityLevel::from_numeric(4), SeverityLevel::Critical);

        assert_eq!(SeverityLevel::from_score(0.1), SeverityLevel::Low);
        assert_eq!(SeverityLevel::from_score(0.9), SeverityLevel::Critical);

        assert!((SeverityLevel::Medium.to_score() - 0.375).abs() < 0.01);
    }

    #[test]
    fn test_anomaly_severity() {
        let severity =
            AnomalySeverity::new(SeverityLevel::High, dec!(50000)).with_materiality(dec!(10000));

        assert_eq!(severity.level, SeverityLevel::High);
        assert!(severity.is_material);
        assert_eq!(severity.materiality_threshold, Some(dec!(10000)));

        // Not material
        let low_severity =
            AnomalySeverity::new(SeverityLevel::Low, dec!(5000)).with_materiality(dec!(10000));
        assert!(!low_severity.is_material);
    }

    #[test]
    fn test_detection_difficulty() {
        assert!(
            (AnomalyDetectionDifficulty::Trivial.expected_detection_rate() - 0.99).abs() < 0.01
        );
        assert!((AnomalyDetectionDifficulty::Expert.expected_detection_rate() - 0.15).abs() < 0.01);

        assert_eq!(
            AnomalyDetectionDifficulty::from_score(0.05),
            AnomalyDetectionDifficulty::Trivial
        );
        assert_eq!(
            AnomalyDetectionDifficulty::from_score(0.90),
            AnomalyDetectionDifficulty::Expert
        );

        assert_eq!(AnomalyDetectionDifficulty::Moderate.name(), "moderate");
    }

    #[test]
    fn test_ground_truth_certainty() {
        assert_eq!(GroundTruthCertainty::Definite.certainty_score(), 1.0);
        assert_eq!(GroundTruthCertainty::Probable.certainty_score(), 0.8);
        assert_eq!(GroundTruthCertainty::Possible.certainty_score(), 0.5);
    }

    #[test]
    fn test_detection_method() {
        assert_eq!(DetectionMethod::RuleBased.name(), "rule_based");
        assert_eq!(DetectionMethod::MachineLearning.name(), "machine_learning");
    }

    #[test]
    fn test_extended_anomaly_label() {
        let base = LabeledAnomaly::new(
            "ANO001".to_string(),
            AnomalyType::Fraud(FraudType::FictitiousVendor),
            "JE001".to_string(),
            "JE".to_string(),
            "1000".to_string(),
            NaiveDate::from_ymd_opt(2024, 6, 15).unwrap(),
        )
        .with_monetary_impact(dec!(100000));

        let extended = ExtendedAnomalyLabel::from_base(base)
            .with_severity(AnomalySeverity::new(SeverityLevel::Critical, dec!(100000)))
            .with_difficulty(AnomalyDetectionDifficulty::Hard)
            .with_method(DetectionMethod::GraphBased)
            .with_method(DetectionMethod::ForensicAudit)
            .with_indicator("New vendor with no history")
            .with_indicator("Large first transaction")
            .with_certainty(GroundTruthCertainty::Definite)
            .with_entity("V001")
            .with_secondary_category(AnomalyCategory::BehavioralAnomaly)
            .with_scheme("SCHEME001", 2);

        assert_eq!(extended.severity.level, SeverityLevel::Critical);
        assert_eq!(
            extended.detection_difficulty,
            AnomalyDetectionDifficulty::Hard
        );
        // from_base adds RuleBased, then we add 2 more (GraphBased, ForensicAudit)
        assert_eq!(extended.recommended_methods.len(), 3);
        assert_eq!(extended.key_indicators.len(), 2);
        assert_eq!(extended.scheme_id, Some("SCHEME001".to_string()));
        assert_eq!(extended.scheme_stage, Some(2));
    }

    #[test]
    fn test_extended_anomaly_label_features() {
        let base = LabeledAnomaly::new(
            "ANO001".to_string(),
            AnomalyType::Fraud(FraudType::SelfApproval),
            "JE001".to_string(),
            "JE".to_string(),
            "1000".to_string(),
            NaiveDate::from_ymd_opt(2024, 6, 15).unwrap(),
        );

        let extended =
            ExtendedAnomalyLabel::from_base(base).with_difficulty(AnomalyDetectionDifficulty::Hard);

        let features = extended.to_features();
        assert_eq!(features.len(), ExtendedAnomalyLabel::feature_count());
        assert_eq!(features.len(), 30);

        // Check difficulty score is in features
        let difficulty_idx = 18; // Position of difficulty_score
        assert!((features[difficulty_idx] - 0.75).abs() < 0.01);
    }

    #[test]
    fn test_extended_label_near_miss() {
        let base = LabeledAnomaly::new(
            "ANO001".to_string(),
            AnomalyType::Statistical(StatisticalAnomalyType::UnusuallyHighAmount),
            "JE001".to_string(),
            "JE".to_string(),
            "1000".to_string(),
            NaiveDate::from_ymd_opt(2024, 6, 15).unwrap(),
        );

        let extended = ExtendedAnomalyLabel::from_base(base)
            .as_near_miss("Year-end bonus payment, legitimately high");

        assert!(extended.is_near_miss);
        assert!(extended.near_miss_explanation.is_some());
    }

    #[test]
    fn test_scheme_type() {
        assert_eq!(
            SchemeType::GradualEmbezzlement.name(),
            "gradual_embezzlement"
        );
        assert_eq!(SchemeType::GradualEmbezzlement.typical_stages(), 4);
        assert_eq!(SchemeType::VendorKickback.typical_stages(), 4);
    }

    #[test]
    fn test_concealment_technique() {
        assert!(ConcealmentTechnique::Collusion.difficulty_bonus() > 0.0);
        assert!(
            ConcealmentTechnique::Collusion.difficulty_bonus()
                > ConcealmentTechnique::TimingExploitation.difficulty_bonus()
        );
    }

    #[test]
    fn test_near_miss_label() {
        let near_miss = NearMissLabel::new(
            "JE001",
            NearMissPattern::ThresholdProximity {
                threshold: dec!(10000),
                proximity: 0.95,
            },
            0.7,
            FalsePositiveTrigger::AmountNearThreshold,
            "Transaction is 95% of threshold but business justified",
        );

        assert_eq!(near_miss.document_id, "JE001");
        assert_eq!(near_miss.suspicion_score, 0.7);
        assert_eq!(
            near_miss.false_positive_trigger,
            FalsePositiveTrigger::AmountNearThreshold
        );
    }

    #[test]
    fn test_legitimate_pattern_type() {
        assert_eq!(
            LegitimatePatternType::YearEndBonus.description(),
            "Year-end bonus payment"
        );
        assert_eq!(
            LegitimatePatternType::InsuranceClaim.description(),
            "Insurance claim reimbursement"
        );
    }

    #[test]
    fn test_severity_detection_difficulty_serialization() {
        let severity = AnomalySeverity::new(SeverityLevel::High, dec!(50000));
        let json = serde_json::to_string(&severity).expect("Failed to serialize");
        let deserialized: AnomalySeverity =
            serde_json::from_str(&json).expect("Failed to deserialize");
        assert_eq!(severity.level, deserialized.level);

        let difficulty = AnomalyDetectionDifficulty::Hard;
        let json = serde_json::to_string(&difficulty).expect("Failed to serialize");
        let deserialized: AnomalyDetectionDifficulty =
            serde_json::from_str(&json).expect("Failed to deserialize");
        assert_eq!(difficulty, deserialized);
    }

    // ========================================
    // ACFE Taxonomy Tests
    // ========================================

    #[test]
    fn test_acfe_fraud_category() {
        let asset = AcfeFraudCategory::AssetMisappropriation;
        assert_eq!(asset.name(), "asset_misappropriation");
        assert!((asset.typical_occurrence_rate() - 0.86).abs() < 0.01);
        assert_eq!(asset.typical_median_loss(), Decimal::new(100_000, 0));
        assert_eq!(asset.typical_detection_months(), 12);

        let corruption = AcfeFraudCategory::Corruption;
        assert_eq!(corruption.name(), "corruption");
        assert!((corruption.typical_occurrence_rate() - 0.33).abs() < 0.01);

        let fs_fraud = AcfeFraudCategory::FinancialStatementFraud;
        assert_eq!(fs_fraud.typical_median_loss(), Decimal::new(954_000, 0));
        assert_eq!(fs_fraud.typical_detection_months(), 24);
    }

    #[test]
    fn test_cash_fraud_scheme() {
        let shell = CashFraudScheme::ShellCompany;
        assert_eq!(shell.category(), AcfeFraudCategory::AssetMisappropriation);
        assert_eq!(shell.subcategory(), "billing_schemes");
        assert_eq!(shell.severity(), 5);
        assert_eq!(
            shell.detection_difficulty(),
            AnomalyDetectionDifficulty::Hard
        );

        let ghost = CashFraudScheme::GhostEmployee;
        assert_eq!(ghost.subcategory(), "payroll_schemes");
        assert_eq!(ghost.severity(), 5);

        // Test all variants exist
        assert_eq!(CashFraudScheme::all_variants().len(), 20);
    }

    #[test]
    fn test_asset_fraud_scheme() {
        let ip_theft = AssetFraudScheme::IntellectualPropertyTheft;
        assert_eq!(
            ip_theft.category(),
            AcfeFraudCategory::AssetMisappropriation
        );
        assert_eq!(ip_theft.subcategory(), "other_assets");
        assert_eq!(ip_theft.severity(), 5);

        let inv_theft = AssetFraudScheme::InventoryTheft;
        assert_eq!(inv_theft.subcategory(), "inventory");
        assert_eq!(inv_theft.severity(), 4);
    }

    #[test]
    fn test_corruption_scheme() {
        let kickback = CorruptionScheme::InvoiceKickback;
        assert_eq!(kickback.category(), AcfeFraudCategory::Corruption);
        assert_eq!(kickback.subcategory(), "bribery");
        assert_eq!(kickback.severity(), 5);
        assert_eq!(
            kickback.detection_difficulty(),
            AnomalyDetectionDifficulty::Expert
        );

        let bid_rigging = CorruptionScheme::BidRigging;
        assert_eq!(bid_rigging.subcategory(), "bribery");
        assert_eq!(
            bid_rigging.detection_difficulty(),
            AnomalyDetectionDifficulty::Hard
        );

        let purchasing = CorruptionScheme::PurchasingConflict;
        assert_eq!(purchasing.subcategory(), "conflicts_of_interest");

        // Test all variants exist
        assert_eq!(CorruptionScheme::all_variants().len(), 10);
    }

    #[test]
    fn test_financial_statement_scheme() {
        let fictitious = FinancialStatementScheme::FictitiousRevenues;
        assert_eq!(
            fictitious.category(),
            AcfeFraudCategory::FinancialStatementFraud
        );
        assert_eq!(fictitious.subcategory(), "overstatement");
        assert_eq!(fictitious.severity(), 5);
        assert_eq!(
            fictitious.detection_difficulty(),
            AnomalyDetectionDifficulty::Expert
        );

        let understated = FinancialStatementScheme::UnderstatedRevenues;
        assert_eq!(understated.subcategory(), "understatement");

        // Test all variants exist
        assert_eq!(FinancialStatementScheme::all_variants().len(), 13);
    }

    #[test]
    fn test_acfe_scheme_unified() {
        let cash_scheme = AcfeScheme::Cash(CashFraudScheme::ShellCompany);
        assert_eq!(
            cash_scheme.category(),
            AcfeFraudCategory::AssetMisappropriation
        );
        assert_eq!(cash_scheme.severity(), 5);

        let corruption_scheme = AcfeScheme::Corruption(CorruptionScheme::BidRigging);
        assert_eq!(corruption_scheme.category(), AcfeFraudCategory::Corruption);

        let fs_scheme = AcfeScheme::FinancialStatement(FinancialStatementScheme::PrematureRevenue);
        assert_eq!(
            fs_scheme.category(),
            AcfeFraudCategory::FinancialStatementFraud
        );
    }

    #[test]
    fn test_acfe_detection_method() {
        let tip = AcfeDetectionMethod::Tip;
        assert!((tip.typical_detection_rate() - 0.42).abs() < 0.01);

        let internal_audit = AcfeDetectionMethod::InternalAudit;
        assert!((internal_audit.typical_detection_rate() - 0.16).abs() < 0.01);

        let external_audit = AcfeDetectionMethod::ExternalAudit;
        assert!((external_audit.typical_detection_rate() - 0.04).abs() < 0.01);

        // Test all variants exist
        assert_eq!(AcfeDetectionMethod::all_variants().len(), 12);
    }

    #[test]
    fn test_perpetrator_department() {
        let accounting = PerpetratorDepartment::Accounting;
        assert!((accounting.typical_occurrence_rate() - 0.21).abs() < 0.01);
        assert_eq!(accounting.typical_median_loss(), Decimal::new(130_000, 0));

        let executive = PerpetratorDepartment::Executive;
        assert_eq!(executive.typical_median_loss(), Decimal::new(600_000, 0));
    }

    #[test]
    fn test_perpetrator_level() {
        let employee = PerpetratorLevel::Employee;
        assert!((employee.typical_occurrence_rate() - 0.42).abs() < 0.01);
        assert_eq!(employee.typical_median_loss(), Decimal::new(50_000, 0));

        let exec = PerpetratorLevel::OwnerExecutive;
        assert_eq!(exec.typical_median_loss(), Decimal::new(337_000, 0));
    }

    #[test]
    fn test_acfe_calibration() {
        let cal = AcfeCalibration::default();
        assert_eq!(cal.median_loss, Decimal::new(117_000, 0));
        assert_eq!(cal.median_duration_months, 12);
        assert!((cal.collusion_rate - 0.50).abs() < 0.01);
        assert!(cal.validate().is_ok());

        // Test custom calibration
        let custom_cal = AcfeCalibration::new(Decimal::new(200_000, 0), 18);
        assert_eq!(custom_cal.median_loss, Decimal::new(200_000, 0));
        assert_eq!(custom_cal.median_duration_months, 18);

        // Test validation failure
        let bad_cal = AcfeCalibration {
            collusion_rate: 1.5,
            ..Default::default()
        };
        assert!(bad_cal.validate().is_err());
    }

    #[test]
    fn test_fraud_triangle() {
        let triangle = FraudTriangle::new(
            PressureType::FinancialTargets,
            vec![
                OpportunityFactor::WeakInternalControls,
                OpportunityFactor::ManagementOverride,
            ],
            Rationalization::ForTheCompanyGood,
        );

        // Risk score should be between 0 and 1
        let risk = triangle.risk_score();
        assert!((0.0..=1.0).contains(&risk));
        // Should be relatively high given the components
        assert!(risk > 0.5);
    }

    #[test]
    fn test_pressure_types() {
        let financial = PressureType::FinancialTargets;
        assert!(financial.risk_weight() > 0.5);

        let gambling = PressureType::GamblingAddiction;
        assert_eq!(gambling.risk_weight(), 0.90);
    }

    #[test]
    fn test_opportunity_factors() {
        let override_factor = OpportunityFactor::ManagementOverride;
        assert_eq!(override_factor.risk_weight(), 0.90);

        let weak_controls = OpportunityFactor::WeakInternalControls;
        assert!(weak_controls.risk_weight() > 0.8);
    }

    #[test]
    fn test_rationalizations() {
        let entitlement = Rationalization::Entitlement;
        assert!(entitlement.risk_weight() > 0.8);

        let borrowing = Rationalization::TemporaryBorrowing;
        assert!(borrowing.risk_weight() < entitlement.risk_weight());
    }

    #[test]
    fn test_acfe_scheme_serialization() {
        let scheme = AcfeScheme::Corruption(CorruptionScheme::BidRigging);
        let json = serde_json::to_string(&scheme).expect("Failed to serialize");
        let deserialized: AcfeScheme = serde_json::from_str(&json).expect("Failed to deserialize");
        assert_eq!(scheme, deserialized);

        let calibration = AcfeCalibration::default();
        let json = serde_json::to_string(&calibration).expect("Failed to serialize");
        let deserialized: AcfeCalibration =
            serde_json::from_str(&json).expect("Failed to deserialize");
        assert_eq!(calibration.median_loss, deserialized.median_loss);
    }
}