corp-finance-core 1.1.0

Institutional-grade corporate finance calculations with 128-bit decimal precision — DCF, WACC, comps, LBO, credit metrics, derivatives, fixed income, options, and 60+ specialty modules. No f64 in financials. WASM-compatible.
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
use rust_decimal::Decimal;
use rust_decimal_macros::dec;
use serde::{Deserialize, Serialize};

use crate::CorpFinanceResult;

// ---------------------------------------------------------------------------
// Enums
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum CustomerType {
    Individual,
    Corporate,
    Trust,
    Foundation,
    Partnership,
    PEP,
    ComplexStructure,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum PepCategory {
    DomesticPEP,
    ForeignPEP,
    InternationalOrgPEP,
    FamilyMember,
    CloseAssociate,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum SourceOfWealth {
    Employment,
    Business,
    Inheritance,
    Investment,
    Unclear,
    HighRiskIndustry,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ProductType {
    RetailBanking,
    PrivateBanking,
    CorrespondentBanking,
    TradeFinance,
    FundInvestment,
    CustodyServices,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum Channel {
    FaceToFace,
    Online,
    IntroducedBusiness,
    ThirdParty,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum RiskLevel {
    Low,
    Medium,
    High,
    Critical,
    Prohibited,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum DueDiligenceLevel {
    SDD,
    CDD,
    EDD,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum MonitoringFrequency {
    Monthly,
    Quarterly,
    Annual,
    Triennial,
}

// ---------------------------------------------------------------------------
// Input / Output structs
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KycRiskInput {
    pub customer_name: String,
    pub customer_type: CustomerType,
    pub jurisdiction_of_incorporation: String,
    pub jurisdiction_of_operations: Vec<String>,
    pub is_pep: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pep_category: Option<PepCategory>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub years_since_pep_role: Option<u32>,
    pub source_of_wealth: SourceOfWealth,
    pub source_of_funds: String,
    pub product_type: ProductType,
    pub channel: Channel,
    pub annual_transaction_volume: Decimal,
    pub average_transaction_size: Decimal,
    pub cross_border_transaction_pct: Decimal,
    pub cash_transaction_pct: Decimal,
    pub ownership_layers: u32,
    pub has_nominee_directors: bool,
    pub has_bearer_shares: bool,
    pub adverse_media_hits: u32,
    pub industry: String,
    pub expected_account_activity: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KycRiskOutput {
    pub overall_risk_score: Decimal,
    pub risk_level: RiskLevel,
    pub risk_breakdown: RiskBreakdown,
    pub due_diligence_level: DueDiligenceLevel,
    pub pep_assessment: Option<PepAssessment>,
    pub red_flags: Vec<RedFlag>,
    pub monitoring_frequency: MonitoringFrequency,
    pub documentation_required: Vec<String>,
    pub recommended_actions: Vec<String>,
    pub jurisdiction_risk_details: Vec<JurisdictionRisk>,
    pub methodology: String,
    pub assumptions: Vec<String>,
    pub warnings: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RiskBreakdown {
    pub customer_score: Decimal,
    pub geographic_score: Decimal,
    pub product_score: Decimal,
    pub transaction_score: Decimal,
    pub source_of_wealth_score: Decimal,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PepAssessment {
    pub category: PepCategory,
    pub de_pep_eligible: bool,
    pub edd_required: bool,
    pub senior_approval_required: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RedFlag {
    pub category: String,
    pub description: String,
    pub severity: RiskLevel,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JurisdictionRisk {
    pub jurisdiction: String,
    pub risk_level: RiskLevel,
    pub fatf_status: String,
    pub eu_list_status: String,
}

// ---------------------------------------------------------------------------
// FATF / jurisdiction risk tables
// ---------------------------------------------------------------------------

/// Countries on the FATF black list (high-risk, call for action).
const FATF_BLACK_LIST: &[&str] = &["north korea", "dprk", "iran", "myanmar"];

/// Countries on the FATF grey list (increased monitoring).
const FATF_GREY_LIST: &[&str] = &[
    "albania",
    "barbados",
    "burkina faso",
    "cameroon",
    "cayman islands",
    "croatia",
    "democratic republic of the congo",
    "gibraltar",
    "haiti",
    "jamaica",
    "jordan",
    "mali",
    "mozambique",
    "nigeria",
    "panama",
    "philippines",
    "senegal",
    "south africa",
    "south sudan",
    "syria",
    "tanzania",
    "turkey",
    "uganda",
    "united arab emirates",
    "vietnam",
    "yemen",
];

/// Comprehensive sanctions jurisdictions (full embargoes).
const SANCTIONED_COUNTRIES: &[&str] = &["north korea", "dprk", "iran", "cuba", "syria", "crimea"];

/// EU high-risk third country list (representative subset).
const EU_HIGH_RISK: &[&str] = &[
    "afghanistan",
    "barbados",
    "burkina faso",
    "cambodia",
    "cayman islands",
    "democratic republic of the congo",
    "gibraltar",
    "haiti",
    "jamaica",
    "jordan",
    "mali",
    "morocco",
    "mozambique",
    "myanmar",
    "nicaragua",
    "nigeria",
    "pakistan",
    "panama",
    "philippines",
    "senegal",
    "south sudan",
    "syria",
    "tanzania",
    "trinidad and tobago",
    "turkey",
    "uganda",
    "united arab emirates",
    "vietnam",
    "yemen",
];

/// High-risk industries from an AML perspective.
const HIGH_RISK_INDUSTRIES: &[&str] = &[
    "gambling",
    "casino",
    "cryptocurrency",
    "crypto",
    "money service",
    "msb",
    "precious metals",
    "arms",
    "weapons",
    "tobacco",
    "marijuana",
    "cannabis",
    "adult entertainment",
    "virtual assets",
];

// ---------------------------------------------------------------------------
// Helper functions
// ---------------------------------------------------------------------------

fn normalize_jurisdiction(j: &str) -> String {
    j.trim().to_lowercase()
}

fn is_fatf_black_list(jurisdiction: &str) -> bool {
    let j = normalize_jurisdiction(jurisdiction);
    FATF_BLACK_LIST.iter().any(|&c| j.contains(c))
}

fn is_fatf_grey_list(jurisdiction: &str) -> bool {
    let j = normalize_jurisdiction(jurisdiction);
    FATF_GREY_LIST.iter().any(|&c| j.contains(c))
}

fn is_sanctioned(jurisdiction: &str) -> bool {
    let j = normalize_jurisdiction(jurisdiction);
    SANCTIONED_COUNTRIES.iter().any(|&c| j.contains(c))
}

fn is_eu_high_risk(jurisdiction: &str) -> bool {
    let j = normalize_jurisdiction(jurisdiction);
    EU_HIGH_RISK.iter().any(|&c| j.contains(c))
}

fn is_high_risk_industry(industry: &str) -> bool {
    let i = industry.trim().to_lowercase();
    HIGH_RISK_INDUSTRIES.iter().any(|&hi| i.contains(hi))
}

fn classify_jurisdiction(jurisdiction: &str) -> JurisdictionRisk {
    let j_lower = normalize_jurisdiction(jurisdiction);

    if is_sanctioned(&j_lower) {
        JurisdictionRisk {
            jurisdiction: jurisdiction.to_string(),
            risk_level: RiskLevel::Prohibited,
            fatf_status: "Sanctioned / Embargoed".to_string(),
            eu_list_status: if is_eu_high_risk(&j_lower) {
                "EU high-risk third country".to_string()
            } else {
                "N/A".to_string()
            },
        }
    } else if is_fatf_black_list(&j_lower) {
        JurisdictionRisk {
            jurisdiction: jurisdiction.to_string(),
            risk_level: RiskLevel::Critical,
            fatf_status: "FATF Black List".to_string(),
            eu_list_status: if is_eu_high_risk(&j_lower) {
                "EU high-risk third country".to_string()
            } else {
                "N/A".to_string()
            },
        }
    } else if is_fatf_grey_list(&j_lower) {
        JurisdictionRisk {
            jurisdiction: jurisdiction.to_string(),
            risk_level: RiskLevel::High,
            fatf_status: "FATF Grey List".to_string(),
            eu_list_status: if is_eu_high_risk(&j_lower) {
                "EU high-risk third country".to_string()
            } else {
                "N/A".to_string()
            },
        }
    } else if is_eu_high_risk(&j_lower) {
        JurisdictionRisk {
            jurisdiction: jurisdiction.to_string(),
            risk_level: RiskLevel::High,
            fatf_status: "Not listed".to_string(),
            eu_list_status: "EU high-risk third country".to_string(),
        }
    } else {
        JurisdictionRisk {
            jurisdiction: jurisdiction.to_string(),
            risk_level: RiskLevel::Low,
            fatf_status: "Not listed".to_string(),
            eu_list_status: "Not listed".to_string(),
        }
    }
}

// ---------------------------------------------------------------------------
// Scoring functions
// ---------------------------------------------------------------------------

/// Customer type risk score (max 25).
fn score_customer_type(input: &KycRiskInput) -> Decimal {
    let base = match input.customer_type {
        CustomerType::Individual => dec!(5),
        CustomerType::Corporate => dec!(10),
        CustomerType::Partnership => dec!(10),
        CustomerType::Trust => dec!(15),
        CustomerType::Foundation => dec!(15),
        CustomerType::ComplexStructure => dec!(20),
        CustomerType::PEP => dec!(25),
    };
    // If the customer is flagged as PEP but type is not PEP, bump to 25
    if input.is_pep && base < dec!(25) {
        dec!(25)
    } else {
        base
    }
}

/// Geographic risk score (max 25).
/// Takes highest risk across all jurisdictions.
fn score_geographic(input: &KycRiskInput) -> Decimal {
    let mut all_jurisdictions = vec![input.jurisdiction_of_incorporation.clone()];
    all_jurisdictions.extend(input.jurisdiction_of_operations.clone());

    let mut max_score = dec!(5); // low-risk default

    for j in &all_jurisdictions {
        let score = if is_sanctioned(j) || is_fatf_black_list(j) {
            dec!(25)
        } else if is_fatf_grey_list(j) {
            dec!(20)
        } else if is_eu_high_risk(j) {
            dec!(18)
        } else {
            dec!(5)
        };
        if score > max_score {
            max_score = score;
        }
    }

    max_score
}

/// Product risk score (max 20).
fn score_product(input: &KycRiskInput) -> Decimal {
    let base = match input.product_type {
        ProductType::RetailBanking => dec!(5),
        ProductType::CustodyServices => dec!(7),
        ProductType::FundInvestment => dec!(8),
        ProductType::PrivateBanking => dec!(10),
        ProductType::CorrespondentBanking => dec!(15),
        ProductType::TradeFinance => dec!(20),
    };

    // Channel adjustment: add up to 5 for high-risk channels, cap at 20
    let channel_adj = match input.channel {
        Channel::FaceToFace => dec!(0),
        Channel::Online => dec!(3),
        Channel::IntroducedBusiness => dec!(2),
        Channel::ThirdParty => dec!(4),
    };

    let total = base + channel_adj;
    if total > dec!(20) {
        dec!(20)
    } else {
        total
    }
}

/// Transaction pattern risk score (max 15).
fn score_transaction(input: &KycRiskInput) -> Decimal {
    let mut score = dec!(3); // baseline normal

    // High cross-border percentage
    if input.cross_border_transaction_pct > dec!(50) {
        score += dec!(3);
    }
    if input.cross_border_transaction_pct > dec!(80) {
        score += dec!(2);
    }

    // High cash percentage (structuring indicator)
    if input.cash_transaction_pct > dec!(30) {
        score += dec!(3);
    }
    if input.cash_transaction_pct > dec!(60) {
        score += dec!(2);
    }

    // Very high transaction volume relative to size
    // (threshold: >10M annual volume is elevated)
    if input.annual_transaction_volume > dec!(10_000_000) {
        score += dec!(2);
    }

    // Cap at 15
    if score > dec!(15) {
        dec!(15)
    } else {
        score
    }
}

/// Source of wealth risk score (max 15).
fn score_source_of_wealth(input: &KycRiskInput) -> Decimal {
    let base = match input.source_of_wealth {
        SourceOfWealth::Employment => dec!(3),
        SourceOfWealth::Investment => dec!(5),
        SourceOfWealth::Business => dec!(6),
        SourceOfWealth::Inheritance => dec!(8),
        SourceOfWealth::Unclear => dec!(12),
        SourceOfWealth::HighRiskIndustry => dec!(15),
    };

    // Additional risk for high-risk industry description
    let industry_adj = if is_high_risk_industry(&input.industry) {
        dec!(3)
    } else {
        dec!(0)
    };

    let total = base + industry_adj;
    if total > dec!(15) {
        dec!(15)
    } else {
        total
    }
}

// ---------------------------------------------------------------------------
// Red flag detection
// ---------------------------------------------------------------------------

fn detect_red_flags(input: &KycRiskInput) -> Vec<RedFlag> {
    let mut flags = Vec::new();

    // Shell company indicators
    if input.has_nominee_directors {
        flags.push(RedFlag {
            category: "Shell Company".to_string(),
            description: "Nominee directors present — potential shell company indicator"
                .to_string(),
            severity: RiskLevel::High,
        });
    }
    if input.has_bearer_shares {
        flags.push(RedFlag {
            category: "Shell Company".to_string(),
            description: "Bearer shares present — ownership concealment risk".to_string(),
            severity: RiskLevel::Critical,
        });
    }

    // Complex ownership
    if input.ownership_layers > 3 {
        flags.push(RedFlag {
            category: "Ownership Complexity".to_string(),
            description: format!(
                "{} ownership layers — complex structure obscuring UBO",
                input.ownership_layers
            ),
            severity: if input.ownership_layers > 5 {
                RiskLevel::Critical
            } else {
                RiskLevel::High
            },
        });
    }

    // Adverse media
    if input.adverse_media_hits > 0 {
        let severity = if input.adverse_media_hits >= 5 {
            RiskLevel::Critical
        } else if input.adverse_media_hits >= 2 {
            RiskLevel::High
        } else {
            RiskLevel::Medium
        };
        flags.push(RedFlag {
            category: "Adverse Media".to_string(),
            description: format!(
                "{} adverse media hit(s) identified",
                input.adverse_media_hits
            ),
            severity,
        });
    }

    // High cash transaction percentage
    if input.cash_transaction_pct > dec!(50) {
        flags.push(RedFlag {
            category: "Transaction Pattern".to_string(),
            description: format!(
                "Cash transactions at {}% — significantly above normal levels",
                input.cash_transaction_pct
            ),
            severity: RiskLevel::High,
        });
    }

    // Structuring indicator: high volume with small avg transaction
    if input.annual_transaction_volume > dec!(1_000_000)
        && input.average_transaction_size < dec!(10_000)
        && input.average_transaction_size > Decimal::ZERO
    {
        let implied_count = input.annual_transaction_volume / input.average_transaction_size;
        if implied_count > dec!(500) {
            flags.push(RedFlag {
                category: "Transaction Pattern".to_string(),
                description: "High volume of small transactions — potential structuring indicator"
                    .to_string(),
                severity: RiskLevel::High,
            });
        }
    }

    // PEP with unclear source of wealth
    if input.is_pep && input.source_of_wealth == SourceOfWealth::Unclear {
        flags.push(RedFlag {
            category: "PEP Risk".to_string(),
            description: "Politically Exposed Person with unclear source of wealth".to_string(),
            severity: RiskLevel::Critical,
        });
    }

    // Jurisdiction mismatch
    if !input.jurisdiction_of_operations.is_empty() {
        let incorp = normalize_jurisdiction(&input.jurisdiction_of_incorporation);
        let has_mismatch = input
            .jurisdiction_of_operations
            .iter()
            .any(|j| normalize_jurisdiction(j) != incorp);
        if has_mismatch {
            flags.push(RedFlag {
                category: "Jurisdiction Mismatch".to_string(),
                description: "Incorporation jurisdiction differs from operational jurisdictions"
                    .to_string(),
                severity: RiskLevel::Medium,
            });
        }
    }

    // High-risk industry
    if is_high_risk_industry(&input.industry) {
        flags.push(RedFlag {
            category: "Industry Risk".to_string(),
            description: format!(
                "Industry '{}' is classified as high-risk for AML purposes",
                input.industry
            ),
            severity: RiskLevel::High,
        });
    }

    // Sanctioned jurisdiction operations
    for j in &input.jurisdiction_of_operations {
        if is_sanctioned(j) {
            flags.push(RedFlag {
                category: "Sanctions".to_string(),
                description: format!("Operations in sanctioned jurisdiction: {}", j),
                severity: RiskLevel::Critical,
            });
        }
    }
    if is_sanctioned(&input.jurisdiction_of_incorporation) {
        flags.push(RedFlag {
            category: "Sanctions".to_string(),
            description: format!(
                "Incorporated in sanctioned jurisdiction: {}",
                input.jurisdiction_of_incorporation
            ),
            severity: RiskLevel::Critical,
        });
    }

    flags
}

// ---------------------------------------------------------------------------
// Due diligence & monitoring
// ---------------------------------------------------------------------------

fn determine_due_diligence(
    risk_score: Decimal,
    risk_level: &RiskLevel,
    is_pep: bool,
) -> DueDiligenceLevel {
    if is_pep {
        return DueDiligenceLevel::EDD;
    }
    match risk_level {
        RiskLevel::Prohibited | RiskLevel::Critical => DueDiligenceLevel::EDD,
        RiskLevel::High => DueDiligenceLevel::EDD,
        RiskLevel::Medium => DueDiligenceLevel::CDD,
        RiskLevel::Low => {
            if risk_score <= dec!(25) {
                DueDiligenceLevel::SDD
            } else {
                DueDiligenceLevel::CDD
            }
        }
    }
}

fn determine_monitoring_frequency(risk_level: &RiskLevel, is_pep: bool) -> MonitoringFrequency {
    if is_pep {
        return MonitoringFrequency::Monthly;
    }
    match risk_level {
        RiskLevel::Prohibited | RiskLevel::Critical => MonitoringFrequency::Monthly,
        RiskLevel::High => MonitoringFrequency::Quarterly,
        RiskLevel::Medium => MonitoringFrequency::Annual,
        RiskLevel::Low => MonitoringFrequency::Triennial,
    }
}

fn determine_risk_level(score: Decimal) -> RiskLevel {
    if score >= dec!(85) {
        RiskLevel::Prohibited
    } else if score >= dec!(70) {
        RiskLevel::Critical
    } else if score >= dec!(50) {
        RiskLevel::High
    } else if score >= dec!(30) {
        RiskLevel::Medium
    } else {
        RiskLevel::Low
    }
}

fn build_documentation_required(dd_level: &DueDiligenceLevel, input: &KycRiskInput) -> Vec<String> {
    let mut docs = vec![
        "Government-issued photo ID".to_string(),
        "Proof of address (utility bill or bank statement)".to_string(),
    ];

    match dd_level {
        DueDiligenceLevel::SDD => {
            // minimal extras
        }
        DueDiligenceLevel::CDD => {
            docs.push("Source of funds declaration".to_string());
            if matches!(
                input.customer_type,
                CustomerType::Corporate | CustomerType::Partnership
            ) {
                docs.push("Certificate of incorporation".to_string());
                docs.push("Register of directors and shareholders".to_string());
            }
        }
        DueDiligenceLevel::EDD => {
            docs.push("Source of funds declaration".to_string());
            docs.push("Source of wealth evidence".to_string());
            docs.push("Enhanced background check report".to_string());
            if input.is_pep {
                docs.push("Senior management sign-off".to_string());
                docs.push("PEP screening report".to_string());
            }
            if matches!(
                input.customer_type,
                CustomerType::Corporate
                    | CustomerType::Partnership
                    | CustomerType::Trust
                    | CustomerType::Foundation
                    | CustomerType::ComplexStructure
            ) {
                docs.push("Certificate of incorporation".to_string());
                docs.push("Register of directors and shareholders".to_string());
                docs.push("Full ownership chain / UBO declaration".to_string());
                docs.push("Audited financial statements".to_string());
            }
            if input.ownership_layers > 3 {
                docs.push("Organisational chart with all ownership layers".to_string());
            }
        }
    }

    docs
}

fn build_recommended_actions(
    risk_level: &RiskLevel,
    dd_level: &DueDiligenceLevel,
    input: &KycRiskInput,
    red_flags: &[RedFlag],
) -> Vec<String> {
    let mut actions = Vec::new();

    match risk_level {
        RiskLevel::Prohibited => {
            actions.push(
                "BLOCK: Do not onboard — sanctioned or prohibited jurisdiction/entity".to_string(),
            );
            actions.push("File SAR immediately if attempted onboarding".to_string());
        }
        RiskLevel::Critical => {
            actions.push("Escalate to MLRO for senior management decision".to_string());
            actions.push("Conduct full Enhanced Due Diligence before proceeding".to_string());
        }
        RiskLevel::High => {
            actions.push("Conduct Enhanced Due Diligence before onboarding".to_string());
            actions.push("Assign dedicated compliance officer for monitoring".to_string());
        }
        RiskLevel::Medium => {
            actions.push("Complete standard Customer Due Diligence".to_string());
        }
        RiskLevel::Low => {
            actions.push("Proceed with Simplified Due Diligence".to_string());
        }
    }

    if input.is_pep {
        actions.push("Obtain senior management approval for PEP relationship".to_string());
        actions.push("Verify source of wealth independently".to_string());
    }

    if !red_flags.is_empty() {
        let critical_count = red_flags
            .iter()
            .filter(|f| matches!(f.severity, RiskLevel::Critical))
            .count();
        if critical_count > 0 {
            actions.push(format!(
                "Investigate {} critical red flag(s) before proceeding",
                critical_count
            ));
        }
    }

    if matches!(dd_level, DueDiligenceLevel::EDD) && input.ownership_layers > 3 {
        actions.push("Map full beneficial ownership chain through all layers".to_string());
    }

    actions
}

// ---------------------------------------------------------------------------
// PEP assessment
// ---------------------------------------------------------------------------

fn assess_pep(input: &KycRiskInput) -> Option<PepAssessment> {
    if !input.is_pep {
        return None;
    }

    let category = input
        .pep_category
        .clone()
        .unwrap_or(PepCategory::ForeignPEP);

    let de_pep_eligible = match input.years_since_pep_role {
        Some(years) => years >= 24,
        None => false,
    };

    let edd_required = true; // always for PEPs

    let senior_approval_required = matches!(
        category,
        PepCategory::DomesticPEP | PepCategory::ForeignPEP | PepCategory::InternationalOrgPEP
    );

    Some(PepAssessment {
        category,
        de_pep_eligible,
        edd_required,
        senior_approval_required,
    })
}

// ---------------------------------------------------------------------------
// Main public function
// ---------------------------------------------------------------------------

/// Assess KYC/AML risk for a customer using FATF-based methodology.
///
/// Returns a comprehensive risk assessment with scores, due diligence
/// requirements, red flags, and recommended actions.
pub fn assess_kyc_risk(input: &KycRiskInput) -> CorpFinanceResult<KycRiskOutput> {
    // Input validation
    if input.customer_name.trim().is_empty() {
        return Err(crate::CorpFinanceError::InvalidInput {
            field: "customer_name".to_string(),
            reason: "Customer name must not be empty".to_string(),
        });
    }
    if input.jurisdiction_of_incorporation.trim().is_empty() {
        return Err(crate::CorpFinanceError::InvalidInput {
            field: "jurisdiction_of_incorporation".to_string(),
            reason: "Jurisdiction of incorporation must not be empty".to_string(),
        });
    }
    if input.cross_border_transaction_pct < Decimal::ZERO
        || input.cross_border_transaction_pct > dec!(100)
    {
        return Err(crate::CorpFinanceError::InvalidInput {
            field: "cross_border_transaction_pct".to_string(),
            reason: "Must be between 0 and 100".to_string(),
        });
    }
    if input.cash_transaction_pct < Decimal::ZERO || input.cash_transaction_pct > dec!(100) {
        return Err(crate::CorpFinanceError::InvalidInput {
            field: "cash_transaction_pct".to_string(),
            reason: "Must be between 0 and 100".to_string(),
        });
    }
    if input.annual_transaction_volume < Decimal::ZERO {
        return Err(crate::CorpFinanceError::InvalidInput {
            field: "annual_transaction_volume".to_string(),
            reason: "Must be non-negative".to_string(),
        });
    }

    // Score each risk dimension
    let customer_score = score_customer_type(input);
    let geographic_score = score_geographic(input);
    let product_score = score_product(input);
    let transaction_score = score_transaction(input);
    let source_of_wealth_score = score_source_of_wealth(input);

    let overall_risk_score = customer_score
        + geographic_score
        + product_score
        + transaction_score
        + source_of_wealth_score;

    // Cap at 100
    let overall_risk_score = if overall_risk_score > dec!(100) {
        dec!(100)
    } else {
        overall_risk_score
    };

    let risk_level = determine_risk_level(overall_risk_score);
    let due_diligence_level =
        determine_due_diligence(overall_risk_score, &risk_level, input.is_pep);
    let monitoring_frequency = determine_monitoring_frequency(&risk_level, input.is_pep);

    let red_flags = detect_red_flags(input);
    let pep_assessment = assess_pep(input);

    // Build jurisdiction risk details
    let mut jurisdiction_risk_details = Vec::new();
    jurisdiction_risk_details.push(classify_jurisdiction(&input.jurisdiction_of_incorporation));
    for j in &input.jurisdiction_of_operations {
        jurisdiction_risk_details.push(classify_jurisdiction(j));
    }

    let documentation_required = build_documentation_required(&due_diligence_level, input);
    let recommended_actions =
        build_recommended_actions(&risk_level, &due_diligence_level, input, &red_flags);

    let risk_breakdown = RiskBreakdown {
        customer_score,
        geographic_score,
        product_score,
        transaction_score,
        source_of_wealth_score,
    };

    let mut assumptions = vec![
        "Risk scoring follows FATF risk-based approach methodology".to_string(),
        "Jurisdiction lists based on FATF mutual evaluations as of assessment date".to_string(),
        "PEP de-classification period set at 24 months per conservative standard".to_string(),
    ];

    let mut warnings = Vec::new();

    // Additional warnings
    if matches!(risk_level, RiskLevel::Prohibited) {
        warnings.push(
            "Customer or jurisdiction is subject to comprehensive sanctions — onboarding prohibited"
                .to_string(),
        );
    }
    if input.is_pep {
        assumptions
            .push("PEP status verified against domestic and international databases".to_string());
    }
    if input.adverse_media_hits > 0 {
        warnings.push(format!(
            "{} adverse media hit(s) require manual review",
            input.adverse_media_hits
        ));
    }

    Ok(KycRiskOutput {
        overall_risk_score,
        risk_level,
        risk_breakdown,
        due_diligence_level,
        pep_assessment,
        red_flags,
        monitoring_frequency,
        documentation_required,
        recommended_actions,
        jurisdiction_risk_details,
        methodology: "FATF Risk-Based Approach — customer, geographic, product, transaction, and source of wealth scoring (0-100)".to_string(),
        assumptions,
        warnings,
    })
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    fn base_input() -> KycRiskInput {
        KycRiskInput {
            customer_name: "Test Customer".to_string(),
            customer_type: CustomerType::Individual,
            jurisdiction_of_incorporation: "United Kingdom".to_string(),
            jurisdiction_of_operations: vec!["United Kingdom".to_string()],
            is_pep: false,
            pep_category: None,
            years_since_pep_role: None,
            source_of_wealth: SourceOfWealth::Employment,
            source_of_funds: "Salary".to_string(),
            product_type: ProductType::RetailBanking,
            channel: Channel::FaceToFace,
            annual_transaction_volume: dec!(50_000),
            average_transaction_size: dec!(500),
            cross_border_transaction_pct: dec!(5),
            cash_transaction_pct: dec!(2),
            ownership_layers: 1,
            has_nominee_directors: false,
            has_bearer_shares: false,
            adverse_media_hits: 0,
            industry: "Technology".to_string(),
            expected_account_activity: "Standard salary deposits and personal spending".to_string(),
        }
    }

    // === Customer type tests ===

    #[test]
    fn test_individual_low_risk() {
        let input = base_input();
        let result = assess_kyc_risk(&input).unwrap();
        assert_eq!(result.risk_level, RiskLevel::Low);
        assert!(result.overall_risk_score <= dec!(30));
        assert_eq!(result.due_diligence_level, DueDiligenceLevel::SDD);
        assert_eq!(result.monitoring_frequency, MonitoringFrequency::Triennial);
    }

    #[test]
    fn test_corporate_customer() {
        let mut input = base_input();
        input.customer_type = CustomerType::Corporate;
        let result = assess_kyc_risk(&input).unwrap();
        assert_eq!(result.risk_breakdown.customer_score, dec!(10));
    }

    #[test]
    fn test_trust_customer() {
        let mut input = base_input();
        input.customer_type = CustomerType::Trust;
        let result = assess_kyc_risk(&input).unwrap();
        assert_eq!(result.risk_breakdown.customer_score, dec!(15));
    }

    #[test]
    fn test_foundation_customer() {
        let mut input = base_input();
        input.customer_type = CustomerType::Foundation;
        let result = assess_kyc_risk(&input).unwrap();
        assert_eq!(result.risk_breakdown.customer_score, dec!(15));
    }

    #[test]
    fn test_partnership_customer() {
        let mut input = base_input();
        input.customer_type = CustomerType::Partnership;
        let result = assess_kyc_risk(&input).unwrap();
        assert_eq!(result.risk_breakdown.customer_score, dec!(10));
    }

    #[test]
    fn test_complex_structure_customer() {
        let mut input = base_input();
        input.customer_type = CustomerType::ComplexStructure;
        let result = assess_kyc_risk(&input).unwrap();
        assert_eq!(result.risk_breakdown.customer_score, dec!(20));
    }

    #[test]
    fn test_pep_customer_type() {
        let mut input = base_input();
        input.customer_type = CustomerType::PEP;
        input.is_pep = true;
        input.pep_category = Some(PepCategory::DomesticPEP);
        let result = assess_kyc_risk(&input).unwrap();
        assert_eq!(result.risk_breakdown.customer_score, dec!(25));
        assert!(result.pep_assessment.is_some());
    }

    // === PEP scenario tests ===

    #[test]
    fn test_domestic_pep() {
        let mut input = base_input();
        input.is_pep = true;
        input.pep_category = Some(PepCategory::DomesticPEP);
        let result = assess_kyc_risk(&input).unwrap();
        let pep = result.pep_assessment.unwrap();
        assert_eq!(pep.category, PepCategory::DomesticPEP);
        assert!(pep.edd_required);
        assert!(pep.senior_approval_required);
        assert!(!pep.de_pep_eligible);
        assert_eq!(result.due_diligence_level, DueDiligenceLevel::EDD);
        assert_eq!(result.monitoring_frequency, MonitoringFrequency::Monthly);
    }

    #[test]
    fn test_foreign_pep() {
        let mut input = base_input();
        input.is_pep = true;
        input.pep_category = Some(PepCategory::ForeignPEP);
        let result = assess_kyc_risk(&input).unwrap();
        let pep = result.pep_assessment.unwrap();
        assert_eq!(pep.category, PepCategory::ForeignPEP);
        assert!(pep.senior_approval_required);
    }

    #[test]
    fn test_international_org_pep() {
        let mut input = base_input();
        input.is_pep = true;
        input.pep_category = Some(PepCategory::InternationalOrgPEP);
        let result = assess_kyc_risk(&input).unwrap();
        let pep = result.pep_assessment.unwrap();
        assert_eq!(pep.category, PepCategory::InternationalOrgPEP);
        assert!(pep.senior_approval_required);
    }

    #[test]
    fn test_pep_family_member() {
        let mut input = base_input();
        input.is_pep = true;
        input.pep_category = Some(PepCategory::FamilyMember);
        let result = assess_kyc_risk(&input).unwrap();
        let pep = result.pep_assessment.unwrap();
        assert_eq!(pep.category, PepCategory::FamilyMember);
        assert!(!pep.senior_approval_required);
        assert!(pep.edd_required);
    }

    #[test]
    fn test_pep_close_associate() {
        let mut input = base_input();
        input.is_pep = true;
        input.pep_category = Some(PepCategory::CloseAssociate);
        let result = assess_kyc_risk(&input).unwrap();
        let pep = result.pep_assessment.unwrap();
        assert_eq!(pep.category, PepCategory::CloseAssociate);
        assert!(!pep.senior_approval_required);
    }

    #[test]
    fn test_de_pep_eligible() {
        let mut input = base_input();
        input.is_pep = true;
        input.pep_category = Some(PepCategory::DomesticPEP);
        input.years_since_pep_role = Some(25);
        let result = assess_kyc_risk(&input).unwrap();
        let pep = result.pep_assessment.unwrap();
        assert!(pep.de_pep_eligible);
    }

    #[test]
    fn test_de_pep_not_eligible_too_recent() {
        let mut input = base_input();
        input.is_pep = true;
        input.pep_category = Some(PepCategory::DomesticPEP);
        input.years_since_pep_role = Some(12);
        let result = assess_kyc_risk(&input).unwrap();
        let pep = result.pep_assessment.unwrap();
        assert!(!pep.de_pep_eligible);
    }

    #[test]
    fn test_pep_default_category_when_none() {
        let mut input = base_input();
        input.is_pep = true;
        input.pep_category = None;
        let result = assess_kyc_risk(&input).unwrap();
        let pep = result.pep_assessment.unwrap();
        assert_eq!(pep.category, PepCategory::ForeignPEP);
    }

    #[test]
    fn test_non_pep_type_but_is_pep_flag() {
        let mut input = base_input();
        input.customer_type = CustomerType::Individual;
        input.is_pep = true;
        input.pep_category = Some(PepCategory::DomesticPEP);
        let result = assess_kyc_risk(&input).unwrap();
        // Customer score should be bumped to 25
        assert_eq!(result.risk_breakdown.customer_score, dec!(25));
    }

    // === Geographic risk tests ===

    #[test]
    fn test_low_risk_jurisdiction() {
        let input = base_input(); // UK
        let result = assess_kyc_risk(&input).unwrap();
        assert_eq!(result.risk_breakdown.geographic_score, dec!(5));
    }

    #[test]
    fn test_fatf_grey_list_jurisdiction() {
        let mut input = base_input();
        input.jurisdiction_of_incorporation = "Turkey".to_string();
        let result = assess_kyc_risk(&input).unwrap();
        assert_eq!(result.risk_breakdown.geographic_score, dec!(20));
    }

    #[test]
    fn test_fatf_black_list_jurisdiction() {
        let mut input = base_input();
        input.jurisdiction_of_incorporation = "Iran".to_string();
        let result = assess_kyc_risk(&input).unwrap();
        assert_eq!(result.risk_breakdown.geographic_score, dec!(25));
    }

    #[test]
    fn test_sanctioned_jurisdiction() {
        let mut input = base_input();
        input.jurisdiction_of_incorporation = "North Korea".to_string();
        let result = assess_kyc_risk(&input).unwrap();
        assert_eq!(result.risk_breakdown.geographic_score, dec!(25));
    }

    #[test]
    fn test_eu_high_risk_jurisdiction() {
        let mut input = base_input();
        input.jurisdiction_of_incorporation = "Cambodia".to_string();
        input.jurisdiction_of_operations = vec!["Cambodia".to_string()];
        let result = assess_kyc_risk(&input).unwrap();
        assert_eq!(result.risk_breakdown.geographic_score, dec!(18));
    }

    #[test]
    fn test_multiple_jurisdictions_highest_risk() {
        let mut input = base_input();
        input.jurisdiction_of_incorporation = "United States".to_string();
        input.jurisdiction_of_operations = vec!["United States".to_string(), "Iran".to_string()];
        let result = assess_kyc_risk(&input).unwrap();
        // Should use highest risk (Iran = 25)
        assert_eq!(result.risk_breakdown.geographic_score, dec!(25));
    }

    #[test]
    fn test_jurisdiction_risk_details_populated() {
        let mut input = base_input();
        input.jurisdiction_of_operations = vec!["Germany".to_string(), "Turkey".to_string()];
        let result = assess_kyc_risk(&input).unwrap();
        // Incorporation + 2 operations = 3 entries
        assert_eq!(result.jurisdiction_risk_details.len(), 3);
    }

    #[test]
    fn test_sanctioned_jurisdiction_prohibited() {
        let mut input = base_input();
        input.jurisdiction_of_incorporation = "Cuba".to_string();
        let result = assess_kyc_risk(&input).unwrap();
        let detail = &result.jurisdiction_risk_details[0];
        assert_eq!(detail.risk_level, RiskLevel::Prohibited);
    }

    // === Product risk tests ===

    #[test]
    fn test_retail_banking_low_product_risk() {
        let input = base_input();
        let result = assess_kyc_risk(&input).unwrap();
        assert_eq!(result.risk_breakdown.product_score, dec!(5));
    }

    #[test]
    fn test_private_banking_product_risk() {
        let mut input = base_input();
        input.product_type = ProductType::PrivateBanking;
        let result = assess_kyc_risk(&input).unwrap();
        assert_eq!(result.risk_breakdown.product_score, dec!(10));
    }

    #[test]
    fn test_correspondent_banking_product_risk() {
        let mut input = base_input();
        input.product_type = ProductType::CorrespondentBanking;
        let result = assess_kyc_risk(&input).unwrap();
        assert_eq!(result.risk_breakdown.product_score, dec!(15));
    }

    #[test]
    fn test_trade_finance_product_risk() {
        let mut input = base_input();
        input.product_type = ProductType::TradeFinance;
        let result = assess_kyc_risk(&input).unwrap();
        assert_eq!(result.risk_breakdown.product_score, dec!(20));
    }

    #[test]
    fn test_fund_investment_product_risk() {
        let mut input = base_input();
        input.product_type = ProductType::FundInvestment;
        let result = assess_kyc_risk(&input).unwrap();
        assert_eq!(result.risk_breakdown.product_score, dec!(8));
    }

    #[test]
    fn test_custody_services_product_risk() {
        let mut input = base_input();
        input.product_type = ProductType::CustodyServices;
        let result = assess_kyc_risk(&input).unwrap();
        assert_eq!(result.risk_breakdown.product_score, dec!(7));
    }

    #[test]
    fn test_online_channel_adds_product_risk() {
        let mut input = base_input();
        input.product_type = ProductType::RetailBanking;
        input.channel = Channel::Online;
        let result = assess_kyc_risk(&input).unwrap();
        // retail (5) + online (3) = 8
        assert_eq!(result.risk_breakdown.product_score, dec!(8));
    }

    #[test]
    fn test_third_party_channel_adds_product_risk() {
        let mut input = base_input();
        input.product_type = ProductType::RetailBanking;
        input.channel = Channel::ThirdParty;
        let result = assess_kyc_risk(&input).unwrap();
        // retail (5) + third party (4) = 9
        assert_eq!(result.risk_breakdown.product_score, dec!(9));
    }

    #[test]
    fn test_product_score_capped_at_20() {
        let mut input = base_input();
        input.product_type = ProductType::TradeFinance; // 20
        input.channel = Channel::ThirdParty; // +4
        let result = assess_kyc_risk(&input).unwrap();
        assert_eq!(result.risk_breakdown.product_score, dec!(20));
    }

    // === Transaction pattern tests ===

    #[test]
    fn test_normal_transaction_pattern() {
        let input = base_input();
        let result = assess_kyc_risk(&input).unwrap();
        assert_eq!(result.risk_breakdown.transaction_score, dec!(3));
    }

    #[test]
    fn test_high_cross_border_pct() {
        let mut input = base_input();
        input.cross_border_transaction_pct = dec!(60);
        let result = assess_kyc_risk(&input).unwrap();
        // base (3) + >50 (3) = 6
        assert_eq!(result.risk_breakdown.transaction_score, dec!(6));
    }

    #[test]
    fn test_very_high_cross_border_pct() {
        let mut input = base_input();
        input.cross_border_transaction_pct = dec!(90);
        let result = assess_kyc_risk(&input).unwrap();
        // base (3) + >50 (3) + >80 (2) = 8
        assert_eq!(result.risk_breakdown.transaction_score, dec!(8));
    }

    #[test]
    fn test_high_cash_pct() {
        let mut input = base_input();
        input.cash_transaction_pct = dec!(40);
        let result = assess_kyc_risk(&input).unwrap();
        // base (3) + >30 (3) = 6
        assert_eq!(result.risk_breakdown.transaction_score, dec!(6));
    }

    #[test]
    fn test_very_high_cash_pct() {
        let mut input = base_input();
        input.cash_transaction_pct = dec!(70);
        let result = assess_kyc_risk(&input).unwrap();
        // base (3) + >30 (3) + >60 (2) = 8
        assert_eq!(result.risk_breakdown.transaction_score, dec!(8));
    }

    #[test]
    fn test_high_transaction_volume() {
        let mut input = base_input();
        input.annual_transaction_volume = dec!(15_000_000);
        let result = assess_kyc_risk(&input).unwrap();
        // base (3) + >10M (2) = 5
        assert_eq!(result.risk_breakdown.transaction_score, dec!(5));
    }

    #[test]
    fn test_transaction_score_capped_at_15() {
        let mut input = base_input();
        input.cross_border_transaction_pct = dec!(95);
        input.cash_transaction_pct = dec!(80);
        input.annual_transaction_volume = dec!(50_000_000);
        let result = assess_kyc_risk(&input).unwrap();
        assert_eq!(result.risk_breakdown.transaction_score, dec!(15));
    }

    // === Source of wealth tests ===

    #[test]
    fn test_employment_source_of_wealth() {
        let input = base_input();
        let result = assess_kyc_risk(&input).unwrap();
        assert_eq!(result.risk_breakdown.source_of_wealth_score, dec!(3));
    }

    #[test]
    fn test_business_source_of_wealth() {
        let mut input = base_input();
        input.source_of_wealth = SourceOfWealth::Business;
        let result = assess_kyc_risk(&input).unwrap();
        assert_eq!(result.risk_breakdown.source_of_wealth_score, dec!(6));
    }

    #[test]
    fn test_inheritance_source_of_wealth() {
        let mut input = base_input();
        input.source_of_wealth = SourceOfWealth::Inheritance;
        let result = assess_kyc_risk(&input).unwrap();
        assert_eq!(result.risk_breakdown.source_of_wealth_score, dec!(8));
    }

    #[test]
    fn test_unclear_source_of_wealth() {
        let mut input = base_input();
        input.source_of_wealth = SourceOfWealth::Unclear;
        let result = assess_kyc_risk(&input).unwrap();
        assert_eq!(result.risk_breakdown.source_of_wealth_score, dec!(12));
    }

    #[test]
    fn test_high_risk_industry_source_of_wealth() {
        let mut input = base_input();
        input.source_of_wealth = SourceOfWealth::HighRiskIndustry;
        let result = assess_kyc_risk(&input).unwrap();
        assert_eq!(result.risk_breakdown.source_of_wealth_score, dec!(15));
    }

    #[test]
    fn test_investment_source_of_wealth() {
        let mut input = base_input();
        input.source_of_wealth = SourceOfWealth::Investment;
        let result = assess_kyc_risk(&input).unwrap();
        assert_eq!(result.risk_breakdown.source_of_wealth_score, dec!(5));
    }

    #[test]
    fn test_high_risk_industry_name_adds_score() {
        let mut input = base_input();
        input.source_of_wealth = SourceOfWealth::Business;
        input.industry = "Gambling operations".to_string();
        let result = assess_kyc_risk(&input).unwrap();
        // business (6) + gambling adj (3) = 9
        assert_eq!(result.risk_breakdown.source_of_wealth_score, dec!(9));
    }

    #[test]
    fn test_source_of_wealth_score_capped_at_15() {
        let mut input = base_input();
        input.source_of_wealth = SourceOfWealth::HighRiskIndustry;
        input.industry = "Casino".to_string();
        let result = assess_kyc_risk(&input).unwrap();
        // high_risk (15) + casino adj (3) = capped at 15
        assert_eq!(result.risk_breakdown.source_of_wealth_score, dec!(15));
    }

    // === Due diligence level tests ===

    #[test]
    fn test_sdd_for_low_risk() {
        let input = base_input();
        let result = assess_kyc_risk(&input).unwrap();
        assert_eq!(result.due_diligence_level, DueDiligenceLevel::SDD);
    }

    #[test]
    fn test_cdd_for_medium_risk() {
        let mut input = base_input();
        input.customer_type = CustomerType::Corporate;
        input.source_of_wealth = SourceOfWealth::Business;
        input.cross_border_transaction_pct = dec!(60);
        let result = assess_kyc_risk(&input).unwrap();
        assert_eq!(result.risk_level, RiskLevel::Medium);
        assert_eq!(result.due_diligence_level, DueDiligenceLevel::CDD);
    }

    #[test]
    fn test_edd_for_high_risk() {
        let mut input = base_input();
        input.customer_type = CustomerType::ComplexStructure;
        input.source_of_wealth = SourceOfWealth::Unclear;
        input.product_type = ProductType::PrivateBanking;
        input.channel = Channel::Online;
        let result = assess_kyc_risk(&input).unwrap();
        assert!(matches!(
            result.risk_level,
            RiskLevel::High | RiskLevel::Critical
        ));
        assert_eq!(result.due_diligence_level, DueDiligenceLevel::EDD);
    }

    #[test]
    fn test_edd_always_for_pep() {
        let mut input = base_input();
        input.is_pep = true;
        input.pep_category = Some(PepCategory::DomesticPEP);
        let result = assess_kyc_risk(&input).unwrap();
        assert_eq!(result.due_diligence_level, DueDiligenceLevel::EDD);
    }

    // === Red flag tests ===

    #[test]
    fn test_no_red_flags_clean_customer() {
        let input = base_input();
        let result = assess_kyc_risk(&input).unwrap();
        assert!(result.red_flags.is_empty());
    }

    #[test]
    fn test_red_flag_nominee_directors() {
        let mut input = base_input();
        input.has_nominee_directors = true;
        let result = assess_kyc_risk(&input).unwrap();
        assert!(result
            .red_flags
            .iter()
            .any(|f| f.category == "Shell Company" && f.description.contains("Nominee")));
    }

    #[test]
    fn test_red_flag_bearer_shares() {
        let mut input = base_input();
        input.has_bearer_shares = true;
        let result = assess_kyc_risk(&input).unwrap();
        assert!(result
            .red_flags
            .iter()
            .any(|f| f.category == "Shell Company" && matches!(f.severity, RiskLevel::Critical)));
    }

    #[test]
    fn test_red_flag_complex_ownership() {
        let mut input = base_input();
        input.ownership_layers = 5;
        let result = assess_kyc_risk(&input).unwrap();
        assert!(result
            .red_flags
            .iter()
            .any(|f| f.category == "Ownership Complexity"));
    }

    #[test]
    fn test_red_flag_very_complex_ownership() {
        let mut input = base_input();
        input.ownership_layers = 7;
        let result = assess_kyc_risk(&input).unwrap();
        let flag = result
            .red_flags
            .iter()
            .find(|f| f.category == "Ownership Complexity")
            .unwrap();
        assert_eq!(flag.severity, RiskLevel::Critical);
    }

    #[test]
    fn test_red_flag_adverse_media_single() {
        let mut input = base_input();
        input.adverse_media_hits = 1;
        let result = assess_kyc_risk(&input).unwrap();
        let flag = result
            .red_flags
            .iter()
            .find(|f| f.category == "Adverse Media")
            .unwrap();
        assert_eq!(flag.severity, RiskLevel::Medium);
    }

    #[test]
    fn test_red_flag_adverse_media_multiple() {
        let mut input = base_input();
        input.adverse_media_hits = 3;
        let result = assess_kyc_risk(&input).unwrap();
        let flag = result
            .red_flags
            .iter()
            .find(|f| f.category == "Adverse Media")
            .unwrap();
        assert_eq!(flag.severity, RiskLevel::High);
    }

    #[test]
    fn test_red_flag_adverse_media_critical() {
        let mut input = base_input();
        input.adverse_media_hits = 5;
        let result = assess_kyc_risk(&input).unwrap();
        let flag = result
            .red_flags
            .iter()
            .find(|f| f.category == "Adverse Media")
            .unwrap();
        assert_eq!(flag.severity, RiskLevel::Critical);
    }

    #[test]
    fn test_red_flag_high_cash_pct() {
        let mut input = base_input();
        input.cash_transaction_pct = dec!(55);
        let result = assess_kyc_risk(&input).unwrap();
        assert!(result
            .red_flags
            .iter()
            .any(|f| f.category == "Transaction Pattern" && f.description.contains("Cash")));
    }

    #[test]
    fn test_red_flag_structuring_indicator() {
        let mut input = base_input();
        input.annual_transaction_volume = dec!(5_000_000);
        input.average_transaction_size = dec!(9_000);
        let result = assess_kyc_risk(&input).unwrap();
        assert!(result
            .red_flags
            .iter()
            .any(|f| f.description.contains("structuring")));
    }

    #[test]
    fn test_red_flag_pep_unclear_wealth() {
        let mut input = base_input();
        input.is_pep = true;
        input.pep_category = Some(PepCategory::DomesticPEP);
        input.source_of_wealth = SourceOfWealth::Unclear;
        let result = assess_kyc_risk(&input).unwrap();
        assert!(result.red_flags.iter().any(|f| f.category == "PEP Risk"));
    }

    #[test]
    fn test_red_flag_jurisdiction_mismatch() {
        let mut input = base_input();
        input.jurisdiction_of_incorporation = "Cayman Islands".to_string();
        input.jurisdiction_of_operations = vec!["United States".to_string()];
        let result = assess_kyc_risk(&input).unwrap();
        assert!(result
            .red_flags
            .iter()
            .any(|f| f.category == "Jurisdiction Mismatch"));
    }

    #[test]
    fn test_red_flag_high_risk_industry() {
        let mut input = base_input();
        input.industry = "Cryptocurrency exchange".to_string();
        let result = assess_kyc_risk(&input).unwrap();
        assert!(result
            .red_flags
            .iter()
            .any(|f| f.category == "Industry Risk"));
    }

    #[test]
    fn test_red_flag_sanctioned_operations() {
        let mut input = base_input();
        input.jurisdiction_of_operations = vec!["United Kingdom".to_string(), "Cuba".to_string()];
        let result = assess_kyc_risk(&input).unwrap();
        assert!(result
            .red_flags
            .iter()
            .any(|f| f.category == "Sanctions" && f.description.contains("Cuba")));
    }

    #[test]
    fn test_red_flag_sanctioned_incorporation() {
        let mut input = base_input();
        input.jurisdiction_of_incorporation = "Syria".to_string();
        let result = assess_kyc_risk(&input).unwrap();
        assert!(result
            .red_flags
            .iter()
            .any(|f| f.category == "Sanctions" && f.description.contains("Syria")));
    }

    // === Monitoring frequency tests ===

    #[test]
    fn test_monthly_monitoring_for_pep() {
        let mut input = base_input();
        input.is_pep = true;
        input.pep_category = Some(PepCategory::DomesticPEP);
        let result = assess_kyc_risk(&input).unwrap();
        assert_eq!(result.monitoring_frequency, MonitoringFrequency::Monthly);
    }

    #[test]
    fn test_triennial_monitoring_for_low_risk() {
        let input = base_input();
        let result = assess_kyc_risk(&input).unwrap();
        assert_eq!(result.monitoring_frequency, MonitoringFrequency::Triennial);
    }

    #[test]
    fn test_quarterly_monitoring_for_high_risk() {
        let mut input = base_input();
        input.customer_type = CustomerType::ComplexStructure;
        input.source_of_wealth = SourceOfWealth::Unclear;
        input.product_type = ProductType::PrivateBanking;
        input.channel = Channel::Online;
        let result = assess_kyc_risk(&input).unwrap();
        // Score should push to high risk => quarterly
        if matches!(result.risk_level, RiskLevel::High) {
            assert_eq!(result.monitoring_frequency, MonitoringFrequency::Quarterly);
        }
    }

    // === Documentation tests ===

    #[test]
    fn test_edd_documentation_includes_source_of_wealth() {
        let mut input = base_input();
        input.is_pep = true;
        input.pep_category = Some(PepCategory::DomesticPEP);
        let result = assess_kyc_risk(&input).unwrap();
        assert!(result
            .documentation_required
            .iter()
            .any(|d| d.contains("Source of wealth")));
    }

    #[test]
    fn test_edd_documentation_includes_pep_report() {
        let mut input = base_input();
        input.is_pep = true;
        input.pep_category = Some(PepCategory::DomesticPEP);
        let result = assess_kyc_risk(&input).unwrap();
        assert!(result
            .documentation_required
            .iter()
            .any(|d| d.contains("PEP screening")));
    }

    #[test]
    fn test_corporate_edd_documentation() {
        let mut input = base_input();
        input.customer_type = CustomerType::Corporate;
        input.is_pep = true;
        input.pep_category = Some(PepCategory::DomesticPEP);
        let result = assess_kyc_risk(&input).unwrap();
        assert!(result
            .documentation_required
            .iter()
            .any(|d| d.contains("UBO")));
    }

    // === Recommended actions tests ===

    #[test]
    fn test_prohibited_actions_block() {
        let mut input = base_input();
        input.jurisdiction_of_incorporation = "North Korea".to_string();
        input.customer_type = CustomerType::ComplexStructure;
        input.source_of_wealth = SourceOfWealth::HighRiskIndustry;
        input.product_type = ProductType::TradeFinance;
        input.cash_transaction_pct = dec!(70);
        input.cross_border_transaction_pct = dec!(90);
        let result = assess_kyc_risk(&input).unwrap();
        if matches!(result.risk_level, RiskLevel::Prohibited) {
            assert!(result
                .recommended_actions
                .iter()
                .any(|a| a.contains("BLOCK")));
        }
    }

    #[test]
    fn test_critical_actions_escalate() {
        let mut input = base_input();
        input.customer_type = CustomerType::ComplexStructure;
        input.jurisdiction_of_incorporation = "Myanmar".to_string();
        input.source_of_wealth = SourceOfWealth::Unclear;
        input.product_type = ProductType::CorrespondentBanking;
        let result = assess_kyc_risk(&input).unwrap();
        if matches!(result.risk_level, RiskLevel::Critical) {
            assert!(result
                .recommended_actions
                .iter()
                .any(|a| a.contains("MLRO")));
        }
    }

    #[test]
    fn test_pep_actions_senior_approval() {
        let mut input = base_input();
        input.is_pep = true;
        input.pep_category = Some(PepCategory::DomesticPEP);
        let result = assess_kyc_risk(&input).unwrap();
        assert!(result
            .recommended_actions
            .iter()
            .any(|a| a.contains("senior management")));
    }

    // === Input validation tests ===

    #[test]
    fn test_empty_customer_name_error() {
        let mut input = base_input();
        input.customer_name = "".to_string();
        let result = assess_kyc_risk(&input);
        assert!(result.is_err());
    }

    #[test]
    fn test_empty_jurisdiction_error() {
        let mut input = base_input();
        input.jurisdiction_of_incorporation = "".to_string();
        let result = assess_kyc_risk(&input);
        assert!(result.is_err());
    }

    #[test]
    fn test_invalid_cross_border_pct_error() {
        let mut input = base_input();
        input.cross_border_transaction_pct = dec!(105);
        let result = assess_kyc_risk(&input);
        assert!(result.is_err());
    }

    #[test]
    fn test_negative_cross_border_pct_error() {
        let mut input = base_input();
        input.cross_border_transaction_pct = dec!(-1);
        let result = assess_kyc_risk(&input);
        assert!(result.is_err());
    }

    #[test]
    fn test_negative_transaction_volume_error() {
        let mut input = base_input();
        input.annual_transaction_volume = dec!(-100);
        let result = assess_kyc_risk(&input);
        assert!(result.is_err());
    }

    #[test]
    fn test_invalid_cash_pct_error() {
        let mut input = base_input();
        input.cash_transaction_pct = dec!(101);
        let result = assess_kyc_risk(&input);
        assert!(result.is_err());
    }

    // === Edge case tests ===

    #[test]
    fn test_zero_transactions() {
        let mut input = base_input();
        input.annual_transaction_volume = Decimal::ZERO;
        input.average_transaction_size = Decimal::ZERO;
        let result = assess_kyc_risk(&input).unwrap();
        assert_eq!(result.risk_breakdown.transaction_score, dec!(3));
    }

    #[test]
    fn test_max_ownership_layers() {
        let mut input = base_input();
        input.ownership_layers = 20;
        let result = assess_kyc_risk(&input).unwrap();
        assert!(result
            .red_flags
            .iter()
            .any(|f| f.category == "Ownership Complexity"
                && matches!(f.severity, RiskLevel::Critical)));
    }

    #[test]
    fn test_overall_score_capped_at_100() {
        let mut input = base_input();
        input.customer_type = CustomerType::PEP;
        input.is_pep = true;
        input.pep_category = Some(PepCategory::DomesticPEP);
        input.jurisdiction_of_incorporation = "North Korea".to_string();
        input.product_type = ProductType::TradeFinance;
        input.cash_transaction_pct = dec!(80);
        input.cross_border_transaction_pct = dec!(90);
        input.source_of_wealth = SourceOfWealth::HighRiskIndustry;
        input.annual_transaction_volume = dec!(50_000_000);
        let result = assess_kyc_risk(&input).unwrap();
        assert!(result.overall_risk_score <= dec!(100));
    }

    #[test]
    fn test_methodology_populated() {
        let input = base_input();
        let result = assess_kyc_risk(&input).unwrap();
        assert!(result.methodology.contains("FATF"));
    }

    #[test]
    fn test_assumptions_populated() {
        let input = base_input();
        let result = assess_kyc_risk(&input).unwrap();
        assert!(!result.assumptions.is_empty());
    }

    #[test]
    fn test_warnings_for_prohibited() {
        let mut input = base_input();
        input.customer_type = CustomerType::PEP;
        input.is_pep = true;
        input.pep_category = Some(PepCategory::DomesticPEP);
        input.jurisdiction_of_incorporation = "North Korea".to_string();
        input.product_type = ProductType::TradeFinance;
        input.source_of_wealth = SourceOfWealth::HighRiskIndustry;
        input.cash_transaction_pct = dec!(80);
        input.cross_border_transaction_pct = dec!(90);
        input.annual_transaction_volume = dec!(50_000_000);
        let result = assess_kyc_risk(&input).unwrap();
        if matches!(result.risk_level, RiskLevel::Prohibited) {
            assert!(result.warnings.iter().any(|w| w.contains("sanctions")));
        }
    }

    #[test]
    fn test_warnings_for_adverse_media() {
        let mut input = base_input();
        input.adverse_media_hits = 3;
        let result = assess_kyc_risk(&input).unwrap();
        assert!(result.warnings.iter().any(|w| w.contains("adverse media")));
    }

    #[test]
    fn test_serde_round_trip() {
        let input = base_input();
        let json = serde_json::to_string(&input).unwrap();
        let deserialized: KycRiskInput = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.customer_name, input.customer_name);
    }

    #[test]
    fn test_output_serde_round_trip() {
        let input = base_input();
        let result = assess_kyc_risk(&input).unwrap();
        let json = serde_json::to_string(&result).unwrap();
        let deserialized: KycRiskOutput = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.overall_risk_score, result.overall_risk_score);
    }

    // === Composite scenario tests ===

    #[test]
    fn test_high_risk_composite() {
        let mut input = base_input();
        input.customer_type = CustomerType::Trust;
        input.jurisdiction_of_incorporation = "Panama".to_string();
        input.product_type = ProductType::PrivateBanking;
        input.channel = Channel::Online;
        input.source_of_wealth = SourceOfWealth::Inheritance;
        input.cross_border_transaction_pct = dec!(60);
        input.has_nominee_directors = true;
        input.adverse_media_hits = 1;
        let result = assess_kyc_risk(&input).unwrap();
        assert!(result.overall_risk_score >= dec!(50));
        assert!(matches!(
            result.risk_level,
            RiskLevel::High | RiskLevel::Critical | RiskLevel::Prohibited
        ));
        assert_eq!(result.due_diligence_level, DueDiligenceLevel::EDD);
    }

    #[test]
    fn test_medium_risk_composite() {
        let mut input = base_input();
        input.customer_type = CustomerType::Corporate;
        input.product_type = ProductType::FundInvestment;
        input.source_of_wealth = SourceOfWealth::Business;
        input.cross_border_transaction_pct = dec!(30);
        let result = assess_kyc_risk(&input).unwrap();
        assert!(result.overall_risk_score >= dec!(25));
    }

    #[test]
    fn test_empty_operations_jurisdictions() {
        let mut input = base_input();
        input.jurisdiction_of_operations = vec![];
        let result = assess_kyc_risk(&input).unwrap();
        // Should still work with just incorporation jurisdiction
        assert!(result.overall_risk_score > Decimal::ZERO);
        assert_eq!(result.jurisdiction_risk_details.len(), 1);
    }

    #[test]
    fn test_case_insensitive_jurisdiction() {
        let mut input = base_input();
        input.jurisdiction_of_incorporation = "IRAN".to_string();
        let result = assess_kyc_risk(&input).unwrap();
        assert_eq!(result.risk_breakdown.geographic_score, dec!(25));
    }

    #[test]
    fn test_dprk_alias() {
        let mut input = base_input();
        input.jurisdiction_of_incorporation = "DPRK".to_string();
        let result = assess_kyc_risk(&input).unwrap();
        assert_eq!(result.risk_breakdown.geographic_score, dec!(25));
    }
}