autumn-cli 0.7.0

CLI tool for the Autumn web framework
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
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
//! `autumn db backup` / `autumn db restore` — logical dump/restore for
//! self-hosted, daemon, and single-binary Autumn deployments (issue #1595).
//!
//! These commands shell out to `pg_dump` / `pg_restore`, resolving the database
//! URL(s) through the **exact same** code path as `autumn migrate` / the other
//! `autumn db` commands ([`crate::migrate::resolve_primary_url`],
//! [`crate::migrate::resolve_shard_database_urls_from_sources`]) so a backup
//! captures precisely the databases the running app uses — control plus every
//! configured shard — under the active profile/env overlay. The destructive
//! `restore` is gated by the same production guard as `autumn db drop`
//! (`guard_destructive`).
//!
//! # Artifact layout (S3-bolt-on friendly — issue #1619)
//!
//! A single `backup` run writes one self-describing *run directory*:
//!
//! ```text
//! <dir>/<profile>/<timestamp>/
//!     manifest.json          # version, created_at, profile, format, targets[]
//!     control.dump           # pg_dump -Fc (default; compressed) — or control.sql (plain)
//!     shard-<name>.dump      # one per configured shard
//! ```
//!
//! The run directory is the atomic unit of both retention (`--keep N`) and any
//! future offsite upload (#1619 can enumerate `manifest.json` and push the whole
//! prefix). Nothing outside this directory is touched.
//!
//! # Zero-external-tools for managed Postgres (AC #2)
//!
//! [`PgTools::locate`](crate::db::backup::PgTools::locate) resolves
//! `pg_dump`/`pg_restore` from, in order: an
//! explicit `AUTUMN_PG_BIN_DIR`, the managed-Postgres bundle's `bin` directory
//! (derived from `AUTUMN_MANAGED_PG_DATA_DIR`, which `autumn serve --bundled-pg`
//! sets), then the `PATH`. A managed-pg daemon therefore needs no externally
//! installed client tools.

use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::Arc;

use autumn_web::alerts::{Alert, AlertChannel, AlertCondition};

use crate::db::s3::{self, S3Client, S3Config, S3Credentials};
use crate::migrate;

/// Environment variable that pins the directory holding `pg_dump`/`pg_restore`.
/// Highest precedence in [`PgTools::locate`](crate::db::backup::PgTools::locate); lets an operator point at a
/// specific client-tools install.
const PG_BIN_DIR_ENV: &str = "AUTUMN_PG_BIN_DIR";

/// The managed-Postgres data-dir env var (`autumn serve --bundled-pg` sets it).
/// Mirrors `autumn_web::managed_pg::MANAGED_PG_DATA_DIR_ENV`; duplicated as a
/// private constant so a backup can locate the bundled client binaries without
/// depending on a managed-pg being constructed.
const MANAGED_PG_DATA_DIR_ENV: &str = "AUTUMN_MANAGED_PG_DATA_DIR";

/// Default directory (relative to the project root) that backup run directories
/// are written under when `--dir` is not given.
const DEFAULT_BACKUP_DIR: &str = "backups";

/// Upper bound on same-timestamp run-directory disambiguation attempts before a
/// backup gives up. Reaching this means ~1000 backups landed in the same second
/// for one profile, which is pathological — surfacing an error beats looping.
const MAX_RUN_DIR_ATTEMPTS: usize = 1000;

/// Marker `pg_dump` writes at the very end of a *plain* SQL dump. Its presence
/// is the integrity signal for plain-format artifacts (a truncated/partial dump
/// will not contain it).
const PLAIN_COMPLETE_MARKER: &str = "PostgreSQL database dump complete";

/// Backup artifact format.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum BackupFormat {
    /// `pg_dump -Fc` custom archive — compressed, restored with `pg_restore`.
    /// The default: compressed on disk and integrity-checkable via
    /// `pg_restore --list`.
    #[default]
    Custom,
    /// Plain `pg_dump` SQL text (uncompressed), restored with `psql`.
    Plain,
}

impl BackupFormat {
    /// The per-target artifact file extension for this format.
    const fn extension(self) -> &'static str {
        match self {
            Self::Custom => "dump",
            Self::Plain => "sql",
        }
    }

    /// The `pg_dump` `--format` flag value for this format.
    const fn pg_dump_format_flag(self) -> &'static str {
        match self {
            Self::Custom => "custom",
            Self::Plain => "plain",
        }
    }

    /// Parse the `--format` CLI value. Accepts the two documented spellings.
    pub fn parse(s: &str) -> Result<Self, String> {
        match s.trim().to_ascii_lowercase().as_str() {
            "custom" | "c" => Ok(Self::Custom),
            "plain" | "sql" | "p" => Ok(Self::Plain),
            other => Err(format!(
                "unknown backup format {other:?} (expected `custom` or `plain`)"
            )),
        }
    }
}

/// Which databases a backup/restore run operates on. Mirrors
/// [`crate::migrate::MigrateTarget`] so `--shard` / `--control-only` behave
/// identically to `autumn migrate`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TargetSelector {
    /// Control database plus every configured shard (the default).
    All,
    /// Only the control database.
    ControlOnly,
    /// A single shard, addressed by its configured name.
    Shard(String),
}

/// Arguments for `autumn db backup`.
#[derive(Debug, Clone)]
pub struct BackupArgs {
    /// Profile overlay to resolve the connection under (see `db create`).
    pub profile: Option<String>,
    /// Root directory for backup run directories (default: `./backups`).
    pub dir: Option<PathBuf>,
    /// Artifact format (default: custom/compressed).
    pub format: BackupFormat,
    /// Retention: keep only the newest N run directories for this profile,
    /// pruning older ones after a successful backup. `None` disables pruning.
    pub keep: Option<usize>,
    /// Which databases to capture.
    pub target: TargetSelector,
    /// Upload the completed run to the configured offsite destination after
    /// local verification + prune (issue #1619). The configured
    /// `backup.offsite.auto_upload = true` has the same effect without the flag.
    pub upload: bool,
}

/// Arguments for `autumn db restore`.
#[derive(Debug, Clone)]
pub struct RestoreArgs {
    /// Path to a backup run directory (or a single artifact file) to restore.
    pub artifact: PathBuf,
    /// Profile overlay to resolve the connection under.
    pub profile: Option<String>,
    /// Bypass the production guard (mirrors `autumn db drop --force`).
    pub force: bool,
    /// Restore only this shard from the artifact (mirrors `--shard`).
    pub shard: Option<String>,
    /// Treat `artifact` as an offsite reference (`<profile>/<timestamp|latest>`,
    /// with an optional `offsite:` prefix) and restore from the offsite
    /// destination instead of a local path (issue #1619). An `offsite:` prefix
    /// on `artifact` implies this even when the flag is not set.
    pub offsite: bool,
}

/// Failure modes for backup/restore. `Display` is credential-safe: no variant
/// ever embeds a resolved URL (only parsed host/port/db), matching the rest of
/// the `db` command family.
#[derive(Debug)]
pub enum BackupError {
    /// No database URL could be resolved from config or environment.
    NoUrl,
    /// A named shard was requested but not found in the resolved topology.
    UnknownShard { name: String, known: Vec<String> },
    /// `pg_dump`/`pg_restore`/`psql` was not found on PATH or in a bundle.
    ToolMissing { tool: String },
    /// A shelled-out tool exited non-zero. Carries the tool name and a
    /// credential-safe context string.
    ToolFailed { tool: String, context: String },
    /// A filesystem operation failed.
    Io {
        context: String,
        source: std::io::Error,
    },
    /// A produced or supplied artifact failed its integrity check.
    IntegrityFailed { detail: String },
    /// The restore artifact path does not exist or has no recognizable layout.
    BadArtifact { detail: String },
    /// A destructive restore was refused because the active profile is
    /// production and `--force` was not supplied.
    ProductionRefused { profile: String },
    /// `--upload` (or an offsite restore) was requested but no
    /// `[backup.offsite]` section is configured.
    OffsiteNotConfigured,
    /// The `[backup.offsite]` section is present but invalid (e.g. no bucket).
    OffsiteConfig { detail: String },
    /// The offsite destination points at the same bucket+endpoint as the app's
    /// user-facing blob storage and `allow_shared_bucket` was not set (AC #3).
    /// Carries only the bucket name (not a credential).
    SharedBucketRefused { bucket: String },
    /// A credential could not be read: the config names an env var that is unset
    /// (or names no env var at all). Carries the VARIABLE NAME, never a value.
    MissingCredentialEnv { var: String },
    /// The local backup succeeded but the offsite upload/verify failed — the
    /// unambiguous split outcome (AC #2/#6). The local artifact is intact.
    OffsiteUploadFailed { local_path: String, detail: String },
    /// A non-split offsite operation (list / download / config load) failed.
    /// `detail` is credential-safe (S3 status/code, never secrets).
    Offsite { op: &'static str, detail: String },
    /// An offsite restore was requested (`--offsite` or an `offsite:` prefix) but
    /// the reference is malformed. Carries the bad reference (no secrets). The
    /// restore MUST fail rather than fall through to a local artifact.
    InvalidOffsiteRef { reference: String },
}

impl std::fmt::Display for BackupError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::NoUrl => write!(
                f,
                "No database URL found.\n  Set database.primary_url (or database.url) in autumn.toml, \
                 or set AUTUMN_DATABASE__PRIMARY_URL / AUTUMN_DATABASE__URL / DATABASE_URL."
            ),
            Self::UnknownShard { name, known } => {
                let detail = if known.is_empty() {
                    "No [[database.shards]] entries found in autumn.toml or environment.".to_owned()
                } else {
                    format!("Known shards: {}", known.join(", "))
                };
                write!(f, "Unknown shard {name:?}.\n  {detail}")
            }
            Self::ToolMissing { tool } => write!(
                f,
                "`{tool}` was not found.\n  Install the PostgreSQL client tools, set \
                 {PG_BIN_DIR_ENV} to their directory, or run against a managed-Postgres app \
                 (which bundles them)."
            ),
            Self::ToolFailed { tool, context } => {
                write!(f, "`{tool}` failed: {context}")
            }
            Self::Io { context, source } => write!(f, "{context}: {source}"),
            Self::IntegrityFailed { detail } => write!(
                f,
                "Backup integrity check failed: {detail}\n  The artifact was NOT reported as \
                 successful; any partial files were removed."
            ),
            Self::BadArtifact { detail } => write!(f, "Cannot restore: {detail}"),
            Self::ProductionRefused { profile } => write!(
                f,
                "Refusing to restore over the {profile:?} profile database.\n  \
                 Re-run with --force if you really mean it (this overwrites data)."
            ),
            Self::OffsiteNotConfigured => write!(
                f,
                "No offsite destination is configured.\n  Add a [backup.offsite] section \
                 (with [backup.offsite.s3] bucket/region/endpoint and *_env credential \
                 indirection) to autumn.toml, or set AUTUMN_BACKUP__OFFSITE__* env vars."
            ),
            Self::OffsiteConfig { detail } => {
                write!(f, "Invalid [backup.offsite] configuration: {detail}")
            }
            Self::SharedBucketRefused { bucket } => write!(
                f,
                "The offsite destination bucket {bucket:?} is the same as the app's \
                 [storage.s3] bucket at the same endpoint.\n  A shared bucket is a deliberate \
                 choice: set backup.offsite.allow_shared_bucket = true to opt in, or point the \
                 offsite backup at a distinct bucket."
            ),
            Self::MissingCredentialEnv { var } => write!(
                f,
                "Offsite credential environment variable {var:?} is not set (or the config \
                 names no *_env variable).\n  Set it to the S3 access key / secret, or fix \
                 backup.offsite.s3.access_key_id_env / secret_access_key_env."
            ),
            Self::OffsiteUploadFailed { local_path, detail } => write!(
                f,
                "Local backup OK at {local_path}\n  OFFSITE UPLOAD FAILED: {detail}\n  \
                 The local artifact is intact — re-run `autumn db backup --upload` after \
                 fixing the offsite destination."
            ),
            Self::Offsite { op, detail } => write!(f, "Offsite {op} failed: {detail}"),
            Self::InvalidOffsiteRef { reference } => write!(
                f,
                "Invalid offsite reference {reference:?}.\n  Expected \
                 offsite:<profile>/<timestamp|latest> (for example offsite:prod/latest)."
            ),
        }
    }
}

impl BackupError {
    fn io(context: impl Into<String>) -> impl FnOnce(std::io::Error) -> Self {
        let context = context.into();
        move |source| Self::Io { context, source }
    }
}

/// A single database captured by (or to be restored from) a backup run.
#[derive(Debug, Clone, PartialEq, Eq)]
struct ResolvedTarget {
    /// Stable label: `"control"` or `"shard:<name>"`.
    label: String,
    /// The resolved connection URL (never printed).
    url: String,
}

// ─── Entry points ───────────────────────────────────────────────────────────

/// Entry point for `autumn db backup`. Prints a credential-safe message and
/// exits non-zero on failure.
pub fn run_backup(args: &BackupArgs) {
    eprintln!("\u{1F342} autumn db backup\n");
    if let Err(e) = backup(args) {
        eprintln!("\u{2717} {e}");
        std::process::exit(1);
    }
}

/// Entry point for `autumn db restore`. Prints a credential-safe message and
/// exits non-zero on failure.
pub fn run_restore(args: &RestoreArgs) {
    eprintln!("\u{1F342} autumn db restore\n");
    if let Err(e) = restore(args) {
        eprintln!("\u{2717} {e}");
        std::process::exit(1);
    }
}

// ─── Backup ─────────────────────────────────────────────────────────────────

fn backup(args: &BackupArgs) -> Result<(), BackupError> {
    let targets = resolve_targets(args.profile.as_deref(), &args.target)?;
    // Include the managed cluster's bundled tools (daemon/cron path, where
    // AUTUMN_MANAGED_PG_DATA_DIR isn't inherited) so a managed backup needs zero
    // external tools on PATH (issue #1595).
    let tools = PgTools::locate_with_extra(managed_pg_data_dir());
    let pg_dump = tools.require("pg_dump")?;

    let profile = migrate::effective_profile(args.profile.as_deref());

    // Offsite pre-flight (issue #1619). Decide whether to upload with a LIGHT
    // probe that reads ONLY `[backup.offsite].auto_upload` (real env → `.env.<p>`
    // → merged TOML), WITHOUT a full `AutumnConfig` load — so a local-only backup
    // never triggers global config validation or encrypted-credentials
    // decryption (a cron job may not export AUTUMN_MASTER_KEY). Only when an
    // upload is actually requested do we do the full, strict resolve (bucket
    // required, creds env resolved, distinct-destination guard) and build the
    // client — BEFORE dumping — so a misconfigured offsite fails fast.
    let should_upload = args.upload || auto_upload_probe(&profile);
    let upload_ctx = if should_upload {
        let offsite = load_offsite(&profile)?
            .ok_or(BackupError::OffsiteNotConfigured)?
            .resolve()?;
        let client = offsite.build_client()?;
        Some((offsite, client))
    } else {
        None
    };

    let root = backup_root(args.dir.as_deref(), &profile);
    let run_dir = create_unique_run_dir(&root, &run_dir_name(&now_utc()))?;

    // Everything below writes into `run_dir`. On ANY failure we remove the
    // whole run directory so a partial/empty artifact is never left behind and
    // never counted toward retention (AC #1).
    let result = backup_into(&run_dir, &targets, args.format, &pg_dump, &tools, &profile);
    if let Err(e) = result {
        let _ = std::fs::remove_dir_all(&run_dir);
        return Err(e);
    }

    eprintln!(
        "\n\u{2713} Backup complete: {} ({} target(s), {} format).",
        run_dir.display(),
        targets.len(),
        args.format.pg_dump_format_flag(),
    );

    // Retention runs only AFTER a verified-successful backup, so a failed run
    // can never prune good history (AC #6).
    if let Some(keep) = args.keep {
        prune(&root, keep)?;
    }

    // Offsite upload runs LAST, only after local verification + prune. A failure
    // here is the split outcome (AC #2/#6): the local artifact stays intact and
    // the command exits non-zero with an unambiguous message.
    if let Some((offsite, client)) = upload_ctx {
        let local_run_id = run_dir
            .file_name()
            .map(|n| n.to_string_lossy().into_owned())
            .unwrap_or_default();
        // The REMOTE run id gains a globally-unique token (P2 #12) so concurrent
        // same-second backups of this profile from different hosts never collide
        // in the bucket; the local run directory keeps its #1595 `<timestamp>`.
        let remote_id = remote_run_id(&local_run_id, &remote_run_token());
        // Canonicalize the profile for the REMOTE key prefix (P2 #22) so upload,
        // `db offsite list`, and restore all address the same `<canonical>/…`
        // location regardless of alias/case spelling — while the LOCAL run dir
        // keeps its raw #1595 `<profile>/<timestamp>` layout (used above).
        let remote_profile = migrate::canonical_profile(&profile);
        if let Err(detail) = upload_run(&offsite, &client, &run_dir, &remote_profile, &remote_id) {
            let local_path = run_dir.display().to_string();
            // #1743: raise an operator alert so an unattended/cron backup with
            // [alerts] configured never fails its upload silently. Best-effort —
            // never changes the exit code; the `?`-equivalent return below still
            // exits non-zero with the same split-outcome message as before.
            emit_offsite_upload_failure_alert(&local_path, &detail);
            return Err(BackupError::OffsiteUploadFailed { local_path, detail });
        }
    }
    Ok(())
}

/// Best-effort operator alert for a failed offsite upload (#1743).
///
/// Discriminator (chosen design): fire on ANY upload failure when `[alerts]`
/// channels are configured. Interactive users with no `[alerts]` destination
/// build zero channels, so this is a no-op for them — the interactive case
/// (message + non-zero exit) is unchanged. Loading the config here is
/// acceptable: we are already on the failure path and about to exit non-zero.
fn emit_offsite_upload_failure_alert(local_path: &str, detail: &str) {
    let config = match autumn_web::config::AutumnConfig::load() {
        Ok(c) => c,
        Err(e) => {
            eprintln!("  \u{26A0} offsite-upload-failed alert skipped: could not load config: {e}");
            return;
        }
    };
    let client = autumn_web::http::Client::from_config(&config.http.client);
    let channels = crate::alert::configured_http_channels(&config.alerts, &client);
    deliver_offsite_upload_alert(&channels, local_path, detail);
}

/// Build the `ScheduledTaskFailure` alert raised when an offsite upload fails.
/// Pure (no I/O) so it can be asserted directly in tests.
fn build_offsite_upload_alert(local_path: &str, detail: &str) -> Alert {
    Alert::trigger(
        AlertCondition::ScheduledTaskFailure,
        "scheduled_task_failure:db-backup-offsite-upload",
    )
    .title("Offsite backup upload failed")
    .summary(detail)
    .detail("local_path", local_path)
    .detail("error", detail)
    .build()
}

/// Deliver the offsite-upload-failure alert through every configured channel on
/// a short-lived current-thread runtime (mirrors `autumn alert test`).
/// Best-effort: a per-channel delivery error is logged but never propagated, so
/// alerting can NEVER change the command's exit code. A no-op when `channels`
/// is empty. Takes `&[Arc<dyn AlertChannel>]` so a test can inject a capturing
/// mock channel and assert the alert that a failed upload raises.
fn deliver_offsite_upload_alert(
    channels: &[Arc<dyn AlertChannel>],
    local_path: &str,
    detail: &str,
) {
    if channels.is_empty() {
        return;
    }
    let alert = build_offsite_upload_alert(local_path, detail);
    let runtime = match tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
    {
        Ok(rt) => rt,
        Err(e) => {
            eprintln!(
                "  \u{26A0} offsite-upload-failed alert not sent: could not start runtime: {e}"
            );
            return;
        }
    };
    runtime.block_on(async {
        for channel in channels {
            if let Err(error) = channel.deliver(&alert).await {
                eprintln!(
                    "  \u{26A0} offsite-upload-failed alert not delivered via {}: {error}",
                    channel.name()
                );
            }
        }
    });
}

/// Dump every target into `run_dir`, verify each artifact, and write the
/// manifest. Returns `Err` (leaving cleanup to the caller) on the first failure.
fn backup_into(
    run_dir: &Path,
    targets: &[ResolvedTarget],
    format: BackupFormat,
    pg_dump: &Path,
    tools: &PgTools,
    profile: &str,
) -> Result<(), BackupError> {
    let mut manifest_targets = Vec::with_capacity(targets.len());
    for target in targets {
        let file_name = artifact_file_name(&target.label, format);
        let out_path = run_dir.join(&file_name);
        eprintln!(
            "\u{2500}\u{2500} backing up {} \u{2500}\u{2500}",
            target.label
        );

        let db = parsed_db_name(&target.url);
        run_pg_dump(pg_dump, &target.url, &out_path, format, &db)?;
        verify_artifact(&out_path, format, &db, tools)?;
        eprintln!("  \u{2713} {file_name} verified.");

        manifest_targets.push(ManifestTarget {
            label: target.label.clone(),
            file: file_name,
            database: db,
        });
    }

    let manifest = Manifest {
        autumn_version: env!("CARGO_PKG_VERSION").to_owned(),
        created_at: now_utc().to_rfc3339(),
        profile: profile.to_owned(),
        format: format.pg_dump_format_flag().to_owned(),
        targets: manifest_targets,
    };
    write_manifest(run_dir, &manifest)
}

