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

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

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

/// Cash flow from the collateral pool for a single period.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PeriodCashflow {
    /// Period number (1-indexed)
    pub period: u32,
    /// Interest collected from collateral in this period
    pub interest: Money,
    /// Principal collected from collateral in this period
    pub principal: Money,
    /// Losses (defaults) in this period
    pub losses: Money,
}

/// Specification for a single tranche in the structure.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TrancheSpec {
    /// Human-readable name (e.g. "AAA", "BBB", "Equity")
    pub name: String,
    /// Par/face amount of the tranche
    pub balance: Money,
    /// Annual coupon rate (decimal, e.g. 0.05 = 5%)
    pub coupon_rate: Rate,
    /// Seniority: 1 = most senior, higher = more subordinated
    pub seniority: u32,
    /// Whether the coupon is fixed-rate (true) or floating (false)
    pub is_fixed_rate: bool,
    /// Number of coupon payments per year (4 = quarterly, 12 = monthly)
    pub payment_frequency: u32,
}

/// Input for CDO/CLO tranching analysis.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TranchingInput {
    /// Deal name / identifier
    pub deal_name: String,
    /// Total collateral pool balance
    pub collateral_balance: Money,
    /// Period-by-period cash flows from the collateral
    pub collateral_cashflows: Vec<PeriodCashflow>,
    /// Tranches ordered by seniority (will be sorted internally)
    pub tranches: Vec<TrancheSpec>,
    /// Initial cash reserve account balance
    pub reserve_account: Money,
    /// Overcollateralisation trigger ratio (e.g. 1.20 = 120%)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub oc_trigger: Option<Decimal>,
    /// Interest coverage trigger ratio (e.g. 1.05)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ic_trigger: Option<Decimal>,
    /// Months during which principal can be reinvested (CLO feature)
    pub reinvestment_period_months: u32,
}

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

/// Result for a single tranche across the life of the deal.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TrancheResult {
    pub name: String,
    pub original_balance: Money,
    pub ending_balance: Money,
    pub total_interest_received: Money,
    pub total_principal_received: Money,
    pub loss_allocated: Money,
    /// IRR of tranche cash flows
    pub yield_to_maturity: Rate,
    /// Weighted average life in years
    pub weighted_average_life: Decimal,
    /// Subordination percentage (junior tranches / total deal)
    pub credit_enhancement_pct: Rate,
}

/// Subordination level for a single tranche.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SubordinationLevel {
    pub tranche_name: String,
    /// Sum of balances junior to this tranche / total tranche balance
    pub subordination_pct: Rate,
}

/// Credit enhancement metrics for the deal.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreditEnhancement {
    /// Subordination for each tranche
    pub subordination: Vec<SubordinationLevel>,
    /// Initial OC ratio: collateral / total tranches
    pub overcollateralisation_initial: Decimal,
    /// Final OC ratio after waterfall
    pub overcollateralisation_final: Decimal,
    /// Excess spread: WAC of collateral - weighted avg tranche coupon
    pub excess_spread: Rate,
    /// Reserve / collateral balance
    pub reserve_account_pct: Rate,
}

/// Payment detail for a single tranche in a single period.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TranchePayment {
    pub tranche_name: String,
    pub interest_paid: Money,
    pub principal_paid: Money,
    pub interest_shortfall: Money,
}

/// Waterfall detail for a single period.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WaterfallPeriod {
    pub period: u32,
    pub available_interest: Money,
    pub available_principal: Money,
    pub losses: Money,
    pub tranche_payments: Vec<TranchePayment>,
    pub reserve_balance: Money,
    pub oc_test_result: Option<bool>,
    pub ic_test_result: Option<bool>,
}

/// Summary metrics for the entire deal.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DealSummary {
    pub total_collateral: Money,
    pub total_tranches: Money,
    pub excess_collateral: Money,
    pub weighted_avg_tranche_cost: Rate,
    pub total_losses: Money,
    pub total_interest_distributed: Money,
    pub total_principal_distributed: Money,
}

/// Full output of the tranching analysis.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TranchingOutput {
    pub tranche_results: Vec<TrancheResult>,
    pub credit_enhancement: CreditEnhancement,
    pub waterfall_periods: Vec<WaterfallPeriod>,
    pub deal_summary: DealSummary,
}

// ---------------------------------------------------------------------------
// Internal state for waterfall simulation
// ---------------------------------------------------------------------------

/// Internal mutable state for a tranche during waterfall execution.
#[derive(Debug, Clone)]
struct TrancheState {
    name: String,
    seniority: u32,
    original_balance: Money,
    current_balance: Money,
    coupon_rate: Rate,
    payment_frequency: u32,
    total_interest_received: Money,
    total_principal_received: Money,
    loss_allocated: Money,
    /// Per-period cash flows for IRR: negative at t=0 (purchase), positive each period
    cash_flows: Vec<Money>,
    /// For WAL: sum of (period * principal_paid)
    wal_numerator: Decimal,
}

// ---------------------------------------------------------------------------
// Main analysis function
// ---------------------------------------------------------------------------

