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
use crate::compat::Instant;
use rust_decimal::Decimal;
use rust_decimal_macros::dec;
use serde::{Deserialize, Serialize};

use crate::error::CorpFinanceError;
use crate::types::{with_metadata, ComputationOutput, Money};
use crate::CorpFinanceResult;

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

/// Basel III/IV asset classes for credit risk Standardised Approach.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum AssetClass {
    Sovereign,
    Bank,
    Corporate,
    Retail,
    Mortgage,
    Equity,
    Other,
}

/// Collateral types for credit risk mitigation (CRM).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum CollateralType {
    Cash,
    GovernmentBond,
    CorporateBond,
    Equity,
    RealEstate,
}

/// Operational risk approach.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum OpRiskApproach {
    BasicIndicator,
    Standardised,
}

// ---------------------------------------------------------------------------
// Input types
// ---------------------------------------------------------------------------

/// Capital structure of the institution.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CapitalStructure {
    /// Common Equity Tier 1
    pub cet1: Money,
    /// Additional Tier 1 (CoCos, preferred shares)
    pub additional_tier1: Money,
    /// Tier 2 (subordinated debt, general provisions)
    pub tier2: Money,
    /// Regulatory deductions (goodwill, deferred tax assets, etc.)
    pub deductions: Money,
}

/// A single credit exposure for RWA calculation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreditExposure {
    pub name: String,
    /// Exposure at default (EAD)
    pub exposure_amount: Money,
    pub asset_class: AssetClass,
    /// Override risk weight (0 to 1.5); if None, derived from asset_class + rating
    #[serde(skip_serializing_if = "Option::is_none")]
    pub risk_weight: Option<Decimal>,
    /// External credit rating: "AAA", "AA", "A", "BBB", "BB", "B", "CCC", "Unrated"
    #[serde(skip_serializing_if = "Option::is_none")]
    pub external_rating: Option<String>,
    /// Collateral value for credit risk mitigation
    #[serde(skip_serializing_if = "Option::is_none")]
    pub collateral_value: Option<Money>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub collateral_type: Option<CollateralType>,
}

/// Gross income for a single business line (Standardised Approach).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BusinessLineIncome {
    /// One of: "CorporateFinance", "Trading", "Retail", "Commercial",
    /// "Payment", "Agency", "AssetMgmt", "RetailBrokerage"
    pub line: String,
    pub gross_income: Money,
}

/// Operational risk calculation inputs.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OperationalRiskInput {
    pub approach: OpRiskApproach,
    /// Three years of gross income (for BIA / SA fallback)
    pub gross_income_3yr: Vec<Money>,
    /// Business-line breakdown (required for Standardised Approach)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub business_lines: Option<Vec<BusinessLineIncome>>,
}

/// Capital buffer requirements.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CapitalBuffers {
    /// Capital conservation buffer (typically 2.5%)
    pub conservation_buffer: Decimal,
    /// Countercyclical buffer (0 - 2.5%, varies by jurisdiction)
    pub countercyclical_buffer: Decimal,
    /// Systemic buffer for G-SIBs (0 - 3.5%)
    pub systemic_buffer: Decimal,
}

/// Top-level input for regulatory capital calculation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RegulatoryCapitalInput {
    pub institution_name: String,
    pub capital: CapitalStructure,
    pub credit_exposures: Vec<CreditExposure>,
    /// Pre-calculated market risk RWA (SA-TB or IMA)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub market_risk_charge: Option<Money>,
    pub operational_risk: OperationalRiskInput,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub buffers: Option<CapitalBuffers>,
}

// ---------------------------------------------------------------------------
// Output types
// ---------------------------------------------------------------------------

/// Summary of available capital after deductions.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CapitalSummary {
    pub cet1_capital: Money,
    /// CET1 + AT1
    pub tier1_capital: Money,
    /// Tier1 + Tier2
    pub total_capital: Money,
    pub deductions: Money,
}

/// Basel III capital adequacy ratios.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CapitalRatios {
    /// CET1 / total RWA
    pub cet1_ratio: Decimal,
    /// Tier 1 / total RWA
    pub tier1_ratio: Decimal,
    /// Total capital / total RWA
    pub total_capital_ratio: Decimal,
    /// Tier 1 / total exposure (not RWA)
    pub leverage_ratio: Decimal,
}

/// Per-exposure detail showing risk weight application and CRM benefit.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExposureDetail {
    pub name: String,
    pub exposure: Money,
    pub risk_weight: Decimal,
    /// exposure * risk_weight (after CRM adjustment)
    pub rwa: Money,
    /// RWA reduction from collateral
    pub crm_benefit: Money,
}

/// Capital buffer stacking.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BufferRequirements {
    /// Basel III minimum CET1 (4.5%)
    pub min_cet1: Decimal,
    /// Conservation buffer (2.5%)
    pub conservation: Decimal,
    pub countercyclical: Decimal,
    pub systemic: Decimal,
    /// Sum of all CET1 requirements
    pub total_cet1_requirement: Decimal,
}

/// Capital surplus or deficit relative to regulatory minimums.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SurplusDeficit {
    /// Actual CET1 ratio minus total CET1 requirement (incl. buffers)
    pub cet1_surplus: Decimal,
    /// Actual Tier 1 ratio minus 6.0%
    pub tier1_surplus: Decimal,
    /// Actual total capital ratio minus 8.0%
    pub total_capital_surplus: Decimal,
    pub meets_requirements: bool,
}

/// Complete regulatory capital output.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RegulatoryCapitalOutput {
    pub capital_summary: CapitalSummary,
    pub credit_rwa: Money,
    pub market_rwa: Money,
    pub operational_rwa: Money,
    pub total_rwa: Money,
    pub capital_ratios: CapitalRatios,
    pub exposure_details: Vec<ExposureDetail>,
    pub buffer_requirements: BufferRequirements,
    pub surplus_deficit: SurplusDeficit,
}

// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------

/// Basel III minimum CET1 ratio
const MIN_CET1_RATIO: Decimal = dec!(0.045);
/// Basel III minimum Tier 1 ratio
const MIN_TIER1_RATIO: Decimal = dec!(0.06);
/// Basel III minimum total capital ratio
const MIN_TOTAL_CAPITAL_RATIO: Decimal = dec!(0.08);
/// Default conservation buffer
const DEFAULT_CONSERVATION: Decimal = dec!(0.025);

// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------