/// Build a `Command` for a pg client tool with the connection password moved
/// out of argv and into `PGPASSWORD`. The full URL (with password) is never
/// passed as a command-line argument, so it can't leak via `ps` /
/// `/proc/<pid>/cmdline` to other local users — matching this module's
/// credential-safety stance. Returns the command plus the password-free
/// `--dbname` value the caller should pass. libpq reads `PGPASSWORD` when the
/// connection string omits the password.
fn pg_command(program: &Path, url: &str) -> (Command, String) {
    let (safe_url, password) = split_password(url);
    let mut cmd = Command::new(program);
    if let Some(pw) = password {
        cmd.env("PGPASSWORD", pw);
    }
    (cmd, safe_url)
}

/// Split a connection string into a `(password-free connstring, password)`
/// pair. Two libpq connection-string forms are handled — the URL form
/// (`postgres://user:pw@host/db`) and the keyword/value form
/// (`host=db user=app password=secret dbname=app`), both of which Autumn's
/// config validation accepts. In either form the password is moved out of the
/// string so the caller can hand it to `PGPASSWORD` instead of leaving it on the
/// argv (visible via `ps` / `/proc/<pid>/cmdline`). A string that is neither a
/// parseable URL nor recognizable keyword/value form — or that simply carries no
/// password — is returned unchanged with `None`, so behavior degrades to the
/// previous "connstring on the command line" path rather than failing.
///
/// For the URL form the returned password is **percent-decoded**:
/// `Url::password()` yields the raw (still percent-encoded) userinfo, but
/// `PGPASSWORD` is consumed by libpq as a literal string — it is *not*
/// percent-decoded. A URL like `postgres://u:p%40ss@h/db` must set
/// `PGPASSWORD=p@ss`, not `p%40ss`, or authentication fails. The username is
/// intentionally left untouched: it stays in the password-free `--dbname` URL,
/// where libpq decodes it as part of URI parsing, so double-decoding it (or
/// moving it to an env var) would be wrong.
fn split_password(url: &str) -> (String, Option<String>) {
    if let Ok(mut parsed) = url::Url::parse(url) {
        let password = parsed.password().map(|pw| {
            percent_encoding::percent_decode_str(pw)
                .decode_utf8_lossy()
                .into_owned()
        });
        if password.is_some() {
            // Clearing the password keeps the username, host, port, db, and
            // any query parameters intact.
            let _ = parsed.set_password(None);
        }
        return (parsed.to_string(), password);
    }
    // Not a URL: it may be libpq keyword/value form, where the password would
    // otherwise ride along on argv.
    split_password_kv(url)
}

/// Move any `password=...` out of a libpq keyword/value connection string,
/// returning `(stripped connstring, password)`. When the input isn't
/// recognizable keyword/value form, or carries no password, the ORIGINAL string
/// is returned untouched with `None` so a password-less connstring is passed
/// through unchanged.
fn split_password_kv(conn: &str) -> (String, Option<String>) {
    let Some(pairs) = parse_libpq_kv(conn) else {
        return (conn.to_owned(), None);
    };
    let mut password = None;
    let mut kept = Vec::with_capacity(pairs.len());
    for (key, value) in pairs {
        if key == "password" {
            // Last one wins, matching libpq (later keywords override earlier).
            password = Some(value);
        } else {
            kept.push((key, value));
        }
    }
    if password.is_none() {
        return (conn.to_owned(), None);
    }
    let rebuilt = kept
        .into_iter()
        .map(|(k, v)| format!("{k}={}", quote_libpq_value(&v)))
        .collect::<Vec<_>>()
        .join(" ");
    (rebuilt, password)
}

/// Parse a libpq keyword/value connection string into `(keyword, value)` pairs.
/// Applies libpq's unquoting rules: whitespace separates pairs and may surround
/// `=`; a value may be single-quoted to contain spaces; and a backslash escapes
/// the next character (so `\'` and `\\` yield a literal quote and backslash).
/// Returns `None` when the string doesn't parse as keyword/value form (no
/// `keyword=` token, a missing `=`, or an unterminated quote), so callers fall
/// back to treating it opaquely.
fn parse_libpq_kv(conn: &str) -> Option<Vec<(String, String)>> {
    let chars: Vec<char> = conn.chars().collect();
    let len = chars.len();
    let mut i = 0;
    let mut pairs = Vec::new();
    loop {
        while i < len && chars[i].is_whitespace() {
            i += 1;
        }
        if i == len {
            break;
        }
        // Keyword: runs up to the next whitespace or `=` (keywords aren't quoted).
        let start = i;
        while i < len && !chars[i].is_whitespace() && chars[i] != '=' {
            i += 1;
        }
        if i == start {
            return None;
        }
        let keyword: String = chars[start..i].iter().collect();
        while i < len && chars[i].is_whitespace() {
            i += 1;
        }
        if i == len || chars[i] != '=' {
            return None;
        }
        i += 1; // consume '='
        while i < len && chars[i].is_whitespace() {
            i += 1;
        }
        // Value: single-quoted (may contain spaces) or bare (ends at whitespace).
        let mut value = String::new();
        if i < len && chars[i] == '\'' {
            i += 1;
            loop {
                if i == len {
                    return None; // unterminated quote
                }
                match chars[i] {
                    '\\' => {
                        i += 1;
                        if i < len {
                            value.push(chars[i]);
                            i += 1;
                        }
                    }
                    '\'' => {
                        i += 1;
                        break;
                    }
                    c => {
                        value.push(c);
                        i += 1;
                    }
                }
            }
        } else {
            while i < len && !chars[i].is_whitespace() {
                if chars[i] == '\\' {
                    i += 1;
                    if i < len {
                        value.push(chars[i]);
                        i += 1;
                    }
                } else {
                    value.push(chars[i]);
                    i += 1;
                }
            }
        }
        pairs.push((keyword, value));
    }
    (!pairs.is_empty()).then_some(pairs)
}

/// Re-serialize a libpq value, single-quoting and backslash-escaping it when it
/// is empty or contains whitespace, a quote, or a backslash, so the rebuilt
/// connection string round-trips back through [`parse_libpq_kv`] (and libpq).
fn quote_libpq_value(value: &str) -> String {
    let needs_quoting = value.is_empty()
        || value
            .chars()
            .any(|c| c.is_whitespace() || c == '\'' || c == '\\');
    if !needs_quoting {
        return value.to_owned();
    }
    let mut out = String::with_capacity(value.len() + 2);
    out.push('\'');
    for c in value.chars() {
        if c == '\'' || c == '\\' {
            out.push('\\');
        }
        out.push(c);
    }
    out.push('\'');
    out
}

/// Build the `pg_dump` argument vector for one target. Pure over its inputs so
/// the flag composition (notably the format-conditional `--clean --if-exists`)
/// is unit-testable without spawning `pg_dump`.
///
/// `--no-owner`/`--no-privileges` keep the artifact portable across roles so a
/// restore into a freshly-created database works without recreating the
/// original owner/grants.
///
/// For the **plain** format we additionally emit `--clean --if-exists`, so the
/// SQL text carries `DROP ... IF EXISTS` before each `CREATE`. A plain restore
/// just pipes the dump through `psql` under `ON_ERROR_STOP=1`; without the drops
/// it aborts with `... already exists` when restoring over a database that still
/// holds the schema (a post-data-loss or prod-overwrite drill). Baking the drops
/// into the dump makes the restore idempotent and safe on both empty and
/// populated databases. The **custom** format deliberately omits `--clean`:
/// `pg_restore --clean --if-exists` performs the drops at restore time instead.
fn pg_dump_args(format: BackupFormat, out_path: &Path, dbname: &str) -> Vec<std::ffi::OsString> {
    use std::ffi::OsString;

    let mut args: Vec<OsString> = vec![
        "--format".into(),
        format.pg_dump_format_flag().into(),
        "--no-owner".into(),
        "--no-privileges".into(),
    ];
    if matches!(format, BackupFormat::Plain) {
        args.push("--clean".into());
        args.push("--if-exists".into());
    }
    args.push("--file".into());
    args.push(out_path.as_os_str().to_owned());
    args.push("--dbname".into());
    args.push(dbname.into());
    args
}

/// Shell out to `pg_dump` for one target.
fn run_pg_dump(
    pg_dump: &Path,
    url: &str,
    out_path: &Path,
    format: BackupFormat,
    db: &str,
) -> Result<(), BackupError> {
    let (mut cmd, safe_url) = pg_command(pg_dump, url);
    let status = cmd
        .args(pg_dump_args(format, out_path, &safe_url))
        .status()
        .map_err(|e| match e.kind() {
            std::io::ErrorKind::NotFound => BackupError::ToolMissing {
                tool: "pg_dump".to_owned(),
            },
            _ => BackupError::ToolFailed {
                tool: "pg_dump".to_owned(),
                context: format!("could not spawn for database {db:?}: {e}"),
            },
        })?;
    if !status.success() {
        return Err(BackupError::ToolFailed {
            tool: "pg_dump".to_owned(),
            context: format!(
                "dump of database {db:?} exited {}",
                exit_desc(status.code())
            ),
        });
    }
    Ok(())
}

/// Verify a freshly-written artifact before it is counted as a success (AC #1).
///
/// * Custom: the file must be non-empty AND `pg_restore --list` must succeed
///   with a non-empty archive table of contents.
/// * Plain: the file must be non-empty AND end with `pg_dump`'s completion marker.
fn verify_artifact(
    path: &Path,
    format: BackupFormat,
    db: &str,
    tools: &PgTools,
) -> Result<(), BackupError> {
    let len = std::fs::metadata(path)
        .map_err(BackupError::io(format!("stat {}", path.display())))?
        .len();
    if len == 0 {
        return Err(BackupError::IntegrityFailed {
            detail: format!("dump of {db:?} is empty (0 bytes)"),
        });
    }
    match format {
        BackupFormat::Custom => {
            // Resolve `pg_restore` through the SAME locator the dump/restore step
            // used (built via `locate_with_extra` with the runtime-discovered
            // managed data dir). Re-locating with a bare `PgTools::locate()` here
            // would miss the daemon/cron path's bundled binary and wrongly report
            // it missing (issue #1595).
            let pg_restore = tools.require("pg_restore")?;
            verify_custom_archive(&pg_restore, path, db)
        }
        BackupFormat::Plain => verify_plain_dump(path, db),
    }
}

/// Structural integrity check for a custom-format archive: `pg_restore --list`
/// parses the archive's TOC and fails on a truncated/corrupt file.
fn verify_custom_archive(pg_restore: &Path, path: &Path, db: &str) -> Result<(), BackupError> {
    let output = Command::new(pg_restore)
        .arg("--list")
        .arg(path)
        .output()
        .map_err(|e| match e.kind() {
            std::io::ErrorKind::NotFound => BackupError::ToolMissing {
                tool: "pg_restore".to_owned(),
            },
            _ => BackupError::ToolFailed {
                tool: "pg_restore".to_owned(),
                context: format!("could not spawn to verify {db:?}: {e}"),
            },
        })?;
    if !output.status.success() {
        return Err(BackupError::IntegrityFailed {
            detail: format!(
                "`pg_restore --list` rejected the {db:?} archive ({})",
                exit_desc(output.status.code())
            ),
        });
    }
    // A valid-but-empty TOC (no dumpable objects) still lists header comment
    // lines; require at least one non-comment entry so a truly empty archive is
    // caught.
    let listing = String::from_utf8_lossy(&output.stdout);
    if toc_entry_count(&listing) == 0 {
        return Err(BackupError::IntegrityFailed {
            detail: format!("the {db:?} archive contains no objects"),
        });
    }
    Ok(())
}

/// Integrity check for a plain SQL dump: it must end with `pg_dump`'s completion
/// marker, which is only written once the dump finished cleanly.
fn verify_plain_dump(path: &Path, db: &str) -> Result<(), BackupError> {
    use std::io::{Read, Seek, SeekFrom};

    let mut file =
        std::fs::File::open(path).map_err(BackupError::io(format!("open {}", path.display())))?;
    let len = file
        .metadata()
        .map_err(BackupError::io(format!("stat {}", path.display())))?
        .len();

    // A 0-byte dump can't contain the completion marker: it's truncated/incomplete.
    if len == 0 {
        return Err(BackupError::IntegrityFailed {
            detail: format!("the {db:?} SQL dump is empty (likely truncated)"),
        });
    }

    // The completion marker is on the final line, so only the dump's tail needs to
    // be read — avoid loading a potentially multi-GB dump into memory. Read the
    // last ~1 KiB (clamped to the file size for tiny files).
    let read_len = len.min(1024);
    let offset = -i64::try_from(read_len).expect("read_len is at most 1024");
    file.seek(SeekFrom::End(offset))
        .map_err(BackupError::io(format!("seek {}", path.display())))?;
    let mut tail = Vec::with_capacity(read_len as usize);
    file.take(read_len)
        .read_to_end(&mut tail)
        .map_err(BackupError::io(format!("read {}", path.display())))?;
    let tail = String::from_utf8_lossy(&tail);

    if plain_dump_is_complete(&tail) {
        Ok(())
    } else {
        Err(BackupError::IntegrityFailed {
            detail: format!(
                "the {db:?} SQL dump is missing pg_dump's completion marker (likely truncated)"
            ),
        })
    }
}

// ─── Restore ────────────────────────────────────────────────────────────────

fn restore(args: &RestoreArgs) -> Result<(), BackupError> {
    // Production guard — identical to `autumn db drop` (AC #4). This guards the
    // RESTORE-TARGET profile (`--profile`), independent of any offsite source.
    let profile = migrate::effective_profile(args.profile.as_deref());
    super::guard_destructive(&profile, args.force).map_err(|_| BackupError::ProductionRefused {
        profile: profile.clone(),
    })?;

    // Offsite restore (issue #1619): when `--offsite` is set or the artifact is
    // an `offsite:<profile>/<ts|latest>` reference, download the run to a fresh
    // temp dir and hand it to the SAME RestorePlan::discover path so the
    // identical integrity-refusal + guard/`--force` protocol applies unchanged.
    let artifact_str = args.artifact.to_string_lossy();
    // When offsite is indicated (flag or `offsite:` prefix) we commit to the
    // offsite path: a malformed reference is an error, never a silent fall
    // through to a same-named local artifact. `None` means offsite was not
    // indicated at all, so a normal local restore proceeds.
    if let Some(oref) = resolve_offsite_ref(args.offsite, &artifact_str) {
        return restore_from_offsite(args, &oref?);
    }

    let plan = RestorePlan::discover(&args.artifact, args.shard.as_deref())?;
    apply_restore_plan(&plan, args)
}

/// Verify every selected artifact, then restore each into its resolved database.
/// Shared by local and offsite restore so both go through the identical
/// verify-all-before-mutate-any protocol (AC #4).
fn apply_restore_plan(plan: &RestorePlan, args: &RestoreArgs) -> Result<(), BackupError> {
    let format = plan.format;
    let entries = plan.select(args.shard.as_deref())?;

    let targets = resolve_targets_for_restore(args.profile.as_deref(), &entries)?;
    // Same bundled-tool discovery as backup: a managed restore must also work
    // with zero external tools on PATH (issue #1595).
    let tools = PgTools::locate_with_extra(managed_pg_data_dir());

    // Verify EVERY artifact before mutating ANY database (AC #4): refuse to
    // start a destructive restore we can't finish.
    for (entry, _url) in &targets {
        let db = format!("(artifact) {}", entry.label);
        verify_artifact(&plan.dir.join(&entry.file), format, &db, &tools)?;
        eprintln!("  \u{2713} {} integrity verified.", entry.file);
    }

    for (entry, url) in &targets {
        eprintln!(
            "\u{2500}\u{2500} restoring {} \u{2500}\u{2500}",
            entry.label
        );
        let artifact = plan.dir.join(&entry.file);
        let db = parsed_db_name(url);
        run_restore_one(&tools, url, &artifact, format, &db)?;
        eprintln!("  \u{2713} restored {}.", entry.label);
    }

    eprintln!("\n\u{2713} Restore complete ({} target(s)).", targets.len());
    Ok(())
}

/// Restore one artifact into one database.
fn run_restore_one(
    tools: &PgTools,
    url: &str,
    artifact: &Path,
    format: BackupFormat,
    db: &str,
) -> Result<(), BackupError> {
    match format {
        BackupFormat::Custom => {
            let pg_restore = tools.require("pg_restore")?;
            // `--clean --if-exists` drops existing objects first so the restore
            // is an overwrite, not a merge; `--no-owner` matches the dump flags.
            let (mut cmd, safe_url) = pg_command(&pg_restore, url);
            let status = cmd
                .arg("--clean")
                .arg("--if-exists")
                .arg("--no-owner")
                .arg("--dbname")
                .arg(&safe_url)
                .arg(artifact)
                .status()
                .map_err(|e| spawn_err("pg_restore", db, &e))?;
            if !status.success() {
                return Err(BackupError::ToolFailed {
                    tool: "pg_restore".to_owned(),
                    context: format!(
                        "restore into database {db:?} exited {}",
                        exit_desc(status.code())
                    ),
                });
            }
            Ok(())
        }
        BackupFormat::Plain => {
            let psql = tools.require("psql")?;
            let (mut cmd, safe_url) = pg_command(&psql, url);
            let status = cmd
                .arg("--set")
                .arg("ON_ERROR_STOP=1")
                .arg("--dbname")
                .arg(&safe_url)
                .arg("--file")
                .arg(artifact)
                .status()
                .map_err(|e| spawn_err("psql", db, &e))?;
            if !status.success() {
                return Err(BackupError::ToolFailed {
                    tool: "psql".to_owned(),
                    context: format!(
                        "restore into database {db:?} exited {}",
                        exit_desc(status.code())
                    ),
                });
            }
            Ok(())
        }
    }
}

fn spawn_err(tool: &str, db: &str, e: &std::io::Error) -> BackupError {
    match e.kind() {
        std::io::ErrorKind::NotFound => BackupError::ToolMissing {
            tool: tool.to_owned(),
        },
        _ => BackupError::ToolFailed {
            tool: tool.to_owned(),
            context: format!("could not spawn for database {db:?}: {e}"),
        },
    }
}

/// A restore artifact resolved to a directory + its manifest entries.
struct RestorePlan {
    /// Directory holding the artifact file(s).
    dir: PathBuf,
    /// Format of the artifacts.
    format: BackupFormat,
    /// The manifest targets to restore.
    entries: Vec<ManifestTarget>,
}

impl RestorePlan {
    /// Discover a restore plan from a user-supplied path. Accepts either a run
    /// directory (containing `manifest.json`) or a single artifact file.
    ///
    /// For a bare single-file artifact there is no manifest to say which target
    /// it belongs to, so the target is resolved from an explicit `--shard`
    /// (`shard`) or, failing that, inferred from the backup writer's filename
    /// convention (see [`single_file_target_label`]). This prevents a
    /// `shard-<name>.dump` from being silently restored into the control
    /// database.
    fn discover(path: &Path, shard: Option<&str>) -> Result<Self, BackupError> {
        if path.is_dir() {
            let manifest = read_manifest(path)?;
            let format = BackupFormat::parse(&manifest.format).map_err(|detail| {
                BackupError::BadArtifact {
                    detail: format!("manifest has an invalid format: {detail}"),
                }
            })?;
            return Ok(Self {
                dir: path.to_path_buf(),
                format,
                entries: manifest.targets,
            });
        }
        if path.is_file() {
            // A bare artifact file: infer format from extension, and resolve the
            // restore target from `--shard` or the filename convention (never
            // blindly `control`, which would corrupt the control DB with shard
            // data).
            let format = infer_format_from_path(path).ok_or_else(|| BackupError::BadArtifact {
                detail: format!(
                    "{} is neither a run directory nor a .dump/.sql artifact",
                    path.display()
                ),
            })?;
            let dir = path
                .parent()
                .map_or_else(|| PathBuf::from("."), Path::to_path_buf);
            let file = path
                .file_name()
                .map(|n| n.to_string_lossy().into_owned())
                .unwrap_or_default();
            let label = single_file_target_label(&file, shard)?;
            return Ok(Self {
                dir,
                format,
                entries: vec![ManifestTarget {
                    label,
                    file,
                    database: String::new(),
                }],
            });
        }
        Err(BackupError::BadArtifact {
            detail: format!("{} does not exist", path.display()),
        })
    }

    /// Filter the plan's entries to a single shard when `--shard` is given.
    fn select(&self, shard: Option<&str>) -> Result<Vec<ManifestTarget>, BackupError> {
        let Some(shard) = shard else {
            return Ok(self.entries.clone());
        };
        let wanted = format!("shard:{shard}");
        let selected: Vec<ManifestTarget> = self
            .entries
            .iter()
            .filter(|t| t.label == wanted)
            .cloned()
            .collect();
        if selected.is_empty() {
            let known: Vec<String> = self.entries.iter().map(|t| t.label.clone()).collect();
            return Err(BackupError::UnknownShard {
                name: shard.to_owned(),
                known,
            });
        }
        Ok(selected)
    }
}

// ─── Target resolution (reuses the migrate URL-resolution path) ─────────────

/// The project-root `.env`-overlaid process environment, mirroring how
/// `autumn migrate` resolves its target URLs (issue #1684). A real env var
/// still wins over `.env`; a malformed `.env` fails loudly (with a `path:line`
/// location) rather than being silently ignored.
fn dotenv_env() -> autumn_web::dotenv::DotenvOsEnv {
    match autumn_web::dotenv::os_env_with_dotenv() {
        Ok(env) => env,
        Err(e) => {
            eprintln!("  \u{274C} .env: {e}");
            std::process::exit(1);
        }
    }
}

