sepa 0.9.0

SEPA identifiers and ISO 20022 payment messages: builders, bank-report parsers, validation and integer monetary arithmetic.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
//! Shared vocabulary for the camt.05x cash-management messages.
//!
//! `camt.052` (intraday report), `camt.053` (end-of-day statement) and
//! `camt.054` (debit/credit notification) describe the same underlying thing —
//! movements on an account — and differ mainly in their wrapper element and in
//! whether balances are present. They share the entry model defined here, so a
//! reconciliation routine can treat all three uniformly.
//!
//! ## Version handling
//!
//! ISO reshaped several elements across the v02 → v13 range. This shared model
//! handles the shapes below; each public reader enforces its own exact supported
//! namespace/version list, not every ISO generation:
//!
//! | Change | Introduced | Handled by |
//! |---|---|---|
//! | `Sts` became a `Cd`/`Prtry` choice | `.001.07` | accepts both forms |
//! | Parties gained a `Pty`/`Agt` wrapper | `.001.07` | accepts both nestings |
//! | `BIC` renamed to `BICFI` | `.001.03` | accepts both spellings |
//! | `TxDtls/Amt` became optional | `.001.08` | falls back to `AmtDtls/TxAmt/Amt`, then to the entry total for a single-detail entry — see [`EntryDetail::signed_ct`] |
//!
//! ## Batch bookings
//!
//! A batch-booked entry carries a `Btch` block plus one `TxDtls` per original
//! transaction. [`CashEntry::batch`] exposes the former — including the
//! `PmtInfId` of the group that produced the booking, which is what matches a
//! statement entry back to a submitted pain.008 — and [`CashEntry::details`]
//! the latter. Read [`EntryDetail::signed_ct`] for each detail (it resolves the
//! amount and its sign from whichever level reported them) and use
//! [`CashEntry::details_reconcile`] to check the parts add up to the whole
//! before posting.

use crate::camt054::CreditDebitIndicator;
use crate::xml::Node;

/// Why a financial observation cannot be used as an exact financial fact.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum FinancialIssue {
    /// A required magnitude is absent.
    MissingAmount,
    /// The reported text is not a decimal amount.
    InvalidAmount,
    /// The reported transaction-count text is not a readable unsigned integer.
    InvalidCount,
    /// An amount magnitude is negative.
    NegativeAmount,
    /// Significant digits below one hundredth cannot be represented.
    SubCentPrecision,
    /// The exact amount or count cannot fit its integer representation.
    Overflow,
    /// The amount has no reported currency.
    MissingCurrency,
    /// Currency spelling is malformed or reserved, not a registry lookup.
    InvalidCurrency,
    /// A required ledger direction is absent.
    MissingDirection,
    /// A reported direction is not an exact supported wire code.
    InvalidDirection,
    /// Raw and interpreted facts disagree.
    InconsistentValue,
    /// Independently reported alternatives disagree or violate a choice.
    ConflictingAlternatives,
    /// Denominations are not comparable for the requested aggregate.
    CurrencyMismatch,
    /// Independently reported aggregate and record magnitudes disagree.
    TotalMismatch,
}

/// Observed source of an original instructed amount, never inferred from value.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum OriginalAmountSource {
    /// Transaction-level `OrgnlInstdAmt`.
    OriginalInstructed,
    /// `OrgnlTxRef/Amt/InstdAmt`.
    TransactionReference,
    /// Historical direct `OrgnlTxRef/InstdAmt` observation.
    LegacyTransactionReference,
}

impl OriginalAmountSource {
    pub(crate) const fn path(self) -> &'static str {
        match self {
            Self::OriginalInstructed => "OrgnlInstdAmt",
            Self::TransactionReference => "OrgnlTxRef/Amt/InstdAmt",
            Self::LegacyTransactionReference => "OrgnlTxRef/InstdAmt",
        }
    }
}

/// Observed route of a cash transaction's itemized amount.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum DetailAmountSource {
    /// Direct `TxDtls/Amt`.
    Direct,
    /// Nested `TxDtls/AmtDtls/TxAmt/Amt`.
    TransactionAmount,
}

impl DetailAmountSource {
    pub(crate) const fn path(self) -> &'static str {
        match self {
            Self::Direct => "Amt",
            Self::TransactionAmount => "AmtDtls/TxAmt/Amt",
        }
    }
}

/// A located observation problem, not an XSD verdict or payment decision.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FinancialDiagnostic {
    /// Logical ISO field location; retained-record indices are zero-based.
    /// Detail indices address flattened observations, not executable `XPath`.
    pub path: String,
    /// Machine-readable reason.
    pub issue: FinancialIssue,
    /// Decoded reported text, including empty/padded text.
    /// `None` indicates no scalar text, including derived/container problems.
    pub raw: Option<String>,
}

pub(crate) fn currency_diagnostics(path: &str, raw: Option<&str>) -> Vec<FinancialDiagnostic> {
    let issue = match raw {
        None => Some(FinancialIssue::MissingCurrency),
        Some(raw)
            if crate::Currency::new(raw)
                .ok()
                .is_none_or(|currency| currency.code() != raw) =>
        {
            Some(FinancialIssue::InvalidCurrency)
        }
        Some(_) => None,
    };
    issue
        .into_iter()
        .map(|issue| FinancialDiagnostic {
            path: path.to_owned(),
            issue,
            raw: raw.map(str::to_owned),
        })
        .collect()
}

pub(crate) fn decimal_diagnostics(
    path: &str,
    raw: Option<&str>,
    interpreted: Option<i64>,
    nonnegative: bool,
) -> Vec<FinancialDiagnostic> {
    let issue = match raw.map(crate::ct_from_eur_str) {
        None if interpreted.is_none() => Some(FinancialIssue::MissingAmount),
        None => interpreted
            .filter(|value| nonnegative && *value < 0)
            .map(|_| FinancialIssue::NegativeAmount),
        Some(Err(crate::AmountError::SubCentPrecision { .. })) => {
            Some(FinancialIssue::SubCentPrecision)
        }
        Some(Err(crate::AmountError::Overflow { .. })) => Some(FinancialIssue::Overflow),
        Some(Err(_)) => Some(FinancialIssue::InvalidAmount),
        Some(Ok(value)) if nonnegative && value < 0 => Some(FinancialIssue::NegativeAmount),
        Some(Ok(value)) if Some(value) != interpreted => Some(FinancialIssue::InconsistentValue),
        Some(Ok(_)) => None,
    };
    issue
        .into_iter()
        .map(|issue| FinancialDiagnostic {
            path: path.to_owned(),
            issue,
            raw: raw.map(str::to_owned),
        })
        .collect()
}

pub(crate) fn optional_amount_diagnostics(
    path: &str,
    raw: Option<&str>,
    ct: Option<i64>,
    currency: Option<&str>,
) -> Vec<FinancialDiagnostic> {
    if raw.is_none() && ct.is_none() && currency.is_none() {
        return Vec::new();
    }
    ReportedAmount {
        ct,
        amount_raw: raw.map(str::to_owned),
        currency: currency.map(str::to_owned),
        ..Default::default()
    }
    .diagnostics(path, None)
}

pub(crate) fn optional_count_diagnostics(
    path: &str,
    raw: Option<&str>,
    count: Option<u64>,
) -> Vec<FinancialDiagnostic> {
    let issue = match raw {
        Some(raw) => match raw.trim().parse::<u64>() {
            Ok(value) if Some(value) == count => None,
            Ok(_) => Some(FinancialIssue::InconsistentValue),
            Err(error) if matches!(error.kind(), std::num::IntErrorKind::PosOverflow) => {
                Some(FinancialIssue::Overflow)
            }
            Err(_) => Some(FinancialIssue::InvalidCount),
        },
        None => count.map(|_| FinancialIssue::InconsistentValue),
    };
    issue.map_or_else(Vec::new, |issue| {
        vec![FinancialDiagnostic {
            path: path.to_owned(),
            issue,
            raw: raw.map(str::to_owned),
        }]
    })
}

pub(crate) fn optional_decimal_diagnostics(
    path: &str,
    raw: Option<&str>,
    ct: Option<i64>,
) -> Vec<FinancialDiagnostic> {
    if raw.is_none() && ct.is_none() {
        Vec::new()
    } else {
        decimal_diagnostics(path, raw, ct, false)
    }
}

fn charge_diagnostics(path: &str, charges: &Charges, result: &mut Vec<FinancialDiagnostic>) {
    if charges.has_mixed_sources() {
        result.push(FinancialDiagnostic {
            path: path.to_owned(),
            issue: FinancialIssue::ConflictingAlternatives,
            raw: None,
        });
    }
    let aggregate = format!("{path}/TtlChrgsAndTaxAmt");
    result.extend(optional_amount_diagnostics(
        &aggregate,
        charges.total_amount_raw.as_deref(),
        charges.total_ct,
        charges.total_currency.as_deref(),
    ));
    let currency = charges
        .records
        .first()
        .and_then(|record| record.amount.currency.as_deref());
    for (index, record) in charges.records.iter().enumerate() {
        let record_path = if record.source == Some(ChargeRecordSource::LegacyFlat) {
            path.to_owned()
        } else {
            format!("{path}/Rcrd[{index}]")
        };
        result.extend(record.amount.diagnostics(
            &format!("{record_path}/Amt"),
            Some(&format!("{record_path}/CdtDbtInd")),
        ));
        if let (Some(expected), Some(actual)) = (currency, record.amount.currency.as_deref())
            && expected != actual
        {
            result.push(FinancialDiagnostic {
                path: format!("{record_path}/Amt/@Ccy"),
                issue: FinancialIssue::CurrencyMismatch,
                raw: Some(actual.to_owned()),
            });
        }
    }
    if let (Some(expected), Some(actual)) = (currency, charges.total_currency.as_deref())
        && charges.total_amount_raw.is_some()
        && expected != actual
    {
        result.push(FinancialDiagnostic {
            path: format!("{aggregate}/@Ccy"),
            issue: FinancialIssue::CurrencyMismatch,
            raw: Some(actual.to_owned()),
        });
    }
    if let Some(total) = charges.total_ct {
        let comparable = charges.total_currency.as_deref().is_some_and(|currency| {
            charges.records.iter().all(|record| {
                record.amount.currency.as_deref() == Some(currency)
                    && record.amount.diagnostics("", None).is_empty()
            })
        }) && optional_amount_diagnostics(
            "",
            charges.total_amount_raw.as_deref(),
            charges.total_ct,
            charges.total_currency.as_deref(),
        )
        .is_empty();
        let sum = charges.records.iter().try_fold(0_i128, |sum, record| {
            sum.checked_add(i128::from(record.amount.ct?))
        });
        if comparable
            && !charges.has_mixed_sources()
            && !charges.records.is_empty()
            && sum.is_some_and(|sum| sum != i128::from(total))
        {
            result.push(FinancialDiagnostic {
                path: aggregate,
                issue: FinancialIssue::TotalMismatch,
                raw: charges.total_amount_raw.clone(),
            });
        }
    }
}

