fints-client 0.1.0

FinTS 3.0 PIN/TAN client for German online banking: accounts, balances, transactions, holdings, SEPA transfers and the payee check
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
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
//! FinTS 3.0 Protocol State Machine (spec-aligned).
//!
//! Implements the dialog lifecycle as defined in FinTS 3.0 Formals (2017-10-06)
//! and PIN/TAN Security (2020-07-10).
//!
//! ## Dialog types (spec Chapter C)
//!
//! - **Synchronization dialog**: HKIDN + HKVVB + HKSYN → get system_id → HKEND.
//!   Marked by presence of HKSYN. Business segments are forbidden (banks return 9110).
//! - **Normal dialog**: HKIDN + HKVVB [+ HKTAN:4] → [TAN confirmation] → business ops → HKEND.
//!
//! ## State machine (spec Chapter C, Section 6)
//!
//! Internal: the typestate `Dialog<S>` (`New`, `Synced`, `Open`,
//! `TanPending`) keeps the dialog's authentication state in the type system.
//! [`crate::jobs::Dialog`] wraps it for callers and adds the pause/resume
//! phases, so the library never waits for a TAN or an app confirmation.
//!
//! ```text
//!   Dialog<New>
//!       ├── sync()           → (Dialog<Synced>, Response)      [sync dialog]
//!       ├── init()           → InitResult                      [normal dialog]
//!       │     ├── Opened(Dialog<Open>)                            0010/0020
//!       │     └── TanRequired(Dialog<TanPending>, TanChallenge)   0030/3955
//!       └── init_no_tan()    → (Dialog<Open>, Response)        [PIN-only login]
//!
//!   Dialog<Synced>
//!       └── end()            → (BankParams, SystemId)
//!
//!   Dialog<Open>
//!       └── send_order()     → Response                        [one order]
//! ```

use std::collections::BTreeSet;
use std::marker::PhantomData;
use tracing::{debug, info, warn};

use jiff::civil::Date;

use crate::error::{BankError, FinTSError, Result};
use crate::message;
use crate::parser::RawSegment;
pub use crate::segments::Order;
use crate::segments::read::{extract_allowed_security_functions, find_touchdown};

use crate::quirks::QuirkChain;
use crate::return_code::{
    Category, Class, ClientWarning, Kind, Notice, NoticeDialog, ReturnCode, Scope,
    looks_like_utf8_as_latin1,
};
use crate::transport::SharedTransport;
use crate::types::*;

mod params;
mod tan;
pub use params::{
    AccountLimit, Allowance, AllowedJob, BalanceParams, Bpd, CamtParams, FormatLists,
    HoldingsParams, InstantParams, InstantStatusParams, InstantSupport, JobDetail, JobParams,
    JobVersion, LimitKind, ParamsError, PinTanParams, SepaParams, StatementParams, TanParams,
    TanVersion, UnparsedParam, Upd, UpdAccount, UpdUsage, VersionMismatch, VopDelivery, VopParams,
};
use params::{bpd_after, upd_after};
pub use tan::{
    Retry, TanDecision, TanOverride, TanOverrides, TanReason, on_sca_required, order_tan,
};

// ═══════════════════════════════════════════════════════════════════════════════
// Typestate markers (per spec dialog states)
// ═══════════════════════════════════════════════════════════════════════════════

/// Dialog not yet started. Can transition to `Synced` or `Open`/`TanPending`.
#[derive(Debug)]
pub(crate) struct New;
/// Synchronization dialog: system_id obtained, BPD/UPD cached. No business ops allowed.
#[derive(Debug)]
pub(crate) struct Synced;
/// Dialog is open and authenticated. Business segments can be sent.
#[derive(Debug)]
pub(crate) struct Open;
/// A TAN challenge is pending (either from init or from a business segment).
#[derive(Debug)]
pub(crate) struct TanPending;

// ═══════════════════════════════════════════════════════════════════════════════
// Typed business segments — replaces raw Vec<DEG> at all internal boundaries
// ═══════════════════════════════════════════════════════════════════════════════

/// A typed FinTS business segment. Every field is validated at construction.
/// This is the ONLY way to construct segments within the crate — raw DEG
/// builders are never called directly from protocol or job code.
#[derive(Debug, Clone)]
pub(crate) enum Segment {
    /// HKIDN: Identifikation (dialog init)
    Identify {
        blz: Blz,
        user_id: UserId,
        system_id: SystemId,
    },
    /// HKVVB: Verarbeitungsvorbereitung
    ProcessPrep {
        bpd_version: u16,
        upd_version: u16,
        product_id: ProductId,
    },
    /// HKSYN: Synchronisierung (get system_id)
    Sync,
    /// HKSAL: Saldenabfrage (balance request)
    Balance { account: Account },
    /// HKKAZ: Kontoumsätze (transaction request)
    Transactions {
        account: Account,
        start_date: Date,
        end_date: Date,
        touchdown: Option<TouchdownPoint>,
    },
    /// HKCAZ: Kontoumsätze im camt-Format (CAMT.052 transaction request)
    CamtTransactions {
        account: Account,
        start_date: Date,
        end_date: Date,
        touchdown: Option<TouchdownPoint>,
    },
    /// HKWPD: Wertpapierdepotaufstellung (securities holdings request)
    Holdings {
        account: Account,
        currency: Option<Currency>,
        touchdown: Option<TouchdownPoint>,
    },
    /// HKWDU: Depotumsätze (depot transactions request)
    DepotTransactions {
        account: Account,
        start_date: Option<Date>,
        end_date: Option<Date>,
        touchdown: Option<TouchdownPoint>,
    },
    /// HKTAN process 4: initiate TAN for a referenced segment
    TanProcess4 {
        reference_seg: SegmentRef,
        tan_medium: Option<TanMediumName>,
    },
    /// HKTAN process S: poll decoupled TAN status
    TanPollDecoupled {
        task_reference: TaskReference,
        tan_medium: Option<TanMediumName>,
    },
    /// HKTAN process 2: submit TAN response
    TanProcess2 {
        task_reference: TaskReference,
        tan_medium: Option<TanMediumName>,
    },
    /// HKEND: dialog end
    End { dialog_id: DialogId },
    /// HKCCS: SEPA credit transfer, the pain.001 bytes as built.
    CreditTransfer {
        account: Account,
        descriptor: String,
        pain: Vec<u8>,
    },
    /// HKIPZ: SEPA instant credit transfer at `version` (chosen before
    /// the pain message was built); `convert` is v2's conversion flag.
    InstantTransfer {
        account: Account,
        version: u16,
        descriptor: String,
        pain: Vec<u8>,
        convert: Option<bool>,
    },
    /// HKIPS: status of an instant payment; `descriptors` are the pain
    /// messages the client reads in the answer.
    InstantStatus {
        account: Account,
        descriptors: Vec<String>,
        order_id: String,
    },
    /// HKVPP: payee check; a poll carries the polling ID and touchdown.
    VopCheck {
        descriptors: Vec<String>,
        polling_id: Option<Vec<u8>>,
        touchdown: Option<String>,
    },
    /// HKVPA: release after the payee check.
    VopExecute { vop_id: Vec<u8> },
}