/// Like [`dotenv_env`] but selects `.env.<profile>` for an explicit target
/// profile (issue #1619). The offsite path resolves config + credentials under
/// the run's profile (`--profile <p>` / `offsite:<p>/…`), so a profile-specific
/// `.env.<p>` is honored even when the ambient shell profile differs — otherwise
/// `autumn db backup --profile test --upload` from a dev/unset shell would miss
/// `.env.test`-provided offsite settings and credentials.
fn dotenv_env_for_profile(profile: &str) -> autumn_web::dotenv::DotenvOsEnv {
    // Normalize aliases/case to the app's resolved profile FIRST (P2 #20), so
    // `--profile development` / `AUTUMN_ENV=PROD` selects `.env.dev` / `.env.prod`
    // — the same file the running app reads — instead of a literal `.env.development`.
    let profile = migrate::canonical_profile(profile);
    match autumn_web::dotenv::os_env_with_dotenv_for_profile(&profile) {
        Ok(env) => env,
        Err(e) => {
            eprintln!("  \u{274C} .env: {e}");
            std::process::exit(1);
        }
    }
}

/// The managed-Postgres published cluster URL, used as a *fallback* control URL
/// for bundled/daemon/single-binary apps (issue #1595). Such deployments often
/// have no `DATABASE_URL` in env or `autumn.toml`; the running cluster instead
/// publishes its URL for one-off CLI commands. This reuses the SAME blessed
/// helper `autumn task`/`autumn build` use to attach to the serve daemon's
/// cluster ([`crate::serve::managed_pg_env`]), which only yields a URL when a
/// live, reachable cluster is published — so a backup can't attach to a dead or
/// foreign endpoint. `None` when no managed cluster is available.
fn managed_pg_fallback_url() -> Option<String> {
    crate::serve::managed_pg_env(None).and_then(|env| env.attach_url)
}

/// The managed-Postgres cluster's data dir, discovered via the SAME blessed
/// helper as [`managed_pg_fallback_url`]. Used to locate the daemon's bundled
/// `pg_dump`/`pg_restore`/`psql`: in the managed-daemon/cron path the backup
/// process is NOT launched with `AUTUMN_MANAGED_PG_DATA_DIR` (only `autumn serve`
/// sets it for its own children), so the env-var-driven tool locator can't find
/// the extracted bundle. Feeding this data dir to the locator as a
/// runtime-discovered candidate lets a managed backup run with zero external
/// tools on PATH (issue #1595, AC #2). `None` when no managed cluster resolves.
fn managed_pg_data_dir() -> Option<PathBuf> {
    crate::serve::managed_pg_env(None).map(|env| env.data_dir)
}

/// Apply the managed-pg published URL as a fallback for the control URL. Pure
/// over its inputs (the fallback is a closure, evaluated lazily) so the
/// precedence — explicit config/env first, managed-pg published URL second — is
/// unit-testable without a live cluster.
fn resolve_control_url<Ff>(resolved: Option<String>, fallback: Ff) -> Option<String>
where
    Ff: FnOnce() -> Option<String>,
{
    resolved.or_else(fallback)
}

/// Resolve the databases a backup run should capture, reusing the SAME
/// resolution `autumn migrate` uses so the set matches the running app exactly.
fn resolve_targets(
    profile: Option<&str>,
    selector: &TargetSelector,
) -> Result<Vec<ResolvedTarget>, BackupError> {
    use autumn_web::config::Env as _;

    // Resolve control AND shards through the SAME `.env`-overlaid environment
    // that `autumn migrate` uses (issue #1684), so a `.env`-provided shard
    // override is honored identically. A real env var still wins over `.env`.
    let table =
        migrate::read_autumn_toml_table_with_profile(Some(&migrate::effective_profile(profile)));
    let env = dotenv_env();
    // Explicit config/env wins; fall back to the managed-pg published URL for
    // bundled/daemon apps that don't export a DATABASE_URL (issue #1595).
    let control = resolve_control_url(
        migrate::resolve_primary_database_url_from_sources(|k| env.var(k), table.as_ref()),
        managed_pg_fallback_url,
    );
    let shards = migrate::resolve_shard_database_urls_from_sources(|k| env.var(k), table.as_ref());
    build_targets(control, shards, selector)
}

/// Pure target-selection logic, separated for unit testing. Mirrors
/// `migrate::build_targets`'s control-first / shards-in-order shape.
fn build_targets(
    control: Option<String>,
    shards: Vec<(String, String)>,
    selector: &TargetSelector,
) -> Result<Vec<ResolvedTarget>, BackupError> {
    match selector {
        TargetSelector::ControlOnly => control
            .map(|url| {
                vec![ResolvedTarget {
                    label: "control".to_owned(),
                    url,
                }]
            })
            .ok_or(BackupError::NoUrl),
        TargetSelector::Shard(name) => {
            let Some((_, url)) = shards.iter().find(|(shard, _)| shard == name) else {
                return Err(BackupError::UnknownShard {
                    name: name.clone(),
                    known: shards.into_iter().map(|(n, _)| n).collect(),
                });
            };
            Ok(vec![ResolvedTarget {
                label: format!("shard:{name}"),
                url: url.clone(),
            }])
        }
        TargetSelector::All => {
            let mut targets = Vec::new();
            if let Some(control_url) = control {
                targets.push(ResolvedTarget {
                    label: "control".to_owned(),
                    url: control_url,
                });
            } else if shards.is_empty() {
                return Err(BackupError::NoUrl);
            }
            for (name, url) in shards {
                targets.push(ResolvedTarget {
                    label: format!("shard:{name}"),
                    url,
                });
            }
            Ok(targets)
        }
    }
}

/// Resolve a live URL for each manifest entry a restore will write to, again
/// via the migrate resolution path so restore hits the same databases.
fn resolve_targets_for_restore(
    profile: Option<&str>,
    entries: &[ManifestTarget],
) -> Result<Vec<(ManifestTarget, String)>, BackupError> {
    use autumn_web::config::Env as _;

    // Same `.env`-overlaid resolution as `resolve_targets` / `autumn migrate`
    // so restore writes to the exact databases a backup would have captured.
    let table =
        migrate::read_autumn_toml_table_with_profile(Some(&migrate::effective_profile(profile)));
    let env = dotenv_env();
    // Same precedence as `resolve_targets`: explicit config/env first, then the
    // managed-pg published URL so a restore into a bundled/daemon app resolves a
    // live control URL without a manually exported DATABASE_URL (issue #1595).
    let control = resolve_control_url(
        migrate::resolve_primary_database_url_from_sources(|k| env.var(k), table.as_ref()),
        managed_pg_fallback_url,
    );
    let shards = migrate::resolve_shard_database_urls_from_sources(|k| env.var(k), table.as_ref());

    let mut out = Vec::with_capacity(entries.len());
    for entry in entries {
        let url = if entry.label == "control" {
            control.clone().ok_or(BackupError::NoUrl)?
        } else if let Some(shard_name) = entry.label.strip_prefix("shard:") {
            shards
                .iter()
                .find(|(n, _)| n == shard_name)
                .map(|(_, u)| u.clone())
                .ok_or_else(|| BackupError::UnknownShard {
                    name: shard_name.to_owned(),
                    known: shards.iter().map(|(n, _)| n.clone()).collect(),
                })?
        } else {
            return Err(BackupError::BadArtifact {
                detail: format!(
                    "manifest target has an unrecognized label {:?}",
                    entry.label
                ),
            });
        };
        out.push((entry.clone(), url));
    }
    Ok(out)
}

// ─── Retention ──────────────────────────────────────────────────────────────

/// Prune all but the newest `keep` run directories under `root`.
fn prune(root: &Path, keep: usize) -> Result<(), BackupError> {
    let mut runs = list_run_dirs(root)?;
    runs.sort(); // ascending by timestamped name
    let to_remove = plan_pruning(&runs, keep);
    for name in &to_remove {
        let path = root.join(name);
        std::fs::remove_dir_all(&path)
            .map_err(BackupError::io(format!("pruning {}", path.display())))?;
        eprintln!("  \u{1F5D1} pruned old backup {name}");
    }
    if !to_remove.is_empty() {
        eprintln!(
            "  \u{2139} retention: kept newest {keep}, pruned {}.",
            to_remove.len()
        );
    }
    Ok(())
}

/// List backup run-directory names directly under `root`. Only directories that
/// actually contain a `manifest.json` are counted, so retention pruning can
/// never delete an unrelated directory a user may have placed alongside the
/// backups (or a partially-written run — those are cleaned up before this runs).
/// Missing root => empty.
fn list_run_dirs(root: &Path) -> Result<Vec<String>, BackupError> {
    if !root.exists() {
        return Ok(Vec::new());
    }
    let mut names = Vec::new();
    for entry in
        std::fs::read_dir(root).map_err(BackupError::io(format!("read {}", root.display())))?
    {
        let entry = entry.map_err(BackupError::io(format!("read entry in {}", root.display())))?;
        let path = entry.path();
        if path.is_dir() && path.join(MANIFEST_FILE).is_file() {
            names.push(entry.file_name().to_string_lossy().into_owned());
        }
    }
    Ok(names)
}

/// Pure retention rule: given run names sorted ascending (oldest first), return
/// the names to remove so that only the newest `keep` remain. `keep == 0` is
/// treated as `keep == 1` (never prune everything from a just-run backup).
fn plan_pruning(sorted_runs: &[String], keep: usize) -> Vec<String> {
    let keep = keep.max(1);
    if sorted_runs.len() <= keep {
        return Vec::new();
    }
    let remove_count = sorted_runs.len() - keep;
    sorted_runs[..remove_count].to_vec()
}

// ─── pg tool location ───────────────────────────────────────────────────────

/// Resolves Postgres client executables (`pg_dump`, `pg_restore`, `psql`)
/// from an ordered list of candidate `bin` directories, falling back to the
/// bare command name (PATH lookup).
pub struct PgTools {
    /// Candidate `bin` directories, highest priority first.
    dirs: Vec<PathBuf>,
}

impl PgTools {
    /// Build the locator from the environment: `AUTUMN_PG_BIN_DIR`, then the
    /// managed-Postgres bundle's `bin` dir, then PATH.
    pub fn locate() -> Self {
        Self::locate_with_extra(None)
    }

    /// Like [`Self::locate`] but also considers a runtime-discovered managed
    /// Postgres data dir (e.g. from [`managed_pg_data_dir`]). The daemon/cron
    /// backup path doesn't inherit `AUTUMN_MANAGED_PG_DATA_DIR`, so without this
    /// the bundled client tools sitting next to the live cluster's data dir would
    /// be invisible and `require` would wrongly report them missing (issue #1595).
    /// The extra dir's derived `bin/` directories are appended after the
    /// env-driven candidates, so an explicit `AUTUMN_PG_BIN_DIR`/env bundle still
    /// wins.
    pub fn locate_with_extra(managed_data_dir: Option<PathBuf>) -> Self {
        let mut dirs = candidate_bin_dirs(
            std::env::var_os(PG_BIN_DIR_ENV).map(PathBuf::from),
            std::env::var_os(MANAGED_PG_DATA_DIR_ENV).map(PathBuf::from),
        );
        // Reuse the exact bundle-layout derivation for the runtime-discovered dir.
        dirs.extend(candidate_bin_dirs(None, managed_data_dir));
        Self { dirs }
    }

    /// Build a locator over an explicit list of candidate `bin` directories.
    /// Test-only seam so callers (e.g. the `doctor` check) can exercise the
    /// discovery/resolution path without mutating the process environment.
    #[cfg(test)]
    pub const fn with_dirs(dirs: Vec<PathBuf>) -> Self {
        Self { dirs }
    }

    /// Resolve a tool to a concrete path (a candidate dir that contains it) or
    /// the bare name for PATH resolution.
    fn resolve(&self, tool: &str) -> PathBuf {
        resolve_tool_in(&self.dirs, tool)
    }

    /// Like [`Self::resolve`] but fails fast with [`BackupError::ToolMissing`]
    /// when the tool is neither in a candidate dir nor on PATH.
    pub fn require(&self, tool: &str) -> Result<PathBuf, BackupError> {
        let resolved = self.resolve(tool);
        // If we resolved to a concrete existing path, use it. Otherwise probe
        // PATH by attempting `--version`; only then decide it's missing.
        if resolved.is_absolute() {
            return Ok(resolved);
        }
        if tool_on_path(tool) {
            Ok(resolved)
        } else {
            Err(BackupError::ToolMissing {
                tool: tool.to_owned(),
            })
        }
    }
}

/// Build the ordered candidate `bin` directories for pg client tools. Pure over
/// its inputs so the ordering/derivation is unit-testable.
fn candidate_bin_dirs(
    pg_bin_dir: Option<PathBuf>,
    managed_data_dir: Option<PathBuf>,
) -> Vec<PathBuf> {
    let mut dirs = Vec::new();
    if let Some(dir) = pg_bin_dir {
        dirs.push(dir);
    }
    if let Some(data_dir) = managed_data_dir {
        // Managed Postgres extracts its binaries to `<data_dir_parent>/postgresql`
        // (see `autumn_web::managed_pg`); the executables live under its `bin/`.
        let install = data_dir.parent().unwrap_or(&data_dir).join("postgresql");
        dirs.push(install.join("bin"));
        // Some bundle layouts nest under a versioned subdirectory; include the
        // install root so a shallow search can find `*/bin`.
        dirs.extend(nested_bin_dirs(&install));
    }
    dirs
}

/// Find `*/bin` directories one level under `install` (managed-pg bundles that
/// nest the toolchain under a version directory). Best-effort; empty if the
/// directory can't be read.
fn nested_bin_dirs(install: &Path) -> Vec<PathBuf> {
    let mut out = Vec::new();
    if let Ok(entries) = std::fs::read_dir(install) {
        for entry in entries.flatten() {
            if entry.path().is_dir() {
                out.push(entry.path().join("bin"));
            }
        }
    }
    out
}

/// Resolve `tool` against candidate dirs: the first dir that actually contains
/// the executable wins; otherwise return the bare name for PATH resolution.
fn resolve_tool_in(dirs: &[PathBuf], tool: &str) -> PathBuf {
    let exe = exe_name(tool);
    for dir in dirs {
        let candidate = dir.join(&exe);
        if candidate.is_file() {
            return candidate;
        }
    }
    PathBuf::from(tool)
}

/// Platform executable file name for a bare tool name.
fn exe_name(tool: &str) -> String {
    if cfg!(windows) {
        format!("{tool}.exe")
    } else {
        tool.to_owned()
    }
}

/// Whether a bare tool name resolves on PATH (probed via `--version`).
fn tool_on_path(tool: &str) -> bool {
    Command::new(tool)
        .arg("--version")
        .output()
        .is_ok_and(|o| o.status.success())
}

// ─── Manifest ───────────────────────────────────────────────────────────────

/// One entry in a backup manifest.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
struct ManifestTarget {
    /// Stable label: `"control"` or `"shard:<name>"`.
    label: String,
    /// Artifact file name within the run directory.
    file: String,
    /// The database name captured (credential-free; for humans).
    #[serde(default)]
    database: String,
}

/// Self-describing metadata for a backup run. Written as `manifest.json`; the
/// contract #1619 (offsite upload) enumerates.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
struct Manifest {
    /// autumn-cli version that produced the backup.
    autumn_version: String,
    /// RFC 3339 UTC timestamp.
    created_at: String,
    /// Effective profile the URLs were resolved under.
    profile: String,
    /// `"custom"` or `"plain"`.
    format: String,
    /// The databases captured.
    targets: Vec<ManifestTarget>,
}

/// The manifest file name inside a run directory.
const MANIFEST_FILE: &str = "manifest.json";

fn write_manifest(run_dir: &Path, manifest: &Manifest) -> Result<(), BackupError> {
    let json = serde_json::to_string_pretty(manifest).map_err(|e| BackupError::Io {
        context: "serializing manifest".to_owned(),
        source: std::io::Error::other(e),
    })?;
    let path = run_dir.join(MANIFEST_FILE);
    std::fs::write(&path, json).map_err(BackupError::io(format!("writing {}", path.display())))
}

fn read_manifest(run_dir: &Path) -> Result<Manifest, BackupError> {
    let path = run_dir.join(MANIFEST_FILE);
    let json = std::fs::read_to_string(&path).map_err(|e| BackupError::BadArtifact {
        detail: format!("{} has no readable {MANIFEST_FILE}: {e}", run_dir.display()),
    })?;
    serde_json::from_str(&json).map_err(|e| BackupError::BadArtifact {
        detail: format!("{} is not a valid manifest: {e}", path.display()),
    })
}

// ─── Path / naming helpers (pure) ───────────────────────────────────────────

/// The root directory for a profile's backup run directories:
/// `<dir or ./backups>/<profile>`.
fn backup_root(dir: Option<&Path>, profile: &str) -> PathBuf {
    let base = dir.map_or_else(|| PathBuf::from(DEFAULT_BACKUP_DIR), Path::to_path_buf);
    base.join(sanitize_component(profile))
}

/// The run-directory name for a given instant: a sortable UTC timestamp.
fn run_dir_name(now: &chrono::DateTime<chrono::Utc>) -> String {
    now.format("%Y%m%dT%H%M%SZ").to_string()
}

/// Create a fresh, uniquely-named run directory under `root`, starting from
/// `base`. The leaf is created **exclusively** (`create_dir`, which fails if it
/// already exists) so a run never reuses — and silently overwrites the
/// `control.dump`/shards/`manifest.json` of — an earlier restore point. Two
/// backups for the same profile in the same second (an overlapping cron run and
/// a manual retry) would otherwise collide, since the name has only second
/// precision; on collision we disambiguate with an incrementing `-N` suffix
/// (`<base>`, `<base>-2`, `<base>-3`, …).
///
/// The suffixed names stay lexically sortable so retention ordering in
/// [`list_run_dirs`]/[`prune`] is preserved: `<base>` sorts before `<base>-2`
/// (it's a prefix), the `-N` variants sort among themselves, and every
/// same-second variant shares the `…SSZ` prefix so it all sorts before the next
/// second's `…S(S+1)Z`.
fn create_unique_run_dir(root: &Path, base: &str) -> Result<PathBuf, BackupError> {
    // The exclusive `create_dir` below won't create intermediate directories, so
    // ensure the profile root exists first.
    std::fs::create_dir_all(root)
        .map_err(BackupError::io(format!("creating {}", root.display())))?;
    for attempt in 1..=MAX_RUN_DIR_ATTEMPTS {
        let name = if attempt == 1 {
            base.to_owned()
        } else {
            format!("{base}-{attempt}")
        };
        let candidate = root.join(&name);
        match std::fs::create_dir(&candidate) {
            Ok(()) => return Ok(candidate),
            Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {}
            Err(e) => {
                return Err(BackupError::io(format!("creating {}", candidate.display()))(e));
            }
        }
    }
    Err(BackupError::Io {
        context: format!("creating a unique run directory under {}", root.display()),
        source: std::io::Error::new(
            std::io::ErrorKind::AlreadyExists,
            format!("exhausted {MAX_RUN_DIR_ATTEMPTS} same-timestamp attempts for {base:?}"),
        ),
    })
}

/// The artifact file name for one target and format.
fn artifact_file_name(label: &str, format: BackupFormat) -> String {
    let ext = format.extension();
    if label == "control" {
        format!("control.{ext}")
    } else if let Some(name) = label.strip_prefix("shard:") {
        format!("shard-{}.{ext}", sanitize_component(name))
    } else {
        format!("{}.{ext}", sanitize_component(label))
    }
}

/// Sanitize a string for safe use as a single path component.
fn sanitize_component(s: &str) -> String {
    let cleaned: String = s
        .chars()
        .map(|c| {
            if c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '.') {
                c
            } else {
                '_'
            }
        })
        .collect();
    // Reject components that would resolve to the current/parent directory (or
    // nothing at all): joining `"."`/`".."`/`""` onto the backup root enables
    // directory traversal out of it.
    if cleaned.is_empty() || cleaned == "." || cleaned == ".." {
        "unnamed".to_owned()
    } else {
        cleaned
    }
}

/// Resolve the restore target label for a *bare single-file* artifact (one with
/// no accompanying manifest to name its target).
///
/// This is the inverse of [`artifact_file_name`]'s naming convention:
/// * an explicit `--shard <name>` always wins (the file IS the artifact the user
///   pointed at, so no manifest entry is required);
/// * otherwise a `shard-<name>.*` file resolves to that shard, and a `control.*`
///   file to the control database;
/// * a `shard-*` file whose shard name can't be recovered is an error rather
///   than a silent restore into control;
/// * any other (user-renamed) file falls back to control — the backup writer
///   only ever emits `control.*` / `shard-*`, so this path carries no shard
///   cross-target hazard while preserving the historical single-file behavior.
fn single_file_target_label(file: &str, shard: Option<&str>) -> Result<String, BackupError> {
    if let Some(name) = shard {
        return Ok(format!("shard:{name}"));
    }
    let stem = Path::new(file)
        .file_stem()
        .and_then(|s| s.to_str())
        .unwrap_or(file);
    if stem == "control" {
        return Ok("control".to_owned());
    }
    if let Some(shard_name) = stem.strip_prefix("shard-") {
        if shard_name.is_empty() {
            return Err(BackupError::BadArtifact {
                detail: format!(
                    "{file:?} looks like a shard artifact but its shard name is missing.\n  \
                     Pass --shard <name>, or restore the run directory (with its manifest.json)."
                ),
            });
        }
        return Ok(format!("shard:{shard_name}"));
    }
    Ok("control".to_owned())
}

/// Infer a backup format from a file extension.
fn infer_format_from_path(path: &Path) -> Option<BackupFormat> {
    match path.extension().and_then(|e| e.to_str()) {
        Some("dump") => Some(BackupFormat::Custom),
        Some("sql") => Some(BackupFormat::Plain),
        _ => None,
    }
}

