corp-finance-core 1.1.0

Institutional-grade corporate finance calculations with 128-bit decimal precision — DCF, WACC, comps, LBO, credit metrics, derivatives, fixed income, options, and 60+ specialty modules. No f64 in financials. WASM-compatible.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
use crate::compat::Instant;
use rust_decimal::Decimal;
use rust_decimal_macros::dec;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};

use crate::error::CorpFinanceError;
use crate::types::*;
use crate::CorpFinanceResult;

// ---------------------------------------------------------------------------
// Public types
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum NodeType {
    Decision,
    Chance,
    Terminal,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TreeNode {
    pub id: String,
    pub name: String,
    pub node_type: NodeType,
    /// Payoff for terminal nodes
    pub value: Option<Decimal>,
    /// Cost incurred at this node
    pub cost: Option<Decimal>,
    /// Probability for chance node children
    pub probability: Option<Decimal>,
    /// IDs of child nodes
    pub children: Vec<String>,
    /// Time period for discounting (year)
    pub time_period: Option<u32>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DecisionTreeInput {
    pub nodes: Vec<TreeNode>,
    /// Discount rate for NPV of outcomes
    pub discount_rate: Decimal,
    /// Certainty equivalent adjustment factor
    pub risk_adjustment: Option<Decimal>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NodeValuation {
    pub id: String,
    pub name: String,
    pub value: Decimal,
    pub optimal_choice: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SensitivityResult {
    pub node_id: String,
    pub base_value: Decimal,
    pub high_value: Decimal,
    pub low_value: Decimal,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DecisionSummary {
    pub decision_node: String,
    pub chosen_branch: String,
    pub chosen_value: Decimal,
    pub alternatives: Vec<(String, Decimal)>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DecisionTreeOutput {
    /// EMV of optimal strategy
    pub expected_monetary_value: Decimal,
    /// Risk-adjusted value if risk_adjustment provided
    pub risk_adjusted_value: Option<Decimal>,
    /// Sequence of node IDs for optimal decisions
    pub optimal_path: Vec<String>,
    /// Human-readable path
    pub optimal_path_names: Vec<String>,
    /// Value at each node
    pub node_values: Vec<NodeValuation>,
    /// Sensitivity analysis for chance nodes
    pub sensitivity: Vec<SensitivityResult>,
    /// EVPI: expected value of perfect information
    pub value_of_perfect_information: Decimal,
    /// At each decision node, which branch is optimal
    pub decision_summary: Vec<DecisionSummary>,
}

// ---------------------------------------------------------------------------
// Decimal helpers
// ---------------------------------------------------------------------------

fn abs_decimal(x: Decimal) -> Decimal {
    if x < Decimal::ZERO {
        -x
    } else {
        x
    }
}

/// Discount factor using iterative multiplication: (1+r)^(-t)
fn discount_factor(rate: Decimal, periods: u32) -> Decimal {
    if periods == 0 || rate == Decimal::ZERO {
        return Decimal::ONE;
    }
    let growth = Decimal::ONE + rate;
    let mut factor = Decimal::ONE;
    for _ in 0..periods {
        factor *= growth;
    }
    if factor == Decimal::ZERO {
        return Decimal::ZERO;
    }
    Decimal::ONE / factor
}

/// Risk adjustment factor: risk_adj^time_period
fn risk_factor(risk_adj: Decimal, periods: u32) -> Decimal {
    if periods == 0 {
        return Decimal::ONE;
    }
    let mut factor = Decimal::ONE;
    for _ in 0..periods {
        factor *= risk_adj;
    }
    factor
}

// ---------------------------------------------------------------------------
// Validation
// ---------------------------------------------------------------------------

fn validate_tree(input: &DecisionTreeInput) -> CorpFinanceResult<()> {
    if input.nodes.is_empty() {
        return Err(CorpFinanceError::InsufficientData(
            "Decision tree must have at least one node".into(),
        ));
    }

    // Build ID set and index
    let mut id_set = HashSet::new();
    let node_map: HashMap<&str, &TreeNode> =
        input.nodes.iter().map(|n| (n.id.as_str(), n)).collect();

    for node in &input.nodes {
        if !id_set.insert(&node.id) {
            return Err(CorpFinanceError::InvalidInput {
                field: "nodes".into(),
                reason: format!("Duplicate node ID: {}", node.id),
            });
        }
    }

    for node in &input.nodes {
        // Terminal nodes must have a value
        if node.node_type == NodeType::Terminal && node.value.is_none() {
            return Err(CorpFinanceError::InvalidInput {
                field: format!("node[{}].value", node.id),
                reason: "Terminal nodes must have a value".into(),
            });
        }

        // Terminal nodes should not have children
        if node.node_type == NodeType::Terminal && !node.children.is_empty() {
            return Err(CorpFinanceError::InvalidInput {
                field: format!("node[{}].children", node.id),
                reason: "Terminal nodes should not have children".into(),
            });
        }

        // Non-terminal nodes should have children
        if node.node_type != NodeType::Terminal && node.children.is_empty() {
            return Err(CorpFinanceError::InvalidInput {
                field: format!("node[{}].children", node.id),
                reason: "Non-terminal nodes must have children".into(),
            });
        }

        // All child IDs must exist
        for child_id in &node.children {
            if !node_map.contains_key(child_id.as_str()) {
                return Err(CorpFinanceError::InvalidInput {
                    field: format!("node[{}].children", node.id),
                    reason: format!("Child ID {} does not exist", child_id),
                });
            }
        }

        // Chance node children should have probabilities
        if node.node_type == NodeType::Chance {
            let mut prob_sum = Decimal::ZERO;
            for child_id in &node.children {
                let child = node_map[child_id.as_str()];
                match child.probability {
                    Some(p) => {
                        if p < Decimal::ZERO || p > Decimal::ONE {
                            return Err(CorpFinanceError::InvalidInput {
                                field: format!("node[{}].probability", child.id),
                                reason: "Probability must be between 0 and 1".into(),
                            });
                        }
                        prob_sum += p;
                    }
                    None => {
                        return Err(CorpFinanceError::InvalidInput {
                            field: format!("node[{}].probability", child.id),
                            reason: "Children of chance nodes must have probabilities".into(),
                        });
                    }
                }
            }
            // Probabilities should sum to ~1
            if abs_decimal(prob_sum - Decimal::ONE) > dec!(0.01) {
                return Err(CorpFinanceError::InvalidInput {
                    field: format!("node[{}].children", node.id),
                    reason: format!(
                        "Chance node child probabilities sum to {} (expected ~1.0)",
                        prob_sum
                    ),
                });
            }
        }
    }

    // Check for cycles using DFS from the root
    let root_id = &input.nodes[0].id;
    let mut visited = HashSet::new();
    let mut stack = HashSet::new();
    if has_cycle(root_id, &node_map, &mut visited, &mut stack) {
        return Err(CorpFinanceError::InvalidInput {
            field: "nodes".into(),
            reason: "Decision tree contains a cycle".into(),
        });
    }

    Ok(())
}

fn has_cycle(
    node_id: &str,
    node_map: &HashMap<&str, &TreeNode>,
    visited: &mut HashSet<String>,
    stack: &mut HashSet<String>,
) -> bool {
    if stack.contains(node_id) {
        return true;
    }
    if visited.contains(node_id) {
        return false;
    }

    visited.insert(node_id.to_string());
    stack.insert(node_id.to_string());

    if let Some(node) = node_map.get(node_id) {
        for child_id in &node.children {
            if has_cycle(child_id, node_map, visited, stack) {
                return true;
            }
        }
    }

    stack.remove(node_id);
    false
}

// ---------------------------------------------------------------------------
// Core recursive rollback
// ---------------------------------------------------------------------------

fn rollback(
    node_id: &str,
    node_map: &HashMap<&str, &TreeNode>,
    discount_rate: Decimal,
    risk_adjustment: Option<Decimal>,
    values: &mut HashMap<String, Decimal>,
    choices: &mut HashMap<String, String>,
) -> Decimal {
    if let Some(&cached) = values.get(node_id) {
        return cached;
    }

    let node = node_map[node_id];
    let cost = node.cost.unwrap_or(Decimal::ZERO);
    let time_period = node.time_period.unwrap_or(0);
    let df = discount_factor(discount_rate, time_period);

    let value = match node.node_type {
        NodeType::Terminal => {
            let base_val = node.value.unwrap_or(Decimal::ZERO);
            let adjusted = if let Some(ra) = risk_adjustment {
                base_val * risk_factor(ra, time_period)
            } else {
                base_val
            };
            adjusted * df - cost
        }
        NodeType::Chance => {
            let mut expected = Decimal::ZERO;
            for child_id in &node.children {
                let child = node_map[child_id.as_str()];
                let prob = child.probability.unwrap_or(Decimal::ZERO);
                let child_val = rollback(
                    child_id,
                    node_map,
                    discount_rate,
                    risk_adjustment,
                    values,
                    choices,
                );
                expected += prob * child_val;
            }
            expected - cost
        }
        NodeType::Decision => {
            let mut best_val = None;
            let mut best_child = String::new();
            for child_id in &node.children {
                let child_val = rollback(
                    child_id,
                    node_map,
                    discount_rate,
                    risk_adjustment,
                    values,
                    choices,
                );
                match best_val {
                    None => {
                        best_val = Some(child_val);
                        best_child = child_id.clone();
                    }
                    Some(current_best) if child_val > current_best => {
                        best_val = Some(child_val);
                        best_child = child_id.clone();
                    }
                    _ => {}
                }
            }
            choices.insert(node_id.to_string(), best_child);
            best_val.unwrap_or(Decimal::ZERO) - cost
        }
    };

    values.insert(node_id.to_string(), value);
    value
}

// ---------------------------------------------------------------------------
// EVPI calculation
// ---------------------------------------------------------------------------

fn compute_evpi(
    root_id: &str,
    node_map: &HashMap<&str, &TreeNode>,
    discount_rate: Decimal,
    emv: Decimal,
) -> Decimal {
    // EVPI = E[max payoffs per chance outcome] - EMV
    // For each chance node, if we had perfect info, we'd pick the best outcome.
    // Collect all terminal node values weighted by their path probabilities.
    // Perfect info: at each chance node, take the max child value (not expected).
    let mut values_pi = HashMap::new();
    let ev_with_pi = rollback_perfect_info(root_id, node_map, discount_rate, &mut values_pi);
    let evpi = ev_with_pi - emv;
    evpi.max(Decimal::ZERO)
}

/// Rollback with perfect information: at chance nodes, take max instead of expected value.
fn rollback_perfect_info(
    node_id: &str,
    node_map: &HashMap<&str, &TreeNode>,
    discount_rate: Decimal,
    values: &mut HashMap<String, Decimal>,
) -> Decimal {
    if let Some(&cached) = values.get(node_id) {
        return cached;
    }

    let node = node_map[node_id];
    let cost = node.cost.unwrap_or(Decimal::ZERO);
    let time_period = node.time_period.unwrap_or(0);
    let df = discount_factor(discount_rate, time_period);

    let value = match node.node_type {
        NodeType::Terminal => {
            let base_val = node.value.unwrap_or(Decimal::ZERO);
            base_val * df - cost
        }
        NodeType::Chance => {
            // With perfect info, we know which outcome will occur.
            // EVPI = sum(p_i * V_i) where V_i = value if we KNEW outcome i
            // and made optimal decisions accordingly.
            let mut expected_best = Decimal::ZERO;
            for child_id in &node.children {
                let child = node_map[child_id.as_str()];
                let prob = child.probability.unwrap_or(Decimal::ZERO);
                let child_val = rollback_perfect_info(child_id, node_map, discount_rate, values);
                expected_best += prob * child_val;
            }
            expected_best - cost
        }
        NodeType::Decision => {
            // With perfect info, we still pick the best branch
            let mut best_val = None;
            for child_id in &node.children {
                let child_val = rollback_perfect_info(child_id, node_map, discount_rate, values);
                match best_val {
                    None => best_val = Some(child_val),
                    Some(current) if child_val > current => best_val = Some(child_val),
                    _ => {}
                }
            }
            best_val.unwrap_or(Decimal::ZERO) - cost
        }
    };

    values.insert(node_id.to_string(), value);
    value
}

// ---------------------------------------------------------------------------
// Sensitivity analysis
// ---------------------------------------------------------------------------

fn compute_sensitivity(
    root_id: &str,
    node_map: &HashMap<&str, &TreeNode>,
    input: &DecisionTreeInput,
    base_emv: Decimal,
) -> Vec<SensitivityResult> {
    let mut results = Vec::new();
    let shift = dec!(0.10); // ±10% probability shift

    for node in &input.nodes {
        if node.node_type != NodeType::Chance {
            continue;
        }
        if node.children.len() < 2 {
            continue;
        }

        // For each child of this chance node, shift its probability up/down by 10%
        // and redistribute among other children
        for (idx, child_id) in node.children.iter().enumerate() {
            let child = node_map[child_id.as_str()];
            let base_prob = child.probability.unwrap_or(Decimal::ZERO);

            // High case: increase this child's probability
            let high_shift = (base_prob + shift).min(Decimal::ONE);
            let emv_high = compute_emv_with_shifted_prob(root_id, input, &node.id, idx, high_shift);

            // Low case: decrease this child's probability
            let low_shift = (base_prob - shift).max(Decimal::ZERO);
            let emv_low = compute_emv_with_shifted_prob(root_id, input, &node.id, idx, low_shift);

            results.push(SensitivityResult {
                node_id: child_id.clone(),
                base_value: base_emv,
                high_value: emv_high,
                low_value: emv_low,
            });
        }
    }

    results
}

/// Recalculate EMV with a shifted probability for one child of a chance node.
fn compute_emv_with_shifted_prob(
    root_id: &str,
    input: &DecisionTreeInput,
    chance_node_id: &str,
    child_idx: usize,
    new_prob: Decimal,
) -> Decimal {
    // Create a modified copy of nodes with adjusted probabilities
    let mut modified_nodes = input.nodes.clone();

    // Find the chance node and adjust probabilities
    let chance_node = modified_nodes
        .iter()
        .find(|n| n.id == chance_node_id)
        .cloned();

    if let Some(cn) = chance_node {
        let original_prob = {
            let child_id = &cn.children[child_idx];
            modified_nodes
                .iter()
                .find(|n| n.id == *child_id)
                .and_then(|n| n.probability)
                .unwrap_or(Decimal::ZERO)
        };

        let prob_delta = new_prob - original_prob;
        let other_count = cn.children.len() - 1;

        if other_count > 0 {
            let redistribution = prob_delta / Decimal::from(other_count as u32);
            for (i, child_id) in cn.children.iter().enumerate() {
                if let Some(child_node) = modified_nodes.iter_mut().find(|n| n.id == *child_id) {
                    if i == child_idx {
                        child_node.probability = Some(new_prob);
                    } else {
                        let old_p = child_node.probability.unwrap_or(Decimal::ZERO);
                        child_node.probability = Some((old_p - redistribution).max(Decimal::ZERO));
                    }
                }
            }
        }
    }

    // Re-run rollback with modified nodes
    let node_map: HashMap<&str, &TreeNode> =
        modified_nodes.iter().map(|n| (n.id.as_str(), n)).collect();
    let mut values = HashMap::new();
    let mut choices = HashMap::new();
    rollback(
        root_id,
        &node_map,
        input.discount_rate,
        None,
        &mut values,
        &mut choices,
    )
}

// ---------------------------------------------------------------------------
// Optimal path extraction
// ---------------------------------------------------------------------------

fn extract_optimal_path(
    root_id: &str,
    node_map: &HashMap<&str, &TreeNode>,
    optimal_choices: &HashMap<String, String>,
) -> (Vec<String>, Vec<String>) {
    let mut path_ids = Vec::new();
    let mut path_names = Vec::new();
    let mut current_id = root_id.to_string();

    while let Some(&node) = node_map.get(current_id.as_str()) {
        path_ids.push(current_id.clone());
        path_names.push(node.name.clone());

        match node.node_type {
            NodeType::Terminal => break,
            NodeType::Decision => {
                // Follow optimal choice
                match optimal_choices.get(&current_id) {
                    Some(next) => current_id = next.clone(),
                    None => break,
                }
            }
            NodeType::Chance => {
                // Follow the highest-probability child (most likely outcome)
                let mut best_prob = Decimal::ZERO;
                let mut best_child = String::new();
                for child_id in &node.children {
                    let child = node_map[child_id.as_str()];
                    let prob = child.probability.unwrap_or(Decimal::ZERO);
                    if prob > best_prob {
                        best_prob = prob;
                        best_child = child_id.clone();
                    }
                }
                if best_child.is_empty() {
                    break;
                }
                current_id = best_child;
            }
        }
    }

    (path_ids, path_names)
}

// ---------------------------------------------------------------------------
// Decision summary
// ---------------------------------------------------------------------------

fn build_decision_summaries(
    node_map: &HashMap<&str, &TreeNode>,
    values: &HashMap<String, Decimal>,
    choices: &HashMap<String, String>,
    nodes: &[TreeNode],
) -> Vec<DecisionSummary> {
    let mut summaries = Vec::new();

    for node in nodes {
        if node.node_type != NodeType::Decision {
            continue;
        }

        let chosen = match choices.get(&node.id) {
            Some(c) => c.clone(),
            None => continue,
        };

        let chosen_name = node_map
            .get(chosen.as_str())
            .map(|n| n.name.clone())
            .unwrap_or_default();
        let chosen_val = values.get(&chosen).copied().unwrap_or(Decimal::ZERO);

        let mut alternatives = Vec::new();
        for child_id in &node.children {
            if *child_id != chosen {
                let child_name = node_map
                    .get(child_id.as_str())
                    .map(|n| n.name.clone())
                    .unwrap_or_default();
                let child_val = values
                    .get(child_id.as_str())
                    .copied()
                    .unwrap_or(Decimal::ZERO);
                alternatives.push((child_name, child_val));
            }
        }

        summaries.push(DecisionSummary {
            decision_node: node.name.clone(),
            chosen_branch: chosen_name,
            chosen_value: chosen_val,
            alternatives,
        });
    }

    summaries
}

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

pub fn analyze_decision_tree(
    input: &DecisionTreeInput,
) -> CorpFinanceResult<ComputationOutput<DecisionTreeOutput>> {
    let start = Instant::now();
    validate_tree(input)?;

    let node_map: HashMap<&str, &TreeNode> =
        input.nodes.iter().map(|n| (n.id.as_str(), n)).collect();

    let root_id = &input.nodes[0].id;

    // Standard rollback (no risk adjustment)
    let mut values = HashMap::new();
    let mut choices = HashMap::new();
    let emv = rollback(
        root_id,
        &node_map,
        input.discount_rate,
        None,
        &mut values,
        &mut choices,
    );

    // Risk-adjusted rollback
    let risk_adjusted = if input.risk_adjustment.is_some() {
        let mut ra_values = HashMap::new();
        let mut ra_choices = HashMap::new();
        let ra_val = rollback(
            root_id,
            &node_map,
            input.discount_rate,
            input.risk_adjustment,
            &mut ra_values,
            &mut ra_choices,
        );
        Some(ra_val)
    } else {
        None
    };

    // Extract optimal path
    let (optimal_path, optimal_path_names) = extract_optimal_path(root_id, &node_map, &choices);

    // Build node valuations
    let node_valuations: Vec<NodeValuation> = input
        .nodes
        .iter()
        .map(|n| {
            let val = values.get(&n.id).copied().unwrap_or(Decimal::ZERO);
            let opt_choice = choices
                .get(&n.id)
                .and_then(|cid| node_map.get(cid.as_str()).map(|cn| cn.name.clone()));
            NodeValuation {
                id: n.id.clone(),
                name: n.name.clone(),
                value: val,
                optimal_choice: opt_choice,
            }
        })
        .collect();

    // Sensitivity analysis
    let sensitivity = compute_sensitivity(root_id, &node_map, input, emv);

    // EVPI
    let evpi = compute_evpi(root_id, &node_map, input.discount_rate, emv);

    // Decision summaries
    let decision_summary = build_decision_summaries(&node_map, &values, &choices, &input.nodes);

    let output = DecisionTreeOutput {
        expected_monetary_value: emv,
        risk_adjusted_value: risk_adjusted,
        optimal_path,
        optimal_path_names,
        node_values: node_valuations,
        sensitivity,
        value_of_perfect_information: evpi,
        decision_summary,
    };

    let warnings = Vec::new();
    let assumptions = serde_json::json!({
        "model": "Decision Tree Analysis (EMV Rollback)",
        "discount_rate": input.discount_rate.to_string(),
        "risk_adjustment": input.risk_adjustment.map(|r| r.to_string()),
        "node_count": input.nodes.len(),
    });

    let elapsed = start.elapsed().as_micros() as u64;
    Ok(with_metadata(
        "Decision Tree Analysis (EMV Rollback)",
        &assumptions,
        warnings,
        elapsed,
        output,
    ))
}

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

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

    fn approx_eq(a: Decimal, b: Decimal, tol: Decimal) -> bool {
        abs_decimal(a - b) < tol
    }

    /// Simple 2-level decision tree:
    /// Root (Decision) -> [Invest, Don't Invest]
    /// Invest (Chance) -> [Success (p=0.6, val=500), Failure (p=0.4, val=-200)]
    /// Don't Invest (Terminal, val=0)
    fn simple_tree() -> DecisionTreeInput {
        DecisionTreeInput {
            nodes: vec![
                TreeNode {
                    id: "root".into(),
                    name: "Investment Decision".into(),
                    node_type: NodeType::Decision,
                    value: None,
                    cost: None,
                    probability: None,
                    children: vec!["invest".into(), "dont_invest".into()],
                    time_period: None,
                },
                TreeNode {
                    id: "invest".into(),
                    name: "Invest".into(),
                    node_type: NodeType::Chance,
                    value: None,
                    cost: Some(dec!(100)),
                    probability: None,
                    children: vec!["success".into(), "failure".into()],
                    time_period: None,
                },
                TreeNode {
                    id: "success".into(),
                    name: "Success".into(),
                    node_type: NodeType::Terminal,
                    value: Some(dec!(500)),
                    cost: None,
                    probability: Some(dec!(0.6)),
                    children: vec![],
                    time_period: None,
                },
                TreeNode {
                    id: "failure".into(),
                    name: "Failure".into(),
                    node_type: NodeType::Terminal,
                    value: Some(dec!(-200)),
                    cost: None,
                    probability: Some(dec!(0.4)),
                    children: vec![],
                    time_period: None,
                },
                TreeNode {
                    id: "dont_invest".into(),
                    name: "Don't Invest".into(),
                    node_type: NodeType::Terminal,
                    value: Some(dec!(0)),
                    cost: None,
                    probability: None,
                    children: vec![],
                    time_period: None,
                },
            ],
            discount_rate: Decimal::ZERO,
            risk_adjustment: None,
        }
    }

    /// Multi-stage investment tree:
    /// Root (Decision) -> [Explore, Abandon]
    /// Explore (Chance, cost=50) -> [High (p=0.3), Medium (p=0.5), Low (p=0.2)]
    /// High (Decision) -> [Develop_H, Sell_H]
    /// Medium (Decision) -> [Develop_M, Sell_M]
    /// Low (Terminal, val=-50)
    /// Develop_H (Terminal, val=1000, cost=200)
    /// Sell_H (Terminal, val=300)
    /// Develop_M (Terminal, val=400, cost=200)
    /// Sell_M (Terminal, val=150)
    /// Abandon (Terminal, val=0)
    fn multi_stage_tree() -> DecisionTreeInput {
        DecisionTreeInput {
            nodes: vec![
                TreeNode {
                    id: "root".into(),
                    name: "Exploration Decision".into(),
                    node_type: NodeType::Decision,
                    value: None,
                    cost: None,
                    probability: None,
                    children: vec!["explore".into(), "abandon".into()],
                    time_period: None,
                },
                TreeNode {
                    id: "explore".into(),
                    name: "Explore".into(),
                    node_type: NodeType::Chance,
                    value: None,
                    cost: Some(dec!(50)),
                    probability: None,
                    children: vec!["high".into(), "medium".into(), "low".into()],
                    time_period: None,
                },
                TreeNode {
                    id: "high".into(),
                    name: "High Reserves".into(),
                    node_type: NodeType::Decision,
                    value: None,
                    cost: None,
                    probability: Some(dec!(0.3)),
                    children: vec!["develop_h".into(), "sell_h".into()],
                    time_period: None,
                },
                TreeNode {
                    id: "medium".into(),
                    name: "Medium Reserves".into(),
                    node_type: NodeType::Decision,
                    value: None,
                    cost: None,
                    probability: Some(dec!(0.5)),
                    children: vec!["develop_m".into(), "sell_m".into()],
                    time_period: None,
                },
                TreeNode {
                    id: "low".into(),
                    name: "Low Reserves".into(),
                    node_type: NodeType::Terminal,
                    value: Some(dec!(-50)),
                    cost: None,
                    probability: Some(dec!(0.2)),
                    children: vec![],
                    time_period: None,
                },
                TreeNode {
                    id: "develop_h".into(),
                    name: "Develop High".into(),
                    node_type: NodeType::Terminal,
                    value: Some(dec!(1000)),
                    cost: Some(dec!(200)),
                    probability: None,
                    children: vec![],
                    time_period: None,
                },
                TreeNode {
                    id: "sell_h".into(),
                    name: "Sell High".into(),
                    node_type: NodeType::Terminal,
                    value: Some(dec!(300)),
                    cost: None,
                    probability: None,
                    children: vec![],
                    time_period: None,
                },
                TreeNode {
                    id: "develop_m".into(),
                    name: "Develop Medium".into(),
                    node_type: NodeType::Terminal,
                    value: Some(dec!(400)),
                    cost: Some(dec!(200)),
                    probability: None,
                    children: vec![],
                    time_period: None,
                },
                TreeNode {
                    id: "sell_m".into(),
                    name: "Sell Medium".into(),
                    node_type: NodeType::Terminal,
                    value: Some(dec!(150)),
                    cost: None,
                    probability: None,
                    children: vec![],
                    time_period: None,
                },
                TreeNode {
                    id: "abandon".into(),
                    name: "Abandon".into(),
                    node_type: NodeType::Terminal,
                    value: Some(dec!(0)),
                    cost: None,
                    probability: None,
                    children: vec![],
                    time_period: None,
                },
            ],
            discount_rate: Decimal::ZERO,
            risk_adjustment: None,
        }
    }

    // -----------------------------------------------------------------------
    // Simple tree tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_simple_tree_emv() {
        let input = simple_tree();
        let result = analyze_decision_tree(&input).unwrap();
        // Invest: EMV = 0.6*500 + 0.4*(-200) - 100 = 300 - 80 - 100 = 120
        // Don't invest: 0
        // Optimal: Invest, EMV = 120
        assert!(
            approx_eq(result.result.expected_monetary_value, dec!(120), dec!(1)),
            "EMV {} should be 120",
            result.result.expected_monetary_value
        );
    }

    #[test]
    fn test_simple_tree_optimal_path_starts_with_root() {
        let input = simple_tree();
        let result = analyze_decision_tree(&input).unwrap();
        assert_eq!(result.result.optimal_path[0], "root");
    }

    #[test]
    fn test_simple_tree_optimal_chooses_invest() {
        let input = simple_tree();
        let result = analyze_decision_tree(&input).unwrap();
        // Should choose "invest" branch
        assert!(
            result.result.optimal_path.contains(&"invest".to_string()),
            "Optimal path should include invest: {:?}",
            result.result.optimal_path
        );
    }

    #[test]
    fn test_simple_tree_decision_summary() {
        let input = simple_tree();
        let result = analyze_decision_tree(&input).unwrap();
        assert!(
            !result.result.decision_summary.is_empty(),
            "Should have decision summaries"
        );
        let summary = &result.result.decision_summary[0];
        assert_eq!(summary.chosen_branch, "Invest");
    }

    #[test]
    fn test_simple_tree_node_values() {
        let input = simple_tree();
        let result = analyze_decision_tree(&input).unwrap();
        // Check that we have valuations for all nodes
        assert_eq!(result.result.node_values.len(), 5);
    }

    #[test]
    fn test_simple_tree_evpi() {
        let input = simple_tree();
        let result = analyze_decision_tree(&input).unwrap();
        // EVPI should be non-negative
        assert!(
            result.result.value_of_perfect_information >= Decimal::ZERO,
            "EVPI {} should be non-negative",
            result.result.value_of_perfect_information
        );
    }

    // -----------------------------------------------------------------------
    // Multi-stage tree tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_multi_stage_emv_positive() {
        let input = multi_stage_tree();
        let result = analyze_decision_tree(&input).unwrap();
        // Explore:
        //   High (p=0.3): max(1000-200, 300) = 800
        //   Medium (p=0.5): max(400-200, 150) = 200
        //   Low (p=0.2): -50
        //   EMV = 0.3*800 + 0.5*200 + 0.2*(-50) - 50 = 240 + 100 - 10 - 50 = 280
        // Abandon: 0
        // Optimal: Explore, EMV = 280
        assert!(
            approx_eq(result.result.expected_monetary_value, dec!(280), dec!(1)),
            "Multi-stage EMV {} should be ~280",
            result.result.expected_monetary_value
        );
    }

    #[test]
    fn test_multi_stage_optimal_path() {
        let input = multi_stage_tree();
        let result = analyze_decision_tree(&input).unwrap();
        assert_eq!(result.result.optimal_path[0], "root");
        assert!(result.result.optimal_path.contains(&"explore".to_string()));
    }

    #[test]
    fn test_multi_stage_decision_summaries() {
        let input = multi_stage_tree();
        let result = analyze_decision_tree(&input).unwrap();
        // Should have decision summaries for: root, high, medium
        assert!(
            result.result.decision_summary.len() >= 2,
            "Should have at least 2 decision summaries, got {}",
            result.result.decision_summary.len()
        );
    }

    #[test]
    fn test_multi_stage_high_develop_optimal() {
        let input = multi_stage_tree();
        let result = analyze_decision_tree(&input).unwrap();
        // For high reserves: develop (1000-200=800) > sell (300)
        let high_summary = result
            .result
            .decision_summary
            .iter()
            .find(|s| s.decision_node == "High Reserves");
        assert!(high_summary.is_some(), "Should have High Reserves summary");
        assert_eq!(high_summary.unwrap().chosen_branch, "Develop High");
    }

    #[test]
    fn test_multi_stage_medium_develop_optimal() {
        let input = multi_stage_tree();
        let result = analyze_decision_tree(&input).unwrap();
        // For medium reserves: develop (400-200=200) > sell (150)
        let med_summary = result
            .result
            .decision_summary
            .iter()
            .find(|s| s.decision_node == "Medium Reserves");
        assert!(med_summary.is_some(), "Should have Medium Reserves summary");
        assert_eq!(med_summary.unwrap().chosen_branch, "Develop Medium");
    }

    // -----------------------------------------------------------------------
    // Discounting tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_discounting_reduces_value() {
        let mut input = simple_tree();
        input.discount_rate = Decimal::ZERO;
        let no_disc = analyze_decision_tree(&input).unwrap();

        // Now with time periods and discounting
        let mut discounted_input = simple_tree();
        discounted_input.discount_rate = dec!(0.10);
        // Set time periods on terminal nodes
        for node in &mut discounted_input.nodes {
            if node.node_type == NodeType::Terminal && node.value.is_some() {
                node.time_period = Some(2); // 2 years out
            }
        }
        let disc = analyze_decision_tree(&discounted_input).unwrap();

        // Discounted EMV should be less than undiscounted (for positive payoffs)
        // The invest branch EMV might be different with discounting
        assert!(
            disc.result.expected_monetary_value < no_disc.result.expected_monetary_value,
            "Discounted EMV {} should be less than undiscounted {}",
            disc.result.expected_monetary_value,
            no_disc.result.expected_monetary_value
        );
    }

    #[test]
    fn test_discount_factor_calculation() {
        // (1+0.10)^(-2) = 1/1.21 ~ 0.8264
        let df = discount_factor(dec!(0.10), 2);
        assert!(
            approx_eq(df, dec!(0.8264), dec!(0.01)),
            "Discount factor {} should be ~0.8264",
            df
        );
    }

    #[test]
    fn test_discount_factor_zero_rate() {
        let df = discount_factor(Decimal::ZERO, 5);
        assert_eq!(df, Decimal::ONE);
    }

    #[test]
    fn test_discount_factor_zero_periods() {
        let df = discount_factor(dec!(0.10), 0);
        assert_eq!(df, Decimal::ONE);
    }

    // -----------------------------------------------------------------------
    // Risk adjustment tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_risk_adjustment_reduces_value() {
        let mut input = simple_tree();
        // Add time periods
        for node in &mut input.nodes {
            if node.node_type == NodeType::Terminal {
                node.time_period = Some(1);
            }
        }
        input.risk_adjustment = Some(dec!(0.90)); // 10% risk discount

        let result = analyze_decision_tree(&input).unwrap();
        assert!(
            result.result.risk_adjusted_value.is_some(),
            "Should have risk-adjusted value"
        );

        let no_risk = {
            let mut nr = input.clone();
            nr.risk_adjustment = None;
            analyze_decision_tree(&nr).unwrap()
        };

        // Risk-adjusted should differ from base (terminal values are adjusted)
        assert_ne!(
            result.result.risk_adjusted_value.unwrap(),
            no_risk.result.expected_monetary_value,
            "Risk-adjusted and base EMV should differ"
        );
    }

    #[test]
    fn test_risk_adjustment_none() {
        let input = simple_tree();
        let result = analyze_decision_tree(&input).unwrap();
        assert!(
            result.result.risk_adjusted_value.is_none(),
            "Should have no risk-adjusted value when not provided"
        );
    }

    // -----------------------------------------------------------------------
    // Sensitivity tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_sensitivity_has_results() {
        let input = simple_tree();
        let result = analyze_decision_tree(&input).unwrap();
        assert!(
            !result.result.sensitivity.is_empty(),
            "Should have sensitivity results for chance nodes"
        );
    }

    #[test]
    fn test_sensitivity_high_low_bracket_base() {
        let input = simple_tree();
        let result = analyze_decision_tree(&input).unwrap();
        for sens in &result.result.sensitivity {
            // High and low should bracket the base (or at least differ)
            assert!(
                sens.high_value != sens.low_value || sens.high_value == sens.base_value,
                "High and low should generally differ"
            );
        }
    }

    // -----------------------------------------------------------------------
    // EVPI tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_evpi_non_negative() {
        let input = simple_tree();
        let result = analyze_decision_tree(&input).unwrap();
        assert!(
            result.result.value_of_perfect_information >= Decimal::ZERO,
            "EVPI should be non-negative, got {}",
            result.result.value_of_perfect_information
        );
    }

    #[test]
    fn test_evpi_multi_stage() {
        let input = multi_stage_tree();
        let result = analyze_decision_tree(&input).unwrap();
        assert!(
            result.result.value_of_perfect_information >= Decimal::ZERO,
            "EVPI should be non-negative for multi-stage, got {}",
            result.result.value_of_perfect_information
        );
    }

    // -----------------------------------------------------------------------
    // Validation error tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_empty_tree_error() {
        let input = DecisionTreeInput {
            nodes: vec![],
            discount_rate: Decimal::ZERO,
            risk_adjustment: None,
        };
        assert!(analyze_decision_tree(&input).is_err());
    }

    #[test]
    fn test_duplicate_id_error() {
        let input = DecisionTreeInput {
            nodes: vec![
                TreeNode {
                    id: "a".into(),
                    name: "A".into(),
                    node_type: NodeType::Terminal,
                    value: Some(dec!(100)),
                    cost: None,
                    probability: None,
                    children: vec![],
                    time_period: None,
                },
                TreeNode {
                    id: "a".into(),
                    name: "A2".into(),
                    node_type: NodeType::Terminal,
                    value: Some(dec!(200)),
                    cost: None,
                    probability: None,
                    children: vec![],
                    time_period: None,
                },
            ],
            discount_rate: Decimal::ZERO,
            risk_adjustment: None,
        };
        assert!(analyze_decision_tree(&input).is_err());
    }

    #[test]
    fn test_terminal_without_value_error() {
        let input = DecisionTreeInput {
            nodes: vec![TreeNode {
                id: "t".into(),
                name: "Terminal".into(),
                node_type: NodeType::Terminal,
                value: None,
                cost: None,
                probability: None,
                children: vec![],
                time_period: None,
            }],
            discount_rate: Decimal::ZERO,
            risk_adjustment: None,
        };
        assert!(analyze_decision_tree(&input).is_err());
    }

    #[test]
    fn test_missing_child_reference_error() {
        let input = DecisionTreeInput {
            nodes: vec![TreeNode {
                id: "root".into(),
                name: "Root".into(),
                node_type: NodeType::Decision,
                value: None,
                cost: None,
                probability: None,
                children: vec!["nonexistent".into()],
                time_period: None,
            }],
            discount_rate: Decimal::ZERO,
            risk_adjustment: None,
        };
        assert!(analyze_decision_tree(&input).is_err());
    }

    #[test]
    fn test_chance_probabilities_must_sum_to_one() {
        let input = DecisionTreeInput {
            nodes: vec![
                TreeNode {
                    id: "root".into(),
                    name: "Root".into(),
                    node_type: NodeType::Chance,
                    value: None,
                    cost: None,
                    probability: None,
                    children: vec!["a".into(), "b".into()],
                    time_period: None,
                },
                TreeNode {
                    id: "a".into(),
                    name: "A".into(),
                    node_type: NodeType::Terminal,
                    value: Some(dec!(100)),
                    cost: None,
                    probability: Some(dec!(0.3)),
                    children: vec![],
                    time_period: None,
                },
                TreeNode {
                    id: "b".into(),
                    name: "B".into(),
                    node_type: NodeType::Terminal,
                    value: Some(dec!(200)),
                    cost: None,
                    probability: Some(dec!(0.3)), // Sum = 0.6, not 1.0
                    children: vec![],
                    time_period: None,
                },
            ],
            discount_rate: Decimal::ZERO,
            risk_adjustment: None,
        };
        assert!(analyze_decision_tree(&input).is_err());
    }

    #[test]
    fn test_non_terminal_without_children_error() {
        let input = DecisionTreeInput {
            nodes: vec![TreeNode {
                id: "root".into(),
                name: "Root".into(),
                node_type: NodeType::Decision,
                value: None,
                cost: None,
                probability: None,
                children: vec![],
                time_period: None,
            }],
            discount_rate: Decimal::ZERO,
            risk_adjustment: None,
        };
        assert!(analyze_decision_tree(&input).is_err());
    }

    #[test]
    fn test_terminal_with_children_error() {
        let input = DecisionTreeInput {
            nodes: vec![
                TreeNode {
                    id: "root".into(),
                    name: "Root".into(),
                    node_type: NodeType::Terminal,
                    value: Some(dec!(100)),
                    cost: None,
                    probability: None,
                    children: vec!["child".into()],
                    time_period: None,
                },
                TreeNode {
                    id: "child".into(),
                    name: "Child".into(),
                    node_type: NodeType::Terminal,
                    value: Some(dec!(50)),
                    cost: None,
                    probability: None,
                    children: vec![],
                    time_period: None,
                },
            ],
            discount_rate: Decimal::ZERO,
            risk_adjustment: None,
        };
        assert!(analyze_decision_tree(&input).is_err());
    }

    // -----------------------------------------------------------------------
    // Single-node tree (just a terminal)
    // -----------------------------------------------------------------------

    #[test]
    fn test_single_terminal_node() {
        let input = DecisionTreeInput {
            nodes: vec![TreeNode {
                id: "only".into(),
                name: "Only Node".into(),
                node_type: NodeType::Terminal,
                value: Some(dec!(42)),
                cost: None,
                probability: None,
                children: vec![],
                time_period: None,
            }],
            discount_rate: Decimal::ZERO,
            risk_adjustment: None,
        };
        let result = analyze_decision_tree(&input).unwrap();
        assert!(
            approx_eq(result.result.expected_monetary_value, dec!(42), dec!(0.01)),
            "Single terminal node EMV should be 42, got {}",
            result.result.expected_monetary_value
        );
    }

    // -----------------------------------------------------------------------
    // Oil exploration example
    // -----------------------------------------------------------------------

    #[test]
    fn test_oil_exploration_tree() {
        // Simplified oil exploration:
        // Root (Decision) -> [Drill, Farm Out]
        // Drill (Chance, cost=5M) -> [Dry Hole (p=0.5), Small (p=0.3), Large (p=0.2)]
        // Dry Hole (Terminal, val=-2M)
        // Small (Terminal, val=10M)
        // Large (Terminal, val=50M)
        // Farm Out (Terminal, val=2M)
        let input = DecisionTreeInput {
            nodes: vec![
                TreeNode {
                    id: "root".into(),
                    name: "Drilling Decision".into(),
                    node_type: NodeType::Decision,
                    value: None,
                    cost: None,
                    probability: None,
                    children: vec!["drill".into(), "farmout".into()],
                    time_period: None,
                },
                TreeNode {
                    id: "drill".into(),
                    name: "Drill".into(),
                    node_type: NodeType::Chance,
                    value: None,
                    cost: Some(dec!(5000000)),
                    probability: None,
                    children: vec!["dry".into(), "small".into(), "large".into()],
                    time_period: None,
                },
                TreeNode {
                    id: "dry".into(),
                    name: "Dry Hole".into(),
                    node_type: NodeType::Terminal,
                    value: Some(dec!(-2000000)),
                    cost: None,
                    probability: Some(dec!(0.5)),
                    children: vec![],
                    time_period: None,
                },
                TreeNode {
                    id: "small".into(),
                    name: "Small Discovery".into(),
                    node_type: NodeType::Terminal,
                    value: Some(dec!(10000000)),
                    cost: None,
                    probability: Some(dec!(0.3)),
                    children: vec![],
                    time_period: None,
                },
                TreeNode {
                    id: "large".into(),
                    name: "Large Discovery".into(),
                    node_type: NodeType::Terminal,
                    value: Some(dec!(50000000)),
                    cost: None,
                    probability: Some(dec!(0.2)),
                    children: vec![],
                    time_period: None,
                },
                TreeNode {
                    id: "farmout".into(),
                    name: "Farm Out".into(),
                    node_type: NodeType::Terminal,
                    value: Some(dec!(2000000)),
                    cost: None,
                    probability: None,
                    children: vec![],
                    time_period: None,
                },
            ],
            discount_rate: Decimal::ZERO,
            risk_adjustment: None,
        };
        let result = analyze_decision_tree(&input).unwrap();
        // Drill EMV = 0.5*(-2M) + 0.3*(10M) + 0.2*(50M) - 5M
        //           = -1M + 3M + 10M - 5M = 7M
        // Farm out: 2M
        // Should drill
        assert!(
            approx_eq(
                result.result.expected_monetary_value,
                dec!(7000000),
                dec!(100000)
            ),
            "Oil drill EMV {} should be ~7M",
            result.result.expected_monetary_value
        );
        assert!(result.result.optimal_path.contains(&"drill".to_string()));
    }

    // -----------------------------------------------------------------------
    // Metadata tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_metadata_populated() {
        let input = simple_tree();
        let result = analyze_decision_tree(&input).unwrap();
        assert!(!result.methodology.is_empty());
        assert!(!result.metadata.version.is_empty());
        assert_eq!(result.metadata.precision, "rust_decimal_128bit");
    }

    #[test]
    fn test_optimal_path_names_populated() {
        let input = simple_tree();
        let result = analyze_decision_tree(&input).unwrap();
        assert!(
            !result.result.optimal_path_names.is_empty(),
            "Optimal path names should be populated"
        );
    }

    // -----------------------------------------------------------------------
    // All-terminal tree (Decision with all Terminal children)
    // -----------------------------------------------------------------------

    #[test]
    fn test_decision_with_all_terminal_children() {
        let input = DecisionTreeInput {
            nodes: vec![
                TreeNode {
                    id: "root".into(),
                    name: "Choose".into(),
                    node_type: NodeType::Decision,
                    value: None,
                    cost: None,
                    probability: None,
                    children: vec!["a".into(), "b".into(), "c".into()],
                    time_period: None,
                },
                TreeNode {
                    id: "a".into(),
                    name: "Option A".into(),
                    node_type: NodeType::Terminal,
                    value: Some(dec!(100)),
                    cost: None,
                    probability: None,
                    children: vec![],
                    time_period: None,
                },
                TreeNode {
                    id: "b".into(),
                    name: "Option B".into(),
                    node_type: NodeType::Terminal,
                    value: Some(dec!(200)),
                    cost: None,
                    probability: None,
                    children: vec![],
                    time_period: None,
                },
                TreeNode {
                    id: "c".into(),
                    name: "Option C".into(),
                    node_type: NodeType::Terminal,
                    value: Some(dec!(150)),
                    cost: None,
                    probability: None,
                    children: vec![],
                    time_period: None,
                },
            ],
            discount_rate: Decimal::ZERO,
            risk_adjustment: None,
        };
        let result = analyze_decision_tree(&input).unwrap();
        // Should pick option B (200)
        assert!(
            approx_eq(result.result.expected_monetary_value, dec!(200), dec!(1)),
            "Should pick best terminal: EMV = {}, expected 200",
            result.result.expected_monetary_value
        );
    }

    // -----------------------------------------------------------------------
    // Cost at nodes test
    // -----------------------------------------------------------------------

    #[test]
    fn test_cost_at_decision_node() {
        let input = DecisionTreeInput {
            nodes: vec![
                TreeNode {
                    id: "root".into(),
                    name: "Root".into(),
                    node_type: NodeType::Decision,
                    value: None,
                    cost: Some(dec!(10)),
                    probability: None,
                    children: vec!["a".into(), "b".into()],
                    time_period: None,
                },
                TreeNode {
                    id: "a".into(),
                    name: "A".into(),
                    node_type: NodeType::Terminal,
                    value: Some(dec!(100)),
                    cost: None,
                    probability: None,
                    children: vec![],
                    time_period: None,
                },
                TreeNode {
                    id: "b".into(),
                    name: "B".into(),
                    node_type: NodeType::Terminal,
                    value: Some(dec!(50)),
                    cost: None,
                    probability: None,
                    children: vec![],
                    time_period: None,
                },
            ],
            discount_rate: Decimal::ZERO,
            risk_adjustment: None,
        };
        let result = analyze_decision_tree(&input).unwrap();
        // Best child = A (100), minus root cost 10 = 90
        assert!(
            approx_eq(result.result.expected_monetary_value, dec!(90), dec!(1)),
            "EMV with cost {} should be 90",
            result.result.expected_monetary_value
        );
    }

    #[test]
    fn test_chance_probability_out_of_range_error() {
        let input = DecisionTreeInput {
            nodes: vec![
                TreeNode {
                    id: "root".into(),
                    name: "Root".into(),
                    node_type: NodeType::Chance,
                    value: None,
                    cost: None,
                    probability: None,
                    children: vec!["a".into(), "b".into()],
                    time_period: None,
                },
                TreeNode {
                    id: "a".into(),
                    name: "A".into(),
                    node_type: NodeType::Terminal,
                    value: Some(dec!(100)),
                    cost: None,
                    probability: Some(dec!(1.5)), // > 1.0
                    children: vec![],
                    time_period: None,
                },
                TreeNode {
                    id: "b".into(),
                    name: "B".into(),
                    node_type: NodeType::Terminal,
                    value: Some(dec!(50)),
                    cost: None,
                    probability: Some(dec!(-0.5)), // negative
                    children: vec![],
                    time_period: None,
                },
            ],
            discount_rate: Decimal::ZERO,
            risk_adjustment: None,
        };
        assert!(analyze_decision_tree(&input).is_err());
    }
}