/// Calculate Basel III/IV regulatory capital and RWA.
///
/// Computes risk-weighted assets under the Standardised Approach for credit
/// risk, operational risk (BIA or SA), and combines with a pre-calculated
/// market risk charge. Derives capital ratios, buffer requirements, and
/// surplus/deficit analysis.
pub fn calculate_regulatory_capital(
    input: &RegulatoryCapitalInput,
) -> CorpFinanceResult<ComputationOutput<RegulatoryCapitalOutput>> {
    let start = Instant::now();
    let mut warnings: Vec<String> = Vec::new();

    // -- Validation ----------------------------------------------------------
    validate_input(input, &mut warnings)?;

    // -- Capital summary -----------------------------------------------------
    let cap = &input.capital;
    let cet1_capital = cap.cet1 - cap.deductions;
    let tier1_capital = cap.cet1 + cap.additional_tier1 - cap.deductions;
    let total_capital = cap.cet1 + cap.additional_tier1 + cap.tier2 - cap.deductions;

    let capital_summary = CapitalSummary {
        cet1_capital,
        tier1_capital,
        total_capital,
        deductions: cap.deductions,
    };

    // -- Credit RWA (Standardised Approach) ----------------------------------
    let mut exposure_details: Vec<ExposureDetail> = Vec::new();
    let mut credit_rwa = Decimal::ZERO;

    for exp in &input.credit_exposures {
        let detail = calculate_exposure_rwa(exp, &mut warnings)?;
        credit_rwa += detail.rwa;
        exposure_details.push(detail);
    }

    // -- Market risk RWA -----------------------------------------------------
    let market_rwa = input.market_risk_charge.unwrap_or(Decimal::ZERO);

    // -- Operational risk RWA ------------------------------------------------
    let operational_rwa = calculate_operational_risk(&input.operational_risk, &mut warnings)?;

    // -- Total RWA -----------------------------------------------------------
    let total_rwa = credit_rwa + market_rwa + operational_rwa;

    if total_rwa.is_zero() {
        return Err(CorpFinanceError::DivisionByZero {
            context: "total RWA is zero; cannot compute capital ratios".into(),
        });
    }

    // -- Capital ratios ------------------------------------------------------
    let cet1_ratio = cet1_capital / total_rwa;
    let tier1_ratio = tier1_capital / total_rwa;
    let total_capital_ratio = total_capital / total_rwa;

    // Leverage ratio: Tier 1 / total exposure (sum of EADs, not RWA)
    let total_exposure: Decimal = input
        .credit_exposures
        .iter()
        .map(|e| e.exposure_amount)
        .sum::<Decimal>()
        + market_rwa; // Include market risk as exposure proxy

    let leverage_ratio = if total_exposure.is_zero() {
        warnings.push("Total exposure is zero; leverage ratio set to zero.".into());
        Decimal::ZERO
    } else {
        tier1_capital / total_exposure
    };

    let capital_ratios = CapitalRatios {
        cet1_ratio,
        tier1_ratio,
        total_capital_ratio,
        leverage_ratio,
    };

    // -- Buffer requirements -------------------------------------------------
    let buffers = input.buffers.as_ref();
    let conservation = buffers
        .map(|b| b.conservation_buffer)
        .unwrap_or(DEFAULT_CONSERVATION);
    let countercyclical = buffers
        .map(|b| b.countercyclical_buffer)
        .unwrap_or(Decimal::ZERO);
    let systemic = buffers.map(|b| b.systemic_buffer).unwrap_or(Decimal::ZERO);
    let total_cet1_requirement = MIN_CET1_RATIO + conservation + countercyclical + systemic;

    let buffer_requirements = BufferRequirements {
        min_cet1: MIN_CET1_RATIO,
        conservation,
        countercyclical,
        systemic,
        total_cet1_requirement,
    };

    // -- Surplus / deficit ---------------------------------------------------
    let cet1_surplus = cet1_ratio - total_cet1_requirement;
    let tier1_surplus = tier1_ratio - MIN_TIER1_RATIO;
    let total_capital_surplus = total_capital_ratio - MIN_TOTAL_CAPITAL_RATIO;

    let meets_requirements = cet1_ratio >= total_cet1_requirement
        && tier1_ratio >= MIN_TIER1_RATIO
        && total_capital_ratio >= MIN_TOTAL_CAPITAL_RATIO;

    if !meets_requirements {
        warnings.push("Institution does NOT meet minimum capital requirements.".into());
    }

    let surplus_deficit = SurplusDeficit {
        cet1_surplus,
        tier1_surplus,
        total_capital_surplus,
        meets_requirements,
    };

    // -- Assemble output -----------------------------------------------------
    let output = RegulatoryCapitalOutput {
        capital_summary,
        credit_rwa,
        market_rwa,
        operational_rwa,
        total_rwa,
        capital_ratios,
        exposure_details,
        buffer_requirements,
        surplus_deficit,
    };

    let elapsed = start.elapsed().as_micros() as u64;
    let assumptions = serde_json::json!({
        "framework": "Basel III / IV Standardised Approach",
        "credit_risk": "SA risk weights by asset class and external rating",
        "operational_risk": format!("{:?}", input.operational_risk.approach),
        "minimum_cet1": "4.5%",
        "minimum_tier1": "6.0%",
        "minimum_total_capital": "8.0%",
    });

    Ok(with_metadata(
        "Basel III/IV Regulatory Capital (Standardised Approach)",
        &assumptions,
        warnings,
        elapsed,
        output,
    ))
}

// ---------------------------------------------------------------------------
// Internal: input validation
// ---------------------------------------------------------------------------