/// Parse just the database name out of a connection URL (credential-safe; used
/// only for human-facing messages). Falls back to `"(database)"`.
fn parsed_db_name(url: &str) -> String {
    url::Url::parse(url)
        .ok()
        .and_then(|u| {
            u.path_segments()
                .and_then(|mut s| s.next().map(str::to_owned))
        })
        .filter(|s| !s.is_empty())
        .unwrap_or_else(|| "(database)".to_owned())
}

/// Count non-comment entries in a `pg_restore --list` TOC listing.
fn toc_entry_count(listing: &str) -> usize {
    listing
        .lines()
        .filter(|l| {
            let t = l.trim();
            !t.is_empty() && !t.starts_with(';')
        })
        .count()
}

/// Whether a plain SQL dump text contains `pg_dump`'s completion marker.
fn plain_dump_is_complete(contents: &str) -> bool {
    contents.contains(PLAIN_COMPLETE_MARKER)
}

/// A human description of a process exit code.
fn exit_desc(code: Option<i32>) -> String {
    code.map_or_else(|| "with a signal".to_owned(), |c| format!("with code {c}"))
}

/// Current UTC instant (indirection kept tiny so tests can reason about naming).
fn now_utc() -> chrono::DateTime<chrono::Utc> {
    chrono::Utc::now()
}

// ─── Offsite S3 upload / restore (issue #1619) ───────────────────────────────

/// A resolved, ready-to-use offsite destination. Built from `[backup.offsite]`
/// (via [`load_offsite`]) with the profile overlay + `AUTUMN_*` overrides
/// already applied. Holds the app's `[storage.s3]` bucket/endpoint too so the
/// distinct-destination guard (AC #3) can compare them without reloading config.
struct ResolvedOffsite {
    s3: S3Config,
    prefix: String,
    keep: Option<usize>,
    allow_shared_bucket: bool,
    access_key_id_env: Option<String>,
    secret_access_key_env: Option<String>,
    app_storage_bucket: Option<String>,
    app_storage_endpoint: Option<String>,
    /// The run's target profile — credentials are read from the `.env.<profile>`
    /// overlay for THIS profile, matching the config read (issue #1619 P2 #6).
    profile: String,
}

impl ResolvedOffsite {
    /// Build the transfer client, enforcing the distinct-destination guard
    /// (AC #3) and resolving credentials from the named env vars first, so a
    /// misconfiguration fails fast before any transfer.
    fn build_client(&self) -> Result<S3Client, BackupError> {
        if !self.allow_shared_bucket
            && destinations_conflict(
                &self.s3.bucket,
                self.s3.endpoint.as_deref(),
                self.app_storage_bucket.as_deref(),
                self.app_storage_endpoint.as_deref(),
            )
        {
            return Err(BackupError::SharedBucketRefused {
                bucket: self.s3.bucket.clone(),
            });
        }
        let creds = self.resolve_credentials()?;
        let client = S3Client::new(self.s3.clone(), creds).map_err(|e| BackupError::Offsite {
            op: "connect",
            detail: e.to_string(),
        })?;
        // Ops escape hatch: tune ONLY the multipart part size (bytes). This does
        // NOT lower the multipart threshold, so shrinking the part size can never
        // push a normal (sub-threshold) file into a multipart upload — it only
        // changes how an already-multipart transfer is chunked. The client
        // re-floors the value to S3's 5 MiB minimum.
        Ok(match self.multipart_part_size_override() {
            Some(bytes) => client.with_part_size(bytes),
            None => client,
        })
    }

    /// Read the `AUTUMN_OFFSITE_MULTIPART_PART_SIZE_BYTES` override (profile-aware,
    /// so a `.env.<profile>` value is honored). A blank/unparseable value is
    /// ignored (falls back to the client defaults).
    fn multipart_part_size_override(&self) -> Option<u64> {
        use autumn_web::config::Env as _;
        let env = dotenv_env_for_profile(&self.profile);
        env.var("AUTUMN_OFFSITE_MULTIPART_PART_SIZE_BYTES")
            .ok()
            .and_then(|v: String| v.trim().parse::<u64>().ok())
            .filter(|&n| n > 0)
    }

    /// Resolve the access key / secret from the environment variables NAMED by
    /// config. Credential *values* never appear in errors — only the var names.
    fn resolve_credentials(&self) -> Result<S3Credentials, BackupError> {
        // Read credentials from the SAME profile-specific `.env.<profile>` overlay
        // the offsite config was resolved under (issue #1619 P2 #6), so a
        // `.env.<profile>`-provided credential value is honored.
        let env = dotenv_env_for_profile(&self.profile);
        let access_key_id = read_credential_env(&env, self.access_key_id_env.as_deref())?;
        let secret_access_key = read_credential_env(&env, self.secret_access_key_env.as_deref())?;
        Ok(S3Credentials {
            access_key_id,
            secret_access_key,
        })
    }
}

/// Read a credential from the env var `var` names. Errors name only the VARIABLE
/// (never a value). A `None`/blank config var name is itself an error.
fn read_credential_env(
    env: &dyn autumn_web::config::Env,
    var: Option<&str>,
) -> Result<String, BackupError> {
    let var = var
        .map(str::trim)
        .filter(|v| !v.is_empty())
        .ok_or_else(|| BackupError::MissingCredentialEnv {
            var: "(none configured)".to_owned(),
        })?;
    env.var(var)
        .ok()
        .filter(|v| !v.is_empty())
        .ok_or_else(|| BackupError::MissingCredentialEnv {
            var: var.to_owned(),
        })
}

/// A leniently-loaded `[backup.offsite]` section: enough to learn `auto_upload`
/// WITHOUT validating the bucket/credentials, so a plain local `autumn db backup`
/// (no `--upload`, no `auto_upload`) never fails just because an incomplete
/// offsite section exists. Call [`LoadedOffsite::resolve`] — only when an upload
/// is actually requested — to get the validated, ready-to-use destination.
struct LoadedOffsite {
    offsite: autumn_web::config::OffsiteBackupConfig,
    app_storage_bucket: Option<String>,
    app_storage_endpoint: Option<String>,
    /// The target profile this section was resolved under (issue #1619 P2 #6).
    profile: String,
}

impl LoadedOffsite {
    /// Validate and resolve into a ready-to-use [`ResolvedOffsite`]. This is the
    /// STRICT step — bucket is required here — and must only run when uploading.
    fn resolve(self) -> Result<ResolvedOffsite, BackupError> {
        let Self {
            offsite,
            app_storage_bucket,
            app_storage_endpoint,
            profile,
        } = self;
        let bucket = offsite
            .s3
            .bucket
            .as_deref()
            .map(str::trim)
            .filter(|b| !b.is_empty())
            .map(str::to_owned)
            .ok_or_else(|| BackupError::OffsiteConfig {
                detail: "backup.offsite.s3.bucket is required".to_owned(),
            })?;
        // Region is only meaningful for AWS + SigV4 scope; many S3-compatible
        // endpoints ignore it. Default to us-east-1 when unset.
        let region = offsite
            .s3
            .region
            .as_deref()
            .map(str::trim)
            .filter(|r| !r.is_empty())
            .unwrap_or("us-east-1")
            .to_owned();
        let s3 = S3Config {
            bucket,
            region,
            endpoint: offsite
                .s3
                .endpoint
                .as_deref()
                .map(str::trim)
                .filter(|e| !e.is_empty())
                .map(str::to_owned),
            force_path_style: offsite.s3.force_path_style,
        };
        Ok(ResolvedOffsite {
            s3,
            prefix: normalize_offsite_prefix(offsite.prefix.as_deref()),
            keep: offsite.keep,
            allow_shared_bucket: offsite.allow_shared_bucket,
            access_key_id_env: offsite.s3.access_key_id_env,
            secret_access_key_env: offsite.s3.secret_access_key_env,
            app_storage_bucket,
            app_storage_endpoint,
            profile,
        })
    }
}

/// Decide whether a backup run should upload, reading ONLY the
/// `[backup.offsite].auto_upload` bit — WITHOUT a full `AutumnConfig` load (no
/// global validation, no encrypted-credentials decryption), so a local-only
/// backup is never blocked by unrelated config/credentials issues (issue #1619
/// P2 #8). Resolution matches the config layering: real env
/// `AUTUMN_BACKUP__OFFSITE__AUTO_UPLOAD` → `.env.<profile>` → merged TOML
/// `[backup.offsite].auto_upload` (base + `[profile.<p>]` overlay) → `false`.
fn auto_upload_probe(profile: &str) -> bool {
    use autumn_web::config::Env as _;
    // Normalize aliases/case to the app's resolved profile (P2 #20) so both the
    // dotenv overlay and the `[profile.<p>]` selection match what the app reads.
    let profile = migrate::canonical_profile(profile);
    // The profile-aware dotenv overlay covers BOTH the real env and `.env.<p>`
    // (a real env var wins inside the overlay), matching P2 #6.
    let env = dotenv_env_for_profile(&profile);
    // Read `autumn.toml` from the config/manifest dir (AUTUMN_MANIFEST_DIR),
    // matching where the full loader + the dotenv overlay look — so an installed/
    // daemon launch with a different CWD still sees `auto_upload` (P2 #15).
    let table = migrate::read_autumn_toml_table_with_profile_from_config_dir(Some(&profile));
    auto_upload_from_sources(|k| env.var(k).ok(), table.as_ref())
}

/// Pure resolution of `auto_upload` from an env lookup + a merged TOML table.
/// Env (real + `.env`) wins over TOML; default `false`. Separated so the
/// precedence is unit-testable without touching the filesystem/process env.
fn auto_upload_from_sources<F>(env_var: F, table: Option<&toml::Table>) -> bool
where
    F: Fn(&str) -> Option<String>,
{
    if let Some(raw) = env_var("AUTUMN_BACKUP__OFFSITE__AUTO_UPLOAD")
        && let Some(b) = parse_bool_flag(&raw)
    {
        return b;
    }
    table
        .and_then(|t| t.get("backup"))
        .and_then(|v| v.get("offsite"))
        .and_then(|v| v.get("auto_upload"))
        .and_then(toml::Value::as_bool)
        .unwrap_or(false)
}

/// Parse a boolean env flag the same way the config loader's `parse_env_bool`
/// does: `1`/`true` → true, `0`/`false` → false (case-insensitive, trimmed),
/// anything else → `None` (ignored).
fn parse_bool_flag(raw: &str) -> Option<bool> {
    match raw.trim().to_ascii_lowercase().as_str() {
        "1" | "true" => Some(true),
        "0" | "false" => Some(false),
        _ => None,
    }
}

/// Leniently resolve the `[backup.offsite]` destination for `profile` WITHOUT a
/// full `AutumnConfig::load` — i.e. WITHOUT decrypting the app credential store
/// (`config/credentials/<profile>.toml.enc`) or running app-wide validation, so
/// offsite commands work on a minimal DR/recovery box that has only DB + offsite
/// env vars and NO `AUTUMN_MASTER_KEY` (issue #1619 P2 #9). It reads only what
/// offsite needs — the merged TOML (base + `[profile.<p>]`) plus real env and
/// `.env.<profile>` overrides — for `[backup.offsite]` AND the `[storage.s3]`
/// bucket/endpoint (the distinct-destination guard needs them).
///
/// `Ok(None)` when no offsite section is configured. NO bucket/cred validation
/// here — that is deferred to [`LoadedOffsite::resolve`] so a local backup is
/// never blocked by an incomplete offsite section.
fn load_offsite(profile: &str) -> Result<Option<LoadedOffsite>, BackupError> {
    // Normalize aliases/case to the app's resolved profile (P2 #20) so the dotenv
    // overlay, the `[profile.<p>]` selection, and the stored profile (later reused
    // for credential resolution) all match what the running app reads.
    let profile = migrate::canonical_profile(profile);
    // Real env + `.env.<profile>` overlay (profile-aware, P2 #6); merged TOML
    // (base + `[profile.<p>]`) — neither decrypts credentials nor validates.
    let env = dotenv_env_for_profile(&profile);
    // Read `autumn.toml` from the config/manifest dir (AUTUMN_MANIFEST_DIR),
    // matching the full loader + dotenv overlay — so an installed/daemon launch
    // whose CWD differs from the config dir still finds the offsite destination
    // (and enforces the distinct-bucket guard) instead of reporting it
    // unconfigured (P2 #15).
    let table = migrate::read_autumn_toml_table_with_profile_from_config_dir(Some(&profile));
    resolve_loaded_offsite(table.as_ref(), &env, &profile)
}

/// Pure core of [`load_offsite`]: build the offsite view from a merged TOML table
/// and an `Env`, applying `AUTUMN_*` overrides via the config crate's
/// env-override machinery (which does NOT decrypt credentials or run global
/// validation). Separated so the DR-box behavior is unit-testable without
/// touching the filesystem / process env / credential store.
fn resolve_loaded_offsite(
    table: Option<&toml::Table>,
    env: &dyn autumn_web::config::Env,
    profile: &str,
) -> Result<Option<LoadedOffsite>, BackupError> {
    // Deserialize the merged TOML into AutumnConfig — plain `Deserialize`, so no
    // credential decryption and no `validate()` (those live only in `load*`).
    let mut cfg = match table {
        Some(t) => {
            let toml_str = toml::to_string(t).map_err(|e| BackupError::Offsite {
                op: "config",
                detail: e.to_string(),
            })?;
            toml::from_str::<autumn_web::config::AutumnConfig>(&toml_str).map_err(|e| {
                BackupError::Offsite {
                    op: "config",
                    detail: e.to_string(),
                }
            })?
        }
        None => autumn_web::config::AutumnConfig::default(),
    };
    // Apply `AUTUMN_*` env overrides (incl. backup.offsite + storage.s3). This is
    // pure field-setting — no decryption, no validation.
    cfg.apply_env_overrides_with_env(env);

    let Some(offsite) = cfg.backup.offsite else {
        return Ok(None);
    };
    // Only treat the `[storage.s3]` bucket as the APP storage destination when the
    // resolved storage backend is actually S3 (P2 #16). If the app runs on the
    // local/disabled backend, a leftover `[storage.s3]` bucket is inert — it is
    // not where the app writes blobs — so it must NOT trip the shared-bucket guard
    // and force `allow_shared_bucket` for an offsite backup that happens to reuse
    // that bucket name.
    let storage_is_s3 = cfg.storage.backend == autumn_web::storage::StorageBackend::S3;
    let (app_storage_bucket, app_storage_endpoint) = if storage_is_s3 {
        (
            cfg.storage
                .s3
                .bucket
                .as_deref()
                .map(str::trim)
                .filter(|b| !b.is_empty())
                .map(str::to_owned),
            cfg.storage
                .s3
                .endpoint
                .as_deref()
                .map(str::trim)
                .filter(|e| !e.is_empty())
                .map(str::to_owned),
        )
    } else {
        (None, None)
    };
    Ok(Some(LoadedOffsite {
        offsite: *offsite,
        app_storage_bucket,
        app_storage_endpoint,
        profile: profile.to_owned(),
    }))
}

/// Whether the offsite destination collides with the app's user-facing blob
/// storage: same bucket AND same canonical endpoint authority. Pure for unit
/// testing the AC #3 opt-in guard.
pub fn destinations_conflict(
    offsite_bucket: &str,
    offsite_endpoint: Option<&str>,
    app_bucket: Option<&str>,
    app_endpoint: Option<&str>,
) -> bool {
    let Some(app_bucket) = app_bucket else {
        return false;
    };
    app_bucket == offsite_bucket
        && canonical_authority(offsite_endpoint, offsite_bucket)
            == canonical_authority(app_endpoint, app_bucket)
}

/// Canonicalize an endpoint to a comparable `scheme://host[:non-default-port]`
/// authority so spellings that address the SAME S3 service compare equal:
///
/// * `None`/empty (the AWS default endpoint) and any explicit `*.amazonaws.com`
///   URL both collapse to `"aws"` — S3 bucket names are globally unique, so the
///   same bucket name is the same bucket regardless of regional endpoint.
/// * default ports (443/https, 80/http) are dropped so `host:443` == bare host.
/// * a virtual-hosted `{bucket}.` host prefix is stripped so it compares equal
///   to the path-style spelling of the same endpoint.
///
/// This prevents a genuinely shared bucket from slipping past the AC #3 guard
/// because one side wrote the endpoint out explicitly and the other left it None.
fn canonical_authority(endpoint: Option<&str>, bucket: &str) -> String {
    let raw = endpoint.unwrap_or("").trim();
    if raw.is_empty() {
        return "aws".to_owned();
    }
    let with_scheme = if raw.contains("://") {
        raw.to_owned()
    } else {
        format!("https://{raw}")
    };
    let Ok(url) = url::Url::parse(&with_scheme) else {
        return raw.to_ascii_lowercase();
    };
    let host = url.host_str().unwrap_or("").to_ascii_lowercase();
    if host == "amazonaws.com" || host.ends_with(".amazonaws.com") {
        return "aws".to_owned();
    }
    // Strip a virtual-hosted `{bucket}.` prefix so path-style and virtual-hosted
    // spellings of the same endpoint compare equal.
    let bucket_prefix = format!("{}.", bucket.to_ascii_lowercase());
    let host = host.strip_prefix(&bucket_prefix).unwrap_or(&host);
    let scheme = url.scheme().to_ascii_lowercase();
    let default_port = match scheme.as_str() {
        "https" => Some(443),
        "http" => Some(80),
        _ => None,
    };
    match url.port() {
        Some(port) if Some(port) != default_port => format!("{scheme}://{host}:{port}"),
        _ => format!("{scheme}://{host}"),
    }
}

/// Normalize a configured key prefix: trim surrounding whitespace and slashes so
/// object keys join cleanly (an empty prefix means "bucket root").
fn normalize_offsite_prefix(prefix: Option<&str>) -> String {
    prefix.unwrap_or("").trim().trim_matches('/').to_owned()
}

/// Join non-empty key segments with `/` (no leading/trailing slash).
fn join_key(parts: &[&str]) -> String {
    parts
        .iter()
        .filter(|p| !p.is_empty())
        .copied()
        .collect::<Vec<_>>()
        .join("/")
}

/// The object key for one file of a run: `{prefix}/{profile}/{run_id}/{file}`.
fn offsite_object_key(prefix: &str, profile: &str, run_id: &str, file: &str) -> String {
    join_key(&[prefix, profile, run_id, file])
}

/// A short, globally-unique token appended to the REMOTE run id so that two
/// hosts backing up the SAME profile in the SAME second to the SAME
/// bucket/prefix never produce colliding S3 keys (issue #1619 P2 #12). Without
/// it, `create_unique_run_dir` only disambiguates within one host's filesystem,
/// so cross-host same-second runs would overwrite/interleave each other's dumps
/// and manifests and corrupt `latest`.
///
/// Derived from host + pid + wall-clock nanoseconds, hashed with the
/// already-linked `sha2`/`hex` crates (no new dependency). It is NON-sensitive —
/// pure entropy, no secret material. The LOCAL run-dir naming (issue #1595
/// `<profile>/<timestamp>/`) is deliberately left unchanged; only the remote key
/// gains the `-<token>` suffix.
fn remote_run_token() -> String {
    use sha2::{Digest, Sha256};
    let host = std::env::var("HOSTNAME")
        .or_else(|_| std::env::var("COMPUTERNAME"))
        .unwrap_or_default();
    let nanos = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map_or(0, |d| d.as_nanos());
    let mut hasher = Sha256::new();
    hasher.update(host.as_bytes());
    hasher.update([0]); // domain separator between host and pid
    hasher.update(std::process::id().to_le_bytes());
    hasher.update(nanos.to_le_bytes());
    // 8 hex chars (4 bytes): with the same-second timestamp already dominating
    // the run id, a cross-host collision needs the same second AND a 32-bit token
    // collision — negligible for the handful of hosts sharing one backup prefix.
    hex::encode(&hasher.finalize()[..4])
}

/// Compose the globally-unique REMOTE run id from the LOCAL run id (a #1595
/// `<timestamp>`) and a [`remote_run_token`]: `<timestamp>-<token>`. The
/// timestamp still dominates the lexical sort, so `latest` (newest by timestamp)
/// is unaffected and the token only tie-breaks same-second runs from different
/// hosts — each a valid distinct backup, neither overwriting the other.
fn remote_run_id(local_run_id: &str, token: &str) -> String {
    format!("{local_run_id}-{token}")
}

/// The list prefix that enumerates every run for a profile (trailing slash so a
/// `list-type=2` prefix match is scoped to this profile).
fn offsite_profile_prefix(prefix: &str, profile: &str) -> String {
    format!("{}/", join_key(&[prefix, profile]))
}

/// The list prefix for the files of a single run.
fn offsite_run_prefix(prefix: &str, profile: &str, run_id: &str) -> String {
    format!("{}/", join_key(&[prefix, profile, run_id]))
}