pub(crate) fn cash_diagnostics(
    path: &str,
    balances: &[StatementBalance],
    entries: &[CashEntry],
) -> Vec<FinancialDiagnostic> {
    let mut result = Vec::new();
    for (index, balance) in balances.iter().enumerate() {
        let location = format!("{path}/Bal[{index}]");
        result.extend(balance.amount.diagnostics(
            &format!("{location}/Amt"),
            Some(&format!("{location}/CdtDbtInd")),
        ));
    }
    let currency = entries
        .first()
        .and_then(|entry| entry.amount.currency.as_deref());
    for (index, entry) in entries.iter().enumerate() {
        let location = format!("{path}/Ntry[{index}]");
        result.extend(entry.amount.diagnostics(
            &format!("{location}/Amt"),
            Some(&format!("{location}/CdtDbtInd")),
        ));
        if let (Some(expected), Some(actual)) = (currency, entry.amount.currency.as_deref())
            && expected != actual
        {
            result.push(FinancialDiagnostic {
                path: format!("{location}/Amt/@Ccy"),
                issue: FinancialIssue::CurrencyMismatch,
                raw: Some(actual.to_owned()),
            });
        }
        if let Some(charges) = &entry.charges {
            charge_diagnostics(&format!("{location}/Chrgs"), charges, &mut result);
        }
        for (index, detail) in entry.details.iter().enumerate() {
            let detail_path = format!("{location}/NtryDtls/TxDtls[{index}]");
            let source = detail
                .amount_source
                .map_or("amount", DetailAmountSource::path);
            if detail.amount_source.is_some()
                || detail.amount.amount_raw.is_some()
                || detail.amount.ct.is_some()
                || detail.amount.currency.is_some()
            {
                result.extend(detail.amount.diagnostics(
                    &format!("{detail_path}/{source}"),
                    Some(&format!("{detail_path}/CdtDbtInd")),
                ));
            }
            if let Some(transaction) = &detail.transaction_amount {
                if detail.amount_source != Some(DetailAmountSource::TransactionAmount) {
                    result.extend(
                        transaction.diagnostics(&format!("{detail_path}/AmtDtls/TxAmt/Amt"), None),
                    );
                }
                if detail
                    .amount
                    .ct
                    .zip(transaction.ct)
                    .is_some_and(|(direct, nested)| direct != nested)
                    || detail
                        .amount
                        .currency
                        .as_deref()
                        .zip(transaction.currency.as_deref())
                        .is_some_and(|(direct, nested)| direct != nested)
                {
                    result.push(FinancialDiagnostic {
                        path: detail_path.clone(),
                        issue: FinancialIssue::ConflictingAlternatives,
                        raw: None,
                    });
                }
            }
            if detail
                .amount
                .currency
                .as_deref()
                .or(detail.signed_currency.as_deref())
                .zip(entry.amount.currency.as_deref())
                .is_some_and(|(own, parent)| own != parent)
            {
                result.push(FinancialDiagnostic {
                    path: format!("{detail_path}/{source}/@Ccy"),
                    issue: FinancialIssue::CurrencyMismatch,
                    raw: detail.amount.currency.clone(),
                });
            }
            if let Some(charges) = &detail.charges {
                charge_diagnostics(&format!("{detail_path}/Chrgs"), charges, &mut result);
            }
        }
    }
    result
}

// ── BalanceType ───────────────────────────────────────────────────────────────

/// Type of a balance in a camt.05x message.
///
/// Appears as `Bal/Tp/CdOrPrtry/Cd`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum BalanceType {
    /// `OPBD` — Opening Booked: balance at start of statement period.
    OpeningBooked,
    /// `CLBD` — Closing Booked: balance at end of statement period.
    ClosingBooked,
    /// `ITBD` — Intraday Booked: intermediate booked balance.
    IntradayBooked,
    /// `CLAV` — Closing Available: available funds at end of period.
    ClosingAvailable,
    /// `OPAV` — Opening Available: available funds at start of period.
    OpeningAvailable,
    /// `FWAV` — Forward Available: future available balance.
    ForwardAvailable,
    /// Any other balance type code.
    Other(String),
    /// The statement carried no `Tp/CdOrPrtry` at all.
    ///
    /// Distinct from `Other("")`, which is a bank that sent an *empty* code.
    /// `Tp` is mandatory on a `CashBalance`, so this means the document is
    /// already outside the schema — and an unlabelled balance must not be
    /// mistaken for one whose label happened to be blank.
    Unspecified,
}

impl BalanceType {
    /// ISO 20022 code string.
    #[must_use]
    pub fn as_code(&self) -> &str {
        match self {
            Self::OpeningBooked => "OPBD",
            Self::ClosingBooked => "CLBD",
            Self::IntradayBooked => "ITBD",
            Self::ClosingAvailable => "CLAV",
            Self::OpeningAvailable => "OPAV",
            Self::ForwardAvailable => "FWAV",
            Self::Other(s) => s,
            Self::Unspecified => "",
        }
    }

    pub(crate) fn from_code(s: &str) -> Self {
        match s.trim() {
            "OPBD" => Self::OpeningBooked,
            "CLBD" => Self::ClosingBooked,
            "ITBD" => Self::IntradayBooked,
            "CLAV" => Self::ClosingAvailable,
            "OPAV" => Self::OpeningAvailable,
            "FWAV" => Self::ForwardAvailable,
            other => Self::Other(other.to_owned()),
        }
    }
}

// ── EntryStatus ───────────────────────────────────────────────────────────────

/// Booking status of a camt.05x entry (`Ntry/Sts`).
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum EntryStatus {
    /// `BOOK` — booked on the account, not proof of irrevocable settlement.
    Booked,
    /// `PDNG` — Pending (not yet settled).
    Pending,
    /// `INFO` — Informational only.
    Info,
    /// `FUTR` — Future-dated entry.
    Future,
    /// Any other status code.
    Other(String),
}

impl EntryStatus {
    pub(crate) fn from_code(s: &str) -> Self {
        match s.trim() {
            "BOOK" => Self::Booked,
            "PDNG" => Self::Pending,
            "INFO" => Self::Info,
            "FUTR" => Self::Future,
            other => Self::Other(other.to_owned()),
        }
    }
}

// ── StatementBalance ──────────────────────────────────────────────────────────

/// A balance entry within a camt.05x message.
#[derive(Debug, Clone)]
#[non_exhaustive]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct StatementBalance {
    /// Balance type (opening booked, closing booked, …).
    pub balance_type: BalanceType,
    /// The balance, as far as the statement determined it.
    pub amount: ReportedAmount,
    /// Balance date exactly as the bank reported it.
    ///
    /// `Bal/Dt` is a date/time choice, so this is `"2026-07-20"` from one bank
    /// and `"2026-07-20T23:59:59"` from the next. Read [`date`](Self::date) for
    /// the day; this field is kept verbatim so nothing is lost.
    pub date_raw: String,
}

impl StatementBalance {
    /// Balance as a signed ct value (+credit, −debit), or `None` when the
    /// statement does not determine it.
    ///
    /// A balance defaulted to credit reports an overdraft as funds available,
    /// which is why there is no default. See [`ReportedAmount`].
    #[inline]
    #[must_use]
    pub fn signed_ct(&self) -> Option<i64> {
        self.amount.signed_ct()
    }

    /// The balance date, or `None` if the bank reported none this crate can read.
    #[must_use]
    pub fn date(&self) -> Option<crate::IsoDate> {
        crate::IsoDate::parse_date_part(&self.date_raw).ok()
    }
}

/// An amount as a bank statement reported it — and only as far as it did.
///
/// Money has three parts and a statement may fail to give any of them: a
/// magnitude (`Amt`), a currency (`Amt/@Ccy`) and a direction (`CdtDbtInd`).
/// **None of them is defaulted.** An unknown direction read as a credit turns a
/// EUR 1,000 debit into a EUR 1,000 credit — a double-sized error in a ledger —
/// so [`signed_ct`](Self::signed_ct) answers `None` instead, and the `*_raw`
/// fields keep what arrived for the operator to look at.
///
/// Every level of a camt document that carries money embeds this type, so the
/// rule has one definition rather than four copies of a convention.
///
/// ```
/// use sepa::parse_camt053;
///
/// # let xml = r#"<Document xmlns="urn:iso:std:iso:20022:tech:xsd:camt.053.001.08">
/// # <BkToCstmrStmt><GrpHdr><MsgId>M</MsgId><CreDtTm>2026-07-21T23:59:00</CreDtTm></GrpHdr><Stmt><Id>S</Id>
/// # <Ntry><Amt Ccy="EUR">1000.00</Amt><CdtDbtInd>DBTI</CdtDbtInd><Sts><Cd>BOOK</Cd></Sts>
/// #
/// #
/// # <BkTxCd><Domn><Cd>PMNT</Cd><Fmly><Cd>RDDT</Cd><SubFmlyCd>PMDD</SubFmlyCd></Fmly></Domn></BkTxCd>
/// # </Ntry>
/// # </Stmt></BkToCstmrStmt></Document>"#;
/// let doc = parse_camt053(xml)?;
/// let entry = &doc.statements[0].entries[0];
///
/// // `DBTI` is not a direction this crate knows, so there is no ledger figure.
/// assert_eq!(entry.signed_ct(), None);
/// // The magnitude is still known, and what arrived is still readable.
/// assert_eq!(entry.amount.ct, Some(100_000));
/// assert_eq!(entry.amount.direction_raw.as_deref(), Some("DBTI"));
/// # Ok::<(), sepa::Camt053ParseError>(())
/// ```
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[non_exhaustive]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ReportedAmount {
    /// Parsed magnitude in **ct** — 1/100 of [`currency`](Self::currency).
    /// Signing rechecks public/restored observations before trusting them.
    ///
    /// `None` when `Amt` was absent, or carried a value this type cannot hold:
    /// a magnitude outside `i64` ct, or significant digits below one cent.
    pub ct: Option<i64>,
    /// ISO 4217 code from the `Ccy` attribute.
    ///
    /// `None` when the attribute was absent. It is **not** defaulted to `EUR`:
    /// camt statements are not EUR-only, and a fabricated currency propagates —
    /// a detail in a different currency is excluded from its entry's sum, so
    /// guessing here silently changes which transactions are counted.
    pub currency: Option<String>,
    /// Decoded `Amt` text, preserving padding/empty values but not source bytes.
    pub amount_raw: Option<String>,
    /// Credit (into the account) or debit (out of it).
    ///
    /// `None` when `CdtDbtInd` was absent or carried a code this crate does not
    /// recognise. The direction is the entire content of this field, and a
    /// wrong one is a two-for-one error in a ledger.
    pub direction: Option<CreditDebitIndicator>,
    /// `CdtDbtInd` exactly as the bank wrote it.
    pub direction_raw: Option<String>,
}

pub(crate) fn net_movement_ct(entries: &[CashEntry]) -> Option<i64> {
    let mut currency = None;
    let total = entries.iter().try_fold(0_i128, |total, entry| {
        let denomination = entry.amount.currency.as_deref()?;
        if currency.is_some_and(|previous| previous != denomination) {
            return None;
        }
        currency = Some(denomination);
        total.checked_add(i128::from(entry.signed_ct()?))
    })?;
    i64::try_from(total).ok()
}