fn validate_input(
    input: &RegulatoryCapitalInput,
    warnings: &mut Vec<String>,
) -> CorpFinanceResult<()> {
    let cap = &input.capital;

    if cap.cet1 < Decimal::ZERO {
        return Err(CorpFinanceError::InvalidInput {
            field: "capital.cet1".into(),
            reason: "CET1 capital cannot be negative.".into(),
        });
    }
    if cap.additional_tier1 < Decimal::ZERO {
        return Err(CorpFinanceError::InvalidInput {
            field: "capital.additional_tier1".into(),
            reason: "AT1 capital cannot be negative.".into(),
        });
    }
    if cap.tier2 < Decimal::ZERO {
        return Err(CorpFinanceError::InvalidInput {
            field: "capital.tier2".into(),
            reason: "Tier 2 capital cannot be negative.".into(),
        });
    }
    if cap.deductions < Decimal::ZERO {
        return Err(CorpFinanceError::InvalidInput {
            field: "capital.deductions".into(),
            reason: "Deductions cannot be negative.".into(),
        });
    }

    if input.credit_exposures.is_empty() {
        warnings.push("No credit exposures provided; credit RWA will be zero.".into());
    }

    for (i, exp) in input.credit_exposures.iter().enumerate() {
        if exp.exposure_amount < Decimal::ZERO {
            return Err(CorpFinanceError::InvalidInput {
                field: format!("credit_exposures[{}].exposure_amount", i),
                reason: "Exposure amount cannot be negative.".into(),
            });
        }
        if let Some(rw) = exp.risk_weight {
            if rw < Decimal::ZERO || rw > dec!(1.5) {
                return Err(CorpFinanceError::InvalidInput {
                    field: format!("credit_exposures[{}].risk_weight", i),
                    reason: "Risk weight must be between 0 and 1.5.".into(),
                });
            }
        }
    }

    // Validate operational risk inputs
    let op = &input.operational_risk;
    if op.gross_income_3yr.is_empty() {
        return Err(CorpFinanceError::InsufficientData(
            "At least one year of gross income required for operational risk.".into(),
        ));
    }
    if op.approach == OpRiskApproach::Standardised && op.business_lines.is_none() {
        return Err(CorpFinanceError::InvalidInput {
            field: "operational_risk.business_lines".into(),
            reason: "Business line breakdown required for Standardised Approach.".into(),
        });
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Internal: credit risk per exposure
// ---------------------------------------------------------------------------

/// Calculate RWA for a single credit exposure, applying CRM if collateral
/// is provided.
fn calculate_exposure_rwa(
    exp: &CreditExposure,
    warnings: &mut Vec<String>,
) -> CorpFinanceResult<ExposureDetail> {
    let risk_weight = match exp.risk_weight {
        Some(rw) => rw,
        None => derive_risk_weight(&exp.asset_class, &exp.external_rating, warnings),
    };

    // Credit risk mitigation via collateral
    let (adjusted_ead, crm_benefit) = apply_crm(exp, risk_weight, warnings);

    let rwa = adjusted_ead * risk_weight;

    Ok(ExposureDetail {
        name: exp.name.clone(),
        exposure: exp.exposure_amount,
        risk_weight,
        rwa,
        crm_benefit,
    })
}

/// Derive the SA risk weight from asset class and external rating.
fn derive_risk_weight(
    asset_class: &AssetClass,
    rating: &Option<String>,
    warnings: &mut Vec<String>,
) -> Decimal {
    let rating_str = rating.as_deref().unwrap_or("Unrated").trim().to_uppercase();

    match asset_class {
        AssetClass::Sovereign => match rating_str.as_str() {
            "AAA" | "AA" => dec!(0),
            "A" => dec!(0.20),
            "BBB" => dec!(0.50),
            "BB" | "B" => dec!(1.00),
            "CCC" => dec!(1.50),
            "UNRATED" => {
                warnings.push("Unrated sovereign assigned 100% risk weight.".into());
                dec!(1.00)
            }
            _ => {
                warnings.push(format!(
                    "Unknown sovereign rating '{}'; assigned 150% risk weight.",
                    rating_str
                ));
                dec!(1.50)
            }
        },
        AssetClass::Bank => match rating_str.as_str() {
            "AAA" | "AA" | "A" => dec!(0.20),
            "BBB" => dec!(0.50),
            "BB" | "B" => dec!(1.00),
            "CCC" => dec!(1.50),
            "UNRATED" => {
                warnings.push("Unrated bank assigned 50% risk weight.".into());
                dec!(0.50)
            }
            _ => {
                warnings.push(format!(
                    "Unknown bank rating '{}'; assigned 150% risk weight.",
                    rating_str
                ));
                dec!(1.50)
            }
        },
        AssetClass::Corporate => match rating_str.as_str() {
            "AAA" | "AA" => dec!(0.20),
            "A" => dec!(0.50),
            "BBB" | "BB" => dec!(1.00),
            "B" => dec!(1.50),
            "CCC" => dec!(1.50),
            "UNRATED" => dec!(1.00),
            _ => {
                warnings.push(format!(
                    "Unknown corporate rating '{}'; assigned 150% risk weight.",
                    rating_str
                ));
                dec!(1.50)
            }
        },
        AssetClass::Retail => dec!(0.75),
        AssetClass::Mortgage => {
            // 35% for residential (default); commercial would be 100% but
            // we treat all mortgage-class exposures as residential here.
            dec!(0.35)
        }
        AssetClass::Equity => {
            // Listed equity 100%, unlisted 150%. Without a flag, default
            // to 100% (listed). Use risk_weight override for unlisted.
            dec!(1.00)
        }
        AssetClass::Other => dec!(1.00),
    }
}

/// Apply credit risk mitigation (CRM) using collateral.
///
/// Returns (adjusted_ead, crm_benefit_in_rwa_terms).
fn apply_crm(
    exp: &CreditExposure,
    risk_weight: Decimal,
    _warnings: &mut Vec<String>,
) -> (Decimal, Decimal) {
    let ead = exp.exposure_amount;

    let (collateral_val, collateral_ty) = match (&exp.collateral_value, &exp.collateral_type) {
        (Some(cv), Some(ct)) if *cv > Decimal::ZERO => (*cv, ct.clone()),
        _ => return (ead, Decimal::ZERO),
    };

    let haircut = collateral_haircut(&collateral_ty);
    let effective_collateral = collateral_val * (Decimal::ONE - haircut);
    let adjusted_ead = (ead - effective_collateral).max(Decimal::ZERO);

    // CRM benefit = reduction in RWA
    let original_rwa = ead * risk_weight;
    let new_rwa = adjusted_ead * risk_weight;
    let crm_benefit = original_rwa - new_rwa;

    (adjusted_ead, crm_benefit)
}

/// Supervisory haircut for collateral types.
fn collateral_haircut(ct: &CollateralType) -> Decimal {
    match ct {
        CollateralType::Cash => dec!(0),
        CollateralType::GovernmentBond => dec!(0.02),
        CollateralType::CorporateBond => dec!(0.08),
        CollateralType::Equity => dec!(0.25),
        CollateralType::RealEstate => dec!(0.30),
    }
}

// ---------------------------------------------------------------------------
// Internal: operational risk
// ---------------------------------------------------------------------------

/// Calculate operational risk RWA under BIA or Standardised Approach.
fn calculate_operational_risk(
    op: &OperationalRiskInput,
    warnings: &mut Vec<String>,
) -> CorpFinanceResult<Decimal> {
    match op.approach {
        OpRiskApproach::BasicIndicator => {
            // BIA: 15% * average of positive gross income over 3 years
            let positive_incomes: Vec<Decimal> = op
                .gross_income_3yr
                .iter()
                .copied()
                .filter(|gi| *gi > Decimal::ZERO)
                .collect();

            if positive_incomes.is_empty() {
                warnings
                    .push("No positive gross income years; operational risk RWA is zero.".into());
                return Ok(Decimal::ZERO);
            }

            let count = Decimal::from(positive_incomes.len() as u64);
            let sum: Decimal = positive_incomes.into_iter().sum();
            let avg = sum / count;

            Ok(avg * dec!(0.15))
        }
        OpRiskApproach::Standardised => {
            let business_lines =
                op.business_lines
                    .as_ref()
                    .ok_or_else(|| CorpFinanceError::InvalidInput {
                        field: "operational_risk.business_lines".into(),
                        reason: "Business line data required for Standardised Approach.".into(),
                    })?;

            let mut total = Decimal::ZERO;
            for bl in business_lines {
                let beta = beta_factor(&bl.line, warnings);
                total += bl.gross_income * beta;
            }

            // Floor at zero
            Ok(total.max(Decimal::ZERO))
        }
    }
}

/// Basel II/III beta factors for operational risk Standardised Approach.
fn beta_factor(line: &str, warnings: &mut Vec<String>) -> Decimal {
    match line {
        "CorporateFinance" => dec!(0.18),
        "Trading" => dec!(0.18),
        "Retail" => dec!(0.12),
        "Commercial" => dec!(0.15),
        "Payment" => dec!(0.18),
        "Agency" => dec!(0.15),
        "AssetMgmt" => dec!(0.12),
        "RetailBrokerage" => dec!(0.12),
        other => {
            warnings.push(format!(
                "Unknown business line '{}'; using default beta of 15%.",
                other
            ));
            dec!(0.15)
        }
    }
}

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

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

    // -- Helpers -------------------------------------------------------------

    fn default_capital() -> CapitalStructure {
        CapitalStructure {
            cet1: dec!(10_000),
            additional_tier1: dec!(2_000),
            tier2: dec!(3_000),
            deductions: dec!(1_000),
        }
    }

    fn default_op_risk() -> OperationalRiskInput {
        OperationalRiskInput {
            approach: OpRiskApproach::BasicIndicator,
            gross_income_3yr: vec![dec!(50_000), dec!(55_000), dec!(60_000)],
            business_lines: None,
        }
    }

    fn simple_exposure(name: &str, amount: Decimal, class: AssetClass) -> CreditExposure {
        CreditExposure {
            name: name.to_string(),
            exposure_amount: amount,
            asset_class: class,
            risk_weight: None,
            external_rating: None,
            collateral_value: None,
            collateral_type: None,
        }
    }

    fn rated_exposure(
        name: &str,
        amount: Decimal,
        class: AssetClass,
        rating: &str,
    ) -> CreditExposure {
        CreditExposure {
            name: name.to_string(),
            exposure_amount: amount,
            asset_class: class,
            risk_weight: None,
            external_rating: Some(rating.to_string()),
            collateral_value: None,
            collateral_type: None,
        }
    }

    fn make_input(exposures: Vec<CreditExposure>) -> RegulatoryCapitalInput {
        RegulatoryCapitalInput {
            institution_name: "Test Bank".to_string(),
            capital: default_capital(),
            credit_exposures: exposures,
            market_risk_charge: None,
            operational_risk: default_op_risk(),
            buffers: None,
        }
    }

    // -- Test: 100% risk weight => RWA = exposure ----------------------------

    #[test]
    fn test_100pct_risk_weight_rwa_equals_exposure() {
        let exp = simple_exposure("Other Asset", dec!(100_000), AssetClass::Other);
        let input = make_input(vec![exp]);
        let result = calculate_regulatory_capital(&input).unwrap();
        // Other class => 100% RW; op risk is added separately
        let detail = &result.result.exposure_details[0];
        assert_eq!(detail.risk_weight, dec!(1.00));
        assert_eq!(detail.rwa, dec!(100_000));
        assert_eq!(result.result.credit_rwa, dec!(100_000));
    }

    // -- Test: OECD sovereign AAA => 0% RW -----------------------------------

    #[test]
    fn test_sovereign_aaa_zero_risk_weight() {
        let exp = rated_exposure("US Treasury", dec!(50_000), AssetClass::Sovereign, "AAA");
        let input = make_input(vec![exp]);
        let result = calculate_regulatory_capital(&input).unwrap();
        let detail = &result.result.exposure_details[0];
        assert_eq!(detail.risk_weight, dec!(0));
        assert_eq!(detail.rwa, dec!(0));
    }

    // -- Test: Sovereign AA => 0% RW -----------------------------------------

    #[test]
    fn test_sovereign_aa_zero_risk_weight() {
        let exp = rated_exposure("UK Gilt", dec!(30_000), AssetClass::Sovereign, "AA");
        let input = make_input(vec![exp]);
        let result = calculate_regulatory_capital(&input).unwrap();
        assert_eq!(result.result.exposure_details[0].risk_weight, dec!(0));
    }

    // -- Test: Corporate BBB => 100% RW --------------------------------------

    #[test]
    fn test_corporate_bbb_100pct() {
        let exp = rated_exposure("Corp BBB", dec!(80_000), AssetClass::Corporate, "BBB");
        let input = make_input(vec![exp]);
        let result = calculate_regulatory_capital(&input).unwrap();
        let detail = &result.result.exposure_details[0];
        assert_eq!(detail.risk_weight, dec!(1.00));
        assert_eq!(detail.rwa, dec!(80_000));
    }

    // -- Test: Retail => 75% RW ----------------------------------------------

    #[test]
    fn test_retail_75pct() {
        let exp = simple_exposure("Retail Portfolio", dec!(40_000), AssetClass::Retail);
        let input = make_input(vec![exp]);
        let result = calculate_regulatory_capital(&input).unwrap();
        let detail = &result.result.exposure_details[0];
        assert_eq!(detail.risk_weight, dec!(0.75));
        assert_eq!(detail.rwa, dec!(30_000));
    }

    // -- Test: Residential mortgage => 35% RW --------------------------------

    #[test]
    fn test_residential_mortgage_35pct() {
        let exp = simple_exposure("Mortgages", dec!(200_000), AssetClass::Mortgage);
        let input = make_input(vec![exp]);
        let result = calculate_regulatory_capital(&input).unwrap();
        let detail = &result.result.exposure_details[0];
        assert_eq!(detail.risk_weight, dec!(0.35));
        assert_eq!(detail.rwa, dec!(70_000));
    }

    // -- Test: CET1 ratio calculation ----------------------------------------

    #[test]
    fn test_cet1_ratio_calculation() {
        // CET1 capital = 10,000 - 1,000 = 9,000
        // Single exposure: 100,000 at 100% => credit_rwa = 100,000
        // Op risk BIA: 15% * avg(50k, 55k, 60k) = 15% * 55k = 8,250
        // Total RWA = 100,000 + 8,250 = 108,250
        // CET1 ratio = 9,000 / 108,250
        let exp = simple_exposure("Loan", dec!(100_000), AssetClass::Other);
        let input = make_input(vec![exp]);
        let result = calculate_regulatory_capital(&input).unwrap();

        let expected_cet1 = dec!(9_000);
        let expected_total_rwa = dec!(100_000) + dec!(8_250);
        let expected_ratio = expected_cet1 / expected_total_rwa;

        assert_eq!(result.result.capital_summary.cet1_capital, expected_cet1);
        assert_eq!(result.result.operational_rwa, dec!(8_250));
        assert_eq!(result.result.total_rwa, expected_total_rwa);
        assert_eq!(result.result.capital_ratios.cet1_ratio, expected_ratio);
    }

    // -- Test: Meets all capital requirements --------------------------------

    #[test]
    fn test_meets_all_requirements() {
        // With generous capital and small exposure, should pass
        let mut input = make_input(vec![simple_exposure(
            "Small Loan",
            dec!(10_000),
            AssetClass::Other,
        )]);
        input.capital = CapitalStructure {
            cet1: dec!(50_000),
            additional_tier1: dec!(10_000),
            tier2: dec!(15_000),
            deductions: dec!(2_000),
        };
        let result = calculate_regulatory_capital(&input).unwrap();
        assert!(result.result.surplus_deficit.meets_requirements);
        assert!(result.result.surplus_deficit.cet1_surplus > Decimal::ZERO);
        assert!(result.result.surplus_deficit.tier1_surplus > Decimal::ZERO);
        assert!(result.result.surplus_deficit.total_capital_surplus > Decimal::ZERO);
    }

    // -- Test: Fails CET1 minimum (< 4.5%) ----------------------------------

    #[test]
    fn test_fails_cet1_minimum() {
        // Tiny CET1 relative to large exposure
        let mut input = make_input(vec![simple_exposure(
            "Big Loan",
            dec!(1_000_000),
            AssetClass::Other,
        )]);
        input.capital = CapitalStructure {
            cet1: dec!(5_000),
            additional_tier1: dec!(1_000),
            tier2: dec!(2_000),
            deductions: dec!(0),
        };
        let result = calculate_regulatory_capital(&input).unwrap();
        // CET1 = 5,000; RWA ~ 1,000,000 + op_risk; CET1 ratio << 4.5%
        assert!(!result.result.surplus_deficit.meets_requirements);
        assert!(result.result.surplus_deficit.cet1_surplus < Decimal::ZERO);
    }

    // -- Test: Operational risk BIA calculation ------------------------------

    #[test]
    fn test_operational_risk_bia() {
        // BIA: 15% * avg(50k, 55k, 60k) = 15% * 55,000 = 8,250
        let input = make_input(vec![simple_exposure(
            "Placeholder",
            dec!(10_000),
            AssetClass::Other,
        )]);
        let result = calculate_regulatory_capital(&input).unwrap();
        assert_eq!(result.result.operational_rwa, dec!(8_250));
    }

    // -- Test: Operational risk SA calculation --------------------------------

    #[test]
    fn test_operational_risk_standardised() {
        let mut input = make_input(vec![simple_exposure(
            "Loan",
            dec!(10_000),
            AssetClass::Other,
        )]);
        input.operational_risk = OperationalRiskInput {
            approach: OpRiskApproach::Standardised,
            gross_income_3yr: vec![dec!(100_000)],
            business_lines: Some(vec![
                BusinessLineIncome {
                    line: "CorporateFinance".to_string(),
                    gross_income: dec!(30_000),
                },
                BusinessLineIncome {
                    line: "Retail".to_string(),
                    gross_income: dec!(20_000),
                },
                BusinessLineIncome {
                    line: "Trading".to_string(),
                    gross_income: dec!(10_000),
                },
            ]),
        };
        let result = calculate_regulatory_capital(&input).unwrap();
        // CorporateFinance: 30k * 0.18 = 5,400
        // Retail: 20k * 0.12 = 2,400
        // Trading: 10k * 0.18 = 1,800
        // Total = 9,600
        assert_eq!(result.result.operational_rwa, dec!(9_600));
    }

    // -- Test: Collateral reduces RWA ----------------------------------------

    #[test]
    fn test_collateral_reduces_rwa() {
        let exp = CreditExposure {
            name: "Secured Loan".to_string(),
            exposure_amount: dec!(100_000),
            asset_class: AssetClass::Corporate,
            risk_weight: None,
            external_rating: Some("BBB".to_string()),
            collateral_value: Some(dec!(50_000)),
            collateral_type: Some(CollateralType::Cash),
        };
        let input = make_input(vec![exp]);
        let result = calculate_regulatory_capital(&input).unwrap();
        let detail = &result.result.exposure_details[0];
        // Cash haircut = 0 => effective collateral = 50,000
        // Adjusted EAD = max(0, 100,000 - 50,000) = 50,000
        // RWA = 50,000 * 1.00 = 50,000
        assert_eq!(detail.rwa, dec!(50_000));
        assert_eq!(detail.crm_benefit, dec!(50_000));
    }

    // -- Test: Govt bond collateral haircut ----------------------------------

    #[test]
    fn test_govt_bond_collateral_haircut() {
        let exp = CreditExposure {
            name: "Govt Bond Secured".to_string(),
            exposure_amount: dec!(100_000),
            asset_class: AssetClass::Corporate,
            risk_weight: None,
            external_rating: Some("BBB".to_string()),
            collateral_value: Some(dec!(50_000)),
            collateral_type: Some(CollateralType::GovernmentBond),
        };
        let input = make_input(vec![exp]);
        let result = calculate_regulatory_capital(&input).unwrap();
        let detail = &result.result.exposure_details[0];
        // Govt bond haircut = 2% => effective collateral = 50,000 * 0.98 = 49,000
        // Adjusted EAD = 100,000 - 49,000 = 51,000
        // RWA = 51,000 * 1.00 = 51,000
        assert_eq!(detail.rwa, dec!(51_000));
        assert_eq!(detail.crm_benefit, dec!(49_000));
    }

    // -- Test: Buffer requirements stack correctly ----------------------------

    #[test]
    fn test_buffer_requirements_stack() {
        let mut input = make_input(vec![simple_exposure(
            "Loan",
            dec!(100_000),
            AssetClass::Other,
        )]);
        input.buffers = Some(CapitalBuffers {
            conservation_buffer: dec!(0.025),
            countercyclical_buffer: dec!(0.01),
            systemic_buffer: dec!(0.02),
        });
        let result = calculate_regulatory_capital(&input).unwrap();
        let buf = &result.result.buffer_requirements;
        assert_eq!(buf.min_cet1, dec!(0.045));
        assert_eq!(buf.conservation, dec!(0.025));
        assert_eq!(buf.countercyclical, dec!(0.01));
        assert_eq!(buf.systemic, dec!(0.02));
        // Total = 4.5% + 2.5% + 1.0% + 2.0% = 10.0%
        assert_eq!(buf.total_cet1_requirement, dec!(0.100));
    }

    // -- Test: Leverage ratio calculation ------------------------------------

    #[test]
    fn test_leverage_ratio() {
        let exp = simple_exposure("Loan", dec!(100_000), AssetClass::Other);
        let input = make_input(vec![exp]);
        let result = calculate_regulatory_capital(&input).unwrap();
        // Tier 1 = CET1 + AT1 - deductions = 10,000 + 2,000 - 1,000 = 11,000
        // Total exposure = 100,000 (credit) + 0 (market) = 100,000
        // Leverage ratio = 11,000 / 100,000 = 0.11
        assert_eq!(result.result.capital_ratios.leverage_ratio, dec!(0.11));
    }

    // -- Test: Multiple exposure classes combined ----------------------------

    #[test]
    fn test_multiple_exposure_classes() {
        let exposures = vec![
            rated_exposure("Sovereign", dec!(50_000), AssetClass::Sovereign, "AAA"),
            rated_exposure("Bank", dec!(30_000), AssetClass::Bank, "A"),
            rated_exposure("Corporate", dec!(40_000), AssetClass::Corporate, "A"),
            simple_exposure("Retail", dec!(20_000), AssetClass::Retail),
            simple_exposure("Mortgage", dec!(60_000), AssetClass::Mortgage),
        ];
        let input = make_input(exposures);
        let result = calculate_regulatory_capital(&input).unwrap();

        // Sovereign AAA: 50,000 * 0% = 0
        // Bank A: 30,000 * 20% = 6,000
        // Corporate A: 40,000 * 50% = 20,000
        // Retail: 20,000 * 75% = 15,000
        // Mortgage: 60,000 * 35% = 21,000
        // Total credit RWA = 62,000
        assert_eq!(result.result.credit_rwa, dec!(62_000));

        assert_eq!(result.result.exposure_details[0].rwa, dec!(0));
        assert_eq!(result.result.exposure_details[1].rwa, dec!(6_000));
        assert_eq!(result.result.exposure_details[2].rwa, dec!(20_000));
        assert_eq!(result.result.exposure_details[3].rwa, dec!(15_000));
        assert_eq!(result.result.exposure_details[4].rwa, dec!(21_000));
    }

    // -- Test: Surplus / deficit calculation ---------------------------------

    #[test]
    fn test_surplus_deficit_calculation() {
        // Capital: CET1=10k, AT1=2k, T2=3k, deductions=1k
        // CET1 capital = 9k, Tier1 = 11k, Total = 14k
        // Exposure: 100k at 100% => credit_rwa = 100k
        // Op risk = 8,250 => total RWA = 108,250
        let exp = simple_exposure("Loan", dec!(100_000), AssetClass::Other);
        let input = make_input(vec![exp]);
        let result = calculate_regulatory_capital(&input).unwrap();

        let total_rwa = result.result.total_rwa;
        let cet1_ratio = dec!(9_000) / total_rwa;
        let tier1_ratio = dec!(11_000) / total_rwa;
        let tc_ratio = dec!(14_000) / total_rwa;

        // Default buffers: conservation = 2.5%, no CCyB, no systemic
        // Total CET1 requirement = 4.5% + 2.5% = 7.0%
        let expected_cet1_surplus = cet1_ratio - dec!(0.070);
        let expected_tier1_surplus = tier1_ratio - dec!(0.06);
        let expected_tc_surplus = tc_ratio - dec!(0.08);

        assert_eq!(
            result.result.surplus_deficit.cet1_surplus,
            expected_cet1_surplus
        );
        assert_eq!(
            result.result.surplus_deficit.tier1_surplus,
            expected_tier1_surplus
        );
        assert_eq!(
            result.result.surplus_deficit.total_capital_surplus,
            expected_tc_surplus
        );
    }

    // -- Test: Capital deductions applied correctly --------------------------

    #[test]
    fn test_capital_deductions_applied() {
        let mut input = make_input(vec![simple_exposure(
            "Loan",
            dec!(100_000),
            AssetClass::Other,
        )]);
        input.capital.deductions = dec!(5_000);
        let result = calculate_regulatory_capital(&input).unwrap();
        let summary = &result.result.capital_summary;
        // CET1 capital = 10,000 - 5,000 = 5,000
        assert_eq!(summary.cet1_capital, dec!(5_000));
        // Tier 1 = 10,000 + 2,000 - 5,000 = 7,000
        assert_eq!(summary.tier1_capital, dec!(7_000));
        // Total = 10,000 + 2,000 + 3,000 - 5,000 = 10,000
        assert_eq!(summary.total_capital, dec!(10_000));
        assert_eq!(summary.deductions, dec!(5_000));
    }

    // -- Test: Unrated corporate => 100% RW ----------------------------------

    #[test]
    fn test_unrated_corporate_100pct() {
        let exp = simple_exposure("Unrated Corp", dec!(60_000), AssetClass::Corporate);
        let input = make_input(vec![exp]);
        let result = calculate_regulatory_capital(&input).unwrap();
        let detail = &result.result.exposure_details[0];
        assert_eq!(detail.risk_weight, dec!(1.00));
        assert_eq!(detail.rwa, dec!(60_000));
    }

    // -- Test: Below BB corporate => 150% RW ---------------------------------

    #[test]
    fn test_below_bb_corporate_150pct() {
        let exp = rated_exposure("Distressed Corp", dec!(40_000), AssetClass::Corporate, "B");
        let input = make_input(vec![exp]);
        let result = calculate_regulatory_capital(&input).unwrap();
        let detail = &result.result.exposure_details[0];
        assert_eq!(detail.risk_weight, dec!(1.50));
        assert_eq!(detail.rwa, dec!(60_000));
    }

    // -- Test: CCC corporate => 150% RW --------------------------------------

    #[test]
    fn test_ccc_corporate_150pct() {
        let exp = rated_exposure("CCC Corp", dec!(20_000), AssetClass::Corporate, "CCC");
        let input = make_input(vec![exp]);
        let result = calculate_regulatory_capital(&input).unwrap();
        let detail = &result.result.exposure_details[0];
        assert_eq!(detail.risk_weight, dec!(1.50));
        assert_eq!(detail.rwa, dec!(30_000));
    }

    // -- Test: Negative CET1 rejected ----------------------------------------

    #[test]
    fn test_negative_cet1_rejected() {
        let mut input = make_input(vec![simple_exposure(
            "Loan",
            dec!(10_000),
            AssetClass::Other,
        )]);
        input.capital.cet1 = dec!(-1);
        let err = calculate_regulatory_capital(&input).unwrap_err();
        match err {
            CorpFinanceError::InvalidInput { field, .. } => {
                assert_eq!(field, "capital.cet1");
            }
            other => panic!("Expected InvalidInput, got {other:?}"),
        }
    }

    // -- Test: Negative exposure amount rejected ------------------------------

    #[test]
    fn test_negative_exposure_rejected() {
        let exp = simple_exposure("Bad", dec!(-100), AssetClass::Other);
        let input = make_input(vec![exp]);
        let err = calculate_regulatory_capital(&input).unwrap_err();
        match err {
            CorpFinanceError::InvalidInput { field, .. } => {
                assert!(field.contains("exposure_amount"));
            }
            other => panic!("Expected InvalidInput, got {other:?}"),
        }
    }

    // -- Test: Risk weight override out of range rejected --------------------

    #[test]
    fn test_risk_weight_override_out_of_range() {
        let exp = CreditExposure {
            name: "Bad RW".to_string(),
            exposure_amount: dec!(10_000),
            asset_class: AssetClass::Other,
            risk_weight: Some(dec!(2.0)), // > 1.5
            external_rating: None,
            collateral_value: None,
            collateral_type: None,
        };
        let input = make_input(vec![exp]);
        let err = calculate_regulatory_capital(&input).unwrap_err();
        match err {
            CorpFinanceError::InvalidInput { field, .. } => {
                assert!(field.contains("risk_weight"));
            }
            other => panic!("Expected InvalidInput, got {other:?}"),
        }
    }

    // -- Test: Risk weight override used when provided -----------------------

    #[test]
    fn test_risk_weight_override() {
        let exp = CreditExposure {
            name: "Custom RW".to_string(),
            exposure_amount: dec!(100_000),
            asset_class: AssetClass::Corporate,
            risk_weight: Some(dec!(0.50)),
            external_rating: Some("BBB".to_string()), // Would be 100% but override is 50%
            collateral_value: None,
            collateral_type: None,
        };
        let input = make_input(vec![exp]);
        let result = calculate_regulatory_capital(&input).unwrap();
        let detail = &result.result.exposure_details[0];
        assert_eq!(detail.risk_weight, dec!(0.50));
        assert_eq!(detail.rwa, dec!(50_000));
    }

    // -- Test: Market risk charge included -----------------------------------

    #[test]
    fn test_market_risk_charge_included() {
        let mut input = make_input(vec![simple_exposure(
            "Loan",
            dec!(100_000),
            AssetClass::Other,
        )]);
        input.market_risk_charge = Some(dec!(15_000));
        let result = calculate_regulatory_capital(&input).unwrap();
        assert_eq!(result.result.market_rwa, dec!(15_000));
        // Total RWA = credit 100k + market 15k + op 8,250 = 123,250
        assert_eq!(result.result.total_rwa, dec!(123_250));
    }

    // -- Test: BIA with negative income year excluded -----------------------

    #[test]
    fn test_bia_negative_income_excluded() {
        let mut input = make_input(vec![simple_exposure(
            "Loan",
            dec!(10_000),
            AssetClass::Other,
        )]);
        input.operational_risk.gross_income_3yr = vec![dec!(100_000), dec!(-20_000), dec!(80_000)];
        let result = calculate_regulatory_capital(&input).unwrap();
        // Avg of positive = (100k + 80k) / 2 = 90k
        // BIA = 15% * 90k = 13,500
        assert_eq!(result.result.operational_rwa, dec!(13_500));
    }

    // -- Test: Equity collateral haircut -------------------------------------

    #[test]
    fn test_equity_collateral_haircut() {
        let exp = CreditExposure {
            name: "Equity Secured".to_string(),
            exposure_amount: dec!(100_000),
            asset_class: AssetClass::Corporate,
            risk_weight: None,
            external_rating: Some("BBB".to_string()),
            collateral_value: Some(dec!(40_000)),
            collateral_type: Some(CollateralType::Equity),
        };
        let input = make_input(vec![exp]);
        let result = calculate_regulatory_capital(&input).unwrap();
        let detail = &result.result.exposure_details[0];
        // Equity haircut = 25% => effective collateral = 40,000 * 0.75 = 30,000
        // Adjusted EAD = 100,000 - 30,000 = 70,000
        // RWA = 70,000 * 1.00 = 70,000
        assert_eq!(detail.rwa, dec!(70_000));
        assert_eq!(detail.crm_benefit, dec!(30_000));
    }

    // -- Test: Tier1 and total capital ratios --------------------------------

    #[test]
    fn test_tier1_and_total_capital_ratios() {
        let exp = simple_exposure("Loan", dec!(100_000), AssetClass::Other);
        let input = make_input(vec![exp]);
        let result = calculate_regulatory_capital(&input).unwrap();

        let total_rwa = result.result.total_rwa;
        // Tier 1 = 10k + 2k - 1k = 11k
        let expected_tier1_ratio = dec!(11_000) / total_rwa;
        // Total capital = 10k + 2k + 3k - 1k = 14k
        let expected_tc_ratio = dec!(14_000) / total_rwa;

        assert_eq!(
            result.result.capital_ratios.tier1_ratio,
            expected_tier1_ratio
        );
        assert_eq!(
            result.result.capital_ratios.total_capital_ratio,
            expected_tc_ratio
        );
    }

    // -- Test: Metadata is populated -----------------------------------------

    #[test]
    fn test_metadata_populated() {
        let input = make_input(vec![simple_exposure(
            "Loan",
            dec!(10_000),
            AssetClass::Other,
        )]);
        let result = calculate_regulatory_capital(&input).unwrap();
        assert!(result.methodology.contains("Basel III"));
        assert_eq!(result.metadata.precision, "rust_decimal_128bit");
        assert!(!result.warnings.is_empty() || result.warnings.is_empty()); // no panic
    }

    // -- Test: Bank rating categories ----------------------------------------

    #[test]
    fn test_bank_rating_risk_weights() {
        // AAA => 20%
        let exp = rated_exposure("Bank AAA", dec!(10_000), AssetClass::Bank, "AAA");
        let mut warnings = Vec::new();
        let detail = calculate_exposure_rwa(&exp, &mut warnings).unwrap();
        assert_eq!(detail.risk_weight, dec!(0.20));

        // BBB => 50%
        let exp = rated_exposure("Bank BBB", dec!(10_000), AssetClass::Bank, "BBB");
        let detail = calculate_exposure_rwa(&exp, &mut warnings).unwrap();
        assert_eq!(detail.risk_weight, dec!(0.50));

        // BB => 100%
        let exp = rated_exposure("Bank BB", dec!(10_000), AssetClass::Bank, "BB");
        let detail = calculate_exposure_rwa(&exp, &mut warnings).unwrap();
        assert_eq!(detail.risk_weight, dec!(1.00));

        // CCC => 150%
        let exp = rated_exposure("Bank CCC", dec!(10_000), AssetClass::Bank, "CCC");
        let detail = calculate_exposure_rwa(&exp, &mut warnings).unwrap();
        assert_eq!(detail.risk_weight, dec!(1.50));
    }

    // -- Test: Sovereign rating categories -----------------------------------

    #[test]
    fn test_sovereign_rating_categories() {
        let mut warnings = Vec::new();

        // A => 20%
        let exp = rated_exposure("Sov A", dec!(10_000), AssetClass::Sovereign, "A");
        let detail = calculate_exposure_rwa(&exp, &mut warnings).unwrap();
        assert_eq!(detail.risk_weight, dec!(0.20));

        // BBB => 50%
        let exp = rated_exposure("Sov BBB", dec!(10_000), AssetClass::Sovereign, "BBB");
        let detail = calculate_exposure_rwa(&exp, &mut warnings).unwrap();
        assert_eq!(detail.risk_weight, dec!(0.50));

        // BB => 100%
        let exp = rated_exposure("Sov BB", dec!(10_000), AssetClass::Sovereign, "BB");
        let detail = calculate_exposure_rwa(&exp, &mut warnings).unwrap();
        assert_eq!(detail.risk_weight, dec!(1.00));

        // CCC => 150%
        let exp = rated_exposure("Sov CCC", dec!(10_000), AssetClass::Sovereign, "CCC");
        let detail = calculate_exposure_rwa(&exp, &mut warnings).unwrap();
        assert_eq!(detail.risk_weight, dec!(1.50));
    }

    // -- Test: Default buffers when none provided ----------------------------

    #[test]
    fn test_default_buffers_when_none() {
        let input = make_input(vec![simple_exposure(
            "Loan",
            dec!(10_000),
            AssetClass::Other,
        )]);
        let result = calculate_regulatory_capital(&input).unwrap();
        let buf = &result.result.buffer_requirements;
        assert_eq!(buf.conservation, dec!(0.025));
        assert_eq!(buf.countercyclical, Decimal::ZERO);
        assert_eq!(buf.systemic, Decimal::ZERO);
        assert_eq!(buf.total_cet1_requirement, dec!(0.070));
    }

    // -- Test: No exposures warns but does not crash -------------------------

    #[test]
    fn test_no_exposures_errors_on_zero_rwa() {
        // With no exposures and potentially zero op risk income, total_rwa could be zero
        let mut input = make_input(vec![]);
        input.operational_risk.gross_income_3yr = vec![dec!(-10)]; // all negative
        let result = calculate_regulatory_capital(&input);
        // Should error on zero total RWA
        assert!(result.is_err());
    }

    // -- Test: Equity exposure 100% risk weight ------------------------------

    #[test]
    fn test_equity_exposure_100pct() {
        let exp = simple_exposure("Listed Equity", dec!(25_000), AssetClass::Equity);
        let input = make_input(vec![exp]);
        let result = calculate_regulatory_capital(&input).unwrap();
        let detail = &result.result.exposure_details[0];
        assert_eq!(detail.risk_weight, dec!(1.00));
        assert_eq!(detail.rwa, dec!(25_000));
    }

    // -- Test: Corporate AA => 20% RW ----------------------------------------

    #[test]
    fn test_corporate_aa_20pct() {
        let exp = rated_exposure("Corp AA", dec!(50_000), AssetClass::Corporate, "AA");
        let input = make_input(vec![exp]);
        let result = calculate_regulatory_capital(&input).unwrap();
        let detail = &result.result.exposure_details[0];
        assert_eq!(detail.risk_weight, dec!(0.20));
        assert_eq!(detail.rwa, dec!(10_000));
    }

    // -- Test: Corporate A => 50% RW -----------------------------------------

    #[test]
    fn test_corporate_a_50pct() {
        let exp = rated_exposure("Corp A", dec!(50_000), AssetClass::Corporate, "A");
        let input = make_input(vec![exp]);
        let result = calculate_regulatory_capital(&input).unwrap();
        let detail = &result.result.exposure_details[0];
        assert_eq!(detail.risk_weight, dec!(0.50));
        assert_eq!(detail.rwa, dec!(25_000));
    }

    // -- Test: Collateral exceeding EAD clamps adjusted EAD to zero ----------

    #[test]
    fn test_collateral_exceeding_ead_clamps_to_zero() {
        let exp = CreditExposure {
            name: "Over-Secured".to_string(),
            exposure_amount: dec!(50_000),
            asset_class: AssetClass::Corporate,
            risk_weight: None,
            external_rating: Some("BBB".to_string()),
            collateral_value: Some(dec!(100_000)),
            collateral_type: Some(CollateralType::Cash),
        };
        let input = make_input(vec![exp]);
        let result = calculate_regulatory_capital(&input).unwrap();
        let detail = &result.result.exposure_details[0];
        // Adjusted EAD = max(0, 50k - 100k) = 0
        assert_eq!(detail.rwa, dec!(0));
        assert_eq!(detail.crm_benefit, dec!(50_000));
    }

    // -- Test: SA with missing business lines errors -------------------------

    #[test]
    fn test_sa_missing_business_lines_rejected() {
        let mut input = make_input(vec![simple_exposure(
            "Loan",
            dec!(10_000),
            AssetClass::Other,
        )]);
        input.operational_risk = OperationalRiskInput {
            approach: OpRiskApproach::Standardised,
            gross_income_3yr: vec![dec!(100_000)],
            business_lines: None,
        };
        let err = calculate_regulatory_capital(&input).unwrap_err();
        match err {
            CorpFinanceError::InvalidInput { field, .. } => {
                assert!(field.contains("business_lines"));
            }
            other => panic!("Expected InvalidInput, got {other:?}"),
        }
    }

    // -- Test: Empty gross income rejected -----------------------------------

    #[test]
    fn test_empty_gross_income_rejected() {
        let mut input = make_input(vec![simple_exposure(
            "Loan",
            dec!(10_000),
            AssetClass::Other,
        )]);
        input.operational_risk.gross_income_3yr = vec![];
        let err = calculate_regulatory_capital(&input).unwrap_err();
        match err {
            CorpFinanceError::InsufficientData(_) => {} // expected
            other => panic!("Expected InsufficientData, got {other:?}"),
        }
    }
}