/// Upload every file of a completed run and verify each remote object matches
/// the local file (AC #2), then run remote retention (AC #5). Returns a
/// credential-safe failure detail string (the caller wraps it into the
/// split-outcome error). Never mutates the local artifact.
fn upload_run(
    offsite: &ResolvedOffsite,
    client: &S3Client,
    run_dir: &Path,
    profile: &str,
    run_id: &str,
) -> Result<(), String> {
    eprintln!("\u{2500}\u{2500} offsite upload \u{2500}\u{2500}");
    let mut files = list_run_files(run_dir).map_err(|e| e.to_string())?;
    files.sort();
    // Upload `manifest.json` LAST so the presence of a verified remote manifest
    // implies the whole run uploaded (completeness marker for `latest`
    // resolution). A stable sort keeps the artifact files in alphabetical order
    // and moves the single manifest to the end.
    files.sort_by_key(|f| f.as_str() == MANIFEST_FILE);
    if files.is_empty() {
        return Err("the completed run directory contains no files to upload".to_owned());
    }
    let mut written_keys: Vec<String> = Vec::new();
    for file in &files {
        let path = run_dir.join(file);
        let key = offsite_object_key(&offsite.prefix, profile, run_id, file);
        // `put_file_and_verify` now self-cleans the single key it just wrote on a
        // verify failure (#1760). This run-level cleanup is still needed for the
        // OTHER keys written EARLIER in the run: record every key up-front, then on
        // ANY failure best-effort delete everything this run wrote — manifest first
        // — so a run whose upload was reported FAILED never keeps a remote
        // `manifest.json` and can't become `latest` or consume a retention slot
        // (restores the P2 #2 invariant; #21).
        written_keys.push(key.clone());
        // Stream the file straight from disk (hash pre-pass + streamed body):
        // a multi-GB dump is never read into memory.
        if let Err(e) = client.put_file_and_verify(&key, &path) {
            let detail = format!("{file}: {e}");
            cleanup_failed_upload(client, &written_keys);
            return Err(detail);
        }
        eprintln!("  \u{2713} uploaded + verified {file}");
    }
    eprintln!(
        "\u{2713} Offsite upload verified: {} object(s) under {}",
        files.len(),
        offsite_run_prefix(&offsite.prefix, profile, run_id),
    );

    // Remote retention (AC #5): only after the just-uploaded run verified, and
    // never removing that run. A retention error is loud but non-fatal — the
    // verified offsite copy already exists.
    if let Some(keep) = offsite.keep
        && let Err(e) = prune_remote(offsite, client, profile, run_id, keep)
    {
        eprintln!("  \u{26A0} offsite retention skipped: {e}");
    }
    Ok(())
}

/// Best-effort removal of the objects a FAILED [`upload_run`] wrote, so a partial
/// upload never leaves a remote `manifest.json` behind — which
/// [`complete_remote_run_ids`] would otherwise count as a COMPLETE run for
/// `latest`/retention (P2 #21). The manifest key is deleted FIRST so that even if
/// a later delete fails, the completeness marker is already gone. Delete failures
/// are swallowed: the caller surfaces the ORIGINAL upload/verify error.
fn cleanup_failed_upload(client: &S3Client, written_keys: &[String]) {
    for key in order_cleanup_keys(written_keys) {
        let _ = client.delete_object(&key);
    }
}

/// Order the keys a failed upload wrote for cleanup: the run's `manifest.json`
/// FIRST (drop the completeness marker before anything else), then the rest in
/// their written order. Pure for unit testing.
fn order_cleanup_keys(written_keys: &[String]) -> Vec<String> {
    let mut keys = written_keys.to_vec();
    // `false` (manifest) sorts before `true` (everything else); stable sort keeps
    // the remaining keys in their original order.
    keys.sort_by_key(|k| !key_is_manifest(k));
    keys
}

/// Whether an object key is a run's manifest (its final path component is
/// [`MANIFEST_FILE`]).
fn key_is_manifest(key: &str) -> bool {
    key.rsplit('/').next() == Some(MANIFEST_FILE)
}

/// List the file names (not sub-directories) directly inside a run directory.
fn list_run_files(run_dir: &Path) -> Result<Vec<String>, BackupError> {
    let mut files = Vec::new();
    for entry in std::fs::read_dir(run_dir)
        .map_err(BackupError::io(format!("read {}", run_dir.display())))?
    {
        let entry = entry.map_err(BackupError::io(format!(
            "read entry in {}",
            run_dir.display()
        )))?;
        if entry.path().is_file() {
            files.push(entry.file_name().to_string_lossy().into_owned());
        }
    }
    Ok(files)
}

/// Prune remote runs for a profile, keeping the newest `keep` COMPLETE runs and
/// NEVER deleting the just-uploaded `keep_run_id`. Independent of the local
/// `--keep`.
///
/// Retention is based ONLY on complete runs (those with a remote `manifest.json`,
/// the same completeness source `resolve_latest_run` uses), so a partial/failed
/// upload can't consume a keep slot and cause an older *complete* backup to be
/// pruned. Incomplete/partial prefixes are left untouched here — deleting them
/// could race a concurrent in-progress upload; GC of stale partial prefixes is a
/// possible follow-up, out of scope for this change.
fn prune_remote(
    offsite: &ResolvedOffsite,
    client: &S3Client,
    profile: &str,
    keep_run_id: &str,
    keep: usize,
) -> Result<(), String> {
    let list_prefix = offsite_profile_prefix(&offsite.prefix, profile);
    let objects = client
        .list_objects_v2(&list_prefix)
        .map_err(|e| e.to_string())?;
    let run_ids = complete_remote_run_ids(&objects, &list_prefix);
    let to_remove = plan_remote_pruning(&run_ids, keep, keep_run_id);
    for run_id in &to_remove {
        let run_prefix = offsite_run_prefix(&offsite.prefix, profile, run_id);
        let run_objects = client
            .list_objects_v2(&run_prefix)
            .map_err(|e| e.to_string())?;
        for obj in &run_objects {
            client.delete_object(&obj.key).map_err(|e| e.to_string())?;
        }
        eprintln!("  \u{1F5D1} pruned remote backup {run_id}");
    }
    Ok(())
}

/// Extract the unique, sorted run-id (timestamp) segments from listed object
/// keys under `list_prefix` (`{list_prefix}{run_id}/{file}`). Test-only: the
/// live paths (`latest` resolution and retention) use the manifest-gated
/// [`complete_remote_run_ids`] so partial runs never count.
#[cfg(test)]
fn remote_run_ids(objects: &[s3::S3Object], list_prefix: &str) -> Vec<String> {
    let mut ids: Vec<String> = objects
        .iter()
        .filter_map(|o| o.key.strip_prefix(list_prefix))
        .filter_map(|rest| rest.split('/').next())
        .filter(|s| !s.is_empty())
        .map(str::to_owned)
        .collect();
    ids.sort();
    ids.dedup();
    ids
}

/// The unique, sorted run ids under `list_prefix` that have a remote
/// `manifest.json` — i.e. COMPLETE runs. Because [`upload_run`] uploads the
/// manifest last, a `--upload` that died partway leaves artifacts but no
/// manifest, so its run id is excluded here. This keeps a partial failed upload
/// from shadowing the last good backup when resolving `latest`.
fn complete_remote_run_ids(objects: &[s3::S3Object], list_prefix: &str) -> Vec<String> {
    let mut ids: Vec<String> = objects
        .iter()
        .filter_map(|o| o.key.strip_prefix(list_prefix))
        .filter_map(|rest| {
            let mut parts = rest.splitn(2, '/');
            match (parts.next(), parts.next()) {
                (Some(run_id), Some(file)) if !run_id.is_empty() && file == MANIFEST_FILE => {
                    Some(run_id.to_owned())
                }
                _ => None,
            }
        })
        .collect();
    ids.sort();
    ids.dedup();
    ids
}

/// Pure remote-retention rule: given run ids sorted ascending, keep the newest
/// `keep` and return the older ones to delete — but ALWAYS exclude
/// `keep_run_id` (the just-uploaded run) from deletion, even if retention math
/// would otherwise remove it. `keep == 0` is clamped to 1.
fn plan_remote_pruning(sorted_run_ids: &[String], keep: usize, keep_run_id: &str) -> Vec<String> {
    let keep = keep.max(1);
    if sorted_run_ids.len() <= keep {
        return Vec::new();
    }
    let remove_count = sorted_run_ids.len() - keep;
    sorted_run_ids[..remove_count]
        .iter()
        .filter(|id| id.as_str() != keep_run_id)
        .cloned()
        .collect()
}

// ─── Offsite list (`autumn db offsite list`, AC #4) ──────────────────────────

/// Entry point for `autumn db offsite list`. Prints a credential-safe message
/// and exits non-zero on failure.
pub fn run_offsite_list(profile: Option<&str>) {
    eprintln!("\u{1F342} autumn db offsite list\n");
    if let Err(e) = offsite_list(profile) {
        eprintln!("\u{2717} {e}");
        std::process::exit(1);
    }
}

fn offsite_list(profile: Option<&str>) -> Result<(), BackupError> {
    // Canonicalize so the list prefix matches the profile the upload wrote under
    // (P2 #22): `list --profile production` and `--profile prod` both read `prod/`.
    let profile = migrate::canonical_profile(&migrate::effective_profile(profile));
    let offsite = load_offsite(&profile)?
        .ok_or(BackupError::OffsiteNotConfigured)?
        .resolve()?;
    let client = offsite.build_client()?;
    let list_prefix = offsite_profile_prefix(&offsite.prefix, &profile);
    let objects = client
        .list_objects_v2(&list_prefix)
        .map_err(|e| BackupError::Offsite {
            op: "list",
            detail: e.to_string(),
        })?;
    let runs = group_offsite_objects(&objects, &list_prefix);
    if runs.is_empty() {
        eprintln!("  \u{2139} No offsite backups found for profile {profile:?}.");
        return Ok(());
    }
    print!("{}", format_offsite_listing(&runs));
    Ok(())
}

/// One offsite run, grouped for listing.
#[derive(Debug, Clone, PartialEq, Eq)]
struct OffsiteRun {
    run_id: String,
    files: Vec<(String, u64)>,
    total: u64,
    /// Whether the run has a remote `manifest.json` (a complete upload). A run
    /// missing it is a partial/failed upload and is flagged in the listing.
    complete: bool,
}

/// Group listed objects (`{list_prefix}{run_id}/{file}`) into runs, sorted by
/// run id ascending. A run is `complete` iff it has a `manifest.json` object.
/// Pure for unit testing.
fn group_offsite_objects(objects: &[s3::S3Object], list_prefix: &str) -> Vec<OffsiteRun> {
    let mut map: std::collections::BTreeMap<String, OffsiteRun> = std::collections::BTreeMap::new();
    for obj in objects {
        let Some(rest) = obj.key.strip_prefix(list_prefix) else {
            continue;
        };
        let mut parts = rest.splitn(2, '/');
        let (Some(run_id), Some(file)) = (parts.next(), parts.next()) else {
            continue;
        };
        if run_id.is_empty() || file.is_empty() {
            continue;
        }
        let run = map.entry(run_id.to_owned()).or_insert_with(|| OffsiteRun {
            run_id: run_id.to_owned(),
            files: Vec::new(),
            total: 0,
            complete: false,
        });
        run.files.push((file.to_owned(), obj.size));
        run.total += obj.size;
        if file == MANIFEST_FILE {
            run.complete = true;
        }
    }
    map.into_values().collect()
}

/// Render a run listing as a human table: timestamp, files (labels), size. An
/// incomplete run (no remote manifest) is flagged so a partial failed upload is
/// never mistaken for a restorable backup. Pure.
fn format_offsite_listing(runs: &[OffsiteRun]) -> String {
    use std::fmt::Write as _;
    let mut out = String::new();
    // Column shows the FULL remote run id (`<timestamp>-<token>`, P2 #12) so an
    // operator can copy it verbatim into `restore offsite:<profile>/<run id>`.
    let _ = writeln!(out, "{:<28}  {:>10}  FILES", "RUN ID", "SIZE");
    for run in runs {
        let mut labels: Vec<&str> = run.files.iter().map(|(f, _)| f.as_str()).collect();
        labels.sort_unstable();
        let mut files = labels.join(", ");
        if !run.complete {
            files.push_str("  (INCOMPLETE — no manifest, not restorable)");
        }
        let _ = writeln!(
            out,
            "{:<28}  {:>10}  {}",
            run.run_id,
            human_size(run.total),
            files,
        );
    }
    out
}

/// A compact human byte size (KiB/MiB/GiB), for the listing table.
fn human_size(bytes: u64) -> String {
    const UNITS: [&str; 4] = ["B", "KiB", "MiB", "GiB"];
    #[allow(clippy::cast_precision_loss)]
    let mut size = bytes as f64;
    let mut unit = 0;
    while size >= 1024.0 && unit < UNITS.len() - 1 {
        size /= 1024.0;
        unit += 1;
    }
    if unit == 0 {
        format!("{bytes} B")
    } else {
        format!("{size:.1} {}", UNITS[unit])
    }
}

// ─── Offsite restore (AC #4) ─────────────────────────────────────────────────

/// A parsed offsite restore reference: which profile's run to pull, and which.
#[derive(Debug, Clone, PartialEq, Eq)]
struct OffsiteRef {
    profile: String,
    selector: OffsiteSelector,
}

/// How the run is chosen: newest, or an explicit timestamp/run id.
#[derive(Debug, Clone, PartialEq, Eq)]
enum OffsiteSelector {
    Latest,
    Exact(String),
}

/// Resolve an offsite reference when offsite restore is indicated. Returns:
///
/// * `None` — offsite is NOT indicated (no `--offsite` flag and no `offsite:`
///   prefix); the caller routes to a normal LOCAL restore.
/// * `Some(Ok(_))` — offsite is indicated and the reference parsed.
/// * `Some(Err(_))` — offsite is indicated but the reference is malformed; the
///   caller MUST surface the error and MUST NEVER fall through to local restore
///   (otherwise `restore --offsite latest` could silently restore an unrelated
///   local path named `latest` in the cwd).
fn resolve_offsite_ref(flag: bool, artifact: &str) -> Option<Result<OffsiteRef, BackupError>> {
    let has_prefix = artifact.starts_with("offsite:");
    if !flag && !has_prefix {
        return None;
    }
    Some(
        parse_offsite_ref(artifact).ok_or_else(|| BackupError::InvalidOffsiteRef {
            reference: artifact.to_owned(),
        }),
    )
}

/// Parse `offsite:<profile>/<timestamp|latest>` (the `offsite:` prefix is
/// optional). Returns `None` when the shape is not `<profile>/<selector>`.
fn parse_offsite_ref(s: &str) -> Option<OffsiteRef> {
    let s = s.strip_prefix("offsite:").unwrap_or(s).trim();
    let (profile, selector) = s.split_once('/')?;
    let profile = profile.trim();
    let selector = selector.trim();
    if profile.is_empty() || selector.is_empty() {
        return None;
    }
    let selector = if selector.eq_ignore_ascii_case("latest") {
        OffsiteSelector::Latest
    } else {
        OffsiteSelector::Exact(selector.to_owned())
    };
    Some(OffsiteRef {
        profile: profile.to_owned(),
        selector,
    })
}

/// Whether `name` is a single, plain filename component safe to `join` onto the
/// download temp dir (P2 #13). Rejects anything that could escape the temp dir
/// BEFORE manifest validation: a name is safe only when it contains no path
/// separator (`/` OR `\` — `\` is a valid Unix filename byte but an S3 key
/// written by a Windows run could carry a traversal like `..\..\evil`), is not
/// `.`/`..`, is not absolute, and has no Windows drive/prefix. The
/// `Component::Normal(c) if c == name` check is the robust cross-platform core;
/// the explicit `\` reject covers the Unix case where `\` is not a separator.
fn is_safe_leaf_name(name: &str) -> bool {
    use std::path::{Component, Path};
    if name.is_empty() || name.contains('/') || name.contains('\\') {
        return false;
    }
    let p = Path::new(name);
    p.components().count() == 1
        && matches!(p.components().next(), Some(Component::Normal(c)) if c == name)
}

/// Download the referenced offsite run to a fresh temp dir and restore from it
/// via the identical [`RestorePlan::discover`] path (AC #4). The temp dir is
/// removed when the guard drops (after the restore completes or fails).
fn restore_from_offsite(args: &RestoreArgs, oref: &OffsiteRef) -> Result<(), BackupError> {
    // Canonicalize the profile so the REMOTE key prefix matches what upload wrote
    // (P2 #22): `offsite:production/latest` reads the same `prod/…` prefix that
    // `--profile production` uploaded under.
    let profile = migrate::canonical_profile(&oref.profile);
    let offsite = load_offsite(&profile)?
        .ok_or(BackupError::OffsiteNotConfigured)?
        .resolve()?;
    let client = offsite.build_client()?;

    let run_id = match &oref.selector {
        OffsiteSelector::Exact(sel) => resolve_exact_run(&offsite, &client, &profile, sel)?,
        OffsiteSelector::Latest => resolve_latest_run(&offsite, &client, &profile)?,
    };
    eprintln!("  \u{2139} restoring from offsite {profile}/{run_id}");

    let run_prefix = offsite_run_prefix(&offsite.prefix, &profile, &run_id);
    let objects = client
        .list_objects_v2(&run_prefix)
        .map_err(|e| BackupError::Offsite {
            op: "list",
            detail: e.to_string(),
        })?;
    if objects.is_empty() {
        return Err(BackupError::BadArtifact {
            detail: format!(
                "no offsite backup found at {profile}/{run_id} (nothing under {run_prefix})"
            ),
        });
    }

    let temp =
        tempfile::tempdir().map_err(BackupError::io("creating a temp dir for offsite restore"))?;
    for obj in &objects {
        let file = obj
            .key
            .strip_prefix(run_prefix.as_str())
            .filter(|f| is_safe_leaf_name(f))
            .ok_or_else(|| BackupError::Offsite {
                op: "download",
                detail: format!(
                    "refusing offsite object with an unsafe key layout: {} (each object name \
                     must be a single plain filename — no path separators, traversal, or drive \
                     prefix)",
                    obj.key
                ),
            })?;
        // Stream the object straight to disk (constant memory), so a multi-GB
        // dump is never buffered while downloading for restore.
        let dest = temp.path().join(file);
        let mut out = std::fs::File::create(&dest)
            .map_err(BackupError::io(format!("creating downloaded {file}")))?;
        client
            .download_object(&obj.key, &mut out)
            .map_err(|e| BackupError::Offsite {
                op: "download",
                detail: format!("{file}: {e}"),
            })?;
        eprintln!("  \u{2913} downloaded {file}");
    }

    // Hand the downloaded run to the SAME plan path so the identical
    // integrity-refusal + restore protocol applies unchanged.
    let plan = RestorePlan::discover(temp.path(), args.shard.as_deref())?;
    apply_restore_plan(&plan, args)
    // `temp` is dropped here, removing the downloaded artifacts.
}

/// Resolve an explicit offsite selector to a single complete remote run id
/// (P2 #12). The selector may be a full remote run id (`<ts>-<token>`) or a bare
/// `<ts>` timestamp (operator convenience, and the pre-token spelling): a
/// complete run matches when it equals the selector or begins with
/// `"{selector}-"`. Errors (never silently guesses) when nothing matches or when
/// several do — the latter happens when two hosts backed up the same second, and
/// the message lists the full run ids so the operator can re-run with one.
fn resolve_exact_run(
    offsite: &ResolvedOffsite,
    client: &S3Client,
    profile: &str,
    selector: &str,
) -> Result<String, BackupError> {
    let list_prefix = offsite_profile_prefix(&offsite.prefix, profile);
    let objects = client
        .list_objects_v2(&list_prefix)
        .map_err(|e| BackupError::Offsite {
            op: "list",
            detail: e.to_string(),
        })?;
    let run_ids = complete_remote_run_ids(&objects, &list_prefix);
    match match_exact_remote_run(&run_ids, selector) {
        Ok(id) => Ok(id),
        Err(candidates) if candidates.is_empty() => Err(BackupError::BadArtifact {
            detail: format!(
                "no complete offsite backup matching {selector:?} for profile {profile:?}"
            ),
        }),
        Err(candidates) => Err(BackupError::BadArtifact {
            detail: format!(
                "offsite reference {selector:?} is ambiguous for profile {profile:?}: it matches \
                 {} runs ({}).\n  Re-run restore with the full run id (see `autumn db offsite list`).",
                candidates.len(),
                candidates.join(", "),
            ),
        }),
    }
}

/// Pure selector match (P2 #12): from COMPLETE remote run ids, return the single
/// one referred to by `selector` (a full `<ts>-<token>` id, or a bare `<ts>` that
/// prefixes exactly one tokened id). `Ok(id)` on a unique match; `Err(matches)`
/// otherwise — an empty vec means none, a multi-element vec means ambiguous.
fn match_exact_remote_run(run_ids: &[String], selector: &str) -> Result<String, Vec<String>> {
    let token_prefix = format!("{selector}-");
    let matches: Vec<String> = run_ids
        .iter()
        .filter(|id| id.as_str() == selector || id.starts_with(&token_prefix))
        .cloned()
        .collect();
    if let [only] = matches.as_slice() {
        Ok(only.clone())
    } else {
        Err(matches)
    }
}