impl ReportedAmount {
    /// Inspect current facts without mutating them or guessing missing values.
    ///
    /// `amount_path` locates the amount element. Supply `direction_path` only
    /// when a ledger sign is required; instructed amounts need no such sign.
    /// Currency checks cover spelling/reserved codes, not registry membership.
    #[must_use]
    pub fn diagnostics(
        &self,
        amount_path: &str,
        direction_path: Option<&str>,
    ) -> Vec<FinancialDiagnostic> {
        let mut result =
            decimal_diagnostics(amount_path, self.amount_raw.as_deref(), self.ct, true);
        result.extend(currency_diagnostics(
            &format!("{amount_path}/@Ccy"),
            self.currency.as_deref(),
        ));
        if let Some(path) = direction_path {
            let issue = match (self.direction_raw.as_deref(), self.direction) {
                (None, None) => Some(FinancialIssue::MissingDirection),
                (Some(raw), _) if raw.parse::<CreditDebitIndicator>().is_err() => {
                    Some(FinancialIssue::InvalidDirection)
                }
                (Some(raw), direction) if raw.parse::<CreditDebitIndicator>().ok() != direction => {
                    Some(FinancialIssue::InconsistentValue)
                }
                _ => None,
            };
            if let Some(issue) = issue {
                result.push(FinancialDiagnostic {
                    path: path.to_owned(),
                    issue,
                    raw: self.direction_raw.clone(),
                });
            }
        }
        result
    }

    /// The ledger figure: positive for a credit, negative for a debit.
    ///
    /// `None` when the statement did not determine it — either the magnitude or
    /// the direction/currency is missing or facts conflict. There is no safe substitute, and
    /// the raw fields hold whatever arrived so an importer can log *what* it
    /// could not read and escalate the row rather than post a guess.
    #[inline]
    #[must_use]
    pub fn signed_ct(&self) -> Option<i64> {
        let currency = self.currency.as_deref()?;
        if crate::Currency::new(currency).ok()?.code() != currency {
            return None;
        }
        let direction = self.direction?;
        let magnitude = self.ct?;
        if self
            .amount_raw
            .as_deref()
            .is_some_and(|raw| crate::ct_from_eur_str(raw).ok() != Some(magnitude))
            || self
                .direction_raw
                .as_deref()
                .is_some_and(|raw| raw.parse::<CreditDebitIndicator>().ok() != Some(direction))
        {
            return None;
        }
        signed(direction, magnitude)
    }

    /// Whether a ledger figure could be established at all.
    #[inline]
    #[must_use]
    pub fn is_resolved(&self) -> bool {
        self.signed_ct().is_some()
    }

    /// Whether this amount is denominated in `currency`, case-insensitively.
    ///
    /// `false` when either side did not state one: an unknown currency is not
    /// a match, because the alternative is summing figures that are not
    /// comparable.
    #[must_use]
    pub fn is_currency(&self, currency: Option<&str>) -> bool {
        match (self.currency.as_deref(), currency) {
            (Some(a), Some(b)) => a.eq_ignore_ascii_case(b),
            _ => false,
        }
    }

    /// Read `Amt` and `CdtDbtInd` from a node, inventing nothing.
    pub(crate) fn parse(node: &Node, tag: &str) -> Self {
        let (ct, currency, amount_raw) = match node.child(tag) {
            None => (None, None, None),
            Some(amt) => (
                crate::ct_from_eur_str(&amt.text)
                    .ok()
                    .filter(|value| *value >= 0),
                amt.attr("Ccy").map(str::to_owned),
                Some(amt.text.clone()),
            ),
        };
        let (direction, direction_raw) = match node.raw_text_of("CdtDbtInd") {
            None => (None, None),
            Some(raw) => (raw.parse().ok(), Some(raw.to_owned())),
        };
        Self {
            ct,
            currency,
            amount_raw,
            direction,
            direction_raw,
        }
    }
}

// ── EntryDetail ───────────────────────────────────────────────────────────────

/// One underlying transaction within a camt.05x entry (`NtryDtls/TxDtls`).
///
/// A **batch-booked** entry — the norm for SEPA direct debit collections, where
/// the bank books one aggregate amount — carries one `TxDtls` per original
/// transaction. Reconciling such an entry requires every detail, not just the
/// first, so [`CashEntry::details`] exposes all of them.
///
/// ## Amounts
///
/// Read [`signed_ct`](Self::signed_ct), not `amount_ct`. `TxDtls/Amt` became
/// optional in `.001.08`, its sign lives in a separate `CdtDbtInd` that may sit
/// at either level, and it is denominated in the transaction's own currency
/// rather than the account's. The parser resolves all three and leaves
/// `signed_ct` as `None` when it cannot — which is the answer a reconciliation
/// routine needs, because the tempting fallback (use the entry total)
/// double-counts every batch of more than one transaction.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[non_exhaustive]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct EntryDetail {
    /// What **this transaction** stated about its own amount (`TxDtls/Amt`, or
    /// `TxDtls/AmtDtls/TxAmt/Amt` in the versions that omit it).
    ///
    /// Distinct from [`signed_ct()`](Self::signed_ct), which is the
    /// *resolved* figure and may have been inherited from the entry. A detail
    /// in a different currency from its entry is real but is not the amount
    /// that hit the account, so it is not summable against the entry total.
    pub amount: ReportedAmount,
    /// Observed itemized amount route; `None` when no amount was reported.
    /// This metadata is not inferred from value equality or inherited money.
    pub amount_source: Option<DetailAmountSource>,
    /// Independent `AmtDtls/TxAmt/Amt` observation, even when direct `Amt` exists.
    /// The detail/entry direction applies; disagreement makes signing unavailable.
    pub transaction_amount: Option<ReportedAmount>,
    /// The resolved ledger amount in **ct**: positive for a credit, negative
    /// for a debit. `None` when it could not be established — see
    /// [`signed_ct`](Self::signed_ct).
    #[cfg_attr(feature = "serde", serde(skip_deserializing))]
    signed_amount_ct: Option<i64>,
    #[cfg_attr(feature = "serde", serde(skip_deserializing))]
    signed_currency: Option<String>,
    /// End-to-end reference from the original payment instruction.
    pub end_to_end_id: Option<String>,
    /// Mandate reference (direct debits, `MndtId`).
    pub mandate_id: Option<String>,
    /// SEPA Creditor Identifier (`CdtrId`).
    pub creditor_id: Option<String>,
    /// Remittance information / payment reference (`RmtInf/Ustrd`).
    ///
    /// `Ustrd` is unbounded in camt, and German banks routinely split one
    /// *Verwendungszweck* across several occurrences of 35 characters each.
    /// All of them are joined with a single space, so the reference reads as
    /// the payer wrote it rather than being cut at the first line.
    pub reference: Option<String>,
    /// Counterparty name (debtor for credits; creditor for debits).
    pub counterparty_name: Option<String>,
    /// Counterparty IBAN.
    pub counterparty_iban: Option<String>,
    /// ISO 20022 return reason code, when this transaction is a return.
    pub return_reason_code: Option<String>,
    /// `TxDtls/RtrInf/AddtlInf` — the bank's free text about the return.
    pub return_additional_info: Option<String>,
    /// `TxDtls/AddtlTxInf` — the bank's free text about this transaction.
    pub additional_info: Option<String>,
    /// `TxDtls/Chrgs` — charges attributed to this transaction.
    ///
    /// On a returned direct debit this is the return fee. See [`Charges`].
    pub charges: Option<Charges>,
}

impl EntryDetail {
    /// This transaction's ledger amount in **ct** — credit positive, debit
    /// negative — or `None` when the statement does not determine it.
    ///
    /// Resolved by the parser as:
    ///
    /// 1. `TxDtls/Amt`, else `TxDtls/AmtDtls/TxAmt/Amt`, signed by
    ///    `TxDtls/CdtDbtInd` where present and by the entry's indicator
    ///    otherwise; then
    /// 2. for a **single-detail** entry with no itemised amount, the entry's
    ///    own signed amount — that identity is safe precisely because there is
    ///    only one transaction to attribute it to; otherwise
    /// 3. `None`.
    ///
    /// A detail denominated in a currency other than the entry's is also
    /// `None`: its amount is real, but it is not the amount that hit the
    /// account, so summing it against the entry total would be wrong.
    ///
    /// Case 3 is the one worth handling explicitly. Falling back to the entry
    /// total per detail — the obvious workaround when this is unavailable —
    /// multiplies a batch booking by its transaction count.
    #[inline]
    #[must_use]
    pub fn signed_ct(&self) -> Option<i64> {
        if let Some(transaction) = &self.transaction_amount {
            let nested = transaction.signed_ct()?;
            if self.amount.currency != transaction.currency
                || self.amount.signed_ct() != Some(nested)
            {
                return None;
            }
        }
        let cached = self.signed_amount_ct?;
        let currency = self.signed_currency.as_deref()?;
        if self.amount.amount_raw.is_some() {
            if self.amount.currency.as_deref() != Some(currency)
                || self.amount.signed_ct() != Some(cached)
            {
                return None;
            }
        } else {
            let direction = self.amount.direction?;
            if self.amount.ct.is_some()
                || self.amount.currency.is_some()
                || signed(direction, cached.checked_abs()?) != Some(cached)
                || self
                    .amount
                    .direction_raw
                    .as_deref()
                    .is_some_and(|raw| raw.parse::<CreditDebitIndicator>().ok() != Some(direction))
            {
                return None;
            }
        }
        Some(cached)
    }

    /// Whether this transaction is a return (Rückläufer).
    #[inline]
    #[must_use]
    pub const fn is_return(&self) -> bool {
        self.return_reason_code.is_some()
    }
}

// ── Charges ───────────────────────────────────────────────────────────────────

/// Observed shape of an itemized charge, not inferred from amount equality.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ChargeRecordSource {
    /// Modern `Chrgs/Rcrd` record.
    Record,
    /// Historical fields directly under `Chrgs`.
    LegacyFlat,
}

/// One charge a bank levied on an entry or a transaction (`Chrgs/Rcrd`).
///
/// For SEPA the case that matters is a **returned direct debit**: the debtor's
/// bank returns the collection and the creditor's bank passes on a return fee.
/// That fee is real money and has to be booked, and it is reported here rather
/// than in the entry amount.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ChargeRecord {
    /// Observed charge shape; absent for records without retained provenance.
    pub source: Option<ChargeRecordSource>,
    /// The charge, as far as the statement determined it.
    ///
    /// Missing and unrecognised directions remain unresolved. A fee is not
    /// evidence of a reported debit direction.
    pub amount: ReportedAmount,
    /// `ChrgInclInd` — whether this charge is **already included** in the
    /// entry's own amount.
    ///
    /// This is the field that decides whether a ledger adds the charge or not.
    /// `Some(true)` means the entry amount already carries it and posting it
    /// again double-counts; `Some(false)` means it is separate. `None` means
    /// the value was not determined — treat it as unresolved rather than
    /// picking a default.
    ///
    /// `None` covers two different situations, and
    /// [`included_in_amount_raw`](Self::included_in_amount_raw) is what tells
    /// them apart: the bank sent no `ChrgInclInd` at all (raw is `None` too),
    /// or it sent one this crate could not read (raw holds it verbatim).
    pub included_in_amount: Option<bool>,
    /// `ChrgInclInd` exactly as it arrived, when it arrived at all.
    ///
    /// `xs:boolean` has four lexical forms — `true`, `false`, `1`, `0` — and
    /// all four resolve. Anything else leaves
    /// [`included_in_amount`](Self::included_in_amount) `None` and lands here,
    /// so "the bank said nothing" and "the bank said `TRUE`" stay
    /// distinguishable. Conflating them would be the `CdtDbtInd` defect on a
    /// field that also decides whether money is posted twice.
    pub included_in_amount_raw: Option<String>,
    /// `Tp/Cd` or `Tp/Prtry` — what kind of charge, when the bank names one.
    pub type_code: Option<String>,
}