/// Analyse a CDO/CLO tranching structure with waterfall distribution.
///
/// Runs the full sequential/turbo waterfall for each period of collateral
/// cash flows, applying OC/IC tests, loss allocation, and reinvestment logic.
pub fn analyze_tranching(
    input: &TranchingInput,
) -> CorpFinanceResult<ComputationOutput<TranchingOutput>> {
    let start = Instant::now();
    let mut warnings: Vec<String> = Vec::new();

    // --- Validate inputs ---
    validate_input(input)?;

    // --- Sort tranches by seniority (ascending = most senior first) ---
    let mut sorted_specs: Vec<&TrancheSpec> = input.tranches.iter().collect();
    sorted_specs.sort_by_key(|t| t.seniority);

    let total_tranche_balance: Money = sorted_specs.iter().map(|t| t.balance).sum();

    // --- Build initial tranche state ---
    let mut tranche_states: Vec<TrancheState> = sorted_specs
        .iter()
        .map(|spec| {
            // First cash flow is the negative purchase price (par)
            TrancheState {
                name: spec.name.clone(),
                seniority: spec.seniority,
                original_balance: spec.balance,
                current_balance: spec.balance,
                coupon_rate: spec.coupon_rate,
                payment_frequency: spec.payment_frequency,
                total_interest_received: Decimal::ZERO,
                total_principal_received: Decimal::ZERO,
                loss_allocated: Decimal::ZERO,
                cash_flows: vec![-spec.balance],
                wal_numerator: Decimal::ZERO,
            }
        })
        .collect();

    // --- Compute initial credit enhancement ---
    let subordination = compute_subordination(&sorted_specs, total_tranche_balance);

    let oc_initial = if total_tranche_balance.is_zero() {
        Decimal::ZERO
    } else {
        input.collateral_balance / total_tranche_balance
    };

    let reserve_pct = if input.collateral_balance.is_zero() {
        Decimal::ZERO
    } else {
        input.reserve_account / input.collateral_balance
    };

    // --- Determine periods_per_year from the most senior tranche ---
    let periods_per_year = if sorted_specs.is_empty() {
        Decimal::from(12u32)
    } else {
        let freq = sorted_specs[0].payment_frequency;
        if freq == 0 {
            Decimal::from(12u32)
        } else {
            Decimal::from(freq)
        }
    };

    // --- Waterfall execution ---
    let mut waterfall_periods: Vec<WaterfallPeriod> = Vec::new();
    let mut reserve_balance = input.reserve_account;
    let mut collateral_balance = input.collateral_balance;
    let mut cumulative_losses = Decimal::ZERO;

    for cf in &input.collateral_cashflows {
        let period = cf.period;
        let period_losses = cf.losses;

        // 1. Allocate losses bottom-up
        cumulative_losses += period_losses;
        allocate_losses(&mut tranche_states, period_losses);

        // Update collateral balance
        collateral_balance = collateral_balance - cf.principal - period_losses;

        // 2. Available cash
        let mut available_interest = cf.interest;
        let available_principal = cf.principal;

        // Draw from reserve if interest is insufficient to cover senior coupon
        let total_interest_due: Money = tranche_states
            .iter()
            .filter(|t| t.current_balance > Decimal::ZERO)
            .map(compute_period_coupon)
            .sum();

        if available_interest < total_interest_due && reserve_balance > Decimal::ZERO {
            let shortfall = total_interest_due - available_interest;
            let draw = shortfall.min(reserve_balance);
            available_interest += draw;
            reserve_balance -= draw;
        }

        // 3. OC and IC tests
        let oc_test = input.oc_trigger.map(|trigger| {
            let senior_balance = tranche_states
                .first()
                .map(|t| t.current_balance)
                .unwrap_or(Decimal::ZERO);
            if senior_balance.is_zero() {
                true
            } else {
                let _oc_ratio = (collateral_balance - cumulative_losses
                    + collateral_balance.min(Decimal::ZERO).abs())
                .max(Decimal::ZERO);
                // Simpler: use remaining collateral / senior outstanding
                let effective_collateral = collateral_balance.max(Decimal::ZERO) + reserve_balance;
                let ratio = effective_collateral / senior_balance;
                ratio >= trigger
            }
        });

        let ic_test = input.ic_trigger.map(|trigger| {
            let senior_interest_due = tranche_states
                .first()
                .map(|t| {
                    if t.current_balance > Decimal::ZERO {
                        compute_period_coupon(t)
                    } else {
                        Decimal::ZERO
                    }
                })
                .unwrap_or(Decimal::ZERO);
            if senior_interest_due.is_zero() {
                true
            } else {
                cf.interest / senior_interest_due >= trigger
            }
        });

        let oc_passed = oc_test.unwrap_or(true);
        let ic_passed = ic_test.unwrap_or(true);

        // 4. Interest waterfall (pay in seniority order)
        let mut tranche_payments: Vec<TranchePayment> = Vec::new();
        let mut remaining_interest = available_interest;

        for (idx, state) in tranche_states.iter_mut().enumerate() {
            if state.current_balance <= Decimal::ZERO {
                tranche_payments.push(TranchePayment {
                    tranche_name: state.name.clone(),
                    interest_paid: Decimal::ZERO,
                    principal_paid: Decimal::ZERO,
                    interest_shortfall: Decimal::ZERO,
                });
                continue;
            }

            let interest_due = compute_period_coupon(state);
            let interest_paid = interest_due.min(remaining_interest);
            let interest_shortfall = interest_due - interest_paid;
            remaining_interest -= interest_paid;

            state.total_interest_received += interest_paid;

            tranche_payments.push(TranchePayment {
                tranche_name: state.name.clone(),
                interest_paid,
                principal_paid: Decimal::ZERO, // filled in principal waterfall
                interest_shortfall,
            });

            // If OC or IC test failed, divert remaining junior interest
            // to senior principal acceleration
            if (!oc_passed || !ic_passed) && idx == 0 {
                // After paying senior interest, any interest that would go
                // to junior tranches gets redirected to senior principal
                // We handle this after the interest loop
            }
        }

        // If OC/IC test failed, remaining interest (that would go to juniors)
        // is redirected to senior principal paydown
        let diverted_interest = if !oc_passed || !ic_passed {
            remaining_interest
        } else {
            Decimal::ZERO
        };

        // 5. Principal waterfall
        let in_reinvestment =
            period <= input.reinvestment_period_months && input.reinvestment_period_months > 0;

        let mut remaining_principal = if in_reinvestment {
            // During reinvestment period, principal is reinvested (returned to pool).
            // Only diverted interest (from test failures) is used for principal paydown.
            collateral_balance += available_principal;
            diverted_interest
        } else {
            available_principal + diverted_interest
        };

        // Sequential pay: most senior first
        for (idx, state) in tranche_states.iter_mut().enumerate() {
            if state.current_balance <= Decimal::ZERO || remaining_principal <= Decimal::ZERO {
                continue;
            }

            // If OC test failed and this is not the most senior tranche,
            // skip principal (turbo amortisation directs all to senior)
            if (!oc_passed || !ic_passed) && idx > 0 {
                continue;
            }

            let principal_paid = state.current_balance.min(remaining_principal);
            state.current_balance -= principal_paid;
            state.total_principal_received += principal_paid;
            remaining_principal -= principal_paid;

            // WAL numerator: period * principal_paid
            state.wal_numerator += Decimal::from(period) * principal_paid;

            // Update the payment record
            if let Some(payment) = tranche_payments.get_mut(idx) {
                payment.principal_paid = principal_paid;
            }
        }

        // If OC/IC passed and not in reinvestment, do sequential for remaining tranches
        if oc_passed && ic_passed && !in_reinvestment && remaining_principal > Decimal::ZERO {
            for (idx, state) in tranche_states.iter_mut().enumerate() {
                if state.current_balance <= Decimal::ZERO || remaining_principal <= Decimal::ZERO {
                    continue;
                }
                let principal_paid = state.current_balance.min(remaining_principal);
                state.current_balance -= principal_paid;
                state.total_principal_received += principal_paid;
                remaining_principal -= principal_paid;
                state.wal_numerator += Decimal::from(period) * principal_paid;

                if let Some(payment) = tranche_payments.get_mut(idx) {
                    payment.principal_paid += principal_paid;
                }
            }
        }

        // 6. Replenish reserve from any remaining interest
        if oc_passed && ic_passed && remaining_interest > Decimal::ZERO {
            let max_reserve = input.reserve_account; // cap at initial level
            if reserve_balance < max_reserve {
                let top_up = (max_reserve - reserve_balance).min(remaining_interest);
                reserve_balance += top_up;
                remaining_interest -= top_up;
            }
        }

        // 7. Record per-period cash flows for each tranche (for IRR calc)
        for (idx, state) in tranche_states.iter_mut().enumerate() {
            let payment = &tranche_payments[idx];
            let total_cf = payment.interest_paid + payment.principal_paid;
            state.cash_flows.push(total_cf);
        }

        waterfall_periods.push(WaterfallPeriod {
            period,
            available_interest: cf.interest,
            available_principal: cf.principal,
            losses: period_losses,
            tranche_payments,
            reserve_balance,
            oc_test_result: oc_test,
            ic_test_result: ic_test,
        });
    }

    // --- Compute tranche results ---
    let mut tranche_results: Vec<TrancheResult> = Vec::new();
    for (idx, state) in tranche_states.iter().enumerate() {
        // Credit enhancement: sum of junior tranche original balances / total
        let junior_balance: Money = tranche_states
            .iter()
            .filter(|t| t.seniority > state.seniority)
            .map(|t| t.original_balance)
            .sum();
        let ce_pct = if total_tranche_balance.is_zero() {
            Decimal::ZERO
        } else {
            junior_balance / total_tranche_balance
        };

        // WAL = sum(period_i * principal_i) / (total_principal * periods_per_year)
        // This converts period-denominated time to years.
        let total_principal = state.total_principal_received;
        let tranche_periods_per_year = Decimal::from(
            sorted_specs
                .get(idx)
                .map(|s| s.payment_frequency)
                .unwrap_or(4),
        );
        let wal = if total_principal.is_zero() {
            Decimal::ZERO
        } else {
            state.wal_numerator / (total_principal * tranche_periods_per_year)
        };

        // YTM via Newton-Raphson on tranche cash flows
        let ytm = compute_tranche_irr(&state.cash_flows, periods_per_year, &mut warnings);

        tranche_results.push(TrancheResult {
            name: state.name.clone(),
            original_balance: state.original_balance,
            ending_balance: state.current_balance,
            total_interest_received: state.total_interest_received,
            total_principal_received: state.total_principal_received,
            loss_allocated: state.loss_allocated,
            yield_to_maturity: ytm,
            weighted_average_life: wal,
            credit_enhancement_pct: ce_pct,
        });
    }

    // --- Deal summary ---
    let total_interest_distributed: Money = tranche_results
        .iter()
        .map(|t| t.total_interest_received)
        .sum();
    let total_principal_distributed: Money = tranche_results
        .iter()
        .map(|t| t.total_principal_received)
        .sum();
    let total_losses: Money = tranche_results.iter().map(|t| t.loss_allocated).sum();

    // Weighted average tranche cost
    let weighted_avg_cost = if total_tranche_balance.is_zero() {
        Decimal::ZERO
    } else {
        sorted_specs
            .iter()
            .map(|t| t.balance * t.coupon_rate)
            .sum::<Decimal>()
            / total_tranche_balance
    };

    // Compute excess spread: approx WAC of collateral interest vs tranche cost
    let total_collateral_interest: Money =
        input.collateral_cashflows.iter().map(|c| c.interest).sum();
    let _total_collateral_principal: Money =
        input.collateral_cashflows.iter().map(|c| c.principal).sum();
    let num_periods = Decimal::from(input.collateral_cashflows.len() as u32);
    let wac_estimate = if input.collateral_balance.is_zero() || num_periods.is_zero() {
        Decimal::ZERO
    } else {
        (total_collateral_interest / num_periods) / input.collateral_balance * periods_per_year
    };
    let excess_spread = wac_estimate - weighted_avg_cost;

    // Final OC ratio
    let final_tranche_balance: Money = tranche_states.iter().map(|t| t.current_balance).sum();
    let final_collateral = collateral_balance.max(Decimal::ZERO);
    let oc_final = if final_tranche_balance.is_zero() {
        if final_collateral > Decimal::ZERO {
            dec!(999.99) // fully paid, infinite OC
        } else {
            Decimal::ZERO
        }
    } else {
        final_collateral / final_tranche_balance
    };

    let credit_enhancement = CreditEnhancement {
        subordination,
        overcollateralisation_initial: oc_initial,
        overcollateralisation_final: oc_final,
        excess_spread,
        reserve_account_pct: reserve_pct,
    };

    let deal_summary = DealSummary {
        total_collateral: input.collateral_balance,
        total_tranches: total_tranche_balance,
        excess_collateral: input.collateral_balance - total_tranche_balance,
        weighted_avg_tranche_cost: weighted_avg_cost,
        total_losses,
        total_interest_distributed,
        total_principal_distributed,
    };

    let output = TranchingOutput {
        tranche_results,
        credit_enhancement,
        waterfall_periods,
        deal_summary,
    };

    let elapsed = start.elapsed().as_micros() as u64;
    Ok(with_metadata(
        "CDO/CLO Tranching: waterfall distribution with OC/IC tests",
        &serde_json::json!({
            "deal_name": input.deal_name,
            "collateral_balance": input.collateral_balance.to_string(),
            "num_tranches": input.tranches.len(),
            "num_periods": input.collateral_cashflows.len(),
            "reinvestment_months": input.reinvestment_period_months,
        }),
        warnings,
        elapsed,
        output,
    ))
}

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