/// Resolve the newest run id for a profile from the offsite listing.
fn resolve_latest_run(
    offsite: &ResolvedOffsite,
    client: &S3Client,
    profile: &str,
) -> Result<String, BackupError> {
    let list_prefix = offsite_profile_prefix(&offsite.prefix, profile);
    let objects = client
        .list_objects_v2(&list_prefix)
        .map_err(|e| BackupError::Offsite {
            op: "list",
            detail: e.to_string(),
        })?;
    // Only consider COMPLETE runs (those with a remote manifest.json), so a
    // partial/failed upload never shadows the last good backup.
    complete_remote_run_ids(&objects, &list_prefix)
        .into_iter()
        .next_back() // sorted ascending; newest complete run is last
        .ok_or_else(|| BackupError::BadArtifact {
            detail: format!("no complete offsite backups found for profile {profile:?}"),
        })
}

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

    #[test]
    fn format_parse_accepts_documented_spellings() {
        assert_eq!(BackupFormat::parse("custom").unwrap(), BackupFormat::Custom);
        assert_eq!(BackupFormat::parse("C").unwrap(), BackupFormat::Custom);
        assert_eq!(BackupFormat::parse("plain").unwrap(), BackupFormat::Plain);
        assert_eq!(BackupFormat::parse("sql").unwrap(), BackupFormat::Plain);
        assert!(BackupFormat::parse("gzip").is_err());
    }

    #[test]
    fn default_format_is_custom_compressed() {
        assert_eq!(BackupFormat::default(), BackupFormat::Custom);
        assert_eq!(BackupFormat::Custom.extension(), "dump");
        assert_eq!(BackupFormat::Plain.extension(), "sql");
    }

    #[test]
    fn backup_root_nests_under_profile() {
        assert_eq!(
            backup_root(None, "dev"),
            PathBuf::from("backups").join("dev")
        );
        assert_eq!(
            backup_root(Some(Path::new("/var/backups")), "prod"),
            PathBuf::from("/var/backups").join("prod")
        );
    }

    #[test]
    fn run_dir_name_is_sortable_utc() {
        use chrono::TimeZone as _;
        let ts = chrono::Utc.with_ymd_and_hms(2026, 7, 10, 4, 5, 6).unwrap();
        assert_eq!(run_dir_name(&ts), "20260710T040506Z");
    }

    #[test]
    fn artifact_file_name_distinguishes_control_and_shards() {
        assert_eq!(
            artifact_file_name("control", BackupFormat::Custom),
            "control.dump"
        );
        assert_eq!(
            artifact_file_name("shard:us_east", BackupFormat::Custom),
            "shard-us_east.dump"
        );
        assert_eq!(
            artifact_file_name("shard:us east", BackupFormat::Plain),
            "shard-us_east.sql"
        );
    }

    #[test]
    fn sanitize_component_replaces_unsafe_chars() {
        assert_eq!(sanitize_component("us-east_1.a"), "us-east_1.a");
        assert_eq!(sanitize_component("a/b:c"), "a_b_c");
        assert_eq!(sanitize_component(""), "unnamed");
        assert_eq!(sanitize_component("../etc"), ".._etc");
    }

    #[test]
    fn sanitize_component_rejects_traversal_components() {
        // A normal name is untouched...
        assert_eq!(sanitize_component("shard1"), "shard1");
        // ...but bare `.`, `..`, and empty (which would join to the current or
        // parent directory, escaping the backup root) fall back to "unnamed".
        assert_eq!(sanitize_component("."), "unnamed");
        assert_eq!(sanitize_component(".."), "unnamed");
        assert_eq!(sanitize_component(""), "unnamed");
    }

    #[test]
    fn build_targets_all_puts_control_first_then_shards() {
        let targets = build_targets(
            Some("postgres://localhost/app".to_owned()),
            vec![
                ("east".to_owned(), "postgres://localhost/east".to_owned()),
                ("west".to_owned(), "postgres://localhost/west".to_owned()),
            ],
            &TargetSelector::All,
        )
        .unwrap();
        let labels: Vec<&str> = targets.iter().map(|t| t.label.as_str()).collect();
        assert_eq!(labels, ["control", "shard:east", "shard:west"]);
    }

    #[test]
    fn build_targets_shard_only_shape_is_allowed() {
        let targets = build_targets(
            None,
            vec![("east".to_owned(), "postgres://localhost/east".to_owned())],
            &TargetSelector::All,
        )
        .unwrap();
        assert_eq!(targets.len(), 1);
        assert_eq!(targets[0].label, "shard:east");
    }

    #[test]
    fn build_targets_all_with_no_url_errors() {
        assert!(matches!(
            build_targets(None, vec![], &TargetSelector::All),
            Err(BackupError::NoUrl)
        ));
    }

    #[test]
    fn build_targets_control_only_ignores_shards() {
        let targets = build_targets(
            Some("postgres://localhost/app".to_owned()),
            vec![("east".to_owned(), "postgres://localhost/east".to_owned())],
            &TargetSelector::ControlOnly,
        )
        .unwrap();
        assert_eq!(targets.len(), 1);
        assert_eq!(targets[0].label, "control");
    }

    #[test]
    fn build_targets_named_shard_selects_one() {
        let targets = build_targets(
            Some("postgres://localhost/app".to_owned()),
            vec![
                ("east".to_owned(), "postgres://localhost/east".to_owned()),
                ("west".to_owned(), "postgres://localhost/west".to_owned()),
            ],
            &TargetSelector::Shard("west".to_owned()),
        )
        .unwrap();
        assert_eq!(targets.len(), 1);
        assert_eq!(targets[0].label, "shard:west");
        assert_eq!(targets[0].url, "postgres://localhost/west");
    }

    #[test]
    fn build_targets_unknown_shard_errors_with_known_list() {
        let err = build_targets(
            Some("postgres://localhost/app".to_owned()),
            vec![("east".to_owned(), "postgres://localhost/east".to_owned())],
            &TargetSelector::Shard("nope".to_owned()),
        )
        .unwrap_err();
        match err {
            BackupError::UnknownShard { name, known } => {
                assert_eq!(name, "nope");
                assert_eq!(known, vec!["east".to_owned()]);
            }
            other => panic!("expected UnknownShard, got {other:?}"),
        }
    }

    #[test]
    fn plan_pruning_keeps_newest_n() {
        let runs: Vec<String> = ["a", "b", "c", "d", "e"]
            .iter()
            .map(|s| (*s).to_owned())
            .collect();
        // keep 2 => remove the 3 oldest (a, b, c).
        assert_eq!(
            plan_pruning(&runs, 2),
            vec!["a".to_owned(), "b".to_owned(), "c".to_owned()]
        );
        // keep >= len => remove nothing.
        assert!(plan_pruning(&runs, 5).is_empty());
        assert!(plan_pruning(&runs, 99).is_empty());
    }

    #[test]
    fn list_run_dirs_only_counts_dirs_with_a_manifest() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path();
        // A real backup run: dir with a manifest.
        let good = root.join("20260710T040506Z");
        std::fs::create_dir_all(&good).unwrap();
        std::fs::write(good.join(MANIFEST_FILE), b"{}").unwrap();
        // An unrelated directory with no manifest must be ignored by pruning.
        std::fs::create_dir_all(root.join("notes")).unwrap();
        // A stray file is not a directory.
        std::fs::write(root.join("README.txt"), b"hi").unwrap();

        let dirs = list_run_dirs(root).unwrap();
        assert_eq!(dirs, vec!["20260710T040506Z".to_owned()]);
    }

    #[test]
    fn create_unique_run_dir_never_overwrites_same_timestamp() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("dev");
        let base = "20260710T040506Z";

        // Two backups for the same profile in the same second must NOT collide.
        let first = create_unique_run_dir(&root, base).unwrap();
        // Mark the first run so we can prove it isn't overwritten/reused.
        std::fs::write(first.join(MANIFEST_FILE), b"{}").unwrap();

        let second = create_unique_run_dir(&root, base).unwrap();

        assert_ne!(first, second);
        assert_eq!(first, root.join(base));
        assert_eq!(second, root.join(format!("{base}-2")));
        // The first run's artifact is untouched (no silent overwrite).
        assert!(first.join(MANIFEST_FILE).is_file());
        // The suffixed name still sorts after the base, preserving retention order.
        assert!(base < format!("{base}-2").as_str());

        // A third collision keeps incrementing.
        let third = create_unique_run_dir(&root, base).unwrap();
        assert_eq!(third, root.join(format!("{base}-3")));
    }

    #[test]
    fn plan_pruning_never_removes_everything_on_keep_zero() {
        let runs: Vec<String> = ["a", "b"].iter().map(|s| (*s).to_owned()).collect();
        // keep 0 is clamped to 1 so a just-taken backup survives.
        assert_eq!(plan_pruning(&runs, 0), vec!["a".to_owned()]);
    }

    #[test]
    fn candidate_bin_dirs_orders_override_then_managed() {
        let dirs = candidate_bin_dirs(
            Some(PathBuf::from("/opt/pg/bin")),
            Some(PathBuf::from("/data/app/pg")),
        );
        assert_eq!(dirs[0], PathBuf::from("/opt/pg/bin"));
        // Managed install bin is `<parent>/postgresql/bin`.
        assert!(dirs.contains(&PathBuf::from("/data/app/postgresql/bin")));
    }

    #[test]
    fn candidate_bin_dirs_empty_without_env() {
        assert!(candidate_bin_dirs(None, None).is_empty());
    }

    #[test]
    fn resolve_tool_in_falls_back_to_bare_name() {
        // No candidate dir contains the tool => bare name for PATH lookup.
        assert_eq!(
            resolve_tool_in(&[PathBuf::from("/nonexistent")], "pg_dump"),
            PathBuf::from("pg_dump")
        );
    }

    #[test]
    fn resolve_tool_in_prefers_existing_candidate() {
        let tmp = tempfile::tempdir().unwrap();
        let bin = tmp.path().join("bin");
        std::fs::create_dir_all(&bin).unwrap();
        let exe = bin.join(exe_name("pg_dump"));
        std::fs::write(&exe, b"#!/bin/sh\n").unwrap();
        assert_eq!(resolve_tool_in(std::slice::from_ref(&bin), "pg_dump"), exe);
    }

    #[test]
    fn managed_data_dir_candidate_locates_bundled_tools() {
        // Simulate the managed-daemon layout: the cluster's data dir sits beside
        // the extracted `postgresql/bin` bundle. In the daemon/cron path
        // AUTUMN_MANAGED_PG_DATA_DIR isn't set, so the ONLY way to find these
        // tools is by feeding the runtime-discovered data dir to the locator
        // (issue #1595). Placing fake `pg_dump`/`pg_restore` there must let the
        // locator resolve them by concrete path.
        let tmp = tempfile::tempdir().unwrap();
        let data_dir = tmp.path().join("pgdata");
        std::fs::create_dir_all(&data_dir).unwrap();
        // `candidate_bin_dirs` derives `<data_dir parent>/postgresql/bin`.
        let bundle_bin = tmp.path().join("postgresql").join("bin");
        std::fs::create_dir_all(&bundle_bin).unwrap();
        let pg_dump = bundle_bin.join(exe_name("pg_dump"));
        let pg_restore = bundle_bin.join(exe_name("pg_restore"));
        std::fs::write(&pg_dump, b"#!/bin/sh\n").unwrap();
        std::fs::write(&pg_restore, b"#!/bin/sh\n").unwrap();

        let dirs = candidate_bin_dirs(None, Some(data_dir));
        assert!(dirs.contains(&bundle_bin));
        assert_eq!(resolve_tool_in(&dirs, "pg_dump"), pg_dump);
        assert_eq!(resolve_tool_in(&dirs, "pg_restore"), pg_restore);
    }

    #[cfg(unix)]
    #[test]
    fn verify_artifact_resolves_pg_restore_from_managed_tools() {
        use std::os::unix::fs::PermissionsExt;
        // Regression for the verify path re-locating with a bare `PgTools::locate()`
        // instead of the threaded, managed-data-dir-aware tools (issue #1595). In
        // the daemon/cron layout the bundled `pg_restore` sits in
        // `<data_dir parent>/postgresql/bin` and is NOT on PATH, so it's reachable
        // only through the managed-data-dir-derived candidate dirs. If verify still
        // called `PgTools::locate()`, this archive would be reported as
        // `pg_restore` missing.
        let tmp = tempfile::tempdir().unwrap();
        let bundle_bin = tmp.path().join("postgresql").join("bin");
        std::fs::create_dir_all(&bundle_bin).unwrap();
        // A fake `pg_restore` that prints a one-entry TOC for `--list`; a real or
        // absent binary would reject this synthetic archive (or be missing), so a
        // successful verify proves the bundled binary is the one that ran.
        let pg_restore = bundle_bin.join("pg_restore");
        std::fs::write(
            &pg_restore,
            "#!/bin/sh\necho ';'\necho '215; 1259 16385 TABLE public posts app'\n",
        )
        .unwrap();
        std::fs::set_permissions(&pg_restore, std::fs::Permissions::from_mode(0o755)).unwrap();

        let data_dir = tmp.path().join("pgdata");
        std::fs::create_dir_all(&data_dir).unwrap();
        // Build the locator from the managed data dir exactly as `locate_with_extra`
        // does (via `candidate_bin_dirs`), but without touching process env so the
        // test is hermetic.
        let tools = PgTools::with_dirs(candidate_bin_dirs(None, Some(data_dir)));

        let archive = tmp.path().join("control.dump");
        std::fs::write(&archive, b"PGDMP synthetic archive").unwrap();

        verify_artifact(&archive, BackupFormat::Custom, "app", &tools)
            .expect("verify resolves the bundled pg_restore and accepts the TOC");
    }

    #[test]
    fn toc_entry_count_ignores_comment_lines() {
        let listing = "\
;
; Archive created at 2026-07-10
;
215; 1259 16385 TABLE public posts app
216; 1259 16390 TABLE public comments app
";
        assert_eq!(toc_entry_count(listing), 2);
    }

    #[test]
    fn toc_entry_count_zero_for_header_only() {
        let listing = ";\n; only comments\n;\n";
        assert_eq!(toc_entry_count(listing), 0);
    }

    #[test]
    fn plain_dump_completion_marker_detected() {
        assert!(plain_dump_is_complete(
            "CREATE TABLE x();\n--\n-- PostgreSQL database dump complete\n--\n"
        ));
        assert!(!plain_dump_is_complete("CREATE TABLE x();\n-- truncated"));
    }

    #[test]
    fn verify_plain_dump_reads_only_tail() {
        let tmp = tempfile::tempdir().unwrap();

        // A complete dump whose body is far larger than the 1 KiB tail window
        // still verifies — the marker is on the final line and only the tail is
        // read.
        let complete = tmp.path().join("complete.sql");
        let mut body = "CREATE TABLE x();\n".repeat(4096);
        body.push_str("--\n-- PostgreSQL database dump complete\n--\n");
        std::fs::write(&complete, &body).unwrap();
        assert!(verify_plain_dump(&complete, "db").is_ok());

        // A dump missing the marker is flagged as an integrity failure, even
        // when the body far exceeds the tail window.
        let truncated = tmp.path().join("truncated.sql");
        std::fs::write(&truncated, "CREATE TABLE x();\n".repeat(4096)).unwrap();
        assert!(matches!(
            verify_plain_dump(&truncated, "db"),
            Err(BackupError::IntegrityFailed { .. })
        ));

        // A tiny (sub-1 KiB) complete dump verifies: the read window clamps to
        // the file size instead of seeking before the start.
        let tiny = tmp.path().join("tiny.sql");
        std::fs::write(&tiny, "-- PostgreSQL database dump complete\n").unwrap();
        assert!(verify_plain_dump(&tiny, "db").is_ok());

        // A 0-byte dump can't hold the marker: truncated/incomplete.
        let empty = tmp.path().join("empty.sql");
        std::fs::write(&empty, b"").unwrap();
        assert!(matches!(
            verify_plain_dump(&empty, "db"),
            Err(BackupError::IntegrityFailed { .. })
        ));
    }

    #[test]
    fn pg_dump_args_bake_clean_into_plain_only() {
        let to_str = |args: &[std::ffi::OsString]| -> Vec<String> {
            args.iter()
                .map(|s| s.to_string_lossy().into_owned())
                .collect()
        };

        // Plain dumps carry `--clean --if-exists` so the SQL includes
        // `DROP ... IF EXISTS` and a `psql` restore is idempotent on a populated DB.
        let plain = pg_dump_args(
            BackupFormat::Plain,
            Path::new("/backups/dev/run/control.sql"),
            "postgres://user@db.example.com/my_app",
        );
        let plain = to_str(&plain);
        assert!(plain.contains(&"--clean".to_owned()));
        assert!(plain.contains(&"--if-exists".to_owned()));
        assert!(
            plain
                .windows(2)
                .any(|w| w[0] == "--format" && w[1] == "plain")
        );
        // The dbname URL and file are still passed.
        assert!(
            plain
                .windows(2)
                .any(|w| w[0] == "--dbname" && w[1] == "postgres://user@db.example.com/my_app")
        );

        // Custom dumps deliberately DON'T clean at dump time — `pg_restore
        // --clean --if-exists` handles that at restore time.
        let custom = pg_dump_args(
            BackupFormat::Custom,
            Path::new("/backups/dev/run/control.dump"),
            "postgres://user@db.example.com/my_app",
        );
        let custom = to_str(&custom);
        assert!(!custom.contains(&"--clean".to_owned()));
        assert!(!custom.contains(&"--if-exists".to_owned()));
        assert!(
            custom
                .windows(2)
                .any(|w| w[0] == "--format" && w[1] == "custom")
        );
    }

    #[test]
    fn resolve_control_url_prefers_config_over_managed_pg_fallback() {
        // When config/env resolves a URL, the managed-pg fallback must NOT be
        // consulted (precedence + lazy evaluation).
        let mut fallback_called = false;
        let got = resolve_control_url(Some("postgres://cfg/db".to_owned()), || {
            fallback_called = true;
            Some("postgres://managed/db".to_owned())
        });
        assert_eq!(got.as_deref(), Some("postgres://cfg/db"));
        assert!(
            !fallback_called,
            "managed-pg fallback must not run when config/env resolves a URL"
        );

        // When config/env yields nothing, the managed-pg published URL fills in.
        let got = resolve_control_url(None, || Some("postgres://managed/db".to_owned()));
        assert_eq!(got.as_deref(), Some("postgres://managed/db"));

        // Neither source available => still None (surfaces as NoUrl upstream).
        let got = resolve_control_url(None, || None);
        assert_eq!(got, None);
    }

    #[test]
    fn infer_format_from_extension() {
        assert_eq!(
            infer_format_from_path(Path::new("control.dump")),
            Some(BackupFormat::Custom)
        );
        assert_eq!(
            infer_format_from_path(Path::new("control.sql")),
            Some(BackupFormat::Plain)
        );
        assert_eq!(infer_format_from_path(Path::new("control.txt")), None);
    }

    #[test]
    fn parsed_db_name_is_credential_safe() {
        let name = parsed_db_name("postgres://user:hunter2@db.example.com:6543/my_app");
        assert_eq!(name, "my_app");
        // Never leaks credentials.
        assert!(!name.contains("hunter2"));
        assert_eq!(parsed_db_name("not a url"), "(database)");
    }

    #[test]
    fn split_password_moves_secret_out_of_url() {
        let (safe, pw) = split_password("postgres://user:hunter2@db.example.com:6543/my_app");
        assert_eq!(pw.as_deref(), Some("hunter2"));
        // The returned URL keeps everything but the password.
        assert!(!safe.contains("hunter2"));
        assert!(safe.contains("user@db.example.com"));
        assert!(safe.contains("/my_app"));
    }

    #[test]
    fn split_password_percent_decodes_for_pgpassword() {
        // Raw password contains `@`, `:`, `/`, and a space — all of which MUST be
        // percent-encoded in the URL userinfo. libpq consumes PGPASSWORD as a
        // literal (no percent-decoding), so `split_password` must hand back the
        // DECODED bytes or authentication fails.
        let raw = "p@ss:w/ord x";
        let encoded = "p%40ss%3Aw%2Ford%20x";
        let url = format!("postgres://user:{encoded}@db.example.com:6543/my_app");

        let (safe, pw) = split_password(&url);
        assert_eq!(pw.as_deref(), Some(raw));

        // The password (encoded or decoded) never remains in the argv URL.
        assert!(!safe.contains(encoded));
        assert!(!safe.contains(raw));
        // The username is preserved (libpq decodes it from the URI itself).
        assert!(safe.contains("user@db.example.com"));
        assert!(safe.contains("/my_app"));
        // The password-free URL is still a VALID URL for `--dbname`.
        assert!(url::Url::parse(&safe).is_ok());
    }

    #[test]
    fn split_password_passthrough_when_absent_or_unparseable() {
        let (safe, pw) = split_password("postgres://user@localhost/app");
        assert_eq!(pw, None);
        assert_eq!(safe, "postgres://user@localhost/app");
        // A non-URL that isn't keyword/value form (no `keyword=`) degrades to
        // passing the string through unchanged.
        let (safe, pw) = split_password("not a url");
        assert_eq!(pw, None);
        assert_eq!(safe, "not a url");
    }

    #[test]
    fn split_password_strips_keyword_value_password() {
        // libpq keyword/value form (which Autumn's config validation accepts):
        // the password must move to PGPASSWORD, never staying on argv where `ps`
        // could read it.
        let (safe, pw) = split_password("host=db user=app password=secret dbname=app");
        assert_eq!(pw.as_deref(), Some("secret"));
        assert!(!safe.contains("password"));
        assert!(!safe.contains("secret"));
        // The remaining keywords are preserved for `--dbname`.
        assert!(safe.contains("host=db"));
        assert!(safe.contains("user=app"));
        assert!(safe.contains("dbname=app"));
        // And the stripped connstring still round-trips through the parser.
        let reparsed = parse_libpq_kv(&safe).unwrap();
        assert!(reparsed.iter().all(|(k, _)| k != "password"));
    }

    #[test]
    fn split_password_handles_single_quoted_keyword_value() {
        // A single-quoted value carries spaces and metacharacters; unquoting must
        // yield the literal password for PGPASSWORD.
        let (safe, pw) = split_password("host=db password='p a$s' dbname=app");
        assert_eq!(pw.as_deref(), Some("p a$s"));
        assert!(!safe.contains("p a$s"));
        assert!(!safe.contains("password"));
        assert!(safe.contains("host=db"));
        assert!(safe.contains("dbname=app"));
    }

    #[test]
    fn split_password_handles_escaped_quote_in_keyword_value() {
        // Backslash escapes inside a quoted value: `\'` is a literal quote and
        // `\\` a literal backslash.
        let (_safe, pw) = split_password(r"host=db password='a\'b\\c'");
        assert_eq!(pw.as_deref(), Some(r"a'b\c"));
    }

    #[test]
    fn split_password_keyword_value_without_password_passes_through() {
        // No `password=` token => the connstring is returned byte-for-byte with
        // no PGPASSWORD, so nothing changes for password-less managed clusters.
        let input = "host=db user=app dbname=app";
        let (safe, pw) = split_password(input);
        assert_eq!(pw, None);
        assert_eq!(safe, input);
    }

    #[test]
    fn restore_plan_select_filters_to_named_shard() {
        let plan = RestorePlan {
            dir: PathBuf::from("/x"),
            format: BackupFormat::Custom,
            entries: vec![
                ManifestTarget {
                    label: "control".to_owned(),
                    file: "control.dump".to_owned(),
                    database: "app".to_owned(),
                },
                ManifestTarget {
                    label: "shard:east".to_owned(),
                    file: "shard-east.dump".to_owned(),
                    database: "east".to_owned(),
                },
            ],
        };
        let all = plan.select(None).unwrap();
        assert_eq!(all.len(), 2);
        let one = plan.select(Some("east")).unwrap();
        assert_eq!(one.len(), 1);
        assert_eq!(one[0].label, "shard:east");
        assert!(matches!(
            plan.select(Some("nope")),
            Err(BackupError::UnknownShard { .. })
        ));
    }

    #[test]
    fn single_file_target_label_infers_from_filename_convention() {
        // control.* -> control.
        assert_eq!(
            single_file_target_label("control.dump", None).unwrap(),
            "control"
        );
        assert_eq!(
            single_file_target_label("control.sql", None).unwrap(),
            "control"
        );
        // shard-<name>.* -> that shard (inference is authoritative: a bare shard
        // dump is NEVER silently treated as control).
        assert_eq!(
            single_file_target_label("shard-east.dump", None).unwrap(),
            "shard:east"
        );
        // An explicit --shard wins over the filename.
        assert_eq!(
            single_file_target_label("control.dump", Some("west")).unwrap(),
            "shard:west"
        );
        // A shard-shaped name with no recoverable shard name errors rather than
        // silently defaulting to control.
        assert!(matches!(
            single_file_target_label("shard-.dump", None),
            Err(BackupError::BadArtifact { .. })
        ));
        // A user-renamed file (not produced by the backup writer) falls back to
        // control, preserving the historical single-file behavior without a shard
        // cross-target hazard.
        assert_eq!(
            single_file_target_label("mybackup.dump", None).unwrap(),
            "control"
        );
    }

    #[test]
    fn discover_bare_shard_file_resolves_to_shard_not_control() {
        // Regression: restoring `shard-east.dump` via the single-file path used to
        // label it `control` unconditionally, silently restoring shard data into
        // the control database.
        let tmp = tempfile::tempdir().unwrap();
        let path = tmp.path().join("shard-east.dump");
        std::fs::write(&path, b"x").unwrap();

        let plan = RestorePlan::discover(&path, None).unwrap();
        assert_eq!(plan.entries.len(), 1);
        assert_eq!(plan.entries[0].label, "shard:east");
        // The inferred label still selects cleanly with a matching --shard.
        assert_eq!(plan.select(Some("east")).unwrap()[0].label, "shard:east");
    }

    #[test]
    fn discover_bare_control_file_resolves_to_control() {
        let tmp = tempfile::tempdir().unwrap();
        let path = tmp.path().join("control.dump");
        std::fs::write(&path, b"x").unwrap();

        let plan = RestorePlan::discover(&path, None).unwrap();
        assert_eq!(plan.entries[0].label, "control");
    }

    #[test]
    fn discover_bare_file_honors_explicit_shard_flag() {
        let tmp = tempfile::tempdir().unwrap();
        // Even a control-named bare file is redirected when the operator explicitly
        // names a shard target.
        let path = tmp.path().join("control.dump");
        std::fs::write(&path, b"x").unwrap();

        let plan = RestorePlan::discover(&path, Some("east")).unwrap();
        assert_eq!(plan.entries[0].label, "shard:east");
    }

    #[test]
    fn manifest_round_trips_through_disk() {
        let tmp = tempfile::tempdir().unwrap();
        let manifest = Manifest {
            autumn_version: "0.6.0".to_owned(),
            created_at: "2026-07-10T04:05:06+00:00".to_owned(),
            profile: "dev".to_owned(),
            format: "custom".to_owned(),
            targets: vec![ManifestTarget {
                label: "control".to_owned(),
                file: "control.dump".to_owned(),
                database: "app".to_owned(),
            }],
        };
        write_manifest(tmp.path(), &manifest).unwrap();
        let read = read_manifest(tmp.path()).unwrap();
        assert_eq!(read, manifest);
    }

    #[test]
    fn errors_are_credential_safe() {
        let e = BackupError::ProductionRefused {
            profile: "prod".to_owned(),
        };
        let s = e.to_string();
        assert!(s.contains("prod"));
        assert!(!s.contains("postgres://"));
    }

    // ─── Offsite (issue #1619) ───────────────────────────────────────────────

    #[test]
    fn offsite_object_key_joins_and_skips_empty_prefix() {
        assert_eq!(
            offsite_object_key("db", "prod", "20260710T040506Z", "control.dump"),
            "db/prod/20260710T040506Z/control.dump"
        );
        // Empty prefix => key rooted at the bucket, no leading slash.
        assert_eq!(
            offsite_object_key("", "prod", "20260710T040506Z", "manifest.json"),
            "prod/20260710T040506Z/manifest.json"
        );
    }

    #[test]
    fn normalize_offsite_prefix_trims_slashes() {
        assert_eq!(normalize_offsite_prefix(Some("/db/backups/")), "db/backups");
        assert_eq!(normalize_offsite_prefix(Some("  db  ")), "db");
        assert_eq!(normalize_offsite_prefix(None), "");
        assert_eq!(normalize_offsite_prefix(Some("")), "");
    }

    #[test]
    fn offsite_profile_and_run_prefixes_are_scoped() {
        assert_eq!(offsite_profile_prefix("db", "prod"), "db/prod/");
        assert_eq!(offsite_profile_prefix("", "prod"), "prod/");
        assert_eq!(
            offsite_run_prefix("db", "prod", "20260710T040506Z"),
            "db/prod/20260710T040506Z/"
        );
    }

    #[test]
    fn offsite_key_prefix_is_canonical_across_profile_spellings() {
        // P2 #22: alias/case profile spellings must resolve to the SAME remote key
        // prefix, so upload (`--profile production`), `db offsite list --profile
        // prod`, and `restore offsite:prod/latest` all address `db/prod/…`.
        for spelling in ["prod", "production", "PROD", "Production", "  prod  "] {
            let p = migrate::canonical_profile(spelling);
            assert_eq!(offsite_profile_prefix("db", &p), "db/prod/", "{spelling:?}");
            assert_eq!(
                offsite_run_prefix("db", &p, "r"),
                "db/prod/r/",
                "{spelling:?}"
            );
            assert_eq!(
                offsite_object_key("db", &p, "r", "manifest.json"),
                "db/prod/r/manifest.json",
                "{spelling:?}"
            );
        }
        // `prod` and `production` yield the identical prefix.
        assert_eq!(
            offsite_profile_prefix("db", &migrate::canonical_profile("prod")),
            offsite_profile_prefix("db", &migrate::canonical_profile("production")),
        );
        // dev aliases collapse the same way.
        assert_eq!(
            offsite_profile_prefix("db", &migrate::canonical_profile("development")),
            "db/dev/"
        );
    }

    #[test]
    fn destinations_conflict_requires_same_bucket_and_endpoint() {
        // Same bucket + same endpoint => conflict.
        assert!(destinations_conflict(
            "shared",
            Some("https://s3.example.test"),
            Some("shared"),
            Some("https://s3.example.test/"),
        ));
        // Same bucket but different endpoint => distinct.
        assert!(!destinations_conflict(
            "shared",
            Some("https://offsite.test"),
            Some("shared"),
            Some("https://app.test"),
        ));
        // Different bucket => distinct.
        assert!(!destinations_conflict(
            "offsite",
            Some("https://s3.example.test"),
            Some("app"),
            Some("https://s3.example.test"),
        ));
        // App storage not configured (no bucket) => never a conflict.
        assert!(!destinations_conflict("offsite", None, None, None));
        // Both AWS default endpoint (None) + same bucket => conflict.
        assert!(destinations_conflict("shared", None, Some("shared"), None));
    }

    #[test]
    fn destinations_conflict_normalizes_ports_and_aws_defaults() {
        // `:443` vs bare host on https must compare equal (default port dropped).
        assert!(destinations_conflict(
            "shared",
            Some("https://minio.example:443"),
            Some("shared"),
            Some("https://minio.example"),
        ));
        // `:80` vs bare host on http likewise.
        assert!(destinations_conflict(
            "shared",
            Some("http://gw:80"),
            Some("shared"),
            Some("http://gw"),
        ));
        // Offsite None (AWS default) vs app spelled as the explicit regional AWS
        // URL, same bucket => still a shared bucket (globally-unique name).
        assert!(destinations_conflict(
            "shared",
            None,
            Some("shared"),
            Some("https://s3.us-east-1.amazonaws.com"),
        ));
        // ...and the virtual-hosted AWS spelling too.
        assert!(destinations_conflict(
            "shared",
            None,
            Some("shared"),
            Some("https://shared.s3.us-east-1.amazonaws.com"),
        ));
        // A non-default port genuinely differs => distinct.
        assert!(!destinations_conflict(
            "shared",
            Some("https://minio.example:9000"),
            Some("shared"),
            Some("https://minio.example"),
        ));
        // Virtual-hosted `{bucket}.` prefix vs path-style same host => equal.
        assert!(destinations_conflict(
            "shared",
            Some("https://shared.minio.example"),
            Some("shared"),
            Some("https://minio.example"),
        ));
    }

    #[test]
    fn shared_bucket_refusal_message_is_credential_safe() {
        let e = BackupError::SharedBucketRefused {
            bucket: "shared".to_owned(),
        };
        let s = e.to_string();
        assert!(s.contains("shared"));
        assert!(s.contains("allow_shared_bucket"));
        assert!(!s.contains("secret"));
    }

    #[test]
    fn missing_credential_env_names_only_the_variable() {
        let e = BackupError::MissingCredentialEnv {
            var: "OFFSITE_SECRET".to_owned(),
        };
        let s = e.to_string();
        assert!(s.contains("OFFSITE_SECRET"));
        // Names the variable, never a value.
        assert!(!s.contains("hunter2"));
    }

    #[test]
    fn upload_failed_reports_split_outcome() {
        let e = BackupError::OffsiteUploadFailed {
            local_path: "/backups/prod/20260710T040506Z".to_owned(),
            detail: "S3 put returned HTTP 403 (AccessDenied)".to_owned(),
        };
        let s = e.to_string();
        assert!(s.contains("Local backup OK at /backups/prod/20260710T040506Z"));
        assert!(s.contains("OFFSITE UPLOAD FAILED"));
        assert!(s.contains("intact"));
    }

    // ── #1743: failed offsite upload raises an operator alert ─────────────────

    #[derive(Default)]
    struct CapturingChannel {
        received: Arc<std::sync::Mutex<Vec<Alert>>>,
    }

    impl AlertChannel for CapturingChannel {
        fn name(&self) -> &'static str {
            "capturing"
        }
        fn deliver<'a>(&'a self, alert: &'a Alert) -> autumn_web::alerts::AlertDeliveryFuture<'a> {
            let received = Arc::clone(&self.received);
            let cloned = alert.clone();
            Box::pin(async move {
                received.lock().expect("lock").push(cloned);
                Ok(())
            })
        }
    }

    #[test]
    fn build_offsite_upload_alert_is_scheduled_task_failure() {
        let alert = build_offsite_upload_alert(
            "/backups/prod/20260710T040506Z",
            "S3 put returned HTTP 403 (AccessDenied)",
        );
        assert_eq!(alert.condition, AlertCondition::ScheduledTaskFailure);
        assert_eq!(alert.title, "Offsite backup upload failed");
        assert_eq!(alert.summary, "S3 put returned HTTP 403 (AccessDenied)");
        assert_eq!(
            alert.dedup_key,
            "scheduled_task_failure:db-backup-offsite-upload"
        );
    }

    #[test]
    fn deliver_offsite_upload_alert_reaches_configured_channel() {
        // Failure-injection: the upload-failure path delivers a
        // ScheduledTaskFailure alert to every configured channel. A capturing
        // mock channel records what a failed upload would raise.
        let capture = Arc::new(std::sync::Mutex::new(Vec::new()));
        let channel = Arc::new(CapturingChannel {
            received: Arc::clone(&capture),
        });
        let channels: Vec<Arc<dyn AlertChannel>> = vec![channel];

        deliver_offsite_upload_alert(
            &channels,
            "/backups/prod/20260710T040506Z",
            "S3 put returned HTTP 403 (AccessDenied)",
        );

        let delivered = {
            let received = capture.lock().expect("lock");
            received.clone()
        };
        assert_eq!(delivered.len(), 1, "exactly one alert must be delivered");
        let alert = &delivered[0];
        assert_eq!(alert.condition, AlertCondition::ScheduledTaskFailure);
        assert_eq!(alert.title, "Offsite backup upload failed");
        assert_eq!(alert.summary, "S3 put returned HTTP 403 (AccessDenied)");
    }

    #[test]
    fn deliver_offsite_upload_alert_is_noop_without_channels() {
        // Interactive case (no [alerts] configured → no channels): must be a
        // no-op so behavior is unchanged. No panic, nothing delivered.
        deliver_offsite_upload_alert(&[], "/backups/prod/run", "boom");
    }

    #[test]
    fn remote_run_ids_extracts_unique_sorted_ids() {
        let list_prefix = "db/prod/";
        let objects = vec![
            s3::S3Object {
                key: "db/prod/20260710T010101Z/control.dump".to_owned(),
                size: 1,
            },
            s3::S3Object {
                key: "db/prod/20260710T010101Z/manifest.json".to_owned(),
                size: 1,
            },
            s3::S3Object {
                key: "db/prod/20260709T010101Z/control.dump".to_owned(),
                size: 1,
            },
        ];
        assert_eq!(
            remote_run_ids(&objects, list_prefix),
            vec!["20260709T010101Z".to_owned(), "20260710T010101Z".to_owned()]
        );
    }

    #[test]
    fn local_backup_does_not_validate_incomplete_offsite() {
        // P2 #1: an incomplete [backup.offsite] (no bucket) must NOT block a plain
        // local `autumn db backup`. The strict bucket check lives in resolve(),
        // which the backup path only calls when an upload is actually requested.
        let offsite = autumn_web::config::OffsiteBackupConfig::default();
        assert!(offsite.s3.bucket.is_none(), "precondition: no bucket set");
        let loaded = LoadedOffsite {
            offsite,
            app_storage_bucket: None,
            app_storage_endpoint: None,
            profile: "test".to_owned(),
        };
        // The strict validation is deferred to resolve() (only reached when
        // uploading) — proving a local-only backup never hits it.
        assert!(matches!(
            loaded.resolve(),
            Err(BackupError::OffsiteConfig { .. })
        ));
    }

    #[test]
    fn auto_upload_probe_precedence_and_repro() {
        // P2 #8: the upload decision reads ONLY `[backup.offsite].auto_upload`
        // from env / .env / merged TOML — never a full config load / credential
        // decryption. Pure `auto_upload_from_sources` proves the precedence.
        let no_env = |_: &str| None;

        // (a) REPRO: no offsite section, no env => false => NO upload, and (by
        // construction) the decision needed no AutumnConfig load / cred decrypt.
        assert!(!auto_upload_from_sources(no_env, None));
        let empty: toml::Table = toml::Table::new();
        assert!(!auto_upload_from_sources(no_env, Some(&empty)));

        // (b) auto_upload=true via TOML [backup.offsite] triggers upload.
        let toml_true: toml::Table =
            toml::from_str("[backup.offsite]\nauto_upload = true\n").unwrap();
        assert!(auto_upload_from_sources(no_env, Some(&toml_true)));

        // (b) auto_upload=true via env (real env or .env.<profile>) triggers it,
        // even with no TOML.
        let env_true =
            |k: &str| (k == "AUTUMN_BACKUP__OFFSITE__AUTO_UPLOAD").then(|| "true".to_owned());
        assert!(auto_upload_from_sources(env_true, None));

        // Env wins over TOML: env "false" overrides TOML true.
        let env_false =
            |k: &str| (k == "AUTUMN_BACKUP__OFFSITE__AUTO_UPLOAD").then(|| "false".to_owned());
        assert!(!auto_upload_from_sources(env_false, Some(&toml_true)));

        // A non-boolean env value is ignored, falling back to TOML.
        let env_junk =
            |k: &str| (k == "AUTUMN_BACKUP__OFFSITE__AUTO_UPLOAD").then(|| "yesplease".to_owned());
        assert!(auto_upload_from_sources(env_junk, Some(&toml_true)));
    }

    #[test]
    fn offsite_resolves_from_config_dir_table_not_cwd() {
        // P2 #15: an installed/daemon launch keeps autumn.toml in the config dir
        // (a different CWD). Reading from that dir — as the offsite loaders now do
        // via read_autumn_toml_table_with_profile_from_config_dir — must surface
        // both auto_upload and the offsite destination. Proven hermetically by
        // reading a temp config dir directly (this test's CWD has no such file).
        let tmp = tempfile::tempdir().unwrap();
        std::fs::write(
            tmp.path().join("autumn.toml"),
            "[backup.offsite]\n\
             auto_upload = true\n\
             prefix = \"db\"\n\
             [backup.offsite.s3]\n\
             bucket = \"offsite-bucket\"\n\
             region = \"us-east-1\"\n\
             endpoint = \"https://minio.example:9000\"\n\
             force_path_style = true\n\
             access_key_id_env = \"OFFSITE_KEY\"\n\
             secret_access_key_env = \"OFFSITE_SECRET\"\n",
        )
        .unwrap();
        let table = migrate::read_autumn_toml_table_with_profile_in(tmp.path(), Some("dev"));
        // auto_upload is read from the config-dir table (env has nothing).
        assert!(auto_upload_from_sources(|_| None, table.as_ref()));
        // And the offsite destination resolves from that same table (no CWD file).
        let env = autumn_web::config::MockEnv::new();
        let loaded = resolve_loaded_offsite(table.as_ref(), &env, "dev").unwrap();
        assert!(
            loaded.is_some(),
            "offsite must resolve from the config-dir autumn.toml"
        );
    }

    #[test]
    fn resolved_offsite_carries_target_profile_for_credentials() {
        // P2 #6: the target profile threads through resolve() into
        // ResolvedOffsite so credential resolution reads the SAME
        // `.env.<profile>` overlay the config was loaded under.
        let mut offsite = autumn_web::config::OffsiteBackupConfig::default();
        offsite.s3.bucket = Some("offsite-bucket".to_owned());
        offsite.s3.access_key_id_env = Some("OFFSITE_KEY".to_owned());
        offsite.s3.secret_access_key_env = Some("OFFSITE_SECRET".to_owned());
        let loaded = LoadedOffsite {
            offsite,
            app_storage_bucket: None,
            app_storage_endpoint: None,
            profile: "test".to_owned(),
        };
        let resolved = loaded.resolve().expect("bucket set, resolves");
        assert_eq!(resolved.profile, "test");
    }

    #[test]
    fn offsite_resolves_without_app_credential_store() {
        // P2 #9: offsite destination resolves from TOML + env WITHOUT a full
        // AutumnConfig::load — so NO credential decryption / AUTUMN_MASTER_KEY is
        // needed. On current code this path went through load_with_env, which
        // would fail on a DR box whose config/credentials/<p>.toml.enc can't be
        // decrypted. `resolve_loaded_offsite` reads only the offsite + storage.s3
        // config, never the credential store.
        let table: toml::Table = toml::from_str(
            "[backup.offsite]\nprefix = \"db\"\n\
             [backup.offsite.s3]\nbucket = \"dr-bucket\"\nregion = \"us-east-1\"\n\
             endpoint = \"https://minio.example:9000\"\nforce_path_style = true\n\
             access_key_id_env = \"OFFSITE_KEY\"\nsecret_access_key_env = \"OFFSITE_SECRET\"\n",
        )
        .unwrap();
        // Real env supplies the S3 credential VALUES; crucially there is NO
        // AUTUMN_MASTER_KEY and no attempt to read a credential store.
        let env = autumn_web::config::MockEnv::new()
            .with("OFFSITE_KEY", "AKIA_DR")
            .with("OFFSITE_SECRET", "shh");
        let loaded = resolve_loaded_offsite(Some(&table), &env, "prod")
            .expect("resolves without a credential store")
            .expect("offsite section present");
        let resolved = loaded.resolve().expect("bucket set");
        assert_eq!(resolved.s3.bucket, "dr-bucket");
        assert_eq!(resolved.profile, "prod");

        // Env-only configuration (no TOML at all) also resolves.
        let env2 = autumn_web::config::MockEnv::new()
            .with("AUTUMN_BACKUP__OFFSITE__S3__BUCKET", "env-bucket");
        let loaded2 = resolve_loaded_offsite(None, &env2, "prod")
            .unwrap()
            .expect("materialized from env");
        assert_eq!(loaded2.offsite.s3.bucket.as_deref(), Some("env-bucket"));
    }

    #[test]
    fn shared_bucket_guard_only_applies_when_storage_backend_is_s3() {
        // P2 #16: a leftover `[storage.s3]` bucket that equals the offsite bucket
        // must NOT trip the shared-bucket guard unless the app storage backend is
        // actually S3. On backend=local/disabled the bucket is inert.
        let env = autumn_web::config::MockEnv::new();
        let make = |backend: &str| -> LoadedOffsite {
            let table: toml::Table = toml::from_str(&format!(
                "[storage]\nbackend = \"{backend}\"\n\
                 [storage.s3]\nbucket = \"shared\"\nendpoint = \"https://minio.example:9000\"\n\
                 [backup.offsite]\nprefix = \"db\"\n\
                 [backup.offsite.s3]\nbucket = \"shared\"\nregion = \"us-east-1\"\n\
                 endpoint = \"https://minio.example:9000\"\nforce_path_style = true\n"
            ))
            .unwrap();
            resolve_loaded_offsite(Some(&table), &env, "prod")
                .expect("resolves")
                .expect("offsite section present")
        };

        // backend=local: the s3 bucket is NOT the app storage destination, so the
        // guard input is empty and destinations_conflict cannot fire.
        let local = make("local");
        assert_eq!(local.app_storage_bucket, None);
        assert!(!destinations_conflict(
            "shared",
            Some("https://minio.example:9000"),
            local.app_storage_bucket.as_deref(),
            local.app_storage_endpoint.as_deref(),
        ));

        // backend=disabled: same — inert `[storage.s3]`.
        assert_eq!(make("disabled").app_storage_bucket, None);

        // backend=s3: the bucket IS the app destination, so the guard fires and
        // the offsite backup must opt in with allow_shared_bucket.
        let s3 = make("s3");
        assert_eq!(s3.app_storage_bucket.as_deref(), Some("shared"));
        assert!(destinations_conflict(
            "shared",
            Some("https://minio.example:9000"),
            s3.app_storage_bucket.as_deref(),
            s3.app_storage_endpoint.as_deref(),
        ));
    }

    #[test]
    fn key_is_manifest_matches_only_the_run_manifest() {
        assert!(key_is_manifest(
            "db/prod/20260710T040506Z-abcd1234/manifest.json"
        ));
        assert!(!key_is_manifest(
            "db/prod/20260710T040506Z-abcd1234/control.dump"
        ));
        // A file that merely contains the word is not the manifest.
        assert!(!key_is_manifest("db/prod/r/not-manifest.json.dump"));
    }

    #[test]
    fn order_cleanup_keys_deletes_manifest_first() {
        // P2 #21: on a failed upload the run manifest must be removed FIRST so the
        // completeness marker is gone even if a later delete fails — otherwise a
        // failed run could still be counted `complete` for latest/retention.
        let written = vec![
            "db/prod/r/control.dump".to_owned(),
            "db/prod/r/shard-a.dump".to_owned(),
            "db/prod/r/manifest.json".to_owned(),
        ];
        let ordered = order_cleanup_keys(&written);
        assert_eq!(ordered[0], "db/prod/r/manifest.json");
        // The remaining (non-manifest) keys keep their written order.
        assert_eq!(
            &ordered[1..],
            &[
                "db/prod/r/control.dump".to_owned(),
                "db/prod/r/shard-a.dump".to_owned()
            ]
        );

        // And downstream: with the manifest deleted, complete_remote_run_ids no
        // longer counts the run — so the failed run can't become `latest`.
        let list_prefix = "db/prod/";
        let after_cleanup = vec![s3::S3Object {
            key: "db/prod/r/control.dump".to_owned(), // manifest was cleaned up
            size: 10,
        }];
        assert!(complete_remote_run_ids(&after_cleanup, list_prefix).is_empty());
    }

    #[test]
    fn complete_remote_run_ids_only_counts_runs_with_a_manifest() {
        // P2 #2: a newer PARTIAL run (artifacts but no manifest.json) and an
        // older COMPLETE run. `latest` must pick the older complete run.
        let list_prefix = "db/prod/";
        let objects = vec![
            // Older, complete run.
            s3::S3Object {
                key: "db/prod/20260709T010101Z/control.dump".to_owned(),
                size: 10,
            },
            s3::S3Object {
                key: "db/prod/20260709T010101Z/manifest.json".to_owned(),
                size: 1,
            },
            // Newer, partial run — died before uploading the (last) manifest.
            s3::S3Object {
                key: "db/prod/20260710T010101Z/control.dump".to_owned(),
                size: 10,
            },
        ];
        // The raw run ids include the partial newer run...
        assert_eq!(
            remote_run_ids(&objects, list_prefix),
            vec!["20260709T010101Z".to_owned(), "20260710T010101Z".to_owned()]
        );
        // ...but only the complete (older) run is a candidate.
        let complete = complete_remote_run_ids(&objects, list_prefix);
        assert_eq!(complete, vec!["20260709T010101Z".to_owned()]);
        // `latest` = newest COMPLETE run = the older run, not the partial newer one.
        assert_eq!(
            complete.into_iter().next_back().as_deref(),
            Some("20260709T010101Z")
        );
    }

    #[test]
    fn remote_retention_counts_only_complete_runs() {
        // P2 #3 (Codex scenario): keep=2 with complete runs A/B, a NEWER partial
        // run C (no manifest), and the just-uploaded complete run D. Retention
        // must count only complete runs — keep the newest 2 complete (D, B),
        // prune the older complete A, and NEVER touch the partial C or D.
        let list_prefix = "db/prod/";
        let a = "20260701T000000Z"; // complete, oldest
        let b = "20260702T000000Z"; // complete
        let c = "20260703T000000Z"; // PARTIAL (no manifest), newer than B
        let d = "20260704T000000Z"; // complete, just uploaded (newest)
        let objects = vec![
            s3::S3Object {
                key: format!("{list_prefix}{a}/control.dump"),
                size: 1,
            },
            s3::S3Object {
                key: format!("{list_prefix}{a}/manifest.json"),
                size: 1,
            },
            s3::S3Object {
                key: format!("{list_prefix}{b}/control.dump"),
                size: 1,
            },
            s3::S3Object {
                key: format!("{list_prefix}{b}/manifest.json"),
                size: 1,
            },
            // Partial C: artifact only, no manifest.
            s3::S3Object {
                key: format!("{list_prefix}{c}/control.dump"),
                size: 1,
            },
            s3::S3Object {
                key: format!("{list_prefix}{d}/control.dump"),
                size: 1,
            },
            s3::S3Object {
                key: format!("{list_prefix}{d}/manifest.json"),
                size: 1,
            },
        ];

        // Retention input = complete runs only (A, B, D) — the partial C is absent.
        let complete = complete_remote_run_ids(&objects, list_prefix);
        assert_eq!(
            complete,
            vec![a.to_owned(), b.to_owned(), d.to_owned()],
            "partial run C must not count as a retention slot"
        );

        // keep=2, just-uploaded=D => prune only the oldest complete run A.
        let to_remove = plan_remote_pruning(&complete, 2, d);
        assert_eq!(to_remove, vec![a.to_owned()]);
        // The partial C is NOT pruned (never a candidate), and D is never pruned.
        assert!(!to_remove.contains(&c.to_owned()));
        assert!(!to_remove.contains(&d.to_owned()));
        // B survives (it's within the newest-2 complete).
        assert!(!to_remove.contains(&b.to_owned()));
    }

    #[test]
    fn upload_orders_manifest_last() {
        // P2 #2(a): the manifest must upload last so its remote presence marks a
        // complete run. Mirror upload_run's ordering.
        let mut files = vec![
            "manifest.json".to_owned(),
            "shard-east.dump".to_owned(),
            "control.dump".to_owned(),
        ];
        files.sort();
        files.sort_by_key(|f| f.as_str() == MANIFEST_FILE);
        assert_eq!(
            files,
            vec![
                "control.dump".to_owned(),
                "shard-east.dump".to_owned(),
                "manifest.json".to_owned(),
            ]
        );
    }

    #[test]
    fn offsite_listing_flags_incomplete_runs() {
        // P2 #2: an incomplete run (no manifest) is flagged in `db offsite list`.
        let list_prefix = "db/prod/";
        let objects = vec![s3::S3Object {
            key: "db/prod/20260710T010101Z/control.dump".to_owned(),
            size: 10,
        }];
        let runs = group_offsite_objects(&objects, list_prefix);
        assert_eq!(runs.len(), 1);
        assert!(!runs[0].complete);
        let table = format_offsite_listing(&runs);
        assert!(table.contains("INCOMPLETE"), "table was: {table}");
    }

    #[test]
    fn is_safe_leaf_name_rejects_separators_and_traversal() {
        // P2 #13: a downloaded offsite object name must be a single plain file.
        for good in ["control.dump", "manifest.json", "shard-orders.dump"] {
            assert!(is_safe_leaf_name(good), "{good:?} should be accepted");
        }
        for bad in [
            "",
            "a/b",
            "a\\b",
            "..\\..\\evil",
            "../evil",
            "C:\\evil",
            "..",
            ".",
            "/abs",
            "sub/manifest.json",
        ] {
            assert!(!is_safe_leaf_name(bad), "{bad:?} should be rejected");
        }
    }

    #[test]
    fn remote_run_token_is_eight_lowercase_hex() {
        // P2 #12: the token is 8 hex chars derived from host/pid/time entropy.
        let token = remote_run_token();
        assert_eq!(token.len(), 8, "token was {token:?}");
        assert!(
            token
                .bytes()
                .all(|b| b.is_ascii_hexdigit() && !b.is_ascii_uppercase()),
            "token must be lowercase hex: {token:?}"
        );
    }

    #[test]
    fn remote_run_id_appends_token_to_local_id() {
        // P2 #12: remote id = <local timestamp>-<token>; the timestamp still leads
        // so lexical sort (and thus `latest`) is unchanged.
        let id = remote_run_id("20260710T040506Z", "deadbeef");
        assert_eq!(id, "20260710T040506Z-deadbeef");
        assert!(id.starts_with("20260710T040506Z"));
    }

    #[test]
    fn match_exact_remote_run_resolves_by_full_id_and_timestamp() {
        // P2 #12: complete remote run ids carry a token; a selector matches by
        // full id OR by the bare timestamp when exactly one tokened id begins with it.
        let ids: Vec<String> = ["20260709T010101Z-aaaa1111", "20260710T040506Z-bbbb2222"]
            .iter()
            .map(|s| (*s).to_owned())
            .collect();
        // Full id → itself.
        assert_eq!(
            match_exact_remote_run(&ids, "20260710T040506Z-bbbb2222").unwrap(),
            "20260710T040506Z-bbbb2222"
        );
        // Bare timestamp uniquely prefixes one run → that run.
        assert_eq!(
            match_exact_remote_run(&ids, "20260710T040506Z").unwrap(),
            "20260710T040506Z-bbbb2222"
        );
        // A timestamp with no matching run → Err(empty).
        assert_eq!(
            match_exact_remote_run(&ids, "20990101T000000Z").unwrap_err(),
            Vec::<String>::new()
        );
    }

    #[test]
    fn match_exact_remote_run_errors_on_ambiguous_same_second() {
        // Two hosts backed up the SAME second → two tokened ids share the <ts>.
        // A bare-timestamp selector is ambiguous and must list both candidates.
        let ids: Vec<String> = ["20260710T040506Z-aaaa1111", "20260710T040506Z-bbbb2222"]
            .iter()
            .map(|s| (*s).to_owned())
            .collect();
        let err = match_exact_remote_run(&ids, "20260710T040506Z").unwrap_err();
        assert_eq!(err.len(), 2);
        assert!(err.contains(&"20260710T040506Z-aaaa1111".to_owned()));
        assert!(err.contains(&"20260710T040506Z-bbbb2222".to_owned()));
        // But the full id still resolves unambiguously.
        assert_eq!(
            match_exact_remote_run(&ids, "20260710T040506Z-aaaa1111").unwrap(),
            "20260710T040506Z-aaaa1111"
        );
    }

    #[test]
    fn complete_remote_run_ids_sort_newest_last_with_tokens() {
        // P2 #12: the <ts> dominates the lexical sort, so `latest` (newest) is the
        // last element regardless of the token tiebreak.
        let list_prefix = "db/prod/";
        let objects = vec![
            s3::S3Object {
                key: "db/prod/20260710T040506Z-zzzz9999/manifest.json".to_owned(),
                size: 1,
            },
            s3::S3Object {
                key: "db/prod/20260711T040506Z-aaaa0000/manifest.json".to_owned(),
                size: 1,
            },
        ];
        let ids = complete_remote_run_ids(&objects, list_prefix);
        assert_eq!(ids.last().unwrap(), "20260711T040506Z-aaaa0000");
    }

    #[test]
    fn plan_remote_pruning_keeps_newest_and_never_the_just_uploaded() {
        let runs: Vec<String> = ["r1", "r2", "r3", "r4", "r5"]
            .iter()
            .map(|s| (*s).to_owned())
            .collect();
        // Keep 2 => remove r1, r2, r3 (the 3 oldest). r5 is the just-uploaded.
        assert_eq!(
            plan_remote_pruning(&runs, 2, "r5"),
            vec!["r1".to_owned(), "r2".to_owned(), "r3".to_owned()]
        );
        // keep >= len => remove nothing.
        assert!(plan_remote_pruning(&runs, 5, "r5").is_empty());
        // keep 0 clamps to 1 (never wipe everything).
        assert_eq!(plan_remote_pruning(&runs, 0, "r5").len(), 4);
    }

    #[test]
    fn plan_remote_pruning_excludes_just_uploaded_even_if_oldest() {
        // Pathological: the just-uploaded run id sorts oldest (clock skew). It
        // must STILL never be pruned.
        let runs: Vec<String> = ["a", "b", "c"].iter().map(|s| (*s).to_owned()).collect();
        // keep 1 => would remove a, b; but "a" is the just-uploaded => keep it.
        assert_eq!(plan_remote_pruning(&runs, 1, "a"), vec!["b".to_owned()]);
    }

    #[test]
    fn parse_offsite_ref_handles_latest_and_explicit() {
        let latest = parse_offsite_ref("offsite:prod/latest").unwrap();
        assert_eq!(latest.profile, "prod");
        assert_eq!(latest.selector, OffsiteSelector::Latest);

        // Case-insensitive "latest".
        assert_eq!(
            parse_offsite_ref("prod/LATEST").unwrap().selector,
            OffsiteSelector::Latest
        );

        let exact = parse_offsite_ref("offsite:prod/20260710T040506Z").unwrap();
        assert_eq!(exact.profile, "prod");
        assert_eq!(
            exact.selector,
            OffsiteSelector::Exact("20260710T040506Z".to_owned())
        );

        // Missing pieces => None.
        assert!(parse_offsite_ref("offsite:prod").is_none());
        assert!(parse_offsite_ref("offsite:/latest").is_none());
        assert!(parse_offsite_ref("prod/").is_none());
    }

    #[test]
    fn resolve_offsite_ref_routes_local_only_when_not_indicated() {
        // (c) A bare local path with neither flag nor prefix routes to LOCAL
        // restore (None), unchanged.
        assert!(resolve_offsite_ref(false, "backups/prod/20260710T040506Z").is_none());
        assert!(resolve_offsite_ref(false, "latest").is_none());

        // (d) Well-formed offsite refs parse (Some(Ok(_))).
        let ok = resolve_offsite_ref(false, "offsite:prod/latest").unwrap();
        assert_eq!(
            ok.unwrap(),
            OffsiteRef {
                profile: "prod".to_owned(),
                selector: OffsiteSelector::Latest,
            }
        );
        let ok = resolve_offsite_ref(true, "prod/20260710T040506Z").unwrap();
        assert_eq!(
            ok.unwrap(),
            OffsiteRef {
                profile: "prod".to_owned(),
                selector: OffsiteSelector::Exact("20260710T040506Z".to_owned()),
            }
        );
    }

    #[test]
    fn resolve_offsite_ref_errors_on_malformed_when_indicated() {
        // (a) `--offsite` with a malformed ref (no `<profile>/`) must ERROR, not
        // fall through to a local artifact named `latest`.
        let err = resolve_offsite_ref(true, "latest").unwrap().unwrap_err();
        assert!(matches!(err, BackupError::InvalidOffsiteRef { .. }));
        let msg = err.to_string();
        assert!(msg.contains("latest"));
        assert!(msg.contains("offsite:<profile>/<timestamp|latest>"));

        // (b) `offsite:` prefix with a malformed body must ERROR too.
        assert!(matches!(
            resolve_offsite_ref(false, "offsite:prod").unwrap(),
            Err(BackupError::InvalidOffsiteRef { .. })
        ));
        assert!(matches!(
            resolve_offsite_ref(false, "offsite:/latest").unwrap(),
            Err(BackupError::InvalidOffsiteRef { .. })
        ));
        // Flag set but empty positional => still an offsite error, not local.
        assert!(matches!(
            resolve_offsite_ref(true, "").unwrap(),
            Err(BackupError::InvalidOffsiteRef { .. })
        ));
    }

    #[test]
    fn group_and_format_offsite_listing() {
        let list_prefix = "db/prod/";
        let objects = vec![
            s3::S3Object {
                key: "db/prod/20260710T040506Z/manifest.json".to_owned(),
                size: 128,
            },
            s3::S3Object {
                key: "db/prod/20260710T040506Z/control.dump".to_owned(),
                size: 4096,
            },
            s3::S3Object {
                key: "db/prod/20260709T040506Z/control.dump".to_owned(),
                size: 2048,
            },
        ];
        let runs = group_offsite_objects(&objects, list_prefix);
        assert_eq!(runs.len(), 2);
        // Sorted ascending by run id.
        assert_eq!(runs[0].run_id, "20260709T040506Z");
        assert_eq!(runs[1].run_id, "20260710T040506Z");
        assert_eq!(runs[1].total, 128 + 4096);

        let table = format_offsite_listing(&runs);
        assert!(table.contains("RUN ID"));
        assert!(table.contains("20260710T040506Z"));
        assert!(table.contains("control.dump"));
        assert!(table.contains("manifest.json"));
    }

    #[test]
    fn human_size_scales_units() {
        assert_eq!(human_size(512), "512 B");
        assert_eq!(human_size(2048), "2.0 KiB");
        assert_eq!(human_size(5 * 1024 * 1024), "5.0 MiB");
    }

    /// Docker/live-DB round-trip (AC #5). Ignored by default; run with a live
    /// Postgres and the pg client tools available:
    ///
    /// ```text
    /// DATABASE_URL=postgres://postgres:postgres@localhost:5432/autumn_backup_it \
    ///   cargo test -p autumn-cli --lib -- --ignored backup_restore_round_trip
    /// ```
    ///
    /// Proves seed → backup → drop rows → restore → row-level equality.
    #[test]
    #[ignore = "requires a live Postgres and pg_dump/pg_restore on PATH"]
    fn backup_restore_round_trip() {
        use diesel::connection::SimpleConnection as _;
        use diesel::{Connection as _, PgConnection, RunQueryDsl as _, sql_query};

        #[derive(diesel::QueryableByName)]
        struct Count {
            #[diesel(sql_type = diesel::sql_types::BigInt)]
            n: i64,
        }
        #[derive(diesel::QueryableByName)]
        struct Rt {
            #[diesel(sql_type = diesel::sql_types::Integer)]
            id: i32,
            #[diesel(sql_type = diesel::sql_types::Text)]
            name: String,
        }

        let Ok(url) = std::env::var("DATABASE_URL") else {
            eprintln!("DATABASE_URL not set; skipping round-trip");
            return;
        };

        // Seed a table with known rows.
        let mut conn = PgConnection::establish(&url).expect("connect");
        conn.batch_execute(
            "DROP TABLE IF EXISTS backup_rt; \
             CREATE TABLE backup_rt (id INT PRIMARY KEY, name TEXT NOT NULL); \
             INSERT INTO backup_rt VALUES (1,'alpha'),(2,'beta'),(3,'gamma');",
        )
        .expect("seed");

        // Backup (custom format) into a temp dir.
        let tmp = tempfile::tempdir().unwrap();
        let tools = PgTools::locate();
        let pg_dump = tools.require("pg_dump").expect("pg_dump present");
        let run_dir = tmp.path().join("run");
        std::fs::create_dir_all(&run_dir).unwrap();
        let targets = vec![ResolvedTarget {
            label: "control".to_owned(),
            url: url.clone(),
        }];
        backup_into(
            &run_dir,
            &targets,
            BackupFormat::Custom,
            &pg_dump,
            &tools,
            "dev",
        )
        .expect("backup succeeds and verifies");

        // Simulate data loss.
        conn.batch_execute("DELETE FROM backup_rt;")
            .expect("delete");
        let after_delete: Count = sql_query("SELECT COUNT(*) AS n FROM backup_rt")
            .get_result(&mut conn)
            .unwrap();
        assert_eq!(after_delete.n, 0);

        // Restore.
        let artifact = run_dir.join("control.dump");
        run_restore_one(&tools, &url, &artifact, BackupFormat::Custom, "dev")
            .expect("restore succeeds");

        // Row-level equality.
        let rows: Vec<Rt> = sql_query("SELECT id, name FROM backup_rt ORDER BY id")
            .load(&mut conn)
            .unwrap();
        let got: Vec<(i32, &str)> = rows.iter().map(|r| (r.id, r.name.as_str())).collect();
        assert_eq!(got, vec![(1, "alpha"), (2, "beta"), (3, "gamma")]);

        conn.batch_execute("DROP TABLE backup_rt;").ok();
    }
}