impl ChargeRecord {
    /// The charge as a signed ledger amount: negative for a debit. `None` when
    /// the statement did not determine it.
    #[inline]
    #[must_use]
    pub fn signed_ct(&self) -> Option<i64> {
        self.amount.signed_ct()
    }
}

/// The `Chrgs` block on an entry or a transaction.
///
/// ISO reshaped this across versions: up to `camt.05x.001.02` the charge sits
/// directly under `Chrgs` as an `Amt`/`CdtDbtInd` pair, and from `.001.04` it
/// moved into `0..n` `Rcrd` blocks with a total beside them. Both are read, and
/// the flat form is reported as a single record so a caller has one shape.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[non_exhaustive]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Charges {
    /// `TtlChrgsAndTaxAmt` — the bank's own total, when it states one.
    ///
    /// Present only in the newer shape, and not always then. Prefer
    /// [`total_signed_ct`](Self::total_signed_ct), which falls back to the
    /// records.
    pub total_ct: Option<i64>,
    /// ISO 4217 currency of `total_ct`.
    pub total_currency: Option<String>,
    /// Decoded `TtlChrgsAndTaxAmt` text, retained even when not representable.
    pub total_amount_raw: Option<String>,
    /// The individual charges.
    pub records: Vec<ChargeRecord>,
}

impl Charges {
    /// The summed signed charge in **ct**, or `None` on overflow or when any
    /// record's direction is unreadable.
    ///
    /// Taken from the records, which is the level that carries the
    /// credit/debit indicator; `TtlChrgsAndTaxAmt` is a magnitude with no sign
    /// of its own. All-or-nothing, like
    /// [`CashEntry::details_signed_sum_ct`]: a partial sum of fees understates
    /// them, and a fee understated is a fee somebody eats.
    #[must_use]
    pub fn total_signed_ct(&self) -> Option<i64> {
        if self.has_mixed_sources() {
            return None;
        }
        let currency = self.records.first()?.amount.currency.as_deref()?;
        if self.total_amount_raw.is_some() {
            if !self
                .total_currency
                .as_deref()?
                .eq_ignore_ascii_case(currency)
            {
                return None;
            }
            let magnitude = self.records.iter().try_fold(0i64, |sum, record| {
                let magnitude = record.amount.ct.filter(|value| *value >= 0)?;
                sum.checked_add(magnitude)
            })?;
            if self.total_ct? != magnitude {
                return None;
            }
        }
        self.records.iter().try_fold(0i64, |acc, record| {
            if !record.amount.is_currency(Some(currency)) {
                return None;
            }
            acc.checked_add(record.signed_ct()?)
        })
    }

    /// Whether every record says it is already inside the entry amount.
    ///
    /// `false` means not every record explicitly says included. It is **not**
    /// permission to post: inspect each record's inclusion, and escalate `None`.
    #[must_use]
    pub fn all_included_in_amount(&self) -> bool {
        !self.records.is_empty()
            && self
                .records
                .iter()
                .all(|r| r.included_in_amount == Some(true))
    }

    fn has_mixed_sources(&self) -> bool {
        self.records
            .iter()
            .any(|record| record.source == Some(ChargeRecordSource::LegacyFlat))
            && self
                .records
                .iter()
                .any(|record| record.source != Some(ChargeRecordSource::LegacyFlat))
    }

    fn parse(node: &Node) -> Option<Self> {
        let chrgs = node.child("Chrgs")?;
        // A charge record is never dropped for being unreadable: a lost fee is
        // a fee somebody eats, and `total_signed_ct` would then sum what is
        // left and report a confident, understated total.
        let record_of = |n: &Node, source| {
            let amount = ReportedAmount::parse(n, "Amt");
            let incl_raw = n.raw_text_of("ChrgInclInd");
            ChargeRecord {
                source: Some(source),
                amount,
                // The whole `xs:boolean` lexical space, and nothing else.
                included_in_amount: incl_raw.and_then(|v| match v.trim() {
                    "true" | "1" => Some(true),
                    "false" | "0" => Some(false),
                    _ => None,
                }),
                included_in_amount_raw: incl_raw.map(str::to_owned),
                type_code: n.child("Tp").and_then(Node::code).map(str::to_owned),
            }
        };

        let mut records: Vec<ChargeRecord> = chrgs
            .children_named("Rcrd")
            .map(|node| record_of(node, ChargeRecordSource::Record))
            .collect();
        // The pre-.001.04 shape puts the charge directly under `Chrgs`.
        let flat = record_of(chrgs, ChargeRecordSource::LegacyFlat);
        if flat.amount.amount_raw.is_some() {
            records.push(flat);
        }

        let total = ReportedAmount::parse(chrgs, "TtlChrgsAndTaxAmt");
        if records.is_empty() && total.amount_raw.is_none() {
            return None;
        }
        Some(Self {
            total_ct: total.ct,
            total_currency: total.currency,
            total_amount_raw: total.amount_raw,
            records,
        })
    }
}

// ── AccountRef ────────────────────────────────────────────────────────────────

/// The account a statement, report or notification is about (`Acct`).
///
/// ISO 20022 types the identifier as a choice — an `IBAN` **or** a proprietary
/// `Othr/Id` — so an account that is not IBAN-addressable is a legal thing for
/// a bank to send. Both are exposed, because collapsing the choice to a single
/// string cannot distinguish "no account identifier" from "an identifier that
/// is not an IBAN".
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[non_exhaustive]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AccountRef {
    /// `Acct/Id/IBAN`, when the account is IBAN-addressable.
    pub iban: Option<String>,
    /// `Acct/Id/Othr/Id` — the proprietary alternative to an IBAN.
    pub other_id: Option<String>,
    /// `Acct/Ccy` — the currency the account is denominated in.
    ///
    /// Worth checking before treating an entry amount as EUR: camt is not a
    /// EUR-only format, and an entry carries its own `Ccy` too.
    pub currency: Option<String>,
    /// BIC of the account servicing institution (`Acct/Svcr`).
    pub servicer_bic: Option<String>,
}

impl AccountRef {
    /// The IBAN, or the proprietary identifier when there is no IBAN.
    ///
    /// For display and logging. Match on [`iban`](Self::iban) directly when the
    /// distinction matters — only an IBAN can be validated or paid to.
    #[must_use]
    pub fn any_id(&self) -> Option<&str> {
        self.iban.as_deref().or(self.other_id.as_deref())
    }

    fn parse(node: &Node) -> Self {
        let Some(acct) = node.child("Acct") else {
            return Self::default();
        };
        let id = acct.child("Id");
        Self {
            iban: id.and_then(|i| i.text_of("IBAN")).map(str::to_owned),
            other_id: id
                .and_then(|i| i.text_at(&["Othr", "Id"]))
                .map(str::to_owned),
            currency: acct.text_of("Ccy").map(str::to_owned),
            servicer_bic: acct.child("Svcr").and_then(agent_bic).map(str::to_owned),
        }
    }
}

// ── BatchInfo ─────────────────────────────────────────────────────────────────

/// The `NtryDtls/Btch` block a bank attaches to an aggregate booking.
///
/// This is the element that closes the loop on a direct debit run: a batch
/// booking carries back the `PmtInfId` of the `PmtInf` that produced it, so a
/// camt entry can be matched to the group of a submitted pain.008 without
/// guessing from amounts and dates.
///
/// Its presence — not the number of `TxDtls` — is what the bank actually
/// asserts about a booking being aggregate; see
/// [`CashEntry::batch_booked`](CashEntry::batch_booked).
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[non_exhaustive]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BatchInfo {
    /// `Btch/MsgId` — the `GrpHdr/MsgId` of the message that was submitted.
    pub message_id: Option<String>,
    /// `Btch/PmtInfId` — the `PmtInf/PmtInfId` of the group that was submitted.
    pub payment_info_id: Option<String>,
    /// `Btch/NbOfTxs` — how many transactions the bank aggregated.
    ///
    /// This is the bank's own count, which may exceed the number of `TxDtls`
    /// elements it chose to itemise.
    pub transaction_count: Option<u64>,
}

impl BatchInfo {
    fn parse(node: &Node) -> Self {
        Self {
            message_id: node.text_of("MsgId").map(str::to_owned),
            payment_info_id: node.text_of("PmtInfId").map(str::to_owned),
            transaction_count: node.text_of("NbOfTxs").and_then(|n| n.parse().ok()),
        }
    }
}

// ── CashEntry ──────────────────────────────────────────────────────────────

/// A single booked or pending entry, shared by camt.052, camt.053 and camt.054.
///
/// The transaction-level fields live in [`details`](Self::details). Accessors
/// such as [`end_to_end_id`](Self::end_to_end_id) read the first detail, which
/// is what you want for an ordinary single-transaction entry; for a batch
/// booking, iterate `details` instead.
#[derive(Debug, Clone)]
#[non_exhaustive]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CashEntry {
    /// The booking amount, as far as the statement determined it.
    ///
    /// The entry is reported even when this resolves to nothing — a booking is
    /// never dropped for being unreadable, because a missing booking is
    /// indistinguishable from one that never happened.
    pub amount: ReportedAmount,
    /// Booking status.
    pub status: EntryStatus,
    /// `true` when the bank booked several transactions as one aggregate entry.
    ///
    /// Taken from the presence of [`batch`](Self::batch) — the bank's own
    /// assertion — and only otherwise inferred from there being more than one
    /// `TxDtls`. Counting details alone misses a batch of one, which is exactly
    /// the case where treating the entry as a single payment double-books it
    /// against a collection run.
    pub batch_booked: bool,
    /// The `NtryDtls/Btch` block, when the bank sent one.
    ///
    /// Carries the `MsgId` and `PmtInfId` of the message that produced this
    /// booking — see [`BatchInfo`].
    pub batch: Option<BatchInfo>,
    /// Booking date (`BookgDt`) exactly as the bank reported it.
    ///
    /// ISO 20022 types this as a date/time choice, so it arrives as
    /// `"2026-07-20"` from one bank and `"2026-07-20T09:14:00"` from the next.
    /// Read [`booking_date`](Self::booking_date) for the day.
    pub booking_date_raw: Option<String>,
    /// Value date (`ValDt`) exactly as the bank reported it — see
    /// [`booking_date_raw`](Self::booking_date_raw).
    pub value_date_raw: Option<String>,
    /// Bank's internal transaction reference (`AcctSvcrRef`).
    pub account_servicer_ref: Option<String>,
    /// Bank transaction code (`BkTxCd`), domain code where available.
    pub bank_tx_code: Option<String>,
    /// `Ntry/AddtlNtryInf` — the bank's free-text description of the booking.
    ///
    /// This is where several German banks put the text a customer sees on the
    /// statement, and for an entry with no `NtryDtls` at all it is often the
    /// only remittance information there is.
    pub additional_info: Option<String>,
    /// `Ntry/Chrgs` — charges the bank levied on this booking.
    ///
    /// A returned SEPA direct debit carries the return fee here or on the
    /// transaction detail, depending on the bank. Read
    /// [`Charges::all_included_in_amount`] before adding it to a ledger: a
    /// charge already inside the entry amount must not be posted twice.
    pub charges: Option<Charges>,
    /// Underlying transactions. Empty when the bank sends no `NtryDtls`.
    pub details: Vec<EntryDetail>,
}