/// Validate the tranching input.
fn validate_input(input: &TranchingInput) -> CorpFinanceResult<()> {
    if input.tranches.is_empty() {
        return Err(CorpFinanceError::InvalidInput {
            field: "tranches".into(),
            reason: "At least one tranche is required".into(),
        });
    }

    if input.collateral_cashflows.is_empty() {
        return Err(CorpFinanceError::InvalidInput {
            field: "collateral_cashflows".into(),
            reason: "At least one period of cash flows is required".into(),
        });
    }

    if input.collateral_balance <= Decimal::ZERO {
        return Err(CorpFinanceError::InvalidInput {
            field: "collateral_balance".into(),
            reason: "Collateral balance must be positive".into(),
        });
    }

    let total_tranches: Money = input.tranches.iter().map(|t| t.balance).sum();
    if total_tranches > input.collateral_balance {
        return Err(CorpFinanceError::InvalidInput {
            field: "tranches".into(),
            reason: "Total tranche balance exceeds collateral balance".into(),
        });
    }

    for tranche in &input.tranches {
        if tranche.balance <= Decimal::ZERO {
            return Err(CorpFinanceError::InvalidInput {
                field: format!("tranche[{}].balance", tranche.name),
                reason: "Tranche balance must be positive".into(),
            });
        }
        if tranche.payment_frequency == 0 {
            return Err(CorpFinanceError::InvalidInput {
                field: format!("tranche[{}].payment_frequency", tranche.name),
                reason: "Payment frequency must be > 0".into(),
            });
        }
        if tranche.coupon_rate < Decimal::ZERO {
            return Err(CorpFinanceError::InvalidInput {
                field: format!("tranche[{}].coupon_rate", tranche.name),
                reason: "Coupon rate cannot be negative".into(),
            });
        }
    }

    Ok(())
}