impl Segment {
    /// Encode with the given segment number, choosing segment versions from
    /// the bank's BPD.
    pub(crate) fn encode(&self, params: &BankParams, number: u16) -> Result<RawSegment> {
        use crate::segments::{encode, request};
        fn td(t: &Option<TouchdownPoint>) -> Option<&str> {
            t.as_ref().map(TouchdownPoint::as_str)
        }
        let national = params.national_account_allowed();
        let with_flag = |a: &Account| a.clone().with_kti_national(national);
        match self {
            Segment::Identify {
                blz,
                user_id,
                system_id,
            } => encode(
                &request::hkidn(blz.as_str(), user_id.as_str(), system_id.as_str())?,
                number,
            ),
            Segment::ProcessPrep {
                bpd_version,
                upd_version,
                product_id,
            } => encode(
                &request::hkvvb(*bpd_version, *upd_version, product_id.as_str())?,
                number,
            ),
            Segment::Sync => encode(&request::hksyn(), number),
            Segment::End { dialog_id } => encode(&request::hkend(dialog_id.as_str())?, number),
            Segment::TanProcess4 {
                reference_seg,
                tan_medium,
            } => request::hktan(
                params.hktan_version()?,
                request::TanRequest::Start {
                    segment: reference_seg.as_str(),
                },
                tan_medium.as_ref().map(|t| t.as_str()),
                number,
            ),
            Segment::TanPollDecoupled {
                task_reference,
                tan_medium,
            } => request::hktan(
                params.hktan_version()?,
                request::TanRequest::DecoupledStatus {
                    task_reference: task_reference.as_str(),
                },
                tan_medium.as_ref().map(|t| t.as_str()),
                number,
            ),
            Segment::TanProcess2 {
                task_reference,
                tan_medium,
            } => request::hktan(
                params.hktan_version()?,
                request::TanRequest::Submit {
                    task_reference: task_reference.as_str(),
                },
                tan_medium.as_ref().map(|t| t.as_str()),
                number,
            ),
            Segment::CreditTransfer {
                account,
                descriptor,
                pain,
            } => {
                let version = params.order_version(Order::Transfer)?;
                request::hkccs(number, version, &with_flag(account), descriptor, pain)
            }
            Segment::InstantTransfer {
                account,
                version,
                descriptor,
                pain,
                convert,
            } => request::hkipz(
                number,
                *version,
                &with_flag(account),
                descriptor,
                pain,
                *convert,
            ),
            Segment::InstantStatus {
                account,
                descriptors,
                order_id,
            } => {
                let version = params.order_version(Order::InstantStatus)?;
                request::hkips(number, version, &with_flag(account), descriptors, order_id)
            }
            Segment::VopCheck {
                descriptors,
                polling_id,
                touchdown,
            } => {
                let version = params.order_version(Order::VopCheck)?;
                request::hkvpp(
                    number,
                    version,
                    descriptors,
                    polling_id.as_deref(),
                    touchdown.as_deref(),
                )
            }
            Segment::VopExecute { vop_id } => {
                let version = params.order_version(Order::VopExecute)?;
                request::hkvpa(number, version, vop_id)
            }
            Segment::Balance { account } => {
                let version = params.order_version(Order::Balance)?;
                request::hksal(number, version, &with_flag(account), None)
            }
            Segment::Transactions {
                account,
                start_date,
                end_date,
                touchdown,
            } => {
                let version = params.order_version(Order::Transactions)?;
                request::hkkaz(
                    number,
                    version,
                    &with_flag(account),
                    *start_date,
                    *end_date,
                    td(touchdown),
                )
            }
            Segment::CamtTransactions {
                account,
                start_date,
                end_date,
                touchdown,
            } => {
                let version = params.order_version(Order::Camt)?;
                request::hkcaz(
                    number,
                    version,
                    &with_flag(account),
                    &params.camt052_format(),
                    *start_date,
                    *end_date,
                    td(touchdown),
                )
            }
            Segment::Holdings {
                account,
                currency,
                touchdown,
            } => {
                let version = params.order_version(Order::Holdings)?;
                request::hkwpd(
                    number,
                    version,
                    account,
                    currency.as_ref().map(|c| c.as_str()),
                    td(touchdown),
                )
            }
            Segment::DepotTransactions {
                account,
                start_date,
                end_date,
                touchdown,
            } => {
                let version = params.order_version(Order::DepotTransactions)?;
                request::hkwdu(
                    number,
                    version,
                    account,
                    *start_date,
                    *end_date,
                    td(touchdown),
                )
            }
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Result of dialog init — response-driven transition
// ═══════════════════════════════════════════════════════════════════════════════

/// Result of `Dialog::init()`. Per the spec, the bank either accepts the dialog
/// (0010/0020) or requires SCA (0030/3955).
pub(crate) enum InitResult {
    /// Dialog opened, no TAN needed. Ready for business segments.
    Opened(Dialog<Open>),
    /// Bank requires TAN on init (SCA). Must confirm before business ops.
    TanRequired(Dialog<TanPending>, TanChallenge),
}

// ═══════════════════════════════════════════════════════════════════════════════
// Parsed bank response
// ═══════════════════════════════════════════════════════════════════════════════

/// Parsed response from the bank after sending a message.
#[derive(Debug)]
pub struct Response {
    /// All segments from the response (including inner HNVSD segments).
    pub segments: Vec<RawSegment>,
    /// Return codes for the whole message (HIRMG), in wire order.
    pub global_codes: Vec<ReturnCode>,
    /// Return codes for segments of the sent message (HIRMS), in wire order.
    pub segment_codes: Vec<ReturnCode>,
}

impl Response {
    pub fn find_segments(&self, seg_type: &str) -> Vec<&RawSegment> {
        self.segments
            .iter()
            .filter(|s| s.segment_type() == seg_type)
            .collect()
    }

    pub fn find_segment(&self, seg_type: &str) -> Option<&RawSegment> {
        self.segments.iter().find(|s| s.segment_type() == seg_type)
    }

    pub fn all_codes(&self) -> impl Iterator<Item = &ReturnCode> {
        self.global_codes.iter().chain(self.segment_codes.iter())
    }

    /// The answer to a typed TAN rejects the TAN and leaves the order for
    /// another one (docs/return-codes-design.md section 1): the class 9
    /// codes are 9941 and, at most, the 9050 summary and a 9360 that still
    /// leaves attempts. Gives the first 9941 and 9360's P1. `None` when
    /// the bank ended the dialog (9800), deleted the order (9210), locked
    /// the access (9931, 9360 without attempts), the TAN timed out or was
    /// used (9951, 9943), or the codes are unreadable.
    pub(crate) fn tan_rejection(&self) -> Option<(ReturnCode, Option<u32>)> {
        if !matches!(self.check_errors(), Err(FinTSError::Bank(_))) {
            return None;
        }
        let mut rejected = None;
        let mut attempts_left = None;
        for c in self.all_codes().filter(|c| c.is_error()) {
            match c.kind() {
                Kind::TanInvalid => {
                    rejected.get_or_insert_with(|| c.clone());
                }
                Kind::PartialErrors => {}
                Kind::LockAfterAttempts {
                    attempts_left: Some(n),
                } if n > 0 => attempts_left = Some(n),
                _ => return None,
            }
        }
        rejected.map(|c| (c, attempts_left))
    }

    /// True if any code has this kind.
    pub(crate) fn has(&self, kind: Kind) -> bool {
        self.all_codes().any(|c| c.kind() == kind)
    }

    /// Fills [`Scope::Segment`]'s `order` of every HIRMS code from the
    /// sent message `request` (its segment numbers). Codes referring to a
    /// number not in the request keep `None`.
    fn name_orders(&mut self, request: &[RawSegment]) {
        for code in &mut self.segment_codes {
            if let Scope::Segment { number, order } = &mut code.scope {
                *order = request
                    .iter()
                    .find(|s| s.segment_number() == *number)
                    .map(|s| SegmentType::new(s.segment_type()));
            }
        }
    }

    /// All segments read with the codec's declarative definitions. A
    /// segment without a definition, or one its definition rejects, comes
    /// back as `ParsedSegment::Raw` with the error that says why
    /// (docs/architecture.md 2.1, "parse what you can, keep the rest").
    pub fn decode_segments(&self) -> Vec<crate::codec::ParsedSegment> {
        crate::codec::parse_segments(self.segments.iter().cloned())
    }

    /// The codec errors of all segments that stayed raw for a reason other
    /// than a missing definition.
    pub fn segment_errors(&self) -> Vec<crate::codec::CodecError> {
        self.decode_segments()
            .into_iter()
            .filter_map(|p| match p {
                crate::codec::ParsedSegment::Raw { error, .. }
                    if error.kind != crate::codec::ErrorKind::UnknownSegment =>
                {
                    Some(error)
                }
                _ => None,
            })
            .collect()
    }

    /// 0030 = order received, TAN required.
    pub fn needs_tan(&self) -> bool {
        self.has(Kind::TanRequired) || self.has(Kind::DecoupledInitiated)
    }

    /// 3955 = decoupled TAN (pushTAN initiated).
    pub fn is_decoupled(&self) -> bool {
        self.has(Kind::DecoupledInitiated)
    }

    /// A status request got no confirmation yet: 3956, or 3957 ("wait for
    /// the push message") without 0020 in the same answer.
    pub fn is_decoupled_pending(&self) -> bool {
        self.has(Kind::DecoupledPending)
            || (self.has(Kind::DecoupledAwaitPush) && !self.has(Kind::OrderExecuted))
    }

    /// 3010 = not available; after a data request banks send it for "no
    /// entries".
    pub fn has_no_entries(&self) -> bool {
        self.has(Kind::NotAvailable)
    }

    /// 3076 = no strong authentication required (SCA exemption).
    pub fn has_sca_exemption(&self) -> bool {
        self.has(Kind::ScaExemption)
    }

    /// 3040 = more data available (touchdown/pagination).
    /// Returns the touchdown point if found.
    pub fn touchdown(&self) -> Option<TouchdownPoint> {
        find_touchdown(&self.segment_codes).or_else(|| find_touchdown(&self.global_codes))
    }

    /// Extract HITAN challenge from response. No HITAN is `Ok(None)`; a
    /// HITAN its definition rejects is [`FinTSError::SegmentUnreadable`].
    pub(crate) fn get_tan_challenge(&self) -> Result<Option<TanChallenge>> {
        let Some(raw) = self.find_segment("HITAN") else {
            return Ok(None);
        };
        let Some(hitan) =
            crate::segments::read::tan_challenge(raw).map_err(FinTSError::SegmentUnreadable)?
        else {
            return Ok(None);
        };
        if hitan.task_reference.is_empty() && hitan.challenge.is_empty() {
            return Ok(None);
        }
        Ok(Some(TanChallenge {
            challenge: ChallengeText::new(hitan.challenge),
            challenge_hhduc: hitan.challenge_hhduc.map(HhdUcData),
            task_reference: TaskReference::new(hitan.task_reference),
            decoupled: self.is_decoupled(),
            challenge_markup: false,
            valid_until: hitan.valid_until,
        }))
    }

    /// The TAN challenge of a business order the bank wants confirmed
    /// (0030/3955 with HITAN), unless it granted an exemption (3076).
    pub(crate) fn order_tan_challenge(&self) -> Result<Option<TanChallenge>> {
        if self.needs_tan() && !self.has_sca_exemption() {
            self.get_tan_challenge()
        } else {
            Ok(None)
        }
    }

    /// Check for fatal errors. Returns `Ok(())` if no errors.
    ///
    /// The reported error is [`Response::select_error`]'s; a credentials
    /// or system ID error wins even over unreadable HIRMG/HIRMS, which
    /// otherwise give `InvalidResponse`.
    pub fn check_errors(&self) -> Result<()> {
        let selected = self.select_error();
        // Credentials and system ID decide what the caller must do, even
        // next to response codes the codec cannot read.
        if let Some(e) = selected
            .as_ref()
            .filter(|e| matches!(e.category(), Category::Credentials(_) | Category::SystemId))
        {
            return Err(FinTSError::Bank(e.clone()));
        }
        // HIRMG/HIRMS the codec rejects: the bank's verdict is unknown, so
        // do not treat the response as a success.
        if let Some(error) = self
            .segments
            .iter()
            .filter(|s| matches!(s.segment_type(), "HIRMG" | "HIRMS"))
            .find_map(|s| crate::segments::read::response_codes(s).err())
        {
            return Err(FinTSError::InvalidResponse(format!(
                "response codes unreadable: {error}"
            )));
        }
        match selected {
            Some(e) => Err(FinTSError::Bank(e)),
            None => Ok(()),
        }
    }

    /// The error to report when the response has class 9 codes
    /// (docs/return-codes-design.md section 3), with all codes of the
    /// response. Specific causes rank above the signature error 9340,
    /// which ranks above the generic codes:
    /// 1. a credentials code that names the cause (9942, 9910, 9930,
    ///    9931), then a system ID code (9391), wherever they appear;
    /// 2. else the first specific segment-scope error;
    /// 3. else the first specific message-scope error (e.g. 9210 "BLZ does
    ///    not match the user" next to 9340);
    /// 4. else 9340 (signature not accepted), wherever it appears;
    /// 5. else the first error, segment scope first.
    ///
    /// Specific means neither generic (9010, 9050, 9800: summaries or
    /// consequences, not the reason) nor 9340.
    pub fn select_error(&self) -> Option<BankError> {
        fn errors(codes: &[ReturnCode]) -> impl Iterator<Item = &ReturnCode> {
            codes.iter().filter(|c| c.is_error())
        }
        let all = || self.all_codes().filter(|c| c.is_error());
        let specific = |c: &&ReturnCode| {
            !matches!(
                c.kind(),
                Kind::GeneralError
                    | Kind::PartialErrors
                    | Kind::DialogAborted
                    | Kind::SignatureInvalid
            )
        };
        let named_credentials = |c: &&ReturnCode| {
            matches!(c.category(), Some(Category::Credentials(_)))
                && c.kind() != Kind::SignatureInvalid
        };
        let selected = all()
            .find(named_credentials)
            .or_else(|| all().find(|c| c.category() == Some(Category::SystemId)))
            .or_else(|| errors(&self.segment_codes).find(specific))
            .or_else(|| errors(&self.global_codes).find(specific))
            .or_else(|| all().find(|c| c.kind() == Kind::SignatureInvalid))
            .or_else(|| {
                errors(&self.segment_codes)
                    .chain(errors(&self.global_codes))
                    .next()
            })?;
        Some(BankError::new(
            selected.clone(),
            self.all_codes().cloned().collect(),
        ))
    }

    /// The codes of this response the caller should see: not class 0 or 9
    /// and not consumed by the protocol (0030, 3040, 3076, 3920, 3955,
    /// 3956, 3091, 3945, 3045; 3090, 3093, 3094 on HKVPP/HKVPA): the
    /// transfer job acts on the VoP and instant codes itself.
    pub fn notices(&self, dialog: NoticeDialog) -> Vec<Notice> {
        self.all_codes()
            .filter(|c| matches!(c.class(), Class::Note | Class::Warning | Class::Undefined))
            .filter(|c| !Notice::is_consumed(c))
            .cloned()
            .map(|c| Notice::new(dialog, c))
            .chain(self.client_warning(dialog))
            .collect()
    }

    /// Heuristic check, once per response, of the free text of HIRMG and
    /// HIRMS (code texts and parameters): UTF-8 sequences in an ISO 8859-1
    /// answer. Statement bodies are not checked. The text stays as decoded.
    fn client_warning(&self, dialog: NoticeDialog) -> Option<Notice> {
        let garbled = self.all_codes().any(|c| {
            looks_like_utf8_as_latin1(&c.text)
                || c.parameters.iter().any(|p| looks_like_utf8_as_latin1(p))
        });
        garbled.then(|| Notice::client(dialog, ClientWarning::Utf8TextInLatin1))
    }

    /// Log the warnings no job turns into a notice of its own.
    pub(crate) fn log_informational_codes(&self) {
        for code in self.all_codes() {
            match code.kind() {
                Kind::VopNotRequired | Kind::VopApprovalNotPossible => {
                    debug!(
                        "[FinTS] {} (Verification of Payee): {}",
                        code.code, code.text
                    );
                }
                Kind::Other if code.class() == Class::Warning => {
                    debug!("[FinTS] Warning {}: {}", code.code, code.text);
                }
                _ => {}
            }
        }
    }

    /// Extract allowed TAN security functions from code 3920.
    pub fn allowed_security_functions(&self) -> Vec<SecurityFunction> {
        extract_allowed_security_functions(&self.segment_codes)
            .into_iter()
            .chain(extract_allowed_security_functions(&self.global_codes))
            .collect()
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// TAN challenge
// ═══════════════════════════════════════════════════════════════════════════════

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub(crate) struct TanChallenge {
    pub challenge: ChallengeText,
    pub challenge_hhduc: Option<HhdUcData>,
    pub task_reference: TaskReference,
    pub decoupled: bool,
    /// HITANS "Challenge strukturiert" of the TAN method in use: the
    /// challenge text may carry markup. The response alone does not know
    /// the method; [`BankParams::challenge_structured`] sets it.
    pub challenge_markup: bool,
    /// HITAN "Gültigkeitsdatum und -uhrzeit für Challenge" (bank local
    /// time), when the bank sends one.
    pub valid_until: Option<jiff::civil::DateTime>,
}

// ═══════════════════════════════════════════════════════════════════════════════
// Bank parameters
// ═══════════════════════════════════════════════════════════════════════════════

/// The bank's parameter data as one dialog knows it.
///
/// The sources are `bpd_segments` (the bank parameters: HIBPA, HIKOM, the
/// TAN segments HITANS and HIPINS, and every `HI…S` order parameter
/// segment) and `upd_segments` (the user parameters: HIUPA and its HIUPD),
/// both byte for byte as received. All other fields are read from them. An
/// answer with a new HIBPA or HIUPA starts a new set; segments are never
/// merged across answers.
#[derive(Debug, Clone)]
pub struct BankParams {
    pub bpd_version: u16,
    pub upd_version: u16,
    pub bpd_segments: Vec<RawSegment>,
    pub upd_segments: Vec<RawSegment>,
    /// The TAN methods of `bpd`, kept as a list for the selection logic.
    pub tan_methods: Vec<TanMethod>,
    pub selected_security_function: SecurityFunction,
    pub selected_tan_medium: Option<TanMediumName>,
    pub allowed_security_functions: Vec<SecurityFunction>,
    pub preferred_security_function: Option<SecurityFunction>,
    /// Set when the bank reported code 3072 (changed access data): the new
    /// user ID to use from the next dialog on.
    pub changed_user_id: Option<UserId>,
    /// Order codes the bank turned out to want HKTAN for although HIPINS did
    /// not say so. Empty until the 9075 retry learns them.
    pub tan_required_learned: BTreeSet<SegmentType>,
    bpd: Option<Bpd>,
    upd: Option<Upd>,
}

impl BankParams {
    pub fn new() -> Self {
        Self {
            bpd_version: 0,
            upd_version: 0,
            bpd_segments: Vec::new(),
            upd_segments: Vec::new(),
            tan_methods: Vec::new(),
            selected_security_function: SecurityFunction::pin_only(),
            selected_tan_medium: None,
            allowed_security_functions: Vec::new(),
            preferred_security_function: None,
            changed_user_id: None,
            tan_required_learned: BTreeSet::new(),
            bpd: None,
            upd: None,
        }
    }

    /// The typed BPD, built from `bpd_segments`; `None` until a readable
    /// HIBPA has arrived.
    pub fn bpd(&self) -> Option<&Bpd> {
        self.bpd.as_ref()
    }

    /// The typed UPD, built from `upd_segments`. A dialog-only UPD
    /// (version 0) is held for the dialog but not stored by
    /// [`SessionState`](crate::SessionState).
    pub fn upd(&self) -> Option<&Upd> {
        self.upd.as_ref()
    }

    /// Ingest BPD/UPD/HISYN/3920/3072 from a response.
    pub fn ingest_response(&mut self, response: &Response, system_id: &mut SystemId) {
        let segs = &response.segments;

        if let Some(bpd) = bpd_after(&self.bpd_segments, segs) {
            self.set_bpd(bpd);
        }
        if let Some(upd) = upd_after(segs) {
            self.set_upd(upd);
        }

        if let Some(sid) = segs
            .iter()
            .find(|s| s.segment_type() == "HISYN")
            .and_then(crate::segments::read::system_id)
            && !sid.is_empty()
        {
            info!("[FinTS] System ID assigned");
            *system_id = SystemId::new(sid);
        }

        let allowed = response.allowed_security_functions();
        if !allowed.is_empty() {
            self.allowed_security_functions = allowed;
        }

        // 3072: access data changed. The new user/customer ID is passed on to the
        // caller; we also drop the UPD so the next dialog reloads it.
        for code in response.all_codes() {
            if let Kind::UserDataChanged { user_id, .. } = code.kind() {
                warn!(
                    "[FinTS] 3072 changed access data (new user ID {})",
                    if user_id.is_some() {
                        "given"
                    } else {
                        "not given"
                    }
                );
                if let Some(uid) = user_id {
                    self.changed_user_id = Some(UserId::new(uid));
                }
                if !segs.iter().any(|s| s.segment_type() == "HIUPA") {
                    self.invalidate_upd();
                }
            }
        }
    }

    /// Replace the BPD and rebuild the typed model, the version and the TAN
    /// methods from it. A HIBPA the codec cannot read leaves no model and
    /// version 0, so the next dialog fetches the BPD again.
    pub(crate) fn set_bpd(&mut self, segments: Vec<RawSegment>) {
        match Bpd::from_segments(&segments) {
            Ok(bpd) => {
                self.bpd_version = bpd.version;
                // Only methods in a HITANS version we can build are offered.
                self.tan_methods = bpd
                    .tan_methods()
                    .filter(|m| crate::segments::request::HKTAN_VERSIONS.contains(&m.hktan_version))
                    .cloned()
                    .collect();
                self.bpd = Some(bpd);
            }
            Err(e) => {
                if !segments.is_empty() {
                    warn!("[FinTS] BPD not usable: {e}");
                }
                if matches!(e, ParamsError::Unreadable(..)) {
                    self.bpd_version = 0;
                }
                self.tan_methods.clear();
                self.bpd = None;
            }
        }
        info!(
            "[FinTS] BPD v{}: {} segments, {} TAN methods",
            self.bpd_version,
            segments.len(),
            self.tan_methods.len(),
        );
        self.bpd_segments = segments;
    }

    /// Replace the UPD and rebuild the typed model, the version and the
    /// accounts from it.
    pub(crate) fn set_upd(&mut self, segments: Vec<RawSegment>) {
        match Upd::from_segments(&segments) {
            Ok(upd) => {
                self.upd_version = upd.version;
                self.upd = Some(upd);
            }
            Err(e) => {
                if !segments.is_empty() {
                    warn!("[FinTS] UPD not usable: {e}");
                }
                if matches!(e, ParamsError::Unreadable(..)) {
                    self.upd_version = 0;
                }
                self.upd = None;
            }
        }
        info!(
            "[FinTS] UPD v{}: {} accounts",
            self.upd_version,
            self.upd.as_ref().map_or(0, |u| u.accounts().len())
        );
        self.upd_segments = segments;
    }

    /// Drop the user parameters; HKVVB then announces UPD version 0 and the
    /// bank sends them anew.
    pub fn invalidate_upd(&mut self) {
        self.upd = None;
        self.upd_segments = Vec::new();
        self.upd_version = 0;
    }

    /// The TAN methods of every HITANS version the bank sent, including
    /// versions the client cannot build (`tan_methods` holds only the
    /// buildable ones once a BPD is set). Empty when no HITANS is known.
    fn hitans_methods(&self) -> Vec<&TanMethod> {
        match &self.bpd {
            Some(bpd) => bpd.tan_methods().collect(),
            None => self.tan_methods.iter().collect(),
        }
    }

    /// HKTAN version to send: the highest version in
    /// `HKTAN_VERSIONS` among the
    /// HITANS versions that list the selected security function (Formals
    /// "Segmentversion": the HITANS version of a method is its HKTAN version).
    /// There is no fallback to another method's version; when none is
    /// buildable the result is [`FinTSError::VersionMismatch`] naming the
    /// HITANS versions that list the method and the ones we implement.
    ///
    /// No HITANS known (first dialog, before any BPD): 7, the highest we
    /// implement. Evidence: the committed Consorsbank trace (live 2026-11-16)
    /// sends `HKTAN:5:7+4+HKIDN` at dialog init and the bank accepts it.
    pub fn hktan_version(&self) -> Result<u16> {
        let implemented = crate::segments::request::HKTAN_VERSIONS;
        let methods = self.hitans_methods();
        if methods.is_empty() {
            return Ok(implemented.iter().copied().max().unwrap_or(7));
        }
        let mut offered: Vec<u16> = methods
            .iter()
            .filter(|m| m.security_function == self.selected_security_function)
            .map(|m| m.hktan_version)
            .collect();
        offered.sort_unstable();
        offered.dedup();
        offered
            .iter()
            .rev()
            .copied()
            .find(|v| implemented.contains(v))
            .ok_or_else(|| {
                FinTSError::VersionMismatch(VersionMismatch {
                    code: SegmentType::new("HKTAN"),
                    offered,
                    implemented: implemented.to_vec(),
                    formats: None,
                })
            })
    }

    /// The HKTAN version for the dialog-initialisation message, or `None`
    /// when it carries no HKTAN. The init message allows HKTAN 6 and 7 only
    /// (PIN/TAN spec B.4, "Dialoginitialisierung": "ein Segment HKTAN ab
    /// Segmentversion #6"), so a selected method that appears only in HITANS
    /// below 6 gets no HKTAN at init; its orders still use its own version.
    /// A pin-only selection (999) with HITANS known has no method to signal
    /// and also gets none.
    pub fn init_hktan_version(&self) -> Result<Option<u16>> {
        if self.selected_security_function == SecurityFunction::pin_only()
            && !self.hitans_methods().is_empty()
        {
            return Ok(None);
        }
        let version = self.hktan_version()?;
        Ok((version >= 6).then_some(version))
    }

    /// Whether the bank allows national account details next to the IBAN
    /// (HISPAS, parameter "Nationale Kontoverbindung erlaubt"). `false`
    /// when the BPD has no HISPAS.
    pub fn national_account_allowed(&self) -> bool {
        self.bpd
            .as_ref()
            .and_then(Bpd::sepa)
            .is_some_and(|s| s.national_account_allowed)
    }

    /// The version of `order` to send: the highest version the bank offers
    /// in its BPD that this client implements ([`Order::implemented`],
    /// Formals C.10). Pure on the parameters, so it also answers "can this
    /// bank do X" without a dialog.
    ///
    /// - No BPD known → [`FinTSError::NoBpd`]: orders run only after
    ///   synchronisation, and no version is guessed.
    /// - The bank offers no version we implement, or not the order at all →
    ///   [`FinTSError::VersionMismatch`] naming both sides, instead of sending
    ///   something the bank will reject.
    pub fn order_version(&self, order: Order) -> Result<u16> {
        let code = order.code();
        let Some(bpd) = &self.bpd else {
            return Err(FinTSError::NoBpd {
                code: SegmentType::new(code),
            });
        };
        let chosen = bpd.highest_common(code, order.implemented());
        debug!(
            "[FinTS] Version negotiation {}: we implement {:?} → {:?}",
            code,
            order.implemented(),
            chosen
        );
        chosen.map_err(FinTSError::VersionMismatch)
    }

    /// The bank's storage period in days ("Speicherzeitraum" of HIKAZS or
    /// HICAZS, Messages C.2.1.1.1 / C.2.3.1.1.1) for the version we would
    /// send of `order` (`Order::Transactions` or `Order::Camt`). `None`
    /// without a BPD, for another order, or when the bank sends 0.
    pub fn days_stored(&self, order: Order) -> Option<u32> {
        let version = self.order_version(order).ok()?;
        let job = self.bpd.as_ref()?.job(order.code())?;
        match &job.versions.get(&version)?.detail {
            JobDetail::Statement(p) => Some(p.days_stored),
            JobDetail::Camt(p) => Some(p.days_stored),
            _ => None,
        }
        .filter(|d| *d > 0)
    }

    /// The camt.052 format to request in HKCAZ: the highest `camt.052.001.NN`
    /// the bank lists in HICAZS, compared by the number NN. Among equal
    /// versions the descriptor without `.xsd` wins. The bank's own string
    /// is sent. camt.052.001.02 when it lists none.
    /// All camt.052 versions parse with [`crate::camt::parse_camt052`].
    pub fn camt052_format(&self) -> String {
        self.bpd
            .iter()
            .flat_map(|b| b.camt_formats())
            .filter_map(|f| Some((crate::camt::camt052_version(f.as_bytes())?, f)))
            .max_by(|(va, a), (vb, b)| {
                va.cmp(vb)
                    .then_with(|| b.ends_with(".xsd").cmp(&a.ends_with(".xsd")))
            })
            .map(|(_, f)| f)
            .cloned()
            .unwrap_or_else(|| crate::segments::request::CAMT052_DEFAULT.to_string())
    }

    /// Select the best security function from 3920 allowed list.
    pub fn select_security_function(&mut self) {
        let allowed = &self.allowed_security_functions;
        if allowed.is_empty() {
            // No 3920 list (yet): honor a cached preference the BPD knows about.
            if let Some(pref) = &self.preferred_security_function
                && self
                    .tan_methods
                    .iter()
                    .any(|m| &m.security_function == pref)
            {
                self.selected_security_function = pref.clone();
            }
            return;
        }

        // A method that no buildable HITANS version lists is not offered:
        // a stored preference for it is ignored like an unknown method, and
        // the automatic choice skips it. Without HITANS everything is open.
        let pin_only = SecurityFunction::pin_only();
        let methods = &self.tan_methods;
        let available = |sf: &SecurityFunction| {
            methods.is_empty()
                || *sf == pin_only
                || methods.iter().any(|m| &m.security_function == sf)
        };
        if let Some(ref pref) = self.preferred_security_function
            && allowed.contains(pref)
            && available(pref)
        {
            self.selected_security_function = pref.clone();
            return;
        }

        let chosen = allowed
            .iter()
            .filter(|sf| *sf != &pin_only && available(sf))
            .max_by_key(|sf| {
                methods
                    .iter()
                    .find(|m| &m.security_function == *sf)
                    .map(|m| if m.is_decoupled { 2i32 } else { 1 })
                    .unwrap_or(0)
            });

        if let Some(sf) = chosen {
            info!("[FinTS] Selected security function: {}", sf);
            self.selected_security_function = sf.clone();
        }
    }

    pub fn is_decoupled(&self) -> bool {
        self.tan_methods
            .iter()
            .find(|m| m.security_function == self.selected_security_function)
            .map(|m| m.is_decoupled)
            .unwrap_or(false)
    }

    /// HITANS "Challenge strukturiert" of the TAN method in use.
    pub fn challenge_structured(&self) -> bool {
        self.tan_methods
            .iter()
            .find(|m| m.security_function == self.selected_security_function)
            .map(|m| m.challenge_structured)
            .unwrap_or(false)
    }

    /// Whether the chosen method wants a TAN medium named in HKTAN. Asks
    /// the first HITANS entry of the method, as the order lists them.
    pub fn needs_tan_medium(&self) -> bool {
        let chosen = &self.selected_security_function;
        let first = self
            .tan_methods
            .iter()
            .find(|m| m.security_function == *chosen);
        first.is_some_and(|m| m.needs_tan_medium)
    }

    /// The chosen method's HITANS entry. When several HITANS versions list
    /// it, the entry of the highest version, which is the HKTAN we send.
    pub fn selected_tan_method(&self) -> Option<&TanMethod> {
        let chosen = &self.selected_security_function;
        let mut best: Option<&TanMethod> = None;
        for method in self
            .tan_methods
            .iter()
            .filter(|m| m.security_function == *chosen)
        {
            if best.is_none_or(|b| method.hktan_version >= b.hktan_version) {
                best = Some(method);
            }
        }
        best
    }

    /// How often and how long to ask for the status of a decoupled
    /// approval, from the chosen method's HITANS 7 entry.
    pub fn decoupled_params(&self) -> DecoupledParams {
        let Some(method) = self.selected_tan_method() else {
            return DecoupledParams::default();
        };
        // HITANS gives seconds; 0 and the -1 we store for "not sent" both
        // mean no requirement.
        let wait = |seconds: i32| {
            (seconds > 0).then(|| std::time::Duration::from_secs(seconds.unsigned_abs().into()))
        };
        // A maximum of 0 means no limit, the same as no maximum.
        let max_polls =
            (method.decoupled_max_polls > 0).then(|| method.decoupled_max_polls.unsigned_abs());
        DecoupledParams {
            wait_before_first_poll: wait(method.wait_before_first_poll),
            wait_before_next_poll: wait(method.wait_before_next_poll),
            max_polls,
            auto_polling_allowed: method.decoupled_auto_polling,
        }
    }
}

impl Default for BankParams {
    fn default() -> Self {
        Self::new()
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Decoupled TAN polling
// ═══════════════════════════════════════════════════════════════════════════════

/// The bank's rules for status requests (HKTAN process S) while a
/// decoupled approval is pending, from HITANS 7. `None` everywhere the
/// bank set no rule.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct DecoupledParams {
    /// Wait at least this long before the first status request.
    pub wait_before_first_poll: Option<std::time::Duration>,
    /// Wait at least this long between two status requests.
    pub wait_before_next_poll: Option<std::time::Duration>,
    /// Send no more status requests than this.
    pub max_polls: Option<u32>,
    /// Whether the client may ask without the user confirming first.
    pub auto_polling_allowed: Option<bool>,
}

// ═══════════════════════════════════════════════════════════════════════════════
// Dialog<S> — the typestate dialog
// ═══════════════════════════════════════════════════════════════════════════════

pub(crate) struct Dialog<S: std::fmt::Debug> {
    transport: SharedTransport,
    url: String,
    quirks: QuirkChain,
    blz: Blz,
    user_id: UserId,
    pin: Pin,
    system_id: SystemId,
    product_id: ProductId,
    dialog_id: DialogId,
    message_number: u16,
    pub params: BankParams,
    /// The bank profile's HKTAN overrides, beating HIPINS.
    tan_overrides: TanOverrides,
    /// Notices of the messages since the last [`Dialog::take_notices`].
    notices: Vec<Notice>,
    /// The dialog the notices are tagged with.
    notice_dialog: NoticeDialog,
    /// Pinned local time ([`crate::BankClient::with_now`]), else the wall clock.
    now: Option<jiff::civil::DateTime>,
    _state: PhantomData<S>,
}

impl<S: std::fmt::Debug> std::fmt::Debug for Dialog<S> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Dialog")
            .field("blz", &self.blz)
            .field("user_id", &self.user_id)
            .field("system_id", &self.system_id)
            .field("dialog_id", &self.dialog_id)
            .field("message_number", &self.message_number)
            .field("state", &std::any::type_name::<S>())
            .finish()
    }
}

// Note: Pin intentionally Debug-redacted (shows "Pin(****)")

// ── Shared internals (all states) ───────────────────────────────────────────

impl<S: std::fmt::Debug> Dialog<S> {
    pub fn system_id(&self) -> &SystemId {
        &self.system_id
    }
    /// The notices of every message exchanged since the last call (or the
    /// dialog's start), including touchdown pages and TAN resubmits.
    pub fn take_notices(&mut self) -> Vec<Notice> {
        std::mem::take(&mut self.notices)
    }
    pub fn bank_params(&self) -> &BankParams {
        &self.params
    }
    pub(crate) fn tan_overrides(&self) -> &TanOverrides {
        &self.tan_overrides
    }

    /// The dialog's local time: pinned, else the wall clock.
    pub(crate) fn now(&self) -> jiff::civil::DateTime {
        self.now
            .unwrap_or_else(|| jiff::ZonedDateTime::now().datetime())
    }

    /// Build the standard HKIDN segment for this dialog.
    fn identify_segment(&self) -> Segment {
        Segment::Identify {
            blz: self.blz.clone(),
            user_id: self.user_id.clone(),
            system_id: self.system_id.clone(),
        }
    }

    /// Build the standard HKVVB segment for this dialog.
    fn process_prep_segment(&self) -> Segment {
        Segment::ProcessPrep {
            bpd_version: self.params.bpd_version,
            upd_version: self.params.upd_version,
            product_id: self.product_id.clone(),
        }
    }

    /// The next message of this dialog around `segments`, signed with the
    /// PIN and, when given, the TAN (HNSHA).
    fn message(&self, segments: &[Segment], tan: Option<&str>) -> Result<Vec<u8>> {
        let signer = message::SecurityContext {
            dialog_id: self.dialog_id.as_str(),
            message_number: self.message_number,
            blz: self.blz.as_str(),
            user_id: self.user_id.as_str(),
            system_id: self.system_id.as_str(),
            pin: self.pin.as_str(),
            tan,
            security_function: self.params.selected_security_function.as_str(),
        };
        message::build_from_segments(&signer, segments, &self.params, self.now())
    }

    /// Sends `segments` signed with the PIN only.
    async fn send_segments(&mut self, segments: &[Segment]) -> Result<Response> {
        self.send(segments, None).await
    }

    /// Sends `segments` with `tan` in the signature closing segment.
    async fn send_segments_with_tan(
        &mut self,
        segments: &[Segment],
        tan: &str,
    ) -> Result<Response> {
        self.send(segments, Some(tan)).await
    }

    async fn send(&mut self, segments: &[Segment], tan: Option<&str>) -> Result<Response> {
        let request = self.message(segments, tan)?;
        if tan.is_none() {
            // Masked by segment (PIN, TAN, IDs, accounts); cut by characters,
            // since a byte index can fall inside an umlaut.
            let masked: String = crate::mask::mask_for_log(&request)
                .chars()
                .take(500)
                .collect();
            info!("[FinTS] Outgoing ({} bytes): {masked}", request.len());
        }
        let answer = self.exchange(&request).await?;
        let response = parse_answer(&answer, self.message_number - 1, &request)?;
        response.log_informational_codes();
        self.notices.extend(response.notices(self.notice_dialog));
        Ok(response)
    }

    async fn send_end(&mut self) -> Result<()> {
        if !self.dialog_id.is_assigned() {
            return Ok(());
        }
        debug!("Ending dialog {}", self.dialog_id);
        let end = [Segment::End {
            dialog_id: self.dialog_id.clone(),
        }];
        let msg_bytes = self.message(&end, None)?;
        let _ = self.exchange(&msg_bytes).await;
        self.dialog_id = DialogId::unassigned();
        Ok(())
    }

    /// One request and its answer, both through the quirk chain. The next
    /// message of the dialog gets the next number.
    async fn exchange(&mut self, request: &[u8]) -> Result<Vec<u8>> {
        let number = self.message_number;
        self.message_number = number + 1;
        let Self {
            transport,
            url,
            quirks,
            dialog_id,
            ..
        } = &*self;
        let ctx = crate::quirks::QuirkContext {
            hbci_version: "300",
            dialog_id: dialog_id.as_str(),
            message_number: number,
        };
        crate::transport::exchange(transport.as_ref(), url, quirks, &ctx, request).await
    }

    /// Sends a dialog initialisation (or another message that may carry
    /// BPD and UPD) and takes over what the answer hands out: the dialog
    /// ID from its header, and parameters, system ID and allowed methods.
    async fn send_and_adopt(&mut self, segments: &[Segment]) -> Result<Response> {
        let response = self.send_segments(segments).await?;
        let header = response.find_segment("HNHBK");
        let assigned = header
            .and_then(crate::segments::read::message_header)
            .map(|(id, _)| DialogId::new(id))
            .filter(DialogId::is_assigned);
        if let Some(id) = assigned {
            self.dialog_id = id;
        }
        self.params.ingest_response(&response, &mut self.system_id);
        Ok(response)
    }

    pub(crate) fn blz(&self) -> &Blz {
        &self.blz
    }

    pub(crate) fn user_id(&self) -> &UserId {
        &self.user_id
    }

    pub(crate) fn url(&self) -> &str {
        &self.url
    }

    pub(crate) fn dialog_id(&self) -> &DialogId {
        &self.dialog_id
    }

    /// The number the next message will carry.
    pub(crate) fn message_number(&self) -> u16 {
        self.message_number
    }

    /// Take BPD, UPD, system ID, 3920 and 3072 from a response.
    pub(crate) fn ingest(&mut self, response: &Response) {
        self.params.ingest_response(response, &mut self.system_id);
    }

    /// HKTAN process 2 for `task_reference`, after the `extra` segments
    /// (HKVPA for a payment, else none), with `tan` in HNSHA (PIN/TAN
    /// B.4.2). Works for the login and for any order. Returns the bank's
    /// answer unchecked.
    pub(crate) async fn send_tan(
        &mut self,
        extra: &[Segment],
        task_reference: &TaskReference,
        tan: &str,
    ) -> Result<Response> {
        let mut segments = extra.to_vec();
        segments.push(Segment::TanProcess2 {
            task_reference: task_reference.clone(),
            tan_medium: self.params.selected_tan_medium.clone(),
        });
        self.send_segments_with_tan(&segments, tan).await
    }

    /// One decoupled status request for `task_reference` (HKTAN process S,
    /// or 2 before HKTAN 7), after the `extra` segments (HKVPA on a
    /// payment's first poll, else none). Returns the bank's answer
    /// unchecked.
    pub(crate) async fn send_tan_status(
        &mut self,
        extra: &[Segment],
        task_reference: &TaskReference,
    ) -> Result<Response> {
        let mut segments = extra.to_vec();
        segments.push(Segment::TanPollDecoupled {
            task_reference: task_reference.clone(),
            tan_medium: self.params.selected_tan_medium.clone(),
        });
        self.send_segments(&segments).await
    }

    /// HKEND, best effort; nothing when the dialog has no ID (never
    /// opened, or forgotten).
    pub(crate) async fn end_quietly(&mut self) {
        self.send_end().await.ok();
    }

    /// Forget the dialog ID: the bank ended the dialog, or its message
    /// number is unknown, so no HKEND is sent for it.
    pub(crate) fn forget_dialog(&mut self) {
        self.dialog_id = DialogId::unassigned();
    }

    pub(crate) fn transition<T: std::fmt::Debug>(self) -> Dialog<T> {
        Dialog {
            transport: self.transport,
            url: self.url,
            quirks: self.quirks,
            blz: self.blz,
            user_id: self.user_id,
            pin: self.pin,
            system_id: self.system_id,
            product_id: self.product_id,
            dialog_id: self.dialog_id,
            message_number: self.message_number,
            params: self.params,
            tan_overrides: self.tan_overrides,
            notices: self.notices,
            notice_dialog: self.notice_dialog,
            now: self.now,
            _state: PhantomData,
        }
    }
}

// ── Dialog<New> ─────────────────────────────────────────────────────────────

impl Dialog<New> {
    pub fn new(
        url: &str,
        blz: &Blz,
        user_id: &UserId,
        pin: &Pin,
        product_id: &ProductId,
    ) -> Result<Self> {
        Ok(Self {
            transport: crate::transport::default_transport()?,
            url: url.to_string(),
            quirks: QuirkChain::default(),
            blz: blz.clone(),
            user_id: user_id.clone(),
            pin: pin.clone(),
            system_id: SystemId::unassigned(),
            product_id: product_id.clone(),
            dialog_id: DialogId::unassigned(),
            message_number: 1,
            params: BankParams::new(),
            tan_overrides: TanOverrides::default(),
            notices: Vec::new(),
            notice_dialog: NoticeDialog::Job,
            now: None,
            _state: PhantomData,
        })
    }

    /// Pin the local time of this dialog's signatures and orders.
    pub fn with_now(mut self, now: Option<jiff::civil::DateTime>) -> Self {
        self.now = now;
        self
    }

    pub fn with_system_id(mut self, system_id: &SystemId) -> Self {
        self.system_id = system_id.clone();
        self
    }

    pub fn with_params(mut self, params: &BankParams) -> Self {
        self.params = params.clone();
        self
    }

    pub fn with_security_function(mut self, function: SecurityFunction) -> Self {
        self.params.selected_security_function = function;
        self
    }

    /// Use a custom quirk chain instead of the default one
    /// (e.g. `QuirkChain::default().with(MyBankFix)`, or `QuirkChain::none()`).
    /// Continue a dialog the bank opened earlier (a thawed paused dialog):
    /// its ID, and the number its next message carries.
    pub(crate) fn resume(mut self, dialog_id: DialogId, message_number: u16) -> Dialog<Open> {
        self.dialog_id = dialog_id;
        self.message_number = message_number;
        self.transition()
    }

    pub(crate) fn with_quirks(mut self, quirks: crate::quirks::QuirkChain) -> Self {
        self.quirks = quirks;
        self
    }

    /// Send through `transport` instead of the default (`HttpTransport` with feature `http`)
    /// (custom timeout, recording, in-process banks).
    pub fn with_transport(mut self, transport: SharedTransport) -> Self {
        self.transport = transport;
        self
    }

    /// The bank profile's per-order HKTAN overrides (beat HIPINS).
    pub fn with_tan_overrides(mut self, overrides: TanOverrides) -> Self {
        self.tan_overrides = overrides;
        self
    }

    /// Synchronization dialog (spec: Initialisierung mit Synchronisierung).
    ///
    /// Sends HKIDN + HKVVB + HKSYN (mode=0 for new system_id).
    /// The bank responds with BPD, UPD, HITANS, HIPINS, and HISYN(system_id).
    /// This dialog is ONLY for synchronization — no business segments allowed.
    pub async fn sync(mut self) -> Result<(Dialog<Synced>, Response)> {
        info!("[FinTS] Sync dialog: BLZ={}", self.blz);
        self.notice_dialog = NoticeDialog::Sync;

        let segments = [
            self.identify_segment(),
            self.process_prep_segment(),
            Segment::Sync,
        ];

        let response = self.send_and_adopt(&segments).await?;
        self.params.select_security_function();

        // Check for fatal errors (sync dialog should not require TAN)
        if !response.needs_tan() {
            response.check_errors()?;
        }

        // Log all BPD parameter segments for diagnostics
        let bpd_summary: Vec<String> = self
            .params
            .bpd_segments
            .iter()
            .map(|s| format!("{}:v{}", s.segment_type(), s.segment_version()))
            .collect();
        info!(
            "[FinTS] Sync complete: BPD v{}, {} TAN methods",
            self.params.bpd_version,
            self.params.tan_methods.len()
        );
        info!(
            "[FinTS] BPD segments ({}): {}",
            bpd_summary.len(),
            bpd_summary.join(", ")
        );
        if let Some(bpd) = self.params.bpd() {
            for u in bpd.unparsed() {
                warn!("[FinTS] BPD segment not fully read: {u:?}");
            }
        }

        Ok((self.transition(), response))
    }

    /// Normal dialog initialization (spec: Dialoginitialisierung).
    ///
    /// Sends HKIDN + HKVVB + HKTAN(process=4, ref=HKIDN).
    /// Response-driven: returns `InitResult::Opened` or `InitResult::TanRequired`
    /// based on the bank's response codes.
    pub async fn init(mut self) -> Result<InitResult> {
        let medium = self.params.selected_tan_medium.clone();
        info!(
            "[FinTS] Init dialog: BLZ={} security_fn={}",
            self.blz, self.params.selected_security_function
        );

        let mut segments = vec![self.identify_segment(), self.process_prep_segment()];
        if self.params.init_hktan_version()?.is_some() {
            segments.push(Segment::TanProcess4 {
                reference_seg: SegmentRef::new("HKIDN"),
                tan_medium: medium,
            });
        } else {
            info!("[FinTS] Init dialog: no HKTAN (selected method below HKTAN 6, or PIN only)");
        }

        let response = self.send_and_adopt(&segments).await?;

        let allowed = response.allowed_security_functions();
        if !allowed.is_empty() {
            self.params.allowed_security_functions = allowed;
            self.params.select_security_function();
        }

        for c in response.all_codes() {
            info!("[FinTS] Init: {} - {}", c.code, c.text);
        }

        // Response-driven transition per spec:
        if response.needs_tan()
            && let Some(challenge) = response.get_tan_challenge()?
        {
            // 0030/3955: TAN required on init → TanPending
            // Decoupled only on the bank's word (3955, PIN/TAN TAN-Prozess
            // 4): a decoupled method may still ask for a typed TAN (0030).
            let challenge = TanChallenge {
                challenge_markup: self.params.challenge_structured(),
                ..challenge
            };
            info!(
                "[FinTS] Init requires TAN: decoupled={}",
                challenge.decoupled
            );
            return Ok(InitResult::TanRequired(self.transition(), challenge));
        }

        // 0010/0020 or 3076: dialog opened, no TAN needed → Open
        response.check_errors()?;
        info!("[FinTS] Init opened without TAN");
        Ok(InitResult::Opened(self.transition()))
    }

    /// Learn the bank's parameters without a synchronization dialog, for a
    /// client that has a system ID but no BPD (e.g. migrated from another
    /// store). HKSYN would assign a new system ID, so the bank would see a
    /// new device and might withhold an SCA exemption (3076).
    ///
    /// Sends HKIDN + HKVVB with the stored system ID, signed PIN-only (999)
    /// and without HKTAN (HKTAN with 999 is an invalid combination). Whether
    /// the bank opens a dialog or aborts it, it answers with its BPD, the UPD
    /// and the allowed security functions (3920); the dialog is ended right
    /// away. Wrong PIN, locked access and a rejected system ID are errors.
    /// Returns the parameters with the best security function selected.
    pub async fn probe_parameters(mut self) -> Result<(BankParams, SystemId)> {
        info!(
            "[FinTS] Parameter probe: BLZ={} with stored system ID",
            self.blz
        );
        self.params.selected_security_function = SecurityFunction::pin_only();
        let segments = [self.identify_segment(), self.process_prep_segment()];
        let response = self.send_and_adopt(&segments).await?;
        for c in response.all_codes() {
            info!("[FinTS] Probe: {} - {}", c.code, c.text);
        }
        let fatal = response.all_codes().find(|c| {
            matches!(
                c.kind(),
                Kind::PinWrong
                    | Kind::SignatureInvalid
                    | Kind::AccountLocked
                    | Kind::SystemIdInvalid
            )
        });
        if fatal.is_some() {
            response.check_errors()?;
        }
        self.send_end().await.ok();
        self.params.select_security_function();
        if self.params.bpd().is_none() {
            return Err(FinTSError::Dialog(
                "parameter probe: the bank sent no BPD".into(),
            ));
        }
        Ok((self.params, self.system_id))
    }

    /// Initialize WITHOUT HKTAN — PIN-only. Used when bank doesn't require SCA.
    pub async fn init_no_tan(mut self) -> Result<(Dialog<Open>, Response)> {
        info!("[FinTS] Init (no HKTAN)");
        let segments = [self.identify_segment(), self.process_prep_segment()];

        let response = self.send_and_adopt(&segments).await?;
        response.check_errors()?;
        Ok((self.transition(), response))
    }
}

// ── Dialog<Synced> ──────────────────────────────────────────────────────────

impl Dialog<Synced> {
    /// End the sync dialog. Returns bank params and system_id for use in normal dialogs.
    pub async fn end(mut self) -> Result<(BankParams, SystemId)> {
        self.send_end().await.ok();
        Ok((self.params, self.system_id))
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Account — the account an order is for
// ═══════════════════════════════════════════════════════════════════════════════

/// The account an order is for, as KTI (international: IBAN and BIC) and,
/// where known, KTV (national: number, subaccount, bank code).
///
/// The constructors refuse an account the bank could not address: no IBAN
/// or no BIC for [`Account::new`], no number or no valid bank code for
/// [`Account::national`]. Orders on `Dialog<Open>` take `&Account`, so
/// they never see an empty identifier.
///
/// ```
/// use fints_client::protocol::Account;
///
/// let account = Account::new("DE14 1234 5678 0000 0047 11", "TESTDEFFXXX").unwrap();
/// assert_eq!(account.iban(), "DE14123456780000004711");
/// assert_eq!(account.account_number(), Some("4711"));
///
/// assert!(Account::new("DE14123456780000004711", " ").is_err());
/// ```
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct Account {
    iban: Iban,
    bic: Bic,
    /// KTV account number, for the segment versions that address accounts
    /// nationally.
    number: Option<String>,
    /// KTV "Unterkontomerkmal".
    subaccount: Option<String>,
    /// KTV bank code.
    blz: Option<Blz>,
    /// Send number, subaccount and bank code in KTI too (`IBAN:BIC:number::280:BLZ`).
    /// Set from the BPD (HISPAS) when the bank allows national account details.
    kti_national: bool,
}

/// `None` for a missing or blank subaccount.
fn subaccount_value(subaccount: Option<&str>) -> Option<String> {
    subaccount.filter(|s| !s.is_empty()).map(String::from)
}

impl Account {
    /// An account by IBAN and BIC; fails when either is blank.
    ///
    /// Blanks are removed from the IBAN and letters upper-cased. A German
    /// IBAN contains the bank code (positions 5 to 12) and the account
    /// number (13 to 22), which become the KTV that HKSAL 5 and 6, HKKAZ 5
    /// and 6 and HKWPD need.
    pub fn new(iban: &str, bic: &str) -> Result<Self> {
        let iban: String = iban
            .split_whitespace()
            .flat_map(str::chars)
            .map(|c| c.to_ascii_uppercase())
            .collect();
        let bic = bic.trim();
        if iban.is_empty() {
            return Err(FinTSError::Dialog("the account has no IBAN".into()));
        }
        if bic.is_empty() {
            return Err(FinTSError::Dialog(
                "the account has no BIC; set it in the account settings".into(),
            ));
        }
        let national = german_iban_parts(&iban);
        Ok(Self {
            number: national.as_ref().map(|(_, number)| number.clone()),
            blz: national.map(|(blz, _)| Blz::new(blz)),
            iban: Iban::new(iban),
            bic: Bic::new(bic),
            subaccount: None,
            kti_national: false,
        })
    }

    /// An account by its KTV alone, the way the UPD lists most depots.
    /// HKWPD addresses such an account; orders whose segment version needs
    /// an IBAN fail for it. National account details exist only in Germany
    /// (country code 280), so the bank code must have 8 digits.
    pub fn national(number: &str, subaccount: Option<&str>, blz: &Blz, bic: &str) -> Result<Self> {
        let number = number.trim();
        let code = blz.as_str();
        if number.is_empty() || code.is_empty() {
            return Err(FinTSError::Dialog(
                "a national account needs an account number and a bank code".into(),
            ));
        }
        if code.len() != 8 || !code.chars().all(|c| c.is_ascii_digit()) {
            return Err(FinTSError::Dialog(format!(
                "bank code {code:?} is not 8 digits"
            )));
        }
        let account = Self {
            iban: Iban::new(""),
            bic: Bic::new(bic.trim()),
            number: None,
            subaccount: None,
            blz: None,
            kti_national: false,
        };
        Ok(account.with_national(number, subaccount, blz))
    }

    /// The same account with this subaccount, for an account the UPD
    /// lists by IBAN and subaccount.
    pub(crate) fn with_subaccount(self, subaccount: Option<&str>) -> Self {
        Self {
            subaccount: subaccount_value(subaccount),
            ..self
        }
    }

    /// The same account with this KTV, which replaces anything taken from
    /// the IBAN; used with the details the UPD gives.
    pub fn with_national(self, number: &str, subaccount: Option<&str>, blz: &Blz) -> Self {
        Self {
            number: Some(number.to_owned()),
            subaccount: subaccount_value(subaccount),
            blz: Some(blz.clone()),
            ..self
        }
    }

    /// Whether KTI carries the national parts too (see [`Account::kti_national`] field).
    pub(crate) fn with_kti_national(self, kti_national: bool) -> Self {
        Self {
            kti_national,
            ..self
        }
    }

    pub(crate) fn kti_national(&self) -> bool {
        self.kti_national
    }

    /// The IBAN without blanks; empty for an account known nationally only.
    pub fn iban(&self) -> &str {
        self.iban.as_str()
    }

    pub fn bic(&self) -> &str {
        self.bic.as_str()
    }

    /// The KTV account number, if known.
    pub fn account_number(&self) -> Option<&str> {
        self.number.as_deref()
    }

    /// The KTV subaccount, if any.
    pub fn subaccount(&self) -> Option<&str> {
        self.subaccount.as_deref()
    }

    /// The KTV bank code, if known.
    pub fn blz(&self) -> Option<&str> {
        self.blz.as_ref().map(Blz::as_str)
    }
}

/// Bank code and account number of a German IBAN (`DE`, two check digits,
/// eight digits bank code, ten digits account number). The account number
/// loses its leading zeros, keeping a single `0` for an all-zero number.
fn german_iban_parts(iban: &str) -> Option<(String, String)> {
    let digits = iban.strip_prefix("DE")?;
    if digits.len() != 20 || !digits.bytes().all(|b| b.is_ascii_digit()) {
        return None;
    }
    let (bank_code, account) = (&digits[2..10], &digits[10..]);
    let significant = account.trim_start_matches('0');
    let number = if significant.is_empty() {
        "0"
    } else {
        significant
    };
    Some((bank_code.to_owned(), number.to_owned()))
}

// ── Dialog<Open> ─────────────────────────────────────────────────────────────

impl Dialog<Open> {
    /// Send a business order, with the 9075 retry and learning.
    pub(crate) async fn send_order(&mut self, segment: Segment, code: &str) -> Result<Response> {
        let (mut response, decision) = self
            .send_order_decided(&[], segment.clone(), code, None)
            .await?;
        match on_sca_required(&decision, response.all_codes().map(ReturnCode::kind)) {
            Retry::No => {}
            Retry::SameDialog => {
                info!(
                    "[FinTS] {}: 9075 without HKTAN, sending it again with HKTAN",
                    code
                );
                let (retry, _) = self
                    .send_order_decided(&[], segment.clone(), code, Some(true))
                    .await?;
                if !retry.all_codes().any(|c| c.is_error()) {
                    info!("[FinTS] {}: 9075 without HKTAN, learned", code);
                    self.params
                        .tan_required_learned
                        .insert(SegmentType::new(code));
                }
                response = retry;
            }
            Retry::DialogGone => {
                info!("[FinTS] {}: 9075 with 9800 without HKTAN, learned", code);
                self.params
                    .tan_required_learned
                    .insert(SegmentType::new(code));
                return Ok(response);
            }
        }
        Ok(response)
    }

    /// A payment order (HKCCS, HKIPZ) after the `extra` segments (HKVPP in
    /// message 1, HKVPA in the execution message), with HKTAN process 4 as
    /// `order_tan` decides (always, unless a profile override says
    /// otherwise). No 9075 retry: a payment is never sent twice.
    pub(crate) async fn send_payment(
        &mut self,
        extra: &[Segment],
        order: Segment,
        code: &str,
    ) -> Result<Response> {
        Ok(self.send_order_decided(extra, order, code, None).await?.0)
    }

    /// HKVPP alone: a payee check poll (VoP C.10.7.1.1). No order, no
    /// HKTAN.
    pub(crate) async fn send_vop_poll(&mut self, poll: Segment) -> Result<Response> {
        self.send_segments(&[poll]).await
    }

    /// Send an order after the `extra` segments, with HKTAN as `order_tan`
    /// decides, or as `force` says (the 9075 retry). Returns the decision
    /// that was used.
    async fn send_order_decided(
        &mut self,
        extra: &[Segment],
        segment: Segment,
        code: &str,
        force: Option<bool>,
    ) -> Result<(Response, TanDecision)> {
        let mut segments = extra.to_vec();
        segments.push(segment);
        let mut decision = order_tan(
            &SegmentType::new(code),
            self.params.selected_security_function != SecurityFunction::pin_only(),
            self.params.bpd().and_then(|b| b.tan_required(code)),
            &self.tan_overrides,
            &self.params.tan_required_learned,
        );
        if let Some(hktan) = force {
            decision.hktan = hktan;
        }
        if decision.hktan {
            info!("[FinTS] {}: + HKTAN:4 ({:?})", code, decision.reason);
            segments.push(Segment::TanProcess4 {
                reference_seg: SegmentRef::new(code),
                tan_medium: self.params.selected_tan_medium.clone(),
            });
        } else {
            info!("[FinTS] {}: no HKTAN ({:?})", code, decision.reason);
        }
        let response = self.send_segments(&segments).await?;
        for c in response.all_codes() {
            if matches!(c.class(), Class::Error | Class::Warning) {
                info!("[FinTS] {}: {} - {}", code, c.code, c.text);
            }
        }
        Ok((response, decision))
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Response parsing
// ═══════════════════════════════════════════════════════════════════════════════

/// The segments of a message with HNVSD replaced by the segments it holds.
fn unwrap_hnvsd(outer_segments: Vec<RawSegment>) -> Result<Vec<RawSegment>> {
    let mut all_segments = Vec::new();
    for seg in outer_segments {
        if seg.segment_type() == "HNVSD" {
            match crate::segments::read::hnvsd_data(&seg)
                .map_err(|e| e.to_string())
                .and_then(|data| crate::codec::wire::tokenize(&data).map_err(|e| e.to_string()))
            {
                Ok(inner) => all_segments.extend(inner),
                // The encrypted envelope holds the whole message: if it does
                // not read, nothing in it is known.
                Err(e) => return Err(FinTSError::Parse(format!("unreadable HNVSD: {e}"))),
            }
        } else {
            all_segments.push(seg);
        }
    }
    Ok(all_segments)
}

/// Parses the bank's answer to `request` (the sent message) and names the
/// order each HIRMS code refers to.
fn parse_answer(data: &[u8], expected_msg_num: u16, request: &[u8]) -> Result<Response> {
    let mut response = parse_response(data, expected_msg_num)?;
    // Our own message: it always tokenizes; if not, orders stay unnamed.
    if let Ok(sent) = crate::codec::wire::tokenize(request)
        .map_err(|e| FinTSError::Parse(e.to_string()))
        .and_then(unwrap_hnvsd)
    {
        response.name_orders(&sent);
    }
    Ok(response)
}

fn parse_response(data: &[u8], expected_msg_num: u16) -> Result<Response> {
    use crate::segments::read;

    let outer_segments =
        crate::codec::wire::tokenize(data).map_err(|e| FinTSError::Parse(e.to_string()))?;

    if let Some((_, resp_num)) = outer_segments
        .iter()
        .find(|s| s.segment_type() == "HNHBK")
        .and_then(read::message_header)
        && resp_num != u64::from(expected_msg_num)
    {
        warn!(
            "Message number mismatch: expected {}, got {}",
            expected_msg_num, resp_num
        );
    }

    // The size in HNHBK is mandatory; a bank that gets it wrong is still
    // read (the answer may be complete), but the mismatch is logged: it is
    // the first sign of a cut or re-encoded answer.
    if let Some(stated) = outer_segments
        .iter()
        .find(|s| s.segment_type() == "HNHBK")
        .and_then(read::message_size)
        && stated != data.len() as u64
    {
        warn!(
            "HNHBK states {} bytes, the answer has {} bytes",
            stated,
            data.len()
        );
    }

    let all_segments = unwrap_hnvsd(outer_segments)?;

    // Every answer carries HIRMG (Formals B.7.1, message layout: "Rückmeldungen zur
    // Gesamtnachricht" is M). Without it the answer is cut off or not
    // a FinTS answer, and must not pass as a success.
    if !all_segments.iter().any(|s| s.segment_type() == "HIRMG") {
        return Err(FinTSError::InvalidResponse(
            "answer without HIRMG (cut off or not a FinTS answer)".into(),
        ));
    }

    let mut global_codes = Vec::new();
    let mut segment_codes = Vec::new();
    for seg in &all_segments {
        let target = match seg.segment_type() {
            "HIRMG" => &mut global_codes,
            "HIRMS" => &mut segment_codes,
            _ => continue,
        };
        // An unreadable HIRMG/HIRMS makes `check_errors` fail.
        match read::response_codes(seg) {
            Ok(codes) => target.extend(codes),
            Err(e) => read::report(&e),
        }
    }

    Ok(Response {
        segments: all_segments,
        global_codes,
        segment_codes,
    })
}

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

    #[test]
    fn response_surfaces_codec_errors_of_raw_segments() {
        let response = Response {
            segments: crate::codec::wire::tokenize(
                b"HIRMG:2:2+0010::ok'\
                  HITANS:3:6:4+1+1+1+J:N:0:921:2:T:HHD:1.4:Name:6:1:TAN:6:N:1:N:0:0:N:N:00:0:N:0'\
                  HITANS:4:6:4+1+1+1+J:N:0:921:2:T:HHD:1.4:Name:6:1:TAN:x'",
            )
            .unwrap(),
            global_codes: vec![],
            segment_codes: vec![],
        };
        let decoded = response.decode_segments();
        assert_eq!(decoded.len(), 3);
        assert!(decoded[1].typed().is_some());
        let errors = response.segment_errors();
        assert_eq!(errors.len(), 1, "HIRMG has no definition yet: not an error");
        assert_eq!(
            errors[0].field,
            "parameters.methods[0].max_return_value_length"
        );
    }

    #[test]
    fn camt_format_choice_by_version_number() {
        let pick = |formats: &str| {
            params_with_bpd(&format!("HICAZS:1:1:4+1+1+1+450:N:N:{formats}'")).camt052_format()
        };
        let u = "urn?:iso?:std?:iso?:20022?:tech?:xsd?:camt.052.001";
        let plain = |n: &str| format!("urn:iso:std:iso:20022:tech:xsd:camt.052.001.{n}");
        // Same version: the descriptor without .xsd, as the bank wrote it.
        assert_eq!(pick(&format!("{u}.02.xsd:{u}.02")), plain("02"));
        assert_eq!(pick(&format!("{u}.02:{u}.02.xsd")), plain("02"));
        assert_eq!(pick(&format!("{u}.02:{u}.08")), plain("08"));
        assert_eq!(
            pick(&format!("{u}.08.xsd:{u}.02")),
            format!("{}.xsd", plain("08"))
        );
        // Numeric, not string order.
        assert_eq!(pick(&format!("{u}.10:{u}.9")), plain("10"));
    }

    #[test]
    fn camt_format_from_hicazs() {
        assert_eq!(
            BankParams::new().camt052_format(),
            "urn:iso:std:iso:20022:tech:xsd:camt.052.001.02"
        );
        let p = params_with_bpd(
            "HICAZS:1:1:4+1+1+1+450:N:N:urn?:iso?:std?:iso?:20022?:tech?:xsd?:camt.052.001.08:urn?:iso?:std?:iso?:20022?:tech?:xsd?:camt.052.001.02'",
        );
        assert_eq!(
            p.camt052_format(),
            "urn:iso:std:iso:20022:tech:xsd:camt.052.001.08"
        );
        let seg = Segment::CamtTransactions {
            account: Account::new("DE00120300009900012345", "BYLADEM1001").unwrap(),
            start_date: Date::new(2026, 1, 1).unwrap(),
            end_date: Date::new(2026, 1, 31).unwrap(),
            touchdown: None,
        };
        assert!(seg.encode(&p, 3).is_ok());
        // A bank without HICAZS: VersionMismatch, not a guessed version.
        let no_camt = params_with_bpd("HIKAZS:1:7:4+1+1+1'");
        assert!(matches!(
            seg.encode(&no_camt, 3),
            Err(FinTSError::VersionMismatch(_))
        ));
    }

    fn params_with_bpd(raw: &str) -> BankParams {
        // The model needs a HIBPA; the tests only care about what follows it.
        let mut p = BankParams::new();
        let wire = format!("HIBPA:1:3:4+1+280:1+Bank+1+1+300'{raw}");
        p.set_bpd(crate::codec::wire::tokenize(wire.as_bytes()).unwrap());
        p
    }

    /// The orders a job exists for: all.
    fn built_orders() -> impl Iterator<Item = Order> {
        Order::ALL.iter().copied()
    }

    /// The parameter segment of an order offering `versions`.
    fn param_segments(order: Order, versions: &[u16]) -> String {
        let code = order.code();
        let param = format!("{}I{}S", &code[..1], &code[2..]);
        versions
            .iter()
            .enumerate()
            .map(|(i, v)| format!("{param}:{}:{v}:4+1+1+0'", i + 2))
            .collect()
    }

    #[test]
    fn order_version_picks_highest_common_version() {
        for order in built_orders() {
            let implemented = order.implemented();
            let max = *implemented.last().unwrap();
            let mut offered = implemented.to_vec();
            offered.push(max + 1);
            let p = params_with_bpd(&param_segments(order, &offered));
            assert_eq!(p.order_version(order).unwrap(), max, "{order:?}");
            let p = params_with_bpd(&param_segments(order, &implemented[..1]));
            assert_eq!(p.order_version(order).unwrap(), implemented[0], "{order:?}");
        }
    }

    #[test]
    fn order_version_without_overlap_names_both_sides() {
        for order in built_orders() {
            let max = *order.implemented().last().unwrap();
            let p = params_with_bpd(&param_segments(order, &[max + 1, max + 2]));
            let Err(FinTSError::VersionMismatch(m)) = p.order_version(order) else {
                panic!("{order:?}: expected VersionMismatch");
            };
            assert_eq!(m.code.as_str(), order.code());
            assert_eq!(m.offered, [max + 1, max + 2]);
            assert_eq!(m.implemented, order.implemented());
        }
        let p = params_with_bpd("HICAZS:1:2:4+1+1+1'");
        assert_eq!(
            p.order_version(Order::Camt).unwrap_err().to_string(),
            "HKCAZ: no common version (bank offers 2, client implements 1)"
        );
    }

    #[test]
    fn order_version_order_not_in_bpd() {
        let p = params_with_bpd("HIKAZS:1:7:4+1+1+1'");
        for &order in Order::ALL.iter().filter(|o| **o != Order::Transactions) {
            let Err(FinTSError::VersionMismatch(m)) = p.order_version(order) else {
                panic!("{order:?}: expected VersionMismatch");
            };
            assert!(m.offered.is_empty(), "{order:?}");
        }
        assert_eq!(
            p.order_version(Order::Camt).unwrap_err().to_string(),
            "HKCAZ: bank does not offer this order (client implements 1)"
        );
    }

    #[test]
    fn order_version_without_bpd_is_an_error() {
        for &order in Order::ALL {
            let err = BankParams::new().order_version(order).unwrap_err();
            assert!(
                matches!(&err, FinTSError::NoBpd { code } if code.as_str() == order.code()),
                "{order:?}: {err}"
            );
        }
        assert_eq!(
            BankParams::new()
                .order_version(Order::Balance)
                .unwrap_err()
                .to_string(),
            "HKSAL: no bank parameter data (BPD) known; synchronise first"
        );
    }

    fn tan_method(sf: &str, v: u16) -> TanMethod {
        TanMethod {
            security_function: SecurityFunction::new(sf),
            tan_process: TanProcess::TwoStep,
            name: sf.into(),
            needs_tan_medium: false,
            decoupled_max_polls: -1,
            wait_before_first_poll: 0,
            wait_before_next_poll: 0,
            is_decoupled: false,
            hktan_version: v,
            zka_name: None,
            decoupled_auto_polling: None,
            challenge_structured: false,
        }
    }

    fn params_with_methods(methods: &[(&str, u16)], selected: &str) -> BankParams {
        let mut p = BankParams::new();
        p.tan_methods = methods.iter().map(|(sf, v)| tan_method(sf, *v)).collect();
        p.selected_security_function = SecurityFunction::new(selected);
        p
    }

    fn hktan_wire(p: &BankParams) -> String {
        let seg = Segment::TanProcess4 {
            reference_seg: SegmentRef::new("HKIDN"),
            tan_medium: None,
        };
        let raw = seg.encode(p, 3).unwrap();
        String::from_utf8(crate::codec::wire::serialize_segment(&raw).unwrap()).unwrap()
    }

    #[test]
    fn hktan_version_is_the_highest_buildable_of_the_selected_method() {
        let p = params_with_methods(&[("902", 6), ("902", 7), ("902", 8), ("912", 6)], "902");
        assert_eq!(p.hktan_version().unwrap(), 7);
        assert_eq!(p.init_hktan_version().unwrap(), Some(7));
        let p = params_with_methods(&[("902", 6), ("902", 7), ("912", 6)], "912");
        assert_eq!(p.hktan_version().unwrap(), 6, "not another method's 7");
    }

    #[test]
    fn hktan_version_without_a_buildable_version_names_both_sides() {
        let p = params_with_methods(&[("902", 8), ("912", 7)], "902");
        let err = p.hktan_version().unwrap_err();
        assert!(
            matches!(
                &err,
                FinTSError::VersionMismatch(v)
                    if v.code.as_str() == "HKTAN" && v.offered == [8] && v.implemented == [5, 6, 7]
            ),
            "{err}"
        );
        // The backstop reaches the wire builder and the init decision.
        assert!(p.init_hktan_version().is_err());
        let seg = Segment::TanPollDecoupled {
            task_reference: TaskReference::new("T"),
            tan_medium: None,
        };
        assert!(matches!(
            seg.encode(&p, 3),
            Err(FinTSError::VersionMismatch(_))
        ));
        // A method no HITANS lists has no version either (empty `offered`).
        let p = params_with_methods(&[("912", 7)], "902");
        assert!(matches!(
            p.hktan_version(),
            Err(FinTSError::VersionMismatch(v)) if v.offered.is_empty()
        ));
    }

    #[test]
    fn hktan_version_without_hitans_is_7() {
        let p = BankParams::new();
        assert_eq!(p.hktan_version().unwrap(), 7);
        assert_eq!(p.init_hktan_version().unwrap(), Some(7));
        assert_eq!(hktan_wire(&p), "HKTAN:3:7+4+HKIDN'");
    }

    #[test]
    fn init_without_hktan_when_the_method_is_below_6() {
        let p = params_with_methods(&[("902", 5), ("912", 7)], "902");
        assert_eq!(
            p.hktan_version().unwrap(),
            5,
            "orders use the method's version"
        );
        assert_eq!(p.init_hktan_version().unwrap(), None);
        assert!(hktan_wire(&p).starts_with("HKTAN:3:5+4+"));
        // PIN only with HITANS known: nothing to signal at init.
        let p = params_with_methods(&[("912", 7)], "999");
        assert_eq!(p.init_hktan_version().unwrap(), None);
    }

    #[test]
    fn unbuildable_tan_methods_are_not_offered_or_selected() {
        let v8 = "HITANS:53:8:4+1+1+1+N:N:0:941:2:TESTAPP:Decoupled::Neue App:::Freigabe:2048:N:2:N:0:0:N:N:00:2:N:1:60:4:2:J:J'";
        let v7 = "HITANS:42:7:4+1+1+1+N:N:0:\
            931:2:TESTHHD:HHD:1.4:Kartenleser Test:6:1:TAN:6:N:1:N:0:0:N:N:00:0:N:0::::::\
            932:2:TESTAPP:Decoupled::App-Freigabe Test:::Freigabe:2048:N:2:N:0:0:N:N:00:2:N:1:60:4:2:J:J:\
            933:2:TESTSMS:mobileTAN::SMS Test:6:1:TAN:6:N:1:N:0:0:N:N:00:2:N:1:::::'";
        let wire = format!("HIBPA:1:3:4+1+280:1+Bank+1+1+300'{v7}{v8}");
        let mut p = BankParams::new();
        p.set_bpd(crate::codec::wire::tokenize(wire.as_bytes()).unwrap());
        let offered: Vec<&str> = p
            .tan_methods
            .iter()
            .map(|m| m.security_function.as_str())
            .collect();
        assert_eq!(offered, ["931", "932", "933"], "941 is HITANS 8");

        // The bank allows 941, but we cannot build its HKTAN: it is skipped
        // like an unknown method, also as a stored preference.
        p.allowed_security_functions =
            vec![SecurityFunction::new("941"), SecurityFunction::new("932")];
        p.preferred_security_function = Some(SecurityFunction::new("941"));
        p.select_security_function();
        assert_eq!(p.selected_security_function, SecurityFunction::new("932"));

        // The backstop still names the HITANS versions of an unbuildable method.
        p.selected_security_function = SecurityFunction::new("941");
        assert!(matches!(
            p.hktan_version(),
            Err(FinTSError::VersionMismatch(v)) if v.offered == [8]
        ));
    }

    /// Decoupled status with the HKTAN version the BPD offers: process S
    /// with HKTAN 7, process 2 with HKTAN 5 and 6 (PIN/TAN B.5.2; owner
    /// decision 2026-11-15; common client behaviour, S only from version 7).
    #[test]
    fn decoupled_poll_follows_the_hktan_version() {
        let poll = Segment::TanPollDecoupled {
            task_reference: TaskReference::new("TASK-1"),
            tan_medium: None,
        };
        let mut p = BankParams::new();
        let wire = |p: &BankParams| {
            let raw = poll.encode(p, 3).unwrap();
            String::from_utf8(crate::codec::wire::serialize_segment(&raw).unwrap()).unwrap()
        };
        for (version, expected) in [
            (7, "HKTAN:3:7+S++++TASK-1+N'"),
            (6, "HKTAN:3:6+2++++TASK-1+N'"),
            (5, "HKTAN:3:5+2++++TASK-1++N'"),
        ] {
            p.tan_methods = vec![TanMethod {
                security_function: SecurityFunction::new("902"),
                tan_process: TanProcess::TwoStep,
                name: "App".into(),
                needs_tan_medium: false,
                decoupled_max_polls: -1,
                wait_before_first_poll: 0,
                wait_before_next_poll: 0,
                is_decoupled: true,
                hktan_version: version,
                zka_name: None,
                decoupled_auto_polling: None,
                challenge_structured: false,
            }];
            p.selected_security_function = SecurityFunction::new("902");
            assert_eq!(wire(&p), expected);
        }
    }

    /// Feeds `data` to everything that reads bank answers. Used by the
    /// property tests below, which only check that nothing panics.
    fn read_everything(data: &[u8]) {
        use crate::segments::{read, response};
        let Ok(answer) = parse_response(data, 1) else {
            return;
        };
        let _ = (
            answer.check_errors(),
            answer.get_tan_challenge(),
            answer.touchdown(),
        );

        let mut params = BankParams::new();
        params.ingest_response(&answer, &mut SystemId::unassigned());
        params.select_security_function();
        let _ = params.camt052_format();

        for segment in &answer.segments {
            let _ = (read::balance(segment), read::tan_challenge(segment));
            let _ = (read::upd_account(segment), read::tan_methods(segment));
            let _ = (
                response::parse_hiupd(segment),
                response::parse_hitans(segment),
            );
        }

        let all = &answer.segments;
        let _ = (read::parse_hiwpd(all), read::parse_hiwdu(all));
        let _ = read::extract_mt940_data(all);
        let camt = read::extract_camt_data(all);
        for report in camt.booked.iter().chain(&camt.pending) {
            let _ = crate::camt::parse_camt052(report, crate::banks::BookingIdSource::None);
        }
    }

    /// Pieces that look like parts of real answers, so that random
    /// combinations reach deep into the readers.
    const ANSWER_PIECES: &[&str] = &[
        "HIRMG:2:2+",
        "HIRMS:3:2:4+",
        "HISAL:4:7:5+",
        "HITAN:5:7:6+",
        "HITANS:6:7:4+1+1+0+",
        "HIUPD:7:6:4+",
        "HICAZ:8:1:5+",
        "HICAZS:9:1:4+1+1+0+400:J:N:",
        "HIWPD:10:6:5+",
        "HIWDU:11:5:5+",
        "HNVSD:999:1+",
        "@4@",
        "@123456789012345678901@",
        "3040::mehr:TP",
        "DE14123456780000004711:TESTDEFFXXX:4711::280:12345678",
        "C:1,5:EUR:20260115",
        ":16R:GENL\r\n:35B:ISIN DE0000000001\r\n:16S:FIN\r\n",
    ];

    use proptest::strategy::Strategy as _;

    proptest::proptest! {
        #[test]
        fn readers_survive_arbitrary_bytes(
            bytes in proptest::collection::vec(proptest::num::u8::ANY, 0..600)
        ) {
            read_everything(&bytes);
        }

        #[test]
        fn readers_survive_shuffled_answer_pieces(
            picks in proptest::collection::vec(
                proptest::prop_oneof![
                    proptest::sample::select(ANSWER_PIECES).prop_map(str::to_owned),
                    "[0-9A-Z:+?'@ ,]{0,10}",
                ],
                0..30,
            )
        ) {
            read_everything(picks.concat().as_bytes());
        }
    }

    /// Return code 3072 hands out a new user ID (Formals, return codes).
    /// The old UPD belongs to the old ID and is asked for again.
    #[test]
    fn new_user_id_from_3072_is_kept_and_the_upd_dropped() {
        let mut params = BankParams::new();
        let upd = b"HIUPA:4:4:3+kunde7+5+0'HIUPD:5:6:3+4711::280:12345678+DE14123456780000004711+kunde7++EUR+Erika Muster'";
        params.set_upd(crate::codec::wire::tokenize(upd).unwrap());
        assert_eq!(params.upd_version, 5);

        let answer = parse_response(
            b"HIRMG:2:2+0010::Nachricht entgegengenommen.'\
              HIRMS:3:2:3+3072::Neue Zugangsdaten:kunde8:kundenid8'",
            1,
        )
        .unwrap();
        // A warning, not an error.
        answer.check_errors().unwrap();
        params.ingest_response(&answer, &mut SystemId::new("SYS-0042"));
        assert_eq!(
            params.changed_user_id.as_ref().map(UserId::as_str),
            Some("kunde8")
        );
        assert_eq!(params.upd_version, 0);
        assert!(params.upd().is_none());
    }

    /// An answer that carries the BPD again (here: the same answer twice)
    /// replaces the stored BPD; nothing is listed twice.
    #[test]
    fn repeated_bpd_replaces_the_stored_one() {
        let answer = parse_response(
            b"HIRMG:2:2+0010::ok'\
              HIBPA:3:3:4+21+280:12345678+Testbank+1+1+300'\
              HITANS:4:7:4+1+1+1+J:N:0:902:2:TEST902:Decoupled:1.0:Test-App:::Freigabe:0:N:1:N:0:0:N:N:00:0:N:0:0:0:0:N:J'",
            1,
        )
        .unwrap();
        let mut params = params_with_bpd("");
        let mut system_id = SystemId::unassigned();
        for _ in 0..2 {
            params.ingest_response(&answer, &mut system_id);
        }
        assert_eq!(params.bpd_version, 21);
        let codes: Vec<_> = params
            .tan_methods
            .iter()
            .map(|m| m.security_function.as_str())
            .collect();
        assert_eq!(codes, ["902"]);
    }

    /// A bank wraps the reason for a rejected order in a generic global
    /// 9050; the segment code must win.
    #[test]
    fn segment_error_wins_over_generic_global_code() {
        let resp = parse_response(
            b"HIRMG:2:2+9050::Die Nachricht enthaelt Fehler'              HIRMS:3:2:3+0020::ok'HIRMS:4:2:4+9010::Verarbeitung nicht moeglich+9210::Keine gueltige Kontoverbindung'",
            1,
        )
        .unwrap();
        let err = resp.check_errors().unwrap_err();
        assert_eq!(
            err.to_string(),
            "Bank error 9210: Keine gueltige Kontoverbindung"
        );
        // Only generic codes: the segment one first.
        let resp = parse_response(
            b"HIRMG:2:2+9050::Teilweise fehlerhaft'HIRMS:3:2:3+9010::Abgelehnt'",
            1,
        )
        .unwrap();
        assert!(
            resp.check_errors()
                .unwrap_err()
                .to_string()
                .contains("9010")
        );
        // A wrong PIN anywhere still wins.
        let resp = parse_response(
            b"HIRMG:2:2+9050::Fehler'HIRMS:3:2:3+9210::Konto'HIRMS:4:2:1+9942::PIN falsch'",
            1,
        )
        .unwrap();
        assert_eq!(
            resp.check_errors().unwrap_err().kind(),
            Category::Credentials(crate::return_code::Credentials::PinWrong)
        );
    }

    /// Notices: warnings and unknown codes; not success, errors or the
    /// codes the protocol consumes (docs/return-codes-design.md section 2).
    #[test]
    fn notices_skip_success_errors_and_consumed_codes() {
        let answer = b"HIRMG:2:2+0010::ok+3050::veraltet+3060::Hinweise'\
            HIRMS:3:2:3+0030::TAN+3040::mehr:abc+3076::SCA+3920::Verfahren:912\
            +3955::decoupled+3956::offen+3091::VOP+3945::VOP+3072::neu:A:B\
            +3010::leer+3957::Push+3958::App+3999::bank+1234::note+9210::Konto'";
        let resp = parse_response(answer, 1).unwrap();
        let codes: Vec<String> = resp
            .notices(NoticeDialog::Job)
            .iter()
            .map(|n| n.code().unwrap().code.as_str().to_string())
            .collect();
        assert_eq!(
            codes,
            [
                "3050", "3060", "3072", "3010", "3957", "3958", "3999", "1234"
            ]
        );
        let none = parse_response(b"HIRMG:2:2+0010::ok'HIRMS:3:2:3+0020::ok'", 1).unwrap();
        assert!(none.notices(NoticeDialog::Job).is_empty());
    }

    /// A wrong typed TAN leaves the order for another TAN only when 9941
    /// is the one cause (docs/return-codes-design.md section 1): (answer,
    /// attempts left when it does).
    #[test]
    fn tan_rejection_only_where_the_order_stays() {
        let cases: &[(&[u8], Option<Option<u32>>)] = &[
            (b"HIRMG:2:2+9050::F'HIRMS:3:2:3+9941::TAN ungueltig'", Some(None)),
            (
                b"HIRMG:2:2+9050::F'HIRMS:3:2:3+9941::TAN ungueltig+9360::Sperre nach weiteren 2:2'",
                Some(Some(2)),
            ),
            // 9360 with no attempts left, or without P1 (locked).
            (
                b"HIRMG:2:2+9050::F'HIRMS:3:2:3+9941::TAN ungueltig+9360::Sperre:0'",
                None,
            ),
            (b"HIRMG:2:2+9050::F'HIRMS:3:2:3+9941::T+9360::gesperrt'", None),
            // The dialog ended, the order was deleted, the access locked.
            (
                b"HIRMG:2:2+9050::F+9800::Dialog abgebrochen'HIRMS:3:2:3+9941::T'",
                None,
            ),
            (
                b"HIRMG:2:2+9050::F'HIRMS:3:2:3+9941::T+9210::Auftrag geloescht'",
                None,
            ),
            (b"HIRMG:2:2+9050::F'HIRMS:3:2:3+9931::gesperrt'", None),
            // Timed out or used: not a wrong TAN.
            (b"HIRMG:2:2+9050::F'HIRMS:3:2:3+9951::Zeit'", None),
            (b"HIRMG:2:2+9050::F'HIRMS:3:2:3+9943::verbraucht'", None),
            // Success.
            (b"HIRMG:2:2+0010::ok'HIRMS:3:2:3+0020::ok'", None),
        ];
        for (answer, want) in cases {
            let resp = parse_response(answer, 1).unwrap();
            let got = resp.tan_rejection();
            let wire = String::from_utf8_lossy(answer);
            assert_eq!(got.as_ref().map(|(_, n)| *n), *want, "{wire}");
            if let Some((code, _)) = got {
                assert_eq!(code.code.as_str(), "9941", "{wire}");
            }
        }
    }

    /// The selection rule (docs/return-codes-design.md section 3) over
    /// multi-code answers: (answer, selected code).
    #[test]
    fn select_error_order() {
        let cases: &[(&[u8], Option<&str>)] = &[
            // Warnings and successes only.
            (b"HIRMG:2:2+3060::Hinweise'HIRMS:3:2:3+0020::ok'", None),
            // Rule 1: credentials, then system ID, wherever they are.
            (
                b"HIRMG:2:2+9050::F'HIRMS:3:2:3+9210::Konto'HIRMS:4:2:1+9942::PIN'",
                Some("9942"),
            ),
            (
                b"HIRMG:2:2+9391::Kundensystem-ID'HIRMS:3:2:3+9931::gesperrt'",
                Some("9931"),
            ),
            (
                b"HIRMG:2:2+9391::Kundensystem-ID'HIRMS:3:2:3+9210::Konto'",
                Some("9391"),
            ),
            // Live sets (2026-11-17), synthetic texts. A wrong BLZ for the
            // user: 9210 names the cause, 9340 is its consequence.
            (
                b"HIRMG:2:2+9050::Nachricht enthaelt Fehler+9800::Dialog abgebrochen\
                  +9210::Bankleitzahl passt nicht zum Benutzer+9340::Ungueltige Signatur'",
                Some("9210"),
            ),
            // A wrong PIN: 9942 next to 9800.
            (
                b"HIRMG:2:2+9800::Dialog abgebrochen+9942::PIN ungueltig'",
                Some("9942"),
            ),
            // 9340 loses to a named credentials cause and to any specific
            // error, segment or message scope; it wins over generic codes.
            (
                b"HIRMG:2:2+9340::Signatur'HIRMS:3:2:1+9942::PIN'",
                Some("9942"),
            ),
            (
                b"HIRMG:2:2+9340::Signatur'HIRMS:3:2:3+9380::nicht berechtigt'",
                Some("9380"),
            ),
            (
                b"HIRMG:2:2+9050::F+9800::Dialog abgebrochen'HIRMS:3:2:1+9340::Signatur'",
                Some("9340"),
            ),
            // Rule 2: the first specific segment error; 9800 is generic.
            (
                b"HIRMG:2:2+9050::F'HIRMS:3:2:3+9010::V+9210::Konto'",
                Some("9210"),
            ),
            (
                b"HIRMG:2:2+9050::F'HIRMS:3:2:3+9800::Dialog abgebrochen+9210::Konto'",
                Some("9210"),
            ),
            (
                b"HIRMG:2:2+9800::Dialog abgebrochen'HIRMS:3:2:3+9210::Konto'",
                Some("9210"),
            ),
            (
                b"HIRMG:2:2+9000::Status indifferent'HIRMS:3:2:3+9210::Konto'",
                Some("9210"),
            ),
            // Rule 3: else the first specific message error.
            (
                b"HIRMG:2:2+9800::Dialog abgebrochen+9075::SCA'HIRMS:3:2:3+9010::V'",
                Some("9075"),
            ),
            (
                b"HIRMG:2:2+9050::F+9000::Status indifferent:HKCAZ'",
                Some("9000"),
            ),
            // Rule 5: only generic codes; segment scope first.
            (
                b"HIRMG:2:2+9050::F'HIRMS:3:2:3+9010::Abgelehnt'",
                Some("9010"),
            ),
            (
                b"HIRMG:2:2+9800::Dialog abgebrochen'HIRMS:3:2:3+9050::F'",
                Some("9050"),
            ),
            (b"HIRMG:2:2+9050::F+9800::Dialog abgebrochen'", Some("9050")),
        ];
        for (answer, want) in cases {
            let resp = parse_response(answer, 1).unwrap();
            let got = resp.select_error();
            let wire = String::from_utf8_lossy(answer);
            assert_eq!(
                got.as_ref().map(|e| e.code().code.as_str()),
                *want,
                "{wire}"
            );
            if let Some(e) = got {
                // The error keeps every code of the response.
                assert_eq!(e.codes().len(), resp.all_codes().count(), "{wire}");
                assert!(resp.check_errors().is_err());
            }
        }
    }

    /// 9000: the outcome is unknown; P1 names the order to ask with.
    #[test]
    fn outcome_unknown_names_the_order_to_check() {
        let resp = parse_response(b"HIRMG:2:2+9000::Status indifferent:HKCAZ'", 1).unwrap();
        let err = resp.check_errors().unwrap_err();
        assert_eq!(err.kind(), Category::OutcomeUnknown);
        assert!(!err.is_retryable());
        assert_eq!(
            err.bank_error().map(|e| e.code().kind()),
            Some(Kind::OutcomeUnknown {
                query: Some(SegmentType::new("HKCAZ"))
            })
        );
    }

    /// A HIRMS code names the order it refers to, read from the sent
    /// message's segment numbers (also inside HNVSD); the error keeps the
    /// whole code line.
    #[test]
    fn segment_codes_name_their_order() {
        let request = b"HNHBK:1:3+000000000100+300+0+1'HNVSK:998:3+PIN:2'\
            HNVSD:999:1+@55@HNSHK:2:4+PIN:2'HKSAL:3:7+DE00:BIC+N'HKTAN:4:7+4+HKSAL''\
            HNHBS:5:1+1'";
        let answer = b"HIRMG:2:2+9050::Fehler'HIRMS:3:2:3+9210:2:Konto unbekannt:P1'\
            HIRMS:4:2:4+0020::ok'HIRMS:5:2:7+3920::Verfahren:912'";
        let resp = parse_answer(answer, 1, request).unwrap();
        let orders: Vec<_> = resp
            .segment_codes
            .iter()
            .map(|c| match &c.scope {
                Scope::Segment { number, order } => (*number, order.clone()),
                Scope::Message => panic!("HIRMS has segment scope"),
            })
            .collect();
        assert_eq!(
            orders,
            [
                (3, Some(SegmentType::new("HKSAL"))),
                (4, Some(SegmentType::new("HKTAN"))),
                (7, None),
            ]
        );
        assert_eq!(resp.global_codes[0].scope, Scope::Message);
        match resp.check_errors() {
            Err(FinTSError::Bank(e)) => {
                let code = e.code();
                assert_eq!(code.code.as_str(), "9210");
                assert_eq!(code.reference_element.as_deref(), Some("2"));
                assert_eq!(code.parameters, ["P1"]);
                assert!(matches!(code.scope, Scope::Segment { number: 3, .. }));
            }
            other => panic!("{other:?}"),
        }
    }

    /// A notice carries its dialog and the segment code its HIRMS
    /// reference names (segment number 3 is the HKSAL, 4 the HKTAN); a
    /// HIRMG notice is message-level, a reference outside the request
    /// shows only the number.
    #[test]
    fn notices_name_dialog_and_order() {
        let request = b"HNHBK:1:3+000000000100+300+0+1'HNVSK:998:3+PIN:2'\
            HNVSD:999:1+@55@HNSHK:2:4+PIN:2'HKSAL:3:7+DE00:BIC+N'HKTAN:4:7+4+HKSAL''\
            HNHBS:5:1+1'";
        let answer = b"HIRMG:2:2+3060::Hinweise'HIRMS:3:2:3+3010::leer'\
            HIRMS:4:2:4+3072::neu:A:B'HIRMS:5:2:7+3999::bank'";
        let resp = parse_answer(answer, 1, request).unwrap();
        let notices = resp.notices(NoticeDialog::Sync);
        let shown: Vec<String> = notices.iter().map(|n| n.to_string()).collect();
        assert_eq!(
            shown,
            [
                "3060 [sync, HIRMG]: Hinweise",
                "3010 [sync, HKSAL]: leer",
                "3072 [sync, HKTAN]: neu",
                "3999 [sync, HIRMS #7]: bank",
            ]
        );
        assert!(notices.iter().all(|n| n.dialog() == NoticeDialog::Sync));
        assert_eq!(
            resp.notices(NoticeDialog::Job)[0].dialog(),
            NoticeDialog::Job
        );
    }

    #[test]
    fn utf8_text_in_latin1_answer_is_a_client_notice() {
        // "für" as UTF-8 bytes in a Latin-1 answer; the real "ü" (0xFC) is fine.
        let bad = parse_response(b"HIRMG:2:2+3060::Hinweis f\xC3\xBCr Sie'", 1).unwrap();
        let notices = bad.notices(NoticeDialog::Job);
        assert_eq!(notices.len(), 2);
        assert_eq!(
            notices[1].client_warning(),
            Some(ClientWarning::Utf8TextInLatin1)
        );
        assert!(notices[1].code().is_none());
        assert!(notices[1].to_string().starts_with("client [job]:"));
        assert_eq!(bad.global_codes[0].text, "Hinweis f\u{c3}\u{bc}r Sie");
        let good = parse_response(b"HIRMG:2:2+3060::Hinweis f\xFCr Sie'", 1).unwrap();
        assert_eq!(good.notices(NoticeDialog::Job).len(), 1);
    }

    /// Every 9xxx code is an error, also one no table lists; the message
    /// keeps the bank's code and text.
    #[test]
    fn unlisted_error_code_fails_with_the_banks_words() {
        let answer = parse_response(b"HIRMG:2:2+9876::Systemfehler, bitte spaeter erneut'", 1);
        let error = answer.unwrap().check_errors().unwrap_err();
        let shown = error.to_string();
        assert_eq!(shown, "Bank error 9876: Systemfehler, bitte spaeter erneut");
    }

    /// HIRMG/HIRMS stay strict (Formals C.8.1: text `an ..80`): a code the
    /// codec cannot read is not taken for a success.
    #[test]
    fn unreadable_response_codes_are_an_error() {
        let wire = format!("HIRMG:2:2+0010::ok'HIRMS:3:2:3+0020::{}'", "x".repeat(81));
        let resp = parse_response(wire.as_bytes(), 1).unwrap();
        assert_eq!(resp.segment_codes.len(), 0);
        assert_eq!(resp.segment_errors().len(), 1);
        let err = resp.check_errors().unwrap_err();
        assert!(matches!(err, FinTSError::InvalidResponse(_)), "{err}");
    }

    /// Bank text is ISO 8859-1 (Formals B.1): 0xE4 is 'ä'.
    #[test]
    fn response_text_is_latin1() {
        let resp = parse_response(b"HIRMG:2:2+0010::Auftr\xe4ge ok'", 1).unwrap();
        assert_eq!(resp.global_codes[0].text, "Aufträge ok");
    }

    /// A depot listed in the UPD without IBAN reaches every order that
    /// addresses accounts nationally, and gets a clear error from the ones
    /// that need an IBAN.
    #[test]
    fn national_only_account_through_all_orders() {
        let depot = Account::national(
            "0123456789",
            Some("01"),
            &Blz::new("12030000"),
            "BYLADEM1001",
        )
        .unwrap();
        assert_eq!(depot.account_number(), Some("0123456789"));
        assert_eq!(depot.subaccount(), Some("01"));

        let p = params_with_bpd(
            "HISALS:1:6:4+1+1+1'HIKAZS:2:6:4+1+1+1'HIWPDS:3:6:4+1+1+1'HIWDUS:4:5:4+1+1+0'HICAZS:7:1:4+1+1+1'",
        );
        let date = Date::new(2026, 1, 1).unwrap();
        let national = [
            Segment::Balance {
                account: depot.clone(),
            },
            Segment::Transactions {
                account: depot.clone(),
                start_date: date,
                end_date: date,
                touchdown: None,
            },
            Segment::Holdings {
                account: depot.clone(),
                currency: None,
                touchdown: None,
            },
            Segment::DepotTransactions {
                account: depot.clone(),
                start_date: None,
                end_date: None,
                touchdown: None,
            },
        ];
        for seg in &national {
            let degs = seg.encode(&p, 3).unwrap().degs;
            assert_eq!(degs[1].get_str(0), "0123456789", "{seg:?}");
            assert_eq!(degs[1].get_str(3), "12030000", "{seg:?}");
        }
        let camt = Segment::CamtTransactions {
            account: depot,
            start_date: date,
            end_date: date,
            touchdown: None,
        };
        let err = camt.encode(&p, 3).unwrap_err();
        assert!(
            matches!(
                err,
                FinTSError::AccountNotAddressable {
                    version: 1,
                    reason: crate::error::AddressingReason::NoIbanBic,
                    ..
                }
            ),
            "{err}"
        );
        assert_eq!(
            err.to_string(),
            "HKCAZ v1 cannot address this account: it needs an IBAN and BIC, which the account lacks"
        );
        assert!(
            Account::national("1", None, &Blz::new("1203000"), "X").is_err(),
            "BLZ has 8 digits"
        );
    }

    #[test]
    fn german_iban_gives_the_national_details() {
        let german = Account::new(" de14 1234 5678 0000 0047 11 ", "TESTDEFFXXX").unwrap();
        assert_eq!(german.iban(), "DE14123456780000004711");
        assert_eq!(german.blz(), Some("12345678"));
        assert_eq!(german.account_number(), Some("4711"));
        assert_eq!(german.subaccount(), None);

        let zeros = Account::new("DE00123456780000000000", "TESTDEFFXXX").unwrap();
        assert_eq!(zeros.account_number(), Some("0"));
    }

    #[test]
    fn other_ibans_need_explicit_national_details() {
        let other = Account::new("XX00000000000000004711", "TESTXXXXXXX").unwrap();
        assert_eq!((other.blz(), other.account_number()), (None, None));

        let with = other.with_national("4711", Some(""), &Blz::new("12345678"));
        assert_eq!(with.account_number(), Some("4711"));
        assert_eq!(with.subaccount(), None);
        assert_eq!(with.blz(), Some("12345678"));
        assert_eq!(with.iban(), "XX00000000000000004711");
    }

    #[test]
    fn accounts_without_identifier_are_refused() {
        assert!(Account::new(" ", "TESTDEFFXXX").is_err());
        assert!(Account::new("DE14123456780000004711", "").is_err());
        let code = Blz::new("12345678");
        assert!(Account::national("", None, &code, "TESTDEFFXXX").is_err());
        assert!(Account::national("4711", None, &Blz::new("1234"), "TESTDEFFXXX").is_err());
        let depot = Account::national(" 4711 ", Some("01"), &code, "TESTDEFFXXX").unwrap();
        assert_eq!(depot.account_number(), Some("4711"));
        assert_eq!(depot.subaccount(), Some("01"));
        assert_eq!(depot.iban(), "");
    }
}