impl CashEntry {
    /// Signed ledger amount: credit is positive (balance increase), debit is
    /// negative (balance decrease). `None` when the statement does not
    /// determine it.
    ///
    /// `None` means the statement did not say — the magnitude or the direction
    /// was missing or unreadable. Escalate the row; [`amount`](Self::amount)
    /// holds what arrived. There is no defensible default: an unknown direction
    /// taken for a credit is a double-sized error in a ledger.
    #[inline]
    #[must_use]
    pub fn signed_ct(&self) -> Option<i64> {
        self.amount.signed_ct()
    }

    /// The booking date — the day the entry hits the account balance.
    ///
    /// `None` when the bank reported none, or reported one this crate cannot
    /// read; [`booking_date_raw`](Self::booking_date_raw) still has whatever
    /// arrived.
    #[must_use]
    pub fn booking_date(&self) -> Option<crate::IsoDate> {
        parse_date(self.booking_date_raw.as_deref())
    }

    /// The value date — the day the entry earns or costs interest.
    #[must_use]
    pub fn value_date(&self) -> Option<crate::IsoDate> {
        parse_date(self.value_date_raw.as_deref())
    }

    /// The first transaction detail, if the entry has any.
    #[inline]
    #[must_use]
    pub fn first_detail(&self) -> Option<&EntryDetail> {
        self.details.first()
    }

    /// End-to-end reference of the first detail.
    #[must_use]
    pub fn end_to_end_id(&self) -> Option<&str> {
        self.first_detail()?.end_to_end_id.as_deref()
    }

    /// Mandate reference of the first detail.
    #[must_use]
    pub fn mandate_id(&self) -> Option<&str> {
        self.first_detail()?.mandate_id.as_deref()
    }

    /// SEPA Creditor Identifier of the first detail.
    #[must_use]
    pub fn creditor_id(&self) -> Option<&str> {
        self.first_detail()?.creditor_id.as_deref()
    }

    /// Remittance information of the first detail.
    #[must_use]
    pub fn reference(&self) -> Option<&str> {
        self.first_detail()?.reference.as_deref()
    }

    /// Counterparty name of the first detail.
    #[must_use]
    pub fn counterparty_name(&self) -> Option<&str> {
        self.first_detail()?.counterparty_name.as_deref()
    }

    /// Counterparty IBAN of the first detail.
    #[must_use]
    pub fn counterparty_iban(&self) -> Option<&str> {
        self.first_detail()?.counterparty_iban.as_deref()
    }

    /// Return reason code of the first detail.
    #[must_use]
    pub fn return_reason_code(&self) -> Option<&str> {
        self.first_detail()?.return_reason_code.as_deref()
    }

    /// Returns `true` if **any** underlying transaction is a return (Rückbuchung).
    ///
    /// Checks every detail, so a single returned collection inside a batch
    /// booking is still reported.
    #[must_use]
    pub fn is_return(&self) -> bool {
        self.details.iter().any(EntryDetail::is_return)
    }

    /// The sum of every detail's [`signed_ct`](EntryDetail::signed_ct), or
    /// `None` if any detail's amount could not be resolved.
    ///
    /// All-or-nothing on purpose: a partial sum silently understates a batch.
    /// An entry with no details sums to `Some(0)` — the sum over nothing, not
    /// the entry total; compare against [`signed_ct`](Self::signed_ct) with
    /// [`details_reconcile`](Self::details_reconcile) rather than substituting
    /// one for the other.
    #[must_use]
    pub fn details_signed_sum_ct(&self) -> Option<i64> {
        if self.details.is_empty() {
            return Some(0);
        }
        let currency = self.amount.currency.as_deref()?;
        let total = self.details.iter().try_fold(0_i128, |sum, detail| {
            if detail.signed_currency.as_deref() != Some(currency) {
                return None;
            }
            sum.checked_add(i128::from(detail.signed_ct()?))
        })?;
        i64::try_from(total).ok()
    }

    /// Whether the details resolve and add up to the entry's own signed amount.
    ///
    /// The reconciliation guard for a batch booking: `false` means the
    /// statement's parts do not account for its whole, and the entry should be
    /// escalated rather than posted.
    ///
    /// An entry with no details at all reconciles trivially — there is nothing
    /// to disagree with — so check [`details`](Self::details) too when a
    /// breakdown is required.
    ///
    /// # Examples
    ///
    /// ```
    /// use sepa::parse_camt053;
    ///
    /// let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
    /// <Document xmlns="urn:iso:std:iso:20022:tech:xsd:camt.053.001.08">
    ///   <BkToCstmrStmt><GrpHdr><MsgId>M</MsgId><CreDtTm>2026-07-21T23:59:00</CreDtTm></GrpHdr><Stmt><Id>S</Id>
    ///     <Ntry>
    ///       <Amt Ccy="EUR">125.00</Amt><CdtDbtInd>CRDT</CdtDbtInd><Sts><Cd>BOOK</Cd></Sts>
    ///       
    ///       <BkTxCd><Domn><Cd>PMNT</Cd><Fmly><Cd>RDDT</Cd><SubFmlyCd>PMDD</SubFmlyCd></Fmly></Domn></BkTxCd>
    /// <NtryDtls>
    ///         <TxDtls><Amt Ccy="EUR">100.00</Amt></TxDtls>
    ///         <TxDtls><Amt Ccy="EUR">25.00</Amt></TxDtls>
    ///       </NtryDtls>
    ///     </Ntry>
    ///   </Stmt></BkToCstmrStmt>
    /// </Document>"#;
    ///
    /// let doc = parse_camt053(xml)?;
    /// let entry = &doc.statements[0].entries[0];
    /// assert_eq!(entry.details_signed_sum_ct(), Some(12_500));
    /// assert!(entry.details_reconcile());
    /// # Ok::<(), sepa::Camt053ParseError>(())
    /// ```
    #[must_use]
    pub fn details_reconcile(&self) -> bool {
        if self.details.is_empty() {
            return true;
        }
        match (self.details_signed_sum_ct(), self.signed_ct()) {
            (Some(sum), Some(total)) => sum == total,
            // The entry's own amount or direction is unreadable, so there is
            // nothing to reconcile *against*. That is not agreement.
            _ => false,
        }
    }
}

/// The date part of an optional bank-supplied date or date-time.
fn parse_date(raw: Option<&str>) -> Option<crate::IsoDate> {
    crate::IsoDate::parse_date_part(raw?).ok()
}

/// Apply a credit/debit indicator to a magnitude.
///
/// Public/restored observations cannot be trusted to contain a valid magnitude.
const fn signed(indicator: CreditDebitIndicator, amount_ct: i64) -> Option<i64> {
    if amount_ct < 0 {
        return None;
    }
    match indicator {
        CreditDebitIndicator::Credit => Some(amount_ct),
        CreditDebitIndicator::Debit => amount_ct.checked_neg(),
    }
}

/// Every `Ustrd` under `RmtInf`, joined with a single space.
///
/// `RmtInf/Ustrd` is `0..n`. A German bank splits a long *Verwendungszweck*
/// across several 35-character occurrences, so reading only the first one
/// truncates the reference an invoice is matched by — usually right where the
/// invoice number sits. `None` when there is no non-empty occurrence.
fn joined_unstructured(rmt_inf: &Node) -> Option<String> {
    let mut out = String::new();
    for part in rmt_inf.children_named("Ustrd") {
        if part.text.is_empty() {
            continue;
        }
        if !out.is_empty() {
            out.push(' ');
        }
        out.push_str(&part.text);
    }
    (!out.is_empty()).then_some(out)
}

/// A party name, handling both the flat and the `Party40Choice` shapes.
///
/// camt.053.001.02 nests the name as `Dbtr/Nm`; from `.001.08` the party is
/// wrapped in a choice, giving `Dbtr/Pty/Nm`. Accept either.
pub(crate) fn party_name(parties: Option<&Node>, tag: &str) -> Option<String> {
    let party = parties?.child(tag)?;
    party
        .text_of("Nm")
        .or_else(|| party.text_at(&["Pty", "Nm"]))
        .map(str::to_owned)
}

pub(crate) fn parse_balance(b: &Node) -> StatementBalance {
    let balance_type = b
        .path(&["Tp", "CdOrPrtry"])
        .and_then(Node::code)
        .map_or(BalanceType::Unspecified, BalanceType::from_code);

    // `Dt` is a DateAndDateTimeChoice: `Dt/Dt` or `Dt/DtTm`.
    let date = b
        .child("Dt")
        .and_then(|d| d.text_of("Dt").or_else(|| d.text_of("DtTm")))
        .unwrap_or_default()
        .to_owned();

    StatementBalance {
        balance_type,
        amount: ReportedAmount::parse(b, "Amt"),
        date_raw: date,
    }
}

pub(crate) fn parse_entry(e: &Node) -> CashEntry {
    let amount = ReportedAmount::parse(e, "Amt");

    // `Sts` is a bare code up to camt.053.001.02 (`<Sts>BOOK</Sts>`) and a
    // choice from .001.08 (`<Sts><Cd>BOOK</Cd></Sts>`). `Node::code` accepts both.
    let status = e
        .child("Sts")
        .and_then(Node::code)
        .map_or(EntryStatus::Booked, EntryStatus::from_code);

    let date_of = |tag: &str| {
        e.child(tag)
            .and_then(|d| d.text_of("Dt").or_else(|| d.text_of("DtTm")))
            .map(str::to_owned)
    };

    let bank_tx_code = e.child("BkTxCd").and_then(|c| {
        c.text_at(&["Domn", "Cd"])
            .or_else(|| c.text_at(&["Prtry", "Cd"]))
            .map(str::to_owned)
    });

    // Every TxDtls is kept: a batch-booked SEPA collection carries one per
    // original transaction, and dropping all but the first loses the data
    // reconciliation actually needs.
    //
    // `TxDtls` belongs under `NtryDtls`, but some banks place it directly under
    // `Ntry`; that non-conformant shape is accepted rather than silently
    // yielding an entry with no details at all.
    let details_parent = e.child("NtryDtls").unwrap_or(e);
    let detail_count = details_parent.children_named("TxDtls").count();
    // `Btch` is the bank's own statement that this booking is an aggregate,
    // and it is authoritative: a batch of one transaction carries `Btch` but
    // only one `TxDtls`, so counting details alone would call it a single
    // payment.
    let batch = details_parent.child("Btch").map(BatchInfo::parse);
    let batch_booked = batch.is_some() || detail_count > 1;
    // The entry total may stand in for a detail's amount only when there is
    // exactly one transaction to attribute it to. Spreading it across a batch
    // would multiply the booking, so the bank's own `NbOfTxs` overrules the
    // number of details it happened to itemise.
    let sole_detail = detail_count == 1
        && batch
            .as_ref()
            .and_then(|b| b.transaction_count)
            .is_none_or(|n| n == 1);
    let details = details_parent
        .children_named("TxDtls")
        .map(|td| {
            parse_detail(
                td,
                &EntryContext {
                    amount: &amount,
                    sole_detail,
                },
            )
        })
        .collect();

    CashEntry {
        amount,
        status,
        batch_booked,
        batch,
        booking_date_raw: date_of("BookgDt"),
        value_date_raw: date_of("ValDt"),
        account_servicer_ref: e.text_of("AcctSvcrRef").map(str::to_owned),
        bank_tx_code,
        additional_info: e.text_of("AddtlNtryInf").map(str::to_owned),
        charges: Charges::parse(e),
        details,
    }
}