/// Compute the period coupon for a tranche (annual rate / payment_frequency * balance).
fn compute_period_coupon(state: &TrancheState) -> Money {
    if state.payment_frequency == 0 || state.current_balance <= Decimal::ZERO {
        return Decimal::ZERO;
    }
    state.current_balance * state.coupon_rate / Decimal::from(state.payment_frequency)
}

/// Allocate losses bottom-up (most junior tranche absorbs first).
fn allocate_losses(tranche_states: &mut [TrancheState], mut losses: Money) {
    // Iterate from most junior (highest seniority number) to most senior
    for state in tranche_states.iter_mut().rev() {
        if losses <= Decimal::ZERO {
            break;
        }
        let absorbed = losses.min(state.current_balance);
        state.current_balance -= absorbed;
        state.loss_allocated += absorbed;
        losses -= absorbed;
    }
}

/// Compute subordination levels for each tranche.
fn compute_subordination(
    sorted_specs: &[&TrancheSpec],
    total_balance: Money,
) -> Vec<SubordinationLevel> {
    sorted_specs
        .iter()
        .map(|spec| {
            let junior_sum: Money = sorted_specs
                .iter()
                .filter(|t| t.seniority > spec.seniority)
                .map(|t| t.balance)
                .sum();
            let pct = if total_balance.is_zero() {
                Decimal::ZERO
            } else {
                junior_sum / total_balance
            };
            SubordinationLevel {
                tranche_name: spec.name.clone(),
                subordination_pct: pct,
            }
        })
        .collect()
}

/// Compute IRR for a tranche's cash flows using Newton-Raphson.
/// Cash flows are periodic (matching the tranche payment frequency).
/// Returns annualised rate.
fn compute_tranche_irr(
    cash_flows: &[Money],
    periods_per_year: Decimal,
    warnings: &mut Vec<String>,
) -> Rate {
    if cash_flows.len() < 2 {
        return Decimal::ZERO;
    }

    // Check if there are any positive flows
    let has_positive = cash_flows.iter().any(|cf| *cf > Decimal::ZERO);
    let has_negative = cash_flows.iter().any(|cf| *cf < Decimal::ZERO);
    if !has_positive || !has_negative {
        return Decimal::ZERO;
    }

    let max_iterations: u32 = 50;
    let threshold = dec!(0.0000001);
    let mut rate = dec!(0.05) / periods_per_year; // initial guess: 5% annualised

    for iteration in 0..max_iterations {
        let mut npv_val = Decimal::ZERO;
        let mut dnpv = Decimal::ZERO;
        let one_plus_r = Decimal::ONE + rate;

        if one_plus_r <= Decimal::ZERO {
            rate = dec!(0.01);
            continue;
        }

        // Use iterative multiplication for discount factors
        let mut discount = Decimal::ONE;
        for (t, cf) in cash_flows.iter().enumerate() {
            if t > 0 {
                discount *= one_plus_r;
            }
            if discount.is_zero() {
                break;
            }
            npv_val += cf / discount;
            if t > 0 {
                let t_dec = Decimal::from(t as i64);
                dnpv -= t_dec * cf / (discount * one_plus_r);
            }
        }

        if npv_val.abs() < threshold {
            // Annualise the periodic rate
            // (1 + periodic_rate)^periods_per_year - 1
            let annual = (Decimal::ONE + rate).powd(periods_per_year) - Decimal::ONE;
            return annual;
        }

        if dnpv.is_zero() {
            warnings.push(format!(
                "IRR derivative zero at iteration {iteration}, using last estimate"
            ));
            let annual = (Decimal::ONE + rate).powd(periods_per_year) - Decimal::ONE;
            return annual;
        }

        rate -= npv_val / dnpv;

        // Guard against divergence
        if rate < dec!(-0.99) {
            rate = dec!(-0.99);
        } else if rate > dec!(10.0) {
            rate = dec!(10.0);
        }
    }

    warnings.push("Tranche IRR did not converge within 50 iterations".into());
    (Decimal::ONE + rate).powd(periods_per_year) - Decimal::ONE
}

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

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

    // -----------------------------------------------------------------------
    // Test helpers
    // -----------------------------------------------------------------------

    /// Create a simple set of collateral cash flows for testing.
    fn make_cashflows(
        num_periods: u32,
        interest_per_period: Decimal,
        principal_per_period: Decimal,
        loss_per_period: Decimal,
    ) -> Vec<PeriodCashflow> {
        (1..=num_periods)
            .map(|p| PeriodCashflow {
                period: p,
                interest: interest_per_period,
                principal: principal_per_period,
                losses: loss_per_period,
            })
            .collect()
    }

    /// Create a 2-tranche (senior/equity) test input.
    fn two_tranche_input() -> TranchingInput {
        TranchingInput {
            deal_name: "Test 2-Tranche".into(),
            collateral_balance: dec!(1000),
            collateral_cashflows: make_cashflows(4, dec!(25), dec!(200), dec!(0)),
            tranches: vec![
                TrancheSpec {
                    name: "Senior".into(),
                    balance: dec!(800),
                    coupon_rate: dec!(0.04),
                    seniority: 1,
                    is_fixed_rate: true,
                    payment_frequency: 4,
                },
                TrancheSpec {
                    name: "Equity".into(),
                    balance: dec!(150),
                    coupon_rate: dec!(0.10),
                    seniority: 2,
                    is_fixed_rate: true,
                    payment_frequency: 4,
                },
            ],
            reserve_account: dec!(10),
            oc_trigger: None,
            ic_trigger: None,
            reinvestment_period_months: 0,
        }
    }

    /// Create a 3-tranche (AAA/BBB/Equity) test input.
    fn three_tranche_input() -> TranchingInput {
        TranchingInput {
            deal_name: "Test 3-Tranche".into(),
            collateral_balance: dec!(1000),
            collateral_cashflows: make_cashflows(8, dec!(20), dec!(100), dec!(0)),
            tranches: vec![
                TrancheSpec {
                    name: "AAA".into(),
                    balance: dec!(600),
                    coupon_rate: dec!(0.03),
                    seniority: 1,
                    is_fixed_rate: true,
                    payment_frequency: 4,
                },
                TrancheSpec {
                    name: "BBB".into(),
                    balance: dec!(200),
                    coupon_rate: dec!(0.06),
                    seniority: 2,
                    is_fixed_rate: true,
                    payment_frequency: 4,
                },
                TrancheSpec {
                    name: "Equity".into(),
                    balance: dec!(100),
                    coupon_rate: dec!(0.12),
                    seniority: 3,
                    is_fixed_rate: true,
                    payment_frequency: 4,
                },
            ],
            reserve_account: dec!(20),
            oc_trigger: None,
            ic_trigger: None,
            reinvestment_period_months: 0,
        }
    }

    // -----------------------------------------------------------------------
    // Test 1: Simple 2-tranche interest allocation
    // -----------------------------------------------------------------------
    #[test]
    fn test_two_tranche_interest_allocation() {
        let input = two_tranche_input();
        let result = analyze_tranching(&input).unwrap();
        let output = &result.result;

        // Senior gets paid first in each period
        let senior = &output.tranche_results[0];
        let equity = &output.tranche_results[1];

        // Senior coupon per period: 800 * 0.04 / 4 = 8
        // Equity coupon per period: 150 * 0.10 / 4 = 3.75
        // Available interest per period: 25
        // Both should be paid in full (8 + 3.75 = 11.75 < 25)
        assert!(senior.total_interest_received > Decimal::ZERO);
        assert!(equity.total_interest_received > Decimal::ZERO);
    }

    // -----------------------------------------------------------------------
    // Test 2: Simple 2-tranche principal allocation (sequential pay)
    // -----------------------------------------------------------------------
    #[test]
    fn test_two_tranche_principal_sequential_pay() {
        let input = two_tranche_input();
        let result = analyze_tranching(&input).unwrap();
        let output = &result.result;

        // Total principal: 200 * 4 = 800
        // Senior has 800 balance, equity has 150
        // Sequential: senior gets principal first
        let senior = &output.tranche_results[0];
        assert_eq!(senior.original_balance, dec!(800));
        // Senior should receive all 800 in principal
        assert_eq!(senior.total_principal_received, dec!(800));
        assert_eq!(senior.ending_balance, dec!(0));
    }

    // -----------------------------------------------------------------------
    // Test 3: 3-tranche sequential pay
    // -----------------------------------------------------------------------
    #[test]
    fn test_three_tranche_sequential_pay() {
        let input = three_tranche_input();
        let result = analyze_tranching(&input).unwrap();
        let output = &result.result;

        let aaa = &output.tranche_results[0];
        let bbb = &output.tranche_results[1];
        let equity = &output.tranche_results[2];

        // Total principal = 100 * 8 = 800
        // AAA = 600, BBB = 200, Equity = 100
        // Sequential: AAA gets 600 first, then BBB gets 200, equity gets 0
        assert_eq!(aaa.total_principal_received, dec!(600));
        assert_eq!(aaa.ending_balance, dec!(0));
        assert_eq!(bbb.total_principal_received, dec!(200));
        assert_eq!(bbb.ending_balance, dec!(0));
        assert_eq!(equity.total_principal_received, dec!(0));
        assert_eq!(equity.ending_balance, dec!(100));
    }

    // -----------------------------------------------------------------------
    // Test 4: Loss allocation bottom-up
    // -----------------------------------------------------------------------
    #[test]
    fn test_loss_allocation_bottom_up() {
        let mut input = two_tranche_input();
        // Add 50 in losses per period for 4 periods = 200 total losses
        input.collateral_cashflows = make_cashflows(4, dec!(25), dec!(200), dec!(50));

        let result = analyze_tranching(&input).unwrap();
        let output = &result.result;

        let senior = &output.tranche_results[0];
        let equity = &output.tranche_results[1];

        // Equity has 150, so first 150 of losses go to equity, remaining 50 to senior
        assert_eq!(equity.loss_allocated, dec!(150));
        assert_eq!(senior.loss_allocated, dec!(50));
    }

    // -----------------------------------------------------------------------
    // Test 5: Losses absorbed entirely by equity, senior unimpaired
    // -----------------------------------------------------------------------
    #[test]
    fn test_losses_absorbed_by_equity_only() {
        let mut input = two_tranche_input();
        // 30 total losses (well within equity's 150 capacity)
        input.collateral_cashflows = make_cashflows(4, dec!(25), dec!(200), dec!(7.5));

        let result = analyze_tranching(&input).unwrap();
        let output = &result.result;

        let senior = &output.tranche_results[0];
        let equity = &output.tranche_results[1];

        assert_eq!(senior.loss_allocated, dec!(0));
        assert_eq!(equity.loss_allocated, dec!(30));
    }

    // -----------------------------------------------------------------------
    // Test 6: Losses exceeding equity, impairing mezzanine
    // -----------------------------------------------------------------------
    #[test]
    fn test_losses_exceed_equity_impair_mezzanine() {
        let mut input = three_tranche_input();
        // 200 total losses: 100 equity + 100 from BBB
        input.collateral_cashflows = make_cashflows(4, dec!(20), dec!(100), dec!(50));

        let result = analyze_tranching(&input).unwrap();
        let output = &result.result;

        let aaa = &output.tranche_results[0];
        let bbb = &output.tranche_results[1];
        let equity = &output.tranche_results[2];

        assert_eq!(equity.loss_allocated, dec!(100));
        assert_eq!(bbb.loss_allocated, dec!(100));
        assert_eq!(aaa.loss_allocated, dec!(0));
    }

    // -----------------------------------------------------------------------
    // Test 7: OC test trigger and turbo amortisation
    // -----------------------------------------------------------------------
    #[test]
    fn test_oc_trigger_turbo_amortisation() {
        let input = TranchingInput {
            deal_name: "OC Test Deal".into(),
            collateral_balance: dec!(1000),
            // 8 periods, moderate losses to trigger OC failure
            collateral_cashflows: make_cashflows(8, dec!(20), dec!(100), dec!(30)),
            tranches: vec![
                TrancheSpec {
                    name: "Senior".into(),
                    balance: dec!(700),
                    coupon_rate: dec!(0.04),
                    seniority: 1,
                    is_fixed_rate: true,
                    payment_frequency: 4,
                },
                TrancheSpec {
                    name: "Junior".into(),
                    balance: dec!(200),
                    coupon_rate: dec!(0.08),
                    seniority: 2,
                    is_fixed_rate: true,
                    payment_frequency: 4,
                },
            ],
            reserve_account: dec!(10),
            oc_trigger: Some(dec!(1.50)), // high trigger to force failure
            ic_trigger: None,
            reinvestment_period_months: 0,
        };

        let result = analyze_tranching(&input).unwrap();
        let output = &result.result;

        // With a high OC trigger, the test should fail in some periods
        let some_failed = output
            .waterfall_periods
            .iter()
            .any(|wp| wp.oc_test_result == Some(false));
        assert!(some_failed, "OC test should fail in at least one period");

        // When OC fails, principal should flow to senior (turbo)
        let senior = &output.tranche_results[0];
        assert!(
            senior.total_principal_received > Decimal::ZERO,
            "Senior should receive principal via turbo"
        );
    }

    // -----------------------------------------------------------------------
    // Test 8: IC test trigger with interest diversion
    // -----------------------------------------------------------------------
    #[test]
    fn test_ic_trigger_interest_diversion() {
        let input = TranchingInput {
            deal_name: "IC Test Deal".into(),
            collateral_balance: dec!(1000),
            // Very low interest to trigger IC failure
            collateral_cashflows: make_cashflows(4, dec!(5), dec!(200), dec!(0)),
            tranches: vec![
                TrancheSpec {
                    name: "Senior".into(),
                    balance: dec!(800),
                    coupon_rate: dec!(0.04),
                    seniority: 1,
                    is_fixed_rate: true,
                    payment_frequency: 4,
                },
                TrancheSpec {
                    name: "Equity".into(),
                    balance: dec!(100),
                    coupon_rate: dec!(0.10),
                    seniority: 2,
                    is_fixed_rate: true,
                    payment_frequency: 4,
                },
            ],
            reserve_account: dec!(10),
            oc_trigger: None,
            ic_trigger: Some(dec!(2.0)), // High IC trigger to force failure
            reinvestment_period_months: 0,
        };

        let result = analyze_tranching(&input).unwrap();
        let output = &result.result;

        // IC test: interest collected / senior interest due >= 2.0
        // Senior interest due = 800 * 0.04 / 4 = 8, available = 5
        // 5 / 8 = 0.625 < 2.0 => fail
        let failed = output
            .waterfall_periods
            .iter()
            .any(|wp| wp.ic_test_result == Some(false));
        assert!(failed, "IC test should fail");
    }

    // -----------------------------------------------------------------------
    // Test 9: Credit enhancement / subordination percentages
    // -----------------------------------------------------------------------
    #[test]
    fn test_credit_enhancement_subordination() {
        let input = three_tranche_input();
        let result = analyze_tranching(&input).unwrap();
        let ce = &result.result.credit_enhancement;

        // AAA subordination: (BBB 200 + Equity 100) / 900 = 33.33%
        let aaa_sub = ce
            .subordination
            .iter()
            .find(|s| s.tranche_name == "AAA")
            .unwrap();
        let expected = dec!(300) / dec!(900);
        assert!(
            (aaa_sub.subordination_pct - expected).abs() < dec!(0.001),
            "AAA subordination should be ~33.3%, got {}",
            aaa_sub.subordination_pct
        );

        // BBB subordination: Equity 100 / 900 = 11.11%
        let bbb_sub = ce
            .subordination
            .iter()
            .find(|s| s.tranche_name == "BBB")
            .unwrap();
        let expected_bbb = dec!(100) / dec!(900);
        assert!(
            (bbb_sub.subordination_pct - expected_bbb).abs() < dec!(0.001),
            "BBB subordination should be ~11.1%, got {}",
            bbb_sub.subordination_pct
        );

        // Equity subordination: 0%
        let eq_sub = ce
            .subordination
            .iter()
            .find(|s| s.tranche_name == "Equity")
            .unwrap();
        assert_eq!(eq_sub.subordination_pct, dec!(0));
    }

    // -----------------------------------------------------------------------
    // Test 10: Credit enhancement in tranche results
    // -----------------------------------------------------------------------
    #[test]
    fn test_tranche_result_credit_enhancement_pct() {
        let input = three_tranche_input();
        let result = analyze_tranching(&input).unwrap();

        let aaa = &result.result.tranche_results[0];
        let bbb = &result.result.tranche_results[1];
        let equity = &result.result.tranche_results[2];

        // AAA: (200 + 100) / 900 = 33.33%
        assert!((aaa.credit_enhancement_pct - dec!(300) / dec!(900)).abs() < dec!(0.001));
        // BBB: 100 / 900 = 11.11%
        assert!((bbb.credit_enhancement_pct - dec!(100) / dec!(900)).abs() < dec!(0.001));
        // Equity: 0%
        assert_eq!(equity.credit_enhancement_pct, dec!(0));
    }

    // -----------------------------------------------------------------------
    // Test 11: Reinvestment period behaviour
    // -----------------------------------------------------------------------
    #[test]
    fn test_reinvestment_period() {
        let mut input = two_tranche_input();
        input.reinvestment_period_months = 2; // First 2 periods reinvest principal

        let result = analyze_tranching(&input).unwrap();
        let output = &result.result;

        // During reinvestment (periods 1-2), principal should not go to tranches
        // (it's reinvested back into the pool)
        let p1_payments = &output.waterfall_periods[0].tranche_payments;
        let p2_payments = &output.waterfall_periods[1].tranche_payments;

        let p1_principal: Decimal = p1_payments.iter().map(|p| p.principal_paid).sum();
        let p2_principal: Decimal = p2_payments.iter().map(|p| p.principal_paid).sum();

        assert_eq!(p1_principal, dec!(0), "Period 1 should reinvest principal");
        assert_eq!(p2_principal, dec!(0), "Period 2 should reinvest principal");

        // After reinvestment, principal should flow to tranches
        let p3_payments = &output.waterfall_periods[2].tranche_payments;
        let p3_principal: Decimal = p3_payments.iter().map(|p| p.principal_paid).sum();
        assert!(
            p3_principal > Decimal::ZERO,
            "Period 3 should distribute principal"
        );
    }

    // -----------------------------------------------------------------------
    // Test 12: WAL calculation
    // -----------------------------------------------------------------------
    #[test]
    fn test_wal_calculation() {
        let input = two_tranche_input();
        let result = analyze_tranching(&input).unwrap();

        let senior = &result.result.tranche_results[0];
        // Senior gets all 800 in principal over 4 quarterly periods
        // WAL should be > 0 and reasonable (measured in years)
        assert!(
            senior.weighted_average_life > Decimal::ZERO,
            "Senior WAL should be positive"
        );
        // With quarterly payments over 4 periods (1 year), WAL should be roughly 0.5-1 years
        assert!(
            senior.weighted_average_life < dec!(2),
            "Senior WAL should be less than 2 years, got {}",
            senior.weighted_average_life
        );
    }

    // -----------------------------------------------------------------------
    // Test 13: YTM/IRR calculation for tranches
    // -----------------------------------------------------------------------
    #[test]
    fn test_tranche_ytm_positive() {
        let input = two_tranche_input();
        let result = analyze_tranching(&input).unwrap();

        let senior = &result.result.tranche_results[0];
        // Senior tranche is paid in full with coupon, so YTM should be close to coupon rate
        assert!(
            senior.yield_to_maturity > Decimal::ZERO,
            "Senior YTM should be positive, got {}",
            senior.yield_to_maturity
        );
        // Should be close to the 4% coupon
        assert!(
            (senior.yield_to_maturity - dec!(0.04)).abs() < dec!(0.02),
            "Senior YTM should be close to 4%, got {}",
            senior.yield_to_maturity
        );
    }

    // -----------------------------------------------------------------------
    // Test 14: Deal summary totals
    // -----------------------------------------------------------------------
    #[test]
    fn test_deal_summary_totals() {
        let input = two_tranche_input();
        let result = analyze_tranching(&input).unwrap();
        let summary = &result.result.deal_summary;

        assert_eq!(summary.total_collateral, dec!(1000));
        assert_eq!(summary.total_tranches, dec!(950)); // 800 + 150
        assert_eq!(summary.excess_collateral, dec!(50));
        assert_eq!(summary.total_losses, dec!(0));

        // Total interest + principal distributed should be positive
        assert!(summary.total_interest_distributed > Decimal::ZERO);
        assert!(summary.total_principal_distributed > Decimal::ZERO);
    }

    // -----------------------------------------------------------------------
    // Test 15: Zero losses scenario — all tranches paid in full
    // -----------------------------------------------------------------------
    #[test]
    fn test_zero_losses_all_paid() {
        // Enough principal to pay all tranches
        let input = TranchingInput {
            deal_name: "Zero Loss Deal".into(),
            collateral_balance: dec!(500),
            collateral_cashflows: make_cashflows(4, dec!(10), dec!(100), dec!(0)),
            tranches: vec![
                TrancheSpec {
                    name: "Senior".into(),
                    balance: dec!(300),
                    coupon_rate: dec!(0.04),
                    seniority: 1,
                    is_fixed_rate: true,
                    payment_frequency: 4,
                },
                TrancheSpec {
                    name: "Junior".into(),
                    balance: dec!(100),
                    coupon_rate: dec!(0.08),
                    seniority: 2,
                    is_fixed_rate: true,
                    payment_frequency: 4,
                },
            ],
            reserve_account: dec!(5),
            oc_trigger: None,
            ic_trigger: None,
            reinvestment_period_months: 0,
        };

        let result = analyze_tranching(&input).unwrap();
        let output = &result.result;

        for tr in &output.tranche_results {
            assert_eq!(
                tr.loss_allocated,
                dec!(0),
                "{} should have no losses",
                tr.name
            );
            assert_eq!(
                tr.ending_balance,
                dec!(0),
                "{} should be fully paid down",
                tr.name
            );
        }
    }

    // -----------------------------------------------------------------------
    // Test 16: Excess spread calculation
    // -----------------------------------------------------------------------
    #[test]
    fn test_excess_spread() {
        let input = two_tranche_input();
        let result = analyze_tranching(&input).unwrap();
        let ce = &result.result.credit_enhancement;

        // Excess spread = collateral WAC - weighted avg tranche cost
        // Should be some positive value for an overcollateralised deal
        // WAC estimate from collateral: 25 per quarter on 1000 = 10% annualised
        // Tranche cost: (800*0.04 + 150*0.10)/950 ≈ 0.0494
        // Excess spread ≈ 0.10 - 0.0494 ≈ 0.05+
        assert!(
            ce.excess_spread > Decimal::ZERO,
            "Excess spread should be positive, got {}",
            ce.excess_spread
        );
    }

    // -----------------------------------------------------------------------
    // Test 17: Reserve account drawdown
    // -----------------------------------------------------------------------
    #[test]
    fn test_reserve_account_drawdown() {
        // Create scenario where interest is just barely enough
        let input = TranchingInput {
            deal_name: "Reserve Draw Deal".into(),
            collateral_balance: dec!(1000),
            // Very low interest to force reserve draw
            collateral_cashflows: make_cashflows(4, dec!(6), dec!(200), dec!(0)),
            tranches: vec![
                TrancheSpec {
                    name: "Senior".into(),
                    balance: dec!(800),
                    coupon_rate: dec!(0.04),
                    seniority: 1,
                    is_fixed_rate: true,
                    payment_frequency: 4,
                },
                TrancheSpec {
                    name: "Equity".into(),
                    balance: dec!(100),
                    coupon_rate: dec!(0.10),
                    seniority: 2,
                    is_fixed_rate: true,
                    payment_frequency: 4,
                },
            ],
            reserve_account: dec!(50),
            oc_trigger: None,
            ic_trigger: None,
            reinvestment_period_months: 0,
        };

        let result = analyze_tranching(&input).unwrap();
        let output = &result.result;

        // Senior coupon = 800 * 0.04 / 4 = 8, Equity = 100 * 0.10 / 4 = 2.50
        // Total due = 10.5, available = 6 => shortfall = 4.5 => draw from reserve
        // After first period, reserve should be < 50
        let first_period_reserve = output.waterfall_periods[0].reserve_balance;
        assert!(
            first_period_reserve < dec!(50),
            "Reserve should be drawn down, got {}",
            first_period_reserve
        );
    }

    // -----------------------------------------------------------------------
    // Test 18: Overcollateralisation initial ratio
    // -----------------------------------------------------------------------
    #[test]
    fn test_oc_initial_ratio() {
        let input = two_tranche_input();
        let result = analyze_tranching(&input).unwrap();
        let ce = &result.result.credit_enhancement;

        // OC initial = 1000 / (800 + 150) = 1000/950 ≈ 1.0526
        let expected = dec!(1000) / dec!(950);
        assert!(
            (ce.overcollateralisation_initial - expected).abs() < dec!(0.001),
            "OC initial should be ~1.053, got {}",
            ce.overcollateralisation_initial
        );
    }

    // -----------------------------------------------------------------------
    // Test 19: Reserve account percentage
    // -----------------------------------------------------------------------
    #[test]
    fn test_reserve_account_pct() {
        let input = two_tranche_input();
        let result = analyze_tranching(&input).unwrap();
        let ce = &result.result.credit_enhancement;

        // Reserve pct = 10 / 1000 = 0.01
        assert_eq!(ce.reserve_account_pct, dec!(0.01));
    }

    // -----------------------------------------------------------------------
    // Test 20: Validation — no tranches
    // -----------------------------------------------------------------------
    #[test]
    fn test_validation_no_tranches() {
        let input = TranchingInput {
            deal_name: "Empty".into(),
            collateral_balance: dec!(1000),
            collateral_cashflows: make_cashflows(4, dec!(25), dec!(200), dec!(0)),
            tranches: vec![],
            reserve_account: dec!(0),
            oc_trigger: None,
            ic_trigger: None,
            reinvestment_period_months: 0,
        };

        let result = analyze_tranching(&input);
        assert!(result.is_err());
    }

    // -----------------------------------------------------------------------
    // Test 21: Validation — no cashflows
    // -----------------------------------------------------------------------
    #[test]
    fn test_validation_no_cashflows() {
        let input = TranchingInput {
            deal_name: "No CFs".into(),
            collateral_balance: dec!(1000),
            collateral_cashflows: vec![],
            tranches: vec![TrancheSpec {
                name: "A".into(),
                balance: dec!(800),
                coupon_rate: dec!(0.04),
                seniority: 1,
                is_fixed_rate: true,
                payment_frequency: 4,
            }],
            reserve_account: dec!(0),
            oc_trigger: None,
            ic_trigger: None,
            reinvestment_period_months: 0,
        };

        assert!(analyze_tranching(&input).is_err());
    }

    // -----------------------------------------------------------------------
    // Test 22: Validation — tranche balance exceeds collateral
    // -----------------------------------------------------------------------
    #[test]
    fn test_validation_tranches_exceed_collateral() {
        let input = TranchingInput {
            deal_name: "Oversize".into(),
            collateral_balance: dec!(100),
            collateral_cashflows: make_cashflows(1, dec!(5), dec!(50), dec!(0)),
            tranches: vec![TrancheSpec {
                name: "A".into(),
                balance: dec!(200),
                coupon_rate: dec!(0.04),
                seniority: 1,
                is_fixed_rate: true,
                payment_frequency: 4,
            }],
            reserve_account: dec!(0),
            oc_trigger: None,
            ic_trigger: None,
            reinvestment_period_months: 0,
        };

        assert!(analyze_tranching(&input).is_err());
    }

    // -----------------------------------------------------------------------
    // Test 23: Waterfall period count matches input
    // -----------------------------------------------------------------------
    #[test]
    fn test_waterfall_period_count() {
        let input = two_tranche_input();
        let result = analyze_tranching(&input).unwrap();
        assert_eq!(result.result.waterfall_periods.len(), 4);
    }

    // -----------------------------------------------------------------------
    // Test 24: Interest shortfall tracked correctly
    // -----------------------------------------------------------------------
    #[test]
    fn test_interest_shortfall() {
        let input = TranchingInput {
            deal_name: "Shortfall Deal".into(),
            collateral_balance: dec!(1000),
            // Very low interest: 2 per period
            collateral_cashflows: make_cashflows(4, dec!(2), dec!(200), dec!(0)),
            tranches: vec![
                TrancheSpec {
                    name: "Senior".into(),
                    balance: dec!(800),
                    coupon_rate: dec!(0.04),
                    seniority: 1,
                    is_fixed_rate: true,
                    payment_frequency: 4,
                },
                TrancheSpec {
                    name: "Equity".into(),
                    balance: dec!(100),
                    coupon_rate: dec!(0.10),
                    seniority: 2,
                    is_fixed_rate: true,
                    payment_frequency: 4,
                },
            ],
            reserve_account: dec!(0), // no reserve to draw from
            oc_trigger: None,
            ic_trigger: None,
            reinvestment_period_months: 0,
        };

        let result = analyze_tranching(&input).unwrap();
        let first_period = &result.result.waterfall_periods[0];

        // Senior due: 800 * 0.04 / 4 = 8, available = 2, shortfall = 6
        let senior_payment = &first_period.tranche_payments[0];
        assert!(
            senior_payment.interest_shortfall > Decimal::ZERO,
            "Senior should have interest shortfall"
        );
    }

    // -----------------------------------------------------------------------
    // Test 25: Weighted average tranche cost
    // -----------------------------------------------------------------------
    #[test]
    fn test_weighted_avg_tranche_cost() {
        let input = two_tranche_input();
        let result = analyze_tranching(&input).unwrap();
        let summary = &result.result.deal_summary;

        // WAC = (800*0.04 + 150*0.10) / 950 = (32 + 15) / 950 = 47/950 ≈ 0.04947
        let expected = dec!(47) / dec!(950);
        assert!(
            (summary.weighted_avg_tranche_cost - expected).abs() < dec!(0.001),
            "Weighted avg cost should be ~4.95%, got {}",
            summary.weighted_avg_tranche_cost
        );
    }

    // -----------------------------------------------------------------------
    // Test 26: Methodology and metadata present
    // -----------------------------------------------------------------------
    #[test]
    fn test_methodology_metadata() {
        let input = two_tranche_input();
        let result = analyze_tranching(&input).unwrap();

        assert!(result.methodology.contains("CDO/CLO Tranching"));
        assert!(!result.metadata.version.is_empty());
    }

    // -----------------------------------------------------------------------
    // Test 27: Seniority ordering is respected regardless of input order
    // -----------------------------------------------------------------------
    #[test]
    fn test_seniority_ordering_independent_of_input() {
        let mut input = three_tranche_input();
        // Reverse the input order
        input.tranches.reverse();

        let result = analyze_tranching(&input).unwrap();
        let output = &result.result;

        // Results should still be AAA first (seniority 1)
        assert_eq!(output.tranche_results[0].name, "AAA");
        assert_eq!(output.tranche_results[1].name, "BBB");
        assert_eq!(output.tranche_results[2].name, "Equity");
    }

    // -----------------------------------------------------------------------
    // Test 28: Equity tranche has zero subordination
    // -----------------------------------------------------------------------
    #[test]
    fn test_equity_zero_subordination() {
        let input = three_tranche_input();
        let result = analyze_tranching(&input).unwrap();

        let equity = &result.result.tranche_results[2];
        assert_eq!(equity.credit_enhancement_pct, dec!(0));
    }

    // -----------------------------------------------------------------------
    // Test 29: Large loss scenario wipes all tranches
    // -----------------------------------------------------------------------
    #[test]
    fn test_total_wipeout() {
        let input = TranchingInput {
            deal_name: "Wipeout".into(),
            collateral_balance: dec!(1000),
            // 250 loss per period * 4 = 1000 total losses
            collateral_cashflows: make_cashflows(4, dec!(10), dec!(0), dec!(250)),
            tranches: vec![
                TrancheSpec {
                    name: "Senior".into(),
                    balance: dec!(600),
                    coupon_rate: dec!(0.04),
                    seniority: 1,
                    is_fixed_rate: true,
                    payment_frequency: 4,
                },
                TrancheSpec {
                    name: "Junior".into(),
                    balance: dec!(200),
                    coupon_rate: dec!(0.08),
                    seniority: 2,
                    is_fixed_rate: true,
                    payment_frequency: 4,
                },
                TrancheSpec {
                    name: "Equity".into(),
                    balance: dec!(100),
                    coupon_rate: dec!(0.12),
                    seniority: 3,
                    is_fixed_rate: true,
                    payment_frequency: 4,
                },
            ],
            reserve_account: dec!(0),
            oc_trigger: None,
            ic_trigger: None,
            reinvestment_period_months: 0,
        };

        let result = analyze_tranching(&input).unwrap();
        let summary = &result.result.deal_summary;

        // Total losses = 1000, all tranches total = 900
        // All tranches should be wiped: equity 100, junior 200, senior 600 -> only 900 absorbed
        assert_eq!(summary.total_losses, dec!(900));
    }

    // -----------------------------------------------------------------------
    // Test 30: Validation — negative collateral balance
    // -----------------------------------------------------------------------
    #[test]
    fn test_validation_negative_collateral() {
        let input = TranchingInput {
            deal_name: "Negative".into(),
            collateral_balance: dec!(-100),
            collateral_cashflows: make_cashflows(1, dec!(5), dec!(50), dec!(0)),
            tranches: vec![TrancheSpec {
                name: "A".into(),
                balance: dec!(50),
                coupon_rate: dec!(0.04),
                seniority: 1,
                is_fixed_rate: true,
                payment_frequency: 4,
            }],
            reserve_account: dec!(0),
            oc_trigger: None,
            ic_trigger: None,
            reinvestment_period_months: 0,
        };

        assert!(analyze_tranching(&input).is_err());
    }
}