/// What the enclosing `Ntry` says, for resolving a detail's amount and sign.
pub(crate) struct EntryContext<'a> {
    pub(crate) amount: &'a ReportedAmount,
    pub(crate) sole_detail: bool,
}

pub(crate) fn parse_detail(td: &Node, entry: &EntryContext<'_>) -> EntryDetail {
    let refs = td.child("Refs");
    let ref_of = |tag: &str| refs.and_then(|r| r.text_of(tag)).map(str::to_owned);

    // `TxDtls/CdtDbtInd` is optional and overrides the entry's when present —
    // that is how a single returned collection inside a credit batch is
    // reported.
    let mut amount = ReportedAmount::parse(td, "Amt");
    let mut amount_source = td.child("Amt").map(|_| DetailAmountSource::Direct);
    // `TxDtls/CdtDbtInd` is optional and overrides the entry's when present.
    // When it is *absent* the entry's applies; when it is present but
    // unreadable it does not silently fall back, because that would resolve a
    // direction the detail itself contradicted.
    if amount.direction_raw.is_none() {
        amount.direction = entry.amount.direction;
    }
    let indicator = amount.direction;

    // Counterparty: for a credit the other side is the debtor, for a debit the creditor.
    let parties = td.child("RltdPties");
    let (name_tag, acct_tag) = match indicator {
        // An unknown direction cannot pick a side; `Dbtr`/`Cdtr` are then read
        // in that order so a name is still surfaced where the file has one.
        Some(CreditDebitIndicator::Credit) | None => ("Dbtr", "DbtrAcct"),
        Some(CreditDebitIndicator::Debit) => ("Cdtr", "CdtrAcct"),
    };

    // `TxDtls/Amt` is the transaction amount; `AmtDtls/TxAmt/Amt` carries the
    // same figure in the messages that omit the former. The direction stays the
    // one resolved above — only the magnitude and currency come from the
    // fallback.
    let transaction_amount = td.path(&["AmtDtls", "TxAmt"]).map(|transaction| {
        let mut reported = ReportedAmount::parse(transaction, "Amt");
        reported.direction = amount.direction;
        reported.direction_raw.clone_from(&amount.direction_raw);
        reported
    });
    if amount.amount_raw.is_none()
        && let Some(fallback) = &transaction_amount
    {
        amount.ct = fallback.ct;
        amount.currency.clone_from(&fallback.currency);
        amount.amount_raw.clone_from(&fallback.amount_raw);
        amount_source = Some(DetailAmountSource::TransactionAmount);
    }

    let signed_amount_ct = if amount.amount_raw.is_some() {
        // A foreign-currency transaction, or one whose currency (or the
        // entry's) the statement did not give: either way the figure is not
        // what hit the account and must not be summed against the entry total.
        if amount.is_currency(entry.amount.currency.as_deref()) {
            amount.signed_ct()
        } else {
            None
        }
    } else if entry.sole_detail && amount_source.is_none() {
        // A sole detail with no itemised amount inherits the entry's, which is
        // safe precisely because there is one transaction to attribute it to.
        entry.amount.signed_ct()
    } else {
        None
    };

    EntryDetail {
        amount,
        amount_source,
        transaction_amount,
        signed_amount_ct,
        signed_currency: entry.amount.currency.clone(),
        end_to_end_id: ref_of("EndToEndId"),
        mandate_id: ref_of("MndtId"),
        creditor_id: ref_of("CdtrId"),
        reference: td.child("RmtInf").and_then(joined_unstructured),
        counterparty_name: party_name(parties, name_tag),
        counterparty_iban: parties
            .and_then(|p| p.text_at(&[acct_tag, "Id", "IBAN"]))
            .map(str::to_owned),
        return_reason_code: td
            .path(&["RtrInf", "Rsn"])
            .and_then(Node::code)
            .map(str::to_owned),
        return_additional_info: td.text_at(&["RtrInf", "AddtlInf"]).map(str::to_owned),
        additional_info: td.text_of("AddtlTxInf").map(str::to_owned),
        charges: Charges::parse(td),
    }
}

// ── shared account / group helpers ────────────────────────────────────────────

/// The account a statement, report or notification is about.
pub(crate) fn account_of(node: &Node) -> AccountRef {
    AccountRef::parse(node)
}

/// `FinInstnId/BIC` (pre-2019) or `FinInstnId/BICFI` (2019 onwards).
///
/// ISO renamed the element in `camt.05x.001.03`; both spellings are accepted so
/// a version-agnostic caller never silently loses the agent BIC.
pub(crate) fn agent_bic(agent: &Node) -> Option<&str> {
    let fin = agent.child("FinInstnId")?;
    fin.text_of("BIC").or_else(|| fin.text_of("BICFI"))
}

/// The `FrToDt` reporting period, tolerating both the `FrDtTm`/`ToDtTm` and the
/// `FrDt`/`ToDt` spellings.
pub(crate) fn period(node: &Node) -> (Option<String>, Option<String>) {
    let range = node.child("FrToDt");
    let at = |dt_tm: &str, dt: &str| {
        range
            .and_then(|r| r.text_of(dt_tm).or_else(|| r.text_of(dt)))
            .map(str::to_owned)
    };
    (at("FrDtTm", "FrDt"), at("ToDtTm", "ToDt"))
}

/// Read `Ntry` children into entries, and `Bal` children into balances.
pub(crate) fn entries_of(node: &Node) -> Vec<CashEntry> {
    node.children_named("Ntry").map(parse_entry).collect()
}

/// Read `Bal` children into balances (camt.052 and camt.053 only).
pub(crate) fn balances_of(node: &Node) -> Vec<StatementBalance> {
    node.children_named("Bal").map(parse_balance).collect()
}

// ── tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::{CashEntry, CreditDebitIndicator};

    #[test]
    fn decoded_money_is_retained_and_conflicting_public_facts_are_unresolved() {
        let mut parsed =
            entry("<Ntry><Amt Ccy=\"EUR\"> \n1.00\t </Amt><CdtDbtInd>CRDT</CdtDbtInd></Ntry>");
        assert_eq!(parsed.amount.amount_raw.as_deref(), Some(" \n1.00\t "));
        assert_eq!(parsed.signed_ct(), Some(100));
        parsed.amount.ct = Some(200);
        assert_eq!(parsed.signed_ct(), None);
        parsed.amount.ct = Some(100);
        parsed.amount.direction = Some(CreditDebitIndicator::Debit);
        assert_eq!(parsed.signed_ct(), None);
        let mut detail = entry("<Ntry><Amt Ccy=\"EUR\">1.00</Amt><CdtDbtInd>CRDT</CdtDbtInd><NtryDtls><TxDtls><Amt Ccy=\"EUR\">1.00</Amt></TxDtls></NtryDtls></Ntry>").details.remove(0);
        detail.amount.ct = Some(-100);
        assert_eq!(detail.signed_ct(), None);
        let mut inherited = entry("<Ntry><Amt Ccy=\"EUR\">1.00</Amt><CdtDbtInd>CRDT</CdtDbtInd><NtryDtls><TxDtls/></NtryDtls></Ntry>").details.remove(0);
        assert_eq!(inherited.signed_ct(), Some(100));
        inherited.amount.direction = Some(CreditDebitIndicator::Debit);
        assert_eq!(inherited.signed_ct(), None);
    }

    #[cfg(feature = "serde")]
    #[test]
    fn restoration_cannot_inject_a_trusted_detail_cache() {
        let detail = entry("<Ntry><Amt Ccy=\"EUR\">1.00</Amt><CdtDbtInd>CRDT</CdtDbtInd><NtryDtls><TxDtls/></NtryDtls></Ntry>").details.remove(0);
        assert_eq!(detail.signed_ct(), Some(100));
        let mut value = serde_json::to_value(&detail).unwrap();
        value["signed_amount_ct"] = serde_json::json!(900);
        let restored: super::EntryDetail = serde_json::from_value(value).unwrap();
        assert_eq!(restored.signed_ct(), None);
    }

    #[test]
    fn invalid_magnitudes_and_missing_charge_directions_never_resolve() {
        for magnitude in [-1, i64::MIN] {
            let amount = super::ReportedAmount {
                ct: Some(magnitude),
                currency: Some("EUR".to_owned()),
                direction: Some(CreditDebitIndicator::Debit),
                ..Default::default()
            };
            assert_eq!(amount.signed_ct(), None);
            assert!(!amount.is_resolved());
        }
        let parsed = entry("<Ntry><Chrgs><Rcrd><Amt Ccy=\"EUR\">3.00</Amt></Rcrd></Chrgs></Ntry>");
        assert_eq!(parsed.charges.unwrap().total_signed_ct(), None);
        let parsed = entry("<Ntry><Amt Ccy=\"EUR\">-1.00</Amt><CdtDbtInd>DBIT</CdtDbtInd></Ntry>");
        assert_eq!(parsed.signed_ct(), None);
    }

    #[test]
    fn unrecorded_charge_totals_and_mixed_currencies_are_not_zero_or_a_sum() {
        let parsed = entry(
            "<Ntry><Chrgs><TtlChrgsAndTaxAmt Ccy=\"USD\">not-money</TtlChrgsAndTaxAmt></Chrgs></Ntry>",
        );
        assert!(parsed.charges.is_some());
        assert_eq!(parsed.charges.unwrap().total_signed_ct(), None);
        let parsed = entry(
            "<Ntry><Chrgs><Rcrd><Amt Ccy=\"EUR\">1.00</Amt><CdtDbtInd>DBIT</CdtDbtInd></Rcrd><Rcrd><Amt Ccy=\"USD\">2.00</Amt><CdtDbtInd>DBIT</CdtDbtInd></Rcrd></Chrgs></Ntry>",
        );
        assert_eq!(parsed.charges.unwrap().total_signed_ct(), None);
    }

    /// Parse a camt.053 statement whose single `Ntry` is `entry_xml`.
    fn entry(entry_xml: &str) -> CashEntry {
        let xml = format!(
            r#"<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:camt.053.001.08">
  <BkToCstmrStmt><GrpHdr><MsgId>M</MsgId><CreDtTm>2026-07-21T23:59:00</CreDtTm></GrpHdr><Stmt><Id>S</Id>
    {entry_xml}
  </Stmt></BkToCstmrStmt>
</Document>"#
        );
        crate::parse_camt053(&xml).unwrap().statements[0]
            .entries
            .remove(0)
    }

    #[test]
    fn an_itemised_detail_carries_its_own_signed_amount() {
        let e = entry(
            r#"<Ntry>
                 <Amt Ccy="EUR">125.00</Amt><CdtDbtInd>CRDT</CdtDbtInd>
                 <NtryDtls>
                   <TxDtls><Amt Ccy="EUR">100.00</Amt></TxDtls>
                   <TxDtls><Amt Ccy="EUR">25.00</Amt></TxDtls>
                 </NtryDtls>
               </Ntry>"#,
        );
        assert!(e.batch_booked);
        assert_eq!(e.details[0].signed_ct(), Some(10_000));
        assert_eq!(e.details[1].signed_ct(), Some(2_500));
        assert_eq!(e.details_signed_sum_ct(), Some(12_500));
        assert!(e.details_reconcile());
    }

    #[test]
    fn a_batch_with_no_itemised_amounts_reports_none_rather_than_the_entry_total() {
        // The whole point: reusing the entry total per detail would book
        // 2 × 125.00 EUR for a single 125.00 EUR entry.
        let e = entry(
            r#"<Ntry>
                 <Amt Ccy="EUR">125.00</Amt><CdtDbtInd>CRDT</CdtDbtInd>
                 <NtryDtls>
                   <TxDtls><Refs><EndToEndId>E1</EndToEndId></Refs></TxDtls>
                   <TxDtls><Refs><EndToEndId>E2</EndToEndId></Refs></TxDtls>
                 </NtryDtls>
               </Ntry>"#,
        );
        assert_eq!(e.details[0].signed_ct(), None);
        assert_eq!(e.details[1].signed_ct(), None);
        assert_eq!(e.details_signed_sum_ct(), None);
        assert!(!e.details_reconcile());
    }

    #[test]
    fn a_sole_detail_inherits_the_entry_amount() {
        // Safe precisely because there is only one transaction to attribute it
        // to — which is the ordinary single-payment booking.
        let e = entry(
            r#"<Ntry>
                 <Amt Ccy="EUR">75.00</Amt><CdtDbtInd>DBIT</CdtDbtInd>
                 <NtryDtls><TxDtls><Refs><MndtId>MND-1</MndtId></Refs></TxDtls></NtryDtls>
               </Ntry>"#,
        );
        assert!(!e.batch_booked);
        assert_eq!(e.details[0].amount.ct, None, "nothing was itemised");
        assert_eq!(e.details[0].signed_ct(), Some(-7_500));
        assert!(e.details_reconcile());
    }

    #[test]
    fn a_detail_level_indicator_overrides_the_entry_level_one() {
        // A returned collection inside an otherwise-credit batch.
        let e = entry(
            r#"<Ntry>
                 <Amt Ccy="EUR">75.00</Amt><CdtDbtInd>CRDT</CdtDbtInd>
                 <NtryDtls>
                   <TxDtls><Amt Ccy="EUR">100.00</Amt></TxDtls>
                   <TxDtls>
                     <Amt Ccy="EUR">25.00</Amt><CdtDbtInd>DBIT</CdtDbtInd>
                     <RtrInf><Rsn><Cd>MD01</Cd></Rsn></RtrInf>
                   </TxDtls>
                 </NtryDtls>
               </Ntry>"#,
        );
        assert_eq!(
            e.details[0].amount.direction,
            Some(CreditDebitIndicator::Credit)
        );
        assert_eq!(
            e.details[1].amount.direction,
            Some(CreditDebitIndicator::Debit)
        );
        assert_eq!(e.details[1].signed_ct(), Some(-2_500));
        assert_eq!(e.details_signed_sum_ct(), Some(7_500));
        assert!(e.details_reconcile());
        assert!(e.is_return());
        assert!(e.details[1].is_return());
    }

    #[test]
    fn amt_dtls_tx_amt_stands_in_for_a_missing_tx_amount() {
        // The shape several German banks send from camt.05x.001.08 onwards.
        let e = entry(
            r#"<Ntry>
                 <Amt Ccy="EUR">30.00</Amt><CdtDbtInd>DBIT</CdtDbtInd>
                 <NtryDtls>
                   <TxDtls><AmtDtls><TxAmt><Amt Ccy="EUR">10.00</Amt></TxAmt></AmtDtls></TxDtls>
                   <TxDtls><AmtDtls><TxAmt><Amt Ccy="EUR">20.00</Amt></TxAmt></AmtDtls></TxDtls>
                 </NtryDtls>
               </Ntry>"#,
        );
        assert_eq!(e.details[0].signed_ct(), Some(-1_000));
        assert_eq!(e.details[1].signed_ct(), Some(-2_000));
        assert!(e.details_reconcile());
    }

    #[test]
    fn a_foreign_currency_detail_is_reported_but_not_summed() {
        // The figure is real; it is simply not the amount that hit the account,
        // so adding it to the entry total would be wrong.
        let e = entry(
            r#"<Ntry>
                 <Amt Ccy="EUR">92.00</Amt><CdtDbtInd>CRDT</CdtDbtInd>
                 <NtryDtls><TxDtls><Amt Ccy="USD">100.00</Amt></TxDtls></NtryDtls>
               </Ntry>"#,
        );
        assert_eq!(e.details[0].amount.ct, Some(10_000));
        assert_eq!(e.details[0].amount.currency.as_deref(), Some("USD"));
        assert_eq!(e.details[0].signed_ct(), None);
        assert!(!e.details_reconcile());
    }

    #[test]
    fn dates_are_typed_whichever_choice_form_the_bank_used() {
        // `BookgDt` is a DateAndDateTimeChoice: a bare date from one bank, a
        // timestamp from the next. Both post on the same day.
        let bare = entry(
            r#"<Ntry>
                 <Amt Ccy="EUR">10.00</Amt><CdtDbtInd>CRDT</CdtDbtInd>
                 <BookgDt><Dt>2026-07-14</Dt></BookgDt>
                 <ValDt><Dt>2026-07-15</Dt></ValDt>
               </Ntry>"#,
        );
        let stamped = entry(
            r#"<Ntry>
                 <Amt Ccy="EUR">10.00</Amt><CdtDbtInd>CRDT</CdtDbtInd>
                 <BookgDt><DtTm>2026-07-14T09:14:00</DtTm></BookgDt>
               </Ntry>"#,
        );
        let day = crate::IsoDate::new(2026, 7, 14).unwrap();
        assert_eq!(bare.booking_date(), Some(day));
        assert_eq!(stamped.booking_date(), Some(day));
        assert_eq!(
            bare.value_date(),
            Some(crate::IsoDate::new(2026, 7, 15).unwrap())
        );

        // The raw text survives either way.
        assert_eq!(
            stamped.booking_date_raw.as_deref(),
            Some("2026-07-14T09:14:00")
        );

        // A missing or unreadable date is `None`, never a panic — the field is
        // bank-supplied.
        let absent = entry(r#"<Ntry><Amt Ccy="EUR">10.00</Amt></Ntry>"#);
        assert_eq!(absent.booking_date(), None);
        let nonsense = entry(
            r#"<Ntry><Amt Ccy="EUR">10.00</Amt><BookgDt><Dt>14.07.2026</Dt></BookgDt></Ntry>"#,
        );
        assert_eq!(nonsense.booking_date(), None);
        assert_eq!(nonsense.booking_date_raw.as_deref(), Some("14.07.2026"));
    }

    #[test]
    fn a_return_fee_is_read_at_either_level_and_in_either_shape() {
        // A returned direct debit is where charges actually matter: the fee is
        // real money the creditor is out, and it is reported beside the entry
        // rather than inside its amount.
        let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:camt.053.001.08">
  <BkToCstmrStmt><GrpHdr><MsgId>M</MsgId><CreDtTm>2026-07-21T23:59:00</CreDtTm></GrpHdr><Stmt><Id>S</Id><Acct><Id><IBAN>DE89370400440532013000</IBAN></Id></Acct><Bal><Tp><CdOrPrtry><Cd>CLBD</Cd></CdOrPrtry></Tp><Amt Ccy="EUR">0.00</Amt><CdtDbtInd>CRDT</CdtDbtInd><Dt><Dt>2026-07-21</Dt></Dt></Bal>
    <Ntry>
      <Amt Ccy="EUR">75.00</Amt><CdtDbtInd>DBIT</CdtDbtInd><Sts><Cd>BOOK</Cd></Sts>
      <BkTxCd><Domn><Cd>PMNT</Cd><Fmly><Cd>RDDT</Cd><SubFmlyCd>PMDD</SubFmlyCd></Fmly></Domn></BkTxCd>
        <Chrgs>
        <TtlChrgsAndTaxAmt Ccy="EUR">3.00</TtlChrgsAndTaxAmt>
        <Rcrd><Amt Ccy="EUR">3.00</Amt><CdtDbtInd>DBIT</CdtDbtInd>
              <ChrgInclInd>false</ChrgInclInd>
              <Tp><Prtry><Id>RETURN_FEE</Id></Prtry></Tp></Rcrd>
      </Chrgs>
      
        <NtryDtls><TxDtls>
        <Amt Ccy="EUR">75.00</Amt>
        <Chrgs><Rcrd><Amt Ccy="EUR">1.50</Amt><CdtDbtInd>DBIT</CdtDbtInd></Rcrd></Chrgs>
        <RtrInf><Rsn><Cd>MS02</Cd></Rsn></RtrInf>
      </TxDtls></NtryDtls>
    </Ntry>
  </Stmt></BkToCstmrStmt>
</Document>"#;
        let doc = crate::parse_camt053(xml).unwrap();
        let entry = &doc.statements[0].entries[0];

        let charges = entry.charges.as_ref().expect("Ntry/Chrgs must be read");
        assert_eq!(charges.total_ct, Some(300));
        assert_eq!(charges.total_signed_ct(), Some(-300));
        assert_eq!(charges.records[0].type_code.as_deref(), Some("RETURN_FEE"));
        // Stated as separate, so a ledger must post it in addition to the entry.
        assert!(!charges.all_included_in_amount());
        assert_eq!(charges.records[0].included_in_amount, Some(false));

        // The detail carries its own, which is where several German banks put it.
        let detail = &entry.details[0];
        assert!(detail.is_return());
        assert_eq!(
            detail.charges.as_ref().unwrap().total_signed_ct(),
            Some(-150)
        );

        // The pre-.001.04 shape puts the charge directly under `Chrgs`.
        let flat = xml.replace(
            "<Chrgs><Rcrd><Amt Ccy=\"EUR\">1.50</Amt><CdtDbtInd>DBIT</CdtDbtInd></Rcrd></Chrgs>",
            "<Chrgs><Amt Ccy=\"EUR\">1.50</Amt><CdtDbtInd>DBIT</CdtDbtInd></Chrgs>",
        );
        let doc = crate::parse_camt053(&flat).unwrap();
        let detail = &doc.statements[0].entries[0].details[0];
        assert_eq!(
            detail.charges.as_ref().unwrap().total_signed_ct(),
            Some(-150)
        );
    }

    #[test]
    fn a_charge_that_does_not_say_whether_it_is_included_is_not_assumed_to_be() {
        // `ChrgInclInd` is optional, and "the bank did not say" is a third
        // answer. Treating silence as "included" would silently drop a fee;
        // treating it as "separate" would silently double-count one. Both are
        // wrong, so `all_included_in_amount` is false and the caller decides.
        let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:camt.053.001.08">
  <BkToCstmrStmt><GrpHdr><MsgId>M</MsgId><CreDtTm>2026-07-21T23:59:00</CreDtTm></GrpHdr><Stmt><Id>S</Id><Acct><Id><IBAN>DE89370400440532013000</IBAN></Id></Acct><Bal><Tp><CdOrPrtry><Cd>CLBD</Cd></CdOrPrtry></Tp><Amt Ccy="EUR">0.00</Amt><CdtDbtInd>CRDT</CdtDbtInd><Dt><Dt>2026-07-21</Dt></Dt></Bal>
    <Ntry><Amt Ccy="EUR">10.00</Amt><CdtDbtInd>DBIT</CdtDbtInd><Sts><Cd>BOOK</Cd></Sts>
      <BkTxCd><Domn><Cd>PMNT</Cd><Fmly><Cd>RDDT</Cd><SubFmlyCd>PMDD</SubFmlyCd></Fmly></Domn></BkTxCd>
        <Chrgs><Rcrd><Amt Ccy="EUR">2.00</Amt></Rcrd></Chrgs>
        
      </Ntry>
  </Stmt></BkToCstmrStmt>
</Document>"#;
        let doc = crate::parse_camt053(xml).unwrap();
        let charges = doc.statements[0].entries[0].charges.as_ref().unwrap();
        assert_eq!(charges.records[0].included_in_amount, None);
        assert!(!charges.all_included_in_amount());
        assert_eq!(charges.records[0].signed_ct(), None);
    }

    #[test]
    fn the_btch_element_identifies_the_submitted_group() {
        // The reconciliation loop closes here: a batch booking names the
        // PmtInfId of the PmtInf that produced it.
        let e = entry(
            r#"<Ntry>
                 <Amt Ccy="EUR">125.00</Amt><CdtDbtInd>DBIT</CdtDbtInd>
                 <NtryDtls>
                   <Btch>
                     <MsgId>DD-2026-07</MsgId>
                     <PmtInfId>DD-2026-07-1</PmtInfId>
                     <NbOfTxs>2</NbOfTxs>
                   </Btch>
                   <TxDtls><Amt Ccy="EUR">100.00</Amt></TxDtls>
                   <TxDtls><Amt Ccy="EUR">25.00</Amt></TxDtls>
                 </NtryDtls>
               </Ntry>"#,
        );
        let batch = e.batch.as_ref().unwrap();
        assert_eq!(batch.message_id.as_deref(), Some("DD-2026-07"));
        assert_eq!(batch.payment_info_id.as_deref(), Some("DD-2026-07-1"));
        assert_eq!(batch.transaction_count, Some(2));
        assert!(e.batch_booked);
        assert!(e.details_reconcile());
    }

    #[test]
    fn a_batch_of_one_is_still_a_batch() {
        // Regression: `batch_booked` was `TxDtls count > 1`, so a single-
        // transaction collection run read as an ordinary payment — and the
        // entry total was then attributed to the one itemised detail even
        // though the bank said it aggregated three.
        let e = entry(
            r#"<Ntry>
                 <Amt Ccy="EUR">125.00</Amt><CdtDbtInd>DBIT</CdtDbtInd>
                 <NtryDtls>
                   <Btch><PmtInfId>DD-1</PmtInfId><NbOfTxs>3</NbOfTxs></Btch>
                   <TxDtls><Refs><EndToEndId>E1</EndToEndId></Refs></TxDtls>
                 </NtryDtls>
               </Ntry>"#,
        );
        assert!(e.batch_booked, "Btch is the bank's own assertion");
        assert_eq!(
            e.details[0].signed_ct(),
            None,
            "3 transactions were aggregated; the entry total is not this one's"
        );
        assert!(!e.details_reconcile());

        // With NbOfTxs = 1 the identity is safe again.
        let single = entry(
            r#"<Ntry>
                 <Amt Ccy="EUR">75.00</Amt><CdtDbtInd>DBIT</CdtDbtInd>
                 <NtryDtls>
                   <Btch><NbOfTxs>1</NbOfTxs></Btch>
                   <TxDtls><Refs><MndtId>MND-1</MndtId></Refs></TxDtls>
                 </NtryDtls>
               </Ntry>"#,
        );
        assert!(single.batch_booked);
        assert_eq!(single.details[0].signed_ct(), Some(-7_500));
    }

    #[test]
    fn the_banks_free_text_survives_at_both_levels() {
        // `AddtlNtryInf` is where several German banks put the statement text,
        // and for an entry with no NtryDtls it is the only description there
        // is. Both it and `AddtlTxInf` used to be dropped on the floor.
        let e = entry(
            r#"<Ntry>
                 <Amt Ccy="EUR">75.00</Amt><CdtDbtInd>DBIT</CdtDbtInd>
                 <AddtlNtryInf>SEPA-LASTSCHRIFT EINZUG</AddtlNtryInf>
                 <NtryDtls><TxDtls>
                   <AddtlTxInf>Kundennummer 4711</AddtlTxInf>
                   <RtrInf><Rsn><Cd>AM04</Cd></Rsn><AddtlInf>Konto nicht gedeckt</AddtlInf></RtrInf>
                 </TxDtls></NtryDtls>
               </Ntry>"#,
        );
        assert_eq!(
            e.additional_info.as_deref(),
            Some("SEPA-LASTSCHRIFT EINZUG")
        );
        assert_eq!(
            e.details[0].additional_info.as_deref(),
            Some("Kundennummer 4711")
        );
        assert_eq!(e.details[0].return_reason_code.as_deref(), Some("AM04"));
        assert_eq!(
            e.details[0].return_additional_info.as_deref(),
            Some("Konto nicht gedeckt")
        );
    }

    #[test]
    fn a_remittance_split_across_several_ustrd_is_read_whole() {
        // Regression: only the first `Ustrd` was read. German banks split a
        // long Verwendungszweck into 35-character occurrences, so the invoice
        // number — which usually sits at the end — was being thrown away.
        let e = entry(
            r#"<Ntry>
                 <Amt Ccy="EUR">75.00</Amt><CdtDbtInd>CRDT</CdtDbtInd>
                 <NtryDtls><TxDtls><RmtInf>
                   <Ustrd>Rechnung 2026-07 Teilzahlung 1 von</Ustrd>
                   <Ustrd>3, Kundennummer 4711</Ustrd>
                   <Ustrd>RG-NR 2026-000815</Ustrd>
                 </RmtInf></TxDtls></NtryDtls>
               </Ntry>"#,
        );
        assert_eq!(
            e.reference(),
            Some("Rechnung 2026-07 Teilzahlung 1 von 3, Kundennummer 4711 RG-NR 2026-000815")
        );

        // A single occurrence is unchanged, and an empty one reads as absent.
        let one = entry(
            r#"<Ntry><Amt Ccy="EUR">1.00</Amt><CdtDbtInd>CRDT</CdtDbtInd>
                 <NtryDtls><TxDtls><RmtInf><Ustrd>Miete Juli</Ustrd></RmtInf></TxDtls></NtryDtls>
               </Ntry>"#,
        );
        assert_eq!(one.reference(), Some("Miete Juli"));
        let none = entry(
            r#"<Ntry><Amt Ccy="EUR">1.00</Amt><CdtDbtInd>CRDT</CdtDbtInd>
                 <NtryDtls><TxDtls><RmtInf><Ustrd/></RmtInf></TxDtls></NtryDtls>
               </Ntry>"#,
        );
        assert_eq!(none.reference(), None);
    }

    #[test]
    fn an_entry_without_details_reconciles_trivially() {
        let e = entry(r#"<Ntry><Amt Ccy="EUR">10.00</Amt><CdtDbtInd>CRDT</CdtDbtInd></Ntry>"#);
        assert_eq!(e.details.len(), 0);
        assert_eq!(e.details_signed_sum_ct(), Some(0));
        assert!(e.details_reconcile());
    }

    #[test]
    fn a_mismatched_batch_is_reported_as_not_reconciling() {
        // The statement's parts do not account for its whole: escalate rather
        // than post.
        let e = entry(
            r#"<Ntry>
                 <Amt Ccy="EUR">125.00</Amt><CdtDbtInd>CRDT</CdtDbtInd>
                 <NtryDtls>
                   <TxDtls><Amt Ccy="EUR">100.00</Amt></TxDtls>
                   <TxDtls><Amt Ccy="EUR">20.00</Amt></TxDtls>
                 </NtryDtls>
               </Ntry>"#,
        );
        assert_eq!(e.details_signed_sum_ct(), Some(12_000));
        assert_eq!(e.signed_ct(), Some(12_500));
        assert!(!e.details_reconcile());
    }

    /// Every type in this module that reports a ledger figure is accounted
    /// for, so a new one cannot be added silently.
    ///
    /// `tests/conformance.rs::cross_level_consistency` requires these types to
    /// answer identically for an unreadable code — but it *names* them, and a
    /// test whose coverage is a list stops covering things. The list is
    /// checked against the module's own source here, so a fifth `signed_ct`
    /// fails the build until somebody classifies it.
    #[test]
    fn every_type_that_reports_money_is_covered_by_the_cross_level_gate() {
        const SRC: &str = include_str!("camt.rs");

        /// Types whose `signed_ct` delegates to [`ReportedAmount`], so the
        /// resolution rule is written once. `tests/conformance.rs` feeds all
        /// of these one unreadable code and requires identical answers.
        const DELEGATES: &[&str] = &["StatementBalance", "ChargeRecord", "CashEntry"];
        /// Types that deliberately answer differently, each with a reason.
        const DIVERGES: &[(&str, &str)] = &[
            ("ReportedAmount", "the definition the others delegate to"),
            (
                "EntryDetail",
                "reports a *resolved* figure that may be inherited from its \
                 entry, so it is not a function of its own `amount` alone",
            ),
        ];

        // Walk the source for `impl <Type> {` blocks containing `fn signed_ct`.
        let mut found: Vec<&str> = Vec::new();
        let mut current: Option<&str> = None;
        for line in SRC.lines() {
            if let Some(rest) = line.strip_prefix("impl ") {
                current = rest.split_whitespace().next().map(str::trim);
            }
            if line.contains("fn signed_ct(")
                && let Some(ty) = current
            {
                found.push(ty);
            }
        }
        found.sort_unstable();
        found.dedup();

        let mut accounted: Vec<&str> = DELEGATES
            .iter()
            .copied()
            .chain(DIVERGES.iter().map(|(t, _)| *t))
            .collect();
        accounted.sort_unstable();

        assert_eq!(
            found, accounted,
            "a `signed_ct` in this module is not accounted for.\n\
             Add it to DELEGATES (and to the four types named in \
             tests/conformance.rs::cross_level_consistency), or to DIVERGES \
             with the reason it may answer differently.\n\
             found:     {found:?}\n\
             accounted: {accounted:?}"
        );

        // The guard is only worth having if it can fail, so prove the walk
        // actually sees something rather than comparing two empty lists.
        assert!(
            found.len() >= 5,
            "the source walk found {} types — it has stopped working",
            found.len()
        );
    }
}