rustango-cms 0.2.0

A page-tree content management system for Rust, built on rustango
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
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
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
13315
13316
13317
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338
13339
13340
13341
13342
13343
13344
13345
13346
13347
13348
13349
13350
13351
13352
13353
13354
13355
13356
13357
13358
13359
13360
13361
13362
13363
13364
13365
13366
13367
13368
13369
13370
13371
13372
13373
13374
13375
13376
13377
13378
13379
13380
13381
13382
13383
13384
13385
13386
13387
13388
13389
13390
13391
13392
13393
13394
13395
13396
13397
13398
13399
13400
13401
13402
13403
13404
13405
13406
13407
13408
13409
13410
13411
13412
13413
13414
13415
13416
13417
13418
13419
13420
13421
13422
13423
13424
13425
13426
13427
13428
13429
13430
13431
13432
13433
13434
13435
13436
13437
13438
13439
13440
13441
13442
13443
13444
13445
13446
13447
13448
13449
13450
13451
13452
13453
13454
13455
13456
13457
13458
13459
13460
13461
13462
13463
13464
13465
13466
13467
13468
13469
13470
13471
13472
13473
13474
13475
13476
13477
13478
13479
13480
13481
13482
13483
13484
13485
13486
13487
13488
13489
13490
13491
13492
13493
13494
13495
13496
13497
13498
13499
13500
13501
13502
13503
13504
13505
13506
13507
13508
13509
13510
13511
13512
13513
13514
13515
13516
13517
13518
13519
13520
13521
13522
13523
13524
13525
13526
13527
13528
13529
13530
13531
13532
13533
13534
13535
13536
13537
13538
13539
13540
13541
13542
13543
13544
13545
13546
13547
13548
13549
13550
13551
13552
13553
13554
13555
13556
13557
13558
13559
13560
13561
13562
13563
13564
13565
13566
13567
13568
13569
13570
13571
13572
13573
13574
13575
13576
13577
13578
13579
13580
13581
13582
13583
13584
13585
13586
13587
13588
13589
13590
13591
13592
13593
13594
13595
13596
13597
13598
13599
13600
13601
13602
13603
13604
13605
13606
13607
13608
13609
13610
13611
13612
13613
13614
13615
13616
13617
13618
13619
13620
13621
13622
13623
13624
13625
13626
13627
13628
13629
13630
13631
13632
13633
13634
13635
13636
13637
13638
13639
13640
13641
13642
13643
13644
13645
13646
13647
13648
13649
13650
13651
13652
13653
13654
13655
13656
13657
13658
13659
13660
13661
13662
13663
13664
13665
13666
13667
13668
13669
13670
13671
13672
13673
13674
13675
13676
13677
13678
13679
13680
13681
13682
13683
13684
13685
13686
13687
13688
13689
13690
13691
13692
13693
13694
13695
13696
13697
13698
13699
13700
13701
13702
13703
13704
13705
13706
13707
13708
13709
13710
13711
13712
13713
13714
13715
13716
13717
13718
13719
13720
13721
13722
13723
13724
13725
13726
13727
13728
13729
13730
13731
13732
13733
13734
13735
13736
13737
13738
13739
13740
13741
13742
13743
13744
13745
13746
13747
13748
13749
13750
13751
13752
13753
13754
13755
13756
13757
13758
13759
13760
13761
13762
13763
13764
13765
13766
13767
13768
13769
13770
13771
13772
13773
13774
13775
13776
13777
13778
13779
13780
13781
13782
13783
13784
13785
13786
13787
13788
13789
13790
13791
13792
13793
13794
13795
13796
13797
13798
13799
13800
13801
13802
13803
13804
13805
13806
13807
13808
13809
13810
13811
13812
13813
13814
13815
13816
13817
13818
13819
13820
13821
13822
13823
13824
13825
13826
13827
13828
13829
13830
13831
13832
13833
13834
13835
13836
13837
13838
13839
13840
13841
13842
13843
13844
13845
13846
13847
13848
13849
13850
13851
13852
13853
13854
13855
13856
13857
13858
13859
13860
13861
13862
13863
13864
13865
13866
13867
13868
13869
13870
13871
13872
13873
13874
13875
13876
13877
13878
13879
13880
13881
13882
13883
13884
13885
13886
13887
13888
13889
13890
13891
13892
13893
13894
13895
13896
13897
13898
13899
13900
13901
13902
13903
13904
13905
13906
13907
13908
13909
13910
13911
13912
13913
13914
13915
13916
13917
13918
13919
13920
13921
13922
13923
13924
13925
13926
13927
13928
13929
13930
13931
13932
13933
13934
13935
13936
13937
13938
13939
13940
13941
13942
13943
13944
13945
13946
13947
13948
13949
13950
13951
13952
13953
13954
13955
13956
13957
13958
13959
13960
13961
13962
13963
13964
13965
13966
13967
13968
13969
13970
13971
13972
13973
13974
13975
13976
13977
13978
13979
13980
13981
13982
13983
13984
13985
13986
13987
13988
13989
13990
13991
13992
13993
13994
13995
13996
13997
13998
13999
14000
14001
14002
14003
14004
14005
14006
14007
14008
14009
14010
14011
14012
14013
14014
14015
14016
14017
14018
14019
14020
14021
14022
14023
14024
14025
14026
14027
14028
14029
14030
14031
14032
14033
14034
14035
14036
14037
14038
14039
14040
14041
14042
14043
14044
14045
14046
14047
14048
14049
14050
14051
14052
14053
14054
14055
14056
14057
14058
14059
14060
14061
14062
14063
14064
14065
14066
14067
14068
14069
14070
14071
14072
14073
14074
14075
14076
14077
14078
14079
14080
14081
14082
14083
14084
14085
14086
14087
14088
14089
14090
14091
14092
14093
14094
14095
14096
14097
14098
14099
14100
14101
14102
14103
14104
14105
14106
14107
14108
14109
14110
14111
14112
14113
14114
14115
14116
14117
14118
14119
14120
14121
14122
14123
14124
14125
14126
14127
14128
14129
14130
14131
14132
14133
14134
14135
14136
14137
14138
14139
14140
14141
14142
14143
14144
14145
14146
14147
14148
14149
14150
14151
14152
14153
14154
14155
14156
14157
14158
14159
14160
14161
14162
14163
14164
14165
14166
14167
14168
14169
14170
14171
14172
14173
14174
14175
14176
14177
14178
14179
14180
14181
14182
14183
14184
14185
14186
14187
14188
14189
14190
14191
14192
14193
14194
14195
14196
14197
14198
14199
14200
14201
14202
14203
14204
14205
14206
14207
14208
14209
14210
14211
14212
14213
14214
14215
14216
14217
14218
14219
14220
14221
14222
14223
14224
14225
14226
14227
14228
14229
14230
14231
14232
14233
14234
14235
14236
14237
14238
14239
14240
14241
14242
14243
14244
14245
14246
14247
14248
14249
14250
14251
14252
14253
14254
14255
14256
14257
14258
14259
14260
14261
14262
14263
14264
14265
14266
14267
14268
14269
14270
14271
14272
14273
14274
14275
14276
14277
14278
14279
14280
14281
14282
14283
14284
14285
14286
14287
14288
14289
14290
14291
14292
14293
14294
14295
14296
14297
14298
14299
14300
14301
14302
14303
14304
14305
14306
14307
14308
14309
14310
14311
14312
14313
14314
14315
14316
14317
14318
14319
14320
14321
14322
14323
14324
14325
14326
14327
14328
14329
14330
14331
14332
14333
14334
14335
14336
14337
14338
14339
14340
14341
14342
14343
14344
14345
14346
14347
14348
14349
14350
14351
14352
14353
14354
14355
14356
14357
14358
14359
14360
14361
14362
14363
14364
14365
14366
14367
14368
14369
14370
14371
14372
14373
14374
14375
14376
14377
14378
14379
14380
14381
14382
14383
14384
14385
14386
14387
14388
14389
14390
14391
14392
14393
14394
14395
14396
14397
14398
14399
14400
14401
14402
14403
14404
14405
14406
14407
14408
14409
14410
14411
14412
14413
14414
14415
14416
14417
14418
14419
14420
14421
14422
14423
14424
14425
14426
14427
14428
14429
14430
14431
14432
14433
14434
14435
14436
14437
14438
14439
14440
14441
14442
14443
14444
14445
14446
14447
14448
14449
14450
14451
14452
14453
14454
14455
14456
14457
14458
14459
14460
14461
14462
14463
14464
14465
14466
14467
14468
14469
14470
14471
14472
14473
14474
14475
14476
14477
14478
14479
14480
14481
14482
14483
14484
14485
14486
14487
14488
14489
14490
14491
14492
14493
14494
14495
14496
14497
14498
14499
14500
14501
14502
14503
14504
14505
14506
14507
14508
14509
14510
14511
14512
14513
14514
14515
14516
14517
14518
14519
14520
14521
14522
14523
14524
14525
14526
14527
14528
14529
14530
14531
14532
14533
14534
14535
14536
14537
14538
14539
14540
14541
14542
14543
14544
14545
14546
14547
14548
14549
14550
14551
14552
14553
14554
14555
14556
14557
14558
14559
14560
14561
14562
14563
14564
14565
14566
14567
14568
14569
14570
14571
14572
14573
14574
14575
14576
14577
14578
14579
14580
14581
14582
14583
14584
14585
14586
14587
14588
14589
14590
14591
14592
14593
14594
14595
14596
14597
14598
14599
14600
14601
14602
14603
14604
14605
14606
14607
14608
14609
14610
14611
14612
14613
14614
14615
14616
14617
14618
14619
14620
14621
14622
14623
14624
14625
14626
14627
14628
14629
14630
14631
14632
14633
14634
14635
14636
14637
14638
14639
14640
14641
14642
14643
14644
14645
14646
14647
14648
14649
14650
14651
14652
14653
14654
14655
14656
14657
14658
14659
14660
14661
14662
14663
14664
14665
14666
14667
14668
14669
14670
14671
14672
14673
14674
14675
14676
14677
14678
14679
14680
14681
14682
14683
14684
14685
14686
14687
14688
14689
14690
14691
14692
14693
14694
14695
14696
14697
14698
14699
14700
14701
14702
14703
14704
14705
14706
14707
14708
14709
14710
14711
14712
14713
14714
14715
14716
14717
14718
14719
14720
14721
14722
14723
14724
14725
14726
14727
14728
14729
14730
14731
14732
14733
14734
14735
14736
14737
14738
14739
14740
14741
14742
14743
14744
14745
14746
14747
14748
14749
14750
14751
14752
14753
14754
14755
14756
14757
14758
14759
14760
14761
14762
14763
14764
14765
14766
14767
14768
14769
14770
14771
14772
14773
14774
14775
14776
14777
14778
14779
14780
14781
14782
14783
14784
14785
14786
14787
14788
14789
14790
14791
14792
14793
14794
14795
14796
14797
14798
14799
14800
14801
14802
14803
14804
14805
14806
14807
14808
14809
14810
14811
14812
14813
14814
14815
14816
14817
14818
14819
14820
14821
14822
14823
14824
14825
14826
14827
14828
14829
14830
14831
14832
14833
14834
14835
14836
14837
14838
14839
14840
14841
14842
14843
14844
14845
14846
14847
14848
14849
14850
14851
14852
14853
14854
14855
14856
14857
14858
14859
14860
14861
14862
14863
14864
14865
14866
14867
14868
14869
14870
14871
14872
14873
14874
14875
14876
14877
14878
14879
14880
14881
14882
14883
14884
14885
14886
14887
14888
14889
14890
14891
14892
14893
14894
14895
14896
14897
14898
14899
14900
14901
14902
14903
14904
14905
14906
14907
14908
14909
14910
14911
14912
14913
14914
14915
14916
14917
14918
14919
14920
14921
14922
14923
14924
14925
14926
14927
14928
14929
14930
14931
14932
14933
14934
14935
14936
14937
14938
14939
14940
14941
14942
14943
14944
14945
14946
14947
14948
14949
14950
14951
14952
14953
14954
14955
14956
14957
14958
14959
14960
14961
14962
14963
14964
14965
14966
14967
14968
14969
14970
14971
14972
14973
14974
14975
14976
14977
14978
14979
14980
14981
14982
14983
14984
14985
14986
14987
14988
14989
14990
14991
14992
14993
14994
14995
14996
14997
14998
14999
15000
15001
15002
15003
15004
15005
15006
15007
15008
15009
15010
15011
15012
15013
15014
15015
15016
15017
15018
15019
15020
15021
15022
15023
15024
15025
15026
15027
15028
15029
15030
15031
15032
15033
15034
15035
15036
15037
15038
15039
15040
15041
15042
15043
15044
15045
15046
15047
15048
15049
15050
15051
15052
15053
15054
15055
15056
15057
15058
15059
15060
15061
15062
15063
15064
15065
15066
15067
15068
15069
15070
15071
15072
15073
15074
15075
15076
15077
15078
15079
15080
15081
15082
15083
15084
15085
15086
15087
15088
15089
15090
15091
15092
15093
15094
15095
15096
15097
15098
15099
15100
15101
15102
15103
15104
15105
15106
15107
15108
15109
15110
15111
15112
15113
15114
15115
15116
15117
15118
15119
15120
15121
15122
15123
15124
15125
15126
15127
15128
15129
15130
15131
15132
15133
15134
15135
15136
15137
15138
15139
15140
15141
15142
15143
15144
15145
15146
15147
15148
15149
15150
15151
15152
15153
15154
15155
15156
15157
15158
15159
15160
15161
15162
15163
15164
15165
15166
15167
15168
15169
15170
15171
15172
15173
15174
15175
15176
15177
15178
15179
15180
15181
15182
15183
15184
15185
15186
15187
15188
15189
15190
15191
15192
15193
15194
15195
15196
15197
15198
15199
15200
15201
15202
15203
15204
15205
15206
15207
15208
15209
15210
15211
15212
15213
15214
15215
15216
15217
15218
15219
15220
15221
15222
15223
15224
15225
15226
15227
15228
15229
15230
15231
15232
15233
15234
15235
15236
15237
15238
15239
15240
15241
15242
15243
15244
15245
15246
15247
15248
15249
15250
15251
15252
15253
15254
15255
15256
15257
15258
15259
15260
15261
15262
15263
15264
15265
15266
15267
15268
15269
15270
15271
15272
15273
15274
15275
15276
15277
15278
15279
15280
15281
15282
15283
15284
15285
15286
15287
15288
15289
15290
15291
15292
15293
15294
15295
15296
15297
15298
15299
15300
15301
15302
15303
15304
15305
15306
15307
15308
15309
15310
15311
15312
15313
15314
15315
15316
15317
15318
15319
15320
15321
15322
15323
15324
15325
15326
15327
15328
15329
15330
15331
15332
15333
15334
15335
15336
15337
15338
15339
15340
15341
15342
15343
15344
15345
15346
15347
15348
15349
15350
15351
15352
15353
15354
15355
15356
15357
15358
15359
15360
15361
15362
15363
15364
15365
15366
15367
15368
15369
15370
15371
15372
15373
15374
15375
15376
15377
15378
15379
15380
15381
15382
15383
15384
15385
15386
15387
15388
15389
15390
15391
15392
15393
15394
15395
15396
15397
15398
15399
15400
15401
15402
15403
15404
15405
15406
15407
15408
15409
15410
15411
15412
15413
15414
15415
15416
15417
15418
15419
15420
15421
15422
15423
15424
15425
15426
15427
15428
15429
15430
15431
15432
15433
15434
15435
15436
15437
15438
15439
15440
15441
15442
15443
15444
15445
15446
15447
15448
15449
15450
15451
15452
15453
15454
15455
15456
15457
15458
15459
15460
15461
15462
15463
15464
15465
15466
15467
15468
15469
15470
15471
15472
15473
15474
15475
15476
15477
15478
15479
15480
15481
15482
15483
15484
15485
15486
15487
15488
15489
15490
15491
15492
15493
15494
15495
15496
15497
15498
15499
15500
15501
15502
15503
15504
15505
15506
15507
15508
15509
15510
15511
15512
15513
15514
15515
15516
15517
15518
15519
15520
15521
15522
15523
15524
15525
15526
15527
15528
15529
15530
15531
15532
15533
15534
15535
15536
15537
15538
15539
15540
15541
15542
15543
15544
15545
15546
15547
15548
15549
15550
15551
15552
15553
15554
15555
15556
15557
15558
15559
15560
15561
15562
15563
15564
15565
15566
15567
15568
15569
15570
15571
15572
15573
15574
15575
15576
15577
15578
15579
15580
15581
15582
15583
15584
15585
15586
15587
15588
15589
15590
15591
15592
15593
15594
15595
15596
15597
15598
15599
15600
15601
15602
15603
15604
15605
15606
15607
15608
15609
15610
15611
15612
15613
15614
15615
15616
15617
15618
15619
15620
15621
15622
15623
15624
15625
15626
15627
15628
15629
15630
15631
15632
15633
15634
15635
15636
15637
15638
15639
15640
15641
15642
15643
15644
15645
15646
15647
15648
15649
15650
15651
15652
15653
15654
15655
15656
15657
15658
15659
15660
15661
15662
15663
15664
15665
15666
15667
15668
15669
15670
15671
15672
15673
15674
15675
15676
15677
15678
15679
15680
15681
15682
15683
15684
15685
15686
15687
15688
15689
15690
15691
15692
15693
15694
15695
15696
15697
15698
15699
15700
15701
15702
15703
15704
15705
15706
15707
15708
15709
15710
15711
15712
15713
15714
15715
15716
15717
15718
15719
15720
15721
15722
15723
15724
15725
15726
15727
15728
15729
15730
15731
15732
15733
15734
15735
15736
15737
15738
15739
15740
15741
15742
15743
15744
15745
15746
15747
15748
15749
15750
15751
15752
15753
15754
15755
15756
15757
15758
15759
15760
15761
15762
15763
15764
15765
15766
15767
15768
15769
15770
15771
15772
15773
15774
15775
15776
15777
15778
15779
15780
15781
15782
15783
15784
15785
15786
15787
15788
15789
15790
15791
15792
15793
15794
15795
15796
15797
15798
15799
15800
15801
15802
15803
15804
15805
15806
15807
15808
15809
15810
15811
15812
15813
15814
15815
15816
15817
15818
15819
15820
15821
15822
15823
15824
15825
15826
15827
15828
15829
15830
15831
15832
15833
15834
15835
15836
15837
15838
15839
15840
15841
15842
15843
15844
15845
15846
15847
15848
15849
15850
15851
15852
15853
15854
15855
15856
15857
15858
15859
15860
15861
15862
15863
15864
15865
15866
15867
15868
15869
15870
15871
15872
15873
15874
15875
15876
15877
15878
15879
15880
15881
15882
15883
15884
15885
15886
15887
15888
15889
15890
15891
15892
15893
15894
15895
15896
15897
15898
15899
15900
15901
15902
15903
15904
15905
15906
15907
15908
15909
15910
15911
15912
15913
15914
15915
15916
15917
15918
15919
15920
15921
15922
15923
15924
15925
15926
15927
15928
15929
15930
15931
15932
15933
15934
15935
15936
15937
15938
15939
15940
15941
15942
15943
15944
15945
15946
15947
15948
15949
15950
15951
15952
15953
15954
15955
15956
15957
15958
15959
15960
15961
15962
15963
15964
15965
15966
15967
15968
15969
15970
15971
15972
15973
15974
15975
15976
15977
15978
15979
15980
15981
15982
15983
15984
15985
15986
15987
15988
15989
15990
15991
15992
15993
15994
15995
15996
15997
15998
15999
16000
16001
16002
16003
16004
16005
16006
16007
16008
16009
16010
16011
16012
16013
16014
16015
16016
16017
16018
16019
16020
16021
16022
16023
16024
16025
16026
16027
16028
16029
16030
16031
16032
16033
16034
16035
16036
16037
16038
16039
16040
16041
16042
16043
16044
16045
16046
16047
16048
16049
16050
16051
16052
16053
16054
16055
16056
16057
16058
16059
16060
16061
16062
16063
16064
16065
16066
16067
16068
16069
16070
16071
16072
16073
16074
16075
16076
16077
16078
16079
16080
16081
16082
16083
16084
16085
16086
16087
16088
16089
16090
16091
16092
16093
16094
16095
16096
16097
16098
16099
16100
16101
16102
16103
16104
16105
16106
16107
16108
16109
16110
16111
16112
16113
16114
16115
16116
16117
16118
16119
16120
16121
16122
16123
16124
16125
16126
16127
16128
16129
16130
16131
16132
16133
16134
16135
16136
16137
16138
16139
16140
16141
16142
16143
16144
16145
16146
16147
16148
16149
16150
16151
16152
16153
16154
16155
16156
16157
16158
16159
16160
16161
16162
16163
16164
16165
16166
16167
16168
16169
16170
16171
16172
16173
16174
16175
16176
16177
16178
16179
16180
16181
16182
16183
16184
16185
16186
16187
16188
16189
16190
16191
16192
16193
16194
16195
16196
16197
16198
16199
16200
16201
16202
16203
16204
16205
16206
16207
16208
16209
16210
16211
16212
16213
16214
16215
16216
16217
16218
16219
16220
16221
16222
16223
16224
16225
16226
16227
16228
16229
16230
16231
16232
16233
16234
16235
16236
16237
16238
16239
16240
16241
16242
16243
16244
16245
16246
16247
16248
16249
16250
16251
16252
16253
16254
16255
16256
16257
16258
16259
16260
16261
16262
16263
16264
16265
16266
16267
16268
16269
16270
16271
16272
16273
16274
16275
16276
16277
16278
16279
16280
16281
16282
16283
16284
16285
16286
16287
16288
16289
16290
16291
16292
16293
16294
16295
16296
16297
16298
16299
16300
16301
16302
16303
16304
16305
16306
16307
16308
16309
16310
16311
16312
16313
16314
16315
16316
16317
16318
16319
16320
16321
16322
16323
16324
16325
16326
16327
16328
16329
16330
16331
16332
16333
16334
16335
16336
16337
16338
16339
16340
16341
16342
16343
16344
16345
16346
16347
16348
16349
16350
16351
16352
16353
16354
16355
16356
16357
16358
16359
16360
16361
16362
16363
16364
16365
16366
16367
16368
16369
16370
16371
16372
16373
16374
16375
16376
16377
16378
16379
16380
16381
16382
16383
16384
16385
16386
16387
16388
16389
16390
16391
16392
16393
16394
16395
16396
16397
16398
16399
16400
16401
16402
16403
16404
16405
16406
16407
16408
16409
16410
16411
16412
16413
16414
16415
16416
16417
16418
16419
16420
16421
16422
16423
16424
16425
16426
16427
16428
16429
16430
16431
16432
16433
16434
16435
16436
16437
16438
16439
16440
16441
16442
16443
16444
16445
16446
16447
16448
16449
16450
16451
16452
16453
16454
16455
16456
16457
16458
16459
16460
16461
16462
16463
16464
16465
16466
16467
16468
16469
16470
16471
16472
16473
16474
16475
16476
16477
16478
16479
16480
16481
16482
16483
16484
16485
16486
16487
16488
16489
16490
16491
16492
16493
16494
16495
16496
16497
16498
16499
16500
16501
16502
16503
16504
16505
16506
16507
16508
16509
16510
16511
16512
16513
16514
16515
16516
16517
16518
16519
16520
16521
16522
16523
16524
16525
16526
16527
16528
16529
16530
16531
16532
16533
16534
16535
16536
16537
16538
16539
16540
16541
16542
16543
16544
16545
16546
16547
16548
16549
16550
16551
16552
16553
16554
16555
16556
16557
16558
16559
16560
16561
16562
16563
16564
16565
16566
16567
16568
16569
16570
16571
16572
16573
16574
16575
16576
16577
16578
16579
16580
16581
16582
16583
16584
16585
16586
16587
16588
16589
16590
16591
16592
16593
16594
16595
16596
16597
16598
16599
16600
16601
16602
16603
16604
16605
16606
16607
16608
16609
16610
16611
16612
16613
16614
16615
16616
16617
16618
16619
16620
16621
16622
16623
16624
16625
16626
16627
16628
16629
16630
16631
16632
16633
16634
16635
16636
16637
16638
16639
16640
16641
16642
16643
16644
16645
16646
16647
16648
16649
16650
16651
16652
16653
16654
16655
16656
16657
16658
16659
16660
16661
16662
16663
16664
16665
16666
16667
16668
16669
16670
16671
16672
16673
16674
16675
16676
16677
16678
16679
16680
16681
16682
16683
16684
16685
16686
16687
16688
16689
16690
16691
16692
16693
16694
16695
16696
16697
16698
16699
16700
16701
16702
16703
16704
16705
16706
16707
16708
16709
16710
16711
16712
16713
16714
16715
16716
16717
16718
16719
16720
16721
16722
16723
16724
16725
16726
16727
16728
16729
16730
16731
16732
16733
16734
16735
16736
16737
16738
16739
16740
16741
16742
16743
16744
16745
16746
16747
16748
16749
16750
16751
16752
16753
16754
16755
16756
16757
16758
16759
16760
16761
16762
16763
16764
16765
16766
16767
16768
16769
16770
16771
16772
16773
16774
16775
16776
16777
16778
16779
16780
16781
16782
16783
16784
16785
16786
16787
16788
16789
16790
16791
16792
16793
16794
16795
16796
16797
16798
16799
16800
16801
16802
16803
16804
16805
16806
16807
16808
16809
16810
16811
16812
16813
16814
16815
16816
16817
16818
16819
16820
16821
16822
16823
16824
16825
16826
16827
16828
16829
16830
16831
16832
16833
16834
16835
16836
16837
16838
16839
16840
16841
16842
16843
16844
16845
16846
16847
16848
16849
16850
16851
16852
16853
16854
16855
16856
16857
16858
16859
16860
16861
16862
16863
16864
16865
16866
16867
16868
16869
16870
16871
16872
16873
16874
16875
16876
16877
16878
16879
16880
16881
16882
16883
16884
16885
16886
16887
16888
16889
16890
16891
16892
16893
16894
16895
16896
16897
16898
16899
16900
16901
16902
16903
16904
16905
16906
16907
16908
16909
16910
16911
16912
16913
16914
16915
16916
16917
16918
16919
16920
16921
16922
16923
16924
16925
16926
16927
16928
16929
16930
16931
16932
16933
16934
16935
16936
16937
16938
16939
16940
16941
16942
16943
16944
16945
16946
16947
16948
16949
16950
16951
16952
16953
16954
16955
16956
16957
16958
16959
16960
16961
16962
16963
16964
16965
16966
16967
16968
16969
16970
16971
16972
16973
16974
16975
16976
16977
16978
16979
16980
16981
16982
16983
16984
16985
16986
16987
16988
16989
16990
16991
16992
16993
16994
16995
16996
16997
16998
16999
17000
17001
17002
17003
17004
17005
17006
17007
17008
17009
17010
17011
17012
17013
17014
17015
17016
17017
17018
17019
17020
17021
17022
17023
17024
17025
17026
17027
17028
17029
17030
17031
17032
17033
17034
17035
17036
17037
17038
17039
17040
17041
17042
17043
17044
17045
17046
17047
17048
17049
17050
17051
17052
17053
17054
17055
17056
17057
17058
17059
17060
17061
17062
17063
17064
17065
17066
17067
17068
17069
17070
17071
17072
17073
17074
17075
17076
17077
17078
17079
17080
17081
17082
17083
17084
17085
17086
17087
17088
17089
17090
17091
17092
17093
17094
17095
17096
17097
17098
17099
17100
17101
17102
17103
17104
17105
17106
17107
17108
17109
17110
17111
17112
17113
17114
17115
17116
17117
17118
17119
17120
17121
17122
17123
17124
17125
17126
17127
17128
17129
17130
17131
17132
17133
17134
17135
17136
17137
17138
17139
17140
17141
17142
17143
17144
17145
17146
17147
17148
17149
17150
17151
17152
17153
17154
17155
17156
17157
17158
17159
17160
17161
17162
17163
17164
17165
17166
17167
17168
17169
17170
17171
17172
17173
17174
17175
17176
17177
17178
17179
17180
17181
17182
17183
17184
17185
17186
17187
17188
17189
17190
17191
17192
17193
17194
17195
17196
17197
17198
17199
17200
17201
17202
17203
17204
17205
17206
17207
17208
17209
17210
17211
17212
17213
17214
17215
17216
17217
17218
17219
17220
17221
17222
17223
17224
17225
17226
17227
17228
17229
17230
17231
17232
17233
17234
17235
17236
17237
17238
17239
17240
17241
17242
17243
17244
17245
17246
17247
17248
17249
17250
17251
17252
17253
17254
17255
17256
17257
17258
17259
17260
17261
17262
17263
17264
17265
17266
17267
17268
17269
17270
17271
17272
17273
17274
17275
17276
17277
17278
17279
17280
17281
17282
17283
17284
17285
17286
17287
17288
17289
17290
17291
17292
17293
17294
17295
17296
17297
17298
17299
17300
17301
17302
17303
17304
17305
17306
17307
17308
17309
17310
17311
17312
17313
17314
17315
17316
17317
17318
17319
17320
17321
17322
17323
17324
17325
17326
17327
17328
17329
17330
17331
17332
17333
17334
17335
17336
17337
17338
17339
17340
17341
17342
17343
17344
17345
17346
17347
17348
17349
17350
17351
17352
17353
17354
17355
17356
17357
17358
17359
17360
17361
17362
17363
17364
17365
17366
17367
17368
17369
17370
17371
17372
17373
17374
17375
17376
17377
17378
17379
17380
17381
17382
17383
17384
17385
17386
17387
17388
17389
17390
17391
17392
17393
17394
17395
17396
17397
17398
17399
17400
17401
17402
17403
17404
17405
17406
17407
17408
17409
17410
17411
17412
17413
17414
17415
17416
17417
17418
17419
17420
17421
17422
17423
17424
17425
17426
17427
17428
17429
17430
17431
17432
17433
17434
17435
17436
17437
17438
17439
17440
17441
17442
17443
17444
17445
17446
17447
17448
17449
17450
17451
17452
17453
17454
17455
17456
17457
17458
17459
17460
17461
17462
17463
17464
17465
17466
17467
17468
17469
17470
17471
17472
17473
17474
17475
17476
17477
17478
17479
17480
17481
17482
17483
17484
17485
17486
17487
17488
17489
17490
17491
17492
17493
17494
17495
17496
17497
17498
17499
17500
17501
17502
17503
17504
17505
17506
17507
17508
17509
17510
17511
17512
17513
17514
17515
17516
17517
17518
17519
17520
17521
17522
17523
17524
17525
17526
17527
17528
17529
17530
17531
17532
17533
17534
17535
17536
17537
17538
17539
17540
17541
17542
17543
17544
17545
17546
17547
17548
17549
17550
17551
17552
17553
17554
17555
17556
17557
17558
17559
17560
17561
17562
17563
17564
17565
17566
17567
17568
17569
17570
17571
17572
17573
17574
17575
17576
17577
17578
17579
17580
17581
17582
17583
17584
17585
17586
17587
17588
17589
17590
17591
17592
17593
17594
17595
17596
17597
17598
17599
17600
17601
17602
17603
17604
17605
17606
17607
17608
17609
17610
17611
17612
17613
17614
17615
17616
17617
17618
17619
17620
17621
17622
17623
17624
17625
17626
17627
17628
17629
17630
17631
17632
17633
17634
17635
17636
17637
17638
17639
17640
17641
17642
17643
17644
17645
17646
17647
17648
17649
17650
17651
17652
17653
17654
17655
17656
17657
17658
17659
17660
17661
17662
17663
17664
17665
17666
17667
17668
17669
17670
17671
17672
17673
17674
17675
17676
17677
17678
17679
17680
17681
17682
17683
17684
17685
17686
17687
17688
17689
17690
17691
17692
17693
17694
17695
17696
17697
17698
17699
17700
17701
17702
17703
17704
17705
17706
17707
17708
17709
17710
17711
17712
17713
17714
17715
17716
17717
17718
17719
17720
17721
17722
17723
17724
17725
17726
17727
17728
17729
17730
17731
17732
17733
17734
17735
17736
17737
17738
17739
17740
17741
17742
17743
17744
17745
17746
17747
17748
17749
17750
17751
17752
17753
17754
17755
17756
17757
17758
17759
17760
17761
17762
17763
17764
17765
17766
17767
17768
17769
17770
17771
17772
17773
17774
17775
17776
17777
17778
17779
17780
17781
17782
17783
17784
17785
17786
17787
17788
17789
17790
17791
17792
17793
17794
17795
17796
17797
17798
17799
17800
17801
17802
17803
17804
17805
17806
17807
17808
17809
17810
17811
17812
17813
17814
17815
17816
17817
17818
17819
17820
17821
17822
17823
17824
17825
17826
17827
17828
17829
17830
17831
17832
17833
17834
17835
17836
17837
17838
17839
17840
17841
17842
17843
17844
17845
17846
17847
17848
17849
17850
17851
17852
17853
17854
17855
17856
17857
17858
17859
17860
17861
17862
17863
17864
17865
17866
17867
17868
17869
17870
17871
17872
17873
17874
17875
17876
17877
17878
17879
17880
17881
17882
17883
17884
17885
17886
17887
17888
17889
17890
17891
17892
17893
17894
17895
17896
17897
17898
17899
17900
17901
17902
17903
17904
17905
17906
17907
17908
17909
17910
17911
17912
17913
17914
17915
17916
17917
17918
17919
17920
17921
17922
17923
17924
17925
17926
17927
17928
17929
17930
17931
17932
17933
17934
17935
17936
17937
17938
17939
17940
17941
17942
17943
17944
17945
17946
17947
17948
17949
17950
17951
17952
17953
17954
17955
17956
17957
17958
17959
17960
17961
17962
17963
17964
17965
17966
17967
17968
17969
17970
17971
17972
17973
17974
17975
17976
17977
17978
17979
17980
17981
17982
17983
17984
17985
17986
17987
17988
17989
17990
17991
17992
17993
17994
17995
17996
17997
17998
17999
18000
18001
18002
18003
18004
18005
18006
18007
18008
18009
18010
18011
18012
18013
18014
18015
18016
18017
18018
18019
18020
18021
18022
18023
18024
18025
18026
18027
18028
18029
18030
18031
18032
18033
18034
18035
18036
18037
18038
18039
18040
18041
18042
18043
18044
18045
18046
18047
18048
18049
18050
18051
18052
18053
18054
18055
18056
18057
18058
18059
18060
18061
18062
18063
18064
18065
18066
18067
18068
18069
18070
18071
18072
18073
18074
18075
18076
18077
18078
18079
18080
18081
18082
18083
18084
18085
18086
18087
18088
18089
18090
18091
18092
18093
18094
18095
18096
18097
18098
18099
18100
18101
18102
18103
18104
18105
18106
18107
18108
18109
18110
18111
18112
18113
18114
18115
18116
18117
18118
18119
18120
18121
18122
18123
18124
18125
18126
18127
18128
18129
18130
18131
18132
18133
18134
18135
18136
18137
18138
18139
18140
18141
18142
18143
18144
18145
18146
18147
18148
18149
18150
18151
18152
18153
18154
18155
18156
18157
18158
18159
18160
18161
18162
18163
18164
18165
18166
18167
18168
18169
18170
18171
18172
18173
18174
18175
18176
18177
18178
18179
18180
18181
18182
18183
18184
18185
18186
18187
18188
18189
18190
18191
18192
18193
18194
18195
18196
18197
18198
18199
18200
18201
18202
18203
18204
18205
18206
18207
18208
18209
18210
18211
18212
18213
18214
18215
18216
18217
18218
18219
18220
18221
18222
18223
18224
18225
18226
18227
18228
18229
18230
18231
18232
18233
18234
18235
18236
18237
18238
18239
18240
18241
18242
18243
18244
18245
18246
18247
18248
18249
18250
18251
18252
18253
18254
18255
18256
18257
18258
18259
18260
18261
18262
18263
18264
18265
18266
18267
18268
18269
18270
18271
18272
18273
18274
18275
18276
18277
18278
18279
18280
18281
18282
18283
18284
18285
18286
18287
18288
18289
18290
18291
18292
18293
18294
18295
18296
18297
18298
18299
18300
18301
18302
18303
18304
18305
18306
18307
18308
18309
18310
18311
18312
18313
18314
18315
18316
18317
18318
18319
18320
18321
18322
18323
18324
18325
18326
18327
18328
18329
18330
18331
18332
18333
18334
18335
18336
18337
18338
18339
18340
18341
18342
18343
18344
18345
18346
18347
18348
18349
18350
18351
18352
18353
18354
18355
18356
18357
18358
18359
18360
18361
18362
18363
18364
18365
18366
18367
18368
18369
18370
18371
18372
18373
18374
18375
18376
18377
18378
18379
18380
18381
18382
18383
18384
18385
18386
18387
18388
18389
18390
18391
18392
18393
18394
18395
18396
18397
18398
18399
18400
18401
18402
18403
18404
18405
18406
18407
18408
18409
18410
18411
18412
18413
18414
18415
18416
18417
18418
18419
18420
18421
18422
18423
18424
18425
18426
18427
18428
18429
18430
18431
18432
18433
18434
18435
18436
18437
18438
18439
18440
18441
18442
18443
18444
18445
18446
18447
18448
18449
18450
18451
18452
18453
18454
18455
18456
18457
18458
18459
18460
18461
18462
18463
18464
18465
18466
18467
18468
18469
18470
18471
18472
18473
18474
18475
18476
18477
18478
18479
18480
18481
18482
18483
18484
18485
18486
18487
18488
18489
18490
18491
18492
18493
18494
18495
18496
18497
18498
18499
18500
18501
18502
18503
18504
18505
18506
18507
18508
18509
18510
18511
18512
18513
18514
18515
18516
18517
18518
18519
18520
18521
18522
18523
18524
18525
18526
18527
18528
18529
18530
18531
18532
18533
18534
18535
18536
18537
18538
18539
18540
18541
18542
18543
18544
18545
18546
18547
18548
18549
18550
18551
18552
18553
18554
18555
18556
18557
18558
18559
18560
18561
18562
18563
18564
18565
18566
18567
18568
18569
18570
18571
18572
18573
18574
18575
18576
18577
18578
18579
18580
18581
18582
18583
18584
18585
18586
18587
18588
18589
18590
18591
18592
18593
18594
18595
18596
18597
18598
18599
18600
18601
18602
18603
18604
18605
18606
18607
18608
18609
18610
18611
18612
18613
18614
18615
18616
18617
18618
18619
18620
18621
18622
18623
18624
18625
18626
18627
18628
18629
18630
18631
18632
18633
18634
18635
18636
18637
18638
18639
18640
18641
18642
18643
18644
18645
18646
18647
18648
18649
18650
18651
18652
18653
18654
18655
18656
18657
18658
18659
18660
18661
18662
18663
18664
18665
18666
18667
18668
18669
18670
18671
18672
18673
18674
18675
18676
18677
18678
18679
18680
18681
18682
18683
18684
18685
18686
18687
18688
18689
18690
18691
18692
18693
18694
18695
18696
18697
18698
18699
18700
18701
18702
18703
18704
18705
18706
18707
18708
18709
18710
18711
18712
18713
18714
18715
18716
18717
18718
18719
18720
18721
18722
18723
18724
18725
18726
18727
18728
18729
18730
18731
18732
18733
18734
18735
18736
18737
18738
18739
18740
18741
18742
18743
18744
18745
18746
18747
18748
18749
18750
18751
18752
18753
18754
18755
18756
18757
18758
18759
18760
18761
18762
18763
18764
18765
18766
18767
18768
18769
18770
18771
18772
18773
18774
18775
18776
18777
18778
18779
18780
18781
18782
18783
18784
18785
18786
18787
18788
18789
18790
18791
18792
18793
18794
18795
18796
18797
18798
18799
18800
18801
18802
18803
18804
18805
18806
18807
18808
18809
18810
18811
18812
18813
18814
18815
18816
18817
18818
18819
18820
18821
18822
18823
18824
18825
18826
18827
18828
18829
18830
18831
18832
18833
18834
18835
18836
18837
18838
18839
18840
18841
18842
18843
18844
18845
18846
18847
18848
18849
18850
18851
18852
18853
18854
18855
18856
18857
18858
18859
18860
18861
18862
18863
18864
18865
18866
18867
18868
18869
18870
18871
18872
18873
18874
18875
18876
18877
18878
18879
18880
18881
18882
18883
18884
18885
18886
18887
18888
18889
18890
18891
18892
18893
18894
18895
18896
18897
18898
18899
18900
18901
18902
18903
18904
18905
18906
18907
18908
18909
18910
18911
18912
18913
18914
18915
18916
18917
18918
18919
18920
18921
18922
18923
18924
18925
18926
18927
18928
18929
18930
18931
18932
18933
18934
18935
18936
18937
18938
18939
18940
18941
18942
18943
18944
18945
18946
18947
18948
18949
18950
18951
18952
18953
18954
18955
18956
18957
18958
18959
18960
18961
18962
18963
18964
18965
18966
18967
18968
18969
18970
18971
18972
18973
18974
18975
18976
18977
18978
18979
18980
18981
18982
18983
18984
18985
18986
18987
18988
18989
18990
18991
18992
18993
18994
18995
18996
18997
18998
18999
19000
19001
19002
19003
19004
19005
19006
19007
19008
19009
19010
19011
19012
19013
19014
19015
19016
19017
19018
19019
19020
19021
19022
19023
19024
19025
19026
19027
19028
19029
19030
19031
19032
19033
19034
19035
19036
19037
19038
19039
19040
19041
19042
19043
19044
19045
19046
19047
19048
19049
19050
19051
19052
19053
19054
19055
19056
19057
19058
19059
19060
19061
19062
19063
19064
19065
19066
19067
19068
19069
19070
19071
19072
19073
19074
19075
19076
19077
19078
19079
19080
19081
19082
19083
19084
19085
19086
19087
19088
19089
19090
19091
19092
19093
19094
19095
19096
19097
19098
19099
19100
19101
19102
19103
19104
19105
19106
19107
19108
19109
19110
19111
19112
19113
19114
19115
19116
19117
19118
19119
19120
19121
19122
19123
19124
19125
19126
19127
19128
19129
19130
19131
19132
19133
19134
19135
19136
19137
19138
19139
19140
19141
19142
19143
19144
19145
19146
19147
19148
19149
19150
19151
19152
19153
19154
19155
19156
19157
19158
19159
19160
19161
19162
19163
19164
19165
19166
19167
19168
19169
19170
19171
19172
19173
19174
19175
19176
19177
19178
19179
19180
19181
19182
19183
19184
19185
19186
19187
19188
19189
19190
19191
19192
19193
19194
19195
19196
19197
19198
19199
19200
19201
19202
19203
19204
19205
19206
19207
19208
19209
19210
19211
19212
19213
19214
19215
19216
19217
19218
19219
19220
19221
19222
19223
19224
19225
19226
19227
19228
19229
19230
19231
19232
19233
19234
19235
19236
19237
19238
19239
19240
19241
19242
19243
19244
19245
19246
19247
19248
19249
19250
19251
19252
19253
19254
19255
19256
19257
19258
19259
19260
19261
19262
19263
19264
19265
19266
19267
19268
19269
19270
19271
19272
19273
19274
19275
19276
19277
19278
19279
19280
19281
19282
19283
19284
19285
19286
19287
19288
19289
19290
19291
19292
19293
19294
19295
19296
19297
19298
19299
19300
19301
19302
19303
19304
19305
19306
19307
19308
19309
19310
19311
19312
19313
19314
19315
19316
19317
19318
19319
19320
19321
19322
19323
19324
19325
19326
19327
19328
19329
19330
19331
19332
19333
19334
19335
19336
19337
19338
19339
19340
19341
19342
19343
19344
19345
19346
19347
19348
19349
19350
19351
19352
19353
19354
19355
19356
19357
19358
19359
19360
19361
19362
19363
19364
19365
19366
19367
19368
19369
19370
19371
19372
19373
19374
19375
19376
19377
19378
19379
19380
19381
19382
19383
19384
19385
19386
19387
19388
19389
19390
19391
19392
19393
19394
19395
19396
19397
19398
19399
19400
19401
19402
19403
19404
19405
19406
19407
19408
19409
19410
19411
19412
19413
19414
19415
19416
19417
19418
19419
19420
19421
19422
19423
19424
19425
19426
19427
19428
19429
19430
19431
19432
19433
19434
19435
19436
19437
19438
19439
19440
19441
19442
19443
19444
19445
19446
19447
19448
19449
19450
19451
19452
19453
19454
19455
19456
19457
19458
19459
19460
19461
19462
19463
19464
19465
19466
19467
19468
19469
19470
19471
19472
19473
19474
19475
19476
19477
19478
19479
19480
19481
19482
19483
19484
19485
19486
19487
19488
19489
19490
19491
19492
19493
19494
19495
19496
19497
19498
19499
19500
19501
19502
19503
19504
19505
19506
19507
19508
19509
19510
19511
19512
19513
19514
19515
19516
19517
19518
19519
19520
19521
19522
19523
19524
19525
19526
19527
19528
19529
19530
19531
19532
19533
19534
19535
19536
19537
19538
19539
19540
19541
19542
19543
19544
19545
19546
19547
19548
19549
19550
19551
19552
19553
19554
19555
19556
19557
19558
19559
19560
19561
19562
19563
19564
19565
19566
19567
19568
19569
19570
19571
19572
19573
19574
19575
19576
19577
19578
19579
19580
19581
19582
19583
19584
19585
19586
19587
19588
19589
19590
19591
19592
19593
19594
19595
19596
19597
19598
19599
19600
19601
19602
19603
19604
19605
19606
19607
19608
19609
19610
19611
19612
19613
19614
19615
19616
19617
19618
19619
19620
19621
19622
19623
19624
19625
19626
19627
19628
19629
19630
19631
19632
19633
19634
19635
19636
19637
19638
19639
19640
19641
19642
19643
19644
19645
19646
19647
19648
19649
19650
19651
19652
19653
19654
19655
19656
19657
19658
19659
19660
19661
19662
19663
19664
19665
19666
19667
19668
19669
19670
19671
19672
19673
19674
19675
19676
19677
19678
19679
19680
19681
19682
19683
19684
19685
19686
19687
19688
19689
19690
19691
19692
19693
19694
19695
19696
19697
19698
19699
19700
19701
19702
19703
19704
19705
19706
19707
19708
19709
19710
19711
19712
19713
19714
19715
19716
19717
19718
19719
19720
19721
19722
19723
19724
19725
19726
19727
19728
19729
19730
19731
19732
19733
19734
19735
19736
19737
19738
19739
19740
19741
19742
19743
19744
19745
19746
19747
19748
19749
19750
19751
19752
19753
19754
19755
19756
19757
19758
19759
19760
19761
19762
19763
19764
19765
19766
19767
19768
19769
19770
19771
19772
19773
19774
19775
19776
19777
19778
19779
19780
19781
19782
19783
19784
19785
19786
19787
19788
19789
19790
19791
19792
19793
19794
19795
19796
19797
19798
19799
19800
19801
19802
19803
19804
19805
19806
19807
19808
19809
19810
19811
19812
19813
19814
19815
19816
19817
19818
19819
19820
19821
19822
19823
19824
19825
19826
19827
19828
19829
19830
19831
19832
19833
19834
19835
19836
19837
19838
19839
19840
19841
19842
19843
19844
19845
19846
19847
19848
19849
19850
19851
19852
19853
19854
19855
19856
19857
19858
19859
19860
19861
19862
19863
19864
19865
19866
19867
19868
19869
19870
19871
19872
19873
19874
19875
19876
19877
19878
19879
19880
19881
19882
19883
19884
19885
19886
19887
19888
19889
19890
19891
19892
19893
19894
19895
19896
19897
19898
19899
19900
19901
19902
19903
19904
19905
19906
19907
19908
19909
19910
19911
19912
19913
19914
19915
19916
19917
19918
19919
19920
19921
19922
19923
19924
19925
19926
19927
19928
19929
19930
19931
19932
19933
19934
19935
19936
19937
19938
19939
19940
19941
19942
19943
19944
19945
19946
19947
19948
19949
19950
19951
19952
19953
19954
19955
19956
19957
19958
19959
19960
19961
19962
19963
19964
19965
19966
19967
19968
19969
19970
19971
19972
19973
19974
19975
19976
19977
19978
19979
19980
19981
19982
19983
19984
19985
19986
19987
19988
19989
19990
19991
19992
19993
19994
19995
19996
19997
19998
19999
20000
20001
20002
20003
20004
20005
20006
20007
20008
20009
20010
20011
20012
20013
20014
20015
20016
20017
20018
20019
20020
20021
20022
20023
20024
20025
20026
20027
20028
20029
20030
20031
20032
20033
20034
20035
20036
20037
20038
20039
20040
20041
20042
20043
20044
20045
20046
20047
20048
20049
20050
20051
20052
20053
20054
20055
20056
20057
20058
20059
20060
20061
20062
20063
20064
20065
20066
20067
20068
20069
20070
20071
20072
20073
20074
20075
20076
20077
20078
20079
20080
20081
20082
20083
20084
20085
20086
20087
20088
20089
20090
20091
20092
20093
20094
20095
20096
20097
20098
20099
20100
20101
20102
20103
20104
20105
20106
20107
20108
20109
20110
20111
20112
20113
20114
20115
20116
20117
20118
20119
20120
20121
20122
20123
20124
20125
20126
20127
20128
20129
20130
20131
20132
20133
20134
20135
20136
20137
20138
20139
20140
20141
20142
20143
20144
20145
20146
20147
20148
20149
20150
20151
20152
20153
20154
20155
20156
20157
20158
20159
20160
20161
20162
20163
20164
20165
20166
20167
20168
20169
20170
20171
20172
20173
20174
20175
20176
20177
20178
20179
20180
20181
20182
20183
20184
20185
20186
20187
20188
20189
20190
20191
20192
20193
20194
20195
20196
20197
20198
20199
20200
20201
20202
20203
20204
20205
20206
20207
20208
20209
20210
20211
20212
20213
20214
20215
20216
20217
20218
20219
20220
20221
20222
20223
20224
20225
20226
20227
20228
20229
20230
20231
20232
20233
20234
20235
20236
20237
20238
20239
20240
20241
20242
20243
20244
20245
20246
20247
20248
20249
20250
20251
20252
20253
20254
20255
20256
20257
20258
20259
20260
20261
20262
20263
20264
20265
20266
20267
20268
20269
20270
20271
20272
20273
20274
20275
20276
20277
20278
20279
20280
20281
20282
20283
20284
20285
20286
20287
20288
20289
20290
20291
20292
20293
20294
20295
20296
20297
20298
20299
20300
20301
20302
20303
20304
20305
20306
20307
20308
20309
20310
20311
20312
20313
20314
20315
20316
20317
20318
20319
20320
20321
20322
20323
20324
20325
20326
20327
20328
20329
20330
20331
20332
20333
20334
20335
20336
20337
20338
20339
20340
20341
20342
20343
20344
20345
20346
20347
20348
20349
20350
20351
20352
20353
20354
20355
20356
20357
20358
20359
20360
20361
20362
20363
20364
20365
20366
20367
20368
20369
20370
20371
20372
20373
20374
20375
20376
20377
20378
20379
20380
20381
20382
20383
20384
20385
20386
20387
20388
20389
20390
20391
20392
20393
20394
20395
20396
20397
20398
20399
20400
20401
20402
20403
20404
20405
20406
20407
20408
20409
20410
20411
20412
20413
20414
20415
20416
20417
20418
20419
20420
20421
20422
20423
20424
20425
20426
20427
20428
20429
20430
20431
20432
20433
20434
20435
20436
20437
20438
20439
20440
20441
20442
20443
20444
20445
20446
20447
20448
20449
20450
20451
20452
20453
20454
20455
20456
20457
20458
20459
20460
20461
20462
20463
20464
20465
20466
20467
20468
20469
20470
20471
20472
20473
20474
20475
20476
20477
20478
20479
20480
20481
20482
20483
20484
20485
20486
20487
20488
20489
20490
20491
20492
20493
20494
20495
20496
20497
20498
20499
20500
20501
20502
20503
20504
20505
20506
20507
20508
20509
20510
20511
20512
20513
20514
20515
20516
20517
20518
20519
20520
20521
20522
20523
20524
20525
20526
20527
20528
20529
20530
20531
20532
20533
20534
20535
20536
20537
20538
20539
20540
20541
20542
20543
20544
20545
20546
20547
20548
20549
20550
20551
20552
20553
20554
20555
20556
20557
20558
20559
20560
20561
20562
20563
20564
20565
20566
20567
20568
20569
20570
20571
20572
20573
20574
20575
20576
20577
20578
20579
20580
20581
20582
20583
20584
20585
20586
20587
20588
20589
20590
20591
20592
20593
20594
20595
20596
20597
20598
20599
20600
20601
20602
20603
20604
20605
20606
20607
20608
20609
20610
20611
20612
20613
20614
20615
20616
20617
20618
20619
20620
20621
20622
20623
20624
20625
20626
20627
20628
20629
20630
20631
20632
20633
20634
20635
20636
20637
20638
20639
20640
20641
20642
20643
20644
20645
20646
20647
20648
20649
20650
20651
20652
20653
20654
20655
20656
20657
20658
20659
20660
20661
20662
20663
20664
20665
20666
20667
20668
20669
20670
20671
20672
20673
20674
20675
20676
20677
20678
20679
20680
20681
20682
20683
20684
20685
20686
20687
20688
20689
20690
20691
20692
20693
20694
20695
20696
20697
20698
20699
20700
20701
20702
20703
20704
20705
20706
20707
20708
20709
20710
20711
20712
20713
20714
20715
20716
20717
20718
20719
20720
20721
20722
20723
20724
20725
20726
20727
20728
20729
20730
20731
20732
20733
20734
20735
20736
20737
20738
20739
20740
20741
20742
20743
20744
20745
20746
20747
20748
20749
20750
20751
20752
20753
20754
20755
20756
20757
20758
20759
20760
20761
20762
20763
20764
20765
20766
20767
20768
20769
20770
20771
20772
20773
20774
20775
20776
20777
20778
20779
20780
20781
20782
20783
20784
20785
20786
20787
20788
20789
20790
20791
20792
20793
20794
20795
20796
20797
20798
20799
20800
20801
20802
20803
20804
20805
20806
20807
20808
20809
20810
20811
20812
20813
20814
20815
20816
20817
20818
20819
20820
20821
20822
20823
20824
20825
20826
20827
20828
20829
20830
20831
20832
20833
20834
20835
20836
20837
20838
20839
20840
20841
20842
20843
20844
20845
20846
20847
20848
20849
20850
20851
20852
20853
20854
20855
20856
20857
20858
20859
20860
20861
20862
20863
20864
20865
20866
20867
20868
20869
20870
20871
20872
20873
20874
20875
20876
20877
20878
20879
20880
20881
20882
20883
20884
20885
20886
20887
20888
20889
20890
20891
20892
20893
20894
20895
20896
20897
20898
20899
20900
20901
20902
20903
20904
20905
20906
20907
20908
20909
20910
20911
20912
20913
20914
20915
20916
20917
20918
20919
20920
20921
20922
20923
20924
20925
20926
20927
20928
20929
20930
20931
20932
20933
20934
20935
20936
20937
20938
20939
20940
20941
20942
20943
20944
20945
20946
20947
20948
20949
20950
20951
20952
20953
20954
20955
20956
20957
20958
20959
20960
20961
20962
20963
20964
20965
20966
20967
20968
20969
20970
20971
20972
20973
20974
20975
20976
20977
20978
20979
20980
20981
20982
20983
20984
20985
20986
20987
20988
20989
20990
20991
20992
20993
20994
20995
20996
20997
20998
20999
21000
21001
21002
21003
21004
21005
21006
21007
21008
21009
21010
21011
21012
21013
21014
21015
21016
21017
21018
21019
21020
21021
21022
21023
21024
21025
21026
21027
21028
21029
21030
21031
21032
21033
21034
21035
21036
21037
21038
21039
21040
21041
21042
21043
21044
21045
21046
21047
21048
21049
21050
21051
21052
21053
21054
21055
21056
21057
21058
21059
21060
21061
21062
21063
21064
21065
21066
21067
21068
21069
21070
21071
21072
21073
21074
21075
21076
21077
21078
21079
21080
21081
21082
21083
21084
21085
21086
21087
21088
21089
21090
21091
21092
21093
21094
21095
21096
21097
21098
21099
21100
21101
21102
21103
21104
21105
21106
21107
21108
21109
21110
21111
21112
21113
21114
21115
21116
21117
21118
21119
21120
21121
21122
21123
21124
21125
21126
21127
21128
21129
21130
21131
21132
21133
21134
21135
21136
21137
21138
21139
21140
21141
21142
21143
21144
21145
21146
21147
21148
21149
21150
21151
21152
21153
21154
21155
21156
21157
21158
21159
21160
21161
21162
21163
21164
21165
21166
21167
21168
21169
21170
21171
21172
21173
21174
21175
21176
21177
21178
21179
21180
21181
21182
21183
21184
21185
21186
21187
21188
21189
21190
21191
21192
21193
21194
21195
21196
21197
21198
21199
21200
21201
21202
21203
21204
21205
21206
21207
21208
21209
21210
21211
21212
21213
21214
21215
21216
21217
21218
21219
21220
21221
21222
21223
21224
21225
21226
21227
21228
21229
21230
21231
21232
21233
21234
21235
21236
21237
21238
21239
21240
21241
21242
21243
21244
21245
21246
21247
21248
21249
21250
21251
21252
21253
21254
21255
21256
21257
21258
21259
21260
21261
21262
21263
21264
21265
21266
21267
21268
21269
21270
21271
21272
21273
21274
21275
21276
21277
21278
21279
21280
21281
21282
21283
21284
21285
21286
21287
21288
21289
21290
21291
21292
21293
21294
21295
21296
21297
21298
21299
21300
21301
21302
21303
21304
21305
21306
21307
21308
21309
21310
21311
21312
21313
21314
21315
21316
21317
21318
21319
21320
21321
21322
21323
21324
21325
21326
21327
21328
21329
21330
21331
21332
21333
21334
21335
21336
21337
21338
21339
21340
21341
21342
21343
21344
21345
21346
21347
21348
21349
21350
21351
21352
21353
21354
21355
21356
21357
21358
21359
21360
21361
21362
21363
21364
21365
21366
21367
21368
21369
21370
21371
21372
21373
21374
21375
21376
21377
21378
21379
21380
21381
21382
21383
21384
21385
21386
21387
21388
21389
21390
21391
21392
21393
21394
21395
21396
21397
21398
21399
21400
21401
21402
21403
21404
21405
21406
21407
21408
21409
21410
21411
21412
21413
21414
21415
21416
21417
21418
21419
21420
21421
21422
21423
21424
21425
21426
21427
21428
21429
21430
21431
21432
21433
21434
21435
21436
21437
21438
21439
21440
21441
21442
21443
21444
21445
21446
21447
21448
21449
21450
21451
21452
21453
21454
21455
21456
21457
21458
21459
21460
21461
21462
21463
21464
21465
21466
21467
21468
21469
21470
21471
21472
21473
21474
21475
21476
21477
21478
21479
21480
21481
21482
21483
21484
21485
21486
21487
21488
21489
21490
21491
21492
21493
21494
21495
21496
21497
21498
21499
21500
21501
21502
21503
21504
21505
21506
21507
21508
21509
21510
21511
21512
21513
21514
21515
21516
21517
21518
21519
21520
21521
21522
21523
21524
21525
21526
21527
21528
21529
21530
21531
21532
21533
21534
21535
21536
21537
21538
21539
21540
21541
21542
21543
21544
21545
21546
21547
21548
21549
21550
21551
21552
21553
21554
21555
21556
21557
21558
21559
21560
21561
21562
21563
21564
21565
21566
21567
21568
21569
21570
21571
21572
21573
21574
21575
21576
21577
21578
21579
21580
21581
21582
21583
21584
21585
21586
21587
21588
21589
21590
21591
21592
21593
21594
21595
21596
21597
21598
21599
21600
21601
21602
21603
21604
21605
21606
21607
21608
21609
21610
21611
21612
21613
21614
21615
21616
21617
21618
21619
21620
21621
21622
21623
21624
21625
21626
21627
21628
21629
21630
21631
21632
21633
21634
21635
21636
21637
21638
21639
21640
21641
21642
21643
21644
21645
21646
21647
21648
21649
21650
21651
21652
21653
21654
21655
21656
21657
21658
21659
21660
21661
21662
21663
21664
21665
21666
21667
21668
21669
21670
21671
21672
21673
21674
21675
21676
21677
21678
21679
21680
21681
21682
21683
21684
21685
21686
21687
21688
21689
21690
21691
21692
21693
21694
21695
21696
21697
21698
21699
21700
21701
21702
21703
21704
21705
21706
21707
21708
21709
21710
21711
21712
21713
21714
21715
21716
21717
21718
21719
21720
21721
21722
21723
21724
21725
21726
21727
21728
21729
21730
21731
21732
21733
21734
21735
21736
21737
21738
21739
21740
21741
21742
21743
21744
21745
21746
21747
21748
21749
21750
21751
21752
21753
21754
21755
21756
21757
21758
21759
21760
21761
21762
21763
21764
21765
21766
21767
21768
21769
21770
21771
21772
21773
21774
21775
21776
21777
21778
21779
21780
21781
21782
21783
21784
21785
21786
21787
21788
21789
21790
21791
21792
21793
21794
21795
21796
21797
21798
21799
21800
21801
21802
21803
21804
21805
21806
21807
21808
21809
21810
21811
21812
21813
21814
21815
21816
21817
21818
21819
21820
21821
21822
21823
21824
21825
21826
21827
21828
21829
21830
21831
21832
21833
21834
21835
21836
21837
21838
21839
21840
21841
21842
21843
21844
21845
21846
21847
21848
21849
21850
21851
21852
21853
21854
21855
21856
21857
21858
21859
21860
21861
21862
21863
21864
21865
21866
21867
21868
21869
21870
21871
21872
21873
21874
21875
21876
21877
21878
21879
21880
21881
21882
21883
21884
21885
21886
21887
21888
21889
21890
21891
21892
21893
21894
21895
21896
21897
21898
21899
21900
21901
21902
21903
21904
21905
21906
21907
21908
21909
21910
21911
21912
21913
21914
21915
21916
21917
21918
21919
21920
21921
21922
21923
21924
21925
21926
21927
21928
21929
21930
21931
21932
21933
21934
21935
21936
21937
21938
21939
21940
21941
21942
21943
21944
21945
21946
21947
21948
21949
21950
21951
21952
21953
21954
21955
21956
21957
21958
21959
21960
21961
21962
21963
21964
21965
21966
21967
21968
21969
21970
21971
21972
21973
21974
21975
21976
21977
21978
21979
21980
21981
21982
21983
21984
21985
21986
21987
21988
21989
21990
21991
21992
21993
21994
21995
21996
21997
21998
21999
22000
22001
22002
22003
22004
22005
22006
22007
22008
22009
22010
22011
22012
22013
22014
22015
22016
22017
22018
22019
22020
22021
22022
22023
22024
22025
22026
22027
22028
22029
22030
22031
22032
22033
22034
22035
22036
22037
22038
22039
22040
22041
22042
22043
22044
22045
22046
22047
22048
22049
22050
22051
22052
22053
22054
22055
22056
22057
22058
22059
22060
22061
22062
22063
22064
22065
22066
22067
22068
22069
22070
22071
22072
22073
22074
22075
22076
22077
22078
22079
22080
22081
22082
22083
22084
22085
22086
22087
22088
22089
22090
22091
22092
22093
22094
22095
22096
22097
22098
22099
22100
22101
22102
22103
22104
22105
22106
22107
22108
22109
22110
22111
22112
22113
22114
22115
22116
22117
22118
22119
22120
22121
22122
22123
22124
22125
22126
22127
22128
22129
22130
22131
22132
22133
22134
22135
22136
22137
22138
22139
22140
22141
22142
22143
22144
22145
22146
22147
22148
22149
22150
22151
22152
22153
22154
22155
22156
22157
22158
22159
22160
22161
22162
22163
22164
22165
22166
22167
22168
22169
22170
22171
22172
22173
22174
22175
22176
22177
22178
22179
22180
22181
22182
22183
22184
22185
22186
22187
22188
22189
22190
22191
22192
22193
22194
22195
22196
22197
22198
22199
22200
22201
22202
22203
22204
22205
22206
22207
22208
22209
22210
22211
22212
22213
22214
22215
22216
22217
22218
22219
22220
22221
22222
22223
22224
22225
22226
22227
22228
22229
22230
22231
22232
22233
22234
22235
22236
22237
22238
22239
22240
22241
22242
22243
22244
22245
22246
22247
22248
22249
22250
22251
22252
22253
22254
22255
22256
22257
22258
22259
22260
22261
22262
22263
22264
22265
22266
22267
22268
22269
22270
22271
22272
22273
22274
22275
22276
22277
22278
22279
22280
22281
22282
22283
22284
22285
22286
22287
22288
22289
22290
22291
22292
22293
22294
22295
22296
22297
22298
22299
22300
22301
22302
22303
22304
22305
22306
22307
22308
22309
22310
22311
22312
22313
22314
22315
22316
22317
22318
22319
22320
22321
22322
22323
22324
22325
22326
22327
22328
22329
22330
22331
22332
22333
22334
22335
22336
22337
22338
22339
22340
22341
22342
22343
22344
22345
22346
22347
22348
22349
22350
22351
22352
22353
22354
22355
22356
22357
22358
22359
22360
22361
22362
22363
22364
22365
22366
22367
22368
22369
22370
22371
22372
22373
22374
22375
22376
22377
22378
22379
22380
22381
22382
22383
22384
22385
22386
22387
22388
22389
22390
22391
22392
22393
22394
22395
22396
22397
22398
22399
22400
22401
22402
22403
22404
22405
22406
22407
22408
22409
22410
22411
22412
22413
22414
22415
22416
22417
22418
22419
22420
22421
22422
22423
22424
22425
22426
22427
22428
22429
22430
22431
22432
22433
22434
22435
22436
22437
22438
22439
22440
22441
22442
22443
22444
22445
22446
22447
22448
22449
22450
22451
22452
22453
22454
22455
22456
22457
22458
22459
22460
22461
22462
22463
22464
22465
22466
22467
22468
22469
22470
22471
22472
22473
22474
22475
22476
22477
22478
22479
22480
22481
22482
22483
22484
22485
22486
22487
22488
22489
22490
22491
22492
22493
22494
22495
22496
22497
22498
22499
22500
22501
22502
22503
22504
22505
22506
22507
22508
22509
22510
22511
22512
22513
22514
22515
22516
22517
22518
22519
22520
22521
22522
22523
22524
22525
22526
22527
22528
22529
22530
22531
22532
22533
22534
22535
22536
22537
22538
22539
22540
22541
22542
22543
22544
22545
22546
22547
22548
22549
22550
22551
22552
22553
22554
22555
22556
22557
22558
22559
22560
22561
22562
22563
22564
22565
22566
22567
22568
22569
22570
22571
22572
22573
22574
22575
22576
22577
22578
22579
22580
22581
22582
22583
22584
22585
22586
22587
22588
22589
22590
22591
22592
22593
22594
22595
22596
22597
22598
22599
22600
22601
22602
22603
22604
22605
22606
22607
22608
22609
22610
22611
22612
22613
22614
22615
22616
22617
22618
22619
22620
22621
22622
22623
22624
22625
22626
22627
22628
22629
22630
22631
22632
22633
22634
22635
22636
22637
22638
22639
22640
22641
22642
22643
22644
22645
22646
22647
22648
22649
22650
22651
22652
22653
22654
22655
22656
22657
22658
22659
22660
22661
22662
22663
22664
22665
22666
22667
22668
22669
22670
22671
22672
22673
22674
22675
22676
22677
22678
22679
22680
22681
22682
22683
22684
22685
22686
22687
22688
22689
22690
22691
22692
22693
22694
22695
22696
22697
22698
22699
22700
22701
22702
22703
22704
22705
22706
22707
22708
22709
22710
22711
22712
22713
22714
22715
22716
22717
22718
22719
22720
22721
22722
22723
22724
22725
22726
22727
22728
22729
22730
22731
22732
22733
22734
22735
22736
22737
22738
22739
22740
22741
22742
22743
22744
22745
22746
22747
22748
22749
22750
22751
22752
22753
22754
22755
22756
22757
22758
22759
22760
22761
22762
22763
22764
22765
22766
22767
22768
22769
22770
22771
22772
22773
22774
22775
22776
22777
22778
22779
22780
22781
22782
22783
22784
22785
22786
22787
22788
22789
22790
22791
22792
22793
22794
22795
22796
22797
22798
22799
22800
22801
22802
22803
22804
22805
22806
22807
22808
22809
22810
22811
22812
22813
22814
22815
22816
22817
22818
22819
22820
22821
22822
22823
22824
22825
22826
22827
22828
22829
22830
22831
22832
22833
22834
22835
22836
22837
22838
22839
22840
22841
22842
22843
22844
22845
22846
22847
22848
22849
22850
22851
22852
22853
22854
22855
22856
22857
22858
22859
22860
22861
22862
22863
22864
22865
22866
22867
22868
22869
22870
22871
22872
22873
22874
22875
22876
22877
22878
//! Admin route handlers — `/cms-admin/...`.

use crate::log_err::LogErr as _;
use super::{AdminError, PageForm};
use crate::locale::Locale;
use crate::media::Media;
use crate::page::Page;
use crate::page_type::find_handler;
use crate::page_type_model::PageType;
use crate::tree_ops::NewPage;
use axum::extract::{Form, Path, Query, State};
use axum::http::{header, HeaderMap, HeaderValue};
use axum::response::{Html, IntoResponse, Redirect, Response};
use axum::Json;
use rustango::core::Column as _;
use rustango::extractors::Tenant;
use rustango::messages::Level as MsgLevel;
use rustango::sql::{transaction_pool, FetcherPool as _, FetcherTx as _};
use serde::Deserialize;
use std::sync::OnceLock;
use tera::Context;
use crate::security_log::Entry as SecurityEntry;

/// HMAC key for the flash-messages cookie, derived from the CMS signing
/// secret so every replica — and the same server after a restart — reads
/// the flash another one set.
fn messages_secret() -> &'static [u8] {
    static SECRET: OnceLock<Vec<u8>> = OnceLock::new();
    SECRET.get_or_init(|| crate::signing::derived_key("rcms:flash"))
}

/// Sanitize WYSIWYG (RichText-widget) HTML server-side before it
/// reaches a page-type handler's `save_extension`. The TipTap editor
/// produces constrained HTML, but a bypassed client could POST anything,
/// so we ammonia-clean every RichText-kind field value (looked up by the
/// handler's own widget definitions) before persisting. This is the
/// on-save enforcement point for extension fields; stream rich-text blocks
/// + markdown bodies already sanitize on their own render paths, and the
/// `| richtext` filter stays a pure linktype-rewriter that trusts the now-
/// sanitized stored HTML. Returns a sanitized clone of `form_map`.
async fn sanitized_extension_form(
    handler: &dyn crate::page_type::PageTypeHandler,
    pool: &rustango::sql::Pool,
    page_id: i64,
    form_map: &std::collections::HashMap<String, String>,
) -> std::collections::HashMap<String, String> {
    let mut out = form_map.clone();
    if let Ok(widgets) = handler.widgets(pool, page_id).await {
        for w in widgets {
            if w.kind == crate::widget::WidgetKind::RichText {
                if let Some(raw) = out.get(&w.name) {
                    let cleaned = crate::markdown::sanitize_html(raw);
                    out.insert(w.name.clone(), cleaned);
                }
            }
        }
    }
    out
}

/// How long a preview token stays valid: long enough for an editor to
/// click through and iterate, short enough that a URL pasted into a chat
/// stops working before it becomes a way in.
const PREVIEW_TOKEN_TTL_SECS: i64 = 3600;

/// Render a CMS-admin Tera template with the CSRF token + drained
/// flash messages stamped into the context. Returns a `Response`
/// that carries:
///   - any `Set-Cookie` the CSRF middleware needs to mint a fresh
///     token (first GET per session, or when the cookie was
///     invalidated);
///   - the clear-cookie that empties the messages store so each
///     flash renders exactly once.
///
/// Every form-rendering admin handler routes through this helper so
/// `{{ csrf_token | csrf_input | safe }}` and
/// `{% for msg in messages %}…{% endfor %}` in templates always
/// resolve correctly.
pub(crate) fn render_with_csrf(
    state: &super::AdminState,
    headers: &HeaderMap,
    template: &str,
    ctx: &mut Context,
) -> Result<Response, AdminError> {
    // #525/#526 — resolve the admin UI locale per request and expose it to the
    // template. Done here so every admin page picks it up without threading
    // through each handler. Precedence: sticky cookie → per-user preference
    // (#526, surfaced into the ctx by `add_user_chrome`) → Accept-Language →
    // per-tenant default (#526) → English.
    let user_pref = ctx
        .get("user_lang_pref")
        .and_then(|v| v.as_str())
        .map(str::to_owned);
    let tenant_default = super::i18n::tenant_default_locale();
    let lang = super::i18n::negotiate(
        headers.get(header::COOKIE).and_then(|v| v.to_str().ok()),
        headers
            .get(header::ACCEPT_LANGUAGE)
            .and_then(|v| v.to_str().ok()),
        user_pref.as_deref(),
        tenant_default.as_deref(),
    );
    let lang_dir = rustango::i18n::text_direction(&lang);
    ctx.insert("LANG", &lang);
    ctx.insert("LANG_DIR", lang_dir);
    let csrf_cookie = rustango::forms::csrf::stamp_into_context(headers, ctx);
    let msg_clear = rustango::messages::stamp_into_context(messages_secret(), headers, ctx);
    let html = state.tera.render(template, ctx)?;
    let mut resp = Html(html).into_response();
    if let Some(c) = csrf_cookie {
        if let Ok(v) = HeaderValue::from_str(&c) {
            resp.headers_mut().append(header::SET_COOKIE, v);
        }
    }
    if let Some(c) = msg_clear {
        if let Ok(v) = HeaderValue::from_str(&c) {
            resp.headers_mut().append(header::SET_COOKIE, v);
        }
    }
    Ok(resp)
}

/// Redirect to a named route registered in [`crate::urls`] AND
/// stage a flash message ([`rustango::messages`]) in the response
/// cookie so the next GET at the destination renders it through
/// the `messages` Tera context variable. The standard "POST → 303
/// → render flash" idiom — every cms-admin POST handler returns
/// through this (or its `_with_params` cousin) so editors get
/// real feedback instead of a silent navigation.
fn redirect_named_with_message(
    name: &'static str,
    level: MsgLevel,
    body: &str,
    headers: &HeaderMap,
) -> Result<Response, AdminError> {
    let url = rustango::urls::reverse(name, &std::collections::HashMap::new())
        .map_err(|e| AdminError::Validation(format!("url reversal of `{name}` failed: {e}")))?;
    // #528 — localize the flash for the request's admin locale (English source
    // is the key; untranslated → unchanged).
    let body = super::i18n::tr(headers, body, &[]);
    Ok(rustango::messages::redirect_with_message(
        messages_secret(),
        headers,
        level,
        &body,
        &url,
    ))
}

/// Like [`redirect_named_with_message`] but the message is a **parameterized**
/// translation key: the English source — with `{name}` placeholders — is
/// the catalog key, localized + interpolated for the request locale before
/// staging. Use instead of `&format!(…)` so the flash localizes. The framework
/// substitutes placeholders into the source-string fallback too, so English
/// (empty catalog) renders correctly without a catalog entry.
fn redirect_named_with_message_args(
    name: &'static str,
    level: MsgLevel,
    key: &str,
    args: &[(&str, &str)],
    headers: &HeaderMap,
) -> Result<Response, AdminError> {
    let url = rustango::urls::reverse(name, &std::collections::HashMap::new())
        .map_err(|e| AdminError::Validation(format!("url reversal of `{name}` failed: {e}")))?;
    let body = super::i18n::tr(headers, key, args);
    Ok(rustango::messages::redirect_with_message(
        messages_secret(),
        headers,
        level,
        &body,
        &url,
    ))
}

/// Count-aware cousin of [`redirect_named_with_message_args`]:
/// `key` is a plural source key, the form is chosen for `n` in the request
/// locale (CLDR rules) and `args` interpolated. Pass the count as an arg too
/// (e.g. `("count", &n.to_string())`) so the chosen form's `{count}` fills in.
fn redirect_named_with_message_plural(
    name: &'static str,
    level: MsgLevel,
    key: &str,
    n: i64,
    args: &[(&str, &str)],
    headers: &HeaderMap,
) -> Result<Response, AdminError> {
    let url = rustango::urls::reverse(name, &std::collections::HashMap::new())
        .map_err(|e| AdminError::Validation(format!("url reversal of `{name}` failed: {e}")))?;
    let body = super::i18n::tr_plural(headers, key, n, args);
    Ok(rustango::messages::redirect_with_message(
        messages_secret(),
        headers,
        level,
        &body,
        &url,
    ))
}

/// Plural cousin of [`redirect_named_with_params_and_message`] —
/// for routes with path params AND a count-aware message.
fn redirect_named_with_params_and_message_plural(
    name: &'static str,
    params: &[(&str, String)],
    level: MsgLevel,
    key: &str,
    n: i64,
    args: &[(&str, &str)],
    headers: &HeaderMap,
) -> Result<Response, AdminError> {
    let owned: std::collections::HashMap<String, String> = params
        .iter()
        .map(|(k, v)| ((*k).to_owned(), v.clone()))
        .collect();
    let url = rustango::urls::reverse_owned(name, &owned)
        .map_err(|e| AdminError::Validation(format!("url reversal of `{name}` failed: {e}")))?;
    let body = super::i18n::tr_plural(headers, key, n, args);
    Ok(rustango::messages::redirect_with_message(
        messages_secret(),
        headers,
        level,
        &body,
        &url,
    ))
}

/// Friendly 401 response for handlers that bail when
/// `SessionUser` resolves to `None`. Two content-types based on
/// `Accept`:
///
/// - **`application/json`** — JSON `{ error: "session_expired",
///   message, login_url }`. XHR / fetch clients (notably the bulk
///   media uploader) used to inline-dump the HTML page into a toast,
///   which read as garbage. JSON lets the client render a clean
///   "Sign in again to keep uploading" banner instead.
/// - **Anything else** — the HTML explainer page below, useful for
///   `<form>` POSTs that hit Submit after the cookie went stale.
///
/// The middleware-level `with_login_required` layer already
/// 302s anonymous GETs to /login. A POST hitting this guard means
/// the cookie was DECODED as anonymous (expired / wrong tenant /
/// stale), so a hard 401 with an explanation lands better than a
/// redirect loop.
fn unauthorized_no_session() -> Response {
    unauthorized_no_session_for(None)
}

/// Content-negotiating variant of [`unauthorized_no_session`]. Pass
/// the request `HeaderMap` so the client's `Accept` (and the legacy
/// `X-Requested-With: XMLHttpRequest`) can pick between the JSON
/// payload and the HTML page.
fn unauthorized_no_session_for(headers: Option<&HeaderMap>) -> Response {
    let wants_json = headers
        .map(|h| {
            let accept = h
                .get(header::ACCEPT)
                .and_then(|v| v.to_str().ok())
                .unwrap_or("");
            if accept.contains("application/json") {
                return true;
            }
            h.get("x-requested-with")
                .and_then(|v| v.to_str().ok())
                .is_some_and(|v| v.eq_ignore_ascii_case("XMLHttpRequest"))
        })
        .unwrap_or(false);
    // #528 — localize for the request's admin locale when headers are present
    // (this is the one polished user-facing admin error page); English otherwise.
    let t = |key: &str| -> String {
        match headers {
            Some(h) => super::i18n::tr(h, key, &[]),
            None => key.to_owned(),
        }
    };
    if wants_json {
        let payload = serde_json::json!({
            "error": "session_expired",
            "message": t("Sign in again to keep uploading."),
            "login_url": "/login?next=/cms-admin/",
        });
        return (
            axum::http::StatusCode::UNAUTHORIZED,
            [(axum::http::header::CONTENT_TYPE, "application/json")],
            serde_json::to_string(&payload).unwrap_or_else(|_| "{}".to_owned()),
        )
            .into_response();
    }
    let lang = headers
        .map(|h| {
            super::i18n::negotiate(
                h.get(header::COOKIE).and_then(|v| v.to_str().ok()),
                h.get(header::ACCEPT_LANGUAGE).and_then(|v| v.to_str().ok()),
                None,
                super::i18n::tenant_default_locale().as_deref(),
            )
        })
        .unwrap_or_else(|| "en".to_owned());
    let dir = rustango::i18n::text_direction(&lang);
    const STYLE: &str = r#"<style>
body{font:14px/1.5 -apple-system,system-ui,sans-serif;color:#222;background:#fafafa;margin:0;padding:48px 20px}
.box{max-width:560px;margin:0 auto;padding:32px;background:#fff;border-radius:12px;box-shadow:0 2px 8px rgba(0,0,0,.08)}
h1{font-size:18px;margin:0 0 12px}
p{margin:0 0 12px;color:#444}
ul{margin:0 0 12px;padding-left:20px;color:#444}
li{margin:4px 0}
a{color:#1565c0}
</style>"#;
    let mut body = String::with_capacity(STYLE.len() + 1024);
    body.push_str("<!doctype html>\n<html lang=\"");
    body.push_str(&crate::forms::render::html_escape(&lang));
    body.push_str("\" dir=\"");
    body.push_str(dir);
    body.push_str("\"><head><meta charset=\"utf-8\"><title>");
    body.push_str(&crate::forms::render::html_escape(&t("Session expired")));
    body.push_str(" — ");
    body.push_str(&crate::forms::render::html_escape(&t("CMS Admin")));
    body.push_str("</title>");
    body.push_str(STYLE);
    body.push_str("</head><body><div class=\"box\">\n<h1>");
    body.push_str(&crate::forms::render::html_escape(&t(
        "Your session has expired",
    )));
    body.push_str("</h1>\n<p>");
    body.push_str(&crate::forms::render::html_escape(&t(
        "The form couldn't be saved because your admin session decoded as anonymous. The most likely causes:",
    )));
    body.push_str("</p>\n<ul>\n<li>");
    body.push_str(&crate::forms::render::html_escape(&t(
        "Your login session expired or was signed out from another tab.",
    )));
    body.push_str("</li>\n<li>");
    body.push_str(&crate::forms::render::html_escape(&t(
        "The cookie was issued for a different tenant slug.",
    )));
    body.push_str("</li>\n<li>");
    body.push_str(&crate::forms::render::html_escape(&t(
        "On sqlite/mysql builds the framework's tenant session store may not be wired up yet (see issue #259).",
    )));
    body.push_str("</li>\n</ul>\n<p><a href=\"/login?next=/cms-admin/\">");
    body.push_str(&crate::forms::render::html_escape(&t("Sign back in")));
    body.push_str("</a>");
    body.push_str(&crate::forms::render::html_escape(&t(
        ", then re-submit the form.",
    )));
    body.push_str("</p>\n</div></body></html>");
    (
        axum::http::StatusCode::UNAUTHORIZED,
        [(axum::http::header::CONTENT_TYPE, "text/html; charset=utf-8")],
        body,
    )
        .into_response()
}

/// Like [`redirect_named_with_message`], but for routes with path
/// parameters (e.g. `rcms-admin:pages:edit` needing `{id}`).
pub(crate) fn redirect_named_with_params_and_message(
    name: &'static str,
    params: &[(&str, String)],
    level: MsgLevel,
    body: &str,
    headers: &HeaderMap,
) -> Result<Response, AdminError> {
    let owned: std::collections::HashMap<String, String> = params
        .iter()
        .map(|(k, v)| ((*k).to_owned(), v.clone()))
        .collect();
    let url = rustango::urls::reverse_owned(name, &owned)
        .map_err(|e| AdminError::Validation(format!("url reversal of `{name}` failed: {e}")))?;
    // #528 — localize the flash for the request's admin locale.
    let body = super::i18n::tr(headers, body, &[]);
    Ok(rustango::messages::redirect_with_message(
        messages_secret(),
        headers,
        level,
        &body,
        &url,
    ))
}

/// Parameterized-key cousin of [`redirect_named_with_params_and_message`]
/// for routes that take path params AND a localized message with `{name}`
/// interpolation.
pub(crate) fn redirect_named_with_params_and_message_args(
    name: &'static str,
    params: &[(&str, String)],
    level: MsgLevel,
    key: &str,
    args: &[(&str, &str)],
    headers: &HeaderMap,
) -> Result<Response, AdminError> {
    let owned: std::collections::HashMap<String, String> = params
        .iter()
        .map(|(k, v)| ((*k).to_owned(), v.clone()))
        .collect();
    let url = rustango::urls::reverse_owned(name, &owned)
        .map_err(|e| AdminError::Validation(format!("url reversal of `{name}` failed: {e}")))?;
    let body = super::i18n::tr(headers, key, args);
    Ok(rustango::messages::redirect_with_message(
        messages_secret(),
        headers,
        level,
        &body,
        &url,
    ))
}

/// Resolve the post-save redirect from the three-action save
/// footer (`_save` / `_continue` /
/// `_addanother`). Shared by `page_new_submit` and `page_edit_submit`
/// so both honour the same button the editor pressed:
///
/// * `continue`   → back to **this** page's edit form (keep editing).
/// * `addanother` → a fresh new-page form, preselecting the same parent
///   so the next page lands as a sibling (`pages:new` honours
///   `?parent=<id>`); root pages omit the query.
/// * anything else (incl. the default `save` / missing) → the page list.
///
/// `body` must already be localized by the caller (via `tr` / `tr_plural`) —
/// this dispatcher only resolves the destination URL and stages the message,
/// so every branch (including `addanother`) treats `body` identically.
fn redirect_after_page_save(
    action: &str,
    page_id: i64,
    parent_id: Option<i64>,
    body: &str,
    headers: &HeaderMap,
) -> Result<Response, AdminError> {
    let url = match action {
        "continue" => rustango::urls::reverse_owned(
            "rcms-admin:pages:edit",
            &std::iter::once(("id".to_owned(), page_id.to_string())).collect(),
        )
        .map_err(|e| AdminError::Validation(format!("url reversal of `pages:edit` failed: {e}")))?,
        "addanother" => {
            let base =
                rustango::urls::reverse("rcms-admin:pages:new", &std::collections::HashMap::new())
                    .map_err(|e| {
                        AdminError::Validation(format!("url reversal of `pages:new` failed: {e}"))
                    })?;
            match parent_id {
                Some(pid) => format!("{base}?parent={pid}"),
                None => base,
            }
        }
        _ => rustango::urls::reverse("rcms-admin:pages:list", &std::collections::HashMap::new())
            .map_err(|e| {
                AdminError::Validation(format!("url reversal of `pages:list` failed: {e}"))
            })?,
    };
    Ok(rustango::messages::redirect_with_message(
        messages_secret(),
        headers,
        MsgLevel::Success,
        body,
        &url,
    ))
}

/// A user's email: the `email` column — what member sign-in, SSO and
/// sign-up use — else the address older admin versions kept in
/// `data.email`. Empty when neither is set.
pub(crate) fn user_email(u: &rustango::tenancy::auth::User) -> String {
    u.email
        .as_deref()
        .map(str::trim)
        .filter(|e| !e.is_empty())
        .or_else(|| u.data.get("email").and_then(|v| v.as_str()).map(str::trim))
        .unwrap_or("")
        .to_owned()
}

/// Add the chrome variables every section template reads from
/// `_base.html`: which sidebar tab is active, the brand-display
/// name, the current tenant slug, and — when a [`SessionUser`](rustango::extractors::SessionUser) is
/// in scope — the sidebar gating set + per-user timezone.
///
/// Pass `session_user.as_ref()` from the handler signature; pass
/// `None` for anon flows (login / password-reset). When `None`,
/// `sidebar_gating` stays `false` so every sidebar item renders.
///
/// Public so a host application's [`AdminPageHandler`] can render inside
/// the admin chrome. Without it the extension point only works for a page
/// that does NOT extend `rcms_admin/_base.html`, since that template
/// dereferences roughly two dozen chrome variables — and hand-supplying
/// them in a host app is a copy that drifts silently on every release.
///
/// [`AdminPageHandler`]: super::admin_page::AdminPageHandler
pub async fn add_chrome(
    ctx: &mut Context,
    tenant: &Tenant,
    active_tab: &str,
    session_user: Option<&rustango::tenancy::auth::User>,
) {
    add_chrome_sync(ctx, tenant, active_tab);
    if let Some(user) = session_user {
        add_user_chrome(ctx, user, tenant.pool()).await;
    }
    // #199 — per-tenant logo + favicon URLs (see [`add_branding_urls`]).
    add_branding_urls(ctx, tenant).await;
}

/// Inject the per-tenant admin logo + favicon URLs from the `branding`
/// site setting into a template context. Missing/empty rows leave the ctx slots
/// unset, so templates fall back to the brand-name text + default icon. Shared
/// by the authenticated chrome ([`add_chrome`]) and the pre-auth login /
/// password-reset screens (which have no `add_chrome`), so those can show the
/// tenant's branding too. The serve routes (`/__cms-branding/*`) live in
/// `public_router` so the images load before sign-in.
pub(crate) async fn add_branding_urls(ctx: &mut Context, tenant: &Tenant) {
    // The site's name as visitors know it: the owner's Branding "Site
    // name", else the tenant's display name (set by the operator).
    let mut site_name = tenant.org.display_name.clone();
    if let Ok(Some(row)) = crate::site_setting::get(tenant.pool(), "branding").await {
        if let Some(n) = row.value_json.get("site_name").and_then(|v| v.as_str()).filter(|n| !n.trim().is_empty()) {
            n.trim().clone_into(&mut site_name);
        }
        let is_set = |key: &str| {
            row.value_json
                .get(key)
                .and_then(|v| v.as_str())
                .is_some_and(|s| !s.is_empty())
        };
        if is_set("logo_storage_key") {
            ctx.insert("branding_logo_url", "/__cms-branding/logo");
        }
        if is_set("favicon_storage_key") {
            ctx.insert("branding_favicon_url", "/__cms-branding/favicon");
        }
    }
    ctx.insert("site_name", &site_name);
}

/// Sync core of [`add_chrome`] — every call site benefits from
/// re-using it, so it's broken out for clarity. Stuffs the static
/// chrome bits (tab, tenant, brand, default tz, registered admin
/// pages) into the context.
fn add_chrome_sync(ctx: &mut Context, tenant: &Tenant, active_tab: &str) {
    ctx.insert("active_tab", active_tab);
    ctx.insert("tenant_slug", &tenant.org.slug);
    // #524 — admin UI locale + text direction for `<html lang/dir>` and the
    // `translate(locale=LANG)` calls. The default here is overridden per
    // request by `render_with_csrf` (#525 negotiation).
    ctx.insert("LANG", "en");
    ctx.insert("LANG_DIR", "ltr");
    // #525 — the shipped UI locales, for the language switcher.
    ctx.insert(
        "ui_locales",
        &super::i18n::UI_LOCALES
            .iter()
            .map(|(c, n)| serde_json::json!({ "code": c, "name": n }))
            .collect::<Vec<_>>(),
    );
    let brand = tenant
        .org
        .brand_name
        .clone()
        .filter(|s| !s.is_empty())
        .unwrap_or_else(|| tenant.org.display_name.clone());
    ctx.insert("brand_name", &brand);
    // user_timezone defaults to empty; PG-authenticated requests
    // get it filled in via `add_user_chrome` (called explicitly by
    // handlers that have a SessionUser extractor in scope).
    ctx.insert("user_timezone", "");
    // #13 V2 — tenant default timezone. Sourced from
    // `RUSTANGO_CMS_DEFAULT_TIMEZONE` so multi-tenant deployments can
    // pin a sensible regional default without a schema change. The
    // boot script in _base.html resolves in order:
    //   user pref → tenant default → browser default.
    // Process configuration, read once rather than on every admin page.
    static DEFAULT_TZ: std::sync::OnceLock<String> = std::sync::OnceLock::new();
    let tenant_default_tz = DEFAULT_TZ
        .get_or_init(|| crate::config::var("DEFAULT_TIMEZONE").unwrap_or_default())
        .clone();
    ctx.insert("tenant_default_timezone", &tenant_default_tz);

    // Custom admin pages (#23) — bucket every registered handler by
    // its declared sidebar section so `_base.html` can group them
    // under the right heading. Sort within each bucket by
    // `(sort_order, label)` so authors can pin position.
    let mut by_section: std::collections::BTreeMap<String, Vec<serde_json::Value>> =
        std::collections::BTreeMap::new();
    for handler in super::admin_page::registered_admin_pages() {
        let section = handler.section().label().to_owned();
        by_section
            .entry(section)
            .or_default()
            .push(serde_json::json!({
                "slug": handler.slug(),
                "label": handler.label(),
                "icon": handler.icon(),
                "sort_order": handler.sort_order(),
            }));
    }
    for items in by_section.values_mut() {
        items.sort_by(|a, b| {
            let ao = a.get("sort_order").and_then(|v| v.as_i64()).unwrap_or(100);
            let bo = b.get("sort_order").and_then(|v| v.as_i64()).unwrap_or(100);
            ao.cmp(&bo).then_with(|| {
                a.get("label")
                    .and_then(|v| v.as_str())
                    .unwrap_or("")
                    .cmp(b.get("label").and_then(|v| v.as_str()).unwrap_or(""))
            })
        });
    }
    ctx.insert("extra_admin_pages_by_section", &by_section);
    // #439 — registry-driven report links. `_base.html` lists these
    // under the Reports group below the built-in reports, so a
    // `register_report!`ed report shows up in the nav with no template
    // edit. Already title-sorted by `registered_reports()`.
    let reports_nav: Vec<serde_json::Value> = super::report::registered_reports()
        .iter()
        .map(|r| {
            serde_json::json!({
                "slug": r.slug(),
                "title": r.title(),
                "icon": r.icon(),
            })
        })
        .collect();
    ctx.insert("registered_reports", &reports_nav);
    // #107 — plugin-contributed admin menu items (runtime hook B13).
    // Rendered at the bottom of the sidebar in stable key order.
    let plugin_menu_items: Vec<serde_json::Value> = crate::hooks::admin_menu_items()
        .into_iter()
        .map(|i| {
            serde_json::json!({
                "key": i.key,
                "label": i.label,
                "icon": i.icon,
                "href": i.href,
            })
        })
        .collect();
    ctx.insert("plugin_admin_menu_items", &plugin_menu_items);
    // #127 — help-menu entries. Rendered in a
    // dedicated "Help" sidebar section above Plugins.
    let help_menu_items: Vec<serde_json::Value> = crate::hooks::help_menu_items()
        .into_iter()
        .map(|i| {
            serde_json::json!({
                "key": i.key,
                "label": i.label,
                "icon": i.icon,
                "href": i.href,
            })
        })
        .collect();
    ctx.insert("help_menu_items", &help_menu_items);
    // #422 — plugin-injected global admin CSS / JS. `_base.html` emits each blob inside
    // a <style> / <script> tag.
    ctx.insert("admin_css_blobs", &crate::hooks::admin_css());
    ctx.insert("admin_js_blobs", &crate::hooks::admin_js());
    // Sidebar gating defaults: when no SessionUser is in scope (anon
    // public_router flows like login + password-reset), keep every
    // sidebar item visible. Handlers with a `SessionUser` extractor
    // override this via `add_user_chrome` below.
    ctx.insert("sidebar_gating", &false);
    ctx.insert("user_is_superuser", &false);
    ctx.insert(
        "allowed_resources",
        &std::collections::HashSet::<String>::new(),
    );
}

/// Per-request chrome extension for authenticated requests. Fills in
/// the bits `add_chrome` can't compute without a user in scope:
///
/// - `user_timezone` from `User.data.timezone`. Empty when
///   unset so the no-flash JS falls back to the browser TZ.
/// - `allowed_resources`: the union of role codenames the user
///   inherits, used to gate sidebar items + admin-side CRUD chips.
///   Superusers skip the gating set entirely (the sidebar
///   template checks `user_is_superuser` first).
/// - `user_is_superuser`: short-circuit flag exposed to templates so
///   they don't iterate the codename set when access is unconditional.
/// - `sidebar_gating: true`: tells `_base.html` to start honoring
///   the gating set (anon flows leave this `false` so login pages
///   render their full chrome).
pub(crate) async fn add_user_chrome(
    ctx: &mut Context,
    user: &rustango::tenancy::auth::User,
    pool: &rustango::sql::Pool,
) {
    let user_timezone = user
        .data
        .get("timezone")
        .and_then(serde_json::Value::as_str)
        .unwrap_or("")
        .to_owned();
    ctx.insert("user_timezone", &user_timezone);
    // #526 — durable per-user admin UI language. Surfaced into the ctx here (the
    // one place with the authenticated user) so `render_with_csrf` can fold it
    // into locale negotiation without threading the user through every handler.
    if let Some(pref) = user
        .data
        .get("admin_lang")
        .and_then(serde_json::Value::as_str)
        .filter(|s| super::i18n::is_ui_locale(s))
    {
        ctx.insert("user_lang_pref", pref);
    }
    ctx.insert("user_is_superuser", &user.is_superuser);
    ctx.insert("user_username", &user.username);
    // #201 — profile chrome. The display name lives in
    // rustango_users.data.display_name; fall back to the username.
    // Avatar lives at /__cms-avatar/<user_id> when the user uploaded
    // one (avatar_path is set as a side-effect of the upload route).
    let display_name = user
        .data
        .get("display_name")
        .and_then(serde_json::Value::as_str)
        .filter(|s| !s.is_empty())
        .unwrap_or(&user.username)
        .to_owned();
    ctx.insert("user_display_name", &display_name);
    let has_avatar = user
        .data
        .get("avatar_path")
        .and_then(serde_json::Value::as_str)
        .map(|s| !s.is_empty())
        .unwrap_or(false);
    if has_avatar {
        let uid = user.id.get().copied().unwrap_or(0);
        ctx.insert("user_avatar_url", &format!("/__cms-avatar/{uid}"));
    }
    let codenames = if user.is_superuser {
        // Superusers bypass gating; we still hand the template an
        // empty set so unconditional checks work without branching.
        std::collections::HashSet::<String>::new()
    } else {
        let uid = user.id.get().copied().unwrap_or(0);
        crate::permissions::user_codenames(pool, uid)
            .await
            .unwrap_or_default()
    };
    ctx.insert("allowed_resources", &codenames);
    ctx.insert("sidebar_gating", &true);
}

/// GET /cms-admin/media/collections — manage every collection in the
/// tenant. Tree view with depth-indented names, per-row rename /
/// delete + a "new" form per parent.
pub async fn collections_list(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
) -> Result<Response, AdminError> {
    let collections: Vec<crate::media::MediaCollection> = crate::media::MediaCollection::objects()
        .order_by(&[("parent_id", false), ("sort_order", false), ("name", false)])
        .fetch(tenant.pool())
        .await?;
    // Walk into a depth-tagged tree so the template can render
    // indented names without computing the depth itself.
    let by_id: std::collections::HashMap<i64, &crate::media::MediaCollection> = collections
        .iter()
        .filter_map(|c| c.id.get().copied().map(|id| (id, c)))
        .collect();
    fn depth(
        c: &crate::media::MediaCollection,
        by_id: &std::collections::HashMap<i64, &crate::media::MediaCollection>,
        cache: &mut std::collections::HashMap<i64, i32>,
    ) -> i32 {
        let id = c.id.get().copied().unwrap_or_default();
        if let Some(d) = cache.get(&id) {
            return *d;
        }
        let d = match c.parent_id {
            None => 0,
            Some(pid) => match by_id.get(&pid) {
                Some(parent) => depth(parent, by_id, cache) + 1,
                None => 0,
            },
        };
        cache.insert(id, d);
        d
    }
    // Media counts per collection so editors see usage at a glance.
    let media: Vec<Media> = Media::objects().fetch(tenant.pool()).await?;
    let mut counts: std::collections::HashMap<i64, i64> = std::collections::HashMap::new();
    for m in &media {
        if let Some(cid) = m.collection_id {
            *counts.entry(cid).or_insert(0) += 1;
        }
    }
    let mut depth_cache: std::collections::HashMap<i64, i32> = std::collections::HashMap::new();
    let rows: Vec<serde_json::Value> = collections
        .iter()
        .map(|c| {
            let id = c.id.get().copied().unwrap_or_default();
            serde_json::json!({
                "id": id,
                "name": c.name,
                "parent_id": c.parent_id,
                // #318 — resolve the parent FK to its display name so the
                // list shows "Photos" instead of a raw `#17`.
                "parent_name": c.parent_id.and_then(|pid| by_id.get(&pid)).map(|p| p.name.clone()),
                "sort_order": c.sort_order,
                "depth": depth(c, &by_id, &mut depth_cache),
                "media_count": counts.get(&id).copied().unwrap_or(0),
                "created_at": c.created_at.get().copied(),
            })
        })
        .collect();

    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "media", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("collections", &rows);
    render_with_csrf(
        &state,
        &headers,
        "rcms_admin/collections_list.html",
        &mut ctx,
    )
}

/// Form payload for create / rename of a collection.
#[derive(Debug, Deserialize)]
pub struct CollectionForm {
    pub name: String,
    #[serde(default, deserialize_with = "super::deserialize_optional_i64")]
    pub parent_id: Option<i64>,
    #[serde(default)]
    pub sort_order: Option<i32>,
}

/// POST /cms-admin/media/collections/new — create.
pub async fn collections_create(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Form(form): Form<CollectionForm>,
) -> Result<Response, AdminError> {
    let name = form.name.trim().to_owned();
    if name.is_empty() {
        return Err(AdminError::Validation(
            "Collection name is required.".to_owned(),
        ));
    }
    let mut row = crate::media::MediaCollection {
        id: rustango::sql::Auto::Unset,
        name: name.clone(),
        parent_id: form.parent_id,
        sort_order: form.sort_order.unwrap_or(100),
        created_at: rustango::sql::Auto::Unset,
    };
    row.insert_pool(tenant.pool()).await?;
    redirect_named_with_message_args(
        "rcms-admin:media:collections",
        MsgLevel::Success,
        "Collection “{name}” created.",
        &[("name", name.as_str())],
        &headers,
    )
}

/// POST /cms-admin/media/collections/{id}/edit — rename + re-parent
/// + reorder. The form posts all three fields; missing parent_id
/// (empty / "0") sends the collection to the root.
pub async fn collections_edit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
    Form(form): Form<CollectionForm>,
) -> Result<Response, AdminError> {
    let mut row = crate::media::MediaCollection::objects()
        .where_(crate::media::MediaCollection::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    let name = form.name.trim().to_owned();
    if name.is_empty() {
        return Err(AdminError::Validation(
            "Collection name is required.".to_owned(),
        ));
    }
    // Guard against parent_id = self (would orphan the row from
    // the tree walk).
    if form.parent_id == Some(id) {
        return Err(AdminError::Validation(
            "A collection cannot be its own parent.".to_owned(),
        ));
    }
    row.name = name;
    row.parent_id = form.parent_id;
    if let Some(so) = form.sort_order {
        row.sort_order = so;
    }
    row.save_pool(tenant.pool()).await?;
    redirect_named_with_message(
        "rcms-admin:media:collections",
        MsgLevel::Success,
        "Collection saved.",
        &headers,
    )
}

/// POST /cms-admin/media/collections/{id}/delete — soft delete via
/// re-parent then delete. Child collections are re-parented to the
/// deleted row's parent; media rows lose their collection_id (FK
/// nulls).
pub async fn collections_delete(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
) -> Result<Response, AdminError> {
    let row = crate::media::MediaCollection::objects()
        .where_(crate::media::MediaCollection::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    let parent = row.parent_id;
    let name = row.name.clone();
    // Re-parent children so the subtree doesn't dangle.
    for mut child in crate::media::MediaCollection::objects()
        .where_(crate::media::MediaCollection::parent_id.eq(Some(id)))
        .fetch(tenant.pool())
        .await?
    {
        child.parent_id = parent;
        child.save_pool(tenant.pool()).await?;
    }
    // Null out media references — media doesn't get deleted, just
    // moves to "uncategorized".
    for mut m in Media::objects()
        .where_(Media::collection_id.eq(Some(id)))
        .fetch(tenant.pool())
        .await?
    {
        m.collection_id = None;
        m.save_pool(tenant.pool()).await?;
    }
    row.delete_pool(tenant.pool()).await?;
    redirect_named_with_message_args(
        "rcms-admin:media:collections",
        MsgLevel::Success,
        "Collection “{name}” deleted. Children re-parented; media moved to Uncategorized.",
        &[("name", name.as_str())],
        &headers,
    )
}

// ============================================================
// #557 — Categories / Taxonomies admin
//
// One vocabulary registry (`cms_taxonomy`) + a per-vocabulary tree of
// `cms_category` rows. The category editor renders as a `Vec<Widget>`
// through the shared `_widget.html` macro — the same engine the page
// editor uses: base fields (name/slug/description + featured-image /
// thumbnail MediaPickers + parent Select) plus any extension widgets a
// custom vocabulary's handler contributes.
// ============================================================

use crate::category::model::{self as category_model, Category, CategoryError};

/// Flash-redirect back to a vocabulary's category tree.
fn category_tree_redirect(
    slug: &str,
    headers: &HeaderMap,
    level: MsgLevel,
    key: &str,
    args: &[(&str, &str)],
) -> Result<Response, AdminError> {
    let body = super::i18n::tr(headers, key, args);
    let url = format!("/cms-admin/taxonomies/{slug}");
    Ok(rustango::messages::redirect_with_message(
        messages_secret(),
        headers,
        level,
        &body,
        &url,
    ))
}

fn opt_i64_from_form(form: &std::collections::HashMap<String, String>, key: &str) -> Option<i64> {
    form.get(key)
        .map(|s| s.trim())
        .filter(|s| !s.is_empty())
        .and_then(|s| s.parse::<i64>().ok())
}

/// The category editor's base fields as widgets (rendered via
/// `_widget.html`, same as page extension fields). `parent_opts` are the
/// selectable parents (excludes self on edit).
fn category_base_widgets(
    cat: Option<&Category>,
    parent_opts: Vec<(String, String)>,
) -> Vec<crate::widget::Widget> {
    use crate::widget::{Widget, WidgetKind};
    let mut opts = vec![(String::new(), "— none (top level) —".to_owned())];
    opts.extend(parent_opts);
    vec![
        Widget::new(WidgetKind::Text, "name", "Name")
            .required()
            .with_value(cat.map(|c| c.name.clone()).unwrap_or_default()),
        Widget::new(WidgetKind::Text, "slug", "Slug")
            .with_help("URL identifier — auto-generated from the name if left blank.")
            .with_value(cat.map(|c| c.slug.clone()).unwrap_or_default()),
        Widget::new(WidgetKind::Textarea, "description", "Description")
            .with_value(cat.map(|c| c.description.clone()).unwrap_or_default()),
        Widget::new(
            WidgetKind::MediaPicker,
            "featured_image_id",
            "Featured image",
        )
        .with_value(
            cat.and_then(|c| c.featured_image_id)
                .map(|i| i.to_string())
                .unwrap_or_default(),
        ),
        Widget::new(WidgetKind::MediaPicker, "thumbnail_id", "Thumbnail").with_value(
            cat.and_then(|c| c.thumbnail_id)
                .map(|i| i.to_string())
                .unwrap_or_default(),
        ),
        Widget::new(WidgetKind::Select, "parent_id", "Parent")
            .with_options(opts)
            .with_help("Nest this category under another (siblings share a parent).")
            .with_value(
                cat.and_then(|c| c.parent_id)
                    .map(|i| i.to_string())
                    .unwrap_or_default(),
            ),
    ]
}

/// Build indented `(id, "— — Name")` parent options for a vocabulary,
/// optionally excluding `exclude` (and, on edit, its descendants can't be
/// picked — the move guard rejects that at save, so we only drop self here).
async fn category_parent_options(
    pool: &rustango::sql::Pool,
    taxonomy_id: i64,
    exclude: Option<i64>,
) -> Result<Vec<(String, String)>, AdminError> {
    let cats = category_model::all_in_taxonomy(pool, taxonomy_id)
        .await
        .map_err(|e| AdminError::Validation(e.to_string()))?;
    Ok(cats
        .iter()
        .filter(|c| c.id.get().copied() != exclude)
        .map(|c| {
            let indent = "— ".repeat((c.depth.max(1) - 1) as usize);
            (
                c.id.get().copied().unwrap_or_default().to_string(),
                format!("{indent}{}", c.name),
            )
        })
        .collect())
}

/// GET /cms-admin/taxonomies — the vocabulary index.
pub async fn taxonomies_index(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
) -> Result<Response, AdminError> {
    let taxes = crate::category::taxonomies(tenant.pool())
        .await
        .map_err(|e| AdminError::Validation(e.to_string()))?;
    let all_cats: Vec<Category> = Category::objects().fetch(tenant.pool()).await?;
    let mut counts: std::collections::HashMap<i64, i64> = std::collections::HashMap::new();
    for c in &all_cats {
        *counts.entry(c.taxonomy_id).or_insert(0) += 1;
    }
    let rows: Vec<serde_json::Value> = taxes
        .iter()
        .map(|t| {
            serde_json::json!({
                "slug": t.slug,
                "verbose_name": t.verbose_name,
                "icon": if t.icon.is_empty() { "sell".to_owned() } else { t.icon.clone() },
                "description": t.description,
                "hierarchical": t.hierarchical,
                "count": counts.get(&t.id.get().copied().unwrap_or_default()).copied().unwrap_or(0),
            })
        })
        .collect();
    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "taxonomies", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("taxonomies", &rows);
    render_with_csrf(
        &state,
        &headers,
        "rcms_admin/taxonomies_list.html",
        &mut ctx,
    )
}

/// GET /cms-admin/taxonomies/{slug} — one vocabulary's category tree.
pub async fn category_tree(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(slug): Path<String>,
) -> Result<Response, AdminError> {
    let tax = crate::category::find_taxonomy(tenant.pool(), &slug)
        .await
        .map_err(|e| AdminError::Validation(e.to_string()))?
        .ok_or_else(|| AdminError::Validation(format!("unknown taxonomy `{slug}`")))?;
    let cats =
        category_model::all_in_taxonomy(tenant.pool(), tax.id.get().copied().unwrap_or_default())
            .await
            .map_err(|e| AdminError::Validation(e.to_string()))?;
    let rows: Vec<serde_json::Value> = cats
        .iter()
        .map(|c| {
            serde_json::json!({
                "id": c.id.get().copied(),
                "name": c.name,
                "slug": c.slug,
                "description": c.description,
                "depth": c.depth,
                "thumbnail_id": c.thumbnail_id,
                "featured_image_id": c.featured_image_id,
            })
        })
        .collect();
    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "taxonomies", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("taxonomy_slug", &tax.slug);
    ctx.insert("taxonomy_name", &tax.verbose_name);
    ctx.insert("categories", &rows);
    render_with_csrf(&state, &headers, "rcms_admin/category_tree.html", &mut ctx)
}

/// GET /cms-admin/taxonomies/{slug}/categories/new — create form.
pub async fn category_new_form(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(slug): Path<String>,
) -> Result<Response, AdminError> {
    let tax = crate::category::find_taxonomy(tenant.pool(), &slug)
        .await
        .map_err(|e| AdminError::Validation(e.to_string()))?
        .ok_or_else(|| AdminError::Validation(format!("unknown taxonomy `{slug}`")))?;
    let tid = tax.id.get().copied().unwrap_or_default();
    let mut fields = if tax.hierarchical {
        category_base_widgets(
            None,
            category_parent_options(tenant.pool(), tid, None).await?,
        )
    } else {
        // Flat vocabulary — drop the parent picker.
        let mut w = category_base_widgets(None, Vec::new());
        w.pop();
        w
    };
    // Extension fields are edited after create (they key on the new id).
    let _ = &mut fields;
    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "taxonomies", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("taxonomy_slug", &tax.slug);
    ctx.insert("taxonomy_name", &tax.verbose_name);
    ctx.insert("mode", "new");
    ctx.insert("fields", &fields);
    render_with_csrf(&state, &headers, "rcms_admin/category_form.html", &mut ctx)
}

/// POST /cms-admin/taxonomies/{slug}/categories/new — create.
pub async fn category_create(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Path(slug): Path<String>,
    Form(form): Form<std::collections::HashMap<String, String>>,
) -> Result<Response, AdminError> {
    let tax = crate::category::find_taxonomy(tenant.pool(), &slug)
        .await
        .map_err(|e| AdminError::Validation(e.to_string()))?
        .ok_or_else(|| AdminError::Validation(format!("unknown taxonomy `{slug}`")))?;
    let tid = tax.id.get().copied().unwrap_or_default();
    let new = category_model::NewCategory {
        name: form
            .get("name")
            .cloned()
            .unwrap_or_default()
            .trim()
            .to_owned(),
        slug: form.get("slug").cloned().unwrap_or_default(),
        description: form.get("description").cloned().unwrap_or_default(),
        featured_image_id: opt_i64_from_form(&form, "featured_image_id"),
        thumbnail_id: opt_i64_from_form(&form, "thumbnail_id"),
        parent_id: opt_i64_from_form(&form, "parent_id"),
    };
    if new.name.is_empty() {
        return category_tree_redirect(
            &slug,
            &headers,
            MsgLevel::Error,
            "Category name is required.",
            &[],
        );
    }
    match category_model::create(tenant.pool(), tid, new).await {
        Ok(cat) => {
            let id = cat.id.get().copied().unwrap_or_default();
            let body = super::i18n::tr(
                &headers,
                "Category “{name}” created.",
                &[("name", cat.name.as_str())],
            );
            let url = format!("/cms-admin/taxonomies/{slug}/categories/{id}/edit");
            Ok(rustango::messages::redirect_with_message(
                messages_secret(),
                &headers,
                MsgLevel::Success,
                &body,
                &url,
            ))
        }
        Err(e) => category_tree_redirect(&slug, &headers, MsgLevel::Error, &e.to_string(), &[]),
    }
}

/// GET /cms-admin/taxonomies/{slug}/categories/{id}/edit — edit form.
pub async fn category_edit_form(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path((slug, id)): Path<(String, i64)>,
    Query(q): Query<EditQuery>,
) -> Result<Response, AdminError> {
    let tax = crate::category::find_taxonomy(tenant.pool(), &slug)
        .await
        .map_err(|e| AdminError::Validation(e.to_string()))?
        .ok_or_else(|| AdminError::Validation(format!("unknown taxonomy `{slug}`")))?;
    let tid = tax.id.get().copied().unwrap_or_default();
    let cat = Category::objects()
        .where_(Category::id.eq(id))
        .where_(Category::taxonomy_id.eq(tid))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    let mut fields = if tax.hierarchical {
        category_base_widgets(
            Some(&cat),
            category_parent_options(tenant.pool(), tid, Some(id)).await?,
        )
    } else {
        let mut w = category_base_widgets(Some(&cat), Vec::new());
        w.pop();
        w
    };
    // Extension widgets from the vocabulary's handler (custom typed fields).
    if let Some(handler) = crate::category::find_handler(&slug) {
        if let Ok(ext) = handler.ext_widgets(tenant.pool(), id).await {
            fields.extend(ext);
        }
    }
    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "taxonomies", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("taxonomy_slug", &tax.slug);
    ctx.insert("taxonomy_name", &tax.verbose_name);
    ctx.insert("mode", "edit");
    ctx.insert("category_id", &id);
    ctx.insert("fields", &fields);
    // #862 — `?locale=<code>` (a non-default active locale) shows the
    // name's translation beside the original, like pages and snippets.
    let locales: Vec<crate::locale::Locale> = crate::locale::Locale::objects()
        .where_(crate::locale::Locale::active.eq(true))
        .order_by(&[("sort_order", false), ("code", false)])
        .fetch(tenant.pool())
        .await?;
    let editing_locale = q
        .locale
        .as_deref()
        .filter(|c| !c.is_empty())
        .and_then(|code| locales.iter().find(|l| l.code == code && !l.is_default).cloned());
    if let Some(loc) = &editing_locale {
        let lid = loc.id.get().copied().unwrap_or_default();
        let mut existing = crate::category_translation::fetch_for_categories(tenant.pool(), &[id], lid)
            .await
            .unwrap_or_default();
        ctx.insert("translation_mode", &true);
        ctx.insert("editing_locale", loc);
        ctx.insert("category_name", &cat.name);
        ctx.insert("tr_name", &existing.remove(&id).and_then(|mut f| f.remove("name")).unwrap_or_default());
    }
    ctx.insert("locales", &locales);
    render_with_csrf(&state, &headers, "rcms_admin/category_form.html", &mut ctx)
}

/// POST /cms-admin/taxonomies/{slug}/categories/{id}/translate?locale=<code>
/// — save a category's per-locale name. An empty box deletes the
/// row, so the original shows again.
pub async fn category_translate_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Path((slug, id)): Path<(String, i64)>,
    Query(q): Query<EditQuery>,
    Form(form): Form<std::collections::HashMap<String, String>>,
) -> Result<Response, AdminError> {
    use crate::category_translation::{CategoryTranslation, TRANSLATABLE_FIELDS};
    let code = q
        .locale
        .as_deref()
        .filter(|c| !c.is_empty())
        .ok_or_else(|| AdminError::Validation("translate requires ?locale=".to_owned()))?;
    let locale = crate::locale::Locale::objects()
        .where_(crate::locale::Locale::code.eq(code.to_owned()))
        .where_(crate::locale::Locale::active.eq(true))
        .where_(crate::locale::Locale::is_default.eq(false))
        .first(tenant.pool())
        .await?
        .ok_or_else(|| AdminError::Validation(format!("unknown locale `{code}`")))?;
    let locale_id = locale.id.get().copied().unwrap_or_default();
    let tax = crate::category::find_taxonomy(tenant.pool(), &slug)
        .await
        .map_err(|e| AdminError::Validation(e.to_string()))?
        .ok_or_else(|| AdminError::Validation(format!("unknown taxonomy `{slug}`")))?;
    Category::objects()
        .where_(Category::id.eq(id))
        .where_(Category::taxonomy_id.eq(tax.id.get().copied().unwrap_or_default()))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    rustango::atomic!(tenant.pool(), |tx| {
        let mut guard = tx.lock().await?;
        let tx = &mut *guard;
        for field in TRANSLATABLE_FIELDS {
            let Some(value) = form.get(&format!("tr__{field}")) else {
                continue;
            };
            let value = value.trim().to_owned();
            let existing: Vec<CategoryTranslation> = CategoryTranslation::objects()
                .where_(CategoryTranslation::category_id.eq(id))
                .where_(CategoryTranslation::locale_id.eq(locale_id))
                .where_(CategoryTranslation::field_path.eq(field.to_owned()))
                .fetch_tx(tx)
                .await?;
            if value.is_empty() {
                for row in existing {
                    row.delete_tx(tx).await?;
                }
            } else if let Some(mut row) = existing.into_iter().next() {
                row.value = value;
                row.save_tx(tx).await?;
            } else {
                let mut row = CategoryTranslation {
                    id: rustango::sql::Auto::Unset,
                    category_id: id,
                    locale_id,
                    field_path: field.to_owned(),
                    value,
                    updated_at: rustango::sql::Auto::Unset,
                };
                row.save_tx(tx).await?;
            }
        }
        Ok::<_, rustango::sql::ExecError>(())
    })
    .await?;
    Ok(Redirect::to(&format!("/cms-admin/taxonomies/{slug}/categories/{id}/edit?locale={code}")).into_response())
}

/// POST /cms-admin/taxonomies/{slug}/categories/{id}/edit — update.
pub async fn category_update(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Path((slug, id)): Path<(String, i64)>,
    Form(form): Form<std::collections::HashMap<String, String>>,
) -> Result<Response, AdminError> {
    let tax = crate::category::find_taxonomy(tenant.pool(), &slug)
        .await
        .map_err(|e| AdminError::Validation(e.to_string()))?
        .ok_or_else(|| AdminError::Validation(format!("unknown taxonomy `{slug}`")))?;
    let tid = tax.id.get().copied().unwrap_or_default();
    let new = category_model::NewCategory {
        name: form
            .get("name")
            .cloned()
            .unwrap_or_default()
            .trim()
            .to_owned(),
        slug: form.get("slug").cloned().unwrap_or_default(),
        description: form.get("description").cloned().unwrap_or_default(),
        featured_image_id: opt_i64_from_form(&form, "featured_image_id"),
        thumbnail_id: opt_i64_from_form(&form, "thumbnail_id"),
        parent_id: opt_i64_from_form(&form, "parent_id"),
    };
    if new.name.is_empty() {
        return category_tree_redirect(
            &slug,
            &headers,
            MsgLevel::Error,
            "Category name is required.",
            &[],
        );
    }
    let new_parent = new.parent_id;
    if let Err(e) = category_model::update(tenant.pool(), tid, id, new).await {
        return category_tree_redirect(&slug, &headers, MsgLevel::Error, &e.to_string(), &[]);
    }
    // Re-parent if the parent changed (cycle-guarded inside move_to;
    // a no-op when the parent is unchanged).
    if let Err(e) = category_model::move_to(tenant.pool(), tid, id, new_parent).await {
        return category_tree_redirect(&slug, &headers, MsgLevel::Error, &e.to_string(), &[]);
    }
    // Extension row (custom vocabularies).
    if let Some(handler) = crate::category::find_handler(&slug) {
        if let Err(e) = handler.save_ext(tenant.pool(), id, &form).await {
            tracing::warn!(category_id = id, error = %e, "category extension save failed");
        }
    }
    category_tree_redirect(&slug, &headers, MsgLevel::Success, "Category saved.", &[])
}

/// POST /cms-admin/taxonomies/{slug}/categories/{id}/delete.
pub async fn category_delete(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Path((slug, id)): Path<(String, i64)>,
) -> Result<Response, AdminError> {
    let tax = crate::category::find_taxonomy(tenant.pool(), &slug)
        .await
        .map_err(|e| AdminError::Validation(e.to_string()))?
        .ok_or_else(|| AdminError::Validation(format!("unknown taxonomy `{slug}`")))?;
    let tid = tax.id.get().copied().unwrap_or_default();
    match category_model::delete(tenant.pool(), tid, id, false).await {
        Ok(_) => {
            category_tree_redirect(&slug, &headers, MsgLevel::Success, "Category deleted.", &[])
        }
        Err(CategoryError::HasChildren(_, n)) => {
            let n = n.to_string();
            category_tree_redirect(
                &slug,
                &headers,
                MsgLevel::Error,
                "Can't delete a category with {count} child categor(ies) — delete or move them first.",
                &[("count", n.as_str())],
            )
        }
        Err(e) => category_tree_redirect(&slug, &headers, MsgLevel::Error, &e.to_string(), &[]),
    }
}

// ============================================================
// #559/#562 — Page-type field builder (visual schema constructor)
//
// A Developer authors a page type's body schema in the builder; save
// writes a DRAFT row, publish flips it live (versioned). Gated on the
// `cms_page_type.build` codename at the handler level (superuser bypass).
// ============================================================

/// Deny access to the builder unless the user is a superuser or holds
/// `cms_page_type.build`. Returns `Some(redirect)` when denied (anon →
/// login, otherwise → no-access), `None` when allowed.
async fn ensure_builder_access(
    tenant: &Tenant,
    user: Option<&rustango::tenancy::auth::User>,
) -> Option<Response> {
    super::codename_gate(tenant, user, "cms_page_type.build").await
}

/// GET /cms-admin/page-types/{id}/build — the visual builder.
pub async fn page_type_build_form(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
) -> Result<Response, AdminError> {
    if let Some(r) = ensure_builder_access(&tenant, session_user.as_ref()).await {
        return Ok(r);
    }
    let pt = crate::page_type_model::PageType::objects()
        .where_(crate::page_type_model::PageType::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;

    let draft = crate::page_builder::model::draft_for(tenant.pool(), id).await?;
    let published = crate::page_builder::model::published_for(tenant.pool(), id).await?;
    // Seed the canvas from the draft, else the live published schema,
    // else an empty document.
    let doc_value = draft
        .as_ref()
        .map(|d| d.document.clone())
        .or_else(|| published.as_ref().map(|p| p.document.clone()))
        .unwrap_or_else(|| serde_json::json!({ "nodes": [] }));
    let schema_json =
        serde_json::to_string(&doc_value).unwrap_or_else(|_| "{\"nodes\":[]}".to_owned());

    let components: Vec<serde_json::Value> =
        crate::page_builder::model::all_components(tenant.pool())
            .await?
            .iter()
            .map(|c| serde_json::json!({ "slug": c.slug, "label": c.label }))
            .collect();
    let components_json = serde_json::to_string(&components).unwrap_or_else(|_| "[]".to_owned());
    let versions: Vec<serde_json::Value> =
        crate::page_builder::model::list_versions(tenant.pool(), id)
            .await?
            .iter()
            .map(|v| {
                serde_json::json!({
                    "version": v.version,
                    "status": v.status,
                    "updated_at": v.updated_at.get().copied(),
                })
            })
            .collect();

    // #566 — deleting a UI-created type is a danger-zone action on this
    // page (not an inline row action). Only for `cms_ui` types, and only
    // while no pages use the type.
    let is_ui_type = pt.app_label == "cms_ui";
    let page_count = if is_ui_type {
        Page::objects()
            .where_(Page::page_type_id.eq(id))
            .fetch(tenant.pool())
            .await
            .map(|v| v.len())
            .unwrap_or(0)
    } else {
        0
    };

    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "page-types", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("page_type_id", &id);
    ctx.insert("title", &pt.verbose_name);
    ctx.insert("schema_json", &schema_json);
    ctx.insert("build_url", &format!("/cms-admin/page-types/{id}/build"));
    ctx.insert(
        "publish_url",
        &format!("/cms-admin/page-types/{id}/build/publish"),
    );
    ctx.insert("delete_url", &format!("/cms-admin/page-types/{id}/delete"));
    ctx.insert("list_url", "/cms-admin/page-types");
    ctx.insert("components_json", &components_json);
    ctx.insert("versions", &versions);
    ctx.insert("has_unpublished", &draft.is_some());
    // #863 — the Translate menu: other languages, once the schema is live.
    let translate_locales: Vec<crate::locale::Locale> = if published.is_some() {
        crate::locale::Locale::objects()
            .where_(crate::locale::Locale::active.eq(true))
            .where_(crate::locale::Locale::is_default.eq(false))
            .order_by(&[("sort_order", false), ("code", false)])
            .fetch(tenant.pool())
            .await?
    } else {
        Vec::new()
    };
    ctx.insert("translate_locales", &translate_locales);
    ctx.insert("translate_url", &format!("/cms-admin/page-types/{id}/translate"));
    ctx.insert("is_ui_type", &is_ui_type);
    ctx.insert("page_count", &page_count);
    ctx.insert("deletable", &(is_ui_type && page_count == 0));
    render_with_csrf(
        &state,
        &headers,
        "rcms_admin/page_type_builder.html",
        &mut ctx,
    )
}

/// GET /cms-admin/page-types/{id}/translate?locale=<code> — the labels of
/// the type's choice options in another language, side by side
/// with the originals like every other translation screen.
pub async fn page_type_translate_form(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
    Query(q): Query<EditQuery>,
) -> Result<Response, AdminError> {
    if let Some(r) = ensure_builder_access(&tenant, session_user.as_ref()).await {
        return Ok(r);
    }
    let pt = crate::page_type_model::PageType::objects()
        .where_(crate::page_type_model::PageType::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    let locale = translate_locale(&tenant, q.locale.as_deref()).await?;
    let published = crate::page_builder::model::published_for(tenant.pool(), id).await?;
    let fields = published
        .as_ref()
        .map(|p| crate::page_builder::choice_i18n::choice_fields(&p.document, &locale.code))
        .unwrap_or_default();
    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "page-types", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("title", &pt.verbose_name);
    ctx.insert("editing_locale", &locale);
    ctx.insert("choice_fields", &fields);
    ctx.insert("build_url", &format!("/cms-admin/page-types/{id}/build"));
    ctx.insert("translate_url", &format!("/cms-admin/page-types/{id}/translate"));
    render_with_csrf(&state, &headers, "rcms_admin/page_type_translate.html", &mut ctx)
}

/// POST /cms-admin/page-types/{id}/translate?locale=<code> — store the
/// option labels (`tr__<field path>__<option value>`) on the published
/// schema and on the draft, if one is open, so publishing it keeps them.
pub async fn page_type_translate_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
    Query(q): Query<EditQuery>,
    Form(form): Form<Vec<(String, String)>>,
) -> Result<Response, AdminError> {
    if let Some(r) = ensure_builder_access(&tenant, session_user.as_ref()).await {
        return Ok(r);
    }
    let locale = translate_locale(&tenant, q.locale.as_deref()).await?;
    let labels: std::collections::HashMap<(String, String), String> = form
        .into_iter()
        .filter_map(|(k, v)| {
            let rest = k.strip_prefix("tr__")?;
            let (path, value) = rest.split_once("__")?;
            Some(((path.to_owned(), value.to_owned()), v))
        })
        .collect();
    for row in [
        crate::page_builder::model::published_for(tenant.pool(), id).await?,
        crate::page_builder::model::draft_for(tenant.pool(), id).await?,
    ]
    .into_iter()
    .flatten()
    {
        let mut row = row;
        if crate::page_builder::choice_i18n::set_choice_labels(&mut row.document, &locale.code, &labels) > 0 {
            row.save_pool(tenant.pool()).await?;
        }
    }
    let back = format!("/cms-admin/page-types/{id}/translate?locale={}", locale.code);
    Ok(rustango::messages::redirect_with_message(
        messages_secret(),
        &headers,
        MsgLevel::Success,
        &super::i18n::tr(&headers, "Translations saved.", &[]),
        &back,
    ))
}

/// The active, non-default locale named by `?locale=`, for the
/// translation screens.
async fn translate_locale(tenant: &Tenant, code: Option<&str>) -> Result<crate::locale::Locale, AdminError> {
    let code = code
        .filter(|c| !c.is_empty())
        .ok_or_else(|| AdminError::Validation("translate requires ?locale=".to_owned()))?;
    crate::locale::Locale::objects()
        .where_(crate::locale::Locale::code.eq(code.to_owned()))
        .where_(crate::locale::Locale::active.eq(true))
        .where_(crate::locale::Locale::is_default.eq(false))
        .first(tenant.pool())
        .await?
        .ok_or_else(|| AdminError::Validation(format!("unknown locale `{code}`")))
}

/// Shared body for save/publish: gate, load the page type, parse +
/// validate the posted schema. Returns the validated document + the
/// problem list, or an early access/redirect / not-found response.
async fn parse_builder_submit(
    tenant: &Tenant,
    user: Option<&rustango::tenancy::auth::User>,
    id: i64,
    schema_str: &str,
) -> Result<Result<(crate::page_builder::Document, Vec<String>), Response>, AdminError> {
    if let Some(r) = ensure_builder_access(tenant, user).await {
        return Ok(Err(r));
    }
    // Ensure the page type exists.
    let exists = crate::page_type_model::PageType::objects()
        .where_(crate::page_type_model::PageType::id.eq(id))
        .first(tenant.pool())
        .await?
        .is_some();
    if !exists {
        return Err(AdminError::NotFound(id));
    }
    let value: serde_json::Value = serde_json::from_str(schema_str)
        .map_err(|e| AdminError::Validation(format!("invalid schema JSON: {e}")))?;
    let doc = crate::page_builder::parse_schema(&value)
        .map_err(|e| AdminError::Validation(format!("invalid schema document: {e}")))?;
    let components = crate::page_builder::model::component_map(tenant.pool()).await?;
    let problems = crate::page_builder::validate_schema(&doc, &components);
    Ok(Ok((doc, problems)))
}

/// POST /cms-admin/page-types/{id}/build — save the schema as a draft.
/// Warnings are non-blocking (surfaced in the flash).
pub async fn page_type_build_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
    Form(form): Form<FormBuildSubmit>,
) -> Result<Response, AdminError> {
    let (doc, problems) =
        match parse_builder_submit(&tenant, session_user.as_ref(), id, &form.schema).await? {
            Ok(v) => v,
            Err(resp) => return Ok(resp),
        };
    let document =
        serde_json::to_value(&doc).unwrap_or_else(|_| serde_json::json!({ "nodes": [] }));
    let created_by = session_user.as_ref().and_then(|u| u.id.get().copied());
    crate::page_builder::model::save_draft(tenant.pool(), id, document, created_by).await?;
    let (level, msg) = if problems.is_empty() {
        (MsgLevel::Success, "Draft saved.".to_owned())
    } else {
        (
            MsgLevel::Warning,
            format!("Draft saved with warnings: {}", problems.join(" ")),
        )
    };
    redirect_named_with_params_and_message(
        "rcms-admin:page-types:build",
        &[("id", id.to_string())],
        level,
        &msg,
        &headers,
    )
}

/// POST /cms-admin/page-types/{id}/build/publish — validate + publish the
/// schema. Validation errors block the publish (draft is still saved).
pub async fn page_type_publish_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
    Form(form): Form<FormBuildSubmit>,
) -> Result<Response, AdminError> {
    let (doc, problems) =
        match parse_builder_submit(&tenant, session_user.as_ref(), id, &form.schema).await? {
            Ok(v) => v,
            Err(resp) => return Ok(resp),
        };
    // Persist what's on screen as the draft first, so a blocked publish
    // still saves the editor's work.
    let document =
        serde_json::to_value(&doc).unwrap_or_else(|_| serde_json::json!({ "nodes": [] }));
    let created_by = session_user.as_ref().and_then(|u| u.id.get().copied());
    crate::page_builder::model::save_draft(tenant.pool(), id, document, created_by).await?;
    if !problems.is_empty() {
        return redirect_named_with_params_and_message(
            "rcms-admin:page-types:build",
            &[("id", id.to_string())],
            MsgLevel::Error,
            &format!("Can't publish — fix these first: {}", problems.join(" ")),
            &headers,
        );
    }
    crate::page_builder::model::publish(tenant.pool(), id).await?;
    redirect_named_with_params_and_message(
        "rcms-admin:page-types:build",
        &[("id", id.to_string())],
        MsgLevel::Success,
        "Schema published — the page editor now uses this structure.",
        &headers,
    )
}

/// Persist a page's page-builder body values from a posted form, when
/// its page type has a published schema. Additive to any code
/// handler's `save_extension` — runs regardless of whether a handler
/// exists.
///
/// # Errors
/// A failed schema/component lookup or value write. A type with no
/// published schema, or one that no longer parses, has nothing to save.
pub(crate) async fn save_page_builder_values(
    pool: &rustango::sql::Pool,
    page_type_id: i64,
    page_id: i64,
    form_map: &std::collections::HashMap<String, String>,
) -> Result<(), rustango::sql::ExecError> {
    let Some(row) = crate::page_builder::model::published_for(pool, page_type_id).await? else {
        return Ok(());
    };
    let Ok(doc) = crate::page_builder::parse_schema(&row.document) else {
        return Ok(());
    };
    let components = crate::page_builder::model::component_map(pool).await?;
    let compiled = crate::page_builder::compile(&doc, &components, row.version.max(1) as u32);
    crate::page_builder::values::save_from_form(pool, page_id, &compiled, form_map).await
}

/// Save a page's type-specific content from a posted page form: the
/// handler's extension row and inline panels, then the page-builder
/// body values. Shared by the new-page and edit submits.
async fn save_type_content(
    pool: &rustango::sql::Pool,
    page_type: Option<&PageType>,
    page_type_id: i64,
    page_id: i64,
    form_map: &std::collections::HashMap<String, String>,
) -> Result<(), AdminError> {
    if let Some(handler) = page_type.and_then(|pt| find_handler(&pt.type_name)) {
        let ext_form = sanitized_extension_form(handler.as_ref(), pool, page_id, form_map).await;
        handler.save_extension(pool, page_id, &ext_form).await?;
        // #117 — inline panels. Reassemble row dicts from the
        // posted `inline__<panel>__<idx>__<field>` keys and hand
        // them to the handler. Handler is responsible for the
        // upsert / delete-removed semantics.
        for spec in handler.inline_panels() {
            let rows = extract_inline_panel_rows(form_map, &spec);
            handler.save_inline_panel(pool, page_id, &spec.name, &rows).await?;
        }
    }
    // #564 — page-builder body values (additive to any code handler).
    save_page_builder_values(pool, page_type_id, page_id, form_map).await?;
    Ok(())
}

/// #146 — reindex the reference graph from the just-saved form
/// values. The (cheap, in-memory) scan stays here where the form is
/// in hand; #432 moves the (DB-write) reindex onto the task queue
/// via `reindex_page` — dispatched + retried when a sink is
/// installed, inline otherwise. Best-effort either way.
async fn reindex_from_form(
    pool: &rustango::sql::Pool,
    org_slug: &str,
    page_id: i64,
    form_map: &std::collections::HashMap<String, String>,
) {
    let mut refs: Vec<crate::reference_index::Reference> = Vec::new();
    for (key, value) in form_map {
        // Scan every form field for /__media__/... + page/snippet
        // links. Field-scoped so the index records which input
        // produced each reference.
        let mut found = crate::reference_index::scan(value, key);
        refs.append(&mut found);
    }
    crate::task_queue::reindex_page(pool, org_slug, page_id, refs).await;
}

/// The page type's own editor fields — the handler's widgets (custom and
/// stream widgets pre-rendered), the page-builder body and the inline
/// panels — into `ctx`. `page_id` 0 is a page not created yet: every
/// field shows its default. `held` is a change held for review, shown in
/// place of the stored values.
#[allow(clippy::too_many_arguments)]
async fn insert_type_fields(
    ctx: &mut Context,
    tera: &tera::Tera,
    pool: &rustango::sql::Pool,
    handler: Option<&dyn crate::page_type::PageTypeHandler>,
    mut widgets: Vec<crate::widget::Widget>,
    page_id: i64,
    page_type_id: i64,
    held: Option<&std::collections::HashMap<String, String>>,
) {
    if let Some(form) = held {
        overlay_pending_widgets(&mut widgets, form);
    }
    // Pre-render `Custom`-kind widgets via the inventory registry.
    // Tera's `{% include %}` requires a string literal, so we
    // render once on the Rust side and stash the result on each
    // widget — same shape `cms_snippet` uses for its
    // pre-fetched-snippet HTML map.
    for w in &mut widgets {
        if matches!(w.kind, crate::widget::WidgetKind::Custom) {
            if let Some(reg) = crate::widget::registry::find_custom_widget(&w.custom_name) {
                let mut sub = Context::new();
                sub.insert("w", &w);
                w.custom_html = tera.render(reg.template, &sub).unwrap_or_else(|e| {
                    tracing::warn!(
                        target: "rustango_cms::widget",
                        custom_name = %w.custom_name,
                        template = reg.template,
                        error = %e,
                        "custom widget template render failed",
                    );
                    String::new()
                });
            }
        }
    }

    // -- StreamField pre-render --------------------------------------
    // Walk each `Widget::Stream` once: parse its stored JSON, render
    // every contained block's editor markup + `<template>` clones
    // for each allowed type. The output lives on `w.custom_html`
    // (same stash slot the Custom widget arm uses); the `_widget.html`
    // macro emits it `| safe` for the stream arm.
    for w in &mut widgets {
        if matches!(w.kind, crate::widget::WidgetKind::Stream) {
            w.custom_html = crate::block::admin::render_stream_editor(w, tera);
        }
    }
    ctx.insert("extension_fields", &widgets);

    // #564 — page-builder body. When the page type has a published
    // schema, pre-render its fixed fields/rows/groups + repeater/flex
    // stream editors (prefilled + lazily upgraded) into one HTML blob the
    // Body fieldset emits, plus a conditional-rules island.
    if let Some((body_html, rules_json, upgraded)) =
        page_builder_editor_ctx(pool, tera, page_id, page_type_id, held).await
    {
        ctx.insert("builder_body_html", &body_html);
        ctx.insert("builder_rules_json", &rules_json);
        ctx.insert("builder_upgraded", &upgraded);
    }

    // #117 — inline panels. Collect each spec the handler declared
    // and the persisted rows behind it. Each panel renders as a
    // section with N rows; per-row fields come from the spec.
    let mut inline_panels: Vec<serde_json::Value> = Vec::new();
    if let Some(h) = handler {
        for spec in h.inline_panels() {
            let rows = h
                .load_inline_panel(pool, page_id, &spec.name)
                .await
                .unwrap_or_default();
            inline_panels.push(serde_json::json!({
                "spec": spec,
                "rows": rows,
            }));
        }
    }
    ctx.insert("inline_panels", &inline_panels);
}

/// GET-side: build the editor HTML + rules island + upgrade flag for a
/// page whose type has a published schema. Returns `None` when
/// the type has no published schema.
async fn page_builder_editor_ctx(
    pool: &rustango::sql::Pool,
    tera: &tera::Tera,
    page_id: i64,
    page_type_id: i64,
    held: Option<&std::collections::HashMap<String, String>>,
) -> Option<(String, String, bool)> {
    let row = crate::page_builder::model::published_for(pool, page_type_id)
        .await
        .ok()??;
    let doc = crate::page_builder::parse_schema(&row.document).ok()?;
    let components = crate::page_builder::model::component_map(pool)
        .await
        .unwrap_or_default();
    let compiled = crate::page_builder::compile(&doc, &components, row.version.max(1) as u32);
    let (mut values, report) =
        crate::page_builder::values::load_upgraded(pool, page_id, &compiled)
            .await
            .unwrap_or_else(|_| (serde_json::json!({}), Default::default()));
    // A change held for review shows in place of the stored values.
    if let Some(form) = held {
        values = serde_json::Value::Object(crate::page_builder::values::build_values(&compiled, form));
    }
    let html =
        crate::page_builder::values::render_body(&compiled, &values, tera);
    // Conditional-rules island: `[{field, rule}]` for page_builder_rules.js.
    let rules: Vec<serde_json::Value> = compiled
        .rules
        .iter()
        .map(|r| serde_json::json!({ "field": r.field, "rule": r.rule }))
        .collect();
    let rules_json = serde_json::to_string(&rules).unwrap_or_else(|_| "[]".to_owned());
    Some((html, rules_json, report.upgraded))
}

// ---- #563 reusable components library -----------------------------------

/// GET /cms-admin/components — the library list (Developer-gated).
pub async fn components_index(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
) -> Result<Response, AdminError> {
    if let Some(r) = ensure_builder_access(&tenant, session_user.as_ref()).await {
        return Ok(r);
    }
    let comps = crate::page_builder::model::all_components(tenant.pool()).await?;
    let mut rows = Vec::with_capacity(comps.len());
    for c in &comps {
        let refs = crate::page_builder::model::component_references(tenant.pool(), &c.slug)
            .await
            .unwrap_or_default();
        rows.push(serde_json::json!({
            "id": c.id.get().copied(),
            "slug": c.slug,
            "label": c.label,
            "icon": if c.icon.is_empty() { "extension".to_owned() } else { c.icon.clone() },
            "description": c.description,
            "version": c.version,
            "ref_count": refs.len(),
        }));
    }
    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "components", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("components", &rows);
    ctx.insert("new_url", "/cms-admin/components/new");
    render_with_csrf(
        &state,
        &headers,
        "rcms_admin/components_list.html",
        &mut ctx,
    )
}

/// Shared render of the component builder shell (new + edit + error re-render).
#[allow(clippy::too_many_arguments)]
async fn render_component_builder(
    state: &super::AdminState,
    headers: &HeaderMap,
    tenant: &Tenant,
    user: Option<&rustango::tenancy::auth::User>,
    id: Option<i64>,
    slug: &str,
    label: &str,
    icon: &str,
    description: &str,
    schema_json: &str,
    error: Option<&str>,
) -> Result<Response, AdminError> {
    let mut ctx = Context::new();
    add_chrome(&mut ctx, tenant, "components", user).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("component_id", &id);
    ctx.insert("slug", slug);
    ctx.insert("label", label);
    ctx.insert("icon", icon);
    ctx.insert("description", description);
    ctx.insert("schema_json", schema_json);
    ctx.insert("is_edit", &id.is_some());
    let save_url = match id {
        Some(cid) => format!("/cms-admin/components/{cid}/edit"),
        None => "/cms-admin/components/new".to_owned(),
    };
    ctx.insert("save_url", &save_url);
    ctx.insert("list_url", "/cms-admin/components");
    if let Some(e) = error {
        ctx.insert("error", e);
    }
    render_with_csrf(
        state,
        headers,
        "rcms_admin/component_builder.html",
        &mut ctx,
    )
}

/// GET /cms-admin/components/new — empty component builder (fields + rows).
pub async fn component_new_form(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
) -> Result<Response, AdminError> {
    if let Some(r) = ensure_builder_access(&tenant, session_user.as_ref()).await {
        return Ok(r);
    }
    render_component_builder(
        &state,
        &headers,
        &tenant,
        session_user.as_ref(),
        None,
        "",
        "",
        "",
        "",
        "{\"nodes\":[]}",
        None,
    )
    .await
}

/// GET /cms-admin/components/{id}/edit — builder seeded from the stored doc.
pub async fn component_edit_form(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
) -> Result<Response, AdminError> {
    if let Some(r) = ensure_builder_access(&tenant, session_user.as_ref()).await {
        return Ok(r);
    }
    let c = crate::page_builder::model::component_by_id(tenant.pool(), id)
        .await?
        .ok_or(AdminError::NotFound(id))?;
    // The builder JS reads `{nodes:[…]}`; a component doc stores `children`.
    let doc: crate::page_builder::schema::ComponentDoc =
        serde_json::from_value(c.document.clone()).unwrap_or_default();
    let schema_json =
        serde_json::to_string(&serde_json::json!({ "nodes": doc.children })).unwrap_or_default();
    render_component_builder(
        &state,
        &headers,
        &tenant,
        session_user.as_ref(),
        Some(id),
        &c.slug,
        &c.label,
        &c.icon,
        &c.description,
        &schema_json,
        None,
    )
    .await
}

/// Parse the posted `{nodes:[…]}` into a validated [`ComponentDoc`].
/// `Ok(doc)` on success; `Err(problems)` with the editor-facing errors.
fn parse_component_doc(
    schema_str: &str,
) -> Result<crate::page_builder::schema::ComponentDoc, String> {
    let value: serde_json::Value =
        serde_json::from_str(schema_str).map_err(|e| format!("invalid schema JSON: {e}"))?;
    let doc = crate::page_builder::parse_schema(&value)
        .map_err(|e| format!("invalid schema document: {e}"))?;
    let cdoc = crate::page_builder::schema::ComponentDoc {
        label_format: None,
        children: doc.nodes,
    };
    let problems = crate::page_builder::schema::validate_component(&cdoc);
    if problems.is_empty() {
        Ok(cdoc)
    } else {
        Err(problems.join(" "))
    }
}

/// POST /cms-admin/components/new — create a component (version 1).
pub async fn component_create(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Form(form): Form<FormComponentSubmit>,
) -> Result<Response, AdminError> {
    if let Some(r) = ensure_builder_access(&tenant, session_user.as_ref()).await {
        return Ok(r);
    }
    let slug = crate::forms::schema::slugify_key(&form.slug);
    // Validate slug → label → document, in order; first failure wins.
    let checked: Result<crate::page_builder::schema::ComponentDoc, String> =
        if !crate::forms::schema::is_valid_key(&slug) {
            Err(
                "Slug is invalid — use letters/digits/underscores, starting with a letter."
                    .to_owned(),
            )
        } else if form.label.trim().is_empty() {
            Err("Label is required.".to_owned())
        } else {
            parse_component_doc(&form.schema)
        };
    // On success, create; a unique-slug clash re-shows with a clear error.
    let msg: Option<String> = match checked {
        Ok(cdoc) => {
            let document = serde_json::to_value(&cdoc).unwrap_or_else(|_| serde_json::json!({}));
            match crate::page_builder::model::create_component(
                tenant.pool(),
                &slug,
                form.label.trim(),
                form.icon.trim(),
                form.description.trim(),
                document,
            )
            .await
            {
                Ok(_) => {
                    return redirect_named_with_message(
                        "rcms-admin:components:index",
                        MsgLevel::Success,
                        "Component created.",
                        &headers,
                    )
                }
                Err(_) => Some(format!(
                    "Could not create — a component with slug `{slug}` may already exist."
                )),
            }
        }
        Err(e) => Some(e),
    };
    render_component_builder(
        &state,
        &headers,
        &tenant,
        session_user.as_ref(),
        None,
        &form.slug,
        &form.label,
        &form.icon,
        &form.description,
        &form.schema,
        msg.as_deref(),
    )
    .await
}

/// POST /cms-admin/components/{id}/edit — update + bump version.
pub async fn component_update(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
    Form(form): Form<FormComponentSubmit>,
) -> Result<Response, AdminError> {
    if let Some(r) = ensure_builder_access(&tenant, session_user.as_ref()).await {
        return Ok(r);
    }
    let existing = crate::page_builder::model::component_by_id(tenant.pool(), id)
        .await?
        .ok_or(AdminError::NotFound(id))?;
    if form.label.trim().is_empty() {
        return render_component_builder(
            &state,
            &headers,
            &tenant,
            session_user.as_ref(),
            Some(id),
            &existing.slug,
            &form.label,
            &form.icon,
            &form.description,
            &form.schema,
            Some("Label is required."),
        )
        .await;
    }
    let cdoc = match parse_component_doc(&form.schema) {
        Ok(d) => d,
        Err(e) => {
            return render_component_builder(
                &state,
                &headers,
                &tenant,
                session_user.as_ref(),
                Some(id),
                &existing.slug,
                &form.label,
                &form.icon,
                &form.description,
                &form.schema,
                Some(&e),
            )
            .await
        }
    };
    let document = serde_json::to_value(&cdoc).unwrap_or_else(|_| serde_json::json!({}));
    crate::page_builder::model::update_component(
        tenant.pool(),
        id,
        form.label.trim(),
        form.icon.trim(),
        form.description.trim(),
        document,
    )
    .await?;
    redirect_named_with_message(
        "rcms-admin:components:index",
        MsgLevel::Success,
        "Component saved — referencing page types pick it up on next open.",
        &headers,
    )
}

/// POST /cms-admin/components/{id}/delete — blocked while referenced.
pub async fn component_delete(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
) -> Result<Response, AdminError> {
    if let Some(r) = ensure_builder_access(&tenant, session_user.as_ref()).await {
        return Ok(r);
    }
    let c = crate::page_builder::model::component_by_id(tenant.pool(), id)
        .await?
        .ok_or(AdminError::NotFound(id))?;
    let refs = crate::page_builder::model::component_references(tenant.pool(), &c.slug).await?;
    if !refs.is_empty() {
        // Resolve referencing page-type names for a clear error.
        let ids: Vec<i64> = refs.iter().map(|(pt, _)| *pt).collect();
        let pts: Vec<PageType> = PageType::objects()
            .where_(PageType::id.is_in(ids))
            .fetch(tenant.pool())
            .await
            .unwrap_or_default();
        let names: Vec<String> = pts.iter().map(|p| p.verbose_name.clone()).collect();
        let listed = if names.is_empty() {
            format!("{} page type(s)", refs.len())
        } else {
            names.join(", ")
        };
        return redirect_named_with_message(
            "rcms-admin:components:index",
            MsgLevel::Error,
            &format!("Can't delete `{}` — still used by: {listed}.", c.slug),
            &headers,
        );
    }
    crate::page_builder::model::delete_component(tenant.pool(), id).await?;
    redirect_named_with_message(
        "rcms-admin:components:index",
        MsgLevel::Success,
        "Component deleted.",
        &headers,
    )
}

// ---- #566 UI-created page types -----------------------------------------

/// GET /cms-admin/page-types/new — the create-a-type form.
pub async fn page_type_new_form(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
) -> Result<Response, AdminError> {
    if let Some(r) = ensure_builder_access(&tenant, session_user.as_ref()).await {
        return Ok(r);
    }
    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "page-types", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("list_url", "/cms-admin/page-types");
    insert_workflow_choices(&mut ctx, tenant.pool()).await;
    render_with_csrf(&state, &headers, "rcms_admin/page_type_form.html", &mut ctx)
}

/// The active workflows, by name, for the page-type form's Workflow field.
/// A failed lookup leaves only "publish directly".
async fn insert_workflow_choices(ctx: &mut Context, pool: &rustango::sql::Pool) {
    let names: Vec<String> = crate::workflow::Workflow::objects()
        .where_(crate::workflow::Workflow::active.eq(true))
        .order_by(&[("name", false)])
        .fetch(pool)
        .await
        .unwrap_or_default()
        .into_iter()
        .map(|w| w.name)
        .collect();
    ctx.insert("workflows", &names);
}

/// A comma-separated list of type identifiers as the JSON array a page-type
/// row stores (empty = no rule).
fn type_list_json(raw: &str) -> serde_json::Value {
    serde_json::Value::Array(
        raw.split(',')
            .map(str::trim)
            .filter(|s| !s.is_empty())
            .map(|s| serde_json::Value::String(s.to_owned()))
            .collect(),
    )
}

/// An admin-made page type by id; `None` for a missing or code type, whose
/// settings are re-seeded from its handler at every boot.
async fn ui_page_type(
    pool: &rustango::sql::Pool,
    id: i64,
) -> Result<Option<crate::page_type_model::PageType>, AdminError> {
    Ok(crate::page_type_model::PageType::objects()
        .where_(crate::page_type_model::PageType::id.eq(id))
        .first(pool)
        .await?
        .filter(|t| crate::page_type::find_handler(&t.type_name).is_none()))
}

/// GET /cms-admin/page-types/{id}/edit — an admin-made type's settings.
pub async fn page_type_edit_form(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    Path(id): Path<i64>,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
) -> Result<Response, AdminError> {
    if let Some(r) = ensure_builder_access(&tenant, session_user.as_ref()).await {
        return Ok(r);
    }
    let Some(pt) = ui_page_type(tenant.pool(), id).await? else {
        return Err(AdminError::NotFound(id));
    };
    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "page-types", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("list_url", "/cms-admin/page-types");
    ctx.insert("edit_id", &id);
    ctx.insert("form_verbose_name", &pt.verbose_name);
    ctx.insert("form_type_name", &pt.type_name);
    ctx.insert("form_allowed_parents", &pt.allowed_parents().join(", "));
    ctx.insert("form_allowed_children", &pt.allowed_children().join(", "));
    ctx.insert("form_workflow", &pt.workflow);
    insert_workflow_choices(&mut ctx, tenant.pool()).await;
    render_with_csrf(&state, &headers, "rcms_admin/page_type_form.html", &mut ctx)
}

/// POST /cms-admin/page-types/{id}/edit — save an admin-made type's name,
/// parent/child rules and workflow. The identifier can't change.
pub async fn page_type_update(
    tenant: Tenant,
    headers: HeaderMap,
    Path(id): Path<i64>,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Form(form): Form<FormPageTypeCreate>,
) -> Result<Response, AdminError> {
    if let Some(r) = ensure_builder_access(&tenant, session_user.as_ref()).await {
        return Ok(r);
    }
    let Some(mut pt) = ui_page_type(tenant.pool(), id).await? else {
        return Err(AdminError::NotFound(id));
    };
    if form.verbose_name.trim().is_empty() {
        return Err(AdminError::Validation("Name is required.".to_owned()));
    }
    pt.verbose_name = form.verbose_name.trim().to_owned();
    pt.allowed_parent_types = type_list_json(&form.allowed_parents);
    pt.allowed_child_types = type_list_json(&form.allowed_children);
    pt.workflow = form.workflow.trim().to_owned();
    pt.save_pool(tenant.pool()).await?;
    redirect_named_with_message(
        "rcms-admin:page-types",
        MsgLevel::Success,
        "Page type settings saved.",
        &headers,
    )
}

/// POST /cms-admin/page-types/new — create a UI page type
/// (`app_label = "cms_ui"`) + an empty draft schema, then jump to the
/// field builder so the Developer designs its body immediately.
pub async fn page_type_create(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Form(form): Form<FormPageTypeCreate>,
) -> Result<Response, AdminError> {
    if let Some(r) = ensure_builder_access(&tenant, session_user.as_ref()).await {
        return Ok(r);
    }
    // Blank identifier derives from the name — `slugify_key` is a FIELD-key
    // slugifier whose empty-input fallback is the literal "field", which made
    // every no-identifier type `cms_ui.field` (and the second one collide).
    let ident_src = if form.type_name.trim().is_empty() {
        form.verbose_name.as_str()
    } else {
        form.type_name.as_str()
    };
    let slug = crate::forms::schema::slugify_key(ident_src);
    let err: Option<String> = if form.verbose_name.trim().is_empty() {
        Some("Name is required.".to_owned())
    } else if !crate::forms::schema::is_valid_key(&slug) {
        Some(
            "Identifier is invalid — use letters/digits/underscores, starting with a letter."
                .to_owned(),
        )
    } else if crate::page_type_model::PageType::objects()
        .where_(crate::page_type_model::PageType::type_name.eq(slug.clone()))
        .first(tenant.pool())
        .await?
        .is_some()
        || crate::page_type::find_handler(&slug).is_some()
    {
        Some(format!(
            "A page type with identifier `{slug}` already exists."
        ))
    } else {
        None
    };
    if let Some(msg) = err {
        let mut ctx = Context::new();
        add_chrome(&mut ctx, &tenant, "page-types", session_user.as_ref()).await;
        add_admin_theme(&mut ctx, tenant.pool()).await;
        ctx.insert("list_url", "/cms-admin/page-types");
        ctx.insert("error", &msg);
        ctx.insert("form_verbose_name", &form.verbose_name);
        ctx.insert("form_type_name", &form.type_name);
        ctx.insert("form_allowed_parents", &form.allowed_parents);
        ctx.insert("form_allowed_children", &form.allowed_children);
        ctx.insert("form_workflow", &form.workflow);
        insert_workflow_choices(&mut ctx, tenant.pool()).await;
        return render_with_csrf(&state, &headers, "rcms_admin/page_type_form.html", &mut ctx);
    }
    // An API type has no HTML representation, so it gets the blank
    // template sentinel rather than the generic schema template —
    // otherwise `page_view::resolve_kind` would see a template and
    // never reach the JSON branch.
    let view_mode = crate::page_view::PageViewMode::parse(&form.view_mode);
    let default_template = if view_mode == crate::page_view::PageViewMode::Api {
        String::new()
    } else {
        crate::page_builder::DEFAULT_SCHEMA_TEMPLATE.to_owned()
    };
    let mut row = crate::page_type_model::PageType {
        id: rustango::sql::Auto::Unset,
        app_label: "cms_ui".to_owned(),
        type_name: slug.clone(),
        verbose_name: form.verbose_name.trim().to_owned(),
        default_template,
        view_mode: view_mode.as_str().to_owned(),
        is_creatable: true,
        allowed_parent_types: type_list_json(&form.allowed_parents),
        allowed_child_types: type_list_json(&form.allowed_children),
        workflow: form.workflow.trim().to_owned(),
        created_at: rustango::sql::Auto::Unset,
        updated_at: rustango::sql::Auto::Unset,
    };
    row.insert_pool(tenant.pool()).await?;
    let new_id = row.id.get().copied().unwrap_or_default();
    // Seed an empty draft schema so the builder opens cleanly.
    let created_by = session_user.as_ref().and_then(|u| u.id.get().copied());
    crate::page_builder::model::save_draft(
        tenant.pool(),
        new_id,
        serde_json::json!({ "nodes": [] }),
        created_by,
    )
    .await
        .log_warn("empty builder draft not seeded");
    redirect_named_with_params_and_message(
        "rcms-admin:page-types:build",
        &[("id", new_id.to_string())],
        MsgLevel::Success,
        "Page type created — design its body, then Publish.",
        &headers,
    )
}

/// POST /cms-admin/page-types/{id}/delete — delete a UI-created type.
/// Refused for code types and for types with existing pages.
pub async fn page_type_delete(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
) -> Result<Response, AdminError> {
    if let Some(r) = ensure_builder_access(&tenant, session_user.as_ref()).await {
        return Ok(r);
    }
    let pt = crate::page_type_model::PageType::objects()
        .where_(crate::page_type_model::PageType::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    // Only UI-created types are deletable; code types never are.
    if pt.app_label != "cms_ui" {
        return redirect_named_with_message(
            "rcms-admin:page-types",
            MsgLevel::Error,
            "Only UI-created page types can be deleted.",
            &headers,
        );
    }
    let page_count = Page::objects()
        .where_(Page::page_type_id.eq(id))
        .fetch(tenant.pool())
        .await
        .map(|v| v.len())
        .unwrap_or(0);
    if page_count > 0 {
        return redirect_named_with_message(
            "rcms-admin:page-types",
            MsgLevel::Error,
            "Can't delete — pages still use this type. Delete or move them first.",
            &headers,
        );
    }
    // Drop the type's schema rows (draft/published/archived) then the row.
    for row in crate::page_builder::model::list_versions(tenant.pool(), id)
        .await
        .unwrap_or_default()
    {
        row.delete_pool(tenant.pool()).await.log_warn("page-builder schema version not deleted");
    }
    pt.delete_pool(tenant.pool()).await?;
    redirect_named_with_message(
        "rcms-admin:page-types",
        MsgLevel::Success,
        "Page type deleted.",
        &headers,
    )
}

/// The canonical column of a translation row shows rich text formatted,
/// not as `<p>…</p>` markup. Sanitized: it is author content rendered
/// into the admin. Empty for every other widget kind (shown as text).
fn translation_canonical_html(widget_tag: &str, canonical: &str) -> String {
    if widget_tag == "richtext" && !canonical.trim().is_empty() {
        crate::markdown::sanitize_html(canonical)
    } else {
        String::new()
    }
}

/// Translation-surface groups for a page's builder body: the
/// canonical text leaves (fixed fields, group members, zone stream
/// blocks) grouped like the StreamField leaf rows, each prefilled with
/// its existing per-locale override. Returns `[]` when the type has no
/// published schema. Paths are `builder.<…>` (see `values::…leaves`).
async fn builder_translation_groups(
    tenant: &Tenant,
    page_id: i64,
    page_type_id: i64,
    existing: &std::collections::HashMap<String, String>,
) -> (Vec<serde_json::Value>, std::collections::HashSet<String>) {
    let mut paths = std::collections::HashSet::new();
    let Ok(Some(row)) =
        crate::page_builder::model::published_for(tenant.pool(), page_type_id).await
    else {
        return (Vec::new(), paths);
    };
    let Ok(doc) = crate::page_builder::parse_schema(&row.document) else {
        return (Vec::new(), paths);
    };
    let components = crate::page_builder::model::component_map(tenant.pool())
        .await
        .unwrap_or_default();
    let compiled = crate::page_builder::compile(&doc, &components, row.version.max(1) as u32);
    let (values, _) = crate::page_builder::values::load_upgraded(tenant.pool(), page_id, &compiled)
        .await
        .unwrap_or_else(|_| (serde_json::json!({}), Default::default()));
    let leaves = crate::page_builder::values::builder_translatable_leaves(&compiled, &values);
    // Group consecutively by block instance, mirroring stream_translations.
    let mut groups: Vec<serde_json::Value> = Vec::new();
    let mut cur = String::new();
    for leaf in &leaves {
        paths.insert(leaf.path.clone());
        let rowv = serde_json::json!({
            "path": leaf.path,
            "widget_kind": leaf.widget_kind.as_tag(),
            "field_label": leaf.field_label,
            "canonical": leaf.canonical_text,
            "canonical_html": translation_canonical_html(leaf.widget_kind.as_tag(), &leaf.canonical_text),
            "value": existing.get(&leaf.path).cloned().unwrap_or_default(),
        });
        if leaf.block_id != cur {
            cur.clone_from(&leaf.block_id);
            groups.push(serde_json::json!({
                "block_type": leaf.block_type,
                "block_label": leaf.block_label,
                "block_id": leaf.block_id,
                "depth": leaf.depth,
                "leaves": [rowv],
            }));
        } else if let Some(arr) = groups
            .last_mut()
            .and_then(|g| g.get_mut("leaves"))
            .and_then(|v| v.as_array_mut())
        {
            arr.push(rowv);
        }
    }
    (groups, paths)
}

/// POST /cms-admin/media/bulk-move-collection — move every checked
/// media id into the selected `collection_id` (or NULL for
/// uncategorized).
pub async fn media_bulk_move_collection(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Form(form): Form<Vec<(String, String)>>,
) -> Result<Response, AdminError> {
    let target: Option<i64> = form
        .iter()
        .find(|(k, _)| k == "collection_id")
        .and_then(|(_, v)| v.parse::<i64>().ok())
        .filter(|id| *id > 0);
    let ids: Vec<i64> = form
        .iter()
        .filter(|(k, _)| k == "ids" || k == "id")
        .filter_map(|(_, v)| v.parse::<i64>().ok())
        .collect();
    let kind = form
        .iter()
        .find(|(k, _)| k == "kind")
        .map(|(_, v)| v.clone())
        .unwrap_or_else(|| "image".to_owned());
    let mut touched = 0;
    for id in ids {
        if let Some(mut m) = Media::objects()
            .where_(Media::id.eq(id))
            .first(tenant.pool())
            .await?
        {
            m.collection_id = target;
            m.save_pool(tenant.pool()).await?;
            touched += 1;
        }
    }
    let redirect_url = if kind == "document" {
        "rcms-admin:documents:list"
    } else {
        "rcms-admin:media:list"
    };
    let dest = match target {
        Some(id) => format!("to collection #{id}"),
        None => "to Uncategorized".to_owned(),
    };
    redirect_named_with_message_plural(
        redirect_url,
        MsgLevel::Success,
        "Moved {count} item(s) {dest}.",
        touched as i64,
        &[("count", &touched.to_string()), ("dest", &dest)],
        &headers,
    )
}

/// GET /cms-admin/media/collections/{id}/permissions — per-collection
/// permission editor. Same role × action grid shape as the
/// page permissions tab, just retargeted at `cms_collection_permission`.
pub async fn collection_permissions_form(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
) -> Result<Response, AdminError> {
    let collection = crate::media::MediaCollection::objects()
        .where_(crate::media::MediaCollection::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    let roles: Vec<rustango::tenancy::permissions::Role> =
        rustango::tenancy::permissions::Role::objects()
            .order_by(&[("name", false)])
            .fetch(tenant.pool())
            .await?;
    let grants: Vec<crate::permissions::CollectionPermission> =
        crate::permissions::CollectionPermission::objects()
            .where_(crate::permissions::CollectionPermission::collection_id.eq(id))
            .fetch(tenant.pool())
            .await?;
    let granted: std::collections::HashSet<(i64, String)> = grants
        .iter()
        .map(|g| (g.role_id, g.permission.clone()))
        .collect();
    let actions: Vec<&str> = crate::permissions::Action::all()
        .iter()
        .map(|a| a.as_str())
        .collect();
    let perm_grid: Vec<serde_json::Value> = roles
        .iter()
        .map(|r| {
            let rid = r.id.get().copied().unwrap_or_default();
            let row_cells: Vec<serde_json::Value> = actions
                .iter()
                .map(|a| {
                    serde_json::json!({
                        "action": a,
                        "granted": granted.contains(&(rid, (*a).to_owned())),
                    })
                })
                .collect();
            serde_json::json!({
                "role_id": rid,
                "role_name": r.name,
                "role_description": r.description,
                "cells": row_cells,
            })
        })
        .collect();

    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "media", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("collection_id", &id);
    ctx.insert("collection_name", &collection.name);
    ctx.insert("perm_actions", &actions);
    ctx.insert("perm_grid", &perm_grid);

    // #195 — view restriction picker. Surface the current direct
    // restriction (if any) and the role list for the groups option.
    let restriction = crate::collection_view_restriction::direct_for_collection(tenant.pool(), id)
        .await
        .unwrap_or_default();
    let restriction_kind = restriction
        .as_ref()
        .map(|r| r.kind.clone())
        .unwrap_or_else(|| "none".to_owned());
    let restriction_groups: std::collections::HashSet<i64> = restriction
        .as_ref()
        .map(|r| r.parsed_groups().into_iter().collect())
        .unwrap_or_default();
    let restriction_has_password = restriction
        .as_ref()
        .map(|r| !r.password_hash.is_empty())
        .unwrap_or(false);
    let role_options: Vec<serde_json::Value> = roles
        .iter()
        .map(|r| {
            let rid = r.id.get().copied().unwrap_or_default();
            serde_json::json!({
                "id": rid,
                "name": r.name,
                "selected": restriction_groups.contains(&rid),
            })
        })
        .collect();
    // #members — permission-codename picker (the permission-engine gate).
    let restriction_codenames: Vec<String> = restriction
        .as_ref()
        .map(|r| r.parsed_codenames())
        .unwrap_or_default();
    let restriction_codenames_str = restriction_codenames.join(",");
    let codename_choices: Vec<serde_json::Value> = super::resources::membership_codenames()
        .into_iter()
        .map(|(codename, label)| {
            serde_json::json!({
                "codename": codename,
                "label": label,
                "checked": restriction_codenames.contains(&codename),
            })
        })
        .collect();
    ctx.insert("restriction_kind", &restriction_kind);
    ctx.insert("restriction_has_password", &restriction_has_password);
    ctx.insert("restriction_roles", &role_options);
    ctx.insert("restriction_codenames_str", &restriction_codenames_str);
    ctx.insert("restriction_codename_choices", &codename_choices);

    render_with_csrf(
        &state,
        &headers,
        "rcms_admin/collection_permissions.html",
        &mut ctx,
    )
}

/// Form body for `POST /cms-admin/media/collections/{id}/privacy`.
#[derive(Deserialize)]
pub struct CollectionPrivacyForm {
    pub kind: String,
    #[serde(default)]
    pub password: String,
    #[serde(default)]
    pub group_ids: String,
    /// Comma-separated permission codenames — used when `kind =
    /// permission` (the permission-engine gate). Empty otherwise.
    #[serde(default)]
    pub codenames: String,
}

/// POST /cms-admin/media/collections/{id}/privacy — upsert the view
/// restriction for a collection. `kind=none` deletes the row.
/// Mirrors the page-privacy submit handler exactly.
pub async fn collection_privacy_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
    Form(form): Form<CollectionPrivacyForm>,
) -> Result<Response, AdminError> {
    use rustango::sql::Auto;
    let existing =
        crate::collection_view_restriction::direct_for_collection(tenant.pool(), id).await?;
    if form.kind == "none" {
        if let Some(row) = existing {
            row.delete_pool(tenant.pool()).await?;
            return redirect_named_with_params_and_message(
                "rcms-admin:media:collection-permissions",
                &[("id", id.to_string())],
                MsgLevel::Success,
                "Removed view restriction — collection is now public.",
                &headers,
            );
        }
        return redirect_named_with_params_and_message(
            "rcms-admin:media:collection-permissions",
            &[("id", id.to_string())],
            MsgLevel::Info,
            "No restriction to remove.",
            &headers,
        );
    }
    let Some(kind) = crate::collection_view_restriction::RestrictionKind::parse(&form.kind) else {
        return Err(AdminError::Validation(format!(
            "unknown restriction kind `{}`",
            form.kind
        )));
    };
    let password_hash = match kind {
        crate::collection_view_restriction::RestrictionKind::Password => {
            if form.password.is_empty() {
                if let Some(row) = &existing {
                    if row.kind == kind.as_str() {
                        row.password_hash.clone()
                    } else {
                        return redirect_named_with_params_and_message(
                            "rcms-admin:media:collection-permissions",
                            &[("id", id.to_string())],
                            MsgLevel::Error,
                            "Password is required when switching the restriction kind to password.",
                            &headers,
                        );
                    }
                } else {
                    return redirect_named_with_params_and_message(
                        "rcms-admin:media:collection-permissions",
                        &[("id", id.to_string())],
                        MsgLevel::Error,
                        "Password is required for password-protected collections.",
                        &headers,
                    );
                }
            } else {
                crate::passwords::hash(&form.password).await
                    .map_err(|e| AdminError::Validation(format!("password hash failed: {e}")))?
            }
        }
        _ => String::new(),
    };
    let group_ids: Vec<i64> = if matches!(
        kind,
        crate::collection_view_restriction::RestrictionKind::Groups
    ) {
        form.group_ids
            .split(',')
            .filter_map(|s| s.trim().parse::<i64>().ok())
            .collect()
    } else {
        Vec::new()
    };
    let group_json = serde_json::json!(group_ids);
    let codenames: Vec<String> = if matches!(
        kind,
        crate::collection_view_restriction::RestrictionKind::Permission
    ) {
        form.codenames
            .split(',')
            .map(str::trim)
            .filter(|s| !s.is_empty())
            .map(str::to_owned)
            .collect()
    } else {
        Vec::new()
    };
    let codenames_json = serde_json::json!(codenames);
    match existing {
        Some(mut row) => {
            row.kind = kind.as_str().to_owned();
            row.password_hash = password_hash;
            row.group_ids = group_json;
            row.codenames = codenames_json;
            row.save_pool(tenant.pool()).await?;
        }
        None => {
            let mut row = crate::collection_view_restriction::CollectionViewRestriction {
                id: Auto::Unset,
                collection_id: id,
                kind: kind.as_str().to_owned(),
                password_hash,
                group_ids: group_json,
                codenames: codenames_json,
                created_at: Auto::Unset,
                updated_at: Auto::Unset,
            };
            row.insert_pool(tenant.pool()).await?;
        }
    }
    redirect_named_with_params_and_message(
        "rcms-admin:media:collection-permissions",
        &[("id", id.to_string())],
        MsgLevel::Success,
        "Collection privacy updated. New requests to /__media__/<spec>/<id> will enforce it.",
        &headers,
    )
}

/// POST /cms-admin/media/collections/{id}/permissions — replace the
/// collection's permission set with the JSON body's grant list.
/// Idempotent want/have diff, returns 204 on success. Mirrors the
/// page-permission save endpoint exactly.
pub async fn collection_permissions_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    Path(collection_id): Path<i64>,
    axum::Json(payload): axum::Json<PagePermissionsPayload>,
) -> Result<Response, AdminError> {
    use rustango::sql::Auto;
    let valid_actions: std::collections::HashSet<&str> = crate::permissions::Action::all()
        .iter()
        .map(|a| a.as_str())
        .collect();
    let want: std::collections::BTreeSet<(i64, String)> = payload
        .grants
        .iter()
        .filter(|g| valid_actions.contains(g.action.as_str()))
        .map(|g| (g.role_id, g.action.clone()))
        .collect();

    let existing: Vec<crate::permissions::CollectionPermission> =
        crate::permissions::CollectionPermission::objects()
            .where_(crate::permissions::CollectionPermission::collection_id.eq(collection_id))
            .fetch(tenant.pool())
            .await?;
    let have: std::collections::BTreeSet<(i64, String)> = existing
        .iter()
        .map(|p| (p.role_id, p.permission.clone()))
        .collect();

    for p in &existing {
        let key = (p.role_id, p.permission.clone());
        if !want.contains(&key) {
            p.clone().delete_pool(tenant.pool()).await?;
        }
    }
    for (role_id, action) in &want {
        if !have.contains(&(*role_id, action.clone())) {
            let mut row = crate::permissions::CollectionPermission {
                id: Auto::Unset,
                collection_id,
                role_id: *role_id,
                permission: action.clone(),
                created_at: Auto::Unset,
            };
            row.insert_pool(tenant.pool()).await?;
        }
    }
    Ok(axum::http::StatusCode::NO_CONTENT.into_response())
}

/// JSON payload for the permission tab's bulk save endpoint —
/// the grid POSTs a list of `(role_id, action)` pairs and the
/// handler replaces the page's grant set with that list.
#[derive(Debug, Deserialize)]
pub struct PagePermissionsPayload {
    pub grants: Vec<PagePermissionGrant>,
}
#[derive(Debug, Deserialize)]
pub struct PagePermissionGrant {
    pub role_id: i64,
    pub action: String,
}

/// POST /cms-admin/pages/{id}/permissions — replace the page's
/// permission set with the list in the JSON body. Idempotent;
/// computes the want/have diff and applies the minimum INSERTs +
/// DELETEs. Returns 204 on success.
pub async fn page_permissions_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    Path(page_id): Path<i64>,
    axum::Json(payload): axum::Json<PagePermissionsPayload>,
) -> Result<Response, AdminError> {
    use rustango::sql::Auto;
    let valid_actions: std::collections::HashSet<&str> = crate::permissions::Action::all()
        .iter()
        .map(|a| a.as_str())
        .collect();
    let want: std::collections::BTreeSet<(i64, String)> = payload
        .grants
        .iter()
        .filter(|g| valid_actions.contains(g.action.as_str()))
        .map(|g| (g.role_id, g.action.clone()))
        .collect();

    let existing: Vec<crate::permissions::PagePermission> =
        crate::permissions::PagePermission::objects()
            .where_(crate::permissions::PagePermission::page_id.eq(page_id))
            .fetch(tenant.pool())
            .await?;
    let have: std::collections::BTreeSet<(i64, String)> = existing
        .iter()
        .map(|p| (p.role_id, p.permission.clone()))
        .collect();

    // Drop rows that aren't wanted.
    for p in &existing {
        let key = (p.role_id, p.permission.clone());
        if !want.contains(&key) {
            p.clone().delete_pool(tenant.pool()).await?;
        }
    }
    // Insert rows that don't exist yet.
    for (role_id, action) in &want {
        if !have.contains(&(*role_id, action.clone())) {
            let mut row = crate::permissions::PagePermission {
                id: Auto::Unset,
                page_id,
                role_id: *role_id,
                permission: action.clone(),
                created_at: Auto::Unset,
            };
            row.insert_pool(tenant.pool()).await?;
        }
    }

    Ok(axum::http::StatusCode::NO_CONTENT.into_response())
}

/// Resolve every [`TabSpec`](crate::page_type::TabSpec) variant
/// into the flat shape page_form.html iterates: `{name, label, icon,
/// badge, html}`. Failures on `Template` / `Widgets` rendering
/// fall back to an inline error banner instead of crashing the page
/// editor — the author sees the Tera message + can fix the template.
fn render_extra_tabs(
    specs: Vec<crate::page_type::TabSpec>,
    tera: &std::sync::Arc<tera::Tera>,
    base_ctx: &Context,
    headers: &HeaderMap,
) -> Vec<serde_json::Value> {
    use crate::page_type::TabBody;
    specs
        .into_iter()
        .map(|spec| {
            let html = match spec.body {
                TabBody::Html(s) => s,
                TabBody::Template { path, context } => {
                    let mut tab_ctx = base_ctx.clone();
                    for (k, v) in context {
                        tab_ctx.insert(&k, &v);
                    }
                    tera.render(&path, &tab_ctx).unwrap_or_else(|e| {
                        format!(
                            r#"<div class="rcms-flash rcms-flash--error" style="margin: 12px 0;">
    <span class="rcms-flash-body"><strong>{label}</strong> {failed} <code>{path}</code>: {e}</span>
</div>"#,
                            label = tera::escape_html(&super::i18n::tr(headers, "Tab template error:", &[])),
                            failed = tera::escape_html(&super::i18n::tr(headers, "failed to render", &[])),
                            path = tera::escape_html(&path),
                            e = tera::escape_html(&e.to_string()),
                        )
                    })
                }
                TabBody::Widgets(widgets) => {
                    let mut ctx = Context::new();
                    ctx.insert("widgets", &widgets);
                    tera.render("rcms_admin/_widget_group.html", &ctx)
                        .unwrap_or_else(|e| {
                            format!(
                                r#"<div class="rcms-flash rcms-flash--error" style="margin: 12px 0;">
    <span class="rcms-flash-body"><strong>{label}</strong> {e}</span>
</div>"#,
                                label = tera::escape_html(&super::i18n::tr(headers, "Widget-group tab error:", &[])),
                                e = tera::escape_html(&e.to_string()),
                            )
                        })
                }
                TabBody::Group { heading, help, layout, widgets } => {
                    // Field group panel (column or row layout).
                    // Wrap the same _widget_group output in a
                    // <fieldset> with a heading + (optional) help
                    // paragraph + layout class.
                    let mut ctx = Context::new();
                    ctx.insert("widgets", &widgets);
                    let inner = tera
                        .render("rcms_admin/_widget_group.html", &ctx)
                        .unwrap_or_else(|e| {
                            format!(
                                r#"<div class="rcms-flash rcms-flash--error" style="margin: 12px 0;">
    <span class="rcms-flash-body"><strong>{label}</strong> {e}</span>
</div>"#,
                                label = tera::escape_html(&super::i18n::tr(headers, "Panel-group tab error:", &[])),
                                e = tera::escape_html(&e.to_string()),
                            )
                        });
                    let help_html = if help.is_empty() {
                        String::new()
                    } else {
                        format!(
                            r#"<p class="rcms-hint rcms-panel-group-help">{}</p>"#,
                            tera::escape_html(&help),
                        )
                    };
                    format!(
                        r#"<fieldset class="rcms-panel-group rcms-panel-group--{layout}">
    <legend>{heading}</legend>
    {help_html}
    <div class="rcms-panel-group-body">{inner}</div>
</fieldset>"#,
                        layout = layout.class_suffix(),
                        heading = tera::escape_html(&heading),
                        help_html = help_html,
                        inner = inner,
                    )
                }
                TabBody::Help { content } => {
                    // Static help block. Author-supplied HTML; emit
                    // raw inside a hint-styled container.
                    format!(
                        r#"<div class="rcms-panel-help">{content}</div>"#,
                        content = content,
                    )
                }
            };
            serde_json::json!({
                "name": spec.name,
                "label": spec.label,
                "icon": spec.icon,
                "badge": spec.badge,
                "html": html,
            })
        })
        .collect()
}

/// GET /cms-admin/x/{slug} — custom admin page dispatcher. Looks up the
/// registered [`AdminPageHandler`](super::admin_page::AdminPageHandler)
/// matching `slug` and forwards to its `render`. Unknown slugs map
/// to 404.
pub async fn admin_page_dispatch(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(slug): Path<String>,
) -> Result<Response, AdminError> {
    match super::admin_page::find_admin_page(&slug) {
        Some(handler) => Ok(handler
            .render(super::admin_page::AdminPageCtx {
                tera: &state.tera,
                tenant: &tenant,
                headers: &headers,
                user: session_user.as_ref(),
            })
            .await),
        None => Err(AdminError::NotFound(0)),
    }
}

/// Load the active admin theme + its brand colors and stuff the
/// rendered CSS / data-attrs into the Tera context so `_base.html`
/// can drop them straight into the page chrome. Falls back gracefully
/// if no theme is configured (admin renders with built-in defaults).
pub(crate) async fn add_admin_theme(ctx: &mut Context, pool: &rustango::sql::Pool) {
    // Pick the row flagged `is_admin_default`; if none, fall back to
    // whichever is `is_default`; if still none, the first row.
    let mut admin_default: Vec<crate::theme::Theme> = crate::theme::Theme::objects()
        .where_(crate::theme::Theme::is_admin_default.eq(true))
        .fetch(pool)
        .await
        .unwrap_or_default();
    let theme = if let Some(t) = admin_default.pop() {
        t
    } else {
        let mut any: Vec<crate::theme::Theme> = crate::theme::Theme::objects()
            .where_(crate::theme::Theme::is_default.eq(true))
            .fetch(pool)
            .await
            .unwrap_or_default();
        match any.pop() {
            Some(t) => t,
            None => {
                let mut first: Vec<crate::theme::Theme> = crate::theme::Theme::objects()
                    .order_by(&[("name", false)])
                    .fetch(pool)
                    .await
                    .unwrap_or_default();
                let Some(t) = first.pop() else {
                    // No themes at all — set empty strings so the
                    // template's `{% if %}` guards short-circuit.
                    ctx.insert("theme_css", &String::new());
                    ctx.insert("theme_slug", &String::new());
                    ctx.insert("theme_default_mode", &"light");
                    ctx.insert("theme_font_url", &String::new());
                    return;
                };
                t
            }
        }
    };
    let theme_id = theme.id.get().copied().unwrap_or_default();
    let brand_colors: Vec<crate::theme::BrandColor> = crate::theme::BrandColor::objects()
        .where_(crate::theme::BrandColor::theme_id.eq(theme_id))
        .order_by(&[("sort_order", false)])
        .fetch(pool)
        .await
        .unwrap_or_default();
    let css = crate::theme::emit_css(&theme, &brand_colors);
    ctx.insert("theme_css", &css);
    ctx.insert("theme_slug", &theme.slug);
    ctx.insert("theme_name", &theme.name);
    ctx.insert("theme_default_mode", &theme.default_mode);
    ctx.insert("theme_font_url", &theme.font_url);
}

/// Query string for the page list / search view.
#[derive(Debug, Deserialize)]
pub struct ListQuery {
    /// Filter children of this parent only. Omitted = root listing.
    /// Ignored when `q` is set — search always spans the full tree.
    #[serde(default)]
    pub parent: Option<i64>,
    /// Full-tree search query. Case-insensitive substring match
    /// across title, slug, and url_path. Empty string skips the
    /// search path and shows the current parent's children.
    #[serde(default)]
    pub q: Option<String>,
    /// Filter results by page type slug — pairs with `q` or stands
    /// alone. Matches `cms_page_type.type_name`.
    #[serde(default)]
    pub r#type: Option<String>,
    /// Filter results by status (`draft|scheduled|published|archived`).
    #[serde(default)]
    pub status: Option<String>,
}

/// GET /cms-admin/pages — flat list of pages directly under the
/// current parent (root by default). Editors drill into a subtree
/// via the per-row "View children" action; the breadcrumb trail
/// at the top lets them navigate back up. Replaces the previous
/// indented full-tree view.
///
/// Per-page permissions: when the request carries a
/// session cookie, the resolver filters out rows the user can't
/// `view`. Superusers + anonymous / no-session contexts (sqlite
/// dev) see every row.
pub async fn page_list(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Query(q): Query<ListQuery>,
) -> Result<Response, AdminError> {
    // Best-effort schedule sweep so editors always see current state.
    // #207 — uses the mailer-aware variant so the pre-publish
    // reminder pass runs on the same tick.
    match crate::page::run_schedule_sweep_with_mailer(
        tenant.pool(),
        state.mailer.as_deref(),
        &state.mailer_from,
        &tenant.org.slug,
    )
    .await
    {
        // A page this sweep put live or took down must leave the cache
        // too, as the timed sweep's purge does (#692).
        Ok(result) if !result.changed_urls.is_empty() => {
            crate::task_queue::purge_urls(
                state.cache_invalidator.clone(),
                tenant.org.slug.clone(),
                result.changed_urls,
            )
            .await;
        }
        Ok(_) => {}
        Err(e) => tracing::warn!(target: "rustango_cms::admin", error = %e, "schedule sweep failed"),
    }

    // Pull every page once — we need the full list both for
    // breadcrumb computation (walk ancestors of `parent`) and child
    // counts (how many pages live under each row in the current
    // listing). Tenants stay in the small-N range; the materialized
    // list is cheap.
    let all_pages: Vec<Page> = Page::objects()
        .order_by(&[("path", false), ("sort_order", false)])
        .fetch(tenant.pool())
        .await?;

    // Index pages by id for ancestor walk.
    let by_id: std::collections::HashMap<i64, &Page> = all_pages
        .iter()
        .filter_map(|p| p.id.get().copied().map(|id| (id, p)))
        .collect();

    // Current parent — None means root listing.
    let parent_page: Option<&Page> = q.parent.and_then(|pid| by_id.get(&pid).copied());

    // Breadcrumb: walk parent chain up to root, then reverse.
    let mut breadcrumbs: Vec<serde_json::Value> = Vec::new();
    if let Some(mut cur) = parent_page {
        breadcrumbs.push(serde_json::json!({
            "id": cur.id.get().copied(),
            "title": cur.title.clone(),
        }));
        while let Some(ppid) = cur.parent_id {
            if let Some(p) = by_id.get(&ppid).copied() {
                breadcrumbs.push(serde_json::json!({
                    "id": p.id.get().copied(),
                    "title": p.title.clone(),
                }));
                cur = p;
            } else {
                break;
            }
        }
        breadcrumbs.reverse();
    }

    // Determine the working set:
    //   - search mode (`q`): full tree, filtered by ILIKE on title/slug/url_path
    //   - browse mode (no `q`): direct children of `parent` only
    let search_query =
        q.q.as_deref()
            .map(str::trim)
            .filter(|s| !s.is_empty())
            .map(str::to_owned);
    let status_filter = q
        .status
        .as_deref()
        .map(str::trim)
        .filter(|s| !s.is_empty())
        .map(str::to_owned);
    let type_filter_slug = q
        .r#type
        .as_deref()
        .map(str::trim)
        .filter(|s| !s.is_empty())
        .map(str::to_owned);

    // Per-page permissions filter (#18 phase 3). Skipped when the
    // request is anonymous (sqlite dev / no session) or the user
    // is a tenant superuser — both paths see every page. For
    // authenticated, non-superuser sessions we pre-compute the
    // viewable set so the browse/search filter below can apply it.
    let viewer_user_id: Option<i64> = session_user
        .as_ref()
        .filter(|u| u.active)
        .and_then(|u| u.id.get().copied());
    let viewer_is_superuser = session_user.as_ref().is_some_and(|u| u.is_superuser);
    let permission_filter_enabled = viewer_user_id.is_some() && !viewer_is_superuser;
    let viewable_ids: Option<std::collections::HashSet<i64>> = match viewer_user_id {
        Some(uid) if permission_filter_enabled => {
            let pages: Vec<(i64, Option<i64>)> = all_pages
                .iter()
                .filter_map(|p| p.id.get().copied().map(|id| (id, p.parent_id)))
                .collect();
            Some(
                crate::permissions::viewable_page_ids(tenant.pool(), uid, &pages)
                    .await
                    .unwrap_or_default(),
            )
        }
        _ => None,
    };

    // #408 — relevance-ranked, all-status admin search on Postgres: FTS
    // finds stemmed / word-boundary matches the substring filter misses
    // (across drafts too) and orders by relevance. `None` on non-PG / a
    // query error → the substring filter below stands.
    let fts_rank: Option<std::collections::HashMap<i64, usize>> = match search_query.as_deref() {
        Some(q) => crate::search::search_page_ids_all_statuses(tenant.pool(), q, 1000)
            .await
            .map(|ids| ids.iter().enumerate().map(|(i, id)| (*id, i)).collect()),
        None => None,
    };
    let visible: Vec<&Page> = if search_query.is_some()
        || status_filter.is_some()
        || type_filter_slug.is_some()
    {
        // Search / filter mode — full-tree.
        let q_lower = search_query.as_deref().map(str::to_lowercase);
        // Resolve type slug → id via the not-yet-fetched type list.
        // We pull `cms_page_type` below for the verbose-name map
        // either way; do it inline here for the filter id.
        let prefetch_types: Vec<PageType> = PageType::objects().fetch(tenant.pool()).await?;
        let type_id_by_slug: std::collections::HashMap<String, i64> = prefetch_types
            .iter()
            .filter_map(|t| t.id.get().copied().map(|id| (t.type_name.clone(), id)))
            .collect();
        let type_id_filter = type_filter_slug
            .as_deref()
            .and_then(|s| type_id_by_slug.get(s).copied());
        all_pages
            .iter()
            .filter(|p| {
                // A full-text hit OR a substring match (#702): full-text finds
                // stemmed words, but not a partial word, a slug fragment or a
                // URL path, so on Postgres it must add to the substring match
                // the other backends use, not replace it. Relevance order is
                // the re-sort below; substring-only hits follow the ranked ones.
                let matches_search = match &q_lower {
                    None => true,
                    Some(needle) => {
                        fts_rank.as_ref().is_some_and(|rank| {
                            p.id.get().copied().is_some_and(|id| rank.contains_key(&id))
                        }) || p.title.to_lowercase().contains(needle)
                            || p.slug.to_lowercase().contains(needle)
                            || p.url_path.to_lowercase().contains(needle)
                    }
                };
                let matches_status = match &status_filter {
                    None => true,
                    Some(s) => p.status == *s,
                };
                let matches_type = match type_id_filter {
                    None => true,
                    Some(id) => p.page_type_id == id,
                };
                matches_search && matches_status && matches_type
            })
            .collect()
    } else {
        // Browse mode — direct children only.
        all_pages
            .iter()
            .filter(|p| p.parent_id == q.parent)
            .collect()
    };
    // Snapshot the pre-permission visible count so the empty-state
    // copy can distinguish "tree is empty here" from "you can't see
    // any of these pages" (#70).
    let visible_before_permission_filter = visible.len();
    // Apply per-page view filter as a final pass — affects both
    // search and browse modes uniformly.
    let visible: Vec<&Page> = match &viewable_ids {
        None => visible,
        Some(set) => visible
            .into_iter()
            .filter(|p| p.id.get().copied().is_some_and(|id| set.contains(&id)))
            .collect(),
    };
    // #408 — when FTS drove the search, order results by relevance
    // (overriding the default path order in search mode).
    let visible: Vec<&Page> = if let Some(rank) = &fts_rank {
        let mut v = visible;
        v.sort_by_key(|p| {
            p.id.get()
                .copied()
                .and_then(|id| rank.get(&id).copied())
                .unwrap_or(usize::MAX)
        });
        v
    } else {
        visible
    };
    let is_search_mode =
        search_query.is_some() || status_filter.is_some() || type_filter_slug.is_some();
    // #70 — empty-state classification. Exactly one of these is
    // surfaced into the template when `rows | length == 0`:
    //   * `empty_filtered`         — search/filter mode dropped every row
    //   * `empty_hidden_by_perms`  — per-page permissions dropped every row
    //                                that would otherwise be visible
    //   * (default)                — genuinely empty section
    let empty_filtered = is_search_mode && visible.is_empty();
    let empty_hidden_by_perms = !is_search_mode
        && visible.is_empty()
        && visible_before_permission_filter > 0
        && permission_filter_enabled;

    // Child counts — for the "View children" CTA.
    let mut child_counts: std::collections::HashMap<i64, i64> = std::collections::HashMap::new();
    for p in &all_pages {
        if let Some(ppid) = p.parent_id {
            *child_counts.entry(ppid).or_insert(0) += 1;
        }
    }

    let types: Vec<PageType> = PageType::objects().fetch(tenant.pool()).await?;
    let type_map: std::collections::HashMap<i64, String> = types
        .iter()
        .filter_map(|t| t.id.get().copied().map(|id| (id, t.verbose_name.clone())))
        .collect();

    let live_urls = build_live_url_map(&all_pages);

    // Helper: walk a page's parent chain into a list of `{id, title}`
    // crumbs (root → page). Only computed in search mode where the
    // editor needs context for results that live deep in the tree.
    let ancestors_for = |p: &Page| -> Vec<serde_json::Value> {
        let mut chain: Vec<serde_json::Value> = Vec::new();
        let mut cur_parent = p.parent_id;
        while let Some(pid) = cur_parent {
            match by_id.get(&pid).copied() {
                Some(ancestor) => {
                    chain.push(serde_json::json!({
                        "id": ancestor.id.get().copied(),
                        "title": ancestor.title.clone(),
                    }));
                    cur_parent = ancestor.parent_id;
                }
                None => break,
            }
        }
        chain.reverse();
        chain
    };

    let rows: Vec<serde_json::Value> = visible
        .iter()
        .map(|p| {
            let id = p.id.get().copied();
            let ancestors = if is_search_mode {
                ancestors_for(p)
            } else {
                Vec::new()
            };
            serde_json::json!({
                "id": id,
                "title": p.title,
                "slug": p.slug,
                "path": p.path,
                "url_path": p.url_path,
                "status": p.status,
                "page_type_id": p.page_type_id,
                "go_live_at": p.go_live_at,
                "expire_at": p.expire_at,
                "updated_at": p.updated_at.get().copied(),
                "published_at": p.published_at,
                "locale_variant_of": p.locale_variant_of,
                "alias_of": p.alias_of,
                "child_count": id.and_then(|i| child_counts.get(&i).copied()).unwrap_or(0),
                "ancestors": ancestors,
            })
        })
        .collect();

    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "pages", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("rows", &rows);
    ctx.insert("type_map", &type_map);
    // #614 — the "+ New root page" menu must offer only root-legal types.
    ctx.insert("creatable_types", &allowed_root_types(&types));
    ctx.insert("live_urls", &live_urls);
    ctx.insert("breadcrumbs", &breadcrumbs);
    ctx.insert("parent_id", &q.parent);
    ctx.insert("parent_title", &parent_page.map(|p| p.title.clone()));
    ctx.insert("search_query", &search_query);
    ctx.insert("status_filter", &status_filter);
    ctx.insert("type_filter", &type_filter_slug);
    ctx.insert("is_search_mode", &is_search_mode);
    ctx.insert("empty_filtered", &empty_filtered);
    ctx.insert("empty_hidden_by_perms", &empty_hidden_by_perms);
    // For the type/status filter dropdowns the template renders, we
    // pass a slug → verbose-name map and a sorted slug list.
    let mut type_slugs: Vec<(String, String)> = types
        .iter()
        .map(|t| (t.type_name.clone(), t.verbose_name.clone()))
        .collect();
    type_slugs.sort_by(|a, b| a.1.cmp(&b.1));
    ctx.insert("type_choices", &type_slugs);

    // #65 — Move-to destination picker for the bulk action bar.
    // Every page (any depth) is a valid parent except the visible
    // selection itself; we expose the full tree pre-formatted with
    // an indent prefix so the dropdown reads as a tree. JS filters
    // out self/descendants of the actual selection at click time.
    let move_choices: Vec<serde_json::Value> = all_pages
        .iter()
        .map(|p| {
            let indent = "—".repeat(p.depth.max(1) as usize - 1);
            let label = if indent.is_empty() {
                p.title.clone()
            } else {
                format!("{indent} {}", p.title)
            };
            serde_json::json!({
                "id": p.id.get().copied(),
                "label": label,
                "path": p.path.clone(),
            })
        })
        .collect();
    ctx.insert("move_choices", &move_choices);
    // #107 — plugin-contributed per-row buttons. Hand the raw button
    // templates over; the template substitutes `{page_id}` per row.
    ctx.insert(
        "plugin_page_listing_buttons",
        &crate::hooks::page_listing_buttons_raw(),
    );
    // #134 — plugin-contributed bulk actions targeting `cms_page`.
    let plugin_bulk_actions: Vec<serde_json::Value> = crate::hooks::bulk_actions_for("cms_page")
        .into_iter()
        .map(|a| {
            serde_json::json!({
                "key": a.key,
                "label": a.label,
                "icon": a.icon,
                "danger": a.danger,
                "confirm_message": a.confirm_message,
                "action_url": a.action_url,
            })
        })
        .collect();
    ctx.insert("plugin_bulk_actions", &plugin_bulk_actions);
    // #438 — active non-default locales for the copy-to-locale bulk
    // action (the default locale already holds canonical content).
    let copy_locales: Vec<Locale> = Locale::objects()
        .where_(Locale::active.eq(true))
        .order_by(&[("sort_order", false), ("code", false)])
        .fetch(tenant.pool())
        .await
        .unwrap_or_default()
        .into_iter()
        .filter(|l| !l.is_default)
        .collect();
    ctx.insert("copy_locales", &copy_locales);
    render_with_csrf(&state, &headers, "rcms_admin/page_list.html", &mut ctx)
}

/// Build a map of `page_id → public URL` for every row in `pages`.
/// Walks the list once in tree order (the list is already sorted by
/// `path` + `sort_order`), so each parent's URL is computed before
/// its children. Root pages with empty slug map to `/`.
/// Reassemble inline-panel rows from the posted form map.
///
/// Form keys are shaped `inline__<panel>__<row_idx>__<field>`. The
/// extractor iterates the spec's fields (so unknown / extraneous
/// keys are ignored), groups them by `row_idx`, and returns rows in
/// `row_idx` order so the handler can persist sort_order from the
/// position in the slice.
///
/// Rows are emitted whenever ANY of the spec's fields appears for
/// that index. The optional `inline__<panel>__<row_idx>____deleted`
/// sentinel hides a row entirely (so the editor can remove rows
/// without re-numbering everything).
fn extract_inline_panel_rows(
    form: &std::collections::HashMap<String, String>,
    spec: &crate::page_type::InlinePanelSpec,
) -> Vec<serde_json::Map<String, serde_json::Value>> {
    let mut by_idx: std::collections::BTreeMap<usize, serde_json::Map<String, serde_json::Value>> =
        std::collections::BTreeMap::new();
    let prefix = format!("inline__{}__", spec.name);
    for (key, value) in form {
        let Some(rest) = key.strip_prefix(&prefix) else {
            continue;
        };
        let Some((idx_str, field_name)) = rest.split_once("__") else {
            continue;
        };
        let Ok(idx) = idx_str.parse::<usize>() else {
            continue;
        };
        if field_name == "__deleted" && value == "1" {
            by_idx.remove(&idx);
            // Mark this index as tombstone — any later entries for
            // it get re-inserted into a fresh row that we then drop
            // at the end. Use a sentinel key to track.
            let mut row = serde_json::Map::new();
            row.insert("__deleted".to_owned(), serde_json::Value::Bool(true));
            by_idx.insert(idx, row);
            continue;
        }
        if !spec.fields.iter().any(|f| f.name == field_name) {
            continue;
        }
        let row = by_idx.entry(idx).or_default();
        if row.contains_key("__deleted") {
            continue;
        }
        row.insert(
            field_name.to_owned(),
            serde_json::Value::String(value.clone()),
        );
    }
    by_idx
        .into_iter()
        .filter_map(|(_, row)| {
            if row.contains_key("__deleted") {
                None
            } else {
                Some(row)
            }
        })
        .collect()
}

fn build_live_url_map(pages: &[Page]) -> std::collections::HashMap<String, String> {
    let mut by_id: std::collections::HashMap<String, String> = std::collections::HashMap::new();
    for p in pages {
        let Some(pid) = p.id.get().copied() else {
            continue;
        };
        let url = match p.parent_id {
            None => {
                if p.slug.is_empty() {
                    "/".to_owned()
                } else {
                    format!("/{}", p.slug)
                }
            }
            Some(parent_id) => {
                let parent_url = by_id
                    .get(&parent_id.to_string())
                    .cloned()
                    .unwrap_or_else(|| "/".to_owned());
                if p.slug.is_empty() {
                    parent_url
                } else if parent_url == "/" {
                    format!("/{}", p.slug)
                } else {
                    format!("{}/{}", parent_url, p.slug)
                }
            }
        };
        // Stringify the id key so Tera's `m[page.id]` lookup works
        // when the serializer flattens to JSON.
        by_id.insert(pid.to_string(), url);
    }
    by_id
}

/// GET /cms-admin/pages/new[?parent=<id>][&type=<page_type_id>] —
/// create flow. Two steps:
///
/// 1. Without `?type=`, render the page-type picker — a tile grid of
///    every creatable type (filtered to the parent's `allowed_child_
///    types` when `?parent=` is set). Picking a tile sends the
///    editor to step 2.
/// 2. With `?type=<id>`, render the page form with the chosen type
///    locked. The hidden `page_type_id` input carries it through to
///    the POST; the editor can NOT change it after this point.
///
/// Splitting the type selection out of the form is what makes the
/// "page-type is permanent" guarantee actually visible to authors —
/// once they're filling fields they're committed to the type.
#[derive(Debug, Deserialize)]
pub struct NewQuery {
    #[serde(default)]
    pub parent: Option<i64>,
    #[serde(default, rename = "type")]
    pub type_id: Option<i64>,
}

pub async fn page_new_form(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Query(q): Query<NewQuery>,
) -> Result<Response, AdminError> {
    let all_types: Vec<PageType> = PageType::objects().fetch(tenant.pool()).await?;

    let (parent, allowed_types) = match q.parent {
        Some(pid) => {
            let parent_row = Page::objects()
                .where_(Page::id.eq(pid))
                .first(tenant.pool())
                .await?
                .ok_or(AdminError::NotFound(pid))?;
            let allowed = allowed_children_for(&parent_row, &all_types);
            (Some(parent_row), allowed)
        }
        // Root: only types that may legally be roots (#614).
        None => (None, allowed_root_types(&all_types)),
    };

    // Step 1: no type picked yet → show the picker tile grid.
    let Some(type_id) = q.type_id else {
        // Enrich each PageType row with the registered handler's
        // `icon()` + `description()` so the picker can show them.
        // Plain `PageType` rows only carry table-level metadata
        // (type_name, verbose_name).
        #[derive(serde::Serialize)]
        struct PickerType<'a> {
            id: i64,
            type_name: &'a str,
            verbose_name: &'a str,
            icon: Option<&'static str>,
            description: Option<&'static str>,
        }
        let allowed_enriched: Vec<PickerType<'_>> = allowed_types
            .iter()
            .map(|t| {
                let handler = find_handler(&t.type_name);
                PickerType {
                    id: t.id.get().copied().unwrap_or_default(),
                    type_name: t.type_name.as_str(),
                    verbose_name: t.verbose_name.as_str(),
                    icon: handler.as_ref().and_then(|h| h.icon()),
                    description: handler.as_ref().and_then(|h| h.description()),
                }
            })
            .collect();

        let mut ctx = Context::new();
        add_chrome(&mut ctx, &tenant, "pages", session_user.as_ref()).await;
        add_admin_theme(&mut ctx, tenant.pool()).await;
        ctx.insert("parent", &parent);
        ctx.insert("allowed_types", &allowed_enriched);
        return render_with_csrf(
            &state,
            &headers,
            "rcms_admin/page_type_picker.html",
            &mut ctx,
        );
    };

    // Step 2: type picked → validate it sits in the allowed set for
    // this parent, then render the form with the type locked. Reject
    // out-of-set selections so a hand-crafted URL can't bypass the
    // parent's whitelist.
    let chosen_type = allowed_types
        .iter()
        .find(|t| t.id.get().copied() == Some(type_id))
        .cloned()
        .ok_or_else(|| {
            AdminError::Validation(format!(
                "page type {type_id} is not creatable under the chosen parent"
            ))
        })?;

    let mut page_seed = empty_page_form_values();
    if let Some(obj) = page_seed.as_object_mut() {
        obj.insert("page_type_id".to_owned(), serde_json::json!(type_id));
    }

    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "pages", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("mode", "new");
    ctx.insert("parent", &parent);
    ctx.insert("chosen_type", &chosen_type);
    ctx.insert("page", &page_seed);
    let review_required = !session_user.as_ref().is_some_and(|u| u.is_superuser)
        && crate::workflow::review_workflow_for_type(tenant.pool(), type_id).await?.is_some();
    ctx.insert("review_required", &review_required);
    // Custom tabs — only the existing-page editor
    // calls the handler for them. New-page mode shows the built-in
    // tabs only.
    ctx.insert("extra_tabs", &Vec::<crate::page_type::TabSpec>::new());
    // The type's own fields, at their defaults, so they can be filled
    // in before the first save (#857).
    let handler = find_handler(&chosen_type.type_name);
    let widgets = match &handler {
        Some(h) => h.widgets(tenant.pool(), 0).await.unwrap_or_default(),
        None => Vec::new(),
    };
    insert_type_fields(&mut ctx, &state.tera, tenant.pool(), handler.as_deref(), widgets, 0, type_id, None).await;
    // Stream-field picker options — every registered block surfaces
    // as an option even on the new-page form (so authors can drop
    // blocks before the first save).
    ctx.insert(
        "picker_options",
        &crate::block::admin::picker_options(&state.tera),
    );
    ctx.insert("page_tags", &Vec::<String>::new());
    ctx.insert("page_tags_csv", "");
    ctx.insert("all_tags", &Vec::<serde_json::Value>::new());
    insert_category_choices(&mut ctx, tenant.pool(), None).await;
    render_with_csrf(&state, &headers, "rcms_admin/page_form.html", &mut ctx)
}

/// Slugify a raw string for the `cms_page.slug` column. Mirrors the
/// JS-side slugifier in `page_form.html` so the auto-fill the
/// editor sees stays consistent with what the server would have
/// generated.
///
/// Lowercase ASCII, hyphens for whitespace, drop everything else,
/// collapse runs of hyphens, trim leading / trailing hyphens, cap
/// length at 60 chars.
pub(crate) fn slugify(raw: &str) -> String {
    let mut out = String::with_capacity(raw.len());
    let mut last_hyphen = true;
    for ch in raw.chars() {
        let c = ch.to_ascii_lowercase();
        if c.is_ascii_alphanumeric() {
            out.push(c);
            last_hyphen = false;
        } else if !last_hyphen {
            out.push('-');
            last_hyphen = true;
        }
    }
    while out.ends_with('-') {
        out.pop();
    }
    if out.len() > 60 {
        out.truncate(60);
        while out.ends_with('-') {
            out.pop();
        }
    }
    out
}

/// Pick a slug that doesn't collide with any sibling of
/// `parent_id`. If `candidate` is free, return it unchanged. If
/// taken, try `<base>-copy`, then `<base>-copy-2`, `<base>-copy-3`,
/// … up to a safety limit of 50 attempts (past which we surface an
/// error so the editor sees the collision).
///
/// Roots compare against other roots (`parent_id IS NULL`); children
/// compare against same-parent siblings only.
pub(crate) async fn dedup_slug_under_parent(
    pool: &rustango::sql::Pool,
    parent_id: Option<i64>,
    candidate: &str,
) -> Result<String, AdminError> {
    let taken: std::collections::HashSet<String> = {
        let q = Page::objects();
        let rows = match parent_id {
            Some(pid) => q.where_(Page::parent_id.eq(pid)).fetch(pool).await?,
            None => q.where_(Page::parent_id.is_null()).fetch(pool).await?,
        };
        rows.into_iter().map(|p| p.slug).collect()
    };

    if !taken.contains(candidate) {
        return Ok(candidate.to_owned());
    }
    let with_copy = format!("{candidate}-copy");
    if !taken.contains(&with_copy) {
        return Ok(with_copy);
    }
    for n in 2..50_u32 {
        let trial = format!("{candidate}-copy-{n}");
        if !taken.contains(&trial) {
            return Ok(trial);
        }
    }
    Err(AdminError::Validation(format!(
        "could not find a free slug for “{candidate}” after 50 -copy attempts"
    )))
}

/// Whether a root page already has the empty slug, i.e. serves "/".
async fn front_page_taken(pool: &rustango::sql::Pool) -> Result<bool, AdminError> {
    use rustango::sql::CounterPool as _;
    let n = Page::objects()
        .where_(Page::parent_id.is_null())
        .where_(Page::slug.eq(String::new()))
        .count(pool)
        .await?;
    Ok(n > 0)
}

/// The page editor's Categories field. A failed lookup leaves the
/// field out rather than failing the form.
async fn insert_category_choices(ctx: &mut tera::Context, pool: &rustango::sql::Pool, page_id: Option<i64>) {
    let (groups, checked) = match crate::category::assign::editor_groups(pool, page_id).await {
        Ok(v) => v,
        Err(e) => {
            tracing::warn!(error = %e, "category choices not loaded; the field is hidden");
            Default::default()
        }
    };
    ctx.insert("category_groups", &groups);
    ctx.insert("page_category_ids", &checked);
}

/// POST /cms-admin/pages/new — submit. Dispatches to
/// `Page::create_root` when no parent_id is in the form, otherwise
/// `Page::create_child`. Both paths compute path/depth/sort_order
/// and enforce the type whitelist.
pub async fn page_new_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Form(form_map): Form<std::collections::HashMap<String, String>>,
) -> Result<Response, AdminError> {
    // The raw map also carries the type's own fields (#857), saved once
    // the page exists.
    let form: PageForm = serde_urlencoded::from_str(
        &serde_urlencoded::to_string(&form_map).unwrap_or_default(),
    )
    .map_err(|e| AdminError::Validation(format!("page form: {e}")))?;
    // #614 — a type that names `allowed_parent_types` may only live under one
    // of those parents, so it can never be a root. The picker now hides such
    // types at the root, but `page_type_id` arrives in the form (and the
    // picker is reachable as `?type=<id>`), so re-check it here rather than
    // trusting the UI.
    if form.parent_id.is_none() {
        let types = PageType::objects().fetch(tenant.pool()).await?;
        if let Some(row) = types
            .iter()
            .find(|t| t.id.get().copied() == Some(form.page_type_id))
        {
            let parents = row.allowed_parents();
            if !parents.is_empty() {
                return Err(AdminError::Validation(format!(
                    "`{}` can't be a root page — it may only be created under: {}",
                    row.type_name,
                    parents.join(", ")
                )));
            }
        }
    }

    // An empty slug on a root page makes it the front page ("/"), as the
    // form's hint says — unless another root already is; otherwise the
    // slug comes from the title.
    let mut effective_slug = if !form.slug.trim().is_empty() {
        slugify(&form.slug)
    } else if form.parent_id.is_none() && !front_page_taken(tenant.pool()).await? {
        String::new()
    } else {
        slugify(&form.title)
    };
    if !effective_slug.is_empty() {
        effective_slug =
            dedup_slug_under_parent(tenant.pool(), form.parent_id, &effective_slug).await?;
    }

    // A new page that would go live takes the publish right (#761, as on
    // edit), and a type with a review workflow goes live only through
    // it. Decided before the insert and saved as a draft instead, so the
    // page is never live for a moment and nothing typed is lost.
    let intended = derive_scheduled_status(form.parsed_status().as_str(), form.parsed_go_live_at());
    let mut held_back: Option<String> = None;
    if let Some(user) = session_user.as_ref().filter(|u| !u.is_superuser) {
        if crate::page::PageStatus::str_goes_live(crate::page::PageStatus::Draft.as_str(), &intended) {
            let uid = user.id.get().copied().unwrap_or_default();
            let req = super::route_perms::Requirement {
                codename: "cms_page.publish".to_owned(),
                page: form.parent_id.map(|pid| (pid, crate::permissions::Action::Publish)),
            };
            if let Some(wf) = crate::workflow::review_workflow_for_type(tenant.pool(), form.page_type_id).await? {
                held_back = Some(super::i18n::tr(
                    &headers,
                    "Saved as a draft: pages of this type go live after review (“{workflow}”). Click Submit for review.",
                    &[("workflow", wf.name.as_str())],
                ));
            } else if !super::route_perms::allows_id(tenant.pool(), uid, false, &req).await {
                held_back = Some(super::i18n::tr(
                    &headers,
                    "Saved as a draft: you don't have permission to publish pages here.",
                    &[],
                ));
            }
        }
    }

    let new = NewPage::new(
        form.page_type_id,
        form.title.clone(),
        effective_slug.clone(),
    )
    .with_status(if held_back.is_some() { crate::page::PageStatus::Draft } else { form.parsed_status() })
    .with_seo(form.seo_title.clone(), form.seo_description.clone());

    let mut page = match form.parent_id {
        Some(pid) => {
            let parent = Page::objects()
                .where_(Page::id.eq(pid))
                .first(tenant.pool())
                .await?
                .ok_or(AdminError::NotFound(pid))?;
            Page::create_child(&tenant, &parent, new).await?
        }
        None => Page::create_root(&tenant, new).await?,
    };

    // Apply the non-tree-shape fields that NewPage doesn't carry
    // (robots, sitemap_priority) via a follow-up save.
    page.robots_index = form.parsed_robots_index();
    page.sitemap_priority = form.parsed_sitemap_priority();
    page.theme_id = form.theme_id.flatten();
    // A held-back page keeps no go-live date: a dated draft would be
    // scheduled, and go live without the review.
    page.go_live_at = if held_back.is_some() { None } else { form.parsed_go_live_at() };
    page.expire_at = form.parsed_expire_at();
    page.status = derive_scheduled_status(&page.status, page.go_live_at);
    page.show_in_menus = form.parsed_show_in_menus();
    page.preview_path = form.preview_path.trim().to_owned();
    page.template_override = form.template_override.trim().to_owned();
    // #183 — social sharing fields.
    page.og_title = form.og_title.clone();
    page.og_description = form.og_description.clone();
    page.og_image_media_id = form.og_image_media_id;
    if let Some(tc) = form.twitter_card.as_deref() {
        if matches!(tc, "summary" | "summary_large_image") {
            page.twitter_card = tc.to_owned();
        }
    }
    page.save_pool(tenant.pool()).await?;
    let new_id = page.id.get().copied().unwrap_or_default();
    // Tags and categories are on the new-page form too; they used to be
    // dropped here, and only an edit saved them.
    if let Some(raw) = form.tags.as_deref().filter(|t| !t.trim().is_empty()) {
        let names: Vec<String> = raw.split(',').map(str::to_owned).collect();
        crate::page_tag::replace_tags(tenant.pool(), new_id, &names).await?;
    }
    crate::category::assign::save_from_form(tenant.pool(), new_id, form.categories.as_deref()).await?;
    let page_type: Option<PageType> = PageType::objects()
        .where_(PageType::id.eq(page.page_type_id))
        .first(tenant.pool())
        .await?;
    save_type_content(tenant.pool(), page_type.as_ref(), page.page_type_id, new_id, &form_map).await?;

    // Capture revision #1 for this page so the history
    // panel has something to show from the moment a page is born.
    crate::revision::capture(tenant.pool(), &page, None)
        .await
        .log_warn("revision not captured; the history has a gap");
    reindex_from_form(tenant.pool(), &tenant.org.slug, new_id, &form_map).await;

    // Public render cache (rustango::cache_page::CachePageLayer)
    // expires on TTL; no manual invalidation needed here.
    let message = held_back.unwrap_or_else(|| {
        super::i18n::tr(&headers, "Created page “{title}”.", &[("title", page.title.as_str())])
    });
    redirect_after_page_save(
        &form.action,
        page.id.get().copied().unwrap_or_default(),
        page.parent_id,
        &message,
        &headers,
    )
}

/// GET /cms-admin/pages/{id}/edit — load and render the form.
/// Query string for the page-edit form. `locale=<code>` flips the
/// editor into translation mode for that locale; absent OR pointing
/// at the default locale shows the canonical edit form.
#[derive(Debug, Deserialize, Default)]
pub struct EditQuery {
    #[serde(default)]
    pub locale: Option<String>,
}

pub async fn page_edit_form(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
    Query(q): Query<EditQuery>,
) -> Result<Response, AdminError> {
    let page = Page::objects()
        .where_(Page::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;

    // #75 — alias proxy. Editing an alias means editing the source;
    // bounce the editor there so they're never tweaking a row that
    // can't actually accept content changes. Flash explains the
    // redirect so it isn't surprising.
    if let Some(source_id) = page.alias_of {
        return redirect_named_with_params_and_message_args(
            "rcms-admin:pages:edit",
            &[("id", source_id.to_string())],
            MsgLevel::Info,
            "Editing the source — alias “{title}” shares content with this row.",
            &[("title", page.title.as_str())],
            &headers,
        );
    }

    // Load all active locales for the picker + translation lookup.
    let locales: Vec<Locale> = Locale::objects()
        .where_(Locale::active.eq(true))
        .order_by(&[("is_default", false), ("code", false)])
        .fetch(tenant.pool())
        .await?;
    let editing_locale: Option<Locale> = match q.locale.as_deref() {
        Some(code) if !code.is_empty() => locales.iter().find(|l| l.code == code).cloned(),
        _ => None,
    };
    let translation_mode = editing_locale
        .as_ref()
        .map(|l| !l.is_default)
        .unwrap_or(false);
    let pending = crate::pending_change::for_page(tenant.pool(), id).await?;
    let existing_translations = if translation_mode {
        let lid = editing_locale
            .as_ref()
            .and_then(|l| l.id.get().copied())
            .unwrap_or(0);
        crate::translation::fetch_for(tenant.pool(), id, lid)
            .await
            .unwrap_or_default()
    } else {
        std::collections::HashMap::new()
    };

    // Theme picker dropdown options.
    let themes_for_picker: Vec<crate::theme::Theme> = crate::theme::Theme::objects()
        .order_by(&[("name", false)])
        .fetch(tenant.pool())
        .await?;

    // Page type is permanent — assigned once at create-time, never
    // reassigned. We surface the type as a read-only chip in the
    // form so authors can still see what they're editing; the edit
    // submit handler also strips `page_type_id` from its canonical
    // keys as a defense-in-depth measure.
    let chosen_type: PageType = PageType::objects()
        .where_(PageType::id.eq(page.page_type_id))
        .first(tenant.pool())
        .await?
        .ok_or_else(|| {
            AdminError::Validation(format!(
                "page_type_id {} on cms_page row has no cms_page_type row",
                page.page_type_id
            ))
        })?;

    // Public URL for the "View live" link in the edit form chrome.
    let ancestors = page.ancestors(tenant.pool()).await.unwrap_or_default();
    let mut segments: Vec<&str> = ancestors
        .iter()
        .filter(|a| !a.slug.is_empty())
        .map(|a| a.slug.as_str())
        .collect();
    if !page.slug.is_empty() {
        segments.push(page.slug.as_str());
    }
    let live_url = if segments.is_empty() {
        "/".to_owned()
    } else {
        format!("/{}", segments.join("/"))
    };

    // Fetch the revision history for the sidebar
    // panel. Sorted newest first, capped at 20 for the panel; older
    // revisions stay queryable via direct URL.
    //
    // The cap is applied in SQL (`.limit`), not after the fetch: every
    // `Revision` row carries a full JSON `snapshot` of the page, so
    // fetching the whole history to show 20 grew the edit-form cost with
    // every save (a page edited 300 times pulled 300 page snapshots).
    let revisions: Vec<crate::revision::Revision> = crate::revision::Revision::objects()
        .where_(crate::revision::Revision::page_id.eq(id))
        .order_by(&[("sequence", true)]) // desc
        .limit(20)
        .fetch(tenant.pool())
        .await?;

    // Sort existing translations into "core" (title / seo_title /
    // seo_description) and "extra" so the template can render the
    // core fields side-by-side with the canonical, and list the rest
    // as editable rows below.
    let core_fields = ["title", "seo_title", "seo_description"];
    let core_translations: std::collections::HashMap<String, String> = core_fields
        .iter()
        .map(|f| {
            (
                (*f).to_owned(),
                existing_translations.get(*f).cloned().unwrap_or_default(),
            )
        })
        .collect();
    let mut extra_translations: Vec<(String, String)> = existing_translations
        .iter()
        .filter(|(k, _)| !core_fields.contains(&k.as_str()))
        .map(|(k, v)| (k.clone(), v.clone()))
        .collect();
    extra_translations.sort_by(|a, b| a.0.cmp(&b.0));

    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "pages", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("mode", "edit");
    ctx.insert("parent", &Option::<Page>::None);
    ctx.insert("chosen_type", &chosen_type);
    ctx.insert("page", &page);
    ctx.insert("live_url", &live_url);
    // Headless preview: when the tenant has configured a decoupled
    // frontend, mint a token now and hand the editor a link that carries
    // it. The mint endpoint is inside the session-gated admin, so an
    // editor could otherwise only use preview by copying a token by hand
    // — which meant decoupled sites had no working preview at all.
    //
    // Minted per render rather than per click: the token is short-lived
    // and page-scoped, and generating it here keeps the link a plain
    // anchor instead of requiring JS. `None` (no frontend set) simply
    // hides the action.
    // The site-wide template, which the page may override. A lookup
    // failure is not fatal — it costs the preview action, not the editor.
    let site_preview_base = match crate::preview_url::configured_base(tenant.pool()).await {
        Ok(base) => base,
        Err(e) => {
            tracing::warn!(
                target: "rustango_cms::admin",
                error = %e,
                "headless preview setting lookup failed",
            );
            None
        }
    };
    // Minted only when there is somewhere to send it: a page with no
    // override on a tenant with no frontend has no preview URL at all.
    let headless_preview_url = if site_preview_base.is_some() || !page.preview_path.trim().is_empty()
    {
        let token = crate::preview_token::mint(
            &tenant.org.slug,
            id,
            chrono::Utc::now().timestamp() + PREVIEW_TOKEN_TTL_SECS,
        );
        crate::preview_url::for_page(
            site_preview_base.as_deref(),
            &page.preview_path,
            &token,
            id,
            &page.url_path,
        )
    } else {
        None
    };
    ctx.insert("headless_preview_url", &headless_preview_url);
    // #532 — full ancestor trail for the editor breadcrumb (each crumb links
    // to that level's children list); plus the immediate parent for "back".
    let ancestor_crumbs: Vec<serde_json::Value> = ancestors
        .iter()
        .map(|a| serde_json::json!({ "id": a.id.get().copied(), "title": a.title }))
        .collect();
    ctx.insert("ancestors", &ancestor_crumbs);
    ctx.insert(
        "parent_crumb",
        &ancestors
            .last()
            .map(|a| serde_json::json!({ "id": a.id.get().copied(), "title": a.title })),
    );
    // #107 — plugin-contributed kebab entries. Server-side substitutes the
    // page id into each entry's `href_template`.
    ctx.insert(
        "plugin_page_action_menu_items",
        &crate::hooks::page_action_menu_items(id),
    );
    // #140 — plugin-contributed page header buttons (next to Save).
    ctx.insert(
        "plugin_page_header_buttons",
        &crate::hooks::page_header_buttons(id),
    );
    // #115 — is the viewing user subscribed to publish events on
    // this page? Drives the Subscribe / Unsubscribe toggle on the
    // Status field.
    let is_subscribed = match session_user.as_ref().and_then(|u| u.id.get().copied()) {
        Some(uid) => crate::page_subscription::is_subscribed(tenant.pool(), id, uid)
            .await
            .unwrap_or(false),
        None => false,
    };
    ctx.insert("is_subscribed", &is_subscribed);
    // #189 — page tags.
    let page_tags: Vec<String> = crate::page_tag::tags_for_page(tenant.pool(), id)
        .await
        .unwrap_or_default()
        .into_iter()
        .map(|t| t.name)
        .collect();
    let page_tags_csv = page_tags.join(", ");
    ctx.insert("page_tags", &page_tags);
    ctx.insert("page_tags_csv", &page_tags_csv);
    let all_tags = crate::page_tag::all_tags(tenant.pool())
        .await
        .unwrap_or_default()
        .into_iter()
        .map(|(name, count)| serde_json::json!({ "name": name, "count": count }))
        .collect::<Vec<_>>();
    ctx.insert("all_tags", &all_tags);
    insert_category_choices(&mut ctx, tenant.pool(), Some(id)).await;
    // #146/#419 — inbound usage ("what links here"), aggregated per
    // referrer with resolved titles + a "used in N places" count.
    // Best-effort; failure leaves the panel empty.
    let usage =
        crate::reference_index::usage_summary(tenant.pool(), crate::reference_index::KIND_PAGE, id)
            .await;
    ctx.insert("usage", &usage);

    // #116 — accessibility heuristics. Run server-side on every
    // page editor open. `body_html` comes from the handler's
    // `load_extension`-rendered HTML when one is available; only the
    // canonical Page fields are checked, since extension shape is
    // per-handler.
    let a11y_violations = crate::a11y::check_page(&page, None);
    let (a11y_errors, a11y_warnings, a11y_infos) = crate::a11y::count_by_severity(&a11y_violations);
    ctx.insert("a11y_violations", &a11y_violations);
    ctx.insert("a11y_errors", &a11y_errors);
    ctx.insert("a11y_warnings", &a11y_warnings);
    ctx.insert("a11y_infos", &a11y_infos);
    // #417 — extensible content/SEO checks (registry-driven). Rendered
    // in a sibling "Content & SEO" panel; same Severity buckets as a11y.
    let mut content_findings = crate::content_checks::run_all(&page);
    // No share image chosen is fine when the page has a photo: the public
    // page uses it (see `meta_tags::fallback_share_image`).
    if content_findings.iter().any(|f| f.code == "missing_og_image")
        && crate::meta_tags::fallback_share_image(tenant.pool(), &page).await.is_some()
    {
        content_findings.retain(|f| f.code != "missing_og_image");
    }
    let (content_errors, content_warnings, content_infos) =
        crate::content_checks::count_by_severity(&content_findings);
    ctx.insert("content_findings", &content_findings);
    ctx.insert("content_errors", &content_errors);
    ctx.insert("content_warnings", &content_warnings);
    ctx.insert("content_infos", &content_infos);
    ctx.insert("revisions", &revisions);

    // #192 — audit-log timeline for this page. Capped at 100 entries
    // for the inline table; the full set is downloadable via the CSV
    // export endpoint linked alongside the table.
    let log_entries = crate::page_log::for_page(tenant.pool(), id, Some(100))
        .await
        .unwrap_or_default();
    let log_user_ids: std::collections::BTreeSet<i64> =
        log_entries.iter().filter_map(|e| e.actor_id).collect();
    let log_users: std::collections::HashMap<i64, String> = if log_user_ids.is_empty() {
        std::collections::HashMap::new()
    } else {
        rustango::tenancy::auth::User::objects()
            .where_(
                rustango::tenancy::auth::User::id
                    .is_in(log_user_ids.into_iter().collect::<Vec<_>>()),
            )
            .fetch(tenant.pool())
            .await
            .unwrap_or_default()
            .into_iter()
            .filter_map(|u| u.id.get().copied().map(|id| (id, u.username)))
            .collect()
    };
    let log_view: Vec<serde_json::Value> = log_entries
        .iter()
        .map(|e| {
            serde_json::json!({
                "id": e.id.get().copied(),
                "action": e.action,
                "actor_id": e.actor_id,
                "actor_username": e.actor_id.and_then(|uid| log_users.get(&uid).cloned()),
                "message": e.message,
                "created_at": e.created_at.get().copied(),
            })
        })
        .collect();
    ctx.insert("page_log", &log_view);

    ctx.insert("locales", &locales);
    ctx.insert("editing_locale", &editing_locale);
    ctx.insert("translation_mode", &translation_mode);
    ctx.insert("core_translations", &core_translations);
    ctx.insert("extra_translations", &extra_translations);
    ctx.insert("themes_for_picker", &themes_for_picker);

    // Extension fields contributed by the page-type handler — typed
    // body / hero / etc. The admin form renders one widget per entry
    // alongside the canonical fields so authors edit "Content" in
    // one place.
    // Resolve the registered handler for this page's type once —
    // both `extension_fields` and `display_fields` come off the
    // same instance.
    // Reuses `chosen_type` (fetched above for the read-only type chip)
    // instead of re-querying the identical `cms_page_type` row.
    let resolved_handler: Option<Box<dyn crate::page_type::PageTypeHandler>> =
        find_handler(&chosen_type.type_name);

    // Which preview surface the pane should render: the iframe, the JSON
    // body, or a plain "not available" notice.
    //
    // "No template" is not just an empty string — a non-empty name that
    // was never registered in Tera fails identically at render time, so
    // both cases have to count or the pane would lazy-load an iframe
    // that can only 500.
    let preview_mode = {
        let kind = crate::page_view::kind_for(&chosen_type);
        let template_renders = !chosen_type.default_template.trim().is_empty()
            && state
                .tera
                .get_template_names()
                .any(|n| n == chosen_type.default_template);
        if kind.serves_json() && !kind.serves_html() {
            "json"
        } else if template_renders {
            "html"
        } else {
            "none"
        }
    };
    ctx.insert("preview_mode", preview_mode);

    // Pull widgets via the rich-typed `widgets()` method, which by
    // default delegates to the legacy `extension_fields()` shape
    // for handlers that haven't migrated. Returns `Vec<Widget>`
    // directly so new handlers can use the full 26-kind catalog
    // (Date, Color, Radio, Checkboxes, MultiSelect, Custom, …).
    let raw_widgets: Vec<crate::widget::Widget> = match &resolved_handler {
        Some(h) => h.widgets(tenant.pool(), id).await.unwrap_or_default(),
        None => Vec::new(),
    };

    // #416 — content metrics (word count / reading time / readability)
    // from the content-bearing widget values, so the editor shows them
    // without opening the preview. Computed before the widgets are
    // mutated for rendering (chooser options etc.).
    let content_metrics =
        crate::metrics::analyze(&crate::metrics::collect_widget_text(&raw_widgets));
    ctx.insert("content_metrics", &content_metrics);

    // #i18n — per-leaf StreamField translation rows. In translation mode,
    // walk each Stream widget's canonical JSON, enumerate every
    // translatable text leaf (recursively, incl. nested blocks), group by
    // block instance, and prefill each from the existing per-locale
    // overrides. `raw_widgets` still holds canonical values here (it is
    // mutated for chooser-option rendering further below).
    if translation_mode {
        let mut stream_translations: Vec<serde_json::Value> = Vec::new();
        let mut leaf_paths: std::collections::HashSet<String> = std::collections::HashSet::new();
        for w in &raw_widgets {
            if !matches!(w.kind, crate::widget::WidgetKind::Stream) {
                continue;
            }
            let base: serde_json::Value = serde_json::from_str(&w.value)
                .unwrap_or_else(|_| serde_json::Value::Array(Vec::new()));
            let leaves = crate::block::translate::collect_translatable_leaves(&w.name, &base);
            if leaves.is_empty() {
                continue;
            }
            let mut groups: Vec<serde_json::Value> = Vec::new();
            let mut cur_id = String::new();
            for leaf in &leaves {
                leaf_paths.insert(leaf.path.clone());
                let row = serde_json::json!({
                    "path": leaf.path,
                    "widget_kind": leaf.widget_kind.as_tag(),
                    "field_label": leaf.field_label,
                    "canonical": leaf.canonical_text,
                    "canonical_html": translation_canonical_html(leaf.widget_kind.as_tag(), &leaf.canonical_text),
                    "value": existing_translations.get(&leaf.path).cloned().unwrap_or_default(),
                });
                if leaf.block_id != cur_id {
                    cur_id.clone_from(&leaf.block_id);
                    groups.push(serde_json::json!({
                        "block_type": leaf.block_type,
                        "block_label": leaf.block_label,
                        "block_id": leaf.block_id,
                        "depth": leaf.depth,
                        "leaves": [row],
                    }));
                } else if let Some(arr) = groups
                    .last_mut()
                    .and_then(|g| g.get_mut("leaves"))
                    .and_then(|v| v.as_array_mut())
                {
                    arr.push(row);
                }
            }
            stream_translations.push(serde_json::json!({ "field": w.name, "label": w.label, "groups": groups }));
        }
        // A code-made type's own text fields (a home page's heading, …).
        // Flat keys — the field's name — which the renderer overlays onto
        // `extension.<name>` (`translation::overlay_translations`). They
        // were translatable but never listed, so the form could not reach
        // them.
        let field_rows: Vec<serde_json::Value> = raw_widgets
            .iter()
            .filter(|w| {
                use crate::widget::WidgetKind as K;
                matches!(w.kind, K::Text | K::Textarea | K::Markdown | K::RichText)
                    && !w.name.starts_with(crate::page_builder::compile::FIELD_PREFIX)
            })
            .map(|w| {
                leaf_paths.insert(w.name.clone());
                serde_json::json!({
                    "path": w.name,
                    "widget_kind": w.kind.as_tag(),
                    "field_label": w.label,
                    "canonical": w.value,
                    "canonical_html": translation_canonical_html(w.kind.as_tag(), &w.value),
                    "value": existing_translations.get(&w.name).cloned().unwrap_or_default(),
                })
            })
            .collect();
        if !field_rows.is_empty() {
            stream_translations.insert(
                0,
                serde_json::json!({
                    "field": "fields",
                    "label": super::i18n::tr(&headers, "Fields", &[]),
                    "groups": [{
                        "block_type": "fields",
                        "block_label": "",
                        "block_id": "fields",
                        "depth": 0,
                        "leaves": field_rows,
                    }],
                }),
            );
        }
        // #567 — page-builder body leaves (fixed fields, group members,
        // repeater/flex zone blocks). Listed as one more translatable
        // "field" alongside the StreamField leaves, keyed `builder.<…>`.
        let (builder_groups, builder_paths) =
            builder_translation_groups(&tenant, id, page.page_type_id, &existing_translations)
                .await;
        if !builder_groups.is_empty() {
            leaf_paths.extend(builder_paths);
            stream_translations
                .push(serde_json::json!({
                    "field": "builder",
                    "label": super::i18n::tr(&headers, "Fields", &[]),
                    "groups": builder_groups,
                }));
        }
        // Don't also list dotted leaf paths under the raw "custom keys"
        // section (orphaned paths whose block was deleted still appear
        // there so editors can clear them).
        extra_translations.retain(|(k, _)| !leaf_paths.contains(k));
        ctx.insert("stream_translations", &stream_translations);
        ctx.insert("extra_translations", &extra_translations);
    }

    // Computed display chips for the "At a glance" panel above the
    // form. See `PageTypeHandler::display_fields` — handlers can
    // surface word counts / derived metadata / linked-snippet
    // previews without subclassing a panel.
    let display_fields: Vec<crate::page_type::DisplayField> = match &resolved_handler {
        Some(h) => h
            .display_fields(tenant.pool(), id)
            .await
            .unwrap_or_default(),
        None => Vec::new(),
    };
    ctx.insert("display_fields", &display_fields);

    // Handler-contributed tabs — appended to the built-in tab strip
    // (Content / Promote / Theme / Revisions). See
    // `PageTypeHandler::extra_tabs`.
    let mut raw_extra_tabs: Vec<crate::page_type::TabSpec> = match &resolved_handler {
        Some(h) => h.extra_tabs(tenant.pool(), id).await.unwrap_or_default(),
        None => Vec::new(),
    };

    // Role list — shared by the Permissions-tab grid below and the
    // Privacy tab's "allowed roles" picker further down. Fetched once:
    // both used to run the same full-table query independently.
    let all_roles: Vec<rustango::tenancy::permissions::Role> =
        rustango::tenancy::permissions::Role::objects()
            .order_by(&[("name", false)])
            .fetch(tenant.pool())
            .await
            .unwrap_or_default();

    // (#18) Permissions tab — every page gets one, regardless of
    // page type. Inject a TabSpec::template entry at the front of
    // the list so it sits right after Theme.
    {
        let grants: Vec<crate::permissions::PagePermission> =
            crate::permissions::PagePermission::objects()
                .where_(crate::permissions::PagePermission::page_id.eq(id))
                .fetch(tenant.pool())
                .await
                .unwrap_or_default();
        // (role_id, action) → granted?
        let granted: std::collections::HashSet<(i64, String)> = grants
            .iter()
            .map(|g| (g.role_id, g.permission.clone()))
            .collect();
        let actions: Vec<&str> = crate::permissions::Action::all()
            .iter()
            .map(|a| a.as_str())
            .collect();
        let perm_grid: Vec<serde_json::Value> = all_roles
            .iter()
            .map(|r| {
                let rid = r.id.get().copied().unwrap_or_default();
                let row_cells: Vec<serde_json::Value> = actions
                    .iter()
                    .map(|a| {
                        serde_json::json!({
                            "action": a,
                            "granted": granted.contains(&(rid, (*a).to_owned())),
                        })
                    })
                    .collect();
                serde_json::json!({
                    "role_id": rid,
                    "role_name": r.name,
                    "role_description": r.description,
                    "cells": row_cells,
                })
            })
            .collect();
        let perm_tab = crate::page_type::TabSpec::template(
            "permissions",
            "Permissions",
            "rcms_admin/_permissions_tab.html",
        )
        .with_icon("admin_panel_settings")
        .with_context_value(
            "perm_actions",
            serde_json::to_value(&actions).unwrap_or_default(),
        )
        .with_context_value(
            "perm_grid",
            serde_json::to_value(&perm_grid).unwrap_or_default(),
        )
        .with_context_value("perm_page_id", serde_json::Value::Number(id.into()));
        raw_extra_tabs.insert(0, perm_tab);
    }
    // (#20) Resolve each `TabSpec` into the flat `{name, label,
    // icon, badge, html}` shape the page_form.html template
    // expects. `Html` variants pass through; `Template` variants
    // render the named Tera template with the standard chrome
    // context + the spec's own context map merged in; `Widgets`
    // variants route through `_widget_group.html` so widget visuals
    // match the Content tab's main stack exactly.
    let extra_tabs: Vec<serde_json::Value> =
        render_extra_tabs(raw_extra_tabs, &state.tera, &ctx, &headers);
    ctx.insert("extra_tabs", &extra_tabs);

    // A change held for review shows in place of the live values.
    let pending_form = pending.as_ref().filter(|_| !translation_mode).map(crate::pending_change::PendingChange::form_map);
    insert_type_fields(
        &mut ctx,
        &state.tera,
        tenant.pool(),
        resolved_handler.as_deref(),
        raw_widgets,
        id,
        page.page_type_id,
        pending_form.as_ref(),
    )
    .await;

    // Picker options — every registered block surfaces as an option in
    // the block-picker dialog inside each stream editor. The dialog
    // filters client-side by `data-allowed` at open time.
    ctx.insert(
        "picker_options",
        &crate::block::admin::picker_options(&state.tera),
    );

    // #72 — editor lock. Best-effort claim on form open: if the page
    // is unlocked or already owned by us, we hold the lock and the
    // form renders normally. If someone else holds an unexpired
    // lock, render the banner with their username + force-unlock
    // affordance; the form itself stays editable (we'll reject the
    // POST unless the user force-unlocks first).
    let mut lock_banner: Option<serde_json::Value> = None;
    let mut lock_owned_by_me = false;
    let reviewing_elsewhere = match session_user.as_ref() {
        Some(u) => read_only_under_review(tenant.pool(), id, u).await?,
        None => false,
    };
    if let Some(viewer_id) = session_user
        .as_ref()
        .and_then(|u| u.id.get().copied())
        .filter(|_| !reviewing_elsewhere)
    {
        match crate::lock::acquire(tenant.pool(), id, viewer_id, false).await {
            Ok(crate::lock::AcquireOutcome::Acquired(_)) => {
                lock_owned_by_me = true;
            }
            Ok(crate::lock::AcquireOutcome::Held(other)) => {
                let holder_name = rustango::tenancy::auth::User::objects()
                    .where_(rustango::tenancy::auth::User::id.eq(other.user_id))
                    .fetch(tenant.pool())
                    .await
                    .ok()
                    .and_then(|v| v.into_iter().next())
                    .map(|u| u.username)
                    .unwrap_or_else(|| format!("user#{}", other.user_id));
                lock_banner = Some(serde_json::json!({
                    "holder_username": holder_name,
                    "holder_id": other.user_id,
                    "acquired_at": other.acquired_at.get().copied(),
                    "expires_at": other.expires_at,
                }));
            }
            Err(e) => {
                tracing::warn!(target: "rustango_cms::admin", page_id = id, error = %e, "lock acquire failed");
            }
        }
    }
    ctx.insert("lock_banner", &lock_banner);
    ctx.insert("lock_owned_by_me", &lock_owned_by_me);
    ctx.insert("lock_heartbeat_secs", &crate::lock::HEARTBEAT_INTERVAL_SECS);

    // #73 PR 2 — workflow context for the action bar + side panel.
    // Surface:
    //   * `workflow_slug` — the slug the page-type opted into (None if direct-publish)
    //   * `workflow` — the resolved Workflow row, if found + active
    //   * `workflow_state` — the active WorkflowState row, if one exists
    //   * `workflow_current_task` — task currently waiting for a decision (when in_progress)
    //   * `workflow_history` — [{task_name, status, decided_by_username, decided_at, comment}, …]
    //   * `workflow_viewer_can_decide` — true when viewer is in the current task's role (or superuser)
    let workflow_slug: Option<String> = chosen_type.workflow_name();
    let mut workflow_ctx: Option<serde_json::Value> = None;
    let mut workflow_state_ctx: Option<serde_json::Value> = None;
    let mut workflow_current_task_ctx: Option<serde_json::Value> = None;
    let mut workflow_history_ctx: Vec<serde_json::Value> = Vec::new();
    let mut workflow_viewer_can_decide = false;
    if let Some(slug) = workflow_slug.as_deref() {
        let workflow = crate::workflow::find_by_name(tenant.pool(), slug)
            .await
            .unwrap_or(None);
        if let Some(wf) = workflow {
            let wf_id = wf.id.get().copied().unwrap_or_default();
            let tasks = crate::workflow::tasks_for(tenant.pool(), wf_id)
                .await
                .unwrap_or_default();
            workflow_ctx = Some(serde_json::json!({
                "id": wf_id,
                "name": wf.name,
                "description": wf.description,
                "task_count": tasks.len(),
            }));
            // The latest round, whatever its outcome: an approved page says
            // so (and can go through review again), not what an earlier
            // round ended with.
            if let Some(state_row) = crate::workflow::latest_state_for_page(tenant.pool(), id)
                .await
                .unwrap_or(None)
            {
                let state_id = state_row.id.get().copied().unwrap_or_default();
                // Current task (when in_progress).
                if let Some(current) = state_row
                    .current_task_id
                    .and_then(|tid| tasks.iter().find(|t| t.id.get().copied() == Some(tid)))
                {
                    workflow_current_task_ctx = Some(serde_json::json!({
                        "id": current.id.get().copied(),
                        "name": current.name,
                        "role_id": current.role_id,
                    }));
                    if let Some(viewer) = session_user.as_ref() {
                        let viewer_id = viewer.id.get().copied().unwrap_or_default();
                        workflow_viewer_can_decide = viewer.is_superuser
                            || user_is_in_role(tenant.pool(), viewer_id, current.role_id)
                                .await
                                .unwrap_or(false);
                    }
                }
                workflow_state_ctx = Some(serde_json::json!({
                    "id": state_id,
                    "status": state_row.status,
                    "requested_by": state_row.requested_by,
                    "requested_at": state_row.requested_at.get().copied(),
                    "finished_at": state_row.finished_at,
                }));
                // Decision history — flatten task name + reviewer username.
                if let Ok(history) = crate::workflow::history_for_page(tenant.pool(), id).await {
                    // Pre-resolve user ids to usernames in one batch.
                    let reviewer_ids: Vec<i64> =
                        history.iter().filter_map(|h| h.decided_by).collect();
                    let reviewers: std::collections::HashMap<i64, String> =
                        if reviewer_ids.is_empty() {
                            std::collections::HashMap::new()
                        } else {
                            rustango::tenancy::auth::User::objects()
                                .where_(rustango::tenancy::auth::User::id.is_in(reviewer_ids))
                                .fetch(tenant.pool())
                                .await
                                .unwrap_or_default()
                                .into_iter()
                                .filter_map(|u| u.id.get().copied().map(|uid| (uid, u.username)))
                                .collect()
                        };
                    let task_name_by_id: std::collections::HashMap<i64, String> = tasks
                        .iter()
                        .filter_map(|t| t.id.get().copied().map(|tid| (tid, t.name.clone())))
                        .collect();
                    workflow_history_ctx = history
                        .iter()
                        .map(|h| {
                            serde_json::json!({
                                "task_name": task_name_by_id
                                    .get(&h.task_id)
                                    .cloned()
                                    .unwrap_or_else(|| format!("task #{}", h.task_id)),
                                "status": h.status,
                                "comment": h.comment,
                                "decided_by_username": h
                                    .decided_by
                                    .and_then(|uid| reviewers.get(&uid).cloned()),
                                "decided_at": h.decided_at,
                            })
                        })
                        .collect();
                }
            }
        }
    }
    // #73 PR 3 — workflow-driven lock. When the page is in_progress
    // AND the viewer is not authorized for the current task (and not
    // a superuser), the form is read-only — the page belongs to
    // whoever is reviewing it right now. Submitter waits for the
    // verdict; non-reviewers can't sneak edits in mid-review.
    let workflow_locked = workflow_state_ctx
        .as_ref()
        .and_then(|s| s.get("status"))
        .and_then(|v| v.as_str())
        .map(|s| s == crate::workflow::WorkflowStatus::InProgress.as_str())
        .unwrap_or(false)
        && !workflow_viewer_can_decide;
    ctx.insert("workflow_slug", &workflow_slug);
    ctx.insert("workflow", &workflow_ctx);
    ctx.insert("workflow_state", &workflow_state_ctx);
    ctx.insert("workflow_current_task", &workflow_current_task_ctx);
    ctx.insert("workflow_history", &workflow_history_ctx);
    ctx.insert("workflow_viewer_can_decide", &workflow_viewer_can_decide);
    ctx.insert("workflow_locked", &workflow_locked);
    let review_required = !session_user.as_ref().is_some_and(|u| u.is_superuser)
        && crate::workflow::review_workflow_for_type(tenant.pool(), page.page_type_id).await?.is_some();
    ctx.insert("review_required", &review_required);
    if let (Some(held), Some(form)) = (pending.as_ref(), pending_form.as_ref()) {
        ctx.insert("page", &overlay_pending_page(&page, form));
        let by = match held.created_by {
            Some(uid) => rustango::tenancy::auth::User::objects()
                .where_(rustango::tenancy::auth::User::id.eq(uid))
                .first(tenant.pool())
                .await?
                .map(|u| u.username),
            None => None,
        };
        ctx.insert(
            "pending_change",
            &serde_json::json!({ "by": by, "updated_at": held.updated_at.get().copied() }),
        );
    }

    // #76 — view-restriction context for the Privacy widget.
    let view_restriction = crate::view_restriction::direct_for_page(tenant.pool(), id)
        .await
        .unwrap_or(None);
    let restriction_kind = view_restriction
        .as_ref()
        .map(|r| r.kind.clone())
        .unwrap_or_else(|| "none".to_owned());
    let restriction_groups = view_restriction
        .as_ref()
        .map(|r| r.parsed_groups())
        .unwrap_or_default();
    let restriction_groups_str = restriction_groups
        .iter()
        .map(i64::to_string)
        .collect::<Vec<_>>()
        .join(",");
    // For the role picker dropdown — reuses the `all_roles` list already
    // fetched once above for the Permissions tab.
    let role_choices: Vec<serde_json::Value> = all_roles
        .iter()
        .map(|r| {
            serde_json::json!({
                "id": r.id.get().copied(),
                "name": r.name,
                "checked": r.id.get().copied().is_some_and(|rid| restriction_groups.contains(&rid)),
            })
        })
        .collect();
    // #members — permission-codename picker (the permission-engine gate).
    let restriction_codenames = view_restriction
        .as_ref()
        .map(|r| r.parsed_codenames())
        .unwrap_or_default();
    let restriction_codenames_str = restriction_codenames.join(",");
    let codename_choices: Vec<serde_json::Value> = super::resources::membership_codenames()
        .into_iter()
        .map(|(codename, label)| {
            serde_json::json!({
                "codename": codename,
                "label": label,
                "checked": restriction_codenames.contains(&codename),
            })
        })
        .collect();
    ctx.insert("restriction_kind", &restriction_kind);
    ctx.insert("restriction_groups_str", &restriction_groups_str);
    ctx.insert("restriction_role_choices", &role_choices);
    ctx.insert("restriction_codenames_str", &restriction_codenames_str);
    ctx.insert("restriction_codename_choices", &codename_choices);
    ctx.insert(
        "restriction_has_password",
        &view_restriction
            .as_ref()
            .map(|r| !r.password_hash.is_empty())
            .unwrap_or(false),
    );

    // #81 PR 1 — comments side panel. Fetch every thread on this
    // page + every reply in one batched query, group per thread,
    // resolve author usernames, and surface as JSON for the panel
    // template. Threads with `resolved_at` set render in a
    // collapsed "Resolved" bucket client-side.
    let threads = crate::comment::threads_for_page(tenant.pool(), id)
        .await
        .unwrap_or_default();
    let thread_ids: Vec<i64> = threads.iter().filter_map(|c| c.id.get().copied()).collect();
    let replies_by_thread = crate::comment::replies_for_threads(tenant.pool(), &thread_ids)
        .await
        .unwrap_or_default();
    let mut comment_user_ids: std::collections::BTreeSet<i64> =
        threads.iter().map(|c| c.author_id).collect();
    for replies in replies_by_thread.values() {
        for r in replies {
            comment_user_ids.insert(r.author_id);
        }
    }
    let comment_users: std::collections::HashMap<i64, String> = if comment_user_ids.is_empty() {
        std::collections::HashMap::new()
    } else {
        rustango::tenancy::auth::User::objects()
            .where_(rustango::tenancy::auth::User::id.is_in(comment_user_ids.iter().copied()))
            .fetch(tenant.pool())
            .await
            .unwrap_or_default()
            .into_iter()
            .filter_map(|u| u.id.get().copied().map(|uid| (uid, u.username)))
            .collect()
    };
    let comment_threads: Vec<serde_json::Value> = threads
        .iter()
        .map(|c| {
            let tid = c.id.get().copied().unwrap_or_default();
            let replies = replies_by_thread.get(&tid).cloned().unwrap_or_default();
            let rendered_replies: Vec<serde_json::Value> = replies
                .iter()
                .map(|r| {
                    serde_json::json!({
                        "id": r.id.get().copied(),
                        "author_username": comment_users.get(&r.author_id).cloned(),
                        "body": r.body,
                        "created_at": r.created_at.get().copied(),
                    })
                })
                .collect();
            serde_json::json!({
                "id": tid,
                "field_path": c.field_path,
                "author_username": comment_users.get(&c.author_id).cloned(),
                "body": c.body,
                "created_at": c.created_at.get().copied(),
                "resolved_at": c.resolved_at,
                "replies": rendered_replies,
                "reply_count": replies.len(),
            })
        })
        .collect();
    ctx.insert("comment_threads", &comment_threads);
    ctx.insert(
        "comment_open_count",
        &comment_threads
            .iter()
            .filter(|t| t.get("resolved_at").map(|v| v.is_null()).unwrap_or(true))
            .count(),
    );
    ctx.insert(
        "comment_resolved_count",
        &comment_threads
            .iter()
            .filter(|t| t.get("resolved_at").map(|v| !v.is_null()).unwrap_or(false))
            .count(),
    );
    // For the field-picker dropdown when adding a new comment.
    ctx.insert(
        "comment_field_choices",
        &serde_json::json!([
            {"value": "title", "label": "Title"},
            {"value": "slug", "label": "Slug"},
            {"value": "seo_title", "label": "SEO title"},
            {"value": "seo_description", "label": "SEO description"},
            {"value": "body", "label": "Body / stream content"},
            {"value": "general", "label": "General — whole page"},
        ]),
    );

    render_with_csrf(&state, &headers, "rcms_admin/page_form.html", &mut ctx)
}

/// POST /cms-admin/pages/{id}/edit — apply edits.
/// POST /cms-admin/pages/{id}/autosave — silent revision capture.
/// Takes the same form payload as
/// `page_edit_submit` but:
///   - Skips status-promotion logic (draft → published doesn't fire).
///   - Captures a revision row tagged `autosave: true` so the
///     timeline distinguishes autosaves from manual saves.
///   - Does NOT cascade slug / url_path recomputation, redirects,
///     workflow events, cache invalidation, or page_subscription
///     notifications — autosaves are scratch state, not publish
///     events.
///   - Returns 204 No Content so the editor JS treats success as a
///     quiet acknowledgement (no flash banner).
pub async fn page_autosave_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
    Form(form_map): Form<std::collections::HashMap<String, String>>,
) -> Result<Response, AdminError> {
    use rustango::sql::FetcherPool as _;
    let mut page = Page::objects()
        .where_(Page::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    // Lock-respect — same as the regular save path. Refuse the
    // autosave when someone else holds the lock; the editor JS
    // surfaces this by suspending future autosaves until the user
    // reloads.
    if let Some(viewer_id) = session_user.as_ref().and_then(|u| u.id.get().copied()) {
        if let Ok(Some(lock)) = crate::lock::current(tenant.pool(), id).await {
            if lock.user_id != viewer_id {
                return Ok((axum::http::StatusCode::CONFLICT, "locked").into_response());
            }
        }
    }
    // Autosave writes the page row itself, so it only touches drafts:
    // on a live (or scheduled) page it would publish half-typed edits,
    // skipping the publish right and any review. The real Save handles
    // those pages; here nothing is written.
    if page.status != crate::page::PageStatus::Draft.as_str() {
        return Ok((axum::http::StatusCode::NO_CONTENT, "").into_response());
    }
    if let Some(u) = session_user.as_ref() {
        if read_only_under_review(tenant.pool(), id, u).await? {
            return Ok((axum::http::StatusCode::CONFLICT, "under review").into_response());
        }
    }
    // Overlay mutable fields. Tree placement (parent / path /
    // url_path / depth / sort_order) intentionally left alone —
    // autosaves never move the page. That includes the slug: writing it
    // here without url_path made the next real save see "no slug change",
    // so the rename never cascaded to url_path, children or a 301 (#679).
    // The edited slug stays in the form and the real save applies it.
    if let Some(v) = form_map.get("title") {
        page.title = v.clone();
    }
    if let Some(v) = form_map.get("seo_title") {
        page.seo_title = v.clone();
    }
    if let Some(v) = form_map.get("seo_description") {
        page.seo_description = v.clone();
    }
    // Skip status promotion. If the editor changed status, we
    // record it for the diff but don't fire any publish-side
    // effects.
    let posted_status = form_map.get("status").cloned();
    page.save_pool(tenant.pool()).await?;
    // Persist extension fields via handler (idempotent upserts).
    let pts: Vec<PageType> = PageType::objects()
        .where_(PageType::id.eq(page.page_type_id))
        .fetch(tenant.pool())
        .await?;
    if let Some(pt) = pts.into_iter().next() {
        if let Some(handler) = find_handler(&pt.type_name) {
            let ext_form =
                sanitized_extension_form(handler.as_ref(), tenant.pool(), id, &form_map).await;
            handler.save_extension(tenant.pool(), id, &ext_form).await?;
        }
    }
    // #564 — page-builder body values (additive to any code handler).
    save_page_builder_values(tenant.pool(), page.page_type_id, id, &form_map).await?;
    // Capture an autosave-flagged revision. Failure here is
    // best-effort.
    let by = session_user.as_ref().and_then(|u| u.id.get().copied());
    if let Ok(mut rev) = crate::revision::capture(tenant.pool(), &page, by).await {
        // Inject the autosave marker into the snapshot before
        // re-saving the row. The snapshot column is a plain
        // serde_json::Value so we can mutate + re-persist.
        if let serde_json::Value::Object(ref mut map) = rev.snapshot {
            map.insert("__autosave".to_owned(), serde_json::Value::Bool(true));
            if let Some(status) = posted_status {
                map.insert(
                    "__autosave_posted_status".to_owned(),
                    serde_json::Value::String(status),
                );
            }
            rev.save_pool(tenant.pool()).await.log_warn("autosave revision not stored");
        }
    }
    Ok((axum::http::StatusCode::NO_CONTENT, "").into_response())
}

/// Allow-list of POST keys that `page_edit_submit` re-feeds into
/// [`PageForm`]. The submit handler filters the incoming form map
/// through this list to drop extension-field entries the page type
/// handler owns. Every field [`PageForm`] consumes — and every field
/// the body of `page_edit_submit` reads off the parsed form — MUST
/// be listed here, or the value is silently dropped on save.
///
/// `page_type_id` is intentionally absent: type is assigned at
/// create-time and is permanent (see comment in the handler).
/// Page fields the editor shows as text, overlaid from a held change.
const PENDING_TEXT_KEYS: &[&str] = &[
    "title",
    "slug",
    "seo_title",
    "seo_description",
    "og_title",
    "og_description",
    "preview_path",
];

/// `page` as the editor shows it with a held change on top: its text
/// fields take the proposed values.
fn overlay_pending_page(
    page: &Page,
    form: &std::collections::HashMap<String, String>,
) -> serde_json::Value {
    let mut value = serde_json::to_value(page).unwrap_or_default();
    if let Some(obj) = value.as_object_mut() {
        for key in PENDING_TEXT_KEYS {
            if let Some(v) = form.get(*key) {
                obj.insert((*key).to_owned(), serde_json::Value::String(v.clone()));
            }
        }
    }
    value
}

/// Give each widget its value from a held change. An unticked checkbox
/// isn't posted, so a Boolean the form lacks is off.
fn overlay_pending_widgets(
    widgets: &mut [crate::widget::Widget],
    form: &std::collections::HashMap<String, String>,
) {
    for w in widgets {
        match form.get(&w.name) {
            Some(v) => w.value.clone_from(v),
            None if matches!(w.kind, crate::widget::WidgetKind::Boolean) => w.value.clear(),
            None => {}
        }
    }
}

const PAGE_FORM_CANONICAL_KEYS: &[&str] = &[
    "title",
    "slug",
    "status",
    "seo_title",
    "seo_description",
    "robots_index",
    "sitemap_priority",
    "parent_id",
    "theme_id",
    // #207 — schedule + auto-archive.
    "go_live_at",
    "expire_at",
    // #22 — surface in navigation editor.
    "show_in_menus",
    // Headless preview — per-page frontend route override.
    "preview_path",
    // Per-page template, overriding the page type's.
    "template_override",
    // #183 — social sharing.
    "og_title",
    "og_description",
    "og_image_media_id",
    "twitter_card",
    // #189 — page tags (comma-separated).
    "tags",
];

/// The acting user for [`apply_page_edit`] — carries exactly what the
/// lock / workflow / attribution logic needs, decoupled from the HTTP
/// session type so the MCP tools can act as a key's owner.
pub(crate) struct PageEditActor {
    pub id: i64,
    pub username: String,
    pub is_superuser: bool,
}

/// A save refused for a *policy* reason (not an error): the caller
/// renders it in its own shape — the HTTP handler as a flash+redirect,
/// an MCP tool as a structured tool error.
pub(crate) enum PageEditRefusal {
    /// Page is mid-review on a workflow step the actor can't edit.
    WorkflowReview { task_name: String },
    /// Another editor holds the edit lock.
    LockedBy { holder: String },
    /// Archived pages can't be unpublished in place.
    ArchivedUnpublish,
    /// The save would put the page live and the actor lacks the publish
    /// right.
    PublishDenied,
    /// The save would put the page live, but pages of its type go live
    /// through the named review workflow.
    ReviewRequired { workflow: String },
}

/// What a successful [`apply_page_edit`] produced, for caller messaging.
pub(crate) struct PageEditOutcome {
    pub page: Page,
    pub redirects_created: usize,
    pub reapproval_kicked: bool,
    /// The save was kept for review; the live page is unchanged.
    pub held_for_review: bool,
}

/// Apply one page edit end-to-end from a canonical+extension form map —
/// the single write path behind both `page_edit_submit` (HTTP) and the
/// MCP `update_page`/`publish_page` tools. Covers: workflow + editor
/// lock policy, the canonical-key overlay (`PAGE_FORM_CANONICAL_KEYS`),
/// scheduled-status derivation + publish timestamps, the atomic tree
/// save with url-path cascade + on-commit cache purges, tags, revision
/// capture (attributed to `actor`), reference reindex, search sync,
/// publish hooks + page log, extension + inline-panel + page-builder
/// value persistence, auto-301s on slug moves, redirect auto-disable on
/// unpublish, and reapproval-on-edit. `notify = false` skips the
/// publish subscription mail (MCP v1). Flash/redirect rendering stays
/// with the HTTP handler.
#[allow(clippy::too_many_arguments)]
pub(crate) async fn apply_page_edit(
    pool: &rustango::sql::Pool,
    org_slug: &str,
    invalidator: &std::sync::Arc<dyn crate::cache_invalidate::PageCacheInvalidator>,
    mailer: Option<&dyn rustango::email::Mailer>,
    mailer_from: &str,
    notify: bool,
    id: i64,
    form_map: &std::collections::HashMap<String, String>,
    actor: Option<&PageEditActor>,
) -> Result<Result<PageEditOutcome, PageEditRefusal>, AdminError> {
    // #73 PR 3 — workflow lock. While the page is in_progress on a
    // workflow, only members of the current task's role (and
    // superusers) can save edits. Submitter waits for the verdict;
    // non-reviewers can't sneak fixes in mid-review. Checked BEFORE
    // the editor lock — workflow restrictions outrank concurrency.
    if let Some(actor) = actor {
        if let Some(active) = crate::workflow::active_state_for_page(pool, id).await? {
            if active.status == crate::workflow::WorkflowStatus::InProgress.as_str()
                && !actor.is_superuser
            {
                let tasks = crate::workflow::tasks_for(pool, active.workflow_id).await?;
                if let Some(current_task) = active
                    .current_task_id
                    .and_then(|tid| tasks.iter().find(|t| t.id.get().copied() == Some(tid)))
                {
                    if !user_is_in_role(pool, actor.id, current_task.role_id).await? {
                        return Ok(Err(PageEditRefusal::WorkflowReview {
                            task_name: current_task.name.clone(),
                        }));
                    }
                }
            }
        }
    }

    // #72 — refuse the write if the page is locked by someone else.
    // The GET path acquires the lock for the viewer; if they got
    // through, this is a no-op. If a second editor saves anyway
    // (race or curl), they hit a 409-flavored redirect with a flash
    // explaining who holds the lock. Force-unlock is a separate
    // POST so accidental clobbers stay impossible.
    if let Some(actor) = actor {
        match crate::lock::current(pool, id).await {
            Ok(Some(lock)) if lock.user_id != actor.id => {
                let holder = rustango::tenancy::auth::User::objects()
                    .where_(rustango::tenancy::auth::User::id.eq(lock.user_id))
                    .fetch(pool)
                    .await
                    .ok()
                    .and_then(|v| v.into_iter().next())
                    .map(|u| u.username)
                    .unwrap_or_else(|| format!("user#{}", lock.user_id));
                return Ok(Err(PageEditRefusal::LockedBy { holder }));
            }
            _ => {}
        }
    }

    // Re-deserialize as PageForm using only the canonical keys. The
    // form map also carries extension-field entries (typed body /
    // hero / etc.) the type's handler is interested in; we hand that
    // raw HashMap to `save_extension` after the canonical save.
    //
    // `page_type_id` is *not* in this list — page type is assigned at
    // create-time and is permanent. Any value posted is ignored. The
    // template hides the selector and the handler defensively drops
    // the key here. Caller scenarios that *must* change a page's
    // type can delete + recreate (preserves slug history via
    // `cms_redirect`).
    let canonical_keys = PAGE_FORM_CANONICAL_KEYS;
    let mut canonical_only: std::collections::HashMap<&str, String> = form_map
        .iter()
        .filter(|(k, _)| canonical_keys.contains(&k.as_str()))
        .map(|(k, v)| (k.as_str(), v.clone()))
        .collect();

    let mut page = Page::objects()
        .where_(Page::id.eq(id))
        .first(pool)
        .await?
        .ok_or(AdminError::NotFound(id))?;

    // Re-inject the immutable `page_type_id` from the persisted row
    // so `PageForm`'s deserializer is satisfied without trusting
    // anything the client posted.
    let frozen_type_id = page.page_type_id.to_string();
    canonical_only.insert("page_type_id", frozen_type_id);
    let canonical_view: std::collections::HashMap<&str, &str> = canonical_only
        .iter()
        .map(|(k, v)| (*k, v.as_str()))
        .collect();
    let form: PageForm = serde_urlencoded::from_str(
        &serde_urlencoded::to_string(&canonical_view).unwrap_or_default(),
    )
    .map_err(|e| AdminError::Validation(format!("canonical form: {e}")))?;

    // A live page of a type whose review workflow asks for re-approval
    // on edit: a non-superuser's save waits for that review instead of
    // changing what visitors see. The final approval applies it (a save
    // with no actor, so it isn't held again).
    if let Some(actor) = actor.filter(|a| !a.is_superuser) {
        if page.status == crate::page::PageStatus::Published.as_str() {
            if let Some(wf) = crate::workflow::review_workflow_for_type(pool, page.page_type_id)
                .await?
                .filter(|wf| wf.require_reapproval_on_edit)
            {
                crate::pending_change::hold(pool, id, form_map, Some(actor.id)).await?;
                let mut started = false;
                if crate::workflow::active_state_for_page(pool, id).await?.is_none() {
                    let tasks =
                        crate::workflow::tasks_for(pool, wf.id.get().copied().unwrap_or_default())
                            .await?;
                    crate::workflow::submit_for_review(pool, id, &wf, &tasks, actor.id).await?;
                    started = true;
                }
                return Ok(Ok(PageEditOutcome {
                    page,
                    redirects_created: 0,
                    reapproval_kicked: started,
                    held_for_review: true,
                }));
            }
        }
    }

    page.title = form.title.clone();
    // #703 — the same normalisation a new page gets, and no two siblings
    // on one slug: they would share a URL and serve whichever row the
    // driver returned. The pattern attribute alone is bypassed by any
    // direct POST (and by MCP update_page, which lands here).
    let new_slug = if form.slug.trim().is_empty() {
        String::new()
    } else {
        slugify(&form.slug)
    };
    let slug_changed = page.slug != new_slug;
    if slug_changed && !new_slug.is_empty() {
        let sibling = Page::objects()
            .where_(Page::slug.eq(new_slug.clone()))
            .where_(Page::id.ne(id));
        let sibling = match page.parent_id {
            Some(pid) => sibling.where_(Page::parent_id.eq(pid)),
            None => sibling.where_(Page::parent_id.is_null()),
        };
        if sibling.first(pool).await?.is_some() {
            return Err(AdminError::Validation(format!(
                "another page under the same parent already uses the slug “{new_slug}”"
            )));
        }
    }
    // Capture the OLD url_path BEFORE the recomputation block below
    // overwrites it. Used after commit to evict the now-stale cache
    // entry under the page's previous URL.
    let old_url_path = page.url_path.clone();
    page.slug = new_slug;
    // #553 — remember whether this save takes the page out of
    // Published, so linked redirect rules can be auto-disabled below.
    let was_published = page.status == crate::page::PageStatus::Published.as_str();
    // #556 — remember whether it was archived, to block an unpublish
    // (archived → draft) below. Archived → published is the unarchive
    // path and stays allowed.
    let was_archived = page.status == crate::page::PageStatus::Archived.as_str();
    let old_status = page.status.clone();
    // page.page_type_id stays as-is — see comment on `canonical_keys`.
    page.status = form.parsed_status().as_str().to_owned();
    page.seo_title = form.seo_title.clone();
    page.seo_description = form.seo_description.clone();
    page.robots_index = form.parsed_robots_index();
    page.sitemap_priority = form.parsed_sitemap_priority();
    // The editor has no theme input: keep the stored theme unless the
    // request carries the field (#902).
    if let Some(theme_id) = form.theme_id {
        page.theme_id = theme_id;
    }
    // #207 — clear the pre-publish reminder flag when go_live_at
    // moves so a fresh schedule horizon gets a fresh reminder.
    let new_go_live = form.parsed_go_live_at();
    if page.go_live_at != new_go_live {
        page.notification_pre_published_sent = false;
    }
    page.go_live_at = new_go_live;
    page.expire_at = form.parsed_expire_at();
    // #264 — derive scheduled state from go_live_at AFTER the form's
    // chosen status has been applied above, so picking "Draft" +
    // setting a future go-live still parks the row in the sweep's
    // pickup queue.
    page.status = derive_scheduled_status(&page.status, page.go_live_at);
    page.show_in_menus = form.parsed_show_in_menus();
    page.preview_path = form.preview_path.trim().to_owned();
    page.template_override = form.template_override.trim().to_owned();
    // #251 — first/last publish timestamps. `published_at` is set
    // once on the first published transition (still the
    // chronological-ordering anchor). `last_published_at` bumps on
    // every save while status=published — sitemap / cache use it
    // for freshness.
    if page.status == crate::page::PageStatus::Published.as_str() {
        let now = chrono::Utc::now();
        if page.published_at.is_none() {
            page.published_at = Some(now);
        }
        page.last_published_at = Some(now);
    }
    // #556 — an archived page can't be unpublished to Draft in place;
    // it must be revived (→ Published) or explicitly unarchived first.
    // This runs before any DB mutation below.
    if was_archived && page.status == crate::page::PageStatus::Draft.as_str() {
        return Ok(Err(PageEditRefusal::ArchivedUnpublish));
    }
    // #761 — judged on the status this save actually lands on, after
    // `derive_scheduled_status`, so neither `archived` nor Draft plus a
    // go-live date gets a draft public without the publish right. Here so
    // the admin form and MCP share one rule. A save with no actor is the
    // CMS's own.
    if let Some(actor) = actor {
        if crate::page::PageStatus::str_goes_live(&old_status, &page.status) {
            // A workflow on the page type is only a promise if nobody can
            // publish around it: only its approval (or a superuser) puts
            // such a page live. Asked first — "submit it for review" is
            // the useful answer whatever the actor's rights.
            if !actor.is_superuser {
                if let Some(wf) = crate::workflow::review_workflow_for_type(pool, page.page_type_id).await? {
                    return Ok(Err(PageEditRefusal::ReviewRequired { workflow: wf.name }));
                }
            }
            let req = super::route_perms::Requirement {
                codename: "cms_page.publish".to_owned(),
                page: Some((id, crate::permissions::Action::Publish)),
            };
            if !super::route_perms::allows_id(pool, actor.id, actor.is_superuser, &req).await {
                return Ok(Err(PageEditRefusal::PublishDenied));
            }
        }
    }
    // #183 — social sharing fields.
    page.og_title = form.og_title.clone();
    page.og_description = form.og_description.clone();
    page.og_image_media_id = form.og_image_media_id;
    if let Some(tc) = form.twitter_card.as_deref() {
        if matches!(tc, "summary" | "summary_large_image") {
            page.twitter_card = tc.to_owned();
        }
    }

    // Recompute the materialized `url_path` when the
    // slug changes, and cascade the recomputation to
    // every descendant so deep trees stay correct after an ancestor
    // rename.
    if slug_changed {
        let parent_url_path = match page.parent_id {
            None => None,
            Some(pid) => Page::objects()
                .where_(Page::id.eq(pid))
                .first(pool)
                .await?
                .map(|p| p.url_path),
        };
        page.url_path = crate::tree_ops::compute_url_path(parent_url_path.as_deref(), &page.slug);
    }
    let new_url_path = page.url_path.clone();

    // Wrap self-save + cascade in one atomic_tree block so the tree
    // never observes a mixed state (own page updated but children
    // stale). The #78 purge of the affected URLs — this page's new
    // URL, plus (on a slug change) its old URL and every descendant —
    // is registered via on_commit, so it fires only once the save
    // commits (never on rollback) and runs detached. #317.
    let invalidator = invalidator.clone();
    let tenant_slug = org_slug.to_owned();
    let purge_new = new_url_path.clone();
    let purge_old = old_url_path.clone();
    let (page, descendant_urls) = crate::tree_ops::atomic_tree(pool, move |tx| {
        Box::pin(async move {
            page.save_tx(tx).await?;
            let descendants: Vec<Page> = if slug_changed {
                crate::tree_ops::cascade_url_path_to_descendants(tx, &page).await?;
                Page::objects()
                    .where_(Page::path.like(crate::tree::descendants_like(&page.path)))
                    .where_(Page::path.ne(page.path.clone()))
                    .fetch_tx(tx)
                    .await?
            } else {
                Vec::new()
            };
            // A rename moves this page and everything under it: keep the
            // old addresses of the public ones working (redirects that
            // follow the page).
            if slug_changed {
                let public = |p: &Page| matches!(p.status.as_str(), "published" | "archived");
                let mut moves = Vec::with_capacity(1 + descendants.len());
                if public(&page) {
                    moves.push((page.id.get().copied().unwrap_or_default(), purge_old.clone(), purge_new.clone()));
                }
                for d in descendants.iter().filter(|d| public(d)) {
                    if let Some(old) = crate::redirect::rebase_path(&d.url_path, &purge_new, &purge_old) {
                        moves.push((d.id.get().copied().unwrap_or_default(), old, d.url_path.clone()));
                    }
                }
                crate::redirect::record_renames_tx(tx, &moves).await?;
            }
            let descendant_urls: Vec<String> = descendants.into_iter().map(|d| d.url_path).collect();
            let mut urls = Vec::with_capacity(2 + descendant_urls.len());
            urls.push(purge_new.clone());
            if slug_changed && purge_old != purge_new && !purge_old.is_empty() {
                urls.push(purge_old);
            }
            urls.extend(descendant_urls.iter().cloned());
            crate::cache_invalidate::invalidate_urls_on_commit(invalidator, tenant_slug, urls);
            Ok((page, descendant_urls))
        })
    })
    .await?;
    // #705 — the page's content goes in straight after the core row, and
    // before anything announces the save: the revision, the reference
    // index, search, publish hooks, subscriber mail and the page log all
    // describe this content. A failed write is returned to the caller,
    // not logged and swallowed behind a "Saved" flash. (The writes can't
    // join the core row's transaction yet — the handler hooks take a
    // pool — so the core row stays committed; resubmitting is safe, the
    // upserts are idempotent.)
    //
    // #189 — tags from the comma-separated `tags` field. Missing → left
    // alone; posted empty → cleared.
    if let Some(tags_raw) = form_map.get("tags") {
        let names: Vec<String> = if tags_raw.is_empty() {
            Vec::new()
        } else {
            tags_raw.split(',').map(|s| s.to_owned()).collect()
        };
        crate::page_tag::replace_tags(pool, id, &names).await?;
    }
    // #842 — categories, same missing/empty rule as tags.
    crate::category::assign::save_from_form(
        pool,
        id,
        form_map.get(crate::category::assign::FORM_KEY).map(String::as_str),
    )
    .await?;
    let pt_opt: Option<PageType> = PageType::objects()
        .where_(PageType::id.eq(page.page_type_id))
        .first(pool)
        .await?;
    save_type_content(pool, pt_opt.as_ref(), page.page_type_id, id, form_map).await?;

    // Capture revision after commit so the committed
    // page row is visible to the sequence SELECT inside capture.
    crate::revision::capture(pool, &page, actor.map(|a| a.id))
        .await
        .log_warn("revision not captured; the history has a gap");

    reindex_from_form(pool, org_slug, id, form_map).await;

    // #408 — keep an external search index (Elasticsearch) in sync:
    // published → indexed, otherwise removed. No-op when no backend is
    // installed (the Postgres FTS path needs no maintained index).
    crate::task_queue::sync_page_search(org_slug, &page).await;

    // #107 — fan out to plugin `after_publish_page` hooks.
    let actor_id = actor.map(|a| a.id);
    if page.status == crate::page::PageStatus::Published.as_str() {
        crate::hooks::fire_after_publish_page(&page);
        // #115 — notify-on-publish subscribers.
        if notify {
            let actor_name = actor.map(|a| a.username.as_str());
            crate::page_subscription::notify_publish(
                mailer,
                mailer_from,
                pool,
                org_slug,
                page.id.get().copied().unwrap_or_default(),
                &page.title,
                &page.url_path,
                actor_name,
            )
            .await;
        }
        // #192 — record the publish in the audit log.
        crate::page_log::record_or_warn(
            pool,
            id,
            crate::page_log::ACTION_PUBLISH,
            actor_id,
            format!("Published “{}”.", page.title),
            None,
        )
        .await;
    } else {
        // #192 — log the edit even when the new status isn't Published.
        crate::page_log::record_or_warn(
            pool,
            id,
            crate::page_log::ACTION_EDIT,
            actor_id,
            format!("Edited “{}”.", page.title),
            Some(serde_json::json!({ "status": page.status })),
        )
        .await;
    }


    // #64 — auto-create 301 redirects from old paths to new ones
    // when a slug change cascades through the URL tree. Editors
    // rename pages all the time; keeping the old URL alive prevents
    // external links + SEO rank from going dead.
    let mut redirects_created = 0usize;
    if slug_changed && old_url_path != new_url_path && !old_url_path.is_empty() {
        // Capture the descendant before/after pairs alongside the
        // page itself. The cascade already updated every descendant's
        // url_path in the same transaction; rebuild the OLD → NEW
        // map from the path prefix relationship: a descendant whose
        // new url_path begins with `new_url_path` had its OLD url at
        // `old_url_path + suffix`.
        let new_prefix = if new_url_path == "/" {
            String::new()
        } else {
            new_url_path.clone()
        };
        let old_prefix = if old_url_path == "/" {
            String::new()
        } else {
            old_url_path.clone()
        };
        let mut pairs: Vec<(String, String)> = vec![(old_url_path.clone(), new_url_path.clone())];
        for new_d in &descendant_urls {
            if let Some(suffix) = new_d.strip_prefix(new_prefix.as_str()) {
                let old_d = format!("{old_prefix}{suffix}");
                if old_d != *new_d {
                    pairs.push((old_d, new_d.clone()));
                }
            }
        }
        // Upsert each pair. If an existing redirect already targets the
        // old path (e.g. from a previous rename), update its `to_path`
        // so the chain compresses to one hop instead of `foo → bar →
        // baz`. New rows land with is_permanent=true (301) + a stamped
        // note for auditability.
        let now = chrono::Utc::now().to_rfc3339();
        for (old_path, new_path) in pairs {
            // Look up any existing row keyed on the old URL.
            let existing: Vec<crate::redirect::Redirect> = crate::redirect::Redirect::objects()
                .where_(crate::redirect::Redirect::from_path.eq(old_path.clone()))
                .fetch(pool)
                .await
                .unwrap_or_default();
            if let Some(mut r) = existing.into_iter().next() {
                // Compress chain: rewrite the existing row's target
                // unless it already points at the new URL.
                if r.to_path != new_path {
                    r.to_path = new_path.clone();
                    r.is_permanent = true;
                    r.note = format!("auto-updated on page rename at {now}");
                    // #553 — link the source page so the rule
                    // auto-disables if the page is later removed.
                    if r.from_page_id.is_none() {
                        r.from_page_id = Some(id);
                    }
                    r.save_pool(pool).await.log_warn("redirect not linked to its source page");
                }
                continue;
            }
            // Compress incoming chain: if an OLD existing row was
            // pointing TO old_path, redirect it straight to new_path
            // instead so the chain stays one hop.
            let stale_chain: Vec<crate::redirect::Redirect> = crate::redirect::Redirect::objects()
                .where_(crate::redirect::Redirect::to_path.eq(old_path.clone()))
                .fetch(pool)
                .await
                .unwrap_or_default();
            for mut r in stale_chain {
                // #708 — renaming back (A → B, then B → A) would chain
                // A → B into A → A. The page lives at A again, so the
                // rule has nothing left to do.
                if r.from_path == new_path {
                    r.delete_pool(pool)
                        .await
                        .log_warn("self-redirect left after a rename back not deleted");
                    continue;
                }
                r.to_path = new_path.clone();
                r.note = format!("auto-chained on page rename at {now}");
                r.save_pool(pool).await.log_warn("redirect chain not updated after a rename");
            }
            // Insert the fresh old → new row.
            let mut row = crate::redirect::Redirect {
                id: rustango::sql::Auto::Unset,
                from_path: old_path,
                to_path: new_path,
                is_permanent: true,
                note: format!("auto-created on page rename at {now}"),
                hit_count: 0,
                overrides_live: false,
                // Link the renamed page so the rule auto-disables if the
                // page is later deleted/unpublished (#553).
                from_page_id: Some(id),
                to_page_id: None,
                is_active: true,
                disabled_reason: String::new(),
                last_hit_at: None,
                created_at: rustango::sql::Auto::Unset,
                updated_at: rustango::sql::Auto::Unset,
            };
            if row.insert_pool(pool).await.is_ok() {
                redirects_created += 1;
            }
        }
    }

    // #78 cache invalidation for the affected URLs is registered via
    // `on_commit` inside the save block above (#317), so it fires only
    // once the save commits and doesn't block this response.

    // #73 PR 3 — reapproval-on-edit. When the page type is bound to
    // a workflow whose `require_reapproval_on_edit` flag is set, and
    // the page has previously been through review (any approved
    // WorkflowState in its history), restart the workflow on this
    // save. The page stays at its current status; a fresh
    // WorkflowState pinned to task #1 kicks off a new approval
    // cycle. No-op when no active workflow exists for the type, or
    // when the page hasn't been approved yet (first-time submitters
    // explicitly submit via the action bar).
    let mut reapproval_kicked = false;
    if let (Some(viewer_id), Some(pt)) = (actor.map(|a| a.id), pt_opt.as_ref()) {
        if let Some(slug) = pt.workflow_name() {
            if let Some(workflow) = crate::workflow::find_by_name(pool, &slug).await? {
                if workflow.require_reapproval_on_edit
                    && crate::workflow::active_state_for_page(pool, id)
                        .await?
                        .is_none()
                    && crate::workflow::has_prior_approval(pool, id).await?
                {
                    let tasks = crate::workflow::tasks_for(
                        pool,
                        workflow.id.get().copied().unwrap_or_default(),
                    )
                    .await?;
                    if !tasks.is_empty() {
                        crate::workflow::submit_for_review(
                            pool, id, &workflow, &tasks, viewer_id,
                        )
                        .await?;
                        reapproval_kicked = true;
                    }
                }
            }
        }
    }

    // Public render cache (rustango::cache_page::CachePageLayer)
    // is TTL-based; the new/old/descendant URLs propagate after the
    // configured TTL elapses.
    let _ = (&new_url_path, slug_changed, &old_url_path, &descendant_urls);
    // #553 — this save took the page out of Published: redirect rules
    // linked to it must not keep serving (a dead source URL, or a
    // destination that now 404s). Best-effort; the save itself stands.
    if was_published && page.status != crate::page::PageStatus::Published.as_str() {
        match crate::redirect::disable_for_page(
            pool,
            id,
            &format!("Linked page “{}” was unpublished", page.title),
        )
        .await
        {
            Ok(0) => {}
            Ok(n) => {
                tracing::info!(
                    page_id = id,
                    disabled = n,
                    "disabled redirects for unpublished page"
                );
            }
            Err(e) => {
                tracing::warn!(page_id = id, error = %e, "failed to disable redirects for unpublished page");
            }
        }
    }

    // A save that went through directly (a superuser's) supersedes a
    // change held for review; the editor showed it pre-filled.
    if actor.is_some() {
        crate::pending_change::discard(pool, id).await?;
    }

    Ok(Ok(PageEditOutcome {
        page,
        redirects_created,
        reapproval_kicked,
        held_for_review: false,
    }))
}

pub async fn page_edit_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
    Form(form_map): Form<std::collections::HashMap<String, String>>,
) -> Result<Response, AdminError> {
    let actor = session_user.as_ref().map(|u| PageEditActor {
        id: u.id.get().copied().unwrap_or_default(),
        username: u.username.clone(),
        is_superuser: u.is_superuser,
    });
    let outcome = apply_page_edit(
        tenant.pool(),
        &tenant.org.slug,
        &_state.cache_invalidator,
        _state.mailer.as_deref(),
        &_state.mailer_from,
        true,
        id,
        &form_map,
        actor.as_ref(),
    )
    .await?;
    let PageEditOutcome {
        page,
        redirects_created,
        reapproval_kicked,
        held_for_review,
    } = match outcome {
        Ok(o) => o,
        Err(PageEditRefusal::WorkflowReview { task_name }) => {
            return redirect_named_with_params_and_message_args(
                "rcms-admin:pages:edit",
                &[("id", id.to_string())],
                MsgLevel::Error,
                "Couldn't save — page is under review on “{name}”. Only reviewers of that step can edit during review.",
                &[("name", task_name.as_str())],
                &headers,
            );
        }
        Err(PageEditRefusal::LockedBy { holder }) => {
            return redirect_named_with_params_and_message_args(
                "rcms-admin:pages:edit",
                &[("id", id.to_string())],
                MsgLevel::Error,
                "Couldn't save — “{holder}” is editing this page right now. Force-unlock to take over.",
                &[("holder", holder.as_str())],
                &headers,
            );
        }
        Err(PageEditRefusal::ArchivedUnpublish) => {
            return redirect_named_with_params_and_message_args(
                "rcms-admin:pages:edit",
                &[("id", id.to_string())],
                MsgLevel::Error,
                "Can't unpublish an archived page. Set it back to Published to unarchive, or delete it after unarchiving.",
                &[],
                &headers,
            );
        }
        Err(PageEditRefusal::PublishDenied) => {
            return redirect_named_with_params_and_message(
                "rcms-admin:pages:edit",
                &[("id", id.to_string())],
                MsgLevel::Error,
                "You don't have permission to publish this page — save it as a draft or submit it for review.",
                &headers,
            );
        }
        Err(PageEditRefusal::ReviewRequired { workflow }) => {
            return redirect_named_with_params_and_message_args(
                "rcms-admin:pages:edit",
                &[("id", id.to_string())],
                MsgLevel::Error,
                "Pages of this type go live after review (“{workflow}”). Save it as a draft and click Submit for review.",
                &[("workflow", workflow.as_str())],
                &headers,
            );
        }
    };
    if held_for_review {
        return redirect_after_page_save(
            form_map.get("_action").map_or("save", String::as_str),
            id,
            page.parent_id,
            &super::i18n::tr(
                &headers,
                "Your changes to “{title}” wait for review. Visitors see the live page until they are approved.",
                &[("title", page.title.as_str())],
            ),
            &headers,
        );
    }
    let mut message = if redirects_created > 0 {
        let count = redirects_created.to_string();
        super::i18n::tr_plural(
            &headers,
            "Saved “{title}”. Auto-created {count} redirect(s) from the old URL(s).",
            redirects_created as i64,
            &[("title", page.title.as_str()), ("count", count.as_str())],
        )
    } else {
        super::i18n::tr(
            &headers,
            "Saved “{title}”.",
            &[("title", page.title.as_str())],
        )
    };
    if reapproval_kicked {
        message.push(' ');
        message.push_str(&super::i18n::tr(
            &headers,
            "Workflow restarted — page enters review again.",
            &[],
        ));
    }
    redirect_after_page_save(
        form_map.get("_action").map_or("save", String::as_str),
        id,
        page.parent_id,
        &message,
        &headers,
    )
}

/// POST /cms-admin/pages/{id}/translate?locale=<code> — upsert
/// `cms_translation` rows for the given locale. Empty values delete
/// the matching row so the canonical content shows through.
///
/// Accepts arbitrary field paths (the three core ones — `title`,
/// `seo_title`, `seo_description` — plus any custom `field_path`
/// referenced by the page's template, e.g. `intro`, `body`,
/// `nav_about`). The form sends them as `tr__<path>` keys; the
/// "add new key" row uses `new_field_path` + `new_field_value`.
/// Apply one batch of translation overrides for `(page_id, locale_id)`:
/// an empty value deletes the override row, a non-empty one upserts it.
/// Wrapped in `atomic!` so a mid-loop failure rolls back the whole batch —
/// callers never leave half-applied translations behind. Callers are
/// responsible for locale validation (in particular: never the default
/// locale — canonical content lives on `cms_page`).
pub(crate) async fn upsert_page_translations(
    pool: &rustango::sql::Pool,
    page_id: i64,
    locale_id: i64,
    updates: Vec<(String, String)>,
) -> Result<(), rustango::sql::ExecError> {
    rustango::atomic!(pool, |tx| {
        let mut guard = tx.lock().await?;
        let tx = &mut *guard;
        for (path, value) in updates {
            if value.is_empty() {
                let rows: Vec<crate::translation::Translation> =
                    crate::translation::Translation::objects()
                        .where_(crate::translation::Translation::page_id.eq(page_id))
                        .where_(crate::translation::Translation::locale_id.eq(locale_id))
                        .where_(crate::translation::Translation::field_path.eq(path.clone()))
                        .fetch_tx(tx)
                        .await?;
                for row in rows {
                    row.delete_tx(tx).await?;
                }
            } else {
                let existing: Vec<crate::translation::Translation> =
                    crate::translation::Translation::objects()
                        .where_(crate::translation::Translation::page_id.eq(page_id))
                        .where_(crate::translation::Translation::locale_id.eq(locale_id))
                        .where_(crate::translation::Translation::field_path.eq(path.clone()))
                        .fetch_tx(tx)
                        .await?;
                if let Some(mut row) = existing.into_iter().next() {
                    row.value = value;
                    row.save_tx(tx).await?;
                } else {
                    let mut row = crate::translation::Translation {
                        id: rustango::sql::Auto::Unset,
                        page_id,
                        locale_id,
                        field_path: path,
                        value,
                        updated_at: rustango::sql::Auto::Unset,
                    };
                    row.save_tx(tx).await?;
                }
            }
        }
        Ok::<_, rustango::sql::ExecError>(())
    })
    .await
}

pub async fn page_translate_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    Path(id): Path<i64>,
    Query(q): Query<EditQuery>,
    Form(form): Form<std::collections::HashMap<String, String>>,
) -> Result<Response, AdminError> {
    let code = q
        .locale
        .as_deref()
        .filter(|c| !c.is_empty())
        .ok_or_else(|| AdminError::Validation("translate requires ?locale=".to_owned()))?;
    let locale = Locale::objects()
        .where_(Locale::code.eq(code.to_owned()))
        .where_(Locale::active.eq(true))
        .first(tenant.pool())
        .await?
        .ok_or_else(|| AdminError::Validation(format!("unknown locale `{code}`")))?;
    if locale.is_default {
        return Ok((
            axum::http::StatusCode::BAD_REQUEST,
            "cannot translate INTO the default locale; canonical content lives on cms_page",
        )
            .into_response());
    }
    let locale_id = locale
        .id
        .get()
        .copied()
        .ok_or_else(|| AdminError::Validation("locale row missing id".to_owned()))?;
    // Verify the page exists; cheap one-row select.
    let _page = Page::objects()
        .where_(Page::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;

    // Collect updates from the form. Keys starting with `tr__` are
    // existing-row edits; `new_field_path` + `new_field_value` is the
    // single "add new key" row at the bottom of the editor.
    let mut updates: Vec<(String, String)> = Vec::new();
    for (k, v) in &form {
        if let Some(path) = k.strip_prefix("tr__") {
            if path.is_empty() {
                continue;
            }
            updates.push((path.to_owned(), v.clone()));
        }
    }
    let new_path = form
        .get("new_field_path")
        .map(|s| s.trim().to_owned())
        .unwrap_or_default();
    if !new_path.is_empty() {
        let new_value = form.get("new_field_value").cloned().unwrap_or_default();
        // Validate shape via the shared guard (≤255, alphanumeric + `_`
        // `.` `-`) — allows dotted block-UUID paths into StreamFields.
        if let Ok(path) = crate::translation::validate_field_path(&new_path) {
            updates.push((path, new_value));
        }
    }

    upsert_page_translations(tenant.pool(), id, locale_id, updates).await?;

    // Invalidate cache for both the canonical and `?lang=` keys so
    // the next public hit re-renders with the updated translations.
    let page = Page::objects()
        .where_(Page::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    // Public render cache (rustango::cache_page::CachePageLayer)
    // expires on TTL; translation edits propagate after the
    // configured timeout.
    let _ = (page, &code);

    Ok(Redirect::to(&format!("/cms-admin/pages/{id}/edit?locale={code}")).into_response())
}

/// POST /cms-admin/pages/{id}/delete — refuse if page has children,
/// otherwise delete the row. Cascade-delete-with-children deferred.
pub async fn page_delete_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
) -> Result<Response, AdminError> {
    let page = Page::objects()
        .where_(Page::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;

    // #556 — archived pages are frozen against deletion (archive is a
    // preservation lock, not a step toward removal). The editor must
    // unarchive first.
    if page.status == crate::page::PageStatus::Archived.as_str() {
        return redirect_named_with_message_args(
            "rcms-admin:pages:list",
            MsgLevel::Error,
            "Can't delete “{title}” while archived — unarchive it first.",
            &[("title", page.title.as_str())],
            &headers,
        );
    }

    let child_count: Vec<Page> = Page::objects()
        .where_(Page::parent_id.eq(id))
        .fetch(tenant.pool())
        .await?;
    if !child_count.is_empty() {
        let count = child_count.len().to_string();
        return redirect_named_with_message_plural(
            "rcms-admin:pages:list",
            MsgLevel::Error,
            "Can't delete “{title}” — it has {count} child page(s). Delete or move them first.",
            child_count.len() as i64,
            &[("title", page.title.as_str()), ("count", count.as_str())],
            &headers,
        );
    }

    // A hostname pointing at this root has a foreign key to it, so the
    // delete would fail with an opaque FK error. Refuse with the names
    // instead — and refuse rather than dropping the mapping, because a
    // live domain quietly falling back to a different site is a change
    // only visitors would notice.
    let serving_hosts = crate::site::hosts_for_root(tenant.pool(), id).await?;
    if !serving_hosts.is_empty() {
        let hosts = serving_hosts.join(", ");
        return redirect_named_with_message_args(
            "rcms-admin:pages:list",
            MsgLevel::Error,
            "Can't delete “{title}” — it is the root served by {hosts}. Change that on the Sites screen first.",
            &[("title", page.title.as_str()), ("hosts", hosts.as_str())],
            &headers,
        );
    }

    // #75 — promote any aliases of this page to standalone pages
    // before deleting the source. Each alias inherits a snapshot of
    // the source's content (title / SEO / status / theme) so its
    // public URL keeps rendering coherent content after the source
    // is gone. Without this step the FK guard would refuse the
    // delete, or worse — aliases would point at a dead row.
    let aliases: Vec<Page> = Page::objects()
        .where_(Page::alias_of.eq(id))
        .fetch(tenant.pool())
        .await?;
    let alias_count = aliases.len();
    let title = page.title.clone();
    let url_path = page.url_path.clone();

    // #317 — alias promotion + the delete run in one `atomic!` block,
    // so a failure can't leave aliases promoted against a still-present
    // source. The #78 cache purge is registered via `on_commit`, so it
    // fires only once the delete commits — a rollback never evicts a
    // still-valid public render. The purge runs detached, so the next
    // visitor sees a 404, not a stale 200, without blocking this
    // response on the cache round-trip.
    let invalidator = _state.cache_invalidator.clone();
    let tenant_slug = tenant.org.slug.clone();
    let purge_url = url_path.clone();
    rustango::atomic!(tenant.pool(), |tx| {
        let mut guard = tx.lock().await?;
        let tx = &mut *guard;
        for mut alias in aliases {
            alias.alias_of = None;
            alias.title = page.title.clone();
            alias.seo_title = page.seo_title.clone();
            alias.seo_description = page.seo_description.clone();
            alias.status = page.status.clone();
            alias.published_at = page.published_at;
            alias.last_published_at = page.last_published_at;
            alias.theme_id = page.theme_id;
            alias.show_in_menus = page.show_in_menus;
            alias.save_tx(tx).await?;
        }
        // Revisions, tags, logs… point at the page with no ON DELETE
        // action; clear them first or the delete fails.
        crate::page_delete::clear_page_references_tx(tx, id).await?;
        page.delete_tx(tx).await?;
        crate::cache_invalidate::invalidate_urls_on_commit(
            invalidator,
            tenant_slug,
            vec![purge_url],
        );
        Ok(())
    })
    .await?;
    // #408 — drop the deleted page from the external search index
    // (no-op when no backend is installed).
    crate::task_queue::remove_page_search(&tenant.org.slug, id).await;
    // #553 — redirect rules linked to this page must not keep serving.
    // Best-effort; the delete already committed.
    if let Err(e) = crate::redirect::disable_for_page(
        tenant.pool(),
        id,
        &format!("Linked page “{title}” was deleted"),
    )
    .await
    {
        tracing::warn!(page_id = id, error = %e, "failed to disable redirects for deleted page");
    }
    if alias_count > 0 {
        redirect_named_with_message_plural(
            "rcms-admin:pages:list",
            MsgLevel::Success,
            "Deleted “{title}”. Promoted {count} alias(es) to standalone page(s) with the source's last content.",
            alias_count as i64,
            &[("title", title.as_str()), ("count", &alias_count.to_string())],
            &headers,
        )
    } else {
        redirect_named_with_message_args(
            "rcms-admin:pages:list",
            MsgLevel::Success,
            "Deleted “{title}”.",
            &[("title", title.as_str())],
            &headers,
        )
    }
}

// =====================================================================
// Slice 6 — drag-reorder + locale variants
// =====================================================================

/// Form payload for `POST /cms-admin/pages/{id}/move` — emitted by
/// the tree view's drag-and-drop JS.
#[derive(Debug, Deserialize)]
pub struct MoveForm {
    /// The drop target's page id. The moved page becomes a child of
    /// this row (appended at the bottom of its existing children).
    /// Negative or zero values mean "move to root".
    pub new_parent: i64,
}

/// POST /cms-admin/pages/{id}/move — change a page's parent +
/// sort_order via the existing `Page::move_to`. Wraps the cascade so
/// every descendant's `url_path` is re-materialized.
pub async fn page_move_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
    Form(form): Form<MoveForm>,
) -> Result<Response, AdminError> {
    let mut page = Page::objects()
        .where_(Page::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    let new_parent: Option<Page> = if form.new_parent > 0 {
        Page::objects()
            .where_(Page::id.eq(form.new_parent))
            .first(tenant.pool())
            .await?
    } else {
        None
    };
    let old_url_path = page.url_path.clone();
    let old_descendant_urls: Vec<String> =
        page.descendants(tenant.pool()).await?.into_iter().map(|d| d.url_path).collect();
    // One transaction moves the tree and its URLs together (#706).
    page.move_to(&tenant, new_parent.as_ref()).await?;
    let new_url_path = page.url_path.clone();

    // #78 — purge the old and new URL of the page and every descendant,
    // now that the move has committed; detached so the response isn't
    // blocked on the cache round-trips.
    let mut urls = old_descendant_urls;
    urls.push(old_url_path.clone());
    urls.push(new_url_path.clone());
    urls.extend(page.descendants(tenant.pool()).await?.into_iter().map(|d| d.url_path));
    let invalidator = _state.cache_invalidator.clone();
    let tenant_slug = tenant.org.slug.clone();
    rustango::__private_runtime::tokio::spawn(async move {
        crate::task_queue::purge_urls(invalidator, tenant_slug, urls).await;
    });

    // #192 — audit-log the move.
    let actor_id = session_user.as_ref().and_then(|u| u.id.get().copied());
    crate::page_log::record_or_warn(
        tenant.pool(),
        id,
        crate::page_log::ACTION_MOVE,
        actor_id,
        format!(
            "Moved “{}” from {old_url_path} to {new_url_path}.",
            page.title
        ),
        Some(serde_json::json!({
            "old_url_path": old_url_path,
            "new_url_path": new_url_path,
            "new_parent_id": form.new_parent,
        })),
    )
    .await;

    Ok((axum::http::StatusCode::OK, "ok").into_response())
}

/// POST /cms-admin/pages/{id}/clone — duplicate the source page as a
/// draft sibling. The copy carries everything the page stores —
/// core fields (SEO, OG/Twitter, theme, template override), tags, the
/// extension row, inline panels and the page-builder body — because it
/// is saved through the same pipeline as an edit, from the source's
/// [`crate::page_form::prefill`]. The new row:
///
/// - Lives under the same parent as the source.
/// - Title gets a " (copy)" suffix.
/// - Slug gets a "-copy" suffix; on conflict with an existing
///   sibling slug, we suffix `-copy-2`, `-copy-3`, … until unique.
/// - Status forces back to Draft, with no schedule, so editors review
///   before publishing.
/// - Locale-variant pointer cleared (variants stay independent of
///   any single source).
///
/// Subtree clone (source + all descendants) is not supported yet.
pub async fn page_clone_submit(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
) -> Result<Response, AdminError> {
    let source = Page::objects()
        .where_(Page::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;

    let title = format!("{} (copy)", source.title);
    let base_slug = source.slug.trim_matches('-').to_owned();
    let slug = unique_sibling_slug(
        tenant.pool(),
        source.parent_id,
        &format!("{base_slug}-copy"),
    )
    .await?;

    let new = crate::tree_ops::NewPage::new(source.page_type_id, title, slug)
        .with_status(crate::page::PageStatus::Draft)
        .with_seo(source.seo_title.clone(), source.seo_description.clone());

    let clone = match source.parent_id {
        Some(pid) => {
            let parent = Page::objects()
                .where_(Page::id.eq(pid))
                .first(tenant.pool())
                .await?
                .ok_or(AdminError::NotFound(pid))?;
            Page::create_child(&tenant, &parent, new).await?
        }
        None => Page::create_root(&tenant, new).await?,
    };
    let new_id = clone.id.get().copied().unwrap_or_default();

    // Copy the content by saving the source's full form onto the new
    // row: the edit pipeline writes the core fields, tags, extension,
    // inline panels and builder body, and captures the first revision
    // with all of it.
    let mut form = crate::page_form::prefill(tenant.pool(), &source).await;
    form.insert("title".into(), clone.title.clone());
    form.insert("slug".into(), clone.slug.clone());
    form.insert(
        "status".into(),
        crate::page::PageStatus::Draft.as_str().to_owned(),
    );
    form.remove("go_live_at");
    form.remove("expire_at");
    let actor = session_user.as_ref().map(|u| PageEditActor {
        id: u.id.get().copied().unwrap_or_default(),
        username: u.username.clone(),
        is_superuser: u.is_superuser,
    });
    let saved = apply_page_edit(
        tenant.pool(),
        &tenant.org.slug,
        &state.cache_invalidator,
        None,
        "",
        false,
        new_id,
        &form,
        actor.as_ref(),
    )
    .await?;
    if saved.is_err() {
        // A page created a moment ago has no workflow or editor lock.
        return Err(AdminError::Validation(
            "the copy was created but its content could not be saved".to_owned(),
        ));
    }
    // #192 — audit-log the clone on BOTH the new row (so the new
    // page's history shows where it came from) and the source (so
    // the source's history records its descendants).
    let actor_id = session_user.as_ref().and_then(|u| u.id.get().copied());
    crate::page_log::record_or_warn(
        tenant.pool(),
        new_id,
        crate::page_log::ACTION_COPY,
        actor_id,
        format!("Cloned from “{}” (page #{id}).", source.title),
        Some(serde_json::json!({ "source_page_id": id })),
    )
    .await;
    crate::page_log::record_or_warn(
        tenant.pool(),
        id,
        crate::page_log::ACTION_COPY,
        actor_id,
        format!("Cloned to new draft page #{new_id}."),
        Some(serde_json::json!({ "clone_page_id": new_id })),
    )
    .await;
    redirect_named_with_params_and_message_args(
        "rcms-admin:pages:edit",
        &[("id", new_id.to_string())],
        MsgLevel::Success,
        "Cloned “{title}” as a draft. Review before publishing.",
        &[("title", source.title.as_str())],
        &headers,
    )
}

/// Form payload for `POST /cms-admin/pages/{id}/privacy`.
/// `kind` is one of `"none"`, `"login"`, `"groups"`, `"password"`.
/// `password` is the cleartext entered when `kind=password` (hashed
/// + discarded server-side). `group_ids` is the comma-separated list
/// for `kind=groups`.
#[derive(Deserialize)]
pub struct PrivacyForm {
    pub kind: String,
    #[serde(default)]
    pub password: String,
    #[serde(default)]
    pub group_ids: String,
    /// Comma-separated permission codenames — used when `kind =
    /// permission` (the permission-engine gate). Empty otherwise.
    #[serde(default)]
    pub codenames: String,
}

pub async fn page_privacy_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
    Form(form): Form<PrivacyForm>,
) -> Result<Response, AdminError> {
    use rustango::sql::Auto;
    let existing = crate::view_restriction::direct_for_page(tenant.pool(), id).await?;
    if form.kind == "none" {
        if let Some(row) = existing {
            row.delete_pool(tenant.pool()).await?;
            return redirect_named_with_params_and_message(
                "rcms-admin:pages:edit",
                &[("id", id.to_string())],
                MsgLevel::Success,
                "Removed view restriction — page is now public.",
                &headers,
            );
        }
        return redirect_named_with_params_and_message(
            "rcms-admin:pages:edit",
            &[("id", id.to_string())],
            MsgLevel::Info,
            "No restriction to remove.",
            &headers,
        );
    }
    let Some(kind) = crate::view_restriction::RestrictionKind::parse(&form.kind) else {
        return Err(AdminError::Validation(format!(
            "unknown restriction kind `{}`",
            form.kind
        )));
    };
    let password_hash = match kind {
        crate::view_restriction::RestrictionKind::Password => {
            // Reuse the existing hash when the editor left the
            // field blank on a page that's already password-
            // protected — lets them flip groups/login back to
            // password without remembering the original.
            if form.password.is_empty() {
                if let Some(row) = &existing {
                    if row.kind == kind.as_str() {
                        row.password_hash.clone()
                    } else {
                        return redirect_named_with_params_and_message(
                            "rcms-admin:pages:edit",
                            &[("id", id.to_string())],
                            MsgLevel::Error,
                            "Password is required when switching the restriction kind to password.",
                            &headers,
                        );
                    }
                } else {
                    return redirect_named_with_params_and_message(
                        "rcms-admin:pages:edit",
                        &[("id", id.to_string())],
                        MsgLevel::Error,
                        "Password is required for password-protected pages.",
                        &headers,
                    );
                }
            } else {
                crate::passwords::hash(&form.password).await
                    .map_err(|e| AdminError::Validation(format!("password hash failed: {e}")))?
            }
        }
        _ => String::new(),
    };
    let group_ids: Vec<i64> = if matches!(kind, crate::view_restriction::RestrictionKind::Groups) {
        form.group_ids
            .split(',')
            .filter_map(|s| s.trim().parse::<i64>().ok())
            .collect()
    } else {
        Vec::new()
    };
    let group_json = serde_json::json!(group_ids);
    let codenames: Vec<String> =
        if matches!(kind, crate::view_restriction::RestrictionKind::Permission) {
            form.codenames
                .split(',')
                .map(str::trim)
                .filter(|s| !s.is_empty())
                .map(str::to_owned)
                .collect()
        } else {
            Vec::new()
        };
    let codenames_json = serde_json::json!(codenames);
    match existing {
        Some(mut row) => {
            row.kind = kind.as_str().to_owned();
            row.password_hash = password_hash;
            row.group_ids = group_json;
            row.codenames = codenames_json;
            row.save_pool(tenant.pool()).await?;
        }
        None => {
            let mut row = crate::view_restriction::PageViewRestriction {
                id: Auto::Unset,
                page_id: id,
                kind: kind.as_str().to_owned(),
                password_hash,
                group_ids: group_json,
                codenames: codenames_json,
                created_at: Auto::Unset,
                updated_at: Auto::Unset,
            };
            row.insert_pool(tenant.pool()).await?;
        }
    }
    redirect_named_with_params_and_message_args(
        "rcms-admin:pages:edit",
        &[("id", id.to_string())],
        MsgLevel::Success,
        match kind {
            crate::view_restriction::RestrictionKind::Login => "Only signed-in people can see this page now.",
            crate::view_restriction::RestrictionKind::Groups => "Only the chosen roles can see this page now.",
            crate::view_restriction::RestrictionKind::Permission => "Only people with the chosen permissions can see this page now.",
            crate::view_restriction::RestrictionKind::Password => "This page asks for a password now.",
        },
        &[],
        &headers,
    )
}

// =====================================================================
// Inline comments (#81 PR 1)
// =====================================================================

#[derive(Debug, Deserialize)]
pub struct CommentNewForm {
    pub field_path: String,
    pub body: String,
}

pub async fn comment_new_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
    Form(form): Form<CommentNewForm>,
) -> Result<Response, AdminError> {
    let Some(viewer_id) = session_user.as_ref().and_then(|u| u.id.get().copied()) else {
        return Ok(unauthorized_no_session());
    };
    let body = form.body.trim();
    let field_path = form.field_path.trim();
    if body.is_empty() || field_path.is_empty() {
        return redirect_named_with_params_and_message(
            "rcms-admin:pages:edit",
            &[("id", id.to_string())],
            MsgLevel::Warning,
            "Comment body and field are both required.",
            &headers,
        );
    }
    let mut row = crate::comment::Comment {
        id: rustango::sql::Auto::Unset,
        page_id: id,
        field_path: field_path.to_owned(),
        author_id: viewer_id,
        body: body.to_owned(),
        resolved_at: None,
        resolved_by: None,
        created_at: rustango::sql::Auto::Unset,
    };
    row.insert_pool(tenant.pool()).await?;
    redirect_named_with_params_and_message(
        "rcms-admin:pages:edit",
        &[("id", id.to_string())],
        MsgLevel::Success,
        "Comment posted.",
        &headers,
    )
}

#[derive(Debug, Deserialize)]
pub struct CommentReplyForm {
    pub body: String,
}

pub async fn comment_reply_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path((id, comment_id)): Path<(i64, i64)>,
    Form(form): Form<CommentReplyForm>,
) -> Result<Response, AdminError> {
    let Some(viewer_id) = session_user.as_ref().and_then(|u| u.id.get().copied()) else {
        return Ok(unauthorized_no_session());
    };
    let body = form.body.trim();
    if body.is_empty() {
        return redirect_named_with_params_and_message(
            "rcms-admin:pages:edit",
            &[("id", id.to_string())],
            MsgLevel::Warning,
            "Reply body is required.",
            &headers,
        );
    }
    let mut row = crate::comment::CommentReply {
        id: rustango::sql::Auto::Unset,
        comment_id,
        author_id: viewer_id,
        body: body.to_owned(),
        created_at: rustango::sql::Auto::Unset,
    };
    row.insert_pool(tenant.pool()).await?;
    redirect_named_with_params_and_message(
        "rcms-admin:pages:edit",
        &[("id", id.to_string())],
        MsgLevel::Success,
        "Reply posted.",
        &headers,
    )
}

pub async fn comment_resolve_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path((id, comment_id)): Path<(i64, i64)>,
) -> Result<Response, AdminError> {
    let Some(viewer_id) = session_user.as_ref().and_then(|u| u.id.get().copied()) else {
        return Ok(unauthorized_no_session());
    };
    let mut row = crate::comment::Comment::objects()
        .where_(crate::comment::Comment::id.eq(comment_id))
        .where_(crate::comment::Comment::page_id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(comment_id))?;
    row.resolved_at = Some(chrono::Utc::now());
    row.resolved_by = Some(viewer_id);
    row.save_pool(tenant.pool()).await?;
    redirect_named_with_params_and_message(
        "rcms-admin:pages:edit",
        &[("id", id.to_string())],
        MsgLevel::Success,
        "Comment resolved.",
        &headers,
    )
}

pub async fn comment_reopen_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Path((id, comment_id)): Path<(i64, i64)>,
) -> Result<Response, AdminError> {
    let mut row = crate::comment::Comment::objects()
        .where_(crate::comment::Comment::id.eq(comment_id))
        .where_(crate::comment::Comment::page_id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(comment_id))?;
    row.resolved_at = None;
    row.resolved_by = None;
    row.save_pool(tenant.pool()).await?;
    redirect_named_with_params_and_message(
        "rcms-admin:pages:edit",
        &[("id", id.to_string())],
        MsgLevel::Info,
        "Comment reopened.",
        &headers,
    )
}

/// POST /cms-admin/pages/{id}/alias — create a live alias of this
/// page as a sibling. The alias is a real tree row at a
/// distinct URL but its content fields proxy to the source. Edits
/// to the source propagate instantly; edits to the alias redirect
/// the editor to the source.
///
/// Differentiation from sibling actions:
///   * Clone (`/clone`)         — deep copy, divergent content
///   * Alias (this)             — one source, multiple URLs
///
/// Localization uses the `cms_translation` per-field override table
/// (see [`crate::translation`]); the legacy structural-variant flow
/// was removed.
pub async fn page_alias_submit(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
) -> Result<Response, AdminError> {
    let source = Page::objects()
        .where_(Page::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;

    // Refuse to alias an alias — the chain would be impossible to
    // resolve without cycle detection, and there's no editorial use
    // case for it. Authors should alias the underlying source.
    if let Some(src_id) = source.alias_of {
        let src_id_str = src_id.to_string();
        return redirect_named_with_params_and_message_args(
            "rcms-admin:pages:edit",
            &[("id", id.to_string())],
            MsgLevel::Error,
            "Can't alias an alias — point your new alias at the underlying source page (#{source_id}) instead.",
            &[("source_id", src_id_str.as_str())],
            &headers,
        );
    }

    let base_slug = source.slug.trim_matches('-').to_owned();
    let slug = unique_sibling_slug(
        tenant.pool(),
        source.parent_id,
        &if base_slug.is_empty() {
            "alias".to_owned()
        } else {
            format!("{base_slug}-alias")
        },
    )
    .await?;

    // tree_ops::NewPage carries title/slug/page_type but the title
    // gets shadowed by the source on render. Use the source's title
    // verbatim so the admin lists still show something meaningful.
    let new = crate::tree_ops::NewPage::new(source.page_type_id, source.title.clone(), slug)
        .with_status(crate::page::PageStatus::Draft)
        .with_seo(source.seo_title.clone(), source.seo_description.clone());

    let mut alias = match source.parent_id {
        Some(pid) => {
            let parent = Page::objects()
                .where_(Page::id.eq(pid))
                .first(tenant.pool())
                .await?
                .ok_or(AdminError::NotFound(pid))?;
            Page::create_child(&tenant, &parent, new).await?
        }
        None => Page::create_root(&tenant, new).await?,
    };
    alias.alias_of = Some(id);
    // Aliases inherit publish status from the source on render —
    // the underlying status column on the alias row is irrelevant
    // for live viewing, but setting it here keeps the admin list
    // visually accurate.
    alias.status = source.status.clone();
    alias.published_at = source.published_at;
    alias.last_published_at = source.last_published_at;
    alias.theme_id = source.theme_id;
    alias.show_in_menus = source.show_in_menus;
    alias.save_pool(tenant.pool()).await?;

    crate::revision::capture(tenant.pool(), &alias, None)
        .await
        .log_warn("revision not captured; the history has a gap");
    let new_id = alias.id.get().copied().unwrap_or_default();
    let _ = state;
    // #192 — audit log on both source + alias.
    let actor_id = session_user.as_ref().and_then(|u| u.id.get().copied());
    crate::page_log::record_or_warn(
        tenant.pool(),
        new_id,
        crate::page_log::ACTION_ALIAS_CREATE,
        actor_id,
        format!("Alias of “{}” (page #{id}).", source.title),
        Some(serde_json::json!({ "alias_of": id })),
    )
    .await;
    crate::page_log::record_or_warn(
        tenant.pool(),
        id,
        crate::page_log::ACTION_ALIAS_CREATE,
        actor_id,
        format!("New alias #{new_id} pointing here."),
        Some(serde_json::json!({ "alias_page_id": new_id })),
    )
    .await;
    redirect_named_with_params_and_message_args(
        "rcms-admin:pages:list",
        // Land on the page list (alias's edit form proxies to the
        // source, so /edit just bounces; the list is the right
        // landing). Include the alias's parent if available so the
        // editor sees the new alias in context.
        &[],
        MsgLevel::Success,
        "Created alias of “{title}”. Edits to the source will propagate to every alias's URL.",
        &[("title", source.title.as_str())],
        &headers,
    )
}

/// Canonical fields seeded into `cms_translation` by the
/// copy-to-locale bulk action: the page's `title`, `seo_title`, and
/// `seo_description`, skipping empties. The source text gives
/// translators a starting point.
fn copy_fields_for(p: &Page) -> Vec<(&'static str, String)> {
    copy_translatable_fields(&p.title, &p.seo_title, &p.seo_description)
}

/// Pure core of [`copy_fields_for`] — selects the non-empty canonical
/// fields. Split out so the selection is unit-testable without a full
/// `Page`.
fn copy_translatable_fields(
    title: &str,
    seo_title: &str,
    seo_description: &str,
) -> Vec<(&'static str, String)> {
    [
        ("title", title),
        ("seo_title", seo_title),
        ("seo_description", seo_description),
    ]
    .into_iter()
    .filter(|(_, v)| !v.trim().is_empty())
    .map(|(k, v)| (k, v.to_owned()))
    .collect()
}

/// POST /cms-admin/pages/bulk — multi-select dispatcher for the
/// page list. Body shape (urlencoded):
/// `action=<verb>&ids=<id>&ids=<id>[&new_parent=<id>]`.
///
/// Verbs:
///   - `publish`     → flips every selected page's `status` to
///                     Published (no-op for already-published rows).
///   - `unpublish`   → flips to Draft (no-op for already-draft rows).
///   - `delete`      → bulk delete. Rejects atomically if ANY selected
///                     page has children — matches the per-row guard.
///   - `move`        → reparent every selected page under `new_parent`
///                     (or to root when `new_parent=0`); cascades
///                     `url_path` to descendants. Filters out rows
///                     that would form a self-loop (you can't move a
///                     page under itself or one of its descendants).
///
/// All four are wrapped in a single transaction so a partial failure
/// rolls back the entire batch. The response is a redirect back to
/// the page list with a flash message summarizing how many rows were
/// touched + which were skipped (with reason).
pub async fn page_bulk_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Form(form): Form<Vec<(String, String)>>,
) -> Result<Response, AdminError> {
    let action = form
        .iter()
        .find(|(k, _)| k == "action")
        .map(|(_, v)| v.clone())
        .unwrap_or_default();
    let ids: Vec<i64> = form
        .iter()
        .filter(|(k, _)| k == "ids" || k == "id")
        .filter_map(|(_, v)| v.parse::<i64>().ok())
        .collect();
    let new_parent_raw = form
        .iter()
        .find(|(k, _)| k == "new_parent")
        .and_then(|(_, v)| v.parse::<i64>().ok());
    // #556 — archive cascade: mark every descendant's own row, not just
    // rely on inheritance. Accepts the checkbox/select truthy forms.
    let archive_cascade = form.iter().any(|(k, v)| {
        k == "archive_cascade" && matches!(v.as_str(), "1" | "on" | "subtree" | "true")
    });

    if action.is_empty() || ids.is_empty() {
        return redirect_named_with_message(
            "rcms-admin:pages:list",
            MsgLevel::Warning,
            "Pick at least one row + an action before submitting.",
            &headers,
        );
    }

    // Load the selected rows in one shot so per-id validation can run
    // before any mutation. The IN-list is small (page list pagination
    // caps the visible set; even an unbounded multi-select is dwarfed
    // by site-wide page counts).
    let pool = tenant.pool();
    let pages: Vec<Page> = {
        let mut out = Vec::with_capacity(ids.len());
        for id in &ids {
            if let Some(p) = Page::objects().where_(Page::id.eq(*id)).first(pool).await? {
                out.push(p);
            }
        }
        out
    };
    if pages.is_empty() {
        return redirect_named_with_message(
            "rcms-admin:pages:list",
            MsgLevel::Warning,
            "None of the selected ids matched a live page.",
            &headers,
        );
    }

    // #672 — the route gate only knows this is a page edit; the action
    // posted decides what it really needs, on every selected page.
    let needs = match action.as_str() {
        "publish" | "unpublish" => crate::permissions::Action::Publish,
        "delete" => crate::permissions::Action::Delete,
        _ => crate::permissions::Action::Edit,
    };
    if let Some(user) = session_user.as_ref() {
        for p in &pages {
            let req = super::route_perms::Requirement {
                codename: format!("cms_page.{}", needs.as_str()),
                page: p.id.get().copied().map(|id| (id, needs)),
            };
            if !super::route_perms::allows(pool, user, &req).await {
                return redirect_named_with_message_args(
                    "rcms-admin:pages:list",
                    MsgLevel::Error,
                    "You don't have permission to {action} “{title}”.",
                    &[("action", action.as_str()), ("title", p.title.as_str())],
                    &headers,
                );
            }
        }
    }

    match action.as_str() {
        "publish" | "unpublish" => {
            let target = if action == "publish" {
                crate::page::PageStatus::Published
            } else {
                crate::page::PageStatus::Draft
            };
            let invalidator = _state.cache_invalidator.clone();
            let tenant_slug = tenant.org.slug.clone();
            let purge_urls: Vec<String> = pages.iter().map(|p| p.url_path.clone()).collect();
            let pages_for_tx = pages.clone();
            // #317 — flip the batch's status in one atomic! block and
            // register the #78 per-URL purge via on_commit, so it fires
            // only once the batch commits (detached; dropped on rollback).
            let touched = rustango::atomic!(pool, |tx| {
                let mut guard = tx.lock().await?;
                let tx = &mut *guard;
                let mut touched = 0usize;
                for mut p in pages_for_tx {
                    // #556 — archived pages are frozen against unpublish;
                    // they must be explicitly unarchived. Skip them in a
                    // bulk unpublish (bulk publish still revives them).
                    if matches!(target, crate::page::PageStatus::Draft)
                        && p.status == crate::page::PageStatus::Archived.as_str()
                    {
                        continue;
                    }
                    if p.status != target.as_str() {
                        p.status = target.as_str().to_owned();
                        if matches!(target, crate::page::PageStatus::Published) {
                            let now = chrono::Utc::now();
                            if p.published_at.is_none() {
                                p.published_at = Some(now);
                            }
                            // #251 — bulk publish also bumps last-publish.
                            p.last_published_at = Some(now);
                        }
                        p.save_tx(tx).await?;
                        touched += 1;
                    }
                }
                crate::cache_invalidate::invalidate_urls_on_commit(
                    invalidator,
                    tenant_slug,
                    purge_urls,
                );
                Ok::<usize, rustango::sql::ExecError>(touched)
            })
            .await?;
            // Capture revisions outside the transaction; failure here
            // only affects history bookkeeping.
            for p in &pages {
                crate::revision::capture(pool, p, None)
                    .await
                    .log_warn("revision not captured; the history has a gap");
            }
            // #553 — bulk unpublish: rules linked to pages leaving
            // Published must not keep serving. `pages` holds the
            // pre-flip rows, so filter on the OLD status.
            if !matches!(target, crate::page::PageStatus::Published) {
                for p in &pages {
                    if p.status != crate::page::PageStatus::Published.as_str() {
                        continue;
                    }
                    let pid = p.id.get().copied().unwrap_or_default();
                    if let Err(e) = crate::redirect::disable_for_page(
                        pool,
                        pid,
                        &format!("Linked page “{}” was unpublished", p.title),
                    )
                    .await
                    {
                        tracing::warn!(page_id = pid, error = %e, "failed to disable redirects for unpublished page");
                    }
                }
            }
            // #107 — plugin after_publish_page on bulk publish only.
            if matches!(target, crate::page::PageStatus::Published) {
                for p in &pages {
                    crate::hooks::fire_after_publish_page(p);
                }
                // #115 — notify-on-publish subscribers (per page).
                let actor = session_user.as_ref().map(|u| u.username.as_str());
                for p in &pages {
                    crate::page_subscription::notify_publish(
                        _state.mailer.as_deref(),
                        &_state.mailer_from,
                        pool,
                        &tenant.org.slug,
                        p.id.get().copied().unwrap_or_default(),
                        &p.title,
                        &p.url_path,
                        actor,
                    )
                    .await;
                }
            }
            let already = (pages.len() - touched).to_string();
            let key = if action == "publish" {
                "Published {count} page(s) ({already} already in the target state)."
            } else {
                "Unpublished {count} page(s) ({already} already in the target state)."
            };
            redirect_named_with_message_plural(
                "rcms-admin:pages:list",
                MsgLevel::Success,
                key,
                touched as i64,
                &[("count", &touched.to_string()), ("already", already.as_str())],
                &headers,
            )
        }
        "delete" => {
            // #556 — archived pages are a preservation lock: they can't
            // be deleted until explicitly unarchived. Reject the whole
            // batch if any selected page is archived (mirrors the child
            // guard's all-or-nothing rule).
            let archived_names: Vec<String> = pages
                .iter()
                .filter(|p| p.status == crate::page::PageStatus::Archived.as_str())
                .map(|p| format!("“{}”", p.title))
                .collect();
            if !archived_names.is_empty() {
                let list = archived_names.join(", ");
                return redirect_named_with_message_args(
                    "rcms-admin:pages:list",
                    MsgLevel::Error,
                    "Can't delete archived page(s) {names} — unarchive them first.",
                    &[("names", list.as_str())],
                    &headers,
                );
            }
            // Atomic guard: if ANY selected page has children, the
            // whole batch fails. Matches the per-row delete rule and
            // the acceptance criterion on the issue.
            let id_set: std::collections::HashSet<i64> =
                pages.iter().filter_map(|p| p.id.get().copied()).collect();
            let mut offenders: Vec<String> = Vec::new();
            for p in &pages {
                let pid = p.id.get().copied().unwrap_or_default();
                let children: Vec<Page> = Page::objects()
                    .where_(Page::parent_id.eq(pid))
                    .fetch(pool)
                    .await?;
                // Children that are themselves part of this batch
                // don't count as blockers — they'll be deleted in
                // the same transaction.
                let external_children = children
                    .iter()
                    .filter(|c| !id_set.contains(&c.id.get().copied().unwrap_or_default()))
                    .count();
                if external_children > 0 {
                    offenders.push(format!("#{pid} “{}” ({external_children})", p.title));
                }
            }
            if !offenders.is_empty() {
                let offenders_list = offenders.join(", ");
                return redirect_named_with_message_args(
                    "rcms-admin:pages:list",
                    MsgLevel::Error,
                    "Bulk delete blocked — these page(s) still have external children: {offenders}.",
                    &[("offenders", offenders_list.as_str())],
                    &headers,
                );
            }
            // Delete deepest-first so child rows go before parents
            // and FK guards stay green. Every row that points at a page
            // (revisions, tags, logs, host extension rows…) is cleared
            // first: those FKs have no ON DELETE action.
            let mut ordered = pages.clone();
            ordered.sort_by(|a, b| b.depth.cmp(&a.depth));
            let invalidator = _state.cache_invalidator.clone();
            let tenant_slug = tenant.org.slug.clone();
            // #78 — purge every deleted URL so the next visitor sees a
            // 404, not a stale 200. #317 — registered via on_commit so
            // it fires only once the whole batch delete commits.
            let purge_urls: Vec<String> = pages.iter().map(|p| p.url_path.clone()).collect();
            rustango::atomic!(pool, |tx| {
                let mut guard = tx.lock().await?;
                let tx = &mut *guard;
                for p in &ordered {
                    let pid = p.id.get().copied().unwrap_or_default();
                    crate::page_delete::clear_page_references_tx(tx, pid).await?;
                }
                for p in ordered {
                    p.delete_tx(tx).await?;
                }
                crate::cache_invalidate::invalidate_urls_on_commit(
                    invalidator,
                    tenant_slug,
                    purge_urls,
                );
                Ok::<(), rustango::sql::ExecError>(())
            })
            .await?;
            // #553 — disable redirect rules linked to any deleted page.
            for p in &pages {
                let pid = p.id.get().copied().unwrap_or_default();
                if let Err(e) = crate::redirect::disable_for_page(
                    pool,
                    pid,
                    &format!("Linked page “{}” was deleted", p.title),
                )
                .await
                {
                    tracing::warn!(page_id = pid, error = %e, "failed to disable redirects for deleted page");
                }
            }
            redirect_named_with_message_plural(
                "rcms-admin:pages:list",
                MsgLevel::Success,
                "Deleted {count} page(s).",
                pages.len() as i64,
                &[("count", &pages.len().to_string())],
                &headers,
            )
        }
        "move" => {
            let Some(new_parent_id) = new_parent_raw else {
                return redirect_named_with_message(
                    "rcms-admin:pages:list",
                    MsgLevel::Warning,
                    "Bulk move requires a destination — pick a parent page.",
                    &headers,
                );
            };
            let new_parent: Option<Page> = if new_parent_id > 0 {
                Page::objects()
                    .where_(Page::id.eq(new_parent_id))
                    .first(pool).await?
            } else {
                None
            };
            // Self-loop guard: can't move a page under itself or any
            // of its descendants. Path-prefix check is cheap.
            if let Some(np) = &new_parent {
                for p in &pages {
                    if np.path.starts_with(&p.path) {
                        return redirect_named_with_message_args(
                            "rcms-admin:pages:list",
                            MsgLevel::Error,
                            "Can't move “{title}” under “{parent}” — destination is itself or a descendant.",
                            &[("title", p.title.as_str()), ("parent", np.title.as_str())],
                            &headers,
                        );
                    }
                }
            }

            // Pre-validate page-type placement for every selected row
            // before mutating anything. If a single page rejects the
            // destination, the whole batch fails — matches the issue's
            // atomicity requirement so editors never end up with a
            // partial reparent.
            if let Some(np) = &new_parent {
                for p in &pages {
                    if let Err(e) =
                        crate::tree_ops::validate_child_placement(pool, np.page_type_id, p.page_type_id)
                            .await
                    {
                        let err = e.to_string();
                        return redirect_named_with_message_args(
                            "rcms-admin:pages:list",
                            MsgLevel::Error,
                            "Move blocked — “{title}” can't live under “{parent}”: {error}",
                            &[("title", p.title.as_str()), ("parent", np.title.as_str()), ("error", err.as_str())],
                            &headers,
                        );
                    }
                }
            }

            // F2 / #317 — reparent the whole batch in ONE atomic_tree so a
            // mid-batch failure rolls back every move (no partial reparent;
            // placement was already pre-validated above). Each page's
            // subtree is pre-read on the pool *before* the transaction, so
            // nothing inside the tx reads the pool (which would deadlock a
            // single-connection SQLite pool); `move_to_in_tx` then reparents
            // each page within the shared tx. The #78 purge of every page's
            // old + new URL + descendant URLs (captured pre-cascade) is
            // registered via on_commit, firing only once the batch commits.
            let mut to_move: Vec<(Page, Vec<Page>)> = Vec::with_capacity(pages.len());
            for p in pages.iter().cloned() {
                let subtree = p.descendants(pool).await?;
                to_move.push((p, subtree));
            }
            let moved = to_move.len();
            let invalidator = _state.cache_invalidator.clone();
            let tenant_slug = tenant.org.slug.clone();
            let np_for_tx = new_parent.clone();
            crate::tree_ops::atomic_tree(pool, move |tx| {
                Box::pin(async move {
                    let mut purge: Vec<String> = Vec::new();
                    for (mut p, subtree) in to_move {
                        let old_url = p.url_path.clone();
                        let descendant_old_urls: Vec<String> =
                            subtree.iter().map(|d| d.url_path.clone()).collect();
                        // Moves the URLs with the tree (#706).
                        p.move_to_in_tx(np_for_tx.as_ref(), subtree, tx).await?;
                        purge.push(old_url);
                        purge.push(p.url_path.clone());
                        purge.extend(descendant_old_urls);
                    }
                    crate::cache_invalidate::invalidate_urls_on_commit(
                        invalidator,
                        tenant_slug,
                        purge,
                    );
                    Ok(())
                })
            })
            .await?;
            redirect_named_with_message_plural(
                "rcms-admin:pages:list",
                MsgLevel::Success,
                "Moved {count} page(s) under “{parent}”.",
                moved as i64,
                &[
                    ("count", &moved.to_string()),
                    (
                        "parent",
                        &new_parent.map(|p| p.title).unwrap_or_else(|| "Root".to_owned()),
                    ),
                ],
                &headers,
            )
        }
        "copy-to-locale" => {
            // #438 — simple-translation bulk: seed `cms_translation`
            // rows for each selected page from its canonical text, so
            // translators start from the source rather than a blank
            // form. Existing translations are left untouched.
            let code = form
                .iter()
                .find(|(k, _)| k == "locale")
                .map(|(_, v)| v.as_str())
                .unwrap_or("");
            let locale = Locale::objects()
                .where_(Locale::code.eq(code.to_owned()))
                .where_(Locale::active.eq(true))
                .first(pool)
                .await?;
            let Some(locale) = locale.filter(|l| !l.is_default) else {
                return redirect_named_with_message(
                    "rcms-admin:pages:list",
                    MsgLevel::Warning,
                    "Copy to locale needs a target locale (and not the default — canonical content already lives there).",
                    &headers,
                );
            };
            let locale_id = locale.id.get().copied().unwrap_or_default();
            let pages_for_tx = pages.clone();
            let (seeded_pages, seeded_fields) = rustango::atomic!(pool, |tx| {
                let mut guard = tx.lock().await?;
                let tx = &mut *guard;
                let mut sp = 0usize;
                let mut sf = 0usize;
                for p in &pages_for_tx {
                    let Some(pid) = p.id.get().copied() else {
                        continue;
                    };
                    // Field paths already translated for this locale —
                    // never overwrite a translator's work.
                    let existing: Vec<crate::translation::Translation> =
                        crate::translation::Translation::objects()
                            .where_(crate::translation::Translation::page_id.eq(pid))
                            .where_(crate::translation::Translation::locale_id.eq(locale_id))
                            .fetch_tx(tx)
                            .await?;
                    let have: std::collections::HashSet<String> =
                        existing.into_iter().map(|t| t.field_path).collect();
                    let mut touched = false;
                    for (path, value) in copy_fields_for(p) {
                        if have.contains(path) {
                            continue;
                        }
                        let mut row = crate::translation::Translation {
                            id: rustango::sql::Auto::Unset,
                            page_id: pid,
                            locale_id,
                            field_path: path.to_owned(),
                            value,
                            updated_at: rustango::sql::Auto::Unset,
                        };
                        row.save_tx(tx).await?;
                        sf += 1;
                        touched = true;
                    }
                    if touched {
                        sp += 1;
                    }
                }
                Ok::<_, rustango::sql::ExecError>((sp, sf))
            })
            .await?;
            // Two independent counts → compose localized noun-phrases into a
            // localized sentence template (#528/#1102).
            let fields = super::i18n::tr_plural(
                &headers,
                "{count} field(s)",
                seeded_fields as i64,
                &[("count", &seeded_fields.to_string())],
            );
            let pages_ph = super::i18n::tr_plural(
                &headers,
                "{count} page(s)",
                seeded_pages as i64,
                &[("count", &seeded_pages.to_string())],
            );
            redirect_named_with_message_args(
                "rcms-admin:pages:list",
                MsgLevel::Success,
                "Seeded {fields} across {pages} into “{locale}”. Existing translations were left untouched.",
                &[
                    ("fields", fields.as_str()),
                    ("pages", pages_ph.as_str()),
                    ("locale", locale.name.as_str()),
                ],
                &headers,
            )
        }
        "archive" | "unarchive" => {
            // #556 — archive: preserve superseded content (serve 200,
            // stay searchable, frozen against delete/unpublish).
            // Unarchive: revive to published. Descendants inherit the
            // treatment at render time, so the whole public subtree is
            // purged; with `archive_cascade` their own rows flip too.
            let archive = action == "archive";
            let plan =
                crate::page::plan_archive(pool, &pages, archive, archive_cascade).await?;
            let invalidator = _state.cache_invalidator.clone();
            let tenant_slug = tenant.org.slug.clone();
            let purge = plan.changed_urls.clone();
            let flips = plan.to_flip.clone();
            let touched = rustango::atomic!(pool, |tx| {
                let mut guard = tx.lock().await?;
                let tx = &mut *guard;
                let mut n = 0usize;
                for mut p in flips {
                    p.save_tx(tx).await?;
                    n += 1;
                }
                crate::cache_invalidate::invalidate_urls_on_commit(
                    invalidator,
                    tenant_slug,
                    purge,
                );
                Ok::<usize, rustango::sql::ExecError>(n)
            })
            .await?;
            // Bookkeeping outside the tx (best-effort): a revision per
            // flipped row + search-index sync (archived stays indexed;
            // the FTS path is query-time so this is a no-op there).
            for p in &plan.to_flip {
                crate::revision::capture(pool, p, None)
                    .await
                    .log_warn("revision not captured; the history has a gap");
                crate::task_queue::sync_page_search(&tenant.org.slug, p).await;
            }
            let key = if archive {
                "Archived {count} page(s)."
            } else {
                "Unarchived {count} page(s)."
            };
            redirect_named_with_message_plural(
                "rcms-admin:pages:list",
                MsgLevel::Success,
                key,
                touched as i64,
                &[("count", &touched.to_string())],
                &headers,
            )
        }
        other => Err(AdminError::Validation(format!(
            "unknown bulk action `{other}` (expected publish / unpublish / archive / unarchive / delete / move / copy-to-locale)"
        ))),
    }
}

/// POST /cms-admin/pages/{id}/unlock — force-release the editor lock.
/// Anyone with Edit permission on this page can call it. The
/// row is deleted and an audit-log entry is written so the action is
/// reversible to inspect.
/// POST /cms-admin/pages/{id}/lock-release — the editor was left: give
/// up our own lock right away, not 30 minutes later, so a colleague who
/// opens the page next can save. Only ever releases the caller's lock.
pub async fn page_lock_release(
    tenant: Tenant,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
) -> Result<Response, AdminError> {
    let Some(viewer_id) = session_user.as_ref().and_then(|u| u.id.get().copied()) else {
        return Ok(unauthorized_no_session());
    };
    crate::lock::release(tenant.pool(), id, viewer_id).await?;
    Ok((axum::http::StatusCode::NO_CONTENT, "").into_response())
}

pub async fn page_unlock_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
) -> Result<Response, AdminError> {
    let released = crate::lock::force_release(tenant.pool(), id).await?;
    let actor_id = session_user.as_ref().and_then(|u| u.id.get().copied());
    let message = if let Some(row) = released {
        let holder = rustango::tenancy::auth::User::objects()
            .where_(rustango::tenancy::auth::User::id.eq(row.user_id))
            .fetch(tenant.pool())
            .await
            .ok()
            .and_then(|v| v.into_iter().next())
            .map(|u| u.username)
            .unwrap_or_else(|| format!("user#{}", row.user_id));
        // #192 — audit log the forced unlock.
        crate::page_log::record_or_warn(
            tenant.pool(),
            id,
            crate::page_log::ACTION_FORCE_UNLOCK,
            actor_id,
            format!("Force-unlocked — “{holder}” no longer holds the editor lock."),
            Some(serde_json::json!({ "previous_holder_user_id": row.user_id })),
        )
        .await;
        format!("Lock released — “{holder}” no longer holds the editor lock on this page.")
    } else {
        "Lock was already released.".to_owned()
    };
    redirect_named_with_params_and_message(
        "rcms-admin:pages:edit",
        &[("id", id.to_string())],
        MsgLevel::Success,
        &message,
        &headers,
    )
}

/// POST /cms-admin/pages/{id}/subscribe — opt the current user in
/// to publish notifications for this page.
/// Idempotent — re-subscribing returns success without inserting a
/// duplicate row.
pub async fn page_subscribe_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
) -> Result<Response, AdminError> {
    let Some(viewer) = session_user.as_ref() else {
        return Ok(unauthorized_no_session());
    };
    let viewer_id = viewer.id.get().copied().unwrap_or_default();
    crate::page_subscription::subscribe(tenant.pool(), id, viewer_id).await?;
    redirect_named_with_params_and_message(
        "rcms-admin:pages:edit",
        &[("id", id.to_string())],
        MsgLevel::Success,
        "Subscribed — you'll get an email when this page publishes.",
        &headers,
    )
}

/// POST /cms-admin/pages/{id}/unsubscribe — opt the current user out
/// of publish notifications. No-op when not subscribed.
pub async fn page_unsubscribe_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
) -> Result<Response, AdminError> {
    let Some(viewer) = session_user.as_ref() else {
        return Ok(unauthorized_no_session());
    };
    let viewer_id = viewer.id.get().copied().unwrap_or_default();
    crate::page_subscription::unsubscribe(tenant.pool(), id, viewer_id).await?;
    redirect_named_with_params_and_message(
        "rcms-admin:pages:edit",
        &[("id", id.to_string())],
        MsgLevel::Success,
        "Unsubscribed — you won't get future publish notifications for this page.",
        &headers,
    )
}

/// True while the page is mid-review and `user` may not decide the
/// current step: the editor is read-only for them, so they must not
/// take (or keep) the edit lock — a submitter who reopens the page
/// would otherwise lock the reviewer out.
async fn read_only_under_review(
    pool: &rustango::sql::Pool,
    page_id: i64,
    user: &rustango::tenancy::auth::User,
) -> Result<bool, AdminError> {
    if user.is_superuser {
        return Ok(false);
    }
    let Some(state) = crate::workflow::active_state_for_page(pool, page_id).await? else {
        return Ok(false);
    };
    if state.status != crate::workflow::WorkflowStatus::InProgress.as_str() {
        return Ok(false);
    }
    let tasks = crate::workflow::tasks_for(pool, state.workflow_id).await?;
    let Some(task) = state
        .current_task_id
        .and_then(|tid| tasks.iter().find(|t| t.id.get().copied() == Some(tid)))
    else {
        return Ok(false);
    };
    let uid = user.id.get().copied().unwrap_or_default();
    Ok(!user_is_in_role(pool, uid, task.role_id).await?)
}

/// POST /cms-admin/pages/{id}/lock-heartbeat — refresh the TTL on
/// the caller's lock. Called every
/// `lock::HEARTBEAT_INTERVAL_SECS` from the page-editor JS while the
/// tab stays open. Returns:
///   * 204 — heartbeat accepted (lock refreshed, or first claim).
///   * 409 — someone else owns the lock; the editor JS should
///           reload to surface the banner.
/// Heartbeat query: editors send `?is_editing=1` once they've
/// touched the form so the presence banner can show "editing" vs
/// "viewing" for other viewers.
#[derive(Debug, Deserialize)]
pub struct HeartbeatQuery {
    #[serde(default)]
    pub is_editing: Option<String>,
}

pub async fn page_lock_heartbeat(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
    Query(q): Query<HeartbeatQuery>,
) -> Result<Response, AdminError> {
    let Some(viewer_id) = session_user.as_ref().and_then(|u| u.id.get().copied()) else {
        return Ok(unauthorized_no_session());
    };

    // #106 — touch the editing-session row on every ping. Stale
    // rows GC lazily here so we don't need a cron.
    let is_editing = q.is_editing.as_deref() == Some("1");
    if let Err(e) = crate::editing_session::touch(tenant.pool(), id, viewer_id, is_editing).await {
        tracing::warn!(target: "rustango_cms::admin", page_id = id, error = %e, "editing-session touch failed (continuing)");
    }
    // GC every ~50 heartbeats to keep the table bounded without
    // doing the SELECT on every call. Probabilistic — fine for v1.
    if rand::random::<u8>() < 5 {
        crate::editing_session::gc_stale(tenant.pool())
            .await
            .log_warn("stale editing sessions not collected");
    }

    // Read-only during someone else's review step: no lock to keep.
    if let Some(u) = session_user.as_ref() {
        if read_only_under_review(tenant.pool(), id, u).await? {
            return Ok((axum::http::StatusCode::NO_CONTENT, "").into_response());
        }
    }
    match crate::lock::acquire(tenant.pool(), id, viewer_id, false).await {
        Ok(crate::lock::AcquireOutcome::Acquired(_)) => {
            Ok((axum::http::StatusCode::NO_CONTENT, "").into_response())
        }
        Ok(crate::lock::AcquireOutcome::Held(_)) => Ok((
            axum::http::StatusCode::CONFLICT,
            "lock held by another editor",
        )
            .into_response()),
        Err(e) => {
            tracing::warn!(target: "rustango_cms::admin", page_id = id, error = %e, "lock heartbeat failed");
            Err(e.into())
        }
    }
}

/// GET /cms-admin/pages/{id}/sessions — JSON list of OTHER active
/// editors / viewers on this page. Returns
/// `[{user_id, username, last_seen_at, is_editing}, ...]`.
pub async fn page_sessions_json(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
) -> Result<Response, AdminError> {
    let Some(viewer_id) = session_user.as_ref().and_then(|u| u.id.get().copied()) else {
        return Ok(unauthorized_no_session());
    };
    let others = crate::editing_session::others_on_page(tenant.pool(), id, viewer_id)
        .await
        .unwrap_or_default();
    if others.is_empty() {
        return Ok(([(header::CONTENT_TYPE, "application/json")], "[]").into_response());
    }
    // Resolve usernames in one batched lookup.
    let user_ids: Vec<i64> = others.iter().map(|s| s.user_id).collect();
    let users: Vec<rustango::tenancy::auth::User> = rustango::tenancy::auth::User::objects()
        .where_(rustango::tenancy::auth::User::id.is_in(user_ids))
        .fetch(tenant.pool())
        .await
        .unwrap_or_default();
    let username_by_id: std::collections::HashMap<i64, String> = users
        .into_iter()
        .filter_map(|u| u.id.get().copied().map(|id| (id, u.username)))
        .collect();
    let payload: Vec<serde_json::Value> = others
        .iter()
        .map(|s| {
            serde_json::json!({
                "user_id": s.user_id,
                "username": username_by_id.get(&s.user_id).cloned(),
                "last_seen_at": s.last_seen_at,
                "is_editing": s.is_editing,
            })
        })
        .collect();
    let body = serde_json::to_string(&payload).unwrap_or_else(|_| "[]".to_owned());
    Ok(([(header::CONTENT_TYPE, "application/json")], body).into_response())
}

/// Pick a slug not already used by any sibling under `parent_id`.
/// Tries `desired`, then `desired-2`, `desired-3`, … up to 100
/// attempts. Returns the first available one.
async fn unique_sibling_slug(
    pool: &rustango::sql::Pool,
    parent_id: Option<i64>,
    desired: &str,
) -> Result<String, AdminError> {
    use rustango::core::Column as _;
    let qs = Page::objects().order_by(&[("slug", false)]);
    let siblings: Vec<Page> = match parent_id {
        Some(pid) => qs.where_(Page::parent_id.eq(pid)).fetch(pool).await?,
        None => qs.where_(Page::parent_id.is_null()).fetch(pool).await?,
    };
    let taken: std::collections::HashSet<String> = siblings.into_iter().map(|p| p.slug).collect();
    if !taken.contains(desired) {
        return Ok(desired.to_owned());
    }
    for n in 2..=100 {
        let candidate = format!("{desired}-{n}");
        if !taken.contains(&candidate) {
            return Ok(candidate);
        }
    }
    Err(AdminError::Validation(
        "Could not allocate a unique slug for the clone — over 100 siblings share the same base."
            .to_owned(),
    ))
}

/// POST /cms-admin/pages/{id}/revert/{rev_id} — replace the page's
/// mutable fields with those from the given revision, then capture a
/// NEW revision recording the revert. URL-shaped fields (slug, path,
/// depth, parent_id, sort_order) are NOT reverted — moving the page
/// is a tree-ops concern, not a content concern.
///
/// Returns to the edit form afterward so the editor can verify the
/// state and continue editing.
pub async fn page_revert_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path((id, rev_id)): Path<(i64, i64)>,
) -> Result<Response, AdminError> {
    let mut page = Page::objects()
        .where_(Page::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    let rev = crate::revision::Revision::objects()
        .where_(crate::revision::Revision::id.eq(rev_id))
        .where_(crate::revision::Revision::page_id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(rev_id))?;

    // Decode the snapshot into a Page shell. Tree-shape fields
    // (parent_id, path, depth, sort_order, slug, url_path) are
    // preserved from the LIVE row — a revert restores content +
    // status, not tree placement.
    let snapshot: Page = match serde_json::from_value(rev.snapshot.clone()) {
        Ok(p) => p,
        Err(e) => {
            return Ok((
                axum::http::StatusCode::BAD_REQUEST,
                format!("revision snapshot could not be decoded: {e}"),
            )
                .into_response());
        }
    };

    let old_url_path = page.url_path.clone();
    page.title = snapshot.title;
    page.page_type_id = snapshot.page_type_id;
    page.status = snapshot.status;
    page.published_at = snapshot.published_at;
    page.go_live_at = snapshot.go_live_at;
    page.expire_at = snapshot.expire_at;
    page.seo_title = snapshot.seo_title;
    page.seo_description = snapshot.seo_description;
    page.robots_index = snapshot.robots_index;
    page.sitemap_priority = snapshot.sitemap_priority;

    let mut tx = transaction_pool(tenant.pool()).await?;
    page.save_tx(&mut tx).await?;
    tx.commit().await?;
    crate::revision::capture(tenant.pool(), &page, None)
        .await
        .log_warn("revision not captured; the history has a gap");

    let _ = old_url_path; // TTL-based cache propagation per rustango::cache_page
                          // #192 — audit log the restore.
    let actor_id = session_user.as_ref().and_then(|u| u.id.get().copied());
    crate::page_log::record_or_warn(
        tenant.pool(),
        id,
        crate::page_log::ACTION_RESTORE,
        actor_id,
        format!("Reverted to revision #{}.", rev.sequence),
        Some(serde_json::json!({ "revision_id": rev_id, "sequence": rev.sequence })),
    )
    .await;
    let sequence_str = rev.sequence.to_string();
    redirect_named_with_params_and_message_args(
        "rcms-admin:pages:edit",
        &[("id", id.to_string())],
        MsgLevel::Success,
        "Reverted to revision #{sequence}.",
        &[("sequence", sequence_str.as_str())],
        &headers,
    )
}

// =====================================================================
// Live preview
// =====================================================================

/// Stamp the response with anti-cache headers so the editor's preview
/// iframe always renders the latest content. Browsers cache HTML
/// aggressively by default; the editor's "Refresh" button and the
/// initial load both need to hit fresh markup.
fn stamp_no_cache(mut response: Response) -> Response {
    let h = response.headers_mut();
    h.insert(
        header::CACHE_CONTROL,
        HeaderValue::from_static("no-store, no-cache, must-revalidate, max-age=0"),
    );
    h.insert(header::PRAGMA, HeaderValue::from_static("no-cache"));
    h.insert(header::EXPIRES, HeaderValue::from_static("0"));
    // Tell upstream caches the response varies per-request even
    // for the same URL (preview pulls live data on every load).
    h.insert(header::VARY, HeaderValue::from_static("*"));
    // Block embedding outside our own admin so other origins can't
    // frame the preview for clickjacking.
    h.insert(
        header::X_FRAME_OPTIONS,
        HeaderValue::from_static("SAMEORIGIN"),
    );
    response
}

/// Inject the axe-core preview-glue script into the
/// rendered preview HTML so the editor can surface client-side
/// accessibility findings alongside the server-side heuristics.
/// Reads the response body, locates the closing `</body>`
/// tag (case-insensitive), and splices two `<script src="…">`
/// tags before it: axe-core itself, then the small glue script
/// that calls `axe.run()` + `postMessage`s the results back to
/// the editor's parent window.
///
/// Non-HTML responses pass through unchanged; pages without a
/// `</body>` tag (malformed templates, plain-text responses) also
/// pass through so the injection is best-effort + non-breaking.
async fn inject_axe_into_preview(response: Response) -> Response {
    // Only touch text/html responses.
    let is_html = response
        .headers()
        .get(header::CONTENT_TYPE)
        .and_then(|v| v.to_str().ok())
        .is_some_and(|ct| ct.to_ascii_lowercase().contains("text/html"));
    if !is_html {
        return response;
    }
    let (parts, body) = response.into_parts();
    // 4 MiB cap matches the rendition route's preview-safe upper
    // bound; admin preview HTML should never approach this.
    let bytes = match axum::body::to_bytes(body, 4 * 1024 * 1024).await {
        Ok(b) => b,
        Err(_) => {
            // Body read failed — return a minimal 500 since we
            // already consumed the original.
            return (
                axum::http::StatusCode::INTERNAL_SERVER_ERROR,
                "preview body read failed",
            )
                .into_response();
        }
    };
    let body_str = match std::str::from_utf8(&bytes) {
        Ok(s) => s,
        Err(_) => {
            // Non-UTF-8 HTML — rebuild without injection.
            return axum::http::Response::from_parts(parts, axum::body::Body::from(bytes));
        }
    };
    let new_body = inject_axe_into_body_str(body_str);
    // Rebuild the response with the patched body + a refreshed
    // Content-Length so axum doesn't emit the stale value.
    let mut resp = axum::http::Response::from_parts(parts, axum::body::Body::from(new_body));
    resp.headers_mut().remove(header::CONTENT_LENGTH);
    resp
}

/// Pure string helper for [`inject_axe_into_preview`] — splice the
/// axe-core + glue `<script>` tags before the closing `</body>`
/// (case-insensitive `rfind`). Pages with no closing body tag get
/// the scripts appended at the end (browsers tolerate trailing
/// `<script>` after `</html>`). Extracted so the injection logic
/// is unit-testable without spinning up an axum Response stack.
fn inject_axe_into_body_str(body: &str) -> String {
    // Fingerprinted like every other admin asset (`?v=`): these are served
    // `immutable`, so an unversioned URL would let a year-old cached copy
    // mask an update.
    let v = super::cms_asset_version();
    let scripts = format!(
        "<script src=\"/cms-admin/static/vendor/axe-core.min.js?v={v}\" defer></script>\n\
         <script src=\"/cms-admin/static/cms-preview-guard.js?v={v}\" defer></script>\n\
         <script src=\"/cms-admin/static/cms-axe-preview.js?v={v}\" defer></script>\n"
    );
    let lower = body.to_ascii_lowercase();
    if let Some(idx) = lower.rfind("</body>") {
        let mut out = String::with_capacity(body.len() + scripts.len());
        out.push_str(&body[..idx]);
        out.push_str(&scripts);
        out.push_str(&body[idx..]);
        out
    } else {
        let mut out = String::with_capacity(body.len() + scripts.len());
        out.push_str(body);
        out.push_str(&scripts);
        out
    }
}

#[cfg(test)]
mod axe_injection_tests {
    use super::inject_axe_into_body_str;

    #[test]
    fn splices_before_lowercase_body_close() {
        let html = "<html><body><h1>hi</h1></body></html>";
        let out = inject_axe_into_body_str(html);
        assert!(out.contains("axe-core.min.js"), "axe script missing: {out}");
        assert!(
            out.contains("cms-axe-preview.js"),
            "glue script missing: {out}"
        );
        assert!(
            out.contains("cms-preview-guard.js"),
            "preview-guard script missing: {out}"
        );
        // Both scripts land BEFORE the body close.
        let close = out.find("</body>").expect("body close kept");
        assert!(
            out[..close].contains("axe-core.min.js"),
            "axe script should be before </body>: {out}"
        );
        assert!(
            out[..close].contains("cms-axe-preview.js"),
            "glue script should be before </body>: {out}"
        );
    }

    #[test]
    fn splices_before_uppercase_body_close() {
        let html = "<HTML><BODY><h1>hi</h1></BODY></HTML>";
        let out = inject_axe_into_body_str(html);
        // The original close tag stays uppercase; injection is still
        // positioned in front of it.
        let close = out.find("</BODY>").expect("uppercase body close kept");
        assert!(out[..close].contains("axe-core.min.js"));
    }

    #[test]
    fn appends_when_no_body_close() {
        let html = "<html><h1>hi</h1>";
        let out = inject_axe_into_body_str(html);
        // URLs carry a `?v=<fingerprint>` cache-bust token, so assert on the
        // script identity + trailing position rather than an exact URL.
        assert!(out.contains("cms-axe-preview.js?v="));
        assert!(out.trim_end().ends_with("defer></script>"));
        assert!(out.contains("axe-core.min.js"));
        // Original content preserved.
        assert!(out.starts_with("<html><h1>hi</h1>"));
    }

    #[test]
    fn uses_rfind_for_nested_body_close_text() {
        // A template that mentions "</body>" inside a comment / text
        // string and ALSO has a real closing tag — we want to splice
        // before the LAST one, not the first.
        let html = "<html><body><pre>literal: &lt;/body&gt;</pre></body></html>";
        let out = inject_axe_into_body_str(html);
        let close = out.rfind("</body>").unwrap();
        assert!(out[..close].contains("axe-core.min.js"));
        // Make sure the literal in the <pre> survived.
        assert!(out.contains("literal: &lt;/body&gt;"));
    }

    #[test]
    fn empty_body_renders_clean() {
        let out = inject_axe_into_body_str("");
        // Nothing to splice before; just the scripts.
        assert!(out.contains("axe-core.min.js"));
        assert!(out.contains("cms-axe-preview.js"));
    }
}

/// Query string for the preview endpoints — carries the editing locale
/// when the editor is in translation mode (`?locale=fr`) so the preview
/// renders that locale's translation instead of the canonical content.
#[derive(serde::Deserialize, Default)]
pub struct PreviewLocaleQuery {
    #[serde(default)]
    locale: Option<String>,
    /// `?format=json` — show the page's API representation instead of
    /// its rendered HTML. This is the editor's preview switch, and it
    /// works for **any** page type, not just ones whose `view_mode`
    /// opts into a public JSON view: an authenticated editor asking to
    /// inspect the API shape is a different act from an anonymous
    /// client negotiating for it.
    #[serde(default)]
    format: Option<String>,
}

/// The Tera a preview must render through: the tenant's, not the global set.
///
/// The public renderer resolves templates per tenant (`router.rs` →
/// [`crate::tenant_templates::TenantTemplates::for_tenant`]), so a
/// preview built from `state.tera` shows a page the visitor will never
/// get. Three ways it goes wrong, all silent: a tenant's override of a
/// shared template is ignored; a `Page::template_override` naming a
/// template that exists only as an override fails `pick_template`'s
/// existence check and falls back to the page type's default; and a
/// block whose template is a tenant override renders its
/// "missing template" fallback.
///
/// Falls back to the global set when no overlay is installed, which is
/// every single-template-set deployment.
fn preview_tera(state: &super::AdminState, tenant: &Tenant) -> std::sync::Arc<tera::Tera> {
    crate::tenant_templates::installed()
        .map(|tt| tt.for_tenant(&tenant.org.slug))
        .unwrap_or_else(|| state.tera.clone())
}

/// GET /cms-admin/pages/{id}/preview — renders the saved page through
/// the public render pipeline (no admin chrome) so the editor's
/// iframe shows exactly what a visitor would see. Anti-cache headers
/// ensure the iframe always pulls fresh markup. `?locale=` (translation
/// mode) renders that locale's saved translation.
pub async fn page_preview(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    Path(id): Path<i64>,
    axum::extract::Query(q): axum::extract::Query<PreviewLocaleQuery>,
) -> Result<Response, AdminError> {
    let page = Page::objects()
        .where_(Page::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    let response = crate::render::render(
        &tenant,
        &preview_tera(&state, &tenant),
        &page,
        "",
        // No site prefix: this path renders the tenant-absolute
        // tree, so links must stay absolute.
        "",
        q.locale.as_deref(),
        None,
        None,
    )
    .await;
    // #208 — splice axe-core + the iframe-glue script before the
    // closing </body> so the editor's accessibility panel can
    // surface client-side findings alongside the server-side ones.
    Ok(stamp_no_cache(inject_axe_into_preview(response).await))
}

/// GET /cms-admin/pages/{id}/preview-token — mint a short-lived
/// signed preview token so a decoupled / headless frontend can fetch
/// this page's DRAFT via `/api/v2/pages/{id}/?preview_token=…`. Returns
/// JSON `{token, expires_at, api_path}`. Auth is enforced by the admin
/// router's login guard.
pub async fn page_preview_token(
    tenant: Tenant,
    Path(id): Path<i64>,
) -> Result<Response, AdminError> {
    let exists = Page::objects()
        .where_(Page::id.eq(id))
        .first(tenant.pool())
        .await?
        .is_some();
    if !exists {
        return Err(AdminError::NotFound(id));
    }
    // 1-hour token: long enough to click through + iterate, short
    // enough to bound exposure of an unpublished page.
    let expires = chrono::Utc::now().timestamp() + PREVIEW_TOKEN_TTL_SECS;
    let token = crate::preview_token::mint(&tenant.org.slug, id, expires);
    Ok(Json(serde_json::json!({
        "token": token,
        "expires_at": expires,
        "api_path": format!("/api/v2/pages/{id}/?preview_token={token}"),
    }))
    .into_response())
}

/// POST /cms-admin/pages/{id}/preview — same as the GET variant, but
/// renders a *virtual* page assembled from the posted form values so
/// the editor can preview unsaved changes. Nothing is persisted. Anti-cache
/// headers + same-origin frame policy mirror the GET handler.
pub async fn page_preview_draft(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    Path(id): Path<i64>,
    axum::extract::Query(q): axum::extract::Query<PreviewLocaleQuery>,
    headers: HeaderMap,
    Form(form_map): Form<std::collections::HashMap<String, String>>,
) -> Result<Response, AdminError> {
    let mut page = Page::objects()
        .where_(Page::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;

    // Overlay the canonical form fields onto the virtual page. The
    // tree placement (parent / path / url_path) stays as-saved so
    // breadcrumb + ancestor lookups still work.
    if let Some(v) = form_map.get("title") {
        page.title = v.clone();
    }
    if let Some(v) = form_map.get("slug") {
        page.slug = v.clone();
    }
    if let Some(v) = form_map
        .get("page_type_id")
        .and_then(|s| s.parse::<i64>().ok())
    {
        page.page_type_id = v;
    }
    if let Some(v) = form_map.get("status") {
        page.status = v.clone();
    }
    if let Some(v) = form_map.get("seo_title") {
        page.seo_title = v.clone();
    }
    if let Some(v) = form_map.get("seo_description") {
        page.seo_description = v.clone();
    }
    // Checkboxes submit `on` when checked, absent when unchecked.
    page.robots_index = form_map
        .get("robots_index")
        .map(|v| matches!(v.as_str(), "on" | "true" | "1"))
        .unwrap_or(page.robots_index);
    if let Some(v) = form_map
        .get("sitemap_priority")
        .and_then(|s| s.parse::<f32>().ok())
    {
        page.sitemap_priority = v;
    }
    if let Some(raw) = form_map.get("theme_id") {
        page.theme_id = if raw.is_empty() || raw == "0" {
            None
        } else {
            raw.parse::<i64>().ok()
        };
    }

    // The editor's JSON pane sends `Accept: application/json`, so the
    // same renderer that answers the public URL answers the preview —
    // which is what makes the pane show the real response body rather
    // than an admin-side reconstruction of it.
    let accept = crate::page_view::parse_accept(
        headers
            .get(axum::http::header::ACCEPT)
            .and_then(|v| v.to_str().ok()),
    );
    let force_json = q.format.as_deref() == Some("json");
    let response = crate::render::render_preview(
        &tenant,
        &preview_tera(&state, &tenant),
        &page,
        "",
        // Preview renders tenant-absolute paths — see the GET twin.
        "",
        q.locale.as_deref(),
        &form_map,
        accept,
        force_json,
    )
    .await;
    // #208 — same axe splice as the GET preview handler above. It gates
    // on `text/html`, so a JSON preview passes through untouched and
    // never pulls axe-core into the pane.
    Ok(stamp_no_cache(inject_axe_into_preview(response).await))
}

// =====================================================================
// Library tab
// =====================================================================

/// GET /cms-admin/library — placeholder until a real
/// `register_library_type!` macro lands. Shows the registered
/// PageTypeHandlers as a stand-in so the chrome demos cleanly.
/// Query for the paginated, filtered, folder-scoped snippet list.
#[derive(Debug, Deserialize, Default)]
pub struct LibraryListQuery {
    /// When set, focuses the list to one library type. When absent,
    /// the overview (one card per registered type with counts) shows.
    #[serde(default)]
    pub r#type: Option<String>,
    /// Folder prefix to scope the list to. Empty / unset = root.
    #[serde(default)]
    pub folder: Option<String>,
    /// Free-text query — matched against the type handler's
    /// `search_fields()`.
    #[serde(default)]
    pub q: Option<String>,
    /// 1-based page number.
    #[serde(default)]
    pub page: Option<u32>,
    /// Rows per page. Named so it is consumed here rather than falling
    /// into `raw_filters` and being read as a filter chip.
    #[serde(default)]
    pub per_page: Option<usize>,
    /// Catch-all bucket for `filter_<key>=<value>` filter chips. Any
    /// query parameter starting with `filter_` lands here.
    #[serde(flatten)]
    pub raw_filters: std::collections::HashMap<String, String>,
}

pub async fn library_list(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Query(q): Query<LibraryListQuery>,
    axum::extract::RawQuery(raw_query): axum::extract::RawQuery,
) -> Result<Response, AdminError> {
    // Sidebar / overview data — all types + total counts. Forms are excluded:
    // they have their own dedicated area (`/cms-admin/forms`, sidebar "Forms")
    // with per-form submission counts, so listing them here would be a second,
    // weaker path to the same thing. Direct `?type=form` URLs still work.
    let types: Vec<crate::library::LibraryType> = crate::library::LibraryType::objects()
        .order_by(&[("type_name", false)])
        .fetch(tenant.pool())
        .await?
        .into_iter()
        .filter(|t| t.type_name != "form")
        .collect();
    let all_snippets: Vec<crate::snippet::Snippet> = crate::snippet::Snippet::objects()
        .fetch(tenant.pool())
        .await?;
    let mut counts_by_type: std::collections::HashMap<String, i64> =
        std::collections::HashMap::new();
    for s in &all_snippets {
        *counts_by_type.entry(s.type_name.clone()).or_default() += 1;
    }

    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "library", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    // #316 — raw query string for the `| querystring(page=…)` filter in
    // the pagination links (preserves type/folder/q/filter_* + sets page).
    ctx.insert("query_string", &raw_query.unwrap_or_default());
    ctx.insert("types", &types);
    ctx.insert("counts_by_type", &counts_by_type);

    // Overview mode — no `?type=`, show one card per registered type.
    let Some(type_name) = q.r#type.as_deref().filter(|s| !s.is_empty()) else {
        ctx.insert("mode", "overview");
        return render_with_csrf(&state, &headers, "rcms_admin/library_list.html", &mut ctx);
    };

    // Resolve the handler so we can call list_display / search_fields
    // / list_filter / render_cell.
    let handler = crate::library::find_handler(type_name);
    let list_display = handler
        .as_ref()
        .map(|h| h.list_display())
        .unwrap_or_else(|| {
            vec![
                crate::library::ListColumn::Field {
                    column: "title",
                    label: "Title",
                },
                crate::library::ListColumn::Field {
                    column: "slug",
                    label: "Slug",
                },
                crate::library::ListColumn::Field {
                    column: "updated_at",
                    label: "Updated",
                },
            ]
        });
    let search_fields = handler
        .as_ref()
        .map(|h| h.search_fields())
        .unwrap_or_else(|| vec!["title", "slug"]);
    let filter_keys = handler
        .as_ref()
        .map(|h| h.list_filter())
        .unwrap_or_default();

    // Folder scoping. Snippets with `folder_path = <current>` are
    // direct contents; we also surface the immediate sub-folder
    // names so the user can drill in.
    let folder = crate::snippet::normalize_folder(q.folder.as_deref().unwrap_or(""));
    let child_folders = crate::snippet::child_folders(tenant.pool(), &folder)
        .await
        .unwrap_or_default();

    // Apply filters — type, folder, then per-data.<key> chips +
    // per-row `can_view` (#21 phase 1) so handler-supplied hooks
    // suppress rows from the list.
    let mut filtered: Vec<crate::snippet::Snippet> = all_snippets
        .iter()
        .filter(|s| s.type_name == type_name && s.folder_path == folder)
        .filter(|s| handler.as_ref().map(|h| h.can_view(s)).unwrap_or(true))
        .cloned()
        .collect();

    // Search — ILIKE pattern on each search_field, ANY match wins.
    let search_q = q.q.as_deref().map(|s| s.trim()).filter(|s| !s.is_empty());
    if let Some(needle) = search_q {
        let needle_lower = needle.to_lowercase();
        filtered.retain(|s| {
            search_fields.iter().any(|field| {
                let hay = match *field {
                    "title" => s.title.to_lowercase(),
                    "slug" => s.slug.to_lowercase(),
                    "body_markdown" => s.body_markdown.to_lowercase(),
                    other => s
                        .data
                        .get(other)
                        .and_then(serde_json::Value::as_str)
                        .map(str::to_lowercase)
                        .unwrap_or_default(),
                };
                hay.contains(&needle_lower)
            })
        });
    }

    // `filter_<key>=<value>` chips — flatten raw_filters into a
    // typed map first so we don't carry useless keys into context.
    let mut active_filters: std::collections::HashMap<String, String> =
        std::collections::HashMap::new();
    for (k, v) in &q.raw_filters {
        if let Some(stripped) = k.strip_prefix("filter_") {
            if !v.is_empty() {
                active_filters.insert(stripped.to_owned(), v.clone());
            }
        }
    }
    if !active_filters.is_empty() {
        filtered.retain(|s| {
            active_filters.iter().all(|(key, want)| {
                let got = s
                    .data
                    .get(key)
                    .map(|v| match v {
                        serde_json::Value::String(s) => s.clone(),
                        other => other.to_string(),
                    })
                    .unwrap_or_default();
                got == *want
            })
        });
    }

    // Filter chip options (distinct values per filterable key).
    let filter_options: std::collections::HashMap<String, Vec<String>> = filter_keys
        .iter()
        .map(|k| {
            let mut opts: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
            for s in &all_snippets {
                if s.type_name != type_name {
                    continue;
                }
                if let Some(v) = s.data.get(*k) {
                    let s = match v {
                        serde_json::Value::String(s) => s.clone(),
                        other => other.to_string(),
                    };
                    if !s.is_empty() {
                        opts.insert(s);
                    }
                }
            }
            ((*k).to_owned(), opts.into_iter().collect())
        })
        .collect();

    // Pagination — driven by `rustango::pagination::Paginator`.
    // `get_page()` never errors; out-of-range numbers clamp to the
    // last page (or 1 when empty). The slice indices come from
    // `start_index() - 1 .. end_index()` (the paginator returns
    // 1-based inclusive indices for "Showing X–Y" UI lines).
    let total = filtered.len();
    let page_size = crate::admin::pagination::clamp_per_page(q.per_page);
    let paginator = rustango::pagination::Paginator::new(total, page_size);
    let requested_page = q.page.unwrap_or(1).max(1) as i64;
    let page_obj = paginator.get_page(requested_page);
    let page = page_obj.number;
    let total_pages = paginator.num_pages().max(1);
    let page_rows: Vec<crate::snippet::Snippet> = if total == 0 {
        Vec::new()
    } else {
        let start = page_obj.start_index().saturating_sub(1);
        let end = page_obj.end_index();
        filtered[start..end].to_vec()
    };

    // Pre-render each row's cells so the template doesn't need to
    // know about ListColumn variants. Each row → Vec<String> of
    // pre-escaped HTML strings (cells), aligned with `column_headers`.
    let column_headers: Vec<serde_json::Value> = list_display
        .iter()
        .map(|c| {
            serde_json::json!({
                "key": c.key(),
                "label": c.label(),
            })
        })
        .collect();
    let rows_html: Vec<serde_json::Value> = page_rows
        .iter()
        .map(|row| {
            let cells: Vec<String> = list_display
                .iter()
                .map(|c| render_list_cell(c, row, handler.as_deref()))
                .collect();
            serde_json::json!({
                "id": row.id.get().copied().unwrap_or_default(),
                "slug": row.slug,
                "cells": cells,
            })
        })
        .collect();

    let active_filters_json: Vec<serde_json::Value> = active_filters
        .iter()
        .map(|(k, v)| serde_json::json!({ "key": k, "value": v }))
        .collect();

    // Handler-declared bulk + row actions (#21 phase 2). Serialize
    // the structs into the JSON shape the template iterates.
    let bulk_actions: Vec<serde_json::Value> = handler
        .as_ref()
        .map(|h| h.bulk_actions())
        .unwrap_or_default()
        .iter()
        .map(serialize_bulk_action)
        .collect();
    let row_actions: Vec<serde_json::Value> = handler
        .as_ref()
        .map(|h| h.row_actions())
        .unwrap_or_default()
        .iter()
        .map(serialize_bulk_action)
        .collect();

    // #134 — plugin-contributed bulk actions targeting `cms_snippet`.
    let plugin_bulk_actions: Vec<serde_json::Value> = crate::hooks::bulk_actions_for("cms_snippet")
        .into_iter()
        .map(|a| {
            serde_json::json!({
                "key": a.key,
                "label": a.label,
                "icon": a.icon,
                "danger": a.danger,
                "confirm_message": a.confirm_message,
                "action_url": a.action_url,
            })
        })
        .collect();

    // #196 — full folder tree for the left rail. Flat list of every
    // folder_path (including derived intermediates), sorted; the
    // template renders each row with depth-based indentation based
    // on the slash count.
    let folder_tree_paths = crate::snippet::folder_tree_for_type(tenant.pool(), type_name)
        .await
        .unwrap_or_default();
    let folder_tree: Vec<serde_json::Value> = folder_tree_paths
        .iter()
        .map(|p| {
            let depth = p.matches('/').count(); // root="" → 0; "a/" → 1; "a/b/" → 2
            let display = if p.is_empty() {
                "Root".to_owned()
            } else {
                // Last non-empty segment is the displayed name.
                p.trim_end_matches('/')
                    .rsplit('/')
                    .next()
                    .unwrap_or("")
                    .to_owned()
            };
            serde_json::json!({
                "path": p,
                "name": display,
                "depth": depth,
                "active": *p == folder,
            })
        })
        .collect();

    ctx.insert("mode", "type");
    ctx.insert("active_type", type_name);
    ctx.insert("folder", &folder);
    ctx.insert("child_folders", &child_folders);
    ctx.insert("folder_tree", &folder_tree);
    ctx.insert("column_headers", &column_headers);
    ctx.insert("rows", &rows_html);
    ctx.insert("plugin_bulk_actions", &plugin_bulk_actions);
    ctx.insert("page", &page);
    ctx.insert("total_pages", &total_pages);
    // Shared control row (`rcms_admin/_pagination.html`); the bespoke
    // vars above stay for the rest of the template.
    ctx.insert(
        "pagination",
        &crate::admin::pagination::context(page as i64, page_size, total),
    );
    ctx.insert("total", &total);
    ctx.insert("page_size", &page_size);
    ctx.insert("search_q", &search_q.unwrap_or(""));
    ctx.insert("filter_keys", &filter_keys);
    ctx.insert("filter_options", &filter_options);
    ctx.insert("active_filters", &active_filters_json);
    ctx.insert("bulk_actions", &bulk_actions);
    ctx.insert("row_actions", &row_actions);
    render_with_csrf(&state, &headers, "rcms_admin/library_list.html", &mut ctx)
}

/// Bulk-action button shape the library_list template iterates.
fn serialize_bulk_action(a: &crate::library::BulkAction) -> serde_json::Value {
    use crate::library::BulkActionStyle;
    let style_class = match a.style {
        BulkActionStyle::Primary => "btn",
        BulkActionStyle::Outlined => "btn btn-outlined",
        BulkActionStyle::Danger => "btn btn-danger",
    };
    serde_json::json!({
        "name": a.name,
        "label": a.label,
        "icon": a.icon,
        "confirm_message": a.confirm_message,
        "style_class": style_class,
    })
}

/// POST /cms-admin/library/{type}/bulk — dispatch a bulk action
/// to the registered LibraryTypeHandler. Body shape:
/// `action=<name>&ids=<id>&ids=<id>...`. Returns the editor back
/// to the type's list view with a flash message reporting how many
/// rows the handler processed.
pub async fn library_bulk_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Path(type_name): Path<String>,
    Form(form): Form<Vec<(String, String)>>,
) -> Result<Response, AdminError> {
    let action = form
        .iter()
        .find(|(k, _)| k == "action")
        .map(|(_, v)| v.clone())
        .unwrap_or_default();
    let ids: Vec<i64> = form
        .iter()
        .filter(|(k, _)| k == "ids" || k == "id")
        .filter_map(|(_, v)| v.parse::<i64>().ok())
        .collect();
    if action.is_empty() || ids.is_empty() {
        return redirect_named_with_message(
            "rcms-admin:library:list",
            MsgLevel::Warning,
            "Pick at least one row + a bulk action before submitting.",
            &headers,
        );
    }
    let Some(handler) = crate::library::find_handler(&type_name) else {
        return Err(AdminError::Validation(format!(
            "unknown library type `{type_name}`"
        )));
    };
    // Validate that the named action is in the handler's declared
    // set — guards against forged form posts.
    let declared: std::collections::HashSet<&'static str> = handler
        .bulk_actions()
        .iter()
        .chain(handler.row_actions().iter())
        .map(|a| a.name)
        .collect();
    if !declared.contains(action.as_str()) {
        return Err(AdminError::Validation(format!(
            "library type `{type_name}` doesn't declare bulk action `{action}`"
        )));
    }
    let touched = handler
        .handle_bulk_action(tenant.pool(), &action, &ids)
        .await?;
    let count = touched.to_string();
    redirect_named_with_message_plural(
        "rcms-admin:library:list",
        MsgLevel::Success,
        "{action}: touched {count} row(s) in `{type_name}`.",
        touched as i64,
        &[
            ("action", action.as_str()),
            ("count", count.as_str()),
            ("type_name", type_name.as_str()),
        ],
        &headers,
    )
}

/// Render one cell for a [`ListColumn`] / row pair into pre-escaped
/// HTML. Handles type-aware formatting: booleans → ✓/✗, datetimes →
/// 10-char prefix, FK-ish text → escape, JSON lookups → kind-driven
/// formatting, method fields → handler-supplied HTML (trusted —
/// caller is responsible for escaping).
fn render_list_cell(
    col: &crate::library::ListColumn,
    row: &crate::snippet::Snippet,
    handler: Option<&dyn crate::library::LibraryTypeHandler>,
) -> String {
    use crate::library::{ListColumn, ListColumnKind};
    match col {
        ListColumn::Field { column, .. } => match *column {
            "id" => row.id.get().copied().unwrap_or_default().to_string(),
            "type_name" => html_escape(&row.type_name),
            "slug" => format!("<code>{}</code>", html_escape(&row.slug)),
            "folder_path" => html_escape(&row.folder_path),
            "title" => format!("<strong>{}</strong>", html_escape(&row.title)),
            "body_markdown" => {
                let preview: String = row.body_markdown.chars().take(80).collect();
                format!("<span>{}</span>", html_escape(&preview))
            }
            "created_at" => row
                .created_at
                .get()
                .map(|d| d.format("%Y-%m-%d").to_string())
                .unwrap_or_default(),
            "updated_at" => row
                .updated_at
                .get()
                .map(|d| d.format("%Y-%m-%d").to_string())
                .unwrap_or_default(),
            other => format!("<em>(unknown field: {})</em>", html_escape(other)),
        },
        ListColumn::JsonField { key, kind, .. } => {
            let val = row.data.get(*key);
            match (kind, val) {
                (ListColumnKind::Bool, Some(serde_json::Value::Bool(b))) => bool_checkbox_html(*b),
                (_, Some(serde_json::Value::Bool(b))) => bool_checkbox_html(*b),
                (ListColumnKind::Integer, Some(serde_json::Value::Number(n)))
                | (ListColumnKind::Float, Some(serde_json::Value::Number(n))) => {
                    html_escape(&n.to_string())
                }
                (_, Some(serde_json::Value::String(s))) => html_escape(s),
                (_, Some(other)) => html_escape(&other.to_string()),
                (_, None) => String::new(),
            }
        }
        ListColumn::Method { name, .. } | ListColumn::Computed { name, .. } => handler
            .map(|h| h.render_cell(name, row))
            .unwrap_or_default(),
    }
}

fn html_escape(s: &str) -> String {
    s.replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
}

fn bool_checkbox_html(b: bool) -> String {
    if b {
        r#"<span class="material-symbols-rounded sm filled" style="color: var(--accent);" aria-label="true">check_box</span>"#.to_owned()
    } else {
        r#"<span class="material-symbols-rounded sm" style="color: var(--muted-2);" aria-label="false">check_box_outline_blank</span>"#.to_owned()
    }
}

/// GET /cms-admin/library/new?type=<type_name> — render the create
/// form for a new snippet. The type pre-selects from the query string
/// so the "+ New <Type>" CTA on the list page lands here.
#[derive(Debug, Deserialize)]
pub struct SnippetNewQuery {
    #[serde(default)]
    pub r#type: Option<String>,
    #[serde(default)]
    pub folder: Option<String>,
}

pub async fn snippet_new_form(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Query(q): Query<SnippetNewQuery>,
) -> Result<Response, AdminError> {
    let types: Vec<crate::library::LibraryType> = crate::library::LibraryType::objects()
        .order_by(&[("type_name", false)])
        .fetch(tenant.pool())
        .await?;
    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "library", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("mode", "new");
    ctx.insert("types", &types);
    ctx.insert("preselect_type", &q.r#type.unwrap_or_default());
    let folder = q.folder.unwrap_or_default();
    ctx.insert(
        "snippet",
        &serde_json::json!({
            "type_name": "",
            "slug": "",
            "title": "",
            "body_markdown": "",
            "folder_path": folder,
        }),
    );
    render_with_csrf(&state, &headers, "rcms_admin/snippet_form.html", &mut ctx)
}

pub async fn snippet_edit_form(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
    Query(q): Query<EditQuery>,
) -> Result<Response, AdminError> {
    let snippet = crate::snippet::Snippet::objects()
        .where_(crate::snippet::Snippet::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    // FB-04 (#537) — forms are edited in the dedicated visual builder, not
    // the generic snippet form. The list's Edit link lands here; bounce to
    // the builder so the entry point "just works".
    if snippet.type_name == "form" {
        let url = rustango::urls::reverse_owned(
            "rcms-admin:forms:build",
            &std::collections::HashMap::from([("id".to_owned(), id.to_string())]),
        )
        .map_err(|e| AdminError::Validation(format!("url reversal failed: {e}")))?;
        return Ok(axum::response::Redirect::to(&url).into_response());
    }
    // Permission gate (#21 phase 3) — `can_view = false` denies
    // the edit page entirely (404 rather than 403 to avoid leaking
    // existence). `can_edit = false` lets the page render read-only
    // — we surface it via a `read_only` context var.
    let handler = crate::library::find_handler(&snippet.type_name);
    if let Some(h) = handler.as_ref() {
        if !h.can_view(&snippet) {
            return Err(AdminError::NotFound(id));
        }
    }
    let read_only = handler
        .as_ref()
        .map(|h| !h.can_edit(&snippet))
        .unwrap_or(false);
    let can_delete = handler
        .as_ref()
        .map(|h| h.can_delete(&snippet))
        .unwrap_or(true);
    // Optional custom edit template — falls back to the default
    // `snippet_form.html` when not overridden.
    let template_path = handler
        .as_ref()
        .and_then(|h| h.edit_template())
        .unwrap_or("rcms_admin/snippet_form.html");

    let types: Vec<crate::library::LibraryType> = crate::library::LibraryType::objects()
        .order_by(&[("type_name", false)])
        .fetch(tenant.pool())
        .await?;
    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "library", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("mode", "edit");
    ctx.insert("types", &types);
    ctx.insert("snippet", &snippet);
    ctx.insert("read_only", &read_only);
    ctx.insert("can_delete", &can_delete);
    // Types that let each element name its own template get the field;
    // for every other type the concept doesn't exist and the form
    // shouldn't imply it does.
    ctx.insert(
        "allows_element_template",
        &handler
            .as_ref()
            .map(|h| h.allows_element_template())
            .unwrap_or(false),
    );
    // #123 — surface revision history + preview opt-in flags so the
    // edit template can render a History card + Preview link only
    // for types that opted in.
    let revisions_enabled = handler.as_ref().is_some_and(|h| h.revisions_enabled());
    let preview_enabled = handler.as_ref().is_some_and(|h| h.preview_enabled());
    let revisions: Vec<crate::snippet::SnippetRevision> = if revisions_enabled {
        crate::snippet::revisions_for(tenant.pool(), id)
            .await
            .unwrap_or_default()
    } else {
        Vec::new()
    };
    ctx.insert("revisions_enabled", &revisions_enabled);
    ctx.insert("preview_enabled", &preview_enabled);
    ctx.insert("snippet_revisions", &revisions);
    // #409 — snippet translation mode. `?locale=<code>` (a non-default
    // active locale) shows a per-field translation editor beside the
    // canonical content, mirroring the page translate flow.
    let locales: Vec<crate::locale::Locale> = crate::locale::Locale::objects()
        .where_(crate::locale::Locale::active.eq(true))
        .order_by(&[("sort_order", false), ("code", false)])
        .fetch(tenant.pool())
        .await?;
    let editing_locale = q
        .locale
        .as_deref()
        .filter(|c| !c.is_empty())
        .and_then(|code| locales.iter().find(|l| l.code == code).cloned());
    let translation_mode = editing_locale.as_ref().is_some_and(|l| !l.is_default);
    let mut core_translations: std::collections::HashMap<String, String> =
        std::collections::HashMap::new();
    let mut extra_translations: Vec<(String, String)> = Vec::new();
    if translation_mode {
        let lid = editing_locale
            .as_ref()
            .and_then(|l| l.id.get().copied())
            .unwrap_or_default();
        let existing = crate::snippet_translation::fetch_for(tenant.pool(), id, lid)
            .await
            .unwrap_or_default();
        let core_keys = ["title", "body_markdown"];
        core_translations = core_keys
            .iter()
            .map(|k| {
                (
                    (*k).to_owned(),
                    existing.get(*k).cloned().unwrap_or_default(),
                )
            })
            .collect();
        extra_translations = existing
            .iter()
            .filter(|(k, _)| !core_keys.contains(&k.as_str()))
            .map(|(k, v)| (k.clone(), v.clone()))
            .collect();
    }
    ctx.insert("locales", &locales);
    ctx.insert("editing_locale", &editing_locale);
    ctx.insert("translation_mode", &translation_mode);
    ctx.insert("core_translations", &core_translations);
    ctx.insert("extra_translations", &extra_translations);
    render_with_csrf(&state, &headers, template_path, &mut ctx)
}

/// POST `/cms-admin/library/{id}/translate?locale=<code>` —
/// upsert per-field snippet translations for a non-default locale.
/// Mirrors [`page_translate_submit`]: `tr__<field>` keys edit existing
/// fields, `new_field_path` + `new_field_value` adds one; an empty
/// value deletes the row (canonical shows through). Atomic batch.
pub async fn snippet_translate_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    Path(id): Path<i64>,
    Query(q): Query<EditQuery>,
    Form(form): Form<std::collections::HashMap<String, String>>,
) -> Result<Response, AdminError> {
    use crate::snippet_translation::SnippetTranslation;
    let code = q
        .locale
        .as_deref()
        .filter(|c| !c.is_empty())
        .ok_or_else(|| AdminError::Validation("translate requires ?locale=".to_owned()))?;
    let locale = crate::locale::Locale::objects()
        .where_(crate::locale::Locale::code.eq(code.to_owned()))
        .where_(crate::locale::Locale::active.eq(true))
        .first(tenant.pool())
        .await?
        .ok_or_else(|| AdminError::Validation(format!("unknown locale `{code}`")))?;
    if locale.is_default {
        return Ok((
            axum::http::StatusCode::BAD_REQUEST,
            "cannot translate INTO the default locale; canonical content lives on cms_snippet",
        )
            .into_response());
    }
    let locale_id = locale
        .id
        .get()
        .copied()
        .ok_or_else(|| AdminError::Validation("locale row missing id".to_owned()))?;
    // Verify the snippet exists.
    let snippet = crate::snippet::Snippet::objects()
        .where_(crate::snippet::Snippet::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;

    // Collect `tr__<field>` edits + an optional new-field row.
    let mut updates: Vec<(String, String)> = Vec::new();
    for (k, v) in &form {
        if let Some(path) = k.strip_prefix("tr__") {
            if !path.is_empty() {
                updates.push((path.to_owned(), v.clone()));
            }
        }
    }
    let new_path_raw = form.get("new_field_path").map(String::as_str).unwrap_or("");
    if !new_path_raw.trim().is_empty() {
        // Reuse the model's validator; silently skip a malformed path
        // rather than 400 the whole batch.
        if let Ok(path) = crate::snippet_translation::validate_field_path(new_path_raw) {
            let value = form.get("new_field_value").cloned().unwrap_or_default();
            updates.push((path, value));
        }
    }

    // Empty value → delete; non-empty → upsert. Atomic batch.
    rustango::atomic!(tenant.pool(), |tx| {
        let mut guard = tx.lock().await?;
        let tx = &mut *guard;
        for (path, value) in updates {
            let existing: Vec<SnippetTranslation> = SnippetTranslation::objects()
                .where_(SnippetTranslation::snippet_id.eq(id))
                .where_(SnippetTranslation::locale_id.eq(locale_id))
                .where_(SnippetTranslation::field_path.eq(path.clone()))
                .fetch_tx(tx)
                .await?;
            if value.is_empty() {
                for row in existing {
                    row.delete_tx(tx).await?;
                }
            } else if let Some(mut row) = existing.into_iter().next() {
                row.value = value;
                row.save_tx(tx).await?;
            } else {
                let mut row = SnippetTranslation {
                    id: rustango::sql::Auto::Unset,
                    snippet_id: id,
                    locale_id,
                    field_path: path,
                    value,
                    updated_at: rustango::sql::Auto::Unset,
                };
                row.save_tx(tx).await?;
            }
        }
        Ok::<_, rustango::sql::ExecError>(())
    })
    .await?;

    // A form is translated on its own screen; its library edit page
    // forwards to the builder and would drop the language.
    let back = if snippet.type_name == "form" {
        format!("/cms-admin/forms/{id}/build?locale={code}")
    } else {
        format!("/cms-admin/library/{id}/edit?locale={code}")
    };
    Ok(Redirect::to(&back).into_response())
}

/// POST /cms-admin/library/snippets/{id}/revert/{seq} — restore a
/// snippet to the snapshot stored in the named revision. Captures a
/// fresh revision after the revert so the timeline stays linear.
pub async fn snippet_revert_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path((id, sequence)): Path<(i64, i32)>,
) -> Result<Response, AdminError> {
    let Some(rev) = crate::snippet::revision_by_sequence(tenant.pool(), id, sequence).await? else {
        return Err(AdminError::NotFound(id));
    };
    let mut snippet = crate::snippet::Snippet::objects()
        .where_(crate::snippet::Snippet::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    // Re-hydrate from the snapshot — title / body / slug / data /
    // folder_path / type_name. Skip primary key + audit timestamps.
    let snap = &rev.snapshot;
    if let Some(v) = snap.get("title").and_then(|v| v.as_str()) {
        snippet.title = v.to_owned();
    }
    if let Some(v) = snap.get("slug").and_then(|v| v.as_str()) {
        snippet.slug = v.to_owned();
    }
    if let Some(v) = snap.get("body_markdown").and_then(|v| v.as_str()) {
        snippet.body_markdown = v.to_owned();
    }
    if let Some(v) = snap.get("folder_path").and_then(|v| v.as_str()) {
        snippet.folder_path = v.to_owned();
    }
    if let Some(v) = snap.get("type_name").and_then(|v| v.as_str()) {
        snippet.type_name = v.to_owned();
    }
    if let Some(v) = snap.get("data") {
        snippet.data = v.clone();
    }
    snippet.save_pool(tenant.pool()).await?;
    // Capture the revert itself so the timeline shows it.
    if let Some(handler) = crate::library::find_handler(&snippet.type_name) {
        if handler.revisions_enabled() {
            let captured_by = session_user.as_ref().and_then(|u| u.id.get().copied());
            crate::snippet::capture_revision(tenant.pool(), &snippet, captured_by)
                .await
                .log_warn("revision not captured; the history has a gap");
        }
    }
    let sequence_str = sequence.to_string();
    redirect_named_with_params_and_message_args(
        "rcms-admin:library:edit",
        &[("id", id.to_string())],
        MsgLevel::Success,
        "Reverted to revision #{sequence}.",
        &[("sequence", sequence_str.as_str())],
        &headers,
    )
}

pub async fn snippet_create_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Form(form): Form<std::collections::HashMap<String, String>>,
) -> Result<Response, AdminError> {
    let type_name = form.get("type_name").cloned().unwrap_or_default();
    let slug = form.get("slug").cloned().unwrap_or_default();
    let title = form.get("title").cloned().unwrap_or_default();
    let body = form.get("body_markdown").cloned().unwrap_or_default();
    let folder_path = form
        .get("folder_path")
        .map(|s| crate::snippet::normalize_folder(s))
        .unwrap_or_default();
    if type_name.is_empty() || slug.is_empty() || title.is_empty() {
        return Err(AdminError::Validation(
            "type, slug, and title are all required".to_owned(),
        ));
    }
    let mut row = crate::snippet::Snippet {
        id: rustango::sql::Auto::Unset,
        type_name,
        slug,
        folder_path,
        title,
        body_markdown: body,
        data: serde_json::Value::Object(serde_json::Map::new()),
        created_at: rustango::sql::Auto::Unset,
        updated_at: rustango::sql::Auto::Unset,
    };
    row.save_pool(tenant.pool()).await?;
    // A new form is empty until its fields are built — go straight to the
    // Form Builder rather than back to the Library list.
    if row.type_name == "form" {
        let id = row.id.get().copied().unwrap_or_default();
        return redirect_named_with_params_and_message(
            "rcms-admin:forms:build",
            &[("id", id.to_string())],
            MsgLevel::Success,
            &super::i18n::tr(
                &headers,
                "Created snippet “{title}”.",
                &[("title", row.title.as_str())],
            ),
            &headers,
        );
    }
    redirect_named_with_message_args(
        "rcms-admin:library:list",
        MsgLevel::Success,
        "Created snippet “{title}”.",
        &[("title", row.title.as_str())],
        &headers,
    )
}

pub async fn snippet_edit_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
    Form(form): Form<std::collections::HashMap<String, String>>,
) -> Result<Response, AdminError> {
    let mut snippet = crate::snippet::Snippet::objects()
        .where_(crate::snippet::Snippet::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    // Permission gate (#21 phase 3) — handler-declared can_edit
    // hook short-circuits the save. V1 default is `true`; hosts
    // override to wire role-aware logic.
    if let Some(handler) = crate::library::find_handler(&snippet.type_name) {
        if !handler.can_edit(&snippet) {
            return Err(AdminError::Validation(format!(
                "Library type `{}` denies edit on this row.",
                snippet.type_name
            )));
        }
        // Cross-field validation hook — surfaces field-scoped error
        // strings before any mutation lands.
        if let Err(errors) = handler.validate(&form) {
            let summary: Vec<String> = errors
                .into_iter()
                .map(|(field, msg)| format!("{field}: {msg}"))
                .collect();
            return Err(AdminError::Validation(summary.join("; ")));
        }
    }
    if let Some(v) = form.get("type_name") {
        if !v.is_empty() {
            snippet.type_name = v.clone();
        }
    }
    if let Some(v) = form.get("slug") {
        if !v.is_empty() {
            snippet.slug = v.clone();
        }
    }
    if let Some(v) = form.get("title") {
        snippet.title = v.clone();
    }
    if let Some(v) = form.get("body_markdown") {
        snippet.body_markdown = v.clone();
    }
    if let Some(v) = form.get("folder_path") {
        snippet.folder_path = crate::snippet::normalize_folder(v);
    }
    // Per-element template name, for types that opted in via
    // `allows_element_template()`. Only that one `data` key is read
    // here: the generic form deliberately does not own `data` (typed
    // extras belong to a type's own `edit_template`), so a type that
    // never opted in cannot have its render repointed from this form.
    if crate::library::find_handler(&snippet.type_name)
        .is_some_and(|h| h.allows_element_template())
    {
        if let Some(v) = form.get("data__template") {
            crate::snippet::set_element_template(&mut snippet.data, v.trim());
        }
    }
    snippet.save_pool(tenant.pool()).await?;
    // #123 — capture a versioned snapshot when the handler opts in
    // via `revisions_enabled()`. Best-effort: capture failure logs
    // + does NOT abort the save.
    if let Some(handler) = crate::library::find_handler(&snippet.type_name) {
        if handler.revisions_enabled() {
            let captured_by = session_user.as_ref().and_then(|u| u.id.get().copied());
            if let Err(e) =
                crate::snippet::capture_revision(tenant.pool(), &snippet, captured_by).await
            {
                tracing::warn!(
                    target: "rustango_cms::admin",
                    snippet_id = id,
                    type_name = %snippet.type_name,
                    error = %e,
                    "snippet revision capture failed",
                );
            }
        }
    }
    // #146 — reindex references from the saved snippet form.
    {
        let mut refs: Vec<crate::reference_index::Reference> = Vec::new();
        for (key, value) in &form {
            let mut found = crate::reference_index::scan(value, key);
            refs.append(&mut found);
        }
        if let Err(e) = crate::reference_index::reindex_source(
            tenant.pool(),
            crate::reference_index::KIND_SNIPPET,
            id,
            refs,
        )
        .await
        {
            tracing::warn!(
                target: "rustango_cms::reference_index",
                snippet_id = id, error = %e,
                "snippet reindex failed (best-effort)"
            );
        }
    }
    redirect_named_with_message_args(
        "rcms-admin:library:list",
        MsgLevel::Success,
        "Saved snippet “{title}”.",
        &[("title", snippet.title.as_str())],
        &headers,
    )
}

pub async fn snippet_delete_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
) -> Result<Response, AdminError> {
    let snippet = crate::snippet::Snippet::objects()
        .where_(crate::snippet::Snippet::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    // Permission gate (#21 phase 3) — handler can deny delete.
    if let Some(handler) = crate::library::find_handler(&snippet.type_name) {
        if !handler.can_delete(&snippet) {
            return Err(AdminError::Validation(format!(
                "Library type `{}` denies delete on this row.",
                snippet.type_name
            )));
        }
    }
    let title = snippet.title.clone();
    let is_form = snippet.type_name == "form";
    snippet.delete_pool(tenant.pool()).await?;
    // Forms are deleted from the Forms screen — go back there.
    if is_form {
        let body = super::i18n::tr(&headers, "Deleted snippet “{title}”.", &[("title", title.as_str())]);
        return Ok(rustango::messages::redirect_with_message(
            messages_secret(),
            &headers,
            MsgLevel::Success,
            &body,
            "/cms-admin/forms",
        ));
    }
    redirect_named_with_message_args(
        "rcms-admin:library:list",
        MsgLevel::Success,
        "Deleted snippet “{title}”.",
        &[("title", title.as_str())],
        &headers,
    )
}

// =====================================================================
// Media + Documents tabs
// =====================================================================

/// Parse a `YYYY-MM-DD` filter into the UTC instant at start-of-day.
/// Empty / unparseable → None.
fn parse_date_floor(raw: Option<&str>) -> Option<chrono::DateTime<chrono::Utc>> {
    let raw = raw?.trim();
    if raw.is_empty() {
        return None;
    }
    chrono::NaiveDate::parse_from_str(raw, "%Y-%m-%d")
        .ok()
        .map(|d| {
            chrono::DateTime::<chrono::Utc>::from_naive_utc_and_offset(
                d.and_hms_opt(0, 0, 0).unwrap_or_default(),
                chrono::Utc,
            )
        })
}

/// Parse a `YYYY-MM-DD` filter into the UTC instant at end-of-day
/// (23:59:59) so the inclusive ceiling matches editor intent.
fn parse_date_ceil(raw: Option<&str>) -> Option<chrono::DateTime<chrono::Utc>> {
    let raw = raw?.trim();
    if raw.is_empty() {
        return None;
    }
    chrono::NaiveDate::parse_from_str(raw, "%Y-%m-%d")
        .ok()
        .map(|d| {
            chrono::DateTime::<chrono::Utc>::from_naive_utc_and_offset(
                d.and_hms_opt(23, 59, 59).unwrap_or_default(),
                chrono::Utc,
            )
        })
}

/// Query string for the media + documents list views. All filters
/// optional; URL state persists so editors can bookmark filtered
/// views.
#[derive(Debug, Deserialize)]
pub struct MediaListQuery {
    /// 1-based page number. Out-of-range values clamp to the last page.
    #[serde(default)]
    pub page: Option<i64>,
    /// Rows per page; clamped to `admin::pagination::MAX_PER_PAGE`.
    #[serde(default)]
    pub per_page: Option<usize>,
    /// Filter to a specific collection. Omitted = all collections.
    #[serde(default)]
    pub collection: Option<i64>,
    /// Case-insensitive substring search across title, filename,
    /// alt_text.
    #[serde(default)]
    pub q: Option<String>,
    /// Date range floor — `YYYY-MM-DD`. Matches `uploaded_at >= from`.
    #[serde(default)]
    pub from: Option<String>,
    /// Date range ceiling — `YYYY-MM-DD`. Matches `uploaded_at <= to`
    /// (end of day in UTC).
    #[serde(default)]
    pub to: Option<String>,
}

// =====================================================================
// Image hard crop (#24) — apply / clone-and-crop
// =====================================================================

/// Form payload for both crop handlers. Coords are in source-image
/// pixel space; `mode` distinguishes apply vs clone-and-crop.
#[derive(Debug, Deserialize)]
pub struct CropForm {
    pub crop_x: u32,
    pub crop_y: u32,
    pub crop_w: u32,
    pub crop_h: u32,
    /// Either "apply" (destructive — overwrites source) or "clone"
    /// (creates a new media row from the cropped bytes).
    pub mode: String,
}

/// POST /cms-admin/media/{id}/crop — apply the crop rect to the
/// source bytes. Two modes:
/// - `mode=apply` — replaces the row's bytes in-place, bumps
///   content_hash, recomputes width/height, purges every rendition
///   for the old hash. Destructive — the original bytes are gone.
/// - `mode=clone` — creates a new media row whose bytes are the
///   cropped output. Original row is untouched. New row inherits
///   title (" — crop" suffix) + alt_text + collection_id; focal
///   point clears so the editor re-marks on the crop.
pub async fn media_crop_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
    Form(form): Form<CropForm>,
) -> Result<Response, AdminError> {
    use rustango::sql::Auto;
    let mut media = Media::objects()
        .where_(Media::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    if media.kind != "image" {
        return Err(AdminError::Validation(
            "Crop applies to images only.".to_owned(),
        ));
    }

    let bytes = crate::media_storage::disk_for(&tenant.org.slug)
        .load(&crate::media_storage::key(
            &tenant.org.slug,
            &media.storage_key,
        ))
        .await
        .map_err(|e| AdminError::Validation(format!("read source: {e}")))?;
    let (cropped_bytes, out_mime) = crate::rendition::crop_image_off_runtime(
        bytes,
        media.mime.clone(),
        (form.crop_x, form.crop_y, form.crop_w, form.crop_h),
    )
    .await
    .map_err(AdminError::Validation)?;

    let new_hash = {
        use sha2::Digest;
        let mut hasher = sha2::Sha256::new();
        hasher.update(&cropped_bytes);
        let digest = hasher.finalize();
        let mut hex = String::with_capacity(digest.len() * 2);
        for b in digest.iter() {
            hex.push_str(&format!("{b:02x}"));
        }
        hex
    };

    match form.mode.as_str() {
        "apply" => {
            // In-place: overwrite the same storage_key. The backend
            // writes the object wholesale (atomic PUT on S3), which
            // replaces the old tmp-file + rename dance.
            crate::media_storage::disk_for(&tenant.org.slug)
                .save(
                    &crate::media_storage::key(&tenant.org.slug, &media.storage_key),
                    &cropped_bytes,
                )
                .await
                .map_err(|e| AdminError::Validation(format!("write crop: {e}")))?;
            let old_hash = media.content_hash.clone();
            media.content_hash = new_hash;
            media.size = cropped_bytes.len() as i64;
            media.mime = out_mime;
            media.width = Some(form.crop_w as i32);
            media.height = Some(form.crop_h as i32);
            // Focal point in original coords no longer maps; clear.
            media.focal_point_x = None;
            media.focal_point_y = None;
            media.save_pool(tenant.pool()).await?;

            // Purge renditions for the OLD content_hash (the route
            // looks them up by hash + filter spec; bytes on disk
            // also need cleaning).
            for r in crate::rendition::MediaRendition::objects()
                .where_(crate::rendition::MediaRendition::content_hash.eq(old_hash))
                .fetch(tenant.pool())
                .await?
            {
                crate::media_storage::disk_for(&tenant.org.slug)
                    .delete(&crate::media_storage::key(&tenant.org.slug, &r.storage_key))
                    .await
                    .log_warn("stored file not deleted; the blob is orphaned");
                r.delete_pool(tenant.pool()).await?;
            }
            let crop_w = form.crop_w.to_string();
            let crop_h = form.crop_h.to_string();
            redirect_named_with_params_and_message_args(
                "rcms-admin:media:edit",
                &[("id", id.to_string())],
                MsgLevel::Success,
                "Crop applied. New dimensions {width}×{height}; renditions purged.",
                &[("width", crop_w.as_str()), ("height", crop_h.as_str())],
                &headers,
            )
        }
        "clone" => {
            // Save as new. Fresh storage_key under the source's
            // content-hash prefix so the on-disk bytes are
            // discoverable. Suffix the title with " — crop" so the
            // new row reads distinctly in lists.
            let ext = match out_mime.as_str() {
                "image/jpeg" => "jpg",
                "image/webp" => "webp",
                _ => "png",
            };
            let new_key = format!("{}-crop-{}.{ext}", &new_hash[..16.min(new_hash.len())], id);
            crate::media_storage::disk_for(&tenant.org.slug)
                .save(
                    &crate::media_storage::key(&tenant.org.slug, &new_key),
                    &cropped_bytes,
                )
                .await
                .map_err(|e| AdminError::Validation(format!("write clone: {e}")))?;

            let clone_title = if media.title.contains(" — crop") {
                media.title.clone()
            } else {
                format!("{} — crop", media.title)
            };
            let mut clone = Media {
                id: Auto::Unset,
                filename: format!(
                    "{}.crop.{ext}",
                    std::path::Path::new(&media.filename)
                        .file_stem()
                        .map(|s| s.to_string_lossy().to_string())
                        .unwrap_or_else(|| "crop".to_owned())
                ),
                content_hash: new_hash,
                mime: out_mime,
                size: cropped_bytes.len() as i64,
                kind: "image".to_owned(),
                width: Some(form.crop_w as i32),
                height: Some(form.crop_h as i32),
                storage_key: new_key,
                title: clone_title,
                alt_text: media.alt_text.clone(),
                description: media.description.clone(),
                uploaded_by: media.uploaded_by,
                collection_id: media.collection_id,
                focal_point_x: None,
                focal_point_y: None,
                uploaded_at: Auto::Unset,
            };
            clone.insert_pool(tenant.pool()).await?;
            let new_id = clone.id.get().copied().unwrap_or_default();
            let new_id_str = new_id.to_string();
            let crop_w = form.crop_w.to_string();
            let crop_h = form.crop_h.to_string();
            redirect_named_with_params_and_message_args(
                "rcms-admin:media:edit",
                &[("id", new_id.to_string())],
                MsgLevel::Success,
                "Saved cropped clone as new media #{id} ({width}×{height}).",
                &[
                    ("id", new_id_str.as_str()),
                    ("width", crop_w.as_str()),
                    ("height", crop_h.as_str()),
                ],
                &headers,
            )
        }
        other => Err(AdminError::Validation(format!(
            "Unknown crop mode `{other}` — expected `apply` or `clone`."
        ))),
    }
}

// =====================================================================
// Image replace (#190)
// =====================================================================

/// POST /cms-admin/media/{id}/replace — re-upload bytes for an existing
/// media row. Keeps the row id (so every page that references it stays
/// linked) and the editorial metadata (title, alt_text). Updates the
/// physical fields: storage_key, content_hash, mime, size, width,
/// height, filename. Focal point survives unless the new dimensions
/// require it to be clamped — it's stored as a 0..1 fraction so it
/// already scales across resizes.
///
/// Old on-disk bytes are best-effort deleted; every cached rendition
/// for the old content_hash is purged so subsequent requests
/// regenerate from the replacement.
pub async fn media_replace_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
    mut multipart: axum::extract::Multipart,
) -> Result<Response, AdminError> {
    use sha2::{Digest, Sha256};

    let mut media = Media::objects()
        .where_(Media::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;

    let mut filename = String::new();
    let mut bytes: Vec<u8> = Vec::new();
    while let Some(field) = multipart
        .next_field()
        .await
        .map_err(|e| AdminError::Upload(format!("multipart parse: {e}")))?
    {
        if field.name().unwrap_or("") == "file" {
            if let Some(fname) = field.file_name() {
                filename = sanitize_filename(fname);
            }
            bytes = field
                .bytes()
                .await
                .map_err(|e| AdminError::Upload(format!("read bytes: {e}")))?
                .to_vec();
        } else {
            let _ = field.bytes().await;
        }
    }
    if filename.is_empty() || bytes.is_empty() {
        return Err(AdminError::Upload("no file submitted".into()));
    }

    let new_mime = mime_guess::from_path(&filename)
        .first_or_octet_stream()
        .essence_str()
        .to_owned();
    refuse_active_upload(&new_mime)?;
    // Replacing an image with a non-image (or vice versa) would
    // silently break every consumer that branches on `kind`.
    let new_kind = Media::classify_mime(&new_mime).to_owned();
    if new_kind != media.kind {
        return Err(AdminError::Validation(format!(
            "Replacement kind `{new_kind}` does not match existing `{}` — replace must keep the media type.",
            media.kind
        )));
    }

    // EXIF strip + decode for dimensions, same as the upload path.
    let (stored, decoded_dims) =
        crate::rendition::strip_exif_off_runtime(bytes, new_mime.clone()).await;
    bytes = stored;

    let mut hasher = Sha256::new();
    hasher.update(&bytes);
    let digest = hasher.finalize();
    let new_hash = digest.iter().fold(String::with_capacity(64), |mut acc, b| {
        use std::fmt::Write as _;
        let _ = write!(acc, "{b:02x}");
        acc
    });

    let new_storage_key = format!("{}-{}", &new_hash[..16.min(new_hash.len())], filename);
    crate::media_storage::disk_for(&tenant.org.slug)
        .save(
            &crate::media_storage::key(&tenant.org.slug, &new_storage_key),
            &bytes,
        )
        .await
        .map_err(|e| AdminError::Upload(format!("write: {e}")))?;

    let old_storage_key = media.storage_key.clone();
    let old_hash = media.content_hash.clone();

    let (new_w, new_h): (Option<i32>, Option<i32>) = decoded_dims
        .map(|(w, h)| (Some(w as i32), Some(h as i32)))
        .unwrap_or((None, None));

    media.filename = filename;
    media.storage_key = new_storage_key;
    media.content_hash = new_hash;
    media.mime = new_mime;
    media.size = bytes.len() as i64;
    media.width = new_w;
    media.height = new_h;
    media.save_pool(tenant.pool()).await?;

    // Best-effort cleanup of the old file on disk. Only remove when
    // the storage_key actually changed — content-addressed naming
    // means an identical replacement reuses the same path.
    if old_storage_key != media.storage_key {
        crate::media_storage::disk_for(&tenant.org.slug)
            .delete(&crate::media_storage::key(
                &tenant.org.slug,
                &old_storage_key,
            ))
            .await
            .log_warn("stored file not deleted; the blob is orphaned");
    }

    // Purge renditions for the old content_hash so the next request
    // regenerates from the replacement bytes.
    if old_hash != media.content_hash {
        for r in crate::rendition::MediaRendition::objects()
            .where_(crate::rendition::MediaRendition::content_hash.eq(old_hash))
            .fetch(tenant.pool())
            .await?
        {
            crate::media_storage::disk_for(&tenant.org.slug)
                .delete(&crate::media_storage::key(&tenant.org.slug, &r.storage_key))
                .await
                .log_warn("stored file not deleted; the blob is orphaned");
            r.delete_pool(tenant.pool()).await?;
        }
    }

    redirect_named_with_params_and_message(
        "rcms-admin:media:edit",
        &[("id", id.to_string())],
        MsgLevel::Success,
        "Replaced. Old renditions purged; every page referencing this image now serves the new bytes.",
        &headers,
    )
}

// =====================================================================
// Image focal point (#4)
// =====================================================================

/// GET /cms-admin/media/{id}/edit — image detail page with the
/// focal-point picker. Image-only; other kinds return 404.
/// Locate every page that references `media_id`. Returns
/// `(page_id, page_title, url_path, status)` rows. Scan reuses the
/// `harvest_media_ids` helper that powers `media_unused_report`,
/// inverted so we collect per-page hits instead of building a
/// tenant-wide used-set.
async fn pages_referencing_media(
    pool: &rustango::sql::Pool,
    media_id: i64,
) -> Result<Vec<(i64, String, String, String)>, rustango::sql::ExecError> {
    let pages: Vec<Page> = Page::objects().fetch(pool).await?;
    let mut hits: Vec<(i64, String, String, String)> = Vec::new();
    for p in &pages {
        let page_id = p.id.get().copied().unwrap_or_default();
        if page_id == 0 {
            continue;
        }
        let mut ids = std::collections::BTreeSet::new();
        // Walk the canonical page JSON first (defensive).
        if let Ok(v) = serde_json::to_value(p) {
            harvest_media_ids(&v, &mut ids);
        }
        // Walk the page-type extension JSON (hero_media_id +
        // stream-block image refs).
        if let Ok(pts) = PageType::objects()
            .where_(PageType::id.eq(p.page_type_id))
            .fetch(pool)
            .await
        {
            if let Some(pt_row) = pts.into_iter().next() {
                if let Some(handler) = crate::page_type::find_handler(&pt_row.type_name) {
                    if let Ok(ext) = handler.load_extension(pool, page_id).await {
                        harvest_media_ids(&ext, &mut ids);
                    }
                }
            }
        }
        if ids.contains(&media_id) {
            hits.push((
                page_id,
                p.title.clone(),
                p.url_path.clone(),
                p.status.clone(),
            ));
        }
    }
    Ok(hits)
}

pub async fn media_edit_form(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
) -> Result<Response, AdminError> {
    let media = Media::objects()
        .where_(Media::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    if media.kind != "image" {
        return Err(AdminError::NotFound(id));
    }
    // #100 — per-image usage report.
    let usages = pages_referencing_media(tenant.pool(), id)
        .await
        .unwrap_or_default();
    let usage_rows: Vec<serde_json::Value> = usages
        .iter()
        .map(|(pid, title, url_path, status)| {
            serde_json::json!({
                "id": pid,
                "title": title,
                "url_path": url_path,
                "status": status,
            })
        })
        .collect();
    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "media", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert(
        "media",
        &serde_json::json!({
            "id": media.id.get().copied(),
            "title": media.title,
            "filename": media.filename,
            "alt_text": media.alt_text,
            "description": media.description,
            "mime": media.mime,
            "size": media.size,
            "width": media.width,
            "height": media.height,
            "focal_point_x": media.focal_point_x,
            "focal_point_y": media.focal_point_y,
            "uploaded_at": media.uploaded_at.get().copied(),
            // Cache-buster for the `rcms_image_url(..., v=media.content_hash)`
            // call in the template — without this the focal-point
            // preview never picks up an in-place re-crop.
            "content_hash": media.content_hash,
        }),
    );
    ctx.insert("usages", &usage_rows);
    ctx.insert("usage_count", &(usage_rows.len() as i64));
    render_with_csrf(&state, &headers, "rcms_admin/media_edit.html", &mut ctx)
}

/// Form payload for the focal-point editor. All four fields
/// arrive as strings from the HTML form; we parse + validate
/// server-side. A submission with empty x/y clears the focal point.
#[derive(Debug, Deserialize)]
pub struct MediaEditForm {
    pub title: String,
    pub alt_text: String,
    #[serde(default)]
    pub description: String,
    #[serde(default)]
    pub focal_point_x: Option<String>,
    #[serde(default)]
    pub focal_point_y: Option<String>,
}

/// POST /cms-admin/media/{id}/edit — save title / alt + focal point.
/// When the focal point changes, every cached rendition for this
/// media row is purged so subsequent requests regenerate around the
/// new center.
pub async fn media_edit_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
    Form(form): Form<MediaEditForm>,
) -> Result<Response, AdminError> {
    let mut media = Media::objects()
        .where_(Media::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    if media.kind != "image" {
        return Err(AdminError::NotFound(id));
    }
    media.title = form.title.trim().to_owned();
    media.alt_text = form.alt_text.trim().to_owned();
    media.description = form.description.trim().to_owned();

    let parse_fraction = |raw: Option<&str>| -> Option<f32> {
        raw.map(str::trim)
            .filter(|s| !s.is_empty())
            .and_then(|s| s.parse::<f32>().ok())
            .map(|f| f.clamp(0.0, 1.0))
    };
    let new_x = parse_fraction(form.focal_point_x.as_deref());
    let new_y = parse_fraction(form.focal_point_y.as_deref());
    let focal_changed = media.focal_point_x != new_x || media.focal_point_y != new_y;
    media.focal_point_x = new_x;
    media.focal_point_y = new_y;
    media.save_pool(tenant.pool()).await?;

    // Invalidate rendition cache rows + delete their on-disk bytes
    // so subsequent requests regenerate with the new focal point.
    if focal_changed {
        let renditions: Vec<crate::rendition::MediaRendition> =
            crate::rendition::MediaRendition::objects()
                .where_(
                    crate::rendition::MediaRendition::content_hash.eq(media.content_hash.clone()),
                )
                .fetch(tenant.pool())
                .await?;
        for r in renditions {
            // Best-effort delete of the on-disk bytes; the DB row
            // delete is what counts (next render miss regenerates).
            crate::media_storage::disk_for(&tenant.org.slug)
                .delete(&crate::media_storage::key(&tenant.org.slug, &r.storage_key))
                .await
                .log_warn("stored file not deleted; the blob is orphaned");
            r.delete_pool(tenant.pool()).await?;
        }
    }
    redirect_named_with_params_and_message(
        "rcms-admin:media:edit",
        &[("id", id.to_string())],
        MsgLevel::Success,
        if focal_changed {
            "Saved. Renditions purged — next request regenerates with the new focal point."
        } else {
            "Saved."
        },
        &headers,
    )
}

/// Recursively scan a JSON value for any string that contains
/// `needle` (already lowercased). Used by admin search to
/// broaden recall into page-type extension fields without listing
/// every key on the handler trait.
fn json_contains_needle(value: &serde_json::Value, needle: &str) -> bool {
    match value {
        serde_json::Value::String(s) => {
            if s.to_lowercase().contains(needle) {
                return true;
            }
            // Stream-block extension columns sometimes hold a JSON
            // array as a string — peek through if it looks like one.
            let trimmed = s.trim_start();
            if trimmed.starts_with('[') || trimmed.starts_with('{') {
                if let Ok(parsed) = serde_json::from_str::<serde_json::Value>(s) {
                    return json_contains_needle(&parsed, needle);
                }
            }
            false
        }
        serde_json::Value::Array(arr) => arr.iter().any(|v| json_contains_needle(v, needle)),
        serde_json::Value::Object(map) => map.values().any(|v| json_contains_needle(v, needle)),
        _ => false,
    }
}

/// Recursively walk `value` (the JSON shape of a page's extension
/// row, plus any nested stream-block JSON we find as string values)
/// and collect every `media_id` reference. Catches:
///
/// - Direct `media_id` / `hero_media_id` / `*_media_id` fields on
///   the extension table (any object key matching `media_id` or
///   ending in `_media_id`).
/// - Stream-block image references: extension rows store the
///   `body_stream` column as a JSON string; we detect string values
///   that look like JSON and recurse into the parsed array.
/// - IDs stored as numbers OR strings — HTML forms submit text, so
///   stream-block `media_id` values land as strings in the JSON.
fn harvest_media_ids(value: &serde_json::Value, out: &mut std::collections::BTreeSet<i64>) {
    use serde_json::Value;
    match value {
        Value::Object(map) => {
            for (key, child) in map {
                let is_media_key = key == "media_id" || key.ends_with("_media_id");
                if is_media_key {
                    match child {
                        Value::Number(n) => {
                            if let Some(id) = n.as_i64() {
                                if id > 0 {
                                    out.insert(id);
                                }
                            }
                        }
                        Value::String(s) => {
                            if let Ok(id) = s.parse::<i64>() {
                                if id > 0 {
                                    out.insert(id);
                                }
                            }
                        }
                        _ => harvest_media_ids(child, out),
                    }
                } else {
                    harvest_media_ids(child, out);
                }
            }
        }
        Value::Array(arr) => {
            for item in arr {
                harvest_media_ids(item, out);
            }
        }
        Value::String(s) => {
            let trimmed = s.trim_start();
            if trimmed.starts_with('[') || trimmed.starts_with('{') {
                if let Ok(parsed) = serde_json::from_str::<Value>(s) {
                    harvest_media_ids(&parsed, out);
                }
            }
        }
        _ => {}
    }
}

/// GET /cms-admin/media/unused — list `cms_media` rows that no page
/// references. Builds a reverse index by walking every page's
/// extension JSON for `media_id` keys, then diffs against the full
/// media table. Used by editors to garbage-collect stale uploads.
pub async fn media_unused_report(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
) -> Result<Response, AdminError> {
    let all_media: Vec<Media> = Media::objects()
        .order_by(&[("uploaded_at", true)])
        .fetch(tenant.pool())
        .await?;
    let pages: Vec<Page> = Page::objects().fetch(tenant.pool()).await?;

    let mut used: std::collections::BTreeSet<i64> = std::collections::BTreeSet::new();

    // 1) Page rows themselves carry no media_id columns today, but
    //    we walk the JSON-serialized page anyway for defense-in-depth
    //    — a future column would auto-surface.
    for p in &pages {
        if let Ok(v) = serde_json::to_value(p) {
            harvest_media_ids(&v, &mut used);
        }
    }

    // 2) Extension rows: every PageType handler loads its own
    //    extension row. The default impl returns Value::Null for
    //    unit-struct types; ours returns the row as JSON (including
    //    body_stream as a string we recurse into).
    for p in &pages {
        let page_id = p.id.get().copied().unwrap_or_default();
        // Resolve the PageType row → type_name → handler.
        // page_type lookup is cheap; tenants have a handful of types.
        let pt: Vec<crate::page_type_model::PageType> = crate::page_type_model::PageType::objects()
            .where_(crate::page_type_model::PageType::id.eq(p.page_type_id))
            .fetch(tenant.pool())
            .await?;
        let Some(pt_row) = pt.into_iter().next() else {
            continue;
        };
        let Some(handler) = crate::page_type::find_handler(&pt_row.type_name) else {
            continue;
        };
        if let Ok(ext_value) = handler.load_extension(tenant.pool(), page_id).await {
            harvest_media_ids(&ext_value, &mut used);
        }
    }

    // 3) Diff: media rows whose ID isn't in the used set are unused.
    let unused: Vec<&Media> = all_media
        .iter()
        .filter(|m| m.id.get().copied().is_some_and(|id| !used.contains(&id)))
        .collect();

    let unused_view: Vec<serde_json::Value> = unused
        .iter()
        .map(|m| {
            serde_json::json!({
                "id": m.id.get().copied(),
                "title": m.title,
                "filename": m.filename,
                "kind": m.kind,
                "mime": m.mime,
                "size": m.size,
                "uploaded_at": m.uploaded_at.get().copied(),
                "uploaded_by": m.uploaded_by,
            })
        })
        .collect();

    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "media-unused", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("unused", &unused_view);
    ctx.insert("total_media", &all_media.len());
    ctx.insert("used_count", &used.len());
    render_with_csrf(&state, &headers, "rcms_admin/media_unused.html", &mut ctx)
}

/// POST /cms-admin/media/unused/delete — bulk-delete the chosen
/// media rows. Receives a list of `id=N` form fields and deletes
/// each in sequence. The handler does NOT re-verify "unused" status
/// — that's the editor's job — but it stays well-behaved because
/// the form template only renders rows that ARE currently unused.
pub async fn media_unused_bulk_delete(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Form(form): Form<Vec<(String, String)>>,
) -> Result<Response, AdminError> {
    use rustango::sql::Auto;
    let ids: Vec<i64> = form
        .iter()
        .filter(|(k, _)| k == "id" || k == "ids")
        .filter_map(|(_, v)| v.parse::<i64>().ok())
        .collect();
    let mut deleted = 0usize;
    for id in ids {
        let row = Media::objects()
            .where_(Media::id.eq(id))
            .first(tenant.pool())
            .await?;
        if let Some(m) = row {
            // Use Model::delete_pool via the row instance.
            // The framework's Auto wrapper holds the id; deletion
            // through the row resolves it cleanly.
            let _ = Auto::Unset::<i64>; // silence unused-import lint
            m.delete_pool(tenant.pool()).await?;
            deleted += 1;
        }
    }
    redirect_named_with_message_plural(
        "rcms-admin:media:unused",
        MsgLevel::Success,
        "Deleted {count} unused media item(s).",
        deleted as i64,
        &[("count", &deleted.to_string())],
        &headers,
    )
}

/// GET /cms-admin/media — list `cms_media` rows where `kind = 'image'`.
pub async fn media_list(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Query(q): Query<MediaListQuery>,
    axum::extract::RawQuery(raw_query): axum::extract::RawQuery,
) -> Result<Response, AdminError> {
    // #142 — opportunistic scratch-upload sweep. Roughly 2% of
    // media-list visits trigger a stale-row GC so dead scratch uploads
    // don't accumulate. Best-effort: file unlink errors log + continue.
    if rand::random::<u8>() < 5 {
        if let Ok(unlinked) = crate::uploaded_file::gc_stale(tenant.pool()).await {
            for key in unlinked {
                crate::media_storage::disk_for(&tenant.org.slug)
                    .delete(&crate::media_storage::key(&tenant.org.slug, &key))
                    .await
                    .log_warn("stored file not deleted; the blob is orphaned");
            }
        }
    }
    render_media_list(
        &state,
        &tenant,
        &headers,
        session_user.as_ref(),
        "image",
        q,
        raw_query,
    )
    .await
}

/// GET /cms-admin/documents — same view filtered to non-image kinds.
pub async fn documents_list(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Query(q): Query<MediaListQuery>,
    axum::extract::RawQuery(raw_query): axum::extract::RawQuery,
) -> Result<Response, AdminError> {
    render_media_list(
        &state,
        &tenant,
        &headers,
        session_user.as_ref(),
        "document",
        q,
        raw_query,
    )
    .await
}

/// Shared list renderer for the image / document views. `kind` is
/// the discriminator on `cms_media.kind` ("image" / "document").
async fn render_media_list(
    state: &super::AdminState,
    tenant: &Tenant,
    headers: &HeaderMap,
    session_user: Option<&rustango::tenancy::auth::User>,
    kind: &str,
    filters: MediaListQuery,
    raw_query: Option<String>,
) -> Result<Response, AdminError> {
    let collection_filter = filters.collection;
    let search = filters
        .q
        .as_deref()
        .map(str::trim)
        .filter(|s| !s.is_empty())
        .map(str::to_lowercase);
    let from_filter = parse_date_floor(filters.from.as_deref());
    let to_filter = parse_date_ceil(filters.to.as_deref());
    // Fetch every media row + every collection once. Collection
    // count is small (a handful per tenant); media count is bounded
    // by what fits comfortably in the admin list. Walk in Rust.
    let all: Vec<Media> = Media::objects()
        .order_by(&[("uploaded_at", true)])
        .fetch(tenant.pool())
        .await?;
    let collections: Vec<crate::media::MediaCollection> = crate::media::MediaCollection::objects()
        .order_by(&[("sort_order", false), ("name", false)])
        .fetch(tenant.pool())
        .await?;

    let items: Vec<&Media> = all
        .iter()
        .filter(|m| {
            let kind_match = if kind == "image" {
                m.kind == "image"
            } else {
                m.kind != "image"
            };
            let collection_match = match collection_filter {
                None => true,
                Some(cid) => m.collection_id == Some(cid),
            };
            let search_match = match &search {
                None => true,
                Some(needle) => {
                    m.title.to_lowercase().contains(needle)
                        || m.filename.to_lowercase().contains(needle)
                        || m.alt_text.to_lowercase().contains(needle)
                }
            };
            let date_match = m.uploaded_at.get().is_none_or(|when| {
                let after_from = from_filter.is_none_or(|floor| *when >= floor);
                let before_to = to_filter.is_none_or(|ceil| *when <= ceil);
                after_from && before_to
            });
            kind_match && collection_match && search_match && date_match
        })
        .collect();

    // Paginate the filtered set, so a large library renders one page of
    // cards and thumbnails. The fetch above is still unbounded: filtering
    // happens in Rust so the case-insensitive search behaves the same on
    // SQLite, Postgres and MySQL (bare LIKE does not) — see #632.
    let per_page = crate::admin::pagination::clamp_per_page(filters.per_page);
    let requested_page = filters.page.unwrap_or(1);
    let total_items = items.len();
    let pagination = crate::admin::pagination::context(requested_page, per_page, total_items);
    let current_page = pagination["page"].as_i64().unwrap_or(1);
    let skip = crate::admin::pagination::offset(current_page, per_page) as usize;
    let items: Vec<&Media> = items.into_iter().skip(skip).take(per_page).collect();

    // Count per collection so the sidebar shows "(N)" beside each
    // name. We count for the CURRENT kind only so the badges line
    // up with the filtered view the editor is looking at.
    let mut counts: std::collections::HashMap<Option<i64>, i64> = std::collections::HashMap::new();
    for m in &all {
        let in_kind = if kind == "image" {
            m.kind == "image"
        } else {
            m.kind != "image"
        };
        if in_kind {
            *counts.entry(m.collection_id).or_insert(0) += 1;
        }
    }

    let collections_view: Vec<serde_json::Value> = collections
        .iter()
        .map(|c| {
            let id = c.id.get().copied();
            serde_json::json!({
                "id": id,
                "name": c.name,
                "parent_id": c.parent_id,
                "count": id.and_then(|i| counts.get(&Some(i)).copied()).unwrap_or(0),
            })
        })
        .collect();
    let uncategorized_count = counts.get(&None).copied().unwrap_or(0);

    let mut ctx = Context::new();
    let active_tab = if kind == "image" {
        "media"
    } else {
        "documents"
    };
    add_chrome(&mut ctx, tenant, active_tab, session_user).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("items", &items);
    ctx.insert("collections", &collections_view);
    ctx.insert("collection_filter", &collection_filter);
    ctx.insert("uncategorized_count", &uncategorized_count);
    ctx.insert("search_q", &filters.q);
    ctx.insert("date_from", &filters.from);
    ctx.insert("date_to", &filters.to);
    ctx.insert(
        "is_filtered",
        &(collection_filter.is_some()
            || search.is_some()
            || from_filter.is_some()
            || to_filter.is_some()),
    );
    // The full match count, not the size of the visible page.
    ctx.insert("filtered_count", &total_items);
    ctx.insert("pagination", &pagination);
    ctx.insert(
        "kind_label",
        if kind == "image" {
            "Media (images)"
        } else {
            "Documents"
        },
    );
    ctx.insert(
        "upload_kind",
        if kind == "image" { "image" } else { "document" },
    );
    ctx.insert(
        "self_url",
        if kind == "image" {
            "/cms-admin/media"
        } else {
            "/cms-admin/documents"
        },
    );
    // #316 — raw query string for the `| querystring(collection=…)` rail
    // links: they preserve the q / from / to filters and just set or
    // clear `collection` (media has no page param). `qs_none` is a null
    // sentinel — Tera has no `null` literal, so `querystring(k=qs_none)`
    // is how the template drops a key.
    ctx.insert("query_string", &raw_query.unwrap_or_default());
    ctx.insert("qs_none", &serde_json::Value::Null);
    render_with_csrf(state, headers, "rcms_admin/media_list.html", &mut ctx)
}

/// GET /cms-admin/media/upload — upload form.
pub async fn media_upload_form(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Query(q): Query<UploadFormQuery>,
) -> Result<Response, AdminError> {
    let kind_label = if q.kind.as_deref() == Some("document") {
        "Documents"
    } else {
        "Media (images)"
    };
    let mut ctx = Context::new();
    add_chrome(
        &mut ctx,
        &tenant,
        if q.kind.as_deref() == Some("document") {
            "documents"
        } else {
            "media"
        },
        session_user.as_ref(),
    )
    .await;
    // Collections picker (#188) — pass into ctx so each queue row
    // can render a per-file dropdown without a follow-up fetch.
    let collections: Vec<crate::media::MediaCollection> = crate::media::MediaCollection::objects()
        .order_by(&[("sort_order", false), ("name", false)])
        .fetch(tenant.pool())
        .await?;
    let collections_view: Vec<serde_json::Value> = collections
        .iter()
        .map(|c| {
            serde_json::json!({
                "id": c.id.get().copied(),
                "name": c.name,
            })
        })
        .collect();
    ctx.insert("collections", &collections_view);
    ctx.insert("kind_label", kind_label);
    ctx.insert("upload_kind", &q.kind.as_deref().unwrap_or("image"));
    render_with_csrf(&state, &headers, "rcms_admin/media_upload.html", &mut ctx)
}

#[derive(Debug, Deserialize)]
pub struct UploadFormQuery {
    #[serde(default)]
    pub kind: Option<String>,
}

/// POST /cms-admin/media/upload — accept a multipart upload, hash
/// the bytes, drop them through the tenant `Storage` backend, and
/// stamp a `cms_media` row.
/// POST /cms-admin/media/upload-staged — write bytes to scratch
/// storage + return JSON `{id, filename, mime, size}`.
/// Returned `id` is the [`crate::uploaded_file::UploadedFile`]
/// row to commit / cancel later.
///
/// Coexists with the existing single-step `media_upload_submit` — that
/// path keeps working unchanged. Use this endpoint for future
/// drag-drop / multi-file UIs where bytes need to land before
/// metadata is collected.
pub async fn media_upload_staged(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    mut multipart: axum::extract::Multipart,
) -> Result<Response, AdminError> {
    use sha2::{Digest, Sha256};
    let Some(viewer) = session_user.as_ref() else {
        // #285 — XHR/fetch clients (the multi-file upload UI) used to
        // get the raw HTML page injected into a toast on session
        // expiry. Pass the request headers so the responder returns
        // JSON when the client sent `Accept: application/json` or
        // `X-Requested-With: XMLHttpRequest`.
        return Ok(unauthorized_no_session_for(Some(&headers)));
    };
    let viewer_id = viewer.id.get().copied().unwrap_or_default();
    let mut filename = String::new();
    let mut bytes: Vec<u8> = Vec::new();
    while let Some(field) = multipart
        .next_field()
        .await
        .map_err(|e| AdminError::Upload(format!("multipart parse: {e}")))?
    {
        if field.name().unwrap_or("") == "file" {
            if let Some(fname) = field.file_name() {
                filename = sanitize_filename(fname);
            }
            bytes = field
                .bytes()
                .await
                .map_err(|e| AdminError::Upload(format!("read bytes: {e}")))?
                .to_vec();
        } else {
            let _ = field.bytes().await;
        }
    }
    if filename.is_empty() || bytes.is_empty() {
        return Err(AdminError::Upload("no file submitted".into()));
    }
    let mut hasher = Sha256::new();
    hasher.update(&bytes);
    let digest = hasher.finalize();
    let hash = digest.iter().fold(String::with_capacity(64), |mut acc, b| {
        use std::fmt::Write as _;
        let _ = write!(acc, "{b:02x}");
        acc
    });
    let mime = mime_guess::from_path(&filename)
        .first_or_octet_stream()
        .essence_str()
        .to_owned();
    refuse_active_upload(&mime)?;
    // #284 — dedup by content hash. If this tenant already has a
    // cms_media row whose `content_hash` matches the bytes the
    // editor just uploaded, skip writing to scratch + creating a
    // new UploadedFile row. The client treats the response as a
    // "skipped" outcome and surfaces it as a toast linking back to
    // the existing media row. Per-tenant query — the
    // `tenant.pool()` is the tenant-scoped pool, so a hash that
    // collides across tenants doesn't dedup across the boundary.
    {
        use rustango::core::Column as _;
        use rustango::sql::FetcherPool as _;
        let existing: Vec<crate::media::Media> = crate::media::Media::objects()
            .where_(crate::media::Media::content_hash.eq(hash.clone()))
            .fetch(tenant.pool())
            .await
            .unwrap_or_default();
        if let Some(existing_media) = existing.into_iter().next() {
            let existing_id = existing_media.id.get().copied().unwrap_or_default();
            let payload = serde_json::json!({
                "id": null,
                "duplicate_of": existing_id,
                "duplicate_title": existing_media.title,
                "duplicate_filename": existing_media.filename,
                "filename": filename,
                "mime": existing_media.mime,
                "size": existing_media.size,
            });
            return Ok((
                [(axum::http::header::CONTENT_TYPE, "application/json")],
                serde_json::to_string(&payload).unwrap_or_else(|_| "{}".to_owned()),
            )
                .into_response());
        }
    }
    let storage_key = format!("scratch/{}-{filename}", &hash[..16]);
    crate::media_storage::disk_for(&tenant.org.slug)
        .save(
            &crate::media_storage::key(&tenant.org.slug, &storage_key),
            &bytes,
        )
        .await
        .map_err(|e| AdminError::Upload(format!("write: {e}")))?;
    let size = bytes.len() as i64;
    let row = crate::uploaded_file::create(
        tenant.pool(),
        viewer_id,
        storage_key,
        filename,
        mime,
        size,
        hash,
    )
    .await?;
    let payload = serde_json::json!({
        "id": row.id.get().copied().unwrap_or_default(),
        "filename": row.original_filename,
        "mime": row.mime,
        "size": row.size,
    });
    Ok((
        [(axum::http::header::CONTENT_TYPE, "application/json")],
        serde_json::to_string(&payload).unwrap_or_else(|_| "{}".to_owned()),
    )
        .into_response())
}

/// One entry in the [`StagedCommitPayload`] commit grid.
#[derive(Debug, Deserialize)]
pub struct StagedCommitItem {
    pub uploaded_file_id: i64,
    #[serde(default)]
    pub title: String,
    #[serde(default)]
    pub alt_text: String,
    #[serde(default)]
    pub collection_id: Option<i64>,
}

/// Body of `POST /cms-admin/media/upload-staged/commit`. The
/// editor uploaded N files into scratch storage; now this payload
/// names each scratch row + its editorial metadata in one
/// transaction-equivalent call.
#[derive(Debug, Deserialize)]
pub struct StagedCommitPayload {
    pub items: Vec<StagedCommitItem>,
}

/// POST /cms-admin/media/upload-staged/commit — promote staged rows
/// to `cms_media` with per-file metadata. Each item:
///
/// 1. Loads the scratch row.
/// 2. Re-reads bytes from the scratch path; EXIF-strips images.
/// 3. Moves the file out of `scratch/` to the canonical
///    `<hash>-<filename>` storage_key.
/// 4. Inserts a `cms_media` row.
/// 5. Deletes the scratch DB row + (best-effort) the scratch file.
///
/// On a per-item failure, that scratch row is left in place so the
/// editor can retry; successful items still commit. The response is
/// JSON `{ committed: [media_id...], failed: [{id, error}...] }`.
pub async fn media_upload_staged_commit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Json(payload): Json<StagedCommitPayload>,
) -> Result<Response, AdminError> {
    use rustango::sql::Auto;
    let Some(viewer) = session_user.as_ref() else {
        // #285 — JSON-aware 401 for fetch clients.
        return Ok(unauthorized_no_session_for(Some(&headers)));
    };
    let viewer_id = viewer.id.get().copied().unwrap_or_default();
    let media_slug = tenant.org.slug.clone();

    let mut committed: Vec<i64> = Vec::new();
    let mut failed: Vec<serde_json::Value> = Vec::new();

    for item in payload.items {
        let result: Result<i64, String> = async {
            let row = crate::uploaded_file::get(tenant.pool(), item.uploaded_file_id)
                .await
                .map_err(|e| format!("lookup: {e}"))?
                .ok_or_else(|| "scratch row missing".to_owned())?;
            if row.user_id != viewer_id && !viewer.is_superuser {
                return Err("not your upload".to_owned());
            }

            let scratch_key = crate::media_storage::key(&media_slug, &row.storage_key);
            let mut bytes = crate::media_storage::disk_for(&media_slug)
                .load(&scratch_key)
                .await
                .map_err(|e| format!("read scratch: {e}"))?;

            // EXIF-strip + decode for pixel dims. Mirrors upload path.
            let (stored, decoded_dims) =
                crate::rendition::strip_exif_off_runtime(bytes, row.mime.clone()).await;
            bytes = stored;

            // Canonical storage_key under the tenant's media dir —
            // content-addressed prefix so renditions can find bytes
            // by hash without hitting the DB.
            let final_key = format!(
                "{}-{}",
                &row.content_hash[..16.min(row.content_hash.len())],
                row.original_filename
            );
            crate::media_storage::disk_for(&media_slug)
                .save(&crate::media_storage::key(&media_slug, &final_key), &bytes)
                .await
                .map_err(|e| format!("write final: {e}"))?;

            let kind = Media::classify_mime(&row.mime).to_owned();
            let (width, height): (Option<i32>, Option<i32>) = decoded_dims
                .map(|(w, h)| (Some(w as i32), Some(h as i32)))
                .unwrap_or((None, None));

            let title = if item.title.trim().is_empty() {
                row.original_filename
                    .rsplit_once('.')
                    .map(|(stem, _)| stem.to_owned())
                    .unwrap_or_else(|| row.original_filename.clone())
            } else {
                item.title.trim().to_owned()
            };

            let mut media_row = Media {
                id: Auto::Unset,
                filename: row.original_filename.clone(),
                content_hash: row.content_hash.clone(),
                mime: row.mime.clone(),
                size: bytes.len() as i64,
                kind,
                width,
                height,
                storage_key: final_key,
                title,
                alt_text: item.alt_text.trim().to_owned(),
                description: String::new(),
                uploaded_by: Some(viewer_id),
                collection_id: item.collection_id,
                focal_point_x: None,
                focal_point_y: None,
                uploaded_at: Auto::Unset,
            };
            media_row
                .insert_pool(tenant.pool())
                .await
                .map_err(|e| format!("insert media: {e}"))?;
            let new_id = media_row.id.get().copied().unwrap_or_default();

            // Best-effort scratch cleanup. Scratch file is a separate
            // path from the final storage_key (final lives outside
            // `scratch/`), so removing it is safe even when the bytes
            // are byte-identical.
            crate::media_storage::disk_for(&media_slug)
                .delete(&scratch_key)
                .await
                .log_warn("stored file not deleted; the blob is orphaned");
            crate::uploaded_file::delete(tenant.pool(), item.uploaded_file_id)
                .await
                .map_err(|e| format!("delete scratch: {e}"))?;
            Ok(new_id)
        }
        .await;

        match result {
            Ok(id) => committed.push(id),
            Err(e) => {
                failed.push(serde_json::json!({
                    "uploaded_file_id": item.uploaded_file_id,
                    "error": e,
                }));
            }
        }
    }

    let body = serde_json::json!({
        "committed": committed,
        "failed": failed,
    });
    Ok((
        [(axum::http::header::CONTENT_TYPE, "application/json")],
        serde_json::to_string(&body).unwrap_or_else(|_| "{}".to_owned()),
    )
        .into_response())
}

/// POST /cms-admin/media/upload-staged/{id}/cancel — drop scratch row
/// + unlink the file. Returns 204.
pub async fn media_upload_staged_cancel(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
) -> Result<Response, AdminError> {
    let Some(viewer) = session_user.as_ref() else {
        // #285 — JSON-aware 401 for fetch clients.
        return Ok(unauthorized_no_session_for(Some(&headers)));
    };
    let viewer_id = viewer.id.get().copied().unwrap_or_default();
    let Some(row) = crate::uploaded_file::get(tenant.pool(), id).await? else {
        return Ok((axum::http::StatusCode::NOT_FOUND, "not found").into_response());
    };
    if row.user_id != viewer_id && !viewer.is_superuser {
        return Ok((axum::http::StatusCode::FORBIDDEN, "not your upload").into_response());
    }
    crate::media_storage::disk_for(&tenant.org.slug)
        .delete(&crate::media_storage::key(
            &tenant.org.slug,
            &row.storage_key,
        ))
        .await
        .log_warn("stored file not deleted; the blob is orphaned");
    crate::uploaded_file::delete(tenant.pool(), id).await?;
    Ok((axum::http::StatusCode::NO_CONTENT, "").into_response())
}

/// Refuse a file a browser would run as a page: HTML, XHTML,
/// JavaScript and XML (which can carry script through XSLT or an XHTML
/// namespace). Serving neutralises every type already; this keeps such
/// files out of the library in the first place. SVG stays allowed — it is
/// served under a no-script CSP.
fn refuse_active_upload(mime: &str) -> Result<(), AdminError> {
    let active = matches!(
        mime,
        "text/html"
            | "application/xhtml+xml"
            | "text/javascript"
            | "application/javascript"
            | "application/ecmascript"
            | "text/xml"
            | "application/xml"
            | "text/xsl"
            | "application/xslt+xml"
    );
    if active {
        return Err(AdminError::Upload(format!(
            "files of type {mime} can't be uploaded: a browser would run them as a page"
        )));
    }
    Ok(())
}

/// Store one media file: sanitize the name, detect + classify the mime,
/// strip EXIF from images, hash, write
/// storage key `<org_slug>/<hash16>-<name>`, and insert the `cms_media`
/// row. The single canonical write path — the browser upload handlers and
/// the MCP `upload_media` tool all funnel through here.
///
/// `dedupe = true` short-circuits on an existing row with the same
/// content hash (returns it instead of storing a byte-identical copy) —
/// the same behavior the staged-upload flow gives the browser.
#[allow(clippy::too_many_arguments)]
pub(crate) async fn store_media(
    pool: &rustango::sql::Pool,
    org_slug: &str,
    mut bytes: Vec<u8>,
    raw_filename: &str,
    title: Option<&str>,
    alt_text: &str,
    collection_id: Option<i64>,
    uploaded_by: Option<i64>,
    dedupe: bool,
) -> Result<Media, AdminError> {
    use rustango::sql::Auto;
    use sha2::{Digest, Sha256};

    let filename = sanitize_filename(raw_filename);
    if filename.is_empty() || bytes.is_empty() {
        return Err(AdminError::Upload("no file submitted".into()));
    }

    let mime = mime_guess::from_path(&filename)
        .first_or_octet_stream()
        .essence_str()
        .to_owned();
    refuse_active_upload(&mime)?;
    let kind = Media::classify_mime(&mime).to_owned();

    // #184 — strip EXIF on image upload, BEFORE hashing so the hash
    // reflects the stored bytes. See `media_upload_submit` for the why.
    let (stored, decoded_dims) = crate::rendition::strip_exif_off_runtime(bytes, mime.clone()).await;
    bytes = stored;

    let mut hasher = Sha256::new();
    hasher.update(&bytes);
    let digest = hasher.finalize();
    let hash = digest.iter().fold(String::with_capacity(64), |mut acc, b| {
        use std::fmt::Write as _;
        let _ = write!(acc, "{b:02x}");
        acc
    });

    if dedupe {
        if let Some(existing) = Media::objects()
            .where_(Media::content_hash.eq(hash.clone()))
            .first(pool)
            .await?
        {
            return Ok(existing);
        }
    }

    // Routed through the pluggable storage backend (Slice 8.x).
    let storage_key = format!("{}-{}", &hash[..16], filename);
    crate::media_storage::disk_for(&org_slug)
        .save(&crate::media_storage::key(org_slug, &storage_key), &bytes)
        .await
        .map_err(|e| AdminError::Upload(format!("write: {e}")))?;

    let title = match title {
        Some(t) if !t.is_empty() => t.to_owned(),
        _ => filename
            .rsplit_once('.')
            .map(|(stem, _)| stem.to_owned())
            .unwrap_or_else(|| filename.clone()),
    };

    // Pixel dimensions fall out of the EXIF-strip decode; non-images /
    // broken decodes get `None`.
    let (width, height): (Option<i32>, Option<i32>) = decoded_dims
        .map(|(w, h)| (Some(w as i32), Some(h as i32)))
        .unwrap_or((None, None));

    let mut row = Media {
        id: Auto::Unset,
        filename,
        content_hash: hash,
        mime,
        size: bytes.len() as i64,
        kind,
        width,
        height,
        storage_key,
        title,
        alt_text: alt_text.to_owned(),
        description: String::new(),
        uploaded_by,
        collection_id,
        focal_point_x: None,
        focal_point_y: None,
        uploaded_at: Auto::Unset,
    };
    row.insert_pool(pool).await?;
    Ok(row)
}

pub async fn media_upload_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    mut multipart: axum::extract::Multipart,
) -> Result<Response, AdminError> {
    let mut filename = String::new();
    let mut bytes: Vec<u8> = Vec::new();
    let mut title = String::new();
    let mut alt_text = String::new();
    // #24 V2 — upload-time crop. When the client submits all four
    // crop coords (source-image pixel space), we decode + crop the
    // bytes before storing so the on-disk canonical file matches the
    // editor's intent. Absence of any of the four = no crop.
    let mut crop_x: Option<u32> = None;
    let mut crop_y: Option<u32> = None;
    let mut crop_w: Option<u32> = None;
    let mut crop_h: Option<u32> = None;

    while let Some(field) = multipart
        .next_field()
        .await
        .map_err(|e| AdminError::Upload(format!("multipart parse: {e}")))?
    {
        let name = field.name().unwrap_or("").to_owned();
        match name.as_str() {
            "file" => {
                if let Some(fname) = field.file_name() {
                    filename = sanitize_filename(fname);
                }
                bytes = field
                    .bytes()
                    .await
                    .map_err(|e| AdminError::Upload(format!("read bytes: {e}")))?
                    .to_vec();
            }
            "title" => {
                title = field
                    .text()
                    .await
                    .map_err(|e| AdminError::Upload(format!("read title: {e}")))?;
            }
            "alt_text" => {
                alt_text = field
                    .text()
                    .await
                    .map_err(|e| AdminError::Upload(format!("read alt_text: {e}")))?;
            }
            "crop_x" | "crop_y" | "crop_w" | "crop_h" => {
                let value = field
                    .text()
                    .await
                    .map_err(|e| AdminError::Upload(format!("read {name}: {e}")))?;
                let parsed: Option<u32> = value.trim().parse().ok();
                match name.as_str() {
                    "crop_x" => crop_x = parsed,
                    "crop_y" => crop_y = parsed,
                    "crop_w" => crop_w = parsed,
                    "crop_h" => crop_h = parsed,
                    _ => {}
                }
            }
            _ => {
                let _ = field.bytes().await;
            }
        }
    }

    if filename.is_empty() || bytes.is_empty() {
        return Err(AdminError::Upload("no file submitted".into()));
    }

    // Apply crop if all four coords arrived. Image-only — vector
    // sources can't be raster-cropped (mirrors the edit-time rule
    // in the V1 crop handler).
    if let (Some(x), Some(y), Some(w), Some(h)) = (crop_x, crop_y, crop_w, crop_h) {
        if w > 0 && h > 0 {
            let detected_mime = mime_guess::from_path(&filename)
                .first_or_octet_stream()
                .essence_str()
                .to_owned();
            refuse_active_upload(&detected_mime)?;
            if detected_mime == "image/svg+xml" {
                return Err(AdminError::Validation(
                    "Cropping SVG sources isn't supported; upload uncropped, then edit the markup."
                        .to_owned(),
                ));
            }
            if detected_mime.starts_with("image/") {
                let (cropped, _out_mime) = crate::rendition::crop_image_off_runtime(
                    bytes,
                    detected_mime.clone(),
                    (x, y, w, h),
                )
                .await
                .map_err(AdminError::Validation)?;
                bytes = cropped;
            }
        }
    }

    // This legacy single-file path ignores any collection choice (the
    // per-file picker lives on the staged upload flow): rows land in
    // "uncategorized" (NULL) under the root collection.
    let row = store_media(
        tenant.pool(),
        &tenant.org.slug,
        bytes,
        &filename,
        (!title.is_empty()).then_some(title.as_str()),
        &alt_text,
        None,
        None,
        false,
    )
    .await?;

    let redirect = if row.kind == "image" {
        "/cms-admin/media"
    } else {
        "/cms-admin/documents"
    };
    Ok(Redirect::to(redirect).into_response())
}

/// POST /cms-admin/media/{id}/delete — drop a media row.
pub async fn media_delete_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    Path(id): Path<i64>,
) -> Result<Response, AdminError> {
    let row = Media::objects()
        .where_(Media::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    // Best-effort storage cleanup — a missing object is not fatal.
    crate::media_storage::disk_for(&tenant.org.slug)
        .delete(&crate::media_storage::key(
            &tenant.org.slug,
            &row.storage_key,
        ))
        .await
        .log_warn("stored file not deleted; the blob is orphaned");
    let redirect = if row.kind == "image" {
        "/cms-admin/media"
    } else {
        "/cms-admin/documents"
    };
    row.delete_pool(tenant.pool()).await?;
    Ok(Redirect::to(redirect).into_response())
}

fn sanitize_filename(raw: &str) -> String {
    raw.chars()
        .map(|c| {
            if c.is_ascii_alphanumeric() || matches!(c, '.' | '-' | '_') {
                c
            } else {
                '_'
            }
        })
        .collect()
}

// =====================================================================
// Locales tab
// =====================================================================

/// GET /cms-admin/locales — list every locale.
/// GET /cms-admin/model/{slug} — generic read-only index for a
/// model registered via `register_model_admin!`: columns come from the
/// model's `admin(list_display = …)`, rows from its type-erased
/// list-rows thunk, all inside the CMS admin chrome. 404s an unknown
/// slug.
pub async fn model_admin_list(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(slug): Path<String>,
) -> Result<Response, AdminError> {
    let Some(ma) = super::model_admin::find(&slug) else {
        return Ok((axum::http::StatusCode::NOT_FOUND, "unknown model admin").into_response());
    };
    let rows_json = (ma.list_rows)(tenant.pool().clone()).await;
    let columns = super::model_admin::list_columns(ma.schema);
    let header_labels: Vec<&str> = columns.iter().map(|(_, l)| l.as_str()).collect();
    let body_rows: Vec<Vec<String>> = rows_json
        .iter()
        .map(|r| {
            columns
                .iter()
                .map(|(name, _)| super::model_admin::row_cell(r, name))
                .collect()
        })
        .collect();
    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "model-admin", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("model_label", ma.label);
    ctx.insert("model_slug", &slug);
    ctx.insert("columns", &header_labels);
    ctx.insert("rows", &body_rows);
    ctx.insert("total", &body_rows.len());
    render_with_csrf(
        &state,
        &headers,
        "rcms_admin/model_admin_list.html",
        &mut ctx,
    )
}

/// `?format=csv` selector for [`report_view`].
#[derive(serde::Deserialize, Default)]
pub struct ReportQuery {
    pub format: Option<String>,
}

/// Render any registered report through the shared chrome'd
/// table, or export it as CSV with `?format=csv`. `active_tab` is
/// `report:<slug>` so `_base.html` highlights the matching sidebar link.
pub async fn report_view(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(slug): Path<String>,
    Query(q): Query<ReportQuery>,
) -> Result<Response, AdminError> {
    let Some(report) = super::report::find_report(&slug) else {
        return Ok((axum::http::StatusCode::NOT_FOUND, "unknown report").into_response());
    };
    let user_id = session_user.as_ref().and_then(|u| u.id.get().copied());
    let columns = report.columns();
    let rows = report.rows(tenant.pool(), user_id).await;

    // CSV export — same rows, flattened to column order.
    if q.format.as_deref() == Some("csv") {
        let mut out = String::with_capacity(128 + rows.len() * 64);
        let header: Vec<String> = columns.iter().map(|c| csv_escape(c.label)).collect();
        out.push_str(&header.join(","));
        out.push('\n');
        for row in &rows {
            let line: Vec<String> = columns
                .iter()
                .map(|c| csv_escape(&report_cell_string(row.get(c.key))))
                .collect();
            out.push_str(&line.join(","));
            out.push('\n');
        }
        return Ok(csv_response(&slug, &out));
    }

    let header_labels: Vec<&str> = columns.iter().map(|c| c.label).collect();
    let view_rows: Vec<serde_json::Value> = rows
        .iter()
        .map(|row| {
            let cells: Vec<serde_json::Value> = columns
                .iter()
                .map(|c| {
                    serde_json::json!({
                        "kind": c.kind.as_str(),
                        "value": report_cell_string(row.get(c.key)),
                    })
                })
                .collect();
            serde_json::json!({
                "edit_url": row.get("edit_url").and_then(|v| v.as_str()),
                "cells": cells,
            })
        })
        .collect();

    let mut ctx = Context::new();
    add_chrome(
        &mut ctx,
        &tenant,
        &format!("report:{slug}"),
        session_user.as_ref(),
    )
    .await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("report_title", report.title());
    ctx.insert("report_slug", &slug);
    ctx.insert("report_description", &report.description());
    ctx.insert("columns", &header_labels);
    ctx.insert("rows", &view_rows);
    ctx.insert("total", &view_rows.len());
    render_with_csrf(&state, &headers, "rcms_admin/report.html", &mut ctx)
}

// ===================================================================== Form builder (#533)

/// GET `/cms-admin/forms` — dedicated Forms list: every form snippet with its
/// per-form submission count and last-submission time, linking straight into
/// the builder and the submission history. This exists because the generic
/// library table can't surface submissions (`LibraryTypeHandler::render_cell`
/// is sync and pool-less), which left "Forms" dead-ending on a Library view
/// with no way to see what a form collected.
pub async fn forms_list(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
) -> Result<Response, AdminError> {
    let pool = tenant.pool();
    let rows = forms_list_rows(pool).await?;

    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "forms", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, pool).await;
    ctx.insert("rows", &rows);
    ctx.insert("total", &rows.len());
    render_with_csrf(&state, &headers, "rcms_admin/forms_list.html", &mut ctx)
}

/// Core of [`forms_list`] — the row model (per-form field count, submission
/// count, latest submission, action URLs), split out so unit tests can drive
/// it with an in-memory pool.
pub(crate) async fn forms_list_rows(
    pool: &rustango::sql::Pool,
) -> Result<Vec<serde_json::Value>, AdminError> {
    let mut forms: Vec<crate::snippet::Snippet> = crate::snippet::Snippet::objects()
        .where_(crate::snippet::Snippet::type_name.eq("form".to_owned()))
        .fetch(pool)
        .await?;
    // Newest-edited first.
    forms.sort_by(|a, b| {
        b.updated_at
            .get()
            .cloned()
            .cmp(&a.updated_at.get().cloned())
    });

    // Per-form submission count + latest submission, grouped in Rust — the
    // tri-dialect pattern used by `counts_by_type` / the pages `child_counts`
    // (no SQL GROUP BY helper in the codebase).
    let entries: Vec<crate::forms::submit::FormEntry> = crate::forms::submit::FormEntry::objects()
        .fetch(pool)
        .await?;
    let mut counts: std::collections::HashMap<i64, i64> = std::collections::HashMap::new();
    let mut last_at: std::collections::HashMap<i64, String> = std::collections::HashMap::new();
    for e in &entries {
        *counts.entry(e.form_snippet_id).or_default() += 1;
        if let Some(at) = e.submitted_at.get() {
            let s = at.format("%Y-%m-%d %H:%M").to_string();
            let slot = last_at.entry(e.form_snippet_id).or_default();
            if s > *slot {
                *slot = s;
            }
        }
    }

    let rows: Vec<serde_json::Value> = forms
        .iter()
        .map(|s| {
            let id = s.id.get().copied().unwrap_or_default();
            let parsed = crate::forms::schema::parse(&s.data).unwrap_or_default();
            serde_json::json!({
                "id": id,
                "title": s.title,
                "slug": s.slug,
                "fields": parsed.field_count(),
                "submissions": counts.get(&id).copied().unwrap_or(0),
                "last_submission": last_at.get(&id).cloned().unwrap_or_default(),
                "updated_at": s.updated_at.get().map(|d| d.format("%Y-%m-%d").to_string()).unwrap_or_default(),
                "build_url": format!("/cms-admin/forms/{id}/build"),
                "submissions_url": format!("/cms-admin/forms/{id}/submissions"),
                "delete_url": format!("/cms-admin/library/{id}/delete"),
            })
        })
        .collect();
    Ok(rows)
}

/// GET `/cms-admin/forms/{id}/build` — the visual form builder editor.
/// Loads the form snippet's schema JSON into the page; the
/// `form_builder.js` engine renders the editor from it.
pub async fn form_build_form(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
    Query(q): Query<EditQuery>,
) -> Result<Response, AdminError> {
    let snippet = crate::snippet::Snippet::objects()
        .where_(crate::snippet::Snippet::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    if snippet.type_name != "form" {
        return Err(AdminError::NotFound(id));
    }
    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "forms", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("title", &snippet.title);
    ctx.insert("form_id", &id);
    ctx.insert("build_url", &format!("/cms-admin/forms/{id}/build"));

    // #550 FB-17 — active non-default locales for the Translate picker.
    let locales: Vec<crate::locale::Locale> = crate::locale::Locale::objects()
        .where_(crate::locale::Locale::active.eq(true))
        .where_(crate::locale::Locale::is_default.eq(false))
        .fetch(tenant.pool())
        .await
        .unwrap_or_default();
    ctx.insert(
        "locales",
        &locales
            .iter()
            .map(|l| serde_json::json!({"code": l.code, "name": l.name}))
            .collect::<Vec<_>>(),
    );

    // Translate mode: ?locale=<code> renders a focused per-leaf editor that
    // posts to the existing snippet-translate endpoint.
    if let Some(code) = q.locale.as_deref().filter(|c| !c.is_empty()) {
        if let Some(loc) = locales.iter().find(|l| l.code == code) {
            let lid = loc.id.get().copied().unwrap_or(0);
            let form = crate::forms::schema::parse(&snippet.data).unwrap_or_default();
            let leaves = crate::forms::schema::translatable_leaves(&form);
            let existing = crate::snippet_translation::fetch_for(tenant.pool(), id, lid)
                .await
                .unwrap_or_default();
            // Consecutive leaves of one field form a group, like the
            // block groups of the page editor.
            let mut groups: Vec<serde_json::Value> = Vec::new();
            for l in &leaves {
                let row = serde_json::json!({
                    "path": l.path,
                    "part": l.part,
                    "n": l.n.to_string(),
                    "canonical": l.text,
                    "value": existing.get(&l.path).cloned().unwrap_or_default(),
                });
                let same = groups
                    .last()
                    .is_some_and(|g| g["name"].as_str() == Some(l.group.as_str()));
                if same {
                    if let Some(arr) = groups
                        .last_mut()
                        .and_then(|g| g.get_mut("leaves"))
                        .and_then(|v| v.as_array_mut())
                    {
                        arr.push(row);
                    }
                } else {
                    groups.push(serde_json::json!({ "name": l.group, "leaves": [row] }));
                }
            }
            ctx.insert("editing_locale", &loc.name);
            ctx.insert("editing_code", &code);
            ctx.insert("trans_groups", &groups);
            ctx.insert(
                "translate_url",
                &format!("/cms-admin/library/{id}/translate"),
            );
            return render_with_csrf(&state, &headers, "rcms_admin/form_translate.html", &mut ctx);
        }
    }

    // The builder's back button says "Forms", so it has to land on the
    // Forms list. Pointing it at the generic Library view dropped editors
    // onto a different listing of the same rows — its own search, its own
    // column picker, no Submissions column — which read as a second,
    // older Forms screen. Every other builder points at its own list.
    ctx.insert("list_url", "/cms-admin/forms");
    // FB-15 — the builder edits the DRAFT; Publish promotes it to live.
    ctx.insert("publish_url", &format!("/cms-admin/forms/{id}/publish"));
    ctx.insert(
        "has_unpublished",
        &crate::forms::schema::has_unpublished_changes(&snippet.data),
    );
    let mut draft = crate::forms::schema::parse_draft(&snippet.data);
    draft.sanitize_rich_text();
    let schema_json = serde_json::to_string(&crate::forms::schema::to_value(&draft))
        .unwrap_or_else(|_| "{}".to_owned());
    ctx.insert("schema_json", &schema_json);
    render_with_csrf(&state, &headers, "rcms_admin/form_builder.html", &mut ctx)
}

#[derive(Debug, Deserialize)]
pub struct FormBuildSubmit {
    pub schema: String,
}

/// Create-a-page-type submit. `type_name` is slugified server-side.
#[derive(Debug, Deserialize)]
pub struct FormPageTypeCreate {
    #[serde(default)]
    pub verbose_name: String,
    #[serde(default)]
    pub type_name: String,
    #[serde(default)]
    pub allowed_parents: String,
    #[serde(default)]
    pub allowed_children: String,
    /// `"auto" | "html" | "api"` — see [`crate::page_view::PageViewMode`].
    /// Offered only for UI types: a code type's mode is re-seeded from
    /// its handler on every boot, so an admin edit would silently revert.
    #[serde(default)]
    pub view_mode: String,
    /// Workflow name, or empty to publish directly.
    #[serde(default)]
    pub workflow: String,
}

/// Component builder submit: builder `schema` (a `{nodes:[…]}` blob)
/// plus the component metadata fields.
#[derive(Debug, Deserialize)]
pub struct FormComponentSubmit {
    #[serde(default)]
    pub slug: String,
    #[serde(default)]
    pub label: String,
    #[serde(default)]
    pub icon: String,
    #[serde(default)]
    pub description: String,
    #[serde(default)]
    pub schema: String,
}

/// POST `/cms-admin/forms/{id}/build` — persist the builder's schema into
/// GET|POST /cms-admin/forms/{id}/preview — render this form the way a
/// visitor sees it, for the editor's sidebar preview.
///
/// GET renders the saved draft; POST renders the schema in the request
/// body, which is how the builder shows unsaved edits — the same
/// saved-vs-unsaved split the page preview uses.
///
/// Crucially this calls [`crate::forms::render::render_form_html`] — the
/// one the public site calls. The builder used to draw its own preview in
/// JavaScript, rebuilding the layout a second time in a second language;
/// the two could only agree by coincidence, and every renderer change had
/// to be made twice or the preview quietly started lying.
pub async fn form_preview(
    tenant: Tenant,
    Path(id): Path<i64>,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
) -> Result<Response, AdminError> {
    let snippet = load_form_snippet(&tenant, id).await?;
    let form = crate::forms::schema::parse_draft(&snippet.data);
    Ok(render_form_preview(id, &form))
}

/// POST /cms-admin/forms/{id}/preview — render the schema in the body, so
/// the builder previews edits that have not been saved. Mirrors
/// `page_preview_draft`.
pub async fn form_preview_draft(
    tenant: Tenant,
    Path(id): Path<i64>,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Form(body): Form<FormBuildSubmit>,
) -> Result<Response, AdminError> {
    let snippet = load_form_snippet(&tenant, id).await?;
    // A schema that does not parse is the normal state mid-edit, not an
    // error worth blanking the pane for: fall back to the saved draft and
    // let the editor keep typing.
    let form = crate::forms::schema::parse_str(&body.schema)
        .unwrap_or_else(|_| crate::forms::schema::parse_draft(&snippet.data));
    Ok(render_form_preview(id, &form))
}

/// The snippet behind a form id, refusing anything that is not a form.
async fn load_form_snippet(
    tenant: &Tenant,
    id: i64,
) -> Result<crate::snippet::Snippet, AdminError> {
    let snippet = crate::snippet::Snippet::objects()
        .where_(crate::snippet::Snippet::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    if snippet.type_name != "form" {
        return Err(AdminError::NotFound(id));
    }
    Ok(snippet)
}

fn render_form_preview(id: i64, form: &crate::forms::schema::Form) -> Response {
    // `embed_id` is empty: this is the standalone render, with no page
    // embedding it and so no per-embed override to resolve.
    let inner = crate::forms::render::render_form_html(form, id, "");
    let html = format!(
        "<!doctype html><html lang=\"en\"><head><meta charset=\"utf-8\">\
         <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\
         <title>Form preview</title>\
         <style>body{{margin:0;padding:24px;font:14px/1.5 system-ui,sans-serif;}}</style>\
         </head><body>{inner}</body></html>"
    );
    stamp_no_cache(
        (
            [(
                axum::http::header::CONTENT_TYPE,
                axum::http::HeaderValue::from_static("text/html; charset=utf-8"),
            )],
            html,
        )
            .into_response(),
    )
}

/// the snippet's `data` column + capture a revision.
pub async fn form_build_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
    Form(form): Form<FormBuildSubmit>,
) -> Result<Response, AdminError> {
    let mut snippet = crate::snippet::Snippet::objects()
        .where_(crate::snippet::Snippet::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    if snippet.type_name != "form" {
        return Err(AdminError::NotFound(id));
    }
    let mut parsed = crate::forms::schema::parse_str(&form.schema)
        .map_err(|e| AdminError::Validation(format!("invalid form schema: {e}")))?;
    parsed.sanitize_rich_text();
    let mut warnings = crate::forms::schema::validate(&parsed);
    // FB-15 — saving stores a DRAFT; the published (public) form is untouched
    // until Publish.
    snippet.data = crate::forms::schema::save_draft_value(&snippet.data, &parsed);
    snippet.save_pool(tenant.pool()).await?;
    // Keep the notification target in step with the recipients typed into
    // form settings, so the two cannot disagree about who gets told.
    if let Err(e) = crate::notify::targets::sync_form_email(
        tenant.pool(),
        id,
        &snippet.title,
        &parsed.settings.notify_emails,
    )
    .await
    {
        tracing::warn!(
            target: "rustango_cms::notify",
            error = %e, form_id = id,
            "could not sync the form's email notification target"
        );
        warnings.push(format!("Email notifications are off: {e}."));
    }
    let (level, msg) = if warnings.is_empty() {
        (MsgLevel::Success, "Draft saved.".to_owned())
    } else {
        (
            MsgLevel::Warning,
            format!("Draft saved with warnings: {}", warnings.join(" ")),
        )
    };
    redirect_named_with_params_and_message(
        "rcms-admin:forms:build",
        &[("id", id.to_string())],
        level,
        &msg,
        &headers,
    )
}

/// POST `/cms-admin/forms/{id}/publish` — publish the submitted schema: it
/// becomes the live (public) form + the draft, and a revision is captured.
pub async fn form_publish_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
    Form(form): Form<FormBuildSubmit>,
) -> Result<Response, AdminError> {
    let mut snippet = crate::snippet::Snippet::objects()
        .where_(crate::snippet::Snippet::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    if snippet.type_name != "form" {
        return Err(AdminError::NotFound(id));
    }
    let mut parsed = crate::forms::schema::parse_str(&form.schema)
        .map_err(|e| AdminError::Validation(format!("invalid form schema: {e}")))?;
    parsed.sanitize_rich_text();
    snippet.data = crate::forms::schema::publish_value(&parsed);
    snippet.save_pool(tenant.pool()).await?;
    if let Some(h) = crate::library::find_handler(&snippet.type_name) {
        if h.revisions_enabled() {
            let uid = session_user.as_ref().and_then(|u| u.id.get().copied());
            crate::snippet::capture_revision(tenant.pool(), &snippet, uid)
                .await
                .log_warn("revision not captured; the history has a gap");
        }
    }
    redirect_named_with_params_and_message(
        "rcms-admin:forms:build",
        &[("id", id.to_string())],
        MsgLevel::Success,
        "Form published — the live form is now up to date.",
        &headers,
    )
}

/// Shared loader for the submissions views: the form
/// snippet, its parsed schema's value-columns, the entries, and a
/// source-page-id → url_path map for provenance links.
async fn load_form_submissions(
    pool: &rustango::sql::Pool,
    form_id: i64,
) -> Result<
    (
        crate::snippet::Snippet,
        Vec<(String, String)>,
        Vec<crate::forms::submit::FormEntry>,
        std::collections::HashMap<i64, String>,
    ),
    AdminError,
> {
    let snippet = crate::snippet::Snippet::objects()
        .where_(crate::snippet::Snippet::id.eq(form_id))
        .where_(crate::snippet::Snippet::type_name.eq("form".to_owned()))
        .first(pool)
        .await?
        .ok_or(AdminError::NotFound(form_id))?;
    let form = crate::forms::schema::parse(&snippet.data).unwrap_or_default();
    let columns: Vec<(String, String)> = form
        .fields()
        .filter(|f| f.field_type.collects_value() && !f.key.is_empty())
        .map(|f| {
            (
                f.key.clone(),
                if f.label.is_empty() {
                    f.key.clone()
                } else {
                    f.label.clone()
                },
            )
        })
        .collect();
    let entries: Vec<crate::forms::submit::FormEntry> = crate::forms::submit::FormEntry::objects()
        .where_(crate::forms::submit::FormEntry::form_snippet_id.eq(form_id))
        .order_by(&[("submitted_at", true)])
        .fetch(pool)
        .await?;
    // Resolve source pages for provenance links.
    let mut page_urls = std::collections::HashMap::new();
    let ids: Vec<i64> = entries
        .iter()
        .map(|e| e.source_page_id)
        .filter(|i| *i != 0)
        .collect();
    if !ids.is_empty() {
        if let Ok(pages) = crate::page::Page::objects().fetch(pool).await {
            for p in pages {
                if let Some(pid) = p.id.get().copied() {
                    if ids.contains(&pid) {
                        page_urls.insert(pid, p.url_path.clone());
                    }
                }
            }
        }
    }
    Ok((snippet, columns, entries, page_urls))
}

/// Render one submission answer cell from the `data_json` value.
fn submission_cell(data: &serde_json::Value, key: &str) -> String {
    match data.get(key) {
        Some(serde_json::Value::Array(items)) => items
            .iter()
            .filter_map(|v| v.as_str().map(str::to_owned))
            .collect::<Vec<_>>()
            .join("; "),
        Some(serde_json::Value::String(s)) => s.clone(),
        Some(other) => other.to_string(),
        None => String::new(),
    }
}

/// GET `/cms-admin/forms/{id}/submissions` — the per-form submission
/// history with a Source-page column.
pub async fn form_submissions_list(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
) -> Result<Response, AdminError> {
    let (snippet, columns, entries, page_urls) = load_form_submissions(tenant.pool(), id).await?;
    // Which columns are file uploads → render those cells as download links.
    let parsed = crate::forms::schema::parse(&snippet.data).unwrap_or_default();
    let file_keys: std::collections::HashSet<String> = parsed
        .fields()
        .filter(|f| matches!(f.field_type, crate::forms::schema::FieldType::File))
        .map(|f| f.key.clone())
        .collect();
    let rows: Vec<serde_json::Value> = entries
        .iter()
        .map(|e| {
            let cells: Vec<serde_json::Value> = columns
                .iter()
                .map(|(k, _)| {
                    let v = submission_cell(&e.data_json, k);
                    if file_keys.contains(k) && !v.is_empty() {
                        // value is the storage key; display the filename part.
                        let name = v.splitn(2, '-').nth(1).unwrap_or(&v).to_owned();
                        serde_json::json!({
                            "value": name,
                            "url": format!("/cms-admin/forms/{id}/submissions/file/{v}"),
                        })
                    } else {
                        serde_json::json!({ "value": v })
                    }
                })
                .collect();
            serde_json::json!({
                // RFC 3339, for the admin's `<time data-utc>` localizer; the
                // minute-precision text is what shows without JavaScript.
                "submitted_at": e
                    .submitted_at
                    .get()
                    .map(|t| t.to_rfc3339_opts(chrono::SecondsFormat::Secs, true))
                    .unwrap_or_default(),
                "submitted_at_text": e
                    .submitted_at
                    .get()
                    .map(|t| t.format("%Y-%m-%d %H:%M UTC").to_string())
                    .unwrap_or_default(),
                "ip": e.ip,
                "source_page": if e.source_page_id == 0 {
                    serde_json::Value::Null
                } else {
                    serde_json::json!({
                        "id": e.source_page_id,
                        "url": page_urls.get(&e.source_page_id).cloned().unwrap_or_default(),
                    })
                },
                "cells": cells,
            })
        })
        .collect();
    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "forms", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("form_title", &snippet.title);
    ctx.insert("form_id", &id);
    ctx.insert("build_url", &format!("/cms-admin/forms/{id}/build"));
    ctx.insert(
        "csv_url",
        &format!("/cms-admin/forms/{id}/submissions/export.csv"),
    );
    ctx.insert(
        "columns",
        &columns.iter().map(|(_, l)| l).collect::<Vec<_>>(),
    );
    ctx.insert("rows", &rows);
    ctx.insert("total", &entries.len());
    render_with_csrf(
        &state,
        &headers,
        "rcms_admin/form_submissions.html",
        &mut ctx,
    )
}

/// GET `/cms-admin/forms/{id}/submissions/export.csv`.
pub async fn form_submissions_csv(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    Path(id): Path<i64>,
) -> Result<Response, AdminError> {
    let (snippet, columns, entries, page_urls) = load_form_submissions(tenant.pool(), id).await?;
    let mut out = String::new();
    out.push_str("submitted_at,source_page,ip");
    for (_, label) in &columns {
        out.push(',');
        out.push_str(&csv_escape(label));
    }
    out.push('\n');
    for e in &entries {
        let when = e
            .submitted_at
            .get()
            .map(ToString::to_string)
            .unwrap_or_default();
        let src = if e.source_page_id == 0 {
            String::new()
        } else {
            page_urls
                .get(&e.source_page_id)
                .cloned()
                .unwrap_or_default()
        };
        out.push_str(&csv_escape(&when));
        out.push(',');
        out.push_str(&csv_escape(&src));
        out.push(',');
        out.push_str(&csv_escape(&e.ip));
        for (k, _) in &columns {
            out.push(',');
            out.push_str(&csv_escape(&submission_cell(&e.data_json, k)));
        }
        out.push('\n');
    }
    let filename = format!("submissions-{}.csv", snippet.slug);
    Ok(csv_response(&filename, &out))
}

/// GET `/cms-admin/forms/{id}/submissions/file/{key}` — download a file
/// uploaded through a form submission (admin-gated). Streams from
/// `./var/form-uploads/<tenant>/<key>`; rejects path traversal.
pub async fn form_submission_file(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Path((_id, key)): Path<(i64, String)>,
) -> Result<Response, AdminError> {
    if key.contains('/') || key.contains('\\') || key.contains("..") || key.is_empty() {
        return Err(AdminError::NotFound(0));
    }
    let path = std::path::PathBuf::from("./var/form-uploads")
        .join(&tenant.org.slug)
        .join(&key);
    let bytes = std::fs::read(&path).map_err(|_| AdminError::NotFound(0))?;
    let mime = mime_guess::from_path(&key)
        .first_or_octet_stream()
        .essence_str()
        .to_owned();
    let download = key.splitn(2, '-').nth(1).unwrap_or(&key).to_owned();
    Ok((
        [
            (header::CONTENT_TYPE, mime),
            (
                header::CONTENT_DISPOSITION,
                format!("attachment; filename=\"{download}\""),
            ),
        ],
        bytes,
    )
        .into_response())
}

#[derive(Debug, Deserialize)]
pub struct SetLangQuery {
    pub lang: String,
}

/// POST `/cms-admin/set-language` (`lang=<code>`) — persist the sticky
/// admin-UI language cookie and the user's durable preference, then
/// redirect back to the Referer. POST so a cross-site link can't
/// rewrite a signed-in user's preference: it goes through the CSRF check
/// like every other admin write.
pub async fn admin_set_language(
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Form(q): Form<SetLangQuery>,
) -> Response {
    let lang = if super::i18n::is_ui_locale(&q.lang) {
        q.lang
    } else {
        "en".to_owned()
    };
    // #526 — persist to the user's durable pref too, so the switcher choice
    // follows them across devices (not just this browser's cookie). Best-effort:
    // a save failure still sets the cookie below.
    if let Some(uid) = session_user.as_ref().and_then(|u| u.id.get().copied()) {
        persist_user_admin_lang(tenant.pool(), uid, &lang)
            .await
            .log_warn("admin language preference not saved");
    }
    // The Referer's path, if it is a local one, else the admin root. The
    // path of `https://evil.test//evil.test/x` is `//evil.test/x`, so it
    // goes through safe_next like any other redirect target (#728).
    let back = headers
        .get(header::REFERER)
        .and_then(|v| v.to_str().ok())
        .and_then(|r| match r.split_once("://") {
            Some((_, rest)) => rest.find('/').map(|i| &rest[i..]),
            None => Some(r),
        })
        .and_then(rustango::auth_decorators::safe_next)
        .unwrap_or_else(|| "/cms-admin/".to_owned());
    let cookie = format!(
        "{}={lang}; Path=/; SameSite=Lax; Max-Age=31536000",
        super::i18n::ADMIN_LANG_COOKIE
    );
    let mut resp = axum::response::Redirect::to(&back).into_response();
    if let Ok(v) = HeaderValue::from_str(&cookie) {
        resp.headers_mut().append(header::SET_COOKIE, v);
    }
    resp
}

/// RFC-4180 CSV field escaping: quote when the value contains a comma,
/// quote, or newline, doubling any embedded quotes. Shared by every
/// admin CSV export (reports, audit log, pages, redirects, …).
fn csv_escape(s: &str) -> String {
    if s.contains(',') || s.contains('"') || s.contains('\n') || s.contains('\r') {
        let mut out = String::with_capacity(s.len() + 2);
        out.push('"');
        for c in s.chars() {
            if c == '"' {
                out.push('"');
            }
            out.push(c);
        }
        out.push('"');
        out
    } else {
        s.to_owned()
    }
}

/// Stringify a report cell value for display / CSV: strings pass
/// through; null / missing → empty; numbers + bools → their literal.
fn report_cell_string(v: Option<&serde_json::Value>) -> String {
    match v {
        Some(serde_json::Value::String(s)) => s.clone(),
        None | Some(serde_json::Value::Null) => String::new(),
        Some(other) => other.to_string(),
    }
}

pub async fn locale_list(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
) -> Result<Response, AdminError> {
    let items: Vec<Locale> = Locale::objects()
        .order_by(&[("sort_order", false), ("code", false)])
        .fetch(tenant.pool())
        .await?;
    // Enrich each row with core's static support level so the list can warn
    // where a content locale outruns the framework (no admin-UI catalog, no
    // language-specific plural rule, unknown to core). Serialize the row then
    // attach `support` — keeps every existing template field working.
    let rows: Vec<serde_json::Value> = items
        .iter()
        .map(|l| {
            let info = rustango::i18n::locale_info(&l.code);
            let ui = super::i18n::is_ui_locale(&l.code);
            let mut v = serde_json::to_value(l).unwrap_or_default();
            v["support"] = serde_json::json!({
                "ui_translated": ui,
                "known": info.known,
                "display_name": info.display_name,
                "has_plural_rules": info.has_plural_rules,
                "is_rtl": info.is_rtl,
                "content_only": !ui,
            });
            v
        })
        .collect();
    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "locales", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("items", &rows);
    render_with_csrf(&state, &headers, "rcms_admin/locale_list.html", &mut ctx)
}

/// Inject core's known-locale roster into a locale-form context: a
/// `known_locales` list (`code`, `name`) for the code datalist, and a
/// `known_locales_meta` map (`code → {ui, is_rtl, has_plural_rules}`) the
/// form JS reads to show a live support hint. Free-text codes stay valid;
/// this is guidance, not a whitelist.
fn insert_known_locales(ctx: &mut Context) {
    let known: Vec<serde_json::Value> = rustango::i18n::known_locales()
        .map(|(code, name)| serde_json::json!({ "code": code, "name": name }))
        .collect();
    let meta: serde_json::Map<String, serde_json::Value> = rustango::i18n::known_locales()
        .map(|(code, _)| {
            let info = rustango::i18n::locale_info(code);
            (
                code.to_owned(),
                serde_json::json!({
                    "ui": super::i18n::is_ui_locale(code),
                    "is_rtl": info.is_rtl,
                    "has_plural_rules": info.has_plural_rules,
                }),
            )
        })
        .collect();
    ctx.insert("known_locales", &known);
    ctx.insert("known_locales_meta", &serde_json::Value::Object(meta));
}

pub async fn locale_new_form(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
) -> Result<Response, AdminError> {
    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "locales", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    insert_known_locales(&mut ctx);
    ctx.insert("mode", "new");
    ctx.insert(
        "locale",
        &serde_json::json!({
            "code": "",
            "name": "",
            "is_default": false,
            "active": true,
            "sort_order": 100,
        }),
    );
    render_with_csrf(&state, &headers, "rcms_admin/locale_form.html", &mut ctx)
}

#[derive(Debug, Deserialize)]
pub struct LocaleForm {
    pub code: String,
    pub name: String,
    #[serde(default)]
    pub is_default: Option<String>,
    #[serde(default)]
    pub active: Option<String>,
    #[serde(default)]
    pub sort_order: Option<i32>,
}

impl LocaleForm {
    fn parsed_default(&self) -> bool {
        self.is_default.is_some()
    }
    fn parsed_active(&self) -> bool {
        self.active.is_some()
    }
    fn parsed_sort_order(&self) -> i32 {
        self.sort_order.unwrap_or(100)
    }
}

pub async fn locale_new_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Form(form): Form<LocaleForm>,
) -> Result<Response, AdminError> {
    use rustango::sql::Auto;
    let name = form.name.trim().to_owned();
    if name.is_empty() {
        return Err(AdminError::Validation(
            "Display name is required.".to_owned(),
        ));
    }
    let mut row = Locale {
        id: Auto::Unset,
        code: crate::locale::validate_code(&form.code).map_err(AdminError::Validation)?,
        name,
        is_default: form.parsed_default(),
        active: form.parsed_active(),
        sort_order: form.parsed_sort_order(),
        created_at: Auto::Unset,
    };
    if row.is_default {
        clear_default_flag(&tenant, None).await?;
    }
    row.insert_pool(tenant.pool()).await?;
    redirect_named_with_message_args(
        "rcms-admin:locales:list",
        MsgLevel::Success,
        "Added locale {name} ({code}).",
        &[("name", row.name.as_str()), ("code", row.code.as_str())],
        &headers,
    )
}

pub async fn locale_edit_form(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
) -> Result<Response, AdminError> {
    let row = Locale::objects()
        .where_(Locale::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "locales", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    insert_known_locales(&mut ctx);
    ctx.insert("mode", "edit");
    ctx.insert("locale", &row);
    render_with_csrf(&state, &headers, "rcms_admin/locale_form.html", &mut ctx)
}

pub async fn locale_edit_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
    Form(form): Form<LocaleForm>,
) -> Result<Response, AdminError> {
    let mut row = Locale::objects()
        .where_(Locale::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    let name = form.name.trim().to_owned();
    if name.is_empty() {
        return Err(AdminError::Validation(
            "Display name is required.".to_owned(),
        ));
    }
    row.code = crate::locale::validate_code(&form.code).map_err(AdminError::Validation)?;
    row.name = name;
    row.is_default = form.parsed_default();
    row.active = form.parsed_active();
    row.sort_order = form.parsed_sort_order();
    if row.is_default {
        clear_default_flag(&tenant, Some(id)).await?;
    }
    row.save_pool(tenant.pool()).await?;
    redirect_named_with_message_args(
        "rcms-admin:locales:list",
        MsgLevel::Success,
        "Saved locale {name} ({code}).",
        &[("name", row.name.as_str()), ("code", row.code.as_str())],
        &headers,
    )
}

pub async fn locale_delete_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
) -> Result<Response, AdminError> {
    let row = Locale::objects()
        .where_(Locale::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    if row.is_default {
        return redirect_named_with_message(
            "rcms-admin:locales:list",
            MsgLevel::Error,
            "Cannot delete the default locale — promote another locale to default first.",
            &headers,
        );
    }
    let label = format!("{} ({})", row.name, row.code);
    // Translations reference the locale; they go with it, in one step,
    // or the foreign keys refuse the delete.
    let pages = crate::translation::Translation::objects()
        .where_(crate::translation::Translation::locale_id.eq(id))
        .compile_delete()
        .map_err(rustango::sql::ExecError::from)?;
    let snippets = crate::snippet_translation::SnippetTranslation::objects()
        .where_(crate::snippet_translation::SnippetTranslation::locale_id.eq(id))
        .compile_delete()
        .map_err(rustango::sql::ExecError::from)?;
    let menus = crate::menu_item_translation::MenuItemTranslation::objects()
        .where_(crate::menu_item_translation::MenuItemTranslation::locale_id.eq(id))
        .compile_delete()
        .map_err(rustango::sql::ExecError::from)?;
    let categories = crate::category_translation::CategoryTranslation::objects()
        .where_(crate::category_translation::CategoryTranslation::locale_id.eq(id))
        .compile_delete()
        .map_err(rustango::sql::ExecError::from)?;
    rustango::atomic!(tenant.pool(), |tx| {
        let mut guard = tx.lock().await?;
        let tx = &mut *guard;
        for q in [&pages, &snippets, &menus, &categories] {
            rustango::sql::delete_tx(tx, q).await?;
        }
        row.delete_tx(tx).await?;
        Ok::<_, rustango::sql::ExecError>(())
    })
    .await?;
    redirect_named_with_message_args(
        "rcms-admin:locales:list",
        MsgLevel::Success,
        "Deleted locale {label}.",
        &[("label", label.as_str())],
        &headers,
    )
}

// ============================================================ SSO Providers
//
// Admin-managed, DB-backed multi-provider SSO (the framework's
// `rustango::admin::SsoProvider`, one row per configurable OIDC/social
// provider). Each enabled row becomes a "Sign in with …" button on the
// login page (`/login/sso/{slug}`). This is the UI that replaces hand-
// editing `rustango_sso_providers` — mirrors the Locales CRUD.

/// Built-in provider presets offered in the `kind` dropdown, plus generic
/// `oidc` (needs an issuer URL). Free-text isn't allowed — the framework
/// only knows these preset keys + `oidc`.
const SSO_KINDS: &[(&str, &str)] = &[
    ("google", "Google"),
    ("microsoft", "Microsoft"),
    ("github", "GitHub"),
    ("gitlab", "GitLab"),
    ("discord", "Discord"),
    ("oidc", "Generic OpenID Connect"),
];

/// Serialize a `SsoProvider` row to JSON for the templates. The framework
/// model isn't `Serialize`, so map the fields by hand.
fn sso_provider_json(p: &rustango::admin::SsoProvider) -> serde_json::Value {
    serde_json::json!({
        "id": p.id.get().copied().unwrap_or_default(),
        "slug": p.slug,
        "label": p.label,
        "kind": p.kind,
        "issuer_url": p.issuer_url,
        "client_id": p.client_id,
        // Never surface the secret itself — just whether one is set (for the
        // edit form's "leave blank to keep" hint).
        "has_secret": !p.client_secret.is_empty(),
        "enabled": p.enabled,
        "sort_order": p.sort_order,
        "scopes": p.scopes,
    })
}

pub async fn sso_provider_list(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
) -> Result<Response, AdminError> {
    // Identity administration is superuser-only (#672).
    if let Some(redirect) = require_superuser_for_user_admin(session_user.as_ref()) {
        return Ok(redirect);
    }
    use rustango::sql::FetcherPool as _;
    let mut items: Vec<rustango::admin::SsoProvider> = rustango::admin::SsoProvider::objects()
        .fetch(tenant.pool())
        .await
        .unwrap_or_default();
    items.sort_by(|a, b| a.sort_order.cmp(&b.sort_order).then(a.slug.cmp(&b.slug)));
    let rows: Vec<serde_json::Value> = items.iter().map(sso_provider_json).collect();
    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "sso-providers", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("items", &rows);
    render_with_csrf(
        &state,
        &headers,
        "rcms_admin/sso_provider_list.html",
        &mut ctx,
    )
}

#[derive(Deserialize)]
pub struct SsoProviderForm {
    pub slug: String,
    pub label: String,
    pub kind: String,
    #[serde(default)]
    pub issuer_url: String,
    pub client_id: String,
    /// The raw OAuth client secret. On edit, blank means "keep the current one"
    /// (the field is never pre-filled — it's write-only).
    #[serde(default)]
    pub client_secret: String,
    #[serde(default)]
    pub scopes: String,
    #[serde(default)]
    pub enabled: Option<String>,
    #[serde(default)]
    pub sort_order: Option<i32>,
}

impl SsoProviderForm {
    /// Validate + normalize the form into field values, or an error message.
    fn validated(&self) -> Result<ValidatedProvider, String> {
        let slug = self.slug.trim().to_lowercase();
        if slug.is_empty()
            || !slug
                .chars()
                .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')
        {
            return Err(
                "Slug is required and may only contain letters, numbers, - and _.".to_owned(),
            );
        }
        let label = self.label.trim().to_owned();
        if label.is_empty() {
            return Err("Button label is required.".to_owned());
        }
        let kind = self.kind.trim().to_lowercase();
        if !SSO_KINDS.iter().any(|(k, _)| *k == kind) {
            return Err("Pick a provider type.".to_owned());
        }
        let client_id = self.client_id.trim().to_owned();
        if client_id.is_empty() {
            return Err("Client ID is required.".to_owned());
        }
        let issuer = self.issuer_url.trim().to_owned();
        if kind == "oidc" && issuer.is_empty() {
            return Err("A generic OpenID Connect provider needs an issuer URL.".to_owned());
        }
        let scopes = self.scopes.trim().to_owned();
        Ok(ValidatedProvider {
            slug,
            label,
            kind,
            issuer_url: (!issuer.is_empty()).then_some(issuer),
            client_id,
            scopes: (!scopes.is_empty()).then_some(scopes),
            enabled: self.enabled.is_some(),
            sort_order: self.sort_order.unwrap_or(0),
        })
    }
}

struct ValidatedProvider {
    slug: String,
    label: String,
    kind: String,
    issuer_url: Option<String>,
    client_id: String,
    scopes: Option<String>,
    enabled: bool,
    sort_order: i32,
}

fn render_sso_provider_form(ctx: &mut Context, mode: &str, provider: serde_json::Value) {
    let kinds: Vec<serde_json::Value> = SSO_KINDS
        .iter()
        .map(|(k, name)| serde_json::json!({ "key": k, "name": name }))
        .collect();
    ctx.insert("mode", mode);
    ctx.insert("kinds", &kinds);
    ctx.insert("provider", &provider);
}

pub async fn sso_provider_new_form(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
) -> Result<Response, AdminError> {
    // Identity administration is superuser-only (#672).
    if let Some(redirect) = require_superuser_for_user_admin(session_user.as_ref()) {
        return Ok(redirect);
    }
    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "sso-providers", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    render_sso_provider_form(
        &mut ctx,
        "new",
        serde_json::json!({
            "slug": "", "label": "", "kind": "google", "issuer_url": null,
            "client_id": "", "has_secret": false, "scopes": null,
            "enabled": true, "sort_order": 0,
        }),
    );
    render_with_csrf(
        &state,
        &headers,
        "rcms_admin/sso_provider_form.html",
        &mut ctx,
    )
}

async fn find_sso_provider(
    tenant: &Tenant,
    id: i64,
) -> Result<rustango::admin::SsoProvider, AdminError> {
    use rustango::sql::FetcherPool as _;
    rustango::admin::SsoProvider::objects()
        .filter("id", id)
        .fetch(tenant.pool())
        .await
        .map_err(|e| AdminError::Validation(format!("db: {e}")))?
        .into_iter()
        .next()
        .ok_or(AdminError::NotFound(id))
}

pub async fn sso_provider_edit_form(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
) -> Result<Response, AdminError> {
    // Identity administration is superuser-only (#672).
    if let Some(redirect) = require_superuser_for_user_admin(session_user.as_ref()) {
        return Ok(redirect);
    }
    let row = find_sso_provider(&tenant, id).await?;
    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "sso-providers", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    render_sso_provider_form(&mut ctx, "edit", sso_provider_json(&row));
    render_with_csrf(
        &state,
        &headers,
        "rcms_admin/sso_provider_form.html",
        &mut ctx,
    )
}

pub async fn sso_provider_new_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Form(form): Form<SsoProviderForm>,
) -> Result<Response, AdminError> {
    // Identity administration is superuser-only (#672).
    if let Some(redirect) = require_superuser_for_user_admin(session_user.as_ref()) {
        return Ok(redirect);
    }
    let v = form.validated().map_err(AdminError::Validation)?;
    let secret = form.client_secret.trim().to_owned();
    if secret.is_empty() {
        return Err(AdminError::Validation(
            "Client secret is required.".to_owned(),
        ));
    }
    let mut row = rustango::admin::SsoProvider {
        id: rustango::sql::Auto::Unset,
        slug: v.slug,
        label: v.label,
        kind: v.kind,
        issuer_url: v.issuer_url,
        client_id: v.client_id,
        client_secret: rustango::casts::Cast::new(secret),
        enabled: v.enabled,
        sort_order: v.sort_order,
        scopes: v.scopes,
        allow_email_link: false,
        created_at: rustango::sql::Auto::Unset,
        updated_at: rustango::sql::Auto::Unset,
    };
    row.insert_pool(tenant.pool()).await?;
    redirect_named_with_message_args(
        "rcms-admin:sso-providers:list",
        MsgLevel::Success,
        "Added SSO provider {label}.",
        &[("label", row.label.as_str())],
        &headers,
    )
}

pub async fn sso_provider_edit_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
    Form(form): Form<SsoProviderForm>,
) -> Result<Response, AdminError> {
    // Identity administration is superuser-only (#672).
    if let Some(redirect) = require_superuser_for_user_admin(session_user.as_ref()) {
        return Ok(redirect);
    }
    let mut row = find_sso_provider(&tenant, id).await?;
    let v = form.validated().map_err(AdminError::Validation)?;
    row.slug = v.slug;
    row.label = v.label;
    row.kind = v.kind;
    row.issuer_url = v.issuer_url;
    row.client_id = v.client_id;
    // Write-only secret: only replace it when a new one was typed; a blank
    // field keeps the stored (encrypted) value.
    let new_secret = form.client_secret.trim();
    if !new_secret.is_empty() {
        row.client_secret = rustango::casts::Cast::new(new_secret.to_owned());
    }
    row.enabled = v.enabled;
    row.sort_order = v.sort_order;
    row.scopes = v.scopes;
    row.save_pool(tenant.pool()).await?;
    redirect_named_with_message_args(
        "rcms-admin:sso-providers:list",
        MsgLevel::Success,
        "Saved SSO provider {label}.",
        &[("label", row.label.as_str())],
        &headers,
    )
}

pub async fn sso_provider_delete_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
) -> Result<Response, AdminError> {
    // Identity administration is superuser-only (#672).
    if let Some(redirect) = require_superuser_for_user_admin(session_user.as_ref()) {
        return Ok(redirect);
    }
    let row = find_sso_provider(&tenant, id).await?;
    let label = row.label.clone();
    row.delete_pool(tenant.pool()).await?;
    redirect_named_with_message_args(
        "rcms-admin:sso-providers:list",
        MsgLevel::Success,
        "Deleted SSO provider {label}.",
        &[("label", label.as_str())],
        &headers,
    )
}

/// Clear `is_default = true` on every other row so the new default
/// row owns the flag uniquely. Optionally exclude an id when
/// editing the same row.
async fn clear_default_flag(tenant: &Tenant, except: Option<i64>) -> Result<(), AdminError> {
    let rows: Vec<Locale> = Locale::objects()
        .where_(Locale::is_default.eq(true))
        .fetch(tenant.pool())
        .await?;
    for mut r in rows {
        if let (Some(skip), Some(id)) = (except, r.id.get().copied()) {
            if skip == id {
                continue;
            }
        }
        r.is_default = false;
        r.save_pool(tenant.pool()).await?;
    }
    Ok(())
}

// =====================================================================
// Workflows — multi-step approval CRUD (#73 PR 1)
// =====================================================================

pub async fn workflows_list(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
) -> Result<Response, AdminError> {
    let items: Vec<crate::workflow::Workflow> = crate::workflow::Workflow::objects()
        .order_by(&[("name", false)])
        .fetch(tenant.pool())
        .await?;
    // Task counts per workflow — surfaced in the list as a quick
    // indicator. One query for all tasks; bucket client-side.
    let all_tasks: Vec<crate::workflow::WorkflowTask> = crate::workflow::WorkflowTask::objects()
        .fetch(tenant.pool())
        .await?;
    let mut task_counts: std::collections::HashMap<i64, usize> = std::collections::HashMap::new();
    for t in &all_tasks {
        *task_counts.entry(t.workflow_id).or_insert(0) += 1;
    }
    let rows: Vec<serde_json::Value> = items
        .iter()
        .map(|w| {
            let id = w.id.get().copied().unwrap_or_default();
            serde_json::json!({
                "id": id,
                "name": w.name,
                "description": w.description,
                "active": w.active,
                "require_reapproval_on_edit": w.require_reapproval_on_edit,
                "task_count": task_counts.get(&id).copied().unwrap_or(0),
                "created_at": w.created_at.get().copied(),
            })
        })
        .collect();
    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "workflows", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("items", &rows);
    render_with_csrf(&state, &headers, "rcms_admin/workflow_list.html", &mut ctx)
}

pub async fn workflow_new_form(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
) -> Result<Response, AdminError> {
    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "workflows", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("mode", "new");
    ctx.insert(
        "workflow",
        &serde_json::json!({
            "name": "",
            "description": "",
            "active": true,
            "require_reapproval_on_edit": false,
        }),
    );
    ctx.insert("tasks", &Vec::<serde_json::Value>::new());
    ctx.insert("roles", &Vec::<serde_json::Value>::new());
    render_with_csrf(&state, &headers, "rcms_admin/workflow_form.html", &mut ctx)
}

#[derive(Debug, Deserialize)]
pub struct WorkflowForm {
    pub name: String,
    #[serde(default)]
    pub description: String,
    #[serde(default)]
    pub active: Option<String>,
    #[serde(default)]
    pub require_reapproval_on_edit: Option<String>,
}

impl WorkflowForm {
    fn parsed_active(&self) -> bool {
        self.active.is_some()
    }
    fn parsed_reapproval(&self) -> bool {
        self.require_reapproval_on_edit.is_some()
    }
}

pub async fn workflow_new_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Form(form): Form<WorkflowForm>,
) -> Result<Response, AdminError> {
    use rustango::sql::Auto;
    let name = form.name.trim().to_owned();
    if name.is_empty() {
        return redirect_named_with_message(
            "rcms-admin:workflows:list",
            MsgLevel::Error,
            "Workflow name is required.",
            &headers,
        );
    }
    let mut row = crate::workflow::Workflow {
        id: Auto::Unset,
        name: name.clone(),
        description: form.description.trim().to_owned(),
        active: form.parsed_active(),
        require_reapproval_on_edit: form.parsed_reapproval(),
        created_at: Auto::Unset,
        updated_at: Auto::Unset,
    };
    row.insert_pool(tenant.pool()).await?;
    let id = row.id.get().copied().unwrap_or_default();
    redirect_named_with_params_and_message_args(
        "rcms-admin:workflows:edit",
        &[("id", id.to_string())],
        MsgLevel::Success,
        "Created workflow “{name}”. Add review steps below.",
        &[("name", name.as_str())],
        &headers,
    )
}

pub async fn workflow_edit_form(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
) -> Result<Response, AdminError> {
    let row = crate::workflow::Workflow::objects()
        .where_(crate::workflow::Workflow::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    let tasks: Vec<crate::workflow::WorkflowTask> = crate::workflow::WorkflowTask::objects()
        .where_(crate::workflow::WorkflowTask::workflow_id.eq(id))
        .order_by(&[("sort_order", false), ("id", false)])
        .fetch(tenant.pool())
        .await?;
    // Role picker — every tenant role is a valid assignee for a
    // task. Sort alphabetically for predictable ordering.
    let roles: Vec<rustango::tenancy::permissions::Role> =
        rustango::tenancy::permissions::Role::objects()
            .order_by(&[("name", false)])
            .fetch(tenant.pool())
            .await?;
    let role_rows: Vec<serde_json::Value> = roles
        .iter()
        .map(|r| {
            serde_json::json!({
                "id": r.id.get().copied(),
                "name": r.name,
            })
        })
        .collect();
    let role_by_id: std::collections::HashMap<i64, String> = roles
        .iter()
        .filter_map(|r| r.id.get().copied().map(|id| (id, r.name.clone())))
        .collect();
    let task_rows: Vec<serde_json::Value> = tasks
        .iter()
        .map(|t| {
            serde_json::json!({
                "id": t.id.get().copied(),
                "name": t.name,
                "role_id": t.role_id,
                "role_name": role_by_id.get(&t.role_id).cloned().unwrap_or_default(),
                "sort_order": t.sort_order,
                "kind": t.kind,
                "webhook_url": t.webhook_url,
            })
        })
        .collect();
    // #191 — every registered task kind, surfaced to the template
    // so the Add-step form can offer the kind picker.
    // A person's approval first: it's what a review step normally is,
    // and the pre-selected kind.
    let mut task_kinds: Vec<serde_json::Value> = crate::task_kind::registered_kinds()
        .iter()
        .map(|k| {
            serde_json::json!({
                "name": k.kind_name(),
                "verbose_name": k.verbose_name(),
            })
        })
        .collect();
    task_kinds.sort_by_key(|k| k["name"] != "group_approval");
    let kind_label = |name: &str| {
        crate::task_kind::registered_kinds()
            .iter()
            .find(|k| k.kind_name() == name)
            .map_or_else(|| name.to_owned(), |k| k.verbose_name().to_owned())
    };
    let task_rows: Vec<serde_json::Value> = task_rows
        .into_iter()
        .map(|mut t| {
            let label = kind_label(t["kind"].as_str().unwrap_or_default());
            t["kind_label"] = serde_json::Value::String(label);
            t
        })
        .collect();
    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "workflows", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("mode", "edit");
    ctx.insert("workflow", &row);
    ctx.insert("tasks", &task_rows);
    ctx.insert("roles", &role_rows);
    ctx.insert("task_kinds", &task_kinds);
    render_with_csrf(&state, &headers, "rcms_admin/workflow_form.html", &mut ctx)
}

pub async fn workflow_edit_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
    Form(form): Form<WorkflowForm>,
) -> Result<Response, AdminError> {
    let mut row = crate::workflow::Workflow::objects()
        .where_(crate::workflow::Workflow::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    let old_name = row.name.clone();
    row.name = form.name.trim().to_owned();
    row.description = form.description.trim().to_owned();
    row.active = form.parsed_active();
    row.require_reapproval_on_edit = form.parsed_reapproval();
    row.save_pool(tenant.pool()).await?;
    // Page types name their workflow; a rename must move them along, or
    // their pages would silently publish without review.
    if old_name != row.name {
        crate::workflow::rebind_page_types(tenant.pool(), &old_name, &row.name).await?;
    }
    redirect_named_with_params_and_message_args(
        "rcms-admin:workflows:edit",
        &[("id", id.to_string())],
        MsgLevel::Success,
        "Saved workflow “{name}”.",
        &[("name", row.name.as_str())],
        &headers,
    )
}

pub async fn workflow_delete_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
) -> Result<Response, AdminError> {
    let row = crate::workflow::Workflow::objects()
        .where_(crate::workflow::Workflow::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    // Page types that use it would quietly stop requiring review.
    let users: Vec<String> = crate::workflow::page_types_using(tenant.pool(), &row.name)
        .await?
        .into_iter()
        .map(|pt| pt.verbose_name)
        .collect();
    if !users.is_empty() {
        let types = users.join(", ");
        return redirect_named_with_message_args(
            "rcms-admin:workflows:list",
            MsgLevel::Error,
            "Can't delete “{title}” — the page types {types} use it. Choose another workflow for them first.",
            &[("title", row.name.as_str()), ("types", types.as_str())],
            &headers,
        );
    }
    // Reject when any WorkflowState references this workflow — the
    // audit trail would otherwise lose its parent FK. Editors should
    // toggle `active = false` instead.
    let in_use: Vec<crate::workflow::WorkflowState> = crate::workflow::WorkflowState::objects()
        .where_(crate::workflow::WorkflowState::workflow_id.eq(id))
        .fetch(tenant.pool())
        .await?;
    if !in_use.is_empty() {
        let count = in_use.len().to_string();
        return redirect_named_with_message_plural(
            "rcms-admin:workflows:list",
            MsgLevel::Error,
            "Can't delete “{title}” — {count} page(s) have history under it. Toggle Active off instead.",
            in_use.len() as i64,
            &[("title", row.name.as_str()), ("count", count.as_str())],
            &headers,
        );
    }
    // Cascade the task rows by hand (FK isn't ON DELETE CASCADE).
    let tasks: Vec<crate::workflow::WorkflowTask> = crate::workflow::WorkflowTask::objects()
        .where_(crate::workflow::WorkflowTask::workflow_id.eq(id))
        .fetch(tenant.pool())
        .await?;
    for t in tasks {
        t.delete_pool(tenant.pool()).await?;
    }
    let name = row.name.clone();
    row.delete_pool(tenant.pool()).await?;
    redirect_named_with_message_args(
        "rcms-admin:workflows:list",
        MsgLevel::Success,
        "Deleted workflow “{name}”.",
        &[("name", name.as_str())],
        &headers,
    )
}

#[derive(Debug, Deserialize)]
pub struct WorkflowTaskForm {
    pub name: String,
    pub role_id: i64,
    /// Task kind discriminator. Defaults to `group_approval`
    /// when the form input is missing or empty so the existing
    /// human-approval flow stays the default behavior.
    #[serde(default)]
    pub kind: Option<String>,
    #[serde(default)]
    pub webhook_url: Option<String>,
}

pub async fn workflow_task_add(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Path(workflow_id): Path<i64>,
    Form(form): Form<WorkflowTaskForm>,
) -> Result<Response, AdminError> {
    use rustango::sql::Auto;
    let name = form.name.trim().to_owned();
    if name.is_empty() {
        return redirect_named_with_params_and_message(
            "rcms-admin:workflows:edit",
            &[("id", workflow_id.to_string())],
            MsgLevel::Error,
            "Step name is required.",
            &headers,
        );
    }
    let existing: Vec<crate::workflow::WorkflowTask> = crate::workflow::WorkflowTask::objects()
        .where_(crate::workflow::WorkflowTask::workflow_id.eq(workflow_id))
        .order_by(&[("sort_order", true)]) // desc
        .fetch(tenant.pool())
        .await?;
    let next_order = existing.first().map_or(10, |t| t.sort_order + 10);
    let kind = form
        .kind
        .as_deref()
        .map(str::trim)
        .filter(|s| !s.is_empty())
        .filter(|s| crate::task_kind::find(s).is_some())
        .unwrap_or("group_approval")
        .to_owned();
    let webhook_url = form
        .webhook_url
        .as_deref()
        .map(str::trim)
        .unwrap_or("")
        .to_owned();
    let mut row = crate::workflow::WorkflowTask {
        id: Auto::Unset,
        workflow_id,
        name: name.clone(),
        role_id: form.role_id,
        sort_order: next_order,
        kind,
        webhook_url,
    };
    row.insert_pool(tenant.pool()).await?;
    redirect_named_with_params_and_message_args(
        "rcms-admin:workflows:edit",
        &[("id", workflow_id.to_string())],
        MsgLevel::Success,
        "Added step “{name}”.",
        &[("name", name.as_str())],
        &headers,
    )
}

pub async fn workflow_task_delete(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Path((workflow_id, task_id)): Path<(i64, i64)>,
) -> Result<Response, AdminError> {
    let row = crate::workflow::WorkflowTask::objects()
        .where_(crate::workflow::WorkflowTask::id.eq(task_id))
        .where_(crate::workflow::WorkflowTask::workflow_id.eq(workflow_id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(task_id))?;
    let name = row.name.clone();
    row.delete_pool(tenant.pool()).await?;
    redirect_named_with_params_and_message_args(
        "rcms-admin:workflows:edit",
        &[("id", workflow_id.to_string())],
        MsgLevel::Success,
        "Removed step “{name}”.",
        &[("name", name.as_str())],
        &headers,
    )
}

// ---------------------------------------------------------------
// Per-page workflow actions (#73 PR 2): submit / approve / reject /
// cancel. Permission rules:
//   - submit: any logged-in user (page-Edit perm already gated the
//     editor URL).
//   - approve / reject: viewer must be a member of the role bound
//     to the current task. Tenant superusers bypass.
//   - cancel: any logged-in user (audit captures who).
// ---------------------------------------------------------------

#[derive(Debug, Deserialize)]
pub struct WorkflowDecisionForm {
    #[serde(default)]
    pub comment: String,
    /// The step the reviewer was looking at. A decision posted
    /// for a step that is no longer current is refused, not applied to
    /// whichever step is current now.
    #[serde(default)]
    pub task_id: Option<i64>,
}

/// The decision the reviewer posted is for a step that is no
/// longer the current one, or another request decided it first.
fn step_already_decided(page_id: i64, headers: &HeaderMap) -> Result<Response, AdminError> {
    redirect_named_with_params_and_message(
        "rcms-admin:pages:edit",
        &[("id", page_id.to_string())],
        MsgLevel::Error,
        "That review step was already decided — check the page's current step before deciding again.",
        headers,
    )
}

async fn user_is_in_role(
    pool: &rustango::sql::Pool,
    user_id: i64,
    role_id: i64,
) -> Result<bool, rustango::sql::ExecError> {
    use rustango::core::Column as _;
    use rustango::sql::FetcherPool as _;
    let rows: Vec<rustango::tenancy::permissions::UserRole> =
        rustango::tenancy::permissions::UserRole::objects()
            .where_(rustango::tenancy::permissions::UserRole::user_id.eq(user_id))
            .where_(rustango::tenancy::permissions::UserRole::role_id.eq(role_id))
            .fetch(pool)
            .await?;
    Ok(!rows.is_empty())
}

pub async fn page_workflow_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
) -> Result<Response, AdminError> {
    let Some(viewer_id) = session_user.as_ref().and_then(|u| u.id.get().copied()) else {
        return Ok(unauthorized_no_session());
    };
    let page = Page::objects()
        .where_(Page::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    // Resolve the page-type → workflow slug.
    let pts: Vec<PageType> = PageType::objects()
        .where_(PageType::id.eq(page.page_type_id))
        .fetch(tenant.pool())
        .await?;
    let Some(pt) = pts.into_iter().next() else {
        return redirect_named_with_params_and_message(
            "rcms-admin:pages:edit",
            &[("id", id.to_string())],
            MsgLevel::Error,
            "Page type row not found.",
            &headers,
        );
    };
    let Some(slug) = pt.workflow_name() else {
        return redirect_named_with_params_and_message(
            "rcms-admin:pages:edit",
            &[("id", id.to_string())],
            MsgLevel::Error,
            "This page type isn't bound to a workflow — nothing to submit.",
            &headers,
        );
    };
    let Some(workflow) = crate::workflow::find_by_name(tenant.pool(), &slug).await? else {
        return redirect_named_with_params_and_message_args(
            "rcms-admin:pages:edit",
            &[("id", id.to_string())],
            MsgLevel::Error,
            "Workflow `{slug}` not found or inactive — ask an admin to enable it.",
            &[("slug", slug.as_str())],
            &headers,
        );
    };
    let tasks = crate::workflow::tasks_for(
        tenant.pool(),
        workflow.id.get().copied().unwrap_or_default(),
    )
    .await?;
    if tasks.is_empty() {
        return redirect_named_with_params_and_message(
            "rcms-admin:pages:edit",
            &[("id", id.to_string())],
            MsgLevel::Error,
            "Workflow has no review steps yet — ask an admin to add at least one step.",
            &headers,
        );
    }
    // Only block when an in-progress review already exists. A
    // `needs_changes` row means the prior cycle was rejected; the
    // author is allowed to resubmit, which leaves the rejected row
    // as audit history and starts a fresh cycle.
    if let Some(active) = crate::workflow::active_state_for_page(tenant.pool(), id).await? {
        if active.status == crate::workflow::WorkflowStatus::InProgress.as_str() {
            return redirect_named_with_params_and_message(
                "rcms-admin:pages:edit",
                &[("id", id.to_string())],
                MsgLevel::Warning,
                "This page is already under review.",
                &headers,
            );
        }
    }
    crate::workflow::submit_for_review(tenant.pool(), id, &workflow, &tasks, viewer_id).await?;
    // The submitter waits for the verdict now; their edit lock would
    // only stop the reviewer from editing.
    crate::lock::release(tenant.pool(), id, viewer_id)
        .await
        .log_warn("editor lock not released on submit");

    // #192 — audit log the workflow submission.
    crate::page_log::record_or_warn(
        tenant.pool(),
        id,
        crate::page_log::ACTION_WORKFLOW_SUBMIT,
        Some(viewer_id),
        format!(
            "Submitted for review — workflow “{}”, first step “{}”.",
            workflow.name, tasks[0].name
        ),
        Some(serde_json::json!({
            "workflow_id": workflow.id.get().copied(),
            "workflow_name": workflow.name,
            "first_task": tasks[0].name,
        })),
    )
    .await;

    // #85 — notify the first task's reviewers that a review is
    // waiting. No-op when no mailer is wired into AdminState.
    notify_workflow(
        _state.mailer.as_deref(),
        &_state.mailer_from,
        tenant.pool(),
        &tenant.org.slug,
        "submit",
        &workflow.name,
        &page.title,
        id,
        Some(&tasks[0].name),
        session_user.as_ref().map(|u| u.username.as_str()),
        None,
        WorkflowRecipients::Role(tasks[0].role_id),
    )
    .await;

    redirect_named_with_params_and_message_args(
        "rcms-admin:pages:edit",
        &[("id", id.to_string())],
        MsgLevel::Success,
        "Submitted for review — first step: “{name}”.",
        &[("name", tasks[0].name.as_str())],
        &headers,
    )
}

/// Recipient selector for [`notify_workflow`].
enum WorkflowRecipients {
    /// Members of a single role (reviewers on the active task).
    Role(i64),
    /// A single user — used to notify the original submitter on
    /// approve-finish / reject / cancel.
    User(i64),
}

/// Helper invoked by the workflow handlers to dispatch the right
/// notification per event. Resolves recipients, composes subject +
/// body, and hands off to the framework's `Mailer`. No-op if the
/// AdminState has no mailer attached.
#[allow(clippy::too_many_arguments)]
async fn notify_workflow(
    mailer: Option<&dyn rustango::email::Mailer>,
    from: &str,
    pool: &rustango::sql::Pool,
    tenant_slug: &str,
    event: &str,
    workflow_name: &str,
    page_title: &str,
    page_id: i64,
    task_name: Option<&str>,
    actor_username: Option<&str>,
    comment: Option<&str>,
    recipients: WorkflowRecipients,
) {
    let Some(mailer) = mailer else {
        return;
    };
    // #197 — resolve recipients to user_ids first so we can filter
    // out users who have muted this event class before composing
    // email bodies.
    let kind = crate::notification_pref::pref_kind_for_workflow_event(event);
    let candidate_user_ids: Vec<i64> = match recipients {
        WorkflowRecipients::Role(role_id) => {
            crate::workflow_mail::role_member_user_ids(pool, role_id)
                .await
                .unwrap_or_default()
        }
        WorkflowRecipients::User(user_id) => vec![user_id],
    };
    let allowed_ids = crate::notification_pref::filter_enabled(pool, &candidate_user_ids, kind)
        .await
        .unwrap_or(candidate_user_ids);
    let mut pairs: Vec<(String, String)> = Vec::with_capacity(allowed_ids.len());
    for uid in allowed_ids {
        if let Ok(Some(pair)) = crate::workflow_mail::user_email(pool, uid, tenant_slug).await {
            pairs.push(pair);
        }
    }
    if pairs.is_empty() {
        return;
    }
    let (subject, body) = crate::workflow_mail::render_notification(
        event,
        workflow_name,
        page_title,
        page_id,
        task_name,
        actor_username,
        comment,
    );
    crate::workflow_mail::notify_many(mailer, from, &pairs, &subject, &body).await;
}

pub async fn page_workflow_approve(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
    Form(form): Form<WorkflowDecisionForm>,
) -> Result<Response, AdminError> {
    let Some(viewer) = session_user.as_ref() else {
        return Ok(unauthorized_no_session());
    };
    let viewer_id = viewer.id.get().copied().unwrap_or_default();
    let Some(mut state) = crate::workflow::active_state_for_page(tenant.pool(), id).await? else {
        return redirect_named_with_params_and_message(
            "rcms-admin:pages:edit",
            &[("id", id.to_string())],
            MsgLevel::Error,
            "No active workflow on this page.",
            &headers,
        );
    };
    let tasks = crate::workflow::tasks_for(tenant.pool(), state.workflow_id).await?;
    let Some(current_task) = state
        .current_task_id
        .and_then(|tid| tasks.iter().find(|t| t.id.get().copied() == Some(tid)))
    else {
        return redirect_named_with_params_and_message(
            "rcms-admin:pages:edit",
            &[("id", id.to_string())],
            MsgLevel::Error,
            "Workflow has no current task — refresh the page.",
            &headers,
        );
    };
    if !viewer.is_superuser
        && !user_is_in_role(tenant.pool(), viewer_id, current_task.role_id).await?
    {
        return redirect_named_with_params_and_message_args(
            "rcms-admin:pages:edit",
            &[("id", id.to_string())],
            MsgLevel::Error,
            "Only members of the task's role can approve “{name}”.",
            &[("name", current_task.name.as_str())],
            &headers,
        );
    }
    if form.task_id.is_some_and(|t| current_task.id.get().copied() != Some(t)) {
        return step_already_decided(id, &headers);
    }
    // Capture submitter_id + page title BEFORE the transition so we
    // can route the notification on Finished.
    let submitter_id = state.requested_by;
    let page_title: String = Page::objects()
        .where_(Page::id.eq(id))
        .fetch(tenant.pool())
        .await
        .ok()
        .and_then(|v| v.into_iter().next())
        .map(|p| p.title)
        .unwrap_or_else(|| format!("page #{id}"));
    let workflow_name: String = crate::workflow::Workflow::objects()
        .where_(crate::workflow::Workflow::id.eq(state.workflow_id))
        .fetch(tenant.pool())
        .await
        .ok()
        .and_then(|v| v.into_iter().next())
        .map(|w| w.name)
        .unwrap_or_else(|| "Workflow".to_owned());
    let outcome = crate::workflow::approve_current(
        tenant.pool(),
        &mut state,
        &tasks,
        viewer_id,
        &form.comment,
    )
    .await?;
    let msg = match outcome {
        crate::workflow::ApproveOutcome::AlreadyDecided => {
            return step_already_decided(id, &headers);
        }
        crate::workflow::ApproveOutcome::Advanced { next_task_id } => {
            let next = tasks
                .iter()
                .find(|t| t.id.get().copied() == Some(next_task_id));
            let next_name = next.map(|t| t.name.as_str()).unwrap_or("next step");
            // #85 — notify reviewers of the new current step.
            if let Some(nt) = next {
                notify_workflow(
                    _state.mailer.as_deref(),
                    &_state.mailer_from,
                    tenant.pool(),
                    &tenant.org.slug,
                    "approve_advance",
                    &workflow_name,
                    &page_title,
                    id,
                    Some(&nt.name),
                    Some(&viewer.username),
                    Some(&form.comment),
                    WorkflowRecipients::Role(nt.role_id),
                )
                .await;
            }
            // #192 — audit log the approve-advance.
            crate::page_log::record_or_warn(
                tenant.pool(),
                id,
                crate::page_log::ACTION_WORKFLOW_APPROVE,
                Some(viewer_id),
                format!(
                    "Approved “{}” — advanced to “{next_name}”.",
                    current_task.name
                ),
                Some(serde_json::json!({
                    "outcome": "advanced",
                    "task_name": current_task.name,
                    "next_task_name": next_name,
                    "comment": form.comment,
                })),
            )
            .await;
            format!("Approved “{}” — moved to “{next_name}”.", current_task.name)
        }
        crate::workflow::ApproveOutcome::Finished => {
            // Changes to the live page held for this review go live now,
            // through the normal save path.
            if let Some(pending) = crate::pending_change::for_page(tenant.pool(), id).await? {
                match apply_page_edit(
                    tenant.pool(),
                    &tenant.org.slug,
                    &_state.cache_invalidator,
                    _state.mailer.as_deref(),
                    &_state.mailer_from,
                    false,
                    id,
                    &pending.form_map(),
                    None,
                )
                .await
                {
                    Ok(Ok(_)) => crate::pending_change::discard(tenant.pool(), id).await?,
                    Ok(Err(_)) | Err(_) => {
                        tracing::warn!(page_id = id, "held change could not be applied on approval");
                    }
                }
            }
            // Auto-publish on workflow completion.
            if let Some(mut page) = Page::objects()
                .where_(Page::id.eq(id))
                .first(tenant.pool())
                .await?
            {
                page.status = crate::page::PageStatus::Published.as_str().to_owned();
                let now = chrono::Utc::now();
                if page.published_at.is_none() {
                    page.published_at = Some(now);
                }
                // #251 — workflow-finish publish bumps last-publish.
                page.last_published_at = Some(now);
                page.save_pool(tenant.pool()).await?;
                // #692 — the same go-live effects as any other publish
                // (search index + after_publish_page hooks), and the page's
                // cached URL goes too: it used to keep serving the old copy.
                crate::page::went_live_effects(&tenant.org.slug, &page).await;
                crate::task_queue::purge_urls(
                    _state.cache_invalidator.clone(),
                    tenant.org.slug.clone(),
                    vec![page.url_path.clone()],
                )
                .await;
                // #115 — notify-on-publish subscribers.
                crate::page_subscription::notify_publish(
                    _state.mailer.as_deref(),
                    &_state.mailer_from,
                    tenant.pool(),
                    &tenant.org.slug,
                    id,
                    &page.title,
                    &page.url_path,
                    Some(&viewer.username),
                )
                .await;
            }
            // #85 — notify the submitter that their submission shipped.
            notify_workflow(
                _state.mailer.as_deref(),
                &_state.mailer_from,
                tenant.pool(),
                &tenant.org.slug,
                "approve_finish",
                &workflow_name,
                &page_title,
                id,
                Some(&current_task.name),
                Some(&viewer.username),
                Some(&form.comment),
                WorkflowRecipients::User(submitter_id),
            )
            .await;
            // #192 — audit log the workflow-finish approval + the
            // auto-publish it triggered.
            crate::page_log::record_or_warn(
                tenant.pool(),
                id,
                crate::page_log::ACTION_WORKFLOW_APPROVE,
                Some(viewer_id),
                format!(
                    "Approved “{}” — workflow complete; page auto-published.",
                    current_task.name
                ),
                Some(serde_json::json!({
                    "outcome": "finished",
                    "task_name": current_task.name,
                    "comment": form.comment,
                })),
            )
            .await;
            crate::page_log::record_or_warn(
                tenant.pool(),
                id,
                crate::page_log::ACTION_PUBLISH,
                Some(viewer_id),
                "Published via workflow approval.",
                Some(serde_json::json!({ "via": "workflow" })),
            )
            .await;
            format!(
                "Approved “{}” — workflow complete, page published.",
                current_task.name
            )
        }
    };
    redirect_named_with_params_and_message(
        "rcms-admin:pages:edit",
        &[("id", id.to_string())],
        MsgLevel::Success,
        &msg,
        &headers,
    )
}

pub async fn page_workflow_reject(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
    Form(form): Form<WorkflowDecisionForm>,
) -> Result<Response, AdminError> {
    let Some(viewer) = session_user.as_ref() else {
        return Ok(unauthorized_no_session());
    };
    let viewer_id = viewer.id.get().copied().unwrap_or_default();
    let Some(mut state) = crate::workflow::active_state_for_page(tenant.pool(), id).await? else {
        return redirect_named_with_params_and_message(
            "rcms-admin:pages:edit",
            &[("id", id.to_string())],
            MsgLevel::Error,
            "No active workflow on this page.",
            &headers,
        );
    };
    let tasks = crate::workflow::tasks_for(tenant.pool(), state.workflow_id).await?;
    let Some(current_task) = state
        .current_task_id
        .and_then(|tid| tasks.iter().find(|t| t.id.get().copied() == Some(tid)))
    else {
        return redirect_named_with_params_and_message(
            "rcms-admin:pages:edit",
            &[("id", id.to_string())],
            MsgLevel::Error,
            "Workflow has no current task — refresh the page.",
            &headers,
        );
    };
    if !viewer.is_superuser
        && !user_is_in_role(tenant.pool(), viewer_id, current_task.role_id).await?
    {
        return redirect_named_with_params_and_message_args(
            "rcms-admin:pages:edit",
            &[("id", id.to_string())],
            MsgLevel::Error,
            "Only members of the task's role can reject “{name}”.",
            &[("name", current_task.name.as_str())],
            &headers,
        );
    }
    if form.task_id.is_some_and(|t| current_task.id.get().copied() != Some(t)) {
        return step_already_decided(id, &headers);
    }
    if form.comment.trim().is_empty() {
        return redirect_named_with_params_and_message(
            "rcms-admin:pages:edit",
            &[("id", id.to_string())],
            MsgLevel::Error,
            "Say what should change — the author sees it in the review history.",
            &headers,
        );
    }
    let submitter_id = state.requested_by;
    let workflow_id = state.workflow_id;
    if !crate::workflow::reject_current(tenant.pool(), &mut state, viewer_id, &form.comment).await? {
        return step_already_decided(id, &headers);
    }
    // #192 — audit log the rejection.
    crate::page_log::record_or_warn(
        tenant.pool(),
        id,
        crate::page_log::ACTION_WORKFLOW_REJECT,
        Some(viewer_id),
        format!("Rejected “{}” — changes requested.", current_task.name),
        Some(serde_json::json!({
            "task_name": current_task.name,
            "comment": form.comment,
        })),
    )
    .await;
    // #85 — notify the submitter that changes are requested.
    let workflow_name: String = crate::workflow::Workflow::objects()
        .where_(crate::workflow::Workflow::id.eq(workflow_id))
        .fetch(tenant.pool())
        .await
        .ok()
        .and_then(|v| v.into_iter().next())
        .map(|w| w.name)
        .unwrap_or_else(|| "Workflow".to_owned());
    let page_title: String = Page::objects()
        .where_(Page::id.eq(id))
        .fetch(tenant.pool())
        .await
        .ok()
        .and_then(|v| v.into_iter().next())
        .map(|p| p.title)
        .unwrap_or_else(|| format!("page #{id}"));
    notify_workflow(
        _state.mailer.as_deref(),
        &_state.mailer_from,
        tenant.pool(),
        &tenant.org.slug,
        "reject",
        &workflow_name,
        &page_title,
        id,
        Some(&current_task.name),
        Some(&viewer.username),
        Some(&form.comment),
        WorkflowRecipients::User(submitter_id),
    )
    .await;
    redirect_named_with_params_and_message_args(
        "rcms-admin:pages:edit",
        &[("id", id.to_string())],
        MsgLevel::Success,
        "Rejected “{name}” — page returned to author for changes.",
        &[("name", current_task.name.as_str())],
        &headers,
    )
}

pub async fn page_workflow_cancel(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
) -> Result<Response, AdminError> {
    let Some(viewer_id) = session_user.as_ref().and_then(|u| u.id.get().copied()) else {
        return Ok(unauthorized_no_session());
    };
    let Some(mut state) = crate::workflow::active_state_for_page(tenant.pool(), id).await? else {
        return redirect_named_with_params_and_message(
            "rcms-admin:pages:edit",
            &[("id", id.to_string())],
            MsgLevel::Warning,
            "This page isn't currently under review.",
            &headers,
        );
    };
    let submitter_id = state.requested_by;
    let workflow_id = state.workflow_id;
    crate::workflow::cancel(tenant.pool(), &mut state, viewer_id).await?;
    // #192 — audit log the cancellation.
    crate::page_log::record_or_warn(
        tenant.pool(),
        id,
        crate::page_log::ACTION_WORKFLOW_CANCEL,
        Some(viewer_id),
        "Workflow cancelled.",
        Some(serde_json::json!({ "workflow_id": workflow_id })),
    )
    .await;
    // #85 — notify the submitter the review was cancelled. No-op
    // when the submitter is the canceller themselves (the mailer
    // still sends but the user's inbox is the audit trail).
    let workflow_name: String = crate::workflow::Workflow::objects()
        .where_(crate::workflow::Workflow::id.eq(workflow_id))
        .fetch(tenant.pool())
        .await
        .ok()
        .and_then(|v| v.into_iter().next())
        .map(|w| w.name)
        .unwrap_or_else(|| "Workflow".to_owned());
    let page_title: String = Page::objects()
        .where_(Page::id.eq(id))
        .fetch(tenant.pool())
        .await
        .ok()
        .and_then(|v| v.into_iter().next())
        .map(|p| p.title)
        .unwrap_or_else(|| format!("page #{id}"));
    let actor_username = session_user.as_ref().map(|u| u.username.as_str());
    notify_workflow(
        _state.mailer.as_deref(),
        &_state.mailer_from,
        tenant.pool(),
        &tenant.org.slug,
        "cancel",
        &workflow_name,
        &page_title,
        id,
        None,
        actor_username,
        None,
        WorkflowRecipients::User(submitter_id),
    )
    .await;
    redirect_named_with_params_and_message(
        "rcms-admin:pages:edit",
        &[("id", id.to_string())],
        MsgLevel::Success,
        "Workflow cancelled.",
        &headers,
    )
}

// =====================================================================
// Per-page audit log CSV export (#192)
// =====================================================================

/// GET /cms-admin/pages/{id}/log.csv — every audit-log entry for the
/// page, newest first, as CSV. Columns: timestamp, action, actor_id,
/// actor_username, message, data_json. The full log (no row cap)
/// emits — the editor explicitly clicked Download.
pub async fn page_log_csv(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    Path(id): Path<i64>,
) -> Result<Response, AdminError> {
    let entries = crate::page_log::for_page(tenant.pool(), id, None).await?;
    let user_ids: std::collections::BTreeSet<i64> =
        entries.iter().filter_map(|e| e.actor_id).collect();
    let users: std::collections::HashMap<i64, String> = if user_ids.is_empty() {
        std::collections::HashMap::new()
    } else {
        rustango::tenancy::auth::User::objects()
            .where_(
                rustango::tenancy::auth::User::id.is_in(user_ids.into_iter().collect::<Vec<_>>()),
            )
            .fetch(tenant.pool())
            .await?
            .into_iter()
            .filter_map(|u| u.id.get().copied().map(|id| (id, u.username)))
            .collect()
    };

    let mut out = String::with_capacity(4096);
    out.push_str("timestamp,action,actor_id,actor_username,message,data_json\n");
    for e in entries {
        let ts = e
            .created_at
            .get()
            .map(|t| t.to_rfc3339())
            .unwrap_or_default();
        let actor_id = e.actor_id.map(|i| i.to_string()).unwrap_or_default();
        let actor_username = e
            .actor_id
            .and_then(|uid| users.get(&uid).cloned())
            .unwrap_or_default();
        out.push_str(&csv_escape(&ts));
        out.push(',');
        out.push_str(&csv_escape(&e.action));
        out.push(',');
        out.push_str(&csv_escape(&actor_id));
        out.push(',');
        out.push_str(&csv_escape(&actor_username));
        out.push(',');
        out.push_str(&csv_escape(&e.message));
        out.push(',');
        out.push_str(&csv_escape(&e.data_json));
        out.push('\n');
    }

    let filename = format!("page-{id}-audit-log.csv");
    Ok((
        [
            (
                axum::http::header::CONTENT_TYPE,
                "text/csv; charset=utf-8".to_owned(),
            ),
            (
                axum::http::header::CONTENT_DISPOSITION,
                format!("attachment; filename=\"{filename}\""),
            ),
        ],
        out,
    )
        .into_response())
}

// =====================================================================
// History — tenant-wide revision audit log (#17)
// =====================================================================

/// Query string for the history view. All filters optional; pagination
/// via `page` (1-indexed, defaults to 1 — `limit` is fixed at 50 to
/// keep the URL state simple).
#[derive(Debug, Deserialize)]
pub struct HistoryQuery {
    #[serde(default)]
    pub page_id: Option<i64>,
    #[serde(default)]
    pub user_id: Option<i64>,
    #[serde(default)]
    pub page: Option<u32>,
    /// Rows per page.
    #[serde(default)]
    pub per_page: Option<usize>,
}

/// GET /cms-admin/history — tenant-wide cross-page audit log built
/// from `cms_revision`. Each row joins to the page (for title +
/// current URL) and the editor (for username). One Rust-side stitch
/// pass instead of a SQL JOIN since the ORM doesn't expose joins
/// fluently yet.
pub async fn history_page(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Query(q): Query<HistoryQuery>,
    axum::extract::RawQuery(raw_query): axum::extract::RawQuery,
) -> Result<Response, AdminError> {
    let page_size = crate::admin::pagination::clamp_per_page(q.per_page);

    // Build the base query with optional filters. The ORM's
    // `.where_()` is additive (AND); we apply each filter only when
    // present so the SQL stays minimal.
    let mut revisions_query =
        crate::revision::Revision::objects().order_by(&[("created_at", true)]); // desc — newest first
    if let Some(pid) = q.page_id {
        revisions_query = revisions_query.where_(crate::revision::Revision::page_id.eq(pid));
    }
    if let Some(uid) = q.user_id {
        revisions_query =
            revisions_query.where_(crate::revision::Revision::created_by.eq(Some(uid)));
    }

    // The ORM builder has no server-side limit/offset yet, so we fetch
    // all matching rows then page in memory via
    // `rustango::pagination::Paginator` (matches the snippet list).
    // `get_page()` clamps out-of-range numbers to the last page, so the
    // prev/next links never dangle. Acceptable on the 50-row scale.
    let all: Vec<crate::revision::Revision> = revisions_query.fetch(tenant.pool()).await?;
    let total = all.len();
    let paginator = rustango::pagination::Paginator::new(total, page_size);
    let page_obj = paginator.get_page(q.page.unwrap_or(1).max(1) as i64);
    let current_page = page_obj.number as u32;
    let has_next = page_obj.has_next();
    let has_prev = page_obj.has_previous();
    let revisions: Vec<crate::revision::Revision> = if total == 0 {
        Vec::new()
    } else {
        all[page_obj.start_index().saturating_sub(1)..page_obj.end_index()].to_vec()
    };

    // Batch-fetch every distinct page + user referenced by this slice.
    let page_ids: std::collections::BTreeSet<i64> = revisions.iter().map(|r| r.page_id).collect();
    let user_ids: std::collections::BTreeSet<i64> =
        revisions.iter().filter_map(|r| r.created_by).collect();

    let pages: Vec<Page> = if page_ids.is_empty() {
        Vec::new()
    } else {
        Page::objects()
            .where_(Page::id.is_in(page_ids.iter().copied()))
            .fetch(tenant.pool())
            .await?
    };
    let users: Vec<rustango::tenancy::auth::User> = if user_ids.is_empty() {
        Vec::new()
    } else {
        rustango::tenancy::auth::User::objects()
            .where_(rustango::tenancy::auth::User::id.is_in(user_ids.iter().copied()))
            .fetch(tenant.pool())
            .await?
    };

    let pages_by_id: std::collections::HashMap<i64, &Page> = pages
        .iter()
        .filter_map(|p| p.id.get().copied().map(|id| (id, p)))
        .collect();
    let users_by_id: std::collections::HashMap<i64, &rustango::tenancy::auth::User> = users
        .iter()
        .filter_map(|u| u.id.get().copied().map(|id| (id, u)))
        .collect();

    // Build a list of view-model dicts for the template — each row
    // carries everything Tera needs (no further lookups inside the
    // template loop). `is_latest` flags the most-recent revision for
    // each page so the UI can disable revert on the current state.
    let mut latest_seq_by_page: std::collections::HashMap<i64, i32> =
        std::collections::HashMap::new();
    for r in &all {
        let cur = latest_seq_by_page.entry(r.page_id).or_insert(r.sequence);
        if r.sequence > *cur {
            *cur = r.sequence;
        }
    }
    // #74 — surface the prior revision's id + sequence per row so
    // the history list's Compare button can deep-link to the diff
    // view. "Prior" = next-lower sequence on the same page. Built
    // from `all` (not the page slice) so a row at the slice boundary
    // still knows its predecessor.
    let mut by_page: std::collections::HashMap<i64, Vec<&crate::revision::Revision>> =
        std::collections::HashMap::new();
    for r in &all {
        by_page.entry(r.page_id).or_default().push(r);
    }
    let mut prev_by_id: std::collections::HashMap<i64, (i64, i32)> =
        std::collections::HashMap::new();
    for (_pid, mut list) in by_page {
        list.sort_by_key(|r| r.sequence);
        for window in list.windows(2) {
            let earlier = window[0];
            let later = window[1];
            if let (Some(later_id), Some(earlier_id)) =
                (later.id.get().copied(), earlier.id.get().copied())
            {
                prev_by_id.insert(later_id, (earlier_id, earlier.sequence));
            }
        }
    }

    let rows: Vec<serde_json::Value> = revisions
        .iter()
        .map(|r| {
            let page = pages_by_id.get(&r.page_id);
            let user = r.created_by.and_then(|uid| users_by_id.get(&uid));
            let created_at = r.created_at.get().copied();
            let (prev_id, prev_sequence) =
                r.id.get()
                    .copied()
                    .and_then(|id| prev_by_id.get(&id).copied())
                    .map(|(a, b)| (Some(a), Some(b)))
                    .unwrap_or((None, None));
            let is_latest = latest_seq_by_page
                .get(&r.page_id)
                .is_some_and(|s| *s == r.sequence);
            serde_json::json!({
                "id": r.id.get().copied(),
                "page_id": r.page_id,
                "sequence": r.sequence,
                "created_at": created_at,
                "page_title": page.map(|p| p.title.clone()),
                "page_url_path": page.map(|p| p.url_path.clone()),
                "page_status": page.map(|p| p.status.as_str()),
                "username": user.map(|u| u.username.clone()),
                "system_user": r.created_by.is_none(),
                "is_latest": is_latest,
                "prev_id": prev_id,
                "prev_sequence": prev_sequence,
            })
        })
        .collect();

    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "history", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("rows", &rows);
    ctx.insert("filter_page_id", &q.page_id);
    ctx.insert("filter_user_id", &q.user_id);
    // #316 — raw query string for the `| querystring(page=…)` pagination
    // links (preserves the page_id / user_id filters + sets page).
    ctx.insert("query_string", &raw_query.unwrap_or_default());
    ctx.insert("current_page", &current_page);
    ctx.insert("has_next", &has_next);
    ctx.insert("has_prev", &has_prev);
    ctx.insert("total", &total);
    ctx.insert("page_size", &page_size);
    ctx.insert(
        "pagination",
        &crate::admin::pagination::context(current_page as i64, page_size, total),
    );
    render_with_csrf(&state, &headers, "rcms_admin/history.html", &mut ctx)
}

/// Query string for the diff view: required `a` + `b` revision ids.
/// `only_changes` (default true) hides unchanged fields when the
/// template renders.
#[derive(Debug, Deserialize)]
pub struct HistoryDiffQuery {
    pub a: i64,
    pub b: i64,
    #[serde(default)]
    pub all: Option<String>,
}

/// GET /cms-admin/pages/{id}/history/diff?a=<rev_id>&b=<rev_id>.
/// Side-by-side comparison of two revisions on the same
/// page. v1 surfaces a flat field-level diff; rich-text +
/// StreamField-aware diff is a follow-up.
pub async fn history_diff(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(page_id): Path<i64>,
    Query(q): Query<HistoryDiffQuery>,
) -> Result<Response, AdminError> {
    let revisions: Vec<crate::revision::Revision> = crate::revision::Revision::objects()
        .where_(crate::revision::Revision::page_id.eq(page_id))
        .where_(crate::revision::Revision::id.is_in([q.a, q.b]))
        .fetch(tenant.pool())
        .await?;
    let a_row = revisions
        .iter()
        .find(|r| r.id.get().copied() == Some(q.a))
        .cloned()
        .ok_or(AdminError::NotFound(q.a))?;
    let b_row = revisions
        .iter()
        .find(|r| r.id.get().copied() == Some(q.b))
        .cloned()
        .ok_or(AdminError::NotFound(q.b))?;

    let page = Page::objects()
        .where_(Page::id.eq(page_id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(page_id))?;

    // Reviewer name lookup for the header.
    let user_ids: Vec<i64> = [a_row.created_by, b_row.created_by]
        .into_iter()
        .flatten()
        .collect();
    let users: Vec<rustango::tenancy::auth::User> = if user_ids.is_empty() {
        Vec::new()
    } else {
        rustango::tenancy::auth::User::objects()
            .where_(rustango::tenancy::auth::User::id.is_in(user_ids))
            .fetch(tenant.pool())
            .await?
    };
    let username_for = |uid: Option<i64>| -> Option<String> {
        uid.and_then(|id| {
            users
                .iter()
                .find(|u| u.id.get().copied() == Some(id))
                .map(|u| u.username.clone())
        })
    };

    let diffs = crate::revision::diff_snapshots(&a_row.snapshot, &b_row.snapshot);
    let changed_count = diffs.iter().filter(|d| d.is_change()).count();
    let only_changes = q.all.is_none();

    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "history", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("page", &page);
    ctx.insert(
        "rev_a",
        &serde_json::json!({
            "id": a_row.id.get().copied(),
            "sequence": a_row.sequence,
            "created_at": a_row.created_at.get().copied(),
            "username": username_for(a_row.created_by),
        }),
    );
    ctx.insert(
        "rev_b",
        &serde_json::json!({
            "id": b_row.id.get().copied(),
            "sequence": b_row.sequence,
            "created_at": b_row.created_at.get().copied(),
            "username": username_for(b_row.created_by),
        }),
    );
    ctx.insert("diffs", &diffs);
    ctx.insert("changed_count", &changed_count);
    ctx.insert("only_changes", &only_changes);
    render_with_csrf(&state, &headers, "rcms_admin/history_diff.html", &mut ctx)
}

// =====================================================================
// Aging-pages report (#101)
// =====================================================================

#[derive(Debug, Deserialize)]
pub struct AnalyticsQuery {
    /// Window in days back from now — defaults to 30, clamped 1..=365.
    #[serde(default)]
    pub days: Option<i64>,
}

/// GET /cms-admin/analytics — the default first-party analytics
/// dashboard: unique visitors, pageviews, bounce, avg time, top pages /
/// referrers / countries / devices, a per-day series, and "active now".
pub async fn analytics_dashboard(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Query(q): Query<AnalyticsQuery>,
) -> Result<Response, AdminError> {
    let days = q.days.unwrap_or(30).clamp(1, 365);
    let to = chrono::Utc::now();
    let from = to - chrono::Duration::days(days);
    let metrics = crate::analytics::query::compute(tenant.pool(), from, to).await?;
    let host = headers
        .get(header::HOST)
        .and_then(|v| v.to_str().ok())
        .unwrap_or("");
    let active_now = crate::analytics::presence_count(host);
    // Max pageviews across the series → the 100%-height reference for the
    // bar chart (min 1 to avoid divide-by-zero).
    let series_max = metrics
        .series
        .iter()
        .map(|p| p.pageviews)
        .max()
        .unwrap_or(0)
        .max(1);

    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "analytics", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("metrics", &metrics);
    ctx.insert("active_now", &active_now);
    ctx.insert("series_max", &series_max);
    ctx.insert("days", &days);
    render_with_csrf(&state, &headers, "rcms_admin/analytics.html", &mut ctx)
}

#[derive(Debug, Deserialize)]
pub struct AgingQuery {
    /// Minimum days stale — defaults to 90.
    #[serde(default)]
    pub min_days: Option<i64>,
    /// Status filter — defaults to "published". Pass `all` to skip.
    #[serde(default)]
    pub status: Option<String>,
    /// 1-indexed page number.
    #[serde(default)]
    pub page: Option<u32>,
    /// Rows per page.
    #[serde(default)]
    pub per_page: Option<usize>,
}

/// GET /cms-admin/reports/aging — table of pages sorted by
/// `updated_at` ascending (oldest first). Editors use this to spot
/// pages that have gone stale.
pub async fn aging_pages_report(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Query(q): Query<AgingQuery>,
    axum::extract::RawQuery(raw_query): axum::extract::RawQuery,
) -> Result<Response, AdminError> {
    let page_size = crate::admin::pagination::clamp_per_page(q.per_page);
    let min_days = q.min_days.unwrap_or(90).max(0);
    let status_filter = q
        .status
        .as_deref()
        .map(str::trim)
        .filter(|s| !s.is_empty() && *s != "all")
        .map(str::to_owned)
        .unwrap_or_else(|| "published".to_owned());

    let now = chrono::Utc::now();
    let mut q_pages = Page::objects().order_by(&[("updated_at", false)]); // asc
    if status_filter != "all" {
        q_pages = q_pages.where_(Page::status.eq(status_filter.clone()));
    }
    let all: Vec<Page> = q_pages.fetch(tenant.pool()).await?;

    // Filter by min-days-stale.
    let stale: Vec<&Page> = all
        .iter()
        .filter(|p| match p.updated_at.get().copied() {
            Some(ts) => {
                let delta = now.signed_duration_since(ts).num_days();
                delta >= min_days
            }
            None => false,
        })
        .collect();
    let total = stale.len();
    // Page in memory via `rustango::pagination::Paginator` (matches the
    // snippet list); `get_page()` clamps out-of-range numbers to the
    // last page so prev/next links never dangle.
    let paginator = rustango::pagination::Paginator::new(total, page_size);
    let page_obj = paginator.get_page(q.page.unwrap_or(1).max(1) as i64);
    let current_page = page_obj.number as u32;
    let has_next = page_obj.has_next();
    let has_prev = page_obj.has_previous();
    let slice: Vec<&&Page> = if total == 0 {
        Vec::new()
    } else {
        stale[page_obj.start_index().saturating_sub(1)..page_obj.end_index()]
            .iter()
            .collect()
    };

    // Per-row last editor — pulled from the latest revision per
    // page in one batched query.
    let page_ids: Vec<i64> = slice.iter().filter_map(|p| p.id.get().copied()).collect();
    let revisions: Vec<crate::revision::Revision> = if page_ids.is_empty() {
        Vec::new()
    } else {
        crate::revision::Revision::objects()
            .where_(crate::revision::Revision::page_id.is_in(page_ids.iter().copied()))
            .order_by(&[("sequence", true)]) // desc, so newest sequence per page first
            .fetch(tenant.pool())
            .await
            .unwrap_or_default()
    };
    let mut last_editor_by_page: std::collections::HashMap<i64, Option<i64>> =
        std::collections::HashMap::new();
    for r in &revisions {
        // First insertion wins for each page_id since we ordered
        // desc by sequence — that's the most-recent revision.
        last_editor_by_page.entry(r.page_id).or_insert(r.created_by);
    }
    let user_ids: Vec<i64> = last_editor_by_page
        .values()
        .filter_map(|opt| *opt)
        .collect();
    let users: Vec<rustango::tenancy::auth::User> = if user_ids.is_empty() {
        Vec::new()
    } else {
        rustango::tenancy::auth::User::objects()
            .where_(rustango::tenancy::auth::User::id.is_in(user_ids))
            .fetch(tenant.pool())
            .await
            .unwrap_or_default()
    };
    let username_by_id: std::collections::HashMap<i64, String> = users
        .into_iter()
        .filter_map(|u| u.id.get().copied().map(|id| (id, u.username)))
        .collect();

    let rows: Vec<serde_json::Value> = slice
        .iter()
        .map(|p| {
            let pid = p.id.get().copied().unwrap_or_default();
            let updated = p.updated_at.get().copied();
            let days_stale = updated.map(|ts| now.signed_duration_since(ts).num_days());
            let editor = last_editor_by_page
                .get(&pid)
                .and_then(|v| *v)
                .and_then(|uid| username_by_id.get(&uid).cloned());
            serde_json::json!({
                "id": pid,
                "title": p.title,
                "url_path": p.url_path,
                "status": p.status,
                "updated_at": updated,
                "days_stale": days_stale,
                "last_editor": editor,
            })
        })
        .collect();

    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "reports", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("rows", &rows);
    ctx.insert("min_days", &min_days);
    ctx.insert("status_filter", &status_filter);
    // #316 — raw query string for the `| querystring(page=…)` pagination
    // links (preserves min_days / status + sets page).
    ctx.insert("query_string", &raw_query.unwrap_or_default());
    ctx.insert("current_page", &current_page);
    ctx.insert("has_next", &has_next);
    ctx.insert("has_prev", &has_prev);
    ctx.insert("total", &total);
    ctx.insert("page_size", &page_size);
    ctx.insert(
        "pagination",
        &crate::admin::pagination::context(current_page as i64, page_size, total),
    );
    render_with_csrf(&state, &headers, "rcms_admin/aging_pages.html", &mut ctx)
}

/// GET /cms-admin/reports/aging.csv — same data as the HTML view
/// but streamed as RFC 4180 CSV. Honors the same `min_days` +
/// `status` filters.
pub async fn aging_pages_csv(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Query(q): Query<AgingQuery>,
) -> Result<Response, AdminError> {
    let min_days = q.min_days.unwrap_or(90).max(0);
    let status_filter = q
        .status
        .as_deref()
        .map(str::trim)
        .filter(|s| !s.is_empty() && *s != "all")
        .map(str::to_owned)
        .unwrap_or_else(|| "published".to_owned());
    let now = chrono::Utc::now();
    let mut q_pages = Page::objects().order_by(&[("updated_at", false)]);
    if status_filter != "all" {
        q_pages = q_pages.where_(Page::status.eq(status_filter.clone()));
    }
    let all: Vec<Page> = q_pages.fetch(tenant.pool()).await?;
    let stale: Vec<&Page> = all
        .iter()
        .filter(|p| match p.updated_at.get().copied() {
            Some(ts) => now.signed_duration_since(ts).num_days() >= min_days,
            None => false,
        })
        .collect();

    let mut out = String::with_capacity(256 + stale.len() * 96);
    out.push_str("page_id,title,url_path,status,updated_at,days_stale\n");
    for p in stale {
        let pid = p.id.get().copied().unwrap_or_default();
        let updated = p.updated_at.get().copied();
        let days = updated
            .map(|ts| now.signed_duration_since(ts).num_days())
            .unwrap_or_default();
        let when = updated.map(|t| t.to_rfc3339()).unwrap_or_default();
        out.push_str(&format!(
            "{},{},{},{},{},{}\n",
            pid,
            csv_escape(&p.title),
            csv_escape(&p.url_path),
            csv_escape(&p.status),
            csv_escape(&when),
            days
        ));
    }
    Ok(csv_response("aging-pages", &out))
}

/// Helper — attach CSV content-type + a date-stamped Content-Disposition.
fn csv_response(name: &str, body: &str) -> Response {
    let filename = format!("{name}-{}.csv", chrono::Utc::now().format("%Y%m%d"));
    let mut response = (
        [(header::CONTENT_TYPE, "text/csv; charset=utf-8")],
        body.to_owned(),
    )
        .into_response();
    if let Ok(v) = HeaderValue::from_str(&format!("attachment; filename=\"{filename}\"")) {
        response
            .headers_mut()
            .insert(header::CONTENT_DISPOSITION, v);
    }
    response
}

// =====================================================================
// Site settings (#108)
// =====================================================================

pub async fn site_settings_list(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
) -> Result<Response, AdminError> {
    let rows = crate::site_setting::list_all(tenant.pool())
        .await
        .unwrap_or_default();
    let by_scope: std::collections::HashMap<&str, &crate::site_setting::SiteSetting> =
        rows.iter().map(|r| (r.scope.as_str(), r)).collect();
    // #406 — registered typed schemas come first and are always listed
    // (configured or not); then any
    // ad-hoc free-form JSON scopes that aren't backed by a schema.
    let mut view: Vec<serde_json::Value> = Vec::new();
    for schema in crate::site_setting::registered_schemas() {
        let row = by_scope.get(schema.scope).copied();
        view.push(serde_json::json!({
            "scope": schema.scope,
            "label": schema.label,
            "typed": true,
            "configured": row.is_some(),
            "preview": row
                .map(|r| serde_json::to_string(&r.value_json).unwrap_or_default())
                .unwrap_or_default(),
            "updated_at": row.and_then(|r| r.updated_at.get().copied()),
        }));
    }
    for s in &rows {
        if crate::site_setting::schema_for(&s.scope).is_some() {
            continue;
        }
        view.push(serde_json::json!({
            "scope": s.scope,
            "label": s.scope,
            "typed": false,
            "configured": true,
            "preview": serde_json::to_string(&s.value_json).unwrap_or_default(),
            "updated_at": s.updated_at.get().copied(),
        }));
    }
    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "site-settings", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("rows", &view);
    render_with_csrf(
        &state,
        &headers,
        "rcms_admin/site_settings_list.html",
        &mut ctx,
    )
}

pub async fn site_setting_edit_form(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(scope): Path<String>,
    Query(q): Query<EditQuery>,
) -> Result<Response, AdminError> {
    let row = crate::site_setting::get(tenant.pool(), &scope)
        .await
        .unwrap_or(None);
    let stored = row
        .as_ref()
        .map(|s| s.value_json.clone())
        .unwrap_or_else(|| serde_json::json!({}));
    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "site-settings", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("scope", &scope);
    ctx.insert("is_new", &row.is_none());
    // #406 — a registered schema renders a typed form; otherwise the
    // free-form JSON textarea covers ad-hoc scopes.
    if let Some(schema) = crate::site_setting::schema_for(&scope) {
        let widgets = crate::site_setting::prefill_widgets((schema.fields)(), &stored);
        ctx.insert("typed", &true);
        ctx.insert("schema_label", schema.label);
        // Other languages: the text fields, translated side by side.
        let locales: Vec<crate::locale::Locale> = crate::locale::Locale::objects()
            .where_(crate::locale::Locale::active.eq(true))
            .order_by(&[("sort_order", false), ("code", false)])
            .fetch(tenant.pool())
            .await?;
        let editing = q
            .locale
            .as_deref()
            .and_then(|c| locales.iter().find(|l| l.code == c && !l.is_default).cloned());
        if let (Some(loc), false) = (&editing, row.is_none()) {
            let tr = crate::site_setting::translations(&stored, &loc.code);
            let rows: Vec<serde_json::Value> = widgets
                .iter()
                .filter(|w| site_setting_translatable(w))
                .map(|w| {
                    serde_json::json!({
                        "name": w.name,
                        "label": w.label,
                        "canonical": w.value,
                        "multiline": !matches!(w.kind, crate::widget::WidgetKind::Text),
                        "value": tr.get(&w.name).and_then(|v| v.as_str()).unwrap_or(""),
                    })
                })
                .collect();
            ctx.insert("translation_mode", &true);
            ctx.insert("editing_locale", loc);
            ctx.insert("tr_rows", &rows);
        }
        ctx.insert("locales", &locales);
        ctx.insert("widgets", &widgets);
    } else {
        ctx.insert("typed", &false);
        let body = serde_json::to_string_pretty(&stored).unwrap_or_else(|_| "{}".to_owned());
        ctx.insert("value_pretty", &body);
    }
    render_with_csrf(
        &state,
        &headers,
        "rcms_admin/site_setting_form.html",
        &mut ctx,
    )
}

pub async fn site_setting_edit_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Path(scope): Path<String>,
    Form(form): Form<std::collections::HashMap<String, String>>,
) -> Result<Response, AdminError> {
    // #406 — typed scopes coerce each declared field by widget kind;
    // unregistered scopes keep the raw-JSON textarea contract.
    let parsed: serde_json::Value = if let Some(schema) = crate::site_setting::schema_for(&scope) {
        let fields = (schema.fields)();
        let missing = crate::site_setting::missing_required(&fields, &form);
        if !missing.is_empty() {
            return Err(AdminError::Validation(format!(
                "These required fields are empty: {}.",
                missing.join(", ")
            )));
        }
        let mut value = crate::site_setting::value_json_from_form(&fields, &form);
        if let Ok(Some(old)) = crate::site_setting::get(tenant.pool(), &scope).await {
            crate::site_setting::keep_translations(&old.value_json, &mut value);
        }
        value
    } else {
        let raw = form.get("value_json").map_or("", |s| s.trim());
        if raw.is_empty() {
            serde_json::json!({})
        } else {
            serde_json::from_str(raw)
                .map_err(|e| AdminError::Validation(format!("value_json isn't valid JSON: {e}")))?
        }
    };
    crate::site_setting::upsert(tenant.pool(), &scope, parsed).await?;
    redirect_named_with_message_args(
        "rcms-admin:site-settings:list",
        MsgLevel::Success,
        "Saved site setting “{scope}”.",
        &[("scope", scope.as_str())],
        &headers,
    )
}

/// A typed setting field worth translating: free text.
fn site_setting_translatable(w: &crate::widget::Widget) -> bool {
    use crate::widget::WidgetKind as K;
    matches!(w.kind, K::Text | K::Textarea | K::Markdown | K::RichText)
}

/// POST /cms-admin/site-settings/{scope}/translate?locale=<code> — store
/// the text fields' translations in the setting's `_i18n` bag.
pub async fn site_setting_translate_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Path(scope): Path<String>,
    Query(q): Query<EditQuery>,
    Form(form): Form<std::collections::HashMap<String, String>>,
) -> Result<Response, AdminError> {
    let locale = translate_locale(&tenant, q.locale.as_deref()).await?;
    let schema = crate::site_setting::schema_for(&scope)
        .ok_or_else(|| AdminError::Validation(format!("`{scope}` has no typed fields to translate")))?;
    let row = crate::site_setting::get(tenant.pool(), &scope)
        .await?
        .ok_or_else(|| AdminError::Validation(format!("save `{scope}` before translating it")))?;
    let fields: serde_json::Map<String, serde_json::Value> = (schema.fields)()
        .iter()
        .filter(|w| site_setting_translatable(w))
        .filter_map(|w| {
            form.get(&format!("tr__{}", w.name))
                .map(|v| (w.name.clone(), serde_json::Value::String(v.trim().to_owned())))
        })
        .collect();
    let mut value = row.value_json;
    crate::site_setting::set_translations(&mut value, &locale.code, fields);
    crate::site_setting::upsert(tenant.pool(), &scope, value).await?;
    Ok(rustango::messages::redirect_with_message(
        messages_secret(),
        &headers,
        MsgLevel::Success,
        &super::i18n::tr(&headers, "Translations saved.", &[]),
        &format!("/cms-admin/site-settings/{scope}/edit?locale={}", locale.code),
    ))
}

pub async fn site_setting_delete_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Path(scope): Path<String>,
) -> Result<Response, AdminError> {
    if let Some(row) = crate::site_setting::get(tenant.pool(), &scope)
        .await
        .unwrap_or(None)
    {
        row.delete_pool(tenant.pool()).await?;
    }
    redirect_named_with_message_args(
        "rcms-admin:site-settings:list",
        MsgLevel::Success,
        "Deleted site setting “{scope}”.",
        &[("scope", scope.as_str())],
        &headers,
    )
}

// =====================================================================
// Locked-pages report (#109)
// =====================================================================

// ----- #143 — search promotions report ---------

/// GET /cms-admin/reports/search — top queries by hit count with
/// counts of pinned promotions per query.
pub async fn search_promotions_report(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
) -> Result<Response, AdminError> {
    let top = crate::search_promotion::top_queries(tenant.pool(), 100)
        .await
        .unwrap_or_default();
    // Annotate each query with its promotion count.
    let mut rows: Vec<serde_json::Value> = Vec::with_capacity(top.len());
    for q in top {
        let qid = q.id.get().copied().unwrap_or_default();
        let promos = crate::search_promotion::promotions_for_query(tenant.pool(), qid)
            .await
            .unwrap_or_default();
        rows.push(serde_json::json!({
            "id": qid,
            "raw": q.raw,
            "normalized": q.normalized,
            "hits": q.hits,
            "last_seen_at": q.last_seen_at.get().copied(),
            "promotion_count": promos.len(),
        }));
    }
    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "reports-search", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("rows", &rows);
    render_with_csrf(
        &state,
        &headers,
        "rcms_admin/search_promotions_report.html",
        &mut ctx,
    )
}

/// GET /cms-admin/reports/search/{id}/edit — edit pinned pages for
/// one query.
pub async fn search_promotion_edit_form(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(query_id): Path<i64>,
) -> Result<Response, AdminError> {
    use rustango::core::Column as _;
    use rustango::sql::FetcherPool as _;
    let queries: Vec<crate::search_promotion::SearchQuery> =
        crate::search_promotion::SearchQuery::objects()
            .where_(crate::search_promotion::SearchQuery::id.eq(query_id))
            .fetch(tenant.pool())
            .await?;
    let Some(query) = queries.into_iter().next() else {
        return Err(AdminError::NotFound(query_id));
    };
    let promotions = crate::search_promotion::promotions_for_query(tenant.pool(), query_id)
        .await
        .unwrap_or_default();
    let pages: Vec<Page> = Page::objects()
        .where_(Page::status.eq("published".to_owned()))
        .order_by(&[("title", false)])
        .fetch(tenant.pool())
        .await
        .unwrap_or_default();
    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "reports-search", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("query", &query);
    ctx.insert("promotions", &promotions);
    ctx.insert("pages", &pages);
    render_with_csrf(
        &state,
        &headers,
        "rcms_admin/search_promotion_edit.html",
        &mut ctx,
    )
}

#[derive(Debug, Deserialize)]
pub struct SearchPromotionForm {
    /// Comma-separated page ids in pin order.
    #[serde(default)]
    pub page_ids: String,
    #[serde(default)]
    pub descriptions: String,
}

/// POST /cms-admin/reports/search/{id}/edit — replace the pinned
/// list with the posted ordered page ids.
pub async fn search_promotion_edit_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Path(query_id): Path<i64>,
    Form(form): Form<SearchPromotionForm>,
) -> Result<Response, AdminError> {
    use rustango::core::Column as _;
    use rustango::sql::Auto;
    use rustango::sql::FetcherPool as _;
    // Drop existing promotions then re-insert fresh — simpler than
    // diffing.
    let existing: Vec<crate::search_promotion::SearchPromotion> =
        crate::search_promotion::SearchPromotion::objects()
            .where_(crate::search_promotion::SearchPromotion::query_id.eq(query_id))
            .fetch(tenant.pool())
            .await?;
    for row in existing {
        row.delete_pool(tenant.pool()).await.log_warn("old search promotion not deleted");
    }
    let ids: Vec<i64> = form
        .page_ids
        .split(',')
        .map(str::trim)
        .filter(|s| !s.is_empty())
        .filter_map(|s| s.parse::<i64>().ok())
        .collect();
    let descriptions: Vec<String> = form
        .descriptions
        .split('\n')
        .map(|s| s.trim().to_owned())
        .collect();
    for (idx, page_id) in ids.iter().enumerate() {
        let mut row = crate::search_promotion::SearchPromotion {
            id: Auto::Unset,
            query_id,
            page_id: *page_id,
            sort_order: idx as i32,
            description: descriptions.get(idx).cloned().unwrap_or_default(),
            created_at: Auto::Unset,
        };
        row.insert_pool(tenant.pool()).await.log_warn("search promotion not saved");
    }
    redirect_named_with_message_plural(
        "rcms-admin:reports:search",
        MsgLevel::Success,
        "Saved {count} pinned result(s).",
        ids.len() as i64,
        &[("count", &ids.len().to_string())],
        &headers,
    )
}

// ----- #141 — scheduled-publish report ---------

/// Derive the effective lifecycle state from `go_live_at`.
/// Any row carrying a future
/// `go_live_at` is treated as `scheduled` regardless of the editor's
/// literal status pick (draft / published) so the sweep job +
/// scheduled-report query pick it up. Clearing `go_live_at` on a
/// previously-scheduled row falls back to `draft` so the row stops
/// claiming a schedule it no longer has.
///
/// `archived` is preserved either way — archive is an explicit
/// lifecycle terminus and an editor un-archives via a different
/// path. A scheduled row whose `go_live_at` has already passed is
/// left alone too; the sweep is the one that flips it to `published`,
/// and stepping in here would steal that transition.
pub(crate) fn derive_scheduled_status(
    current: &str,
    go_live_at: Option<chrono::DateTime<chrono::Utc>>,
) -> String {
    let archived = crate::page::PageStatus::Archived.as_str();
    if current == archived {
        return current.to_owned();
    }
    let now = chrono::Utc::now();
    let future = go_live_at.is_some_and(|t| t > now);
    let scheduled = crate::page::PageStatus::Scheduled.as_str();
    let draft = crate::page::PageStatus::Draft.as_str();
    match (current, future, go_live_at) {
        (_, true, _) => scheduled.to_owned(),
        (s, false, None) if s == scheduled => draft.to_owned(),
        _ => current.to_owned(),
    }
}

#[cfg(test)]
mod scheduled_status_tests {
    use super::derive_scheduled_status;
    use chrono::{Duration, Utc};

    #[test]
    fn future_go_live_promotes_draft_to_scheduled() {
        let go = Utc::now() + Duration::hours(1);
        assert_eq!(derive_scheduled_status("draft", Some(go)), "scheduled");
    }

    #[test]
    fn future_go_live_also_promotes_published() {
        // Editor picked "published" + a future go-live. The row
        // defers to the sweep — surface as scheduled.
        let go = Utc::now() + Duration::hours(2);
        assert_eq!(derive_scheduled_status("published", Some(go)), "scheduled");
    }

    #[test]
    fn cleared_go_live_demotes_scheduled_to_draft() {
        assert_eq!(derive_scheduled_status("scheduled", None), "draft");
    }

    #[test]
    fn past_go_live_on_scheduled_left_alone_for_sweep() {
        let past = Utc::now() - Duration::hours(1);
        assert_eq!(
            derive_scheduled_status("scheduled", Some(past)),
            "scheduled",
        );
    }

    #[test]
    fn archived_preserved_regardless_of_go_live() {
        let go = Utc::now() + Duration::hours(1);
        assert_eq!(derive_scheduled_status("archived", Some(go)), "archived");
        assert_eq!(derive_scheduled_status("archived", None), "archived");
    }

    #[test]
    fn draft_with_no_go_live_unchanged() {
        assert_eq!(derive_scheduled_status("draft", None), "draft");
    }

    #[test]
    fn published_with_no_go_live_unchanged() {
        assert_eq!(derive_scheduled_status("published", None), "published");
    }
}

/// Build the dataset for the scheduled-publish report: every page
/// with a future `go_live_at` plus every published page with a
/// future `expire_at`. Rows sorted by go_live_at ascending (soonest
/// first), with expirations interleaved by their expire_at.
async fn scheduled_pages_rows(
    pool: &rustango::sql::Pool,
) -> Result<Vec<serde_json::Value>, rustango::sql::ExecError> {
    use rustango::sql::FetcherPool as _;
    let pages: Vec<Page> = Page::objects().fetch(pool).await?;
    let now = chrono::Utc::now();
    let mut rows: Vec<serde_json::Value> = Vec::new();
    for p in &pages {
        let pid = p.id.get().copied().unwrap_or_default();
        if let Some(go) = p.go_live_at {
            // #264 — surface any page with a future `go_live_at`,
            // even if its status hasn't been coerced to "scheduled"
            // yet (the save handler now does the coercion, but legacy
            // rows or out-of-band edits may have `go_live_at` set on
            // a draft/published row — the editor still wants to see
            // the upcoming flip). Archived / expired rows are excluded
            // because both are explicit lifecycle termini.
            if go > now
                && p.status != crate::page::PageStatus::Archived.as_str()
                && p.status != crate::page::PageStatus::Expired.as_str()
            {
                let countdown = (go - now).num_seconds();
                rows.push(serde_json::json!({
                    "kind": "publish",
                    "when": go,
                    "countdown_secs": countdown,
                    "page_id": pid,
                    "title": p.title,
                    "url_path": p.url_path,
                    "status": p.status,
                }));
            }
        }
        if let Some(exp) = p.expire_at {
            // A scheduled page with both dates comes down too: list it.
            if exp > now
                && (p.status == crate::page::PageStatus::Published.as_str()
                    || p.status == crate::page::PageStatus::Scheduled.as_str())
            {
                let countdown = (exp - now).num_seconds();
                rows.push(serde_json::json!({
                    "kind": "expire",
                    "when": exp,
                    "countdown_secs": countdown,
                    "page_id": pid,
                    "title": p.title,
                    "url_path": p.url_path,
                    "status": p.status,
                }));
            }
        }
    }
    rows.sort_by_key(|r| {
        r.get("countdown_secs")
            .and_then(|v| v.as_i64())
            .unwrap_or(i64::MAX)
    });
    Ok(rows)
}

pub async fn scheduled_report(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
) -> Result<Response, AdminError> {
    let rows = scheduled_pages_rows(tenant.pool())
        .await
        .unwrap_or_default();
    let mut ctx = Context::new();
    add_chrome(
        &mut ctx,
        &tenant,
        "reports-scheduled",
        session_user.as_ref(),
    )
    .await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("rows", &rows);
    render_with_csrf(
        &state,
        &headers,
        "rcms_admin/scheduled_report.html",
        &mut ctx,
    )
}

pub async fn scheduled_report_csv(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
) -> Result<Response, AdminError> {
    let rows = scheduled_pages_rows(tenant.pool())
        .await
        .unwrap_or_default();
    let mut out = String::with_capacity(256 + rows.len() * 160);
    out.push_str("kind,page_id,title,url_path,status,when,countdown_secs\n");
    for r in &rows {
        out.push_str(&format!(
            "{},{},{},{},{},{},{}\n",
            csv_escape(r.get("kind").and_then(|v| v.as_str()).unwrap_or_default()),
            r.get("page_id")
                .and_then(|v| v.as_i64())
                .unwrap_or_default(),
            csv_escape(r.get("title").and_then(|v| v.as_str()).unwrap_or_default()),
            csv_escape(
                r.get("url_path")
                    .and_then(|v| v.as_str())
                    .unwrap_or_default()
            ),
            csv_escape(r.get("status").and_then(|v| v.as_str()).unwrap_or_default()),
            csv_escape(&r.get("when").map(ToString::to_string).unwrap_or_default()),
            r.get("countdown_secs")
                .and_then(|v| v.as_i64())
                .unwrap_or_default(),
        ));
    }
    Ok(csv_response("scheduled-pages", &out))
}

/// Build the locked-pages dataset: every live PageLock joined with
/// its page + holder username. Returns rows sorted by acquired_at
/// desc (newest holds first). Excludes stale locks past their TTL.
async fn locked_pages_rows(
    pool: &rustango::sql::Pool,
) -> Result<Vec<serde_json::Value>, rustango::sql::ExecError> {
    use rustango::sql::FetcherPool as _;
    let locks: Vec<crate::lock::PageLock> = crate::lock::PageLock::objects()
        .order_by(&[("acquired_at", true)]) // desc
        .fetch(pool)
        .await?;
    let now = chrono::Utc::now();
    let live: Vec<&crate::lock::PageLock> = locks.iter().filter(|l| !l.is_stale(now)).collect();
    if live.is_empty() {
        return Ok(Vec::new());
    }
    let page_ids: Vec<i64> = live.iter().map(|l| l.page_id).collect();
    let user_ids: Vec<i64> = live.iter().map(|l| l.user_id).collect();
    let pages: Vec<Page> = Page::objects()
        .where_(Page::id.is_in(page_ids))
        .fetch(pool)
        .await
        .unwrap_or_default();
    let users: Vec<rustango::tenancy::auth::User> = rustango::tenancy::auth::User::objects()
        .where_(rustango::tenancy::auth::User::id.is_in(user_ids))
        .fetch(pool)
        .await
        .unwrap_or_default();
    let page_by_id: std::collections::HashMap<i64, &Page> = pages
        .iter()
        .filter_map(|p| p.id.get().copied().map(|id| (id, p)))
        .collect();
    let user_by_id: std::collections::HashMap<i64, String> = users
        .into_iter()
        .filter_map(|u| u.id.get().copied().map(|id| (id, u.username)))
        .collect();
    Ok(live
        .into_iter()
        .map(|l| {
            let title = page_by_id
                .get(&l.page_id)
                .map(|p| p.title.clone())
                .unwrap_or_else(|| format!("page #{}", l.page_id));
            let url_path = page_by_id
                .get(&l.page_id)
                .map(|p| p.url_path.clone())
                .unwrap_or_default();
            let username = user_by_id
                .get(&l.user_id)
                .cloned()
                .unwrap_or_else(|| format!("user#{}", l.user_id));
            let ttl_remaining = (l.expires_at - now).num_seconds();
            serde_json::json!({
                "page_id": l.page_id,
                "title": title,
                "url_path": url_path,
                "user_id": l.user_id,
                "username": username,
                "acquired_at": l.acquired_at.get().copied(),
                "expires_at": l.expires_at,
                "ttl_remaining_secs": ttl_remaining,
            })
        })
        .collect())
}

pub async fn locked_pages_report(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
) -> Result<Response, AdminError> {
    let rows = locked_pages_rows(tenant.pool()).await.unwrap_or_default();
    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "reports-locked", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("rows", &rows);
    render_with_csrf(&state, &headers, "rcms_admin/locked_pages.html", &mut ctx)
}

pub async fn locked_pages_csv(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
) -> Result<Response, AdminError> {
    let rows = locked_pages_rows(tenant.pool()).await.unwrap_or_default();
    let mut out = String::with_capacity(256 + rows.len() * 128);
    out.push_str("page_id,title,url_path,username,acquired_at,expires_at\n");
    for r in &rows {
        out.push_str(&format!(
            "{},{},{},{},{},{}\n",
            r.get("page_id")
                .and_then(|v| v.as_i64())
                .unwrap_or_default(),
            csv_escape(r.get("title").and_then(|v| v.as_str()).unwrap_or_default()),
            csv_escape(
                r.get("url_path")
                    .and_then(|v| v.as_str())
                    .unwrap_or_default()
            ),
            csv_escape(
                r.get("username")
                    .and_then(|v| v.as_str())
                    .unwrap_or_default()
            ),
            csv_escape(
                r.get("acquired_at")
                    .and_then(|v| v.as_str())
                    .unwrap_or_default()
            ),
            csv_escape(
                r.get("expires_at")
                    .and_then(|v| v.as_str())
                    .unwrap_or_default()
            ),
        ));
    }
    Ok(csv_response("locked-pages", &out))
}

// =====================================================================
// Workflows report (#109)
// =====================================================================

async fn workflows_report_rows(
    pool: &rustango::sql::Pool,
) -> Result<Vec<serde_json::Value>, rustango::sql::ExecError> {
    use rustango::sql::FetcherPool as _;
    let states: Vec<crate::workflow::WorkflowState> = crate::workflow::WorkflowState::objects()
        .order_by(&[("requested_at", true)]) // desc
        .fetch(pool)
        .await?;
    if states.is_empty() {
        return Ok(Vec::new());
    }
    let workflow_ids: Vec<i64> = states.iter().map(|s| s.workflow_id).collect();
    let page_ids: Vec<i64> = states.iter().map(|s| s.page_id).collect();
    let user_ids: Vec<i64> = states.iter().map(|s| s.requested_by).collect();
    let workflows: Vec<crate::workflow::Workflow> = crate::workflow::Workflow::objects()
        .where_(crate::workflow::Workflow::id.is_in(workflow_ids))
        .fetch(pool)
        .await
        .unwrap_or_default();
    let pages: Vec<Page> = Page::objects()
        .where_(Page::id.is_in(page_ids))
        .fetch(pool)
        .await
        .unwrap_or_default();
    let users: Vec<rustango::tenancy::auth::User> = rustango::tenancy::auth::User::objects()
        .where_(rustango::tenancy::auth::User::id.is_in(user_ids))
        .fetch(pool)
        .await
        .unwrap_or_default();
    let task_ids: Vec<i64> = states.iter().filter_map(|s| s.current_task_id).collect();
    let tasks: Vec<crate::workflow::WorkflowTask> = if task_ids.is_empty() {
        Vec::new()
    } else {
        crate::workflow::WorkflowTask::objects()
            .where_(crate::workflow::WorkflowTask::id.is_in(task_ids))
            .fetch(pool)
            .await
            .unwrap_or_default()
    };
    let workflow_by_id: std::collections::HashMap<i64, String> = workflows
        .into_iter()
        .filter_map(|w| w.id.get().copied().map(|id| (id, w.name)))
        .collect();
    let page_by_id: std::collections::HashMap<i64, &Page> = pages
        .iter()
        .filter_map(|p| p.id.get().copied().map(|id| (id, p)))
        .collect();
    let user_by_id: std::collections::HashMap<i64, String> = users
        .into_iter()
        .filter_map(|u| u.id.get().copied().map(|id| (id, u.username)))
        .collect();
    let task_by_id: std::collections::HashMap<i64, String> = tasks
        .into_iter()
        .filter_map(|t| t.id.get().copied().map(|id| (id, t.name)))
        .collect();
    Ok(states
        .iter()
        .map(|s| {
            serde_json::json!({
                "id": s.id.get().copied(),
                "workflow_name": workflow_by_id.get(&s.workflow_id).cloned().unwrap_or_else(|| format!("workflow #{}", s.workflow_id)),
                "page_id": s.page_id,
                "page_title": page_by_id.get(&s.page_id).map(|p| p.title.clone()).unwrap_or_else(|| format!("page #{}", s.page_id)),
                "page_url_path": page_by_id.get(&s.page_id).map(|p| p.url_path.clone()).unwrap_or_default(),
                "status": s.status,
                "current_task_name": s.current_task_id.and_then(|tid| task_by_id.get(&tid).cloned()),
                "requested_by_username": user_by_id.get(&s.requested_by).cloned(),
                "requested_at": s.requested_at.get().copied(),
                "finished_at": s.finished_at,
            })
        })
        .collect())
}

pub async fn workflows_report(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
) -> Result<Response, AdminError> {
    let rows = workflows_report_rows(tenant.pool())
        .await
        .unwrap_or_default();
    let mut ctx = Context::new();
    add_chrome(
        &mut ctx,
        &tenant,
        "reports-workflows",
        session_user.as_ref(),
    )
    .await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("rows", &rows);
    render_with_csrf(
        &state,
        &headers,
        "rcms_admin/workflow_report.html",
        &mut ctx,
    )
}

// =====================================================================
// #122 — revision storage report + manual prune.
// =====================================================================

/// GET /cms-admin/reports/revisions — list pages ordered by revision
/// count so editors can see which ones accumulate history fastest.
/// Surface a "Prune now" form scoped to the whole tenant.
pub async fn revisions_report(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
) -> Result<Response, AdminError> {
    use rustango::sql::FetcherPool as _;
    let pages: Vec<Page> = Page::objects().fetch(tenant.pool()).await?;
    let revisions: Vec<crate::revision::Revision> = crate::revision::Revision::objects()
        .fetch(tenant.pool())
        .await
        .unwrap_or_default();
    let mut count_by_page: std::collections::HashMap<i64, usize> = std::collections::HashMap::new();
    for r in &revisions {
        *count_by_page.entry(r.page_id).or_default() += 1;
    }
    let mut rows: Vec<serde_json::Value> = pages
        .into_iter()
        .filter_map(|p| {
            let pid = p.id.get().copied()?;
            let count = count_by_page.get(&pid).copied().unwrap_or(0);
            if count == 0 {
                return None;
            }
            Some(serde_json::json!({
                "page_id": pid,
                "title": p.title,
                "url_path": p.url_path,
                "status": p.status,
                "revision_count": count,
            }))
        })
        .collect();
    rows.sort_by(|a, b| {
        let ac = a
            .get("revision_count")
            .and_then(|v| v.as_u64())
            .unwrap_or(0);
        let bc = b
            .get("revision_count")
            .and_then(|v| v.as_u64())
            .unwrap_or(0);
        bc.cmp(&ac)
    });
    let total_revisions = revisions.len();
    let mut ctx = Context::new();
    add_chrome(
        &mut ctx,
        &tenant,
        "reports-revisions",
        session_user.as_ref(),
    )
    .await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("rows", &rows);
    ctx.insert("total_revisions", &total_revisions);
    ctx.insert("default_keep_latest", &crate::revision::DEFAULT_KEEP_LATEST);
    render_with_csrf(
        &state,
        &headers,
        "rcms_admin/revisions_report.html",
        &mut ctx,
    )
}

/// Form posted from the revisions report's "Prune now" button.
#[derive(Debug, Deserialize)]
pub struct RevisionsPruneForm {
    #[serde(default)]
    pub keep_latest: Option<usize>,
    #[serde(default)]
    pub include_published: Option<String>,
}

/// POST /cms-admin/reports/revisions/prune — manual prune. Honours
/// the same `(keep_latest, keep_published)` knobs as the library
/// helper. Redirects back to the report with a count flash.
pub async fn revisions_report_prune(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Form(form): Form<RevisionsPruneForm>,
) -> Result<Response, AdminError> {
    let keep_latest = form
        .keep_latest
        .unwrap_or(crate::revision::DEFAULT_KEEP_LATEST)
        .max(1);
    // The form check sends "1" when the box is ticked → keep
    // published-status revisions. Unchecked = include them in the
    // sweep (more aggressive).
    let keep_published = form.include_published.as_deref() != Some("1");
    let (pages_scanned, rows_deleted) =
        crate::revision::prune_all_pages(tenant.pool(), keep_latest, keep_published)
            .await
            .unwrap_or((0, 0));
    let revisions = super::i18n::tr_plural(
        &headers,
        "{count} revision(s)",
        rows_deleted as i64,
        &[("count", &rows_deleted.to_string())],
    );
    let pages_ph = super::i18n::tr_plural(
        &headers,
        "{count} page(s)",
        pages_scanned as i64,
        &[("count", &pages_scanned.to_string())],
    );
    let kl = keep_latest.to_string();
    let kp = keep_published.to_string();
    redirect_named_with_message_args(
        "rcms-admin:reports:revisions",
        MsgLevel::Success,
        "Pruned {revisions} across {pages} (keep_latest={keep_latest}, keep_published={keep_published}).",
        &[
            ("revisions", revisions.as_str()),
            ("pages", pages_ph.as_str()),
            ("keep_latest", kl.as_str()),
            ("keep_published", kp.as_str()),
        ],
        &headers,
    )
}

pub async fn workflows_report_csv(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
) -> Result<Response, AdminError> {
    let rows = workflows_report_rows(tenant.pool())
        .await
        .unwrap_or_default();
    let mut out = String::with_capacity(256 + rows.len() * 192);
    out.push_str("workflow,page_id,page_title,page_url,status,current_task,requested_by,requested_at,finished_at\n");
    for r in &rows {
        out.push_str(&format!(
            "{},{},{},{},{},{},{},{},{}\n",
            csv_escape(
                r.get("workflow_name")
                    .and_then(|v| v.as_str())
                    .unwrap_or_default()
            ),
            r.get("page_id")
                .and_then(|v| v.as_i64())
                .unwrap_or_default(),
            csv_escape(
                r.get("page_title")
                    .and_then(|v| v.as_str())
                    .unwrap_or_default()
            ),
            csv_escape(
                r.get("page_url_path")
                    .and_then(|v| v.as_str())
                    .unwrap_or_default()
            ),
            csv_escape(r.get("status").and_then(|v| v.as_str()).unwrap_or_default()),
            csv_escape(
                r.get("current_task_name")
                    .and_then(|v| v.as_str())
                    .unwrap_or_default()
            ),
            csv_escape(
                r.get("requested_by_username")
                    .and_then(|v| v.as_str())
                    .unwrap_or_default()
            ),
            csv_escape(
                r.get("requested_at")
                    .and_then(|v| v.as_str())
                    .unwrap_or_default()
            ),
            csv_escape(
                r.get("finished_at")
                    .and_then(|v| v.as_str())
                    .unwrap_or_default()
            ),
        ));
    }
    Ok(csv_response("workflow-report", &out))
}

// =====================================================================
// Global admin search (#77 — stage 0)
// =====================================================================

/// Query for the global search. `q` is the search needle; when
/// `autocomplete=1` is set the handler returns a JSON shape suited
/// to the topbar dropdown (no surrounding chrome).
#[derive(Debug, Deserialize)]
pub struct AdminSearchQuery {
    #[serde(default)]
    pub q: Option<String>,
    #[serde(default)]
    pub autocomplete: Option<String>,
}

/// Query string for `GET /cms-admin/__page-chooser`.
#[derive(Debug, Deserialize)]
pub struct PageChooserQuery {
    #[serde(default)]
    pub q: Option<String>,
}

/// Query string for the generic `GET /cms-admin/__chooser/{slug}`.
#[derive(Debug, Default, Deserialize)]
pub struct ChooserSearchQuery {
    #[serde(default)]
    pub q: Option<String>,
}

/// GET `/cms-admin/__chooser/{slug}?q=<needle>` — JSON endpoint
/// backing any `register_chooser!`ed model, generalising the three
/// bespoke choosers. Returns `{ "items": [ { id, title, sub, … } ] }`,
/// the shape the shared overlay (`cms-ux.js`) renders. Unknown slug → 404.
pub async fn chooser_search_json(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    Path(slug): Path<String>,
    Query(q): Query<ChooserSearchQuery>,
) -> Result<Response, AdminError> {
    let Some(cvs) = super::chooser::find(&slug) else {
        return Ok((axum::http::StatusCode::NOT_FOUND, "unknown chooser").into_response());
    };
    let items = (cvs.search_rows)(tenant.pool().clone(), q.q.unwrap_or_default()).await;
    let body = serde_json::json!({ "items": items });
    Ok((
        [(header::CONTENT_TYPE, "application/json")],
        serde_json::to_string(&body).unwrap_or_else(|_| "{}".to_owned()),
    )
        .into_response())
}

/// Query string for `GET /cms-admin/__media-picker`.
#[derive(Debug, Default, Deserialize)]
pub struct MediaPickerQuery {
    #[serde(default)]
    pub q: Option<String>,
    /// Collection filter: omitted = all collections, `0` = uncategorized
    /// (NULL collection_id), any other value = that exact collection.
    #[serde(default)]
    pub collection: Option<i64>,
    /// `"image"` (default) | `"document"` | `"other"` | `"any"`.
    #[serde(default)]
    pub kind: Option<String>,
    /// Comma-separated media ids — resolve exactly these rows (any kind
    /// unless `kind` is set explicitly). Used by the widget-label
    /// hydration pass to turn a server-rendered `#42` into the title.
    #[serde(default)]
    pub ids: Option<String>,
    #[serde(default)]
    pub limit: Option<usize>,
    #[serde(default)]
    pub offset: Option<usize>,
}

/// GET `/cms-admin/__media-picker` — the media picker's data endpoint.
///
/// Richer than the generic `/cms-admin/__chooser/media` (which stays for
/// back-compat): collection + kind filters, offset pagination, and
/// server-computed `thumb_url`/`preview_url` rendition URLs. The URLs are
/// built here (not in JS) because renditions can be signature-gated
/// and only the server can sign a spec.
///
/// Filtering is in-Rust over a full fetch — the crate's tri-dialect
/// convention (see `render_media_list`); no SQL LIKE.
pub async fn media_picker_json(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    Query(q): Query<MediaPickerQuery>,
) -> Result<Response, AdminError> {
    let body = media_picker_payload(tenant.pool(), &q).await?;
    Ok((
        [(header::CONTENT_TYPE, "application/json")],
        serde_json::to_string(&body).unwrap_or_else(|_| "{}".to_owned()),
    )
        .into_response())
}

/// Core of [`media_picker_json`], split out so unit tests can drive it with
/// an in-memory pool (the handler's `Tenant`/`State` extractors need a full
/// admin app).
pub(crate) async fn media_picker_payload(
    pool: &rustango::sql::Pool,
    q: &MediaPickerQuery,
) -> Result<serde_json::Value, AdminError> {
    let mut rows: Vec<Media> = Media::objects()
        .order_by(&[("uploaded_at", true)])
        .fetch(pool)
        .await?;

    // Explicit-id lookup (label hydration): keep exactly those rows, and
    // default the kind filter OFF — a video/document id must resolve too.
    let id_set: Option<std::collections::HashSet<i64>> = q.ids.as_deref().map(|s| {
        s.split(',')
            .filter_map(|t| t.trim().parse::<i64>().ok())
            .collect()
    });
    if let Some(ids) = &id_set {
        rows.retain(|m| m.id.get().copied().is_some_and(|id| ids.contains(&id)));
    }
    // Kind filter — the picker is image-first ("Choose an image…");
    // documents have their own chooser kind. `any` opts out.
    let kind = q
        .kind
        .as_deref()
        .unwrap_or(if id_set.is_some() { "any" } else { "image" });
    if kind != "any" {
        rows.retain(|m| m.kind == kind);
    }
    // Per-collection counts for the dropdown, computed over the kind-filtered
    // set (before collection/search narrow it) so the numbers answer
    // "how many would this filter show?".
    let mut collection_counts: std::collections::HashMap<i64, i64> =
        std::collections::HashMap::new();
    let mut uncategorized_count: i64 = 0;
    for m in &rows {
        match m.collection_id {
            Some(cid) => *collection_counts.entry(cid).or_default() += 1,
            None => uncategorized_count += 1,
        }
    }
    match q.collection {
        Some(0) => rows.retain(|m| m.collection_id.is_none()),
        Some(cid) => rows.retain(|m| m.collection_id == Some(cid)),
        None => {}
    }
    if let Some(needle) = q.q.as_deref().map(str::trim).filter(|s| !s.is_empty()) {
        let needle = needle.to_lowercase();
        rows.retain(|m| {
            m.title.to_lowercase().contains(&needle)
                || m.filename.to_lowercase().contains(&needle)
                || m.alt_text.to_lowercase().contains(&needle)
                || m.description.to_lowercase().contains(&needle)
        });
    }

    let total = rows.len();
    let limit = q.limit.unwrap_or(60).min(200);
    let offset = q.offset.unwrap_or(0).min(total);
    let has_more = offset + limit < total;
    let page: Vec<serde_json::Value> = rows[offset..(offset + limit).min(total)]
        .iter()
        .map(|m| {
            let id = m.id.get().copied().unwrap_or_default();
            let is_image = m.kind == "image";
            let thumb_url = is_image
                .then(|| {
                    crate::rendition::rendition_url_for(
                        id,
                        "fill-320x320|format-webp|quality-70",
                        Some(m.content_hash.as_str()),
                    )
                })
                .flatten();
            let preview_url = is_image
                .then(|| {
                    crate::rendition::rendition_url_for(
                        id,
                        "max-640x480|format-webp|quality-80",
                        Some(m.content_hash.as_str()),
                    )
                })
                .flatten();
            serde_json::json!({
                "id": id,
                "title": if m.title.is_empty() { m.filename.clone() } else { m.title.clone() },
                "filename": m.filename,
                "kind": m.kind,
                "mime": m.mime,
                "size": m.size,
                "width": m.width,
                "height": m.height,
                "collection_id": m.collection_id,
                "alt_text": m.alt_text,
                "uploaded_at": m.uploaded_at,
                "thumb_url": thumb_url,
                "preview_url": preview_url,
            })
        })
        .collect();

    // Collections in DFS order so the dropdown nests visually
    // ("— Child" under its parent). A visited set guards against
    // accidental parent cycles.
    let cols: Vec<crate::media::MediaCollection> =
        crate::media::MediaCollection::objects().fetch(pool).await?;
    let mut children: std::collections::HashMap<Option<i64>, Vec<&crate::media::MediaCollection>> =
        std::collections::HashMap::new();
    for c in &cols {
        children.entry(c.parent_id).or_default().push(c);
    }
    for v in children.values_mut() {
        v.sort_by(|a, b| a.sort_order.cmp(&b.sort_order).then(a.name.cmp(&b.name)));
    }
    let mut ordered: Vec<serde_json::Value> = Vec::with_capacity(cols.len());
    let mut visited: std::collections::HashSet<i64> = std::collections::HashSet::new();
    let mut stack: Vec<(i64, usize)> = children
        .get(&None)
        .map(|roots| {
            roots
                .iter()
                .rev()
                .filter_map(|c| c.id.get().copied())
                .map(|id| (id, 0))
                .collect()
        })
        .unwrap_or_default();
    let by_id: std::collections::HashMap<i64, &crate::media::MediaCollection> = cols
        .iter()
        .filter_map(|c| c.id.get().copied().map(|id| (id, c)))
        .collect();
    while let Some((id, depth)) = stack.pop() {
        if !visited.insert(id) {
            continue;
        }
        if let Some(c) = by_id.get(&id) {
            ordered.push(serde_json::json!({
                "id": id,
                "name": c.name,
                "depth": depth,
                "count": collection_counts.get(&id).copied().unwrap_or(0),
            }));
            if let Some(kids) = children.get(&Some(id)) {
                for k in kids.iter().rev() {
                    if let Some(kid) = k.id.get().copied() {
                        stack.push((kid, depth + 1));
                    }
                }
            }
        }
    }

    Ok(serde_json::json!({
        "items": page,
        "collections": ordered,
        "uncategorized_count": uncategorized_count,
        "total": total,
        "has_more": has_more,
    }))
}

/// GET /cms-admin/__page-chooser?q=<needle> — JSON endpoint backing
/// the PageChooser widget. Returns the top-N matching
/// pages by title / slug / url_path (LIKE, lowercased), bounded at 50.
/// Empty `q` returns the most recently updated pages so the chooser
/// has something useful to show on first open.
pub async fn page_chooser_json(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    Query(q): Query<PageChooserQuery>,
) -> Result<Response, AdminError> {
    let needle =
        q.q.as_deref()
            .map(str::trim)
            .filter(|s| !s.is_empty())
            .map(str::to_lowercase);

    let mut pages: Vec<Page> = Page::objects()
        .order_by(&[("updated_at", true)])
        .fetch(tenant.pool())
        .await
        .unwrap_or_default();

    if let Some(n) = needle.as_ref() {
        pages.retain(|p| {
            p.title.to_lowercase().contains(n)
                || p.slug.to_lowercase().contains(n)
                || p.url_path.to_lowercase().contains(n)
        });
    }

    let items: Vec<serde_json::Value> = pages
        .iter()
        .take(50)
        .map(|p| {
            serde_json::json!({
                "id": p.id.get().copied(),
                "title": p.title,
                "slug": p.slug,
                "url_path": p.url_path,
                "depth": p.depth,
                "parent_id": p.parent_id,
                "status": p.status,
            })
        })
        .collect();

    let body = serde_json::json!({ "items": items });
    Ok((
        [(header::CONTENT_TYPE, "application/json")],
        serde_json::to_string(&body).unwrap_or_else(|_| "{}".to_owned()),
    )
        .into_response())
}

/// Query string for `GET /cms-admin/__snippet-chooser`. Optional
/// `type_name` narrows to one snippet kind — the `data-chooser-filter`
/// attribute on the widget threads through.
#[derive(Debug, Deserialize)]
pub struct SnippetChooserQuery {
    #[serde(default)]
    pub q: Option<String>,
    #[serde(default)]
    pub type_name: Option<String>,
    /// Comma-separated snippet ids — resolve exactly these rows. Used by
    /// the widget-label hydration pass (a server-rendered value only has
    /// the raw id).
    #[serde(default)]
    pub ids: Option<String>,
}

/// GET /cms-admin/__snippet-chooser?q=…&type_name=… — JSON endpoint
/// backing the SnippetChooser widget. Returns the
/// top-50 matching snippets by title / slug; optionally filtered
/// to a single `type_name`.
pub async fn snippet_chooser_json(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    Query(q): Query<SnippetChooserQuery>,
) -> Result<Response, AdminError> {
    let needle =
        q.q.as_deref()
            .map(str::trim)
            .filter(|s| !s.is_empty())
            .map(str::to_lowercase);
    let type_filter = q
        .type_name
        .as_deref()
        .map(str::trim)
        .filter(|s| !s.is_empty())
        .map(str::to_owned);

    let mut snippets: Vec<crate::snippet::Snippet> = crate::snippet::Snippet::objects()
        .order_by(&[("updated_at", true)])
        .fetch(tenant.pool())
        .await
        .unwrap_or_default();

    if let Some(ids) = q.ids.as_deref() {
        let id_set: std::collections::HashSet<i64> = ids
            .split(',')
            .filter_map(|t| t.trim().parse::<i64>().ok())
            .collect();
        snippets.retain(|s| s.id.get().copied().is_some_and(|id| id_set.contains(&id)));
    }
    if let Some(tn) = type_filter.as_ref() {
        snippets.retain(|s| &s.type_name == tn);
    }
    if let Some(n) = needle.as_ref() {
        snippets
            .retain(|s| s.title.to_lowercase().contains(n) || s.slug.to_lowercase().contains(n));
    }

    let items: Vec<serde_json::Value> = snippets
        .iter()
        .take(50)
        .map(|s| {
            serde_json::json!({
                "id": s.id.get().copied(),
                "title": s.title,
                "slug": s.slug,
                "type_name": s.type_name,
            })
        })
        .collect();

    let body = serde_json::json!({ "items": items });
    Ok((
        [(header::CONTENT_TYPE, "application/json")],
        serde_json::to_string(&body).unwrap_or_else(|_| "{}".to_owned()),
    )
        .into_response())
}

/// Query string for `GET /cms-admin/__document-chooser`.
#[derive(Debug, Deserialize)]
pub struct DocumentChooserQuery {
    #[serde(default)]
    pub q: Option<String>,
}

/// GET /cms-admin/__document-chooser?q=… — JSON endpoint backing
/// the DocumentChooser widget. Returns top-50 media
/// rows where `kind = "document"` (i.e. non-images), filtered by
/// title / filename.
pub async fn document_chooser_json(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    Query(q): Query<DocumentChooserQuery>,
) -> Result<Response, AdminError> {
    let needle =
        q.q.as_deref()
            .map(str::trim)
            .filter(|s| !s.is_empty())
            .map(str::to_lowercase);

    let mut rows: Vec<Media> = Media::objects()
        .where_(Media::kind.eq("document".to_owned()))
        .order_by(&[("uploaded_at", true)])
        .fetch(tenant.pool())
        .await
        .unwrap_or_default();

    if let Some(n) = needle.as_ref() {
        rows.retain(|m| {
            m.title.to_lowercase().contains(n) || m.filename.to_lowercase().contains(n)
        });
    }

    let items: Vec<serde_json::Value> = rows
        .iter()
        .take(50)
        .map(|m| {
            serde_json::json!({
                "id": m.id.get().copied(),
                "title": m.title,
                "filename": m.filename,
                "kind": m.kind,
                "mime": m.mime,
            })
        })
        .collect();

    let body = serde_json::json!({ "items": items });
    Ok((
        [(header::CONTENT_TYPE, "application/json")],
        serde_json::to_string(&body).unwrap_or_else(|_| "{}".to_owned()),
    )
        .into_response())
}

/// GET /cms-admin/search?q=<needle>[&autocomplete=1] — unified search
/// across pages, snippets, and media. In-memory case-insensitive
/// substring match only; the ranked `crate::search` backend is not
/// wired into this handler.
pub async fn admin_search(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Query(q): Query<AdminSearchQuery>,
) -> Result<Response, AdminError> {
    let needle =
        q.q.as_deref()
            .map(str::trim)
            .filter(|s| !s.is_empty())
            .map(str::to_lowercase);
    let autocomplete = q.autocomplete.as_deref() == Some("1");

    // #143 — log every non-empty query. Best-effort; never blocks
    // the response. Skip autocomplete pings so the analytics surface
    // tracks user-intent queries, not keystroke-level ones.
    if !autocomplete {
        if let Some(raw) = q.q.as_deref().map(str::trim).filter(|s| !s.is_empty()) {
            if let Err(e) = crate::search_promotion::log_query(tenant.pool(), raw).await {
                tracing::debug!(
                    target: "rustango_cms::search",
                    error = %e,
                    "search query log failed (best-effort)",
                );
            }
        }
    }

    let mut pages_out: Vec<serde_json::Value> = Vec::new();
    let mut snippets_out: Vec<serde_json::Value> = Vec::new();
    let mut media_out: Vec<serde_json::Value> = Vec::new();

    if let Some(needle) = needle.as_ref() {
        // Pages. #204 — recall extends from title/slug/url to also
        // include seo_title, seo_description, og_title, og_description
        // and (best-effort) the page-type extension's `load_extension`
        // JSON. Title hits float to the top — they're more relevant
        // than a stray match in a body paragraph.
        let all_pages: Vec<Page> = Page::objects()
            .order_by(&[("updated_at", true)])
            .fetch(tenant.pool())
            .await
            .unwrap_or_default();
        let page_types: Vec<PageType> = PageType::objects()
            .fetch(tenant.pool())
            .await
            .unwrap_or_default();
        let page_type_by_id: std::collections::HashMap<i64, String> = page_types
            .into_iter()
            .filter_map(|pt| pt.id.get().copied().map(|id| (id, pt.type_name)))
            .collect();
        let mut title_hits: Vec<serde_json::Value> = Vec::new();
        let mut body_hits: Vec<serde_json::Value> = Vec::new();
        for p in all_pages {
            if title_hits.len() + body_hits.len() >= 20 {
                break;
            }
            let title = p.title.to_lowercase();
            let slug = p.slug.to_lowercase();
            let url_path = p.url_path.to_lowercase();
            let primary_hit =
                title.contains(needle) || slug.contains(needle) || url_path.contains(needle);

            // Wider body search — SEO/OG fields + extension JSON.
            let mut body_match = false;
            if !primary_hit {
                if p.seo_title.to_lowercase().contains(needle)
                    || p.seo_description.to_lowercase().contains(needle)
                    || p.og_title.to_lowercase().contains(needle)
                    || p.og_description.to_lowercase().contains(needle)
                {
                    body_match = true;
                }
                if !body_match {
                    if let Some(type_name) = page_type_by_id.get(&p.page_type_id) {
                        if let Some(handler) = crate::page_type::find_handler(type_name) {
                            let page_id = p.id.get().copied().unwrap_or_default();
                            if let Ok(ext) = handler.load_extension(tenant.pool(), page_id).await {
                                body_match = json_contains_needle(&ext, needle);
                            }
                        }
                    }
                }
            }

            if primary_hit || body_match {
                let row = serde_json::json!({
                    "id": p.id.get().copied(),
                    "title": p.title,
                    "slug": p.slug,
                    "url_path": p.url_path,
                    "status": p.status,
                });
                if primary_hit {
                    title_hits.push(row);
                } else {
                    body_hits.push(row);
                }
            }
        }
        // Title-first ordering — primary hits float above body hits.
        pages_out.extend(title_hits);
        pages_out.extend(body_hits);
        if pages_out.len() > 20 {
            pages_out.truncate(20);
        }

        // Snippets.
        let all_snippets: Vec<crate::snippet::Snippet> = crate::snippet::Snippet::objects()
            .order_by(&[("updated_at", true)])
            .fetch(tenant.pool())
            .await
            .unwrap_or_default();
        for s in all_snippets {
            if snippets_out.len() >= 20 {
                break;
            }
            let title = s.title.to_lowercase();
            let slug = s.slug.to_lowercase();
            if title.contains(needle) || slug.contains(needle) {
                snippets_out.push(serde_json::json!({
                    "id": s.id.get().copied(),
                    "title": s.title,
                    "slug": s.slug,
                    "type_name": s.type_name,
                }));
            }
        }

        // Media.
        let all_media: Vec<crate::media::Media> = crate::media::Media::objects()
            .order_by(&[("created_at", true)])
            .fetch(tenant.pool())
            .await
            .unwrap_or_default();
        for m in all_media {
            if media_out.len() >= 20 {
                break;
            }
            let title = m.title.to_lowercase();
            let filename = m.filename.to_lowercase();
            let alt = m.alt_text.to_lowercase();
            if title.contains(needle) || filename.contains(needle) || alt.contains(needle) {
                media_out.push(serde_json::json!({
                    "id": m.id.get().copied(),
                    "title": m.title,
                    "filename": m.filename,
                    "alt_text": m.alt_text,
                    "kind": m.kind,
                }));
            }
        }
    }

    // #107 — plugin-contributed search sources
    // (`register_admin_search_area!`). Each area returns its own
    // hit list keyed by area label. Empty when no query.
    let plugin_areas: Vec<serde_json::Value> = if let Some(query) = q.q.as_deref() {
        let trimmed = query.trim();
        if trimmed.is_empty() {
            Vec::new()
        } else {
            crate::hooks::run_admin_search_areas(trimmed)
                .into_iter()
                .map(|(label, hits)| {
                    serde_json::json!({
                        "label": label,
                        "hits": hits,
                    })
                })
                .collect()
        }
    } else {
        Vec::new()
    };

    if autocomplete {
        // Compact JSON shape for the topbar dropdown.
        let payload = serde_json::json!({
            "q": q.q,
            "pages": pages_out.iter().take(5).collect::<Vec<_>>(),
            "snippets": snippets_out.iter().take(5).collect::<Vec<_>>(),
            "media": media_out.iter().take(5).collect::<Vec<_>>(),
            "plugin_areas": plugin_areas,
        });
        return Ok((
            [(header::CONTENT_TYPE, "application/json")],
            serde_json::to_string(&payload).unwrap_or_else(|_| "{}".to_owned()),
        )
            .into_response());
    }

    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "search", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("query", &q.q.unwrap_or_default());
    ctx.insert("pages", &pages_out);
    ctx.insert("snippets", &snippets_out);
    ctx.insert("media", &media_out);
    let plugin_hit_count: usize = plugin_areas
        .iter()
        .filter_map(|a| a.get("hits").and_then(|h| h.as_array()).map(Vec::len))
        .sum();
    ctx.insert("plugin_search_areas", &plugin_areas);
    ctx.insert(
        "total_hits",
        &(pages_out.len() + snippets_out.len() + media_out.len() + plugin_hit_count),
    );
    render_with_csrf(&state, &headers, "rcms_admin/admin_search.html", &mut ctx)
}

// =====================================================================
// Navigation menus (#22)
// =====================================================================

/// GET /cms-admin/navigation — list every menu.
pub async fn navigation_list(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
) -> Result<Response, AdminError> {
    let menus: Vec<crate::navigation::Menu> = crate::navigation::Menu::objects()
        .order_by(&[("name", false)])
        .fetch(tenant.pool())
        .await?;
    // Per-menu item count so the list view shows "(N items)".
    let all_items: Vec<crate::navigation::MenuItem> = crate::navigation::MenuItem::objects()
        .fetch(tenant.pool())
        .await?;
    let mut counts: std::collections::HashMap<i64, i64> = std::collections::HashMap::new();
    for it in &all_items {
        *counts.entry(it.menu_id).or_insert(0) += 1;
    }
    let rows: Vec<serde_json::Value> = menus
        .iter()
        .map(|m| {
            let id = m.id.get().copied().unwrap_or_default();
            serde_json::json!({
                "id": id,
                "slug": m.slug,
                "name": m.name,
                "item_count": counts.get(&id).copied().unwrap_or(0),
                "created_at": m.created_at.get().copied(),
            })
        })
        .collect();

    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "navigation", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("menus", &rows);
    render_with_csrf(
        &state,
        &headers,
        "rcms_admin/navigation_list.html",
        &mut ctx,
    )
}

/// Form payload for create / rename of a menu.
#[derive(Debug, Deserialize)]
pub struct MenuForm {
    pub slug: String,
    pub name: String,
}

/// POST /cms-admin/navigation/new — create a new menu.
pub async fn navigation_create_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Form(form): Form<MenuForm>,
) -> Result<Response, AdminError> {
    let slug = form.slug.trim().to_lowercase();
    let name = form.name.trim().to_owned();
    if slug.is_empty() || name.is_empty() {
        return redirect_named_with_message(
            "rcms-admin:navigation:list",
            MsgLevel::Warning,
            "Menu slug + name are both required.",
            &headers,
        );
    }
    let mut row = crate::navigation::Menu {
        id: rustango::sql::Auto::Unset,
        slug: slug.clone(),
        name,
        created_at: rustango::sql::Auto::Unset,
    };
    row.insert_pool(tenant.pool()).await?;
    redirect_named_with_message_args(
        "rcms-admin:navigation:list",
        MsgLevel::Success,
        "Menu “{slug}” created.",
        &[("slug", slug.as_str())],
        &headers,
    )
}

/// POST /cms-admin/navigation/{id}/delete — delete a menu (cascade
/// deletes its items via the schema FK).
pub async fn navigation_delete_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
) -> Result<Response, AdminError> {
    let menu = crate::navigation::Menu::objects()
        .where_(crate::navigation::Menu::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    // Items, their translations, then the menu — all in one transaction.
    // This used to delete items in fetch order with pool-level writes, so
    // a nested menu tripped the self-FK partway through and left the menu
    // half-deleted, and a translated item made it fail every time.
    let items: Vec<crate::navigation::MenuItem> = crate::navigation::MenuItem::objects()
        .where_(crate::navigation::MenuItem::menu_id.eq(id))
        .fetch(tenant.pool())
        .await?;
    rustango::atomic!(tenant.pool(), |tx| {
        let mut guard = tx.lock().await?;
        let tx = &mut *guard;
        delete_menu_items_tx(tx, items).await?;
        menu.delete_tx(tx).await?;
        Ok::<_, rustango::sql::ExecError>(())
    })
    .await?;
    redirect_named_with_message(
        "rcms-admin:navigation:list",
        MsgLevel::Success,
        "Menu deleted.",
        &headers,
    )
}

/// POST /cms-admin/navigation/{id}/clone — duplicate a menu + all
/// its items. The new menu inherits the same item tree
/// (parent_id pointers remapped to the new MenuItem ids); page links
/// stay pointed at the same pages (no page duplication). Slug gets a
/// "-copy" suffix, then "-copy-2" / "-copy-3" / … to dodge the
/// unique constraint on `Menu.slug`. Name gets " (copy)" appended.
pub async fn navigation_clone_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
) -> Result<Response, AdminError> {
    let source = crate::navigation::Menu::objects()
        .where_(crate::navigation::Menu::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;

    // Pick a unique slug. Menu.slug is unique across the tenant.
    let base = source.slug.trim_matches('-').to_owned();
    let desired = format!("{base}-copy");
    let existing_slugs: std::collections::HashSet<String> = crate::navigation::Menu::objects()
        .fetch(tenant.pool())
        .await?
        .into_iter()
        .map(|m| m.slug)
        .collect();
    let slug = if !existing_slugs.contains(&desired) {
        desired
    } else {
        let mut chosen = None;
        for n in 2..=100 {
            let candidate = format!("{desired}-{n}");
            if !existing_slugs.contains(&candidate) {
                chosen = Some(candidate);
                break;
            }
        }
        chosen.ok_or_else(|| {
            AdminError::Validation(
                "Could not allocate a unique slug for the cloned menu.".to_owned(),
            )
        })?
    };

    let mut clone_menu = crate::navigation::Menu {
        id: rustango::sql::Auto::Unset,
        slug,
        name: format!("{} (copy)", source.name),
        created_at: rustango::sql::Auto::Unset,
    };
    // Clone items. `parent_id` needs remapping from source ids to the
    // freshly-inserted clone ids, so a parent must be inserted before its
    // children. `(parent_id.is_some(), sort_order, id)` is NOT that
    // order: it puts every non-root in one bucket sorted by `sort_order`,
    // which says nothing about depth. `seed_from_pages` numbers
    // `sort_order` per sibling bucket, so a grandchild routinely sorts
    // ahead of its parent — cloning a three-level menu then failed
    // partway and, with no transaction, left a junk `*-copy` behind.
    // Pre-order is the guarantee we actually need.
    let items = crate::navigation::order_pre_order(
        crate::navigation::MenuItem::objects()
            .where_(crate::navigation::MenuItem::menu_id.eq(id))
            .order_by(&[("sort_order", false), ("id", false)])
            .fetch(tenant.pool())
            .await?,
    );
    let count = items.len().to_string();

    // Menu row and items together: a failure halfway through used to
    // leave the tenant with an empty, half-built `*-copy` menu.
    let new_menu_id = rustango::atomic!(tenant.pool(), |tx| {
        let mut guard = tx.lock().await?;
        let tx = &mut *guard;
        clone_menu.save_tx(tx).await?;
        let new_menu_id = clone_menu.id.get().copied().unwrap_or_default();

        let mut id_map: std::collections::HashMap<i64, i64> = std::collections::HashMap::new();
        for item in &items {
            let old_id = item.id.get().copied().unwrap_or_default();
            // Pre-order guarantees the parent is mapped. An unreachable
            // item (orphan or cycle) is appended by `order_pre_order`
            // and re-parents to the root rather than aborting the clone
            // — losing the nesting of already-broken data beats refusing
            // to copy the menu at all.
            let new_parent = item.parent_id.and_then(|p| id_map.get(&p).copied());
            let mut clone_item = crate::navigation::MenuItem {
                id: rustango::sql::Auto::Unset,
                menu_id: new_menu_id,
                parent_id: new_parent,
                sort_order: item.sort_order,
                label: item.label.clone(),
                page_id: item.page_id,
                external_url: item.external_url.clone(),
                open_in_new_tab: item.open_in_new_tab,
            };
            clone_item.save_tx(tx).await?;
            id_map.insert(old_id, clone_item.id.get().copied().unwrap_or_default());
        }
        Ok::<_, rustango::sql::ExecError>(new_menu_id)
    })
    .await?;

    redirect_named_with_params_and_message_args(
        "rcms-admin:navigation:edit",
        &[("id", new_menu_id.to_string())],
        MsgLevel::Success,
        "Cloned “{name}” with {count} items.",
        &[("name", source.name.as_str()), ("count", count.as_str())],
        &headers,
    )
}

/// POST /cms-admin/navigation/{id}/seed-from-pages — Option B.
/// Replaces this menu's items with a tree
/// seeded from every published page that has `show_in_menus = true`.
/// The page tree's structure is preserved: a child MenuItem points
/// at its parent MenuItem when both pages are in the menu set; if a
/// page's parent is NOT in the menu set, the menu item lands at
/// the top level.
///
/// Idempotent — running it twice rebuilds the same tree (page ids
/// stay constant, so the second pass produces identical items).
/// Drops the existing menu items first; failure rolls back via
/// the per-row delete loop (matches the save-tree handler's
/// drop-and-rebuild approach).
pub async fn navigation_seed_from_pages_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
) -> Result<Response, AdminError> {
    use rustango::core::Column as _;
    use rustango::sql::FetcherPool as _;

    let menu = crate::navigation::Menu::objects()
        .where_(crate::navigation::Menu::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;

    // Step 1: collect what is there now. Deleted inside the transaction
    // below, not here — this used to delete row by row on the pool, in
    // fetch order, so re-seeding a nested menu tripped the self-FK
    // partway through and left the menu wiped with nothing re-inserted.
    // A second click on an "idempotent" button was straight data loss.
    let existing: Vec<crate::navigation::MenuItem> = crate::navigation::MenuItem::objects()
        .where_(crate::navigation::MenuItem::menu_id.eq(id))
        .fetch(tenant.pool())
        .await?;

    // Step 2: fetch every menu-eligible page in materialized-path
    // order (tree-pre-order: a parent always appears before its
    // children, so the parent's new MenuItem id is mapped by the
    // time the child's row is processed).
    let mut pages: Vec<crate::page::Page> = crate::page::Page::objects()
        .where_(crate::page::Page::show_in_menus.eq(true))
        .where_(crate::page::Page::status.eq("published".to_owned()))
        .order_by(&[("path", false), ("id", false)])
        .fetch(tenant.pool())
        .await?;
    // Error pages are routing furniture, not navigation — excluded here
    // as `auto_menu`, `sitemap` and the tree endpoint all exclude them.
    // A 404 handler marked "Show in menus" used to land in the navbar.
    let error_tid = crate::error_pages::error_page_type_id(tenant.pool()).await;
    pages.retain(|p| error_tid.is_none_or(|tid| p.page_type_id != tid));

    // Step 3: insert one MenuItem per page, mapping page.parent_id →
    // the freshly-inserted MenuItem id of that parent (if the parent
    // is also in the menu set). sort_order mirrors the sibling-
    // relative order in the page tree.
    let mut page_to_item: std::collections::HashMap<i64, i64> = std::collections::HashMap::new();
    let mut sibling_counter: std::collections::HashMap<Option<i64>, i32> =
        std::collections::HashMap::new();
    // One item per eligible page, so the count is known before the write
    // — and reading a counter mutated inside the transaction closure
    // would always have returned 0 anyway.
    let inserted = pages.len();
    rustango::atomic!(tenant.pool(), |tx| {
        let mut guard = tx.lock().await?;
        let tx = &mut *guard;
    delete_menu_items_tx(tx, existing).await?;
    for page in &pages {
        let page_id = page.id.get().copied().unwrap_or_default();
        let parent_menu_item_id: Option<i64> = page
            .parent_id
            .and_then(|pid| page_to_item.get(&pid).copied());
        // Sort order: monotonically increasing within each
        // parent-bucket so the menu builder shows pages in the same
        // sibling order the page tree has.
        let bucket = parent_menu_item_id;
        let sort_order = *sibling_counter
            .entry(bucket)
            .and_modify(|n| *n += 1)
            .or_insert(0);
        let mut item = crate::navigation::MenuItem {
            id: rustango::sql::Auto::Unset,
            menu_id: id,
            parent_id: parent_menu_item_id,
            sort_order,
            label: page.title.clone(),
            page_id: Some(page_id),
            external_url: None,
            open_in_new_tab: false,
        };
        item.save_tx(tx).await?;
        let new_item_id = item.id.get().copied().unwrap_or_default();
        page_to_item.insert(page_id, new_item_id);
    }
    Ok::<_, rustango::sql::ExecError>(())
    })
    .await?;

    redirect_named_with_params_and_message_plural(
        "rcms-admin:navigation:edit",
        &[("id", id.to_string())],
        MsgLevel::Success,
        "Seeded “{name}” with {count} item(s) from pages marked Show in menus.",
        inserted as i64,
        &[
            ("name", menu.name.as_str()),
            ("count", &inserted.to_string()),
        ],
        &headers,
    )
}

/// Delete menu items and their translations inside a transaction.
///
/// `cms_menu_item_translation.item_id` and `cms_menu_item.parent_id` are
/// both FKs with no `ON DELETE`, so the order is forced: translations
/// first, then items deepest-first. Three handlers got this wrong in
/// three different ways before it lived in one place — the menu delete
/// ignored translations entirely, the re-seed deleted in fetch order, and
/// save-tree sorted on `parent_id.is_none()`, which ties every non-root.
async fn delete_menu_items_tx(
    tx: &mut rustango::sql::PoolTx<'_>,
    items: Vec<crate::navigation::MenuItem>,
) -> Result<(), rustango::sql::ExecError> {
    use crate::menu_item_translation::MenuItemTranslation;
    if items.is_empty() {
        return Ok(());
    }
    let ids: Vec<i64> = items.iter().filter_map(|it| it.id.get().copied()).collect();
    if !ids.is_empty() {
        let orphans: Vec<MenuItemTranslation> = MenuItemTranslation::objects()
            .where_(MenuItemTranslation::item_id.is_in(ids))
            .fetch_tx(tx)
            .await?;
        for row in orphans {
            row.delete_tx(tx).await?;
        }
    }
    for it in crate::navigation::order_deepest_first(items) {
        it.delete_tx(tx).await?;
    }
    Ok(())
}

/// POST /cms-admin/navigation/{id}/save-tree — WP-style menu builder
/// save. Accepts a JSON array of items in display order; each
/// entry's `local_id` is its position in the array (0-indexed) and
/// `parent_local_id` references the array index of the parent (null
/// for top-level). The handler:
///
///   1. Validates that the tree is acyclic + every parent_local_id
///      < the child's local_id (insert-order invariant).
///   2. Updates rows the payload still carries an `id` for, **in
///      place**, and inserts the rest.
///   3. Deletes the rows the payload dropped, children before parents.
///
/// It used to delete every row and re-insert, which meant a menu item's
/// id changed on every save. That was survivable while nothing
/// referenced those ids — and stopped being survivable the moment
/// `cms_menu_item_translation` keyed translations on `item_id`, since a
/// reorder would silently discard every translated label. Ids are now
/// stable across a save.
///
/// Failure rolls back; no half-saved state.
#[derive(Debug, Deserialize)]
pub struct SaveTreeItem {
    /// Existing `cms_menu_item.id`. `None` for items added in this
    /// session, in which case the handler inserts a new row. An id that
    /// doesn't belong to this menu is treated the same way, so a stale
    /// or forged id can't reassign another menu's item.
    #[serde(default)]
    pub id: Option<i64>,
    /// 0-indexed position in the submitted array.
    pub local_id: i32,
    /// Array index of the parent, or `None` for top-level.
    #[serde(default)]
    pub parent_local_id: Option<i32>,
    pub label: String,
    #[serde(default)]
    pub page_id: Option<i64>,
    #[serde(default)]
    pub external_url: Option<String>,
    #[serde(default)]
    pub open_in_new_tab: bool,
}

#[derive(Debug, Deserialize)]
pub struct SaveTreePayload {
    pub items: Vec<SaveTreeItem>,
}

pub async fn navigation_save_tree_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    Path(id): Path<i64>,
    axum::Json(payload): axum::Json<SaveTreePayload>,
) -> Result<Response, AdminError> {
    // Validate tree shape: every parent_local_id must reference an
    // earlier index (so we can insert in order without two passes).
    for (i, item) in payload.items.iter().enumerate() {
        if item.local_id as usize != i {
            return Err(AdminError::Validation(format!(
                "items[{i}].local_id must equal its array index"
            )));
        }
        if let Some(p) = item.parent_local_id {
            if (p as usize) >= i {
                return Err(AdminError::Validation(format!(
                    "items[{i}].parent_local_id {p} must reference an earlier item"
                )));
            }
        }
    }

    // Exactly one link target, and lengths the columns can hold. The
    // form handler enforced this and `save-tree` did not, so the builder
    // could store an item with both (`page_id` silently wins, and
    // unpublishing that page drops an item carrying a perfectly good
    // external fallback) or with neither (rendered as a link to `/`,
    // indistinguishable from a grouping header). An over-long label
    // reached the driver as a raw error and surfaced as a 500.
    for (i, item) in payload.items.iter().enumerate() {
        let external = item
            .external_url
            .as_deref()
            .map(str::trim)
            .filter(|s| !s.is_empty());
        match (item.page_id, external) {
            (Some(_), Some(_)) => {
                return Err(AdminError::Validation(format!(
                    "items[{i}] sets both a page and an external URL — pick one"
                )))
            }
            (None, None) => {
                return Err(AdminError::Validation(format!(
                    "items[{i}] has no link target — set a page or an external URL"
                )))
            }
            _ => {}
        }
        if item.label.chars().count() > 200 {
            return Err(AdminError::Validation(format!(
                "items[{i}].label is too long (max 200 characters)"
            )));
        }
        if external.is_some_and(|u| u.chars().count() > 500) {
            return Err(AdminError::Validation(format!(
                "items[{i}].external_url is too long (max 500 characters)"
            )));
        }
    }

    // An id may appear at most once. The upsert below claims each kept row
    // out of `existing_by_id`, so a repeat would find the row already
    // taken — previously an `.expect` that panicked mid-transaction.
    let mut seen: std::collections::HashSet<i64> = std::collections::HashSet::new();
    for (i, item) in payload.items.iter().enumerate() {
        if let Some(iid) = item.id {
            if !seen.insert(iid) {
                return Err(AdminError::Validation(format!(
                    "items[{i}].id {iid} appears more than once"
                )));
            }
        }
    }

    // Every sibling handler verifies the menu first; this one went
    // straight to inserting rows with `menu_id: id`, so a stale or
    // mistyped id surfaced as a raw FK violation (a 500) instead of a
    // 404.
    if crate::navigation::Menu::objects()
        .where_(crate::navigation::Menu::id.eq(id))
        .first(tenant.pool())
        .await?
        .is_none()
    {
        return Err(AdminError::NotFound(id));
    }

    let existing: Vec<crate::navigation::MenuItem> = crate::navigation::MenuItem::objects()
        .where_(crate::navigation::MenuItem::menu_id.eq(id))
        .fetch(tenant.pool())
        .await?;
    let mut existing_by_id: std::collections::HashMap<i64, crate::navigation::MenuItem> = existing
        .into_iter()
        .filter_map(|it| it.id.get().copied().map(|iid| (iid, it)))
        .collect();

    // Claim each payload entry's existing row up front, so the write loop
    // below has no fallible lookup left to make. `Some(row)` updates in
    // place (keeping the id, and with it the item's translations);
    // `None` inserts. An id from another menu, or one that isn't in this
    // menu at all, resolves to `None` rather than stealing that row.
    let plan: Vec<Option<crate::navigation::MenuItem>> = payload
        .items
        .iter()
        .map(|it| it.id.and_then(|iid| existing_by_id.remove(&iid)))
        .collect();

    let items_saved = payload.items.len();
    rustango::atomic!(tenant.pool(), |tx| {
        let mut guard = tx.lock().await?;
        let tx = &mut *guard;
        // Upsert in array order, resolving each item's parent from the
        // slot its `local_id` indexes. The validation above pins
        // `local_id` to the array index and requires
        // `parent_local_id < local_id`, so a parent's slot is always
        // filled before a child reads it — no fallible lookup here, and
        // no way to express one.
        let mut resolved: Vec<i64> = Vec::with_capacity(payload.items.len());
        for ((i, item), claimed) in payload.items.iter().enumerate().zip(plan) {
            let parent_id = item.parent_local_id.map(|p| resolved[p as usize]);
            let external_url = item
                .external_url
                .as_ref()
                .filter(|s| !s.trim().is_empty())
                .cloned();
            let sort_order = (i as i32) * 10;

            let new_id = match claimed {
                Some(mut row) => {
                    // Update in place — this is what keeps the id, and
                    // therefore the item's translations, alive.
                    let iid = row.id.get().copied().unwrap_or_default();
                    row.parent_id = parent_id;
                    row.sort_order = sort_order;
                    row.label = item.label.clone();
                    row.page_id = item.page_id;
                    row.external_url = external_url;
                    row.open_in_new_tab = item.open_in_new_tab;
                    row.save_tx(tx).await?;
                    iid
                }
                None => {
                    let mut row = crate::navigation::MenuItem {
                        id: rustango::sql::Auto::Unset,
                        menu_id: id,
                        parent_id,
                        sort_order,
                        label: item.label.clone(),
                        page_id: item.page_id,
                        external_url,
                        open_in_new_tab: item.open_in_new_tab,
                    };
                    row.save_tx(tx).await?;
                    row.id.get().copied().unwrap_or_default()
                }
            };
            resolved.push(new_id);
        }

        // Whatever the payload dropped. Every surviving row has already
        // been repointed above, so nothing still references these — but
        // they may reference each other, and `MenuItem.parent_id` is an
        // FK to `cms_menu_item.id` with no `ON DELETE`, so a parent can
        // only go once its children are gone.
        delete_menu_items_tx(tx, existing_by_id.into_values().collect()).await?;
        Ok::<_, rustango::sql::ExecError>(())
    })
    .await?;

    Ok(axum::Json(serde_json::json!({
        "ok": true,
        "items_saved": items_saved,
    }))
    .into_response())
}

/// GET /cms-admin/navigation/{id}/edit — item editor for one menu.
pub async fn navigation_edit_form(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
    Query(q): Query<EditQuery>,
) -> Result<Response, AdminError> {
    let menu = crate::navigation::Menu::objects()
        .where_(crate::navigation::Menu::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    // Pre-order, not `ORDER BY parent_id`. The builder derives each
    // item's parent from DOM adjacency on save, so the DOM has to start
    // in pre-order or merely opening a menu and pressing Save rewrites
    // its nesting — see `navigation::order_pre_order`.
    let items = crate::navigation::order_pre_order(
        crate::navigation::MenuItem::objects()
            .where_(crate::navigation::MenuItem::menu_id.eq(id))
            .order_by(&[("sort_order", false), ("id", false)])
            .fetch(tenant.pool())
            .await?,
    );
    // Pages flagged `show_in_menus = true` are the suggested-picker
    // entries; fall back to "every page" if no editor has opted any
    // pages in yet.
    let suggested: Vec<Page> = Page::objects()
        .where_(Page::show_in_menus.eq(true))
        .order_by(&[("title", false)])
        .fetch(tenant.pool())
        .await?;
    // WP-style builder (#44): the picker always shows every page so
    // editors can drop any of them in. The legacy curated flag stays
    // available for downstream theme rendering but no longer hides
    // pages from the admin picker.
    let all_pages: Vec<Page> = Page::objects()
        .order_by(&[("title", false)])
        .fetch(tenant.pool())
        .await?;

    // Resolve item rows into a render-friendly shape (label, url,
    // page title for the dropdown when set).
    let by_page_id: std::collections::HashMap<i64, &Page> = all_pages
        .iter()
        .filter_map(|p| p.id.get().copied().map(|id| (id, p)))
        .collect();
    // ?locale=<non-default> switches the editor into translation mode:
    // the tree builder still edits canonical structure, and a second
    // panel edits this locale's label overrides. Same shape as the page
    // and snippet editors, so the locale switcher behaves identically
    // wherever an editor meets one.
    let locales: Vec<Locale> = Locale::objects()
        .where_(Locale::active.eq(true))
        .order_by(&[("is_default", true), ("code", false)])
        .fetch(tenant.pool())
        .await?;
    let editing_locale: Option<Locale> = match q.locale.as_deref().filter(|c| !c.is_empty()) {
        Some(code) => locales.iter().find(|l| l.code == code).cloned(),
        None => None,
    };
    let translation_mode = editing_locale.as_ref().is_some_and(|l| !l.is_default);
    let item_translations = match editing_locale.as_ref().filter(|l| !l.is_default) {
        Some(loc) => {
            let lid = loc.id.get().copied().unwrap_or_default();
            let ids: Vec<i64> = items.iter().filter_map(|it| it.id.get().copied()).collect();
            crate::menu_item_translation::fetch_for_items(tenant.pool(), &ids, lid).await?
        }
        None => std::collections::HashMap::new(),
    };

    let item_rows: Vec<serde_json::Value> = items
        .iter()
        .map(|it| {
            let resolved_label = if it.label.trim().is_empty() {
                it.page_id
                    .and_then(|pid| by_page_id.get(&pid).map(|p| p.title.clone()))
                    .unwrap_or_default()
            } else {
                it.label.clone()
            };
            let resolved_url = match (it.page_id, it.external_url.clone()) {
                (Some(pid), _) => by_page_id
                    .get(&pid)
                    .map(|p| p.url_path.clone())
                    .unwrap_or_else(|| "—".to_owned()),
                (None, Some(url)) => url,
                _ => "—".to_owned(),
            };
            let tr = it
                .id
                .get()
                .and_then(|iid| item_translations.get(iid));
            serde_json::json!({
                "id": it.id.get().copied(),
                "label": it.label,
                "resolved_label": resolved_label,
                "page_id": it.page_id,
                "external_url": it.external_url,
                "parent_id": it.parent_id,
                "sort_order": it.sort_order,
                "open_in_new_tab": it.open_in_new_tab,
                "resolved_url": resolved_url,
                "tr_label": tr.and_then(|m| m.get("label")).cloned().unwrap_or_default(),
                "tr_external_url": tr
                    .and_then(|m| m.get("external_url"))
                    .cloned()
                    .unwrap_or_default(),
            })
        })
        .collect();

    // Error pages are served for a status code; a menu never links one.
    let error_tid = crate::error_pages::error_page_type_id(tenant.pool()).await;
    let linkable = |p: &&Page| error_tid.map_or(true, |tid| p.page_type_id != tid);
    let picker_pages: Vec<serde_json::Value> = all_pages
        .iter()
        .filter(linkable)
        .map(|p| {
            serde_json::json!({
                "id": p.id.get().copied(),
                "title": p.title,
                "url_path": p.url_path,
            })
        })
        .collect();

    // #49c — recent-pages tab. 20 most-recently-updated pages so
    // editors who just published something can drop it into the menu
    // without scrolling the full picker. Fetched via a separate
    // updated_at-desc query so the existing alphabetical picker
    // stays unchanged.
    let recent_pages_raw: Vec<Page> = Page::objects()
        .order_by(&[("updated_at", true)])
        .fetch(tenant.pool())
        .await?;
    let picker_recent: Vec<serde_json::Value> = recent_pages_raw
        .iter()
        .filter(linkable)
        .take(20)
        .map(|p| {
            serde_json::json!({
                "id": p.id.get().copied(),
                "title": p.title,
                "url_path": p.url_path,
                "updated_at": p.updated_at,
            })
        })
        .collect();

    // #49d — Library tab. Each registered `LibraryTypeHandler` opts
    // in via `menu_picker_entries(pool)`; we flatten the result
    // across every handler and tag each entry with the contributing
    // type's verbose_name so editors can tell where an entry came
    // from. Failure in any handler short-circuits the response so
    // bugs surface loudly instead of silently hiding entries.
    let mut picker_library: Vec<serde_json::Value> = Vec::new();
    for handler in crate::library::registered_handlers() {
        let entries = handler
            .menu_picker_entries(tenant.pool())
            .await
            .map_err(|e| {
                AdminError::Validation(format!(
                    "library `{}::menu_picker_entries` failed: {e}",
                    handler.type_name()
                ))
            })?;
        for entry in entries {
            picker_library.push(serde_json::json!({
                "label": entry.label,
                "url": entry.url,
                "kind_hint": entry.kind_hint,
                "library_type": handler.verbose_name(),
            }));
        }
    }

    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "navigation", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("menu_id", &menu.id.get().copied());
    ctx.insert("menu_slug", &menu.slug);
    ctx.insert("menu_name", &menu.name);
    ctx.insert("items", &item_rows);
    ctx.insert("picker_pages", &picker_pages);
    ctx.insert("picker_recent", &picker_recent);
    ctx.insert("picker_library", &picker_library);
    ctx.insert("has_library_picker", &!picker_library.is_empty());
    ctx.insert("has_curated_picker", &!suggested.is_empty());
    ctx.insert("locales", &locales);
    ctx.insert("editing_locale", &editing_locale);
    ctx.insert("translation_mode", &translation_mode);
    render_with_csrf(
        &state,
        &headers,
        "rcms_admin/navigation_edit.html",
        &mut ctx,
    )
}

/// POST /cms-admin/navigation/{id}/translate?locale=<code> — save this
/// locale's label overrides for a menu's items.
///
/// Separate from the tree save on purpose. The builder posts JSON and
/// owns *structure*; this posts a form and owns *text*, so an editor
/// translating labels cannot accidentally reshape the menu, and a
/// reorder cannot drop a translation.
///
/// Field names are `tr__<item id>__<field>`. An empty value deletes the
/// override rather than storing a blank, which is what makes the
/// canonical label show through again.
pub async fn navigation_translate_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    Path(id): Path<i64>,
    Query(q): Query<EditQuery>,
    Form(form): Form<std::collections::HashMap<String, String>>,
) -> Result<Response, AdminError> {
    use crate::menu_item_translation::MenuItemTranslation;

    let code = q
        .locale
        .as_deref()
        .filter(|c| !c.is_empty())
        .ok_or_else(|| AdminError::Validation("translate requires ?locale=".to_owned()))?;
    let locale = Locale::objects()
        .where_(Locale::code.eq(code.to_owned()))
        .where_(Locale::active.eq(true))
        .first(tenant.pool())
        .await?
        .ok_or_else(|| AdminError::Validation(format!("unknown locale `{code}`")))?;
    if locale.is_default {
        return Ok((
            axum::http::StatusCode::BAD_REQUEST,
            "cannot translate INTO the default locale; canonical labels live on cms_menu_item",
        )
            .into_response());
    }
    let locale_id = locale
        .id
        .get()
        .copied()
        .ok_or_else(|| AdminError::Validation("locale row missing id".to_owned()))?;

    // Only this menu's items are editable here — the field names carry
    // item ids, and without this check a crafted post could write
    // translations for another menu's items.
    let own_items: std::collections::HashSet<i64> = crate::navigation::MenuItem::objects()
        .where_(crate::navigation::MenuItem::menu_id.eq(id))
        .fetch(tenant.pool())
        .await?
        .iter()
        .filter_map(|it| it.id.get().copied())
        .collect();

    let mut updates: Vec<(i64, String, String)> = Vec::new();
    for (k, v) in &form {
        let Some(rest) = k.strip_prefix("tr__") else {
            continue;
        };
        let Some((raw_id, raw_field)) = rest.split_once("__") else {
            continue;
        };
        let Ok(item_id) = raw_id.parse::<i64>() else {
            continue;
        };
        if !own_items.contains(&item_id) {
            continue;
        }
        // A malformed field is skipped rather than 400-ing the batch, so
        // one bad input doesn't discard an editor's other edits.
        if let Ok(field) = crate::menu_item_translation::validate_field_path(raw_field) {
            updates.push((item_id, field, v.trim().to_owned()));
        }
    }

    rustango::atomic!(tenant.pool(), |tx| {
        let mut guard = tx.lock().await?;
        let tx = &mut *guard;
        for (item_id, field, value) in updates {
            let existing: Vec<MenuItemTranslation> = MenuItemTranslation::objects()
                .where_(MenuItemTranslation::item_id.eq(item_id))
                .where_(MenuItemTranslation::locale_id.eq(locale_id))
                .where_(MenuItemTranslation::field_path.eq(field.clone()))
                .fetch_tx(tx)
                .await?;
            if value.is_empty() {
                for row in existing {
                    row.delete_tx(tx).await?;
                }
            } else if let Some(mut row) = existing.into_iter().next() {
                row.value = value;
                row.save_tx(tx).await?;
            } else {
                let mut row = MenuItemTranslation {
                    id: rustango::sql::Auto::Unset,
                    item_id,
                    locale_id,
                    field_path: field,
                    value,
                    updated_at: rustango::sql::Auto::Unset,
                };
                row.save_tx(tx).await?;
            }
        }
        Ok::<_, rustango::sql::ExecError>(())
    })
    .await?;

    Ok(Redirect::to(&format!(
        "/cms-admin/navigation/{id}/edit?locale={code}"
    ))
    .into_response())
}

/// Form payload for adding / updating a menu item. Empty strings on
/// `page_id` and `external_url` map to None; the validator enforces
/// "exactly one of the two".
#[derive(Debug, Deserialize)]
pub struct MenuItemForm {
    pub label: String,
    #[serde(default, deserialize_with = "super::deserialize_optional_i64")]
    pub page_id: Option<i64>,
    #[serde(default)]
    pub external_url: Option<String>,
    #[serde(default, deserialize_with = "super::deserialize_optional_i64")]
    pub parent_id: Option<i64>,
    #[serde(default)]
    pub sort_order: Option<i32>,
    #[serde(default)]
    pub open_in_new_tab: Option<String>,
}

/// Reject a `parent_id` that does not belong to `menu_id`.
///
/// The form took the value straight from the request, so an item could be
/// parented into another menu — leaving it invisible in *both* (each
/// resolver buckets by its own `menu_id`, so the child is unreachable in
/// one and filtered out of the other) and making the other menu
/// permanently undeletable, because its item is now referenced by a row
/// the delete never sees.
async fn validate_menu_parent(
    tenant: &Tenant,
    menu_id: i64,
    parent_id: Option<i64>,
) -> Result<(), AdminError> {
    let Some(pid) = parent_id else {
        return Ok(());
    };
    let parent = crate::navigation::MenuItem::objects()
        .where_(crate::navigation::MenuItem::id.eq(pid))
        .first(tenant.pool())
        .await?;
    match parent {
        Some(p) if p.menu_id == menu_id => Ok(()),
        Some(_) => Err(AdminError::Validation(
            "That parent item belongs to a different menu.".to_owned(),
        )),
        None => Err(AdminError::Validation(
            "That parent item no longer exists.".to_owned(),
        )),
    }
}

impl MenuItemForm {
    fn parsed_external_url(&self) -> Option<String> {
        self.external_url
            .as_deref()
            .map(str::trim)
            .filter(|s| !s.is_empty())
            .map(str::to_owned)
    }
}

/// POST /cms-admin/navigation/{id}/items — create a new menu item.
pub async fn navigation_item_create(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Path(menu_id): Path<i64>,
    Form(form): Form<MenuItemForm>,
) -> Result<Response, AdminError> {
    let external = form.parsed_external_url();
    // "Exactly one of page_id / external_url" — fail soft to the
    // edit page with a warning message.
    if form.page_id.is_some() == external.is_some() {
        return redirect_named_with_params_and_message(
            "rcms-admin:navigation:edit",
            &[("id", menu_id.to_string())],
            MsgLevel::Warning,
            "Pick exactly one of: a page link, or a custom URL.",
            &headers,
        );
    }
    // The parent must live in this menu — see `validate_menu_parent`.
    validate_menu_parent(&tenant, menu_id, form.parent_id).await?;
    let mut row = crate::navigation::MenuItem {
        id: rustango::sql::Auto::Unset,
        menu_id,
        parent_id: form.parent_id,
        sort_order: form.sort_order.unwrap_or(100),
        label: form.label.trim().to_owned(),
        page_id: form.page_id,
        external_url: external,
        open_in_new_tab: form.open_in_new_tab.is_some(),
    };
    row.insert_pool(tenant.pool()).await?;
    redirect_named_with_params_and_message(
        "rcms-admin:navigation:edit",
        &[("id", menu_id.to_string())],
        MsgLevel::Success,
        "Item added.",
        &headers,
    )
}

/// POST /cms-admin/navigation/{menu_id}/items/{id}/delete.
pub async fn navigation_item_delete(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Path((menu_id, id)): Path<(i64, i64)>,
) -> Result<Response, AdminError> {
    let row = crate::navigation::MenuItem::objects()
        .where_(crate::navigation::MenuItem::id.eq(id))
        .where_(crate::navigation::MenuItem::menu_id.eq(menu_id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    // Re-parent any children to the deleted item's parent so the
    // tree doesn't orphan a whole subtree.
    let children: Vec<crate::navigation::MenuItem> = crate::navigation::MenuItem::objects()
        .where_(crate::navigation::MenuItem::parent_id.eq(Some(id)))
        .fetch(tenant.pool())
        .await?;
    for mut c in children {
        c.parent_id = row.parent_id;
        c.save_pool(tenant.pool()).await?;
    }
    row.delete_pool(tenant.pool()).await?;
    redirect_named_with_params_and_message(
        "rcms-admin:navigation:edit",
        &[("id", menu_id.to_string())],
        MsgLevel::Success,
        "Item deleted.",
        &headers,
    )
}

// =====================================================================
// Page types — usage dashboard (#16)
// =====================================================================

/// GET /cms-admin/page-types — registry of every page type the host
/// has compiled in, with how many pages of each currently exist + the
/// most-recent one. Useful before deprecating a type or refactoring
/// its declared fields.
pub async fn page_types_dashboard(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
) -> Result<Response, AdminError> {
    // Pull every Page row once and bucket by `page_type_id` in Rust;
    // sites have N pages but only a handful of types, so one fetch +
    // a HashMap aggregate beats a per-type COUNT round-trip.
    let pages: Vec<Page> = Page::objects().fetch(tenant.pool()).await?;
    let page_types: Vec<PageType> = PageType::objects().fetch(tenant.pool()).await?;

    let mut counts: std::collections::HashMap<i64, i64> = std::collections::HashMap::new();
    let mut latest: std::collections::HashMap<i64, chrono::DateTime<chrono::Utc>> =
        std::collections::HashMap::new();
    for p in &pages {
        *counts.entry(p.page_type_id).or_insert(0) += 1;
        if let Some(when) = p.published_at.or_else(|| p.created_at.get().copied()) {
            latest
                .entry(p.page_type_id)
                .and_modify(|cur| {
                    if when > *cur {
                        *cur = when;
                    }
                })
                .or_insert(when);
        }
    }

    let type_id_by_name: std::collections::HashMap<&str, i64> = page_types
        .iter()
        .filter_map(|t| t.id.get().copied().map(|id| (t.type_name.as_str(), id)))
        .collect();

    // Cross-reference the in-process handler registry with the DB
    // `cms_page_type` rows so unregistered + un-seeded types both
    // surface (handler-only = "compiled in but never used"; row-only
    // = "DB drift from an old build").
    let mut handler_names = std::collections::BTreeSet::new();
    let mut rows: Vec<serde_json::Value> = Vec::new();
    for handler in crate::page_type::registered_handlers() {
        let name = handler.type_name().to_owned();
        handler_names.insert(name.clone());
        let row_id = type_id_by_name.get(name.as_str()).copied();
        let count = row_id.and_then(|id| counts.get(&id).copied()).unwrap_or(0);
        let last = row_id.and_then(|id| latest.get(&id).copied());
        rows.push(serde_json::json!({
            "type_name": name,
            "verbose_name": handler.verbose_name(),
            "app_label": handler.app_label(),
            "icon": handler.icon(),
            "description": handler.description(),
            "default_template": handler.default_template(),
            "feed_kind": handler.feed_kind(),
            "allowed_parents": handler.allowed_parent_types(),
            "allowed_children": handler.allowed_child_types(),
            "is_creatable": handler.is_creatable(),
            "count": count,
            "latest": last,
            "registered": true,
            "row_id": row_id,
        }));
    }

    // DB-only types — appear in `cms_page_type` but no handler is
    // compiled in. Two flavours: UI-created types (#566,
    // `app_label = "cms_ui"`, first-class — creatable + deletable) and
    // build drift (a stale row from an old binary — surfaced so editors
    // notice). Only UI types get a delete affordance, and only while no
    // pages use them; code types are never deletable from the UI.
    for t in &page_types {
        if !handler_names.contains(&t.type_name) {
            let id = t.id.get().copied().unwrap_or_default();
            let count = counts.get(&id).copied().unwrap_or(0);
            let ui_type = t.app_label == "cms_ui";
            rows.push(serde_json::json!({
                "type_name": t.type_name,
                "verbose_name": t.verbose_name,
                "app_label": t.app_label,
                "icon": if ui_type { serde_json::Value::String("dashboard_customize".to_owned()) } else { serde_json::Value::Null },
                "description": serde_json::Value::Null,
                "default_template": t.default_template,
                "feed_kind": serde_json::Value::Null,
                "allowed_parents": t.allowed_parent_types.clone(),
                "allowed_children": t.allowed_child_types.clone(),
                "is_creatable": ui_type && t.is_creatable,
                "count": count,
                "latest": latest.get(&id).copied(),
                "registered": false,
                "row_id": id,
                "ui_type": ui_type,
                "deletable": ui_type && count == 0,
            }));
        }
    }
    // Sort: most-used first; ties break by verbose_name.
    rows.sort_by(|a, b| {
        let ac = a.get("count").and_then(|v| v.as_i64()).unwrap_or(0);
        let bc = b.get("count").and_then(|v| v.as_i64()).unwrap_or(0);
        bc.cmp(&ac).then_with(|| {
            a.get("verbose_name")
                .and_then(|v| v.as_str())
                .unwrap_or("")
                .cmp(b.get("verbose_name").and_then(|v| v.as_str()).unwrap_or(""))
        })
    });

    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "page-types", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("rows", &rows);
    ctx.insert("total_pages", &pages.len());
    ctx.insert("total_types", &rows.len());
    // #566 — the New-page-type affordance is Developer-gated (same
    // codename as the field builder). The template also checks the flag.
    let can_build = session_user.as_ref().is_some_and(|u| u.is_superuser)
        || crate::permissions::user_codenames(
            tenant.pool(),
            session_user
                .as_ref()
                .and_then(|u| u.id.get().copied())
                .unwrap_or_default(),
        )
        .await
        .unwrap_or_default()
        .contains("cms_page_type.build");
    ctx.insert("can_build_types", &can_build);
    ctx.insert("new_type_url", "/cms-admin/page-types/new");
    render_with_csrf(&state, &headers, "rcms_admin/page_types.html", &mut ctx)
}

// =====================================================================
// Users / Roles — native CMS surface (#9). Replaces the prior
// `Redirect::to("/admin/rustango_users")` stub.
// =====================================================================

/// User management is a **superuser-only** operation.
///
/// The create/edit handlers set `is_superuser`, `active`, and role
/// memberships straight from the submitted form, and the handlers
/// themselves previously performed no authorization at all — so any user
/// whose request reached the admin router could mint a superuser or
/// assign themselves a privileged role. Because *every* one of those
/// fields is a privilege the caller might not hold, the safe invariant
/// is to restrict the whole user-management surface to superusers rather
/// than try to police individual field grants. (Delegated, codename-
/// scoped user management that forbids privilege escalation can be added
/// later as a deliberate feature.)
///
/// The same gate covers roles and SSO providers, which decide who
/// a user *is* as much as the user rows do: a role editor could grant its
/// own role every codename, and an SSO-provider editor could point an
/// issuer at an IdP it controls and sign in as any user by email.
///
/// Returns `Some(redirect)` when denied — anonymous → login, an
/// authenticated non-superuser → the no-access page — and `None` when
/// the caller is a superuser and may proceed.
fn require_superuser_for_user_admin(
    user: Option<&rustango::tenancy::auth::User>,
) -> Option<Response> {
    use axum::response::{IntoResponse, Redirect};
    user_admin_denial_target(user.map(|u| u.is_superuser))
        .map(|path| Redirect::to(path).into_response())
}

/// The redirect target for a user-management request, or `None` when the
/// caller (identified only by their superuser status: `None` = anonymous,
/// `Some(false)` = signed-in non-superuser, `Some(true)` = superuser) may
/// proceed. Split out from [`require_superuser_for_user_admin`] so the
/// deny/allow decision is unit-testable without constructing a framework
/// `User` (which has no `Default`).
fn user_admin_denial_target(is_superuser: Option<bool>) -> Option<&'static str> {
    match is_superuser {
        Some(true) => None,
        Some(false) => Some("/cms-admin/no-access"),
        None => Some("/login"),
    }
}

/// GET /cms-admin/users — every tenant user with their assigned
/// roles + a delta-since indicator on the last password rotation.
pub async fn users_list(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
) -> Result<Response, AdminError> {
    if let Some(redirect) = require_superuser_for_user_admin(session_user.as_ref()) {
        return Ok(redirect);
    }
    let users: Vec<rustango::tenancy::auth::User> = rustango::tenancy::auth::User::objects()
        .order_by(&[("username", false)])
        .fetch(tenant.pool())
        .await?;
    let roles: Vec<rustango::tenancy::permissions::Role> =
        rustango::tenancy::permissions::Role::objects()
            .fetch(tenant.pool())
            .await?;
    let user_roles: Vec<rustango::tenancy::permissions::UserRole> =
        rustango::tenancy::permissions::UserRole::objects()
            .fetch(tenant.pool())
            .await?;

    let roles_by_id: std::collections::HashMap<i64, &rustango::tenancy::permissions::Role> = roles
        .iter()
        .filter_map(|r| r.id.get().copied().map(|id| (id, r)))
        .collect();
    let mut roles_by_user: std::collections::HashMap<i64, Vec<String>> =
        std::collections::HashMap::new();
    for ur in &user_roles {
        if let Some(role) = roles_by_id.get(&ur.role_id) {
            roles_by_user
                .entry(ur.user_id)
                .or_default()
                .push(role.name.clone());
        }
    }

    let rows: Vec<serde_json::Value> = users
        .iter()
        .map(|u| {
            let id = u.id.get().copied();
            serde_json::json!({
                "id": id,
                "username": u.username,
                "email": user_email(u),
                "is_superuser": u.is_superuser,
                "active": u.active,
                "created_at": u.created_at,
                "password_changed_at": u.password_changed_at,
                "roles": id.and_then(|i| roles_by_user.get(&i).cloned()).unwrap_or_default(),
            })
        })
        .collect();

    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "users", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("users", &rows);
    ctx.insert("total_users", &users.len());
    ctx.insert("active_users", &users.iter().filter(|u| u.active).count());
    render_with_csrf(&state, &headers, "rcms_admin/users_list.html", &mut ctx)
}

/// Query string for the security log: filters plus pagination. Not a
/// flattened [`crate::security_log::Filter`]: serde's `flatten` reads
/// every query value as a string, which breaks the numeric fields.
#[derive(Debug, Deserialize)]
pub struct SecurityLogQuery {
    #[serde(default)]
    pub event: String,
    #[serde(default)]
    pub username: String,
    #[serde(default)]
    pub page: Option<i64>,
    #[serde(default)]
    pub per_page: Option<usize>,
}

/// The tag colour a security event shows in.
fn security_event_tone(event: &str) -> &'static str {
    use crate::security_log as log;
    match event {
        log::EVENT_LOGIN => "published",
        log::EVENT_LOGIN_FAILED => "scheduled",
        log::EVENT_LOCKOUT => "expired",
        log::EVENT_PERMISSIONS_CHANGED => "archived",
        _ => "draft",
    }
}

/// GET /cms-admin/security-log — sign-ins, lockouts, sign-outs, password
/// resets and permission changes, newest first (#433). Superuser-only,
/// like the user and role screens it audits.
pub async fn security_log_page(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Query(q): Query<SecurityLogQuery>,
) -> Result<Response, AdminError> {
    if let Some(redirect) = require_superuser_for_user_admin(session_user.as_ref()) {
        return Ok(redirect);
    }
    let per_page = crate::admin::pagination::clamp_per_page(q.per_page);
    let filter = crate::security_log::Filter {
        event: q.event.trim().to_owned(),
        username: q.username.trim().to_owned(),
    };
    let total = crate::security_log::count(tenant.pool(), &filter).await?;
    let pagination = crate::admin::pagination::context(
        q.page.unwrap_or(1),
        per_page,
        usize::try_from(total).unwrap_or(0),
    );
    // Clamped into range, so a page past the end shows the last one.
    let page = pagination["page"].as_i64().unwrap_or(1);
    let events = crate::security_log::list(
        tenant.pool(),
        &filter,
        per_page as i64,
        crate::admin::pagination::offset(page, per_page),
    )
    .await?;
    let rows: Vec<serde_json::Value> = events
        .iter()
        .map(|e| {
            serde_json::json!({
                "event": e.event,
                "tone": security_event_tone(&e.event),
                "source": e.source,
                "user_id": e.user_id,
                "username": e.username,
                "reason": e.reason,
                "actor": e.actor,
                "detail": e.detail,
                "ip_address": e.ip_address,
                "user_agent": e.user_agent,
                "created_at": e.created_at.get().copied(),
            })
        })
        .collect();

    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "security-log", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("rows", &rows);
    ctx.insert("total", &total);
    ctx.insert("events", crate::security_log::EVENTS);
    ctx.insert("filter_event", &filter.event);
    ctx.insert("filter_username", &filter.username);
    ctx.insert("pagination", &pagination);
    render_with_csrf(&state, &headers, "rcms_admin/security_log.html", &mut ctx)
}

/// GET /cms-admin/roles — list every role with its permission count
/// and member count.
pub async fn roles_list(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
) -> Result<Response, AdminError> {
    // Identity administration is superuser-only (#672).
    if let Some(redirect) = require_superuser_for_user_admin(session_user.as_ref()) {
        return Ok(redirect);
    }
    let roles: Vec<rustango::tenancy::permissions::Role> =
        rustango::tenancy::permissions::Role::objects()
            .order_by(&[("name", false)])
            .fetch(tenant.pool())
            .await?;
    let perms: Vec<rustango::tenancy::permissions::RolePermission> =
        rustango::tenancy::permissions::RolePermission::objects()
            .fetch(tenant.pool())
            .await?;
    let user_roles: Vec<rustango::tenancy::permissions::UserRole> =
        rustango::tenancy::permissions::UserRole::objects()
            .fetch(tenant.pool())
            .await?;

    let mut perm_counts: std::collections::HashMap<i64, i64> = std::collections::HashMap::new();
    for p in &perms {
        *perm_counts.entry(p.role_id).or_insert(0) += 1;
    }
    let mut member_counts: std::collections::HashMap<i64, i64> = std::collections::HashMap::new();
    for ur in &user_roles {
        *member_counts.entry(ur.role_id).or_insert(0) += 1;
    }

    let rows: Vec<serde_json::Value> = roles
        .iter()
        .map(|r| {
            let id = r.id.get().copied().unwrap_or_default();
            serde_json::json!({
                "id": id,
                "name": r.name,
                "description": r.description,
                "perm_count": perm_counts.get(&id).copied().unwrap_or(0),
                "member_count": member_counts.get(&id).copied().unwrap_or(0),
            })
        })
        .collect();
    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "roles", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("roles", &rows);
    render_with_csrf(&state, &headers, "rcms_admin/roles_list.html", &mut ctx)
}

/// GET `/cms-admin/permissions` — tenant-wide permission
/// overview matrix. The role editor matrix shows one role at a time;
/// this is the read-only birds-eye: every resource (grouped by section)
/// × every role, each cell listing the granted actions. Answers "who
/// can publish pages?" / "what can this role touch?" at a glance.
pub async fn permissions_overview(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
) -> Result<Response, AdminError> {
    let roles: Vec<rustango::tenancy::permissions::Role> =
        rustango::tenancy::permissions::Role::objects()
            .order_by(&[("name", false)])
            .fetch(tenant.pool())
            .await?;
    let perms: Vec<rustango::tenancy::permissions::RolePermission> =
        rustango::tenancy::permissions::RolePermission::objects()
            .fetch(tenant.pool())
            .await?;
    let granted: std::collections::HashSet<(i64, String)> = perms
        .iter()
        .map(|p| (p.role_id, p.codename.clone()))
        .collect();

    let role_headers: Vec<serde_json::Value> = roles
        .iter()
        .map(|r| {
            serde_json::json!({
                "id": r.id.get().copied().unwrap_or_default(),
                "name": r.name,
            })
        })
        .collect();

    // sections → resource rows → one cell per role (granted action codes).
    let sections: Vec<serde_json::Value> = super::resources::grouped()
        .into_iter()
        .map(|(section, resources)| {
            let rows: Vec<serde_json::Value> = resources
                .iter()
                .map(|res| {
                    let cells: Vec<serde_json::Value> = roles
                        .iter()
                        .map(|r| {
                            let rid = r.id.get().copied().unwrap_or_default();
                            let actions = super::resources::granted_actions(res, rid, &granted);
                            serde_json::json!({
                                "actions": actions,
                                "any": !actions.is_empty(),
                            })
                        })
                        .collect();
                    serde_json::json!({
                        "label": res.label,
                        "key": res.key,
                        "cells": cells,
                    })
                })
                .collect();
            serde_json::json!({
                "label": section.label(),
                "rows": rows,
            })
        })
        .collect();

    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "permissions", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("roles", &role_headers);
    ctx.insert("sections", &sections);
    ctx.insert("role_count", &role_headers.len());
    render_with_csrf(
        &state,
        &headers,
        "rcms_admin/permissions_overview.html",
        &mut ctx,
    )
}

/// GET /cms-admin/roles/new — create form.
pub async fn roles_new_form(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
) -> Result<Response, AdminError> {
    // Identity administration is superuser-only (#672).
    if let Some(redirect) = require_superuser_for_user_admin(session_user.as_ref()) {
        return Ok(redirect);
    }
    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "roles", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("mode", "new");
    ctx.insert(
        "role",
        &serde_json::json!({ "id": serde_json::Value::Null, "name": "", "description": "", "permissions": [] }),
    );
    ctx.insert("matrix", &build_permission_matrix(&[]));
    ctx.insert("legacy_codenames", &Vec::<String>::new());
    render_with_csrf(&state, &headers, "rcms_admin/role_form.html", &mut ctx)
}

/// GET /cms-admin/roles/{id}/edit — edit form with permission
/// codenames pre-populated.
pub async fn roles_edit_form(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
) -> Result<Response, AdminError> {
    // Identity administration is superuser-only (#672).
    if let Some(redirect) = require_superuser_for_user_admin(session_user.as_ref()) {
        return Ok(redirect);
    }
    let role = rustango::tenancy::permissions::Role::objects()
        .where_(rustango::tenancy::permissions::Role::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    let perms: Vec<rustango::tenancy::permissions::RolePermission> =
        rustango::tenancy::permissions::RolePermission::objects()
            .where_(rustango::tenancy::permissions::RolePermission::role_id.eq(id))
            .order_by(&[("codename", false)])
            .fetch(tenant.pool())
            .await?;
    let codenames: Vec<String> = perms.iter().map(|p| p.codename.clone()).collect();
    let (_matched, legacy) = split_matrix_codenames(&codenames);
    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "roles", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("mode", "edit");
    ctx.insert(
        "role",
        &serde_json::json!({
            "id": role.id.get().copied(),
            "name": role.name,
            "description": role.description,
            "permissions": codenames,
        }),
    );
    ctx.insert("matrix", &build_permission_matrix(&codenames));
    ctx.insert("legacy_codenames", &legacy);
    render_with_csrf(&state, &headers, "rcms_admin/role_form.html", &mut ctx)
}

/// Build the matrix structure consumed by `role_form.html`.
/// Returns one JSON object per [`super::resources::ResourceSection`]
/// with its rows pre-populated against the granted codename set so
/// the template can render checkboxes pre-checked.
///
/// The matrix is keyed by *resource key* (e.g. `pages`,
/// `library_item:author`) rather than codename so the same template
/// works regardless of which library types / custom admin pages a
/// downstream crate registers.
fn build_permission_matrix(granted: &[String]) -> serde_json::Value {
    use super::resources::{codename, grouped};
    use crate::permissions::Action;

    let granted_set: std::collections::HashSet<&str> = granted.iter().map(String::as_str).collect();

    let mut sections = Vec::new();
    for (section, rows) in grouped() {
        let rows_json: Vec<serde_json::Value> = rows
            .iter()
            .map(|r| {
                let applicable: Vec<&str> = r.actions.iter().map(|a| a.as_str()).collect();
                let granted_actions: Vec<&str> = [
                    Action::Access,
                    Action::AccessAdmin,
                    Action::View,
                    Action::Add,
                    Action::Edit,
                    Action::Publish,
                    Action::Delete,
                ]
                .iter()
                .copied()
                .filter(|action| {
                    r.actions.contains(action)
                        && granted_set.contains(codename(r, *action).as_str())
                })
                .map(|a| a.as_str())
                .collect();
                serde_json::json!({
                    "key": r.key,
                    "label": r.label,
                    "applicable": applicable,
                    "granted": granted_actions,
                })
            })
            .collect();
        sections.push(serde_json::json!({
            "section_label": section.label(),
            "rows": rows_json,
        }));
    }
    serde_json::Value::Array(sections)
}

/// Split the supplied codename list into:
/// 1. The codenames represented in the matrix (`matched`) — these
///    get fully re-computed from the form submission.
/// 2. Everything else (`legacy`) — preserved across saves so
///    out-of-matrix grants (third-party codenames, ad-hoc strings
///    from before the matrix existed) survive a role edit.
fn split_matrix_codenames(granted: &[String]) -> (Vec<String>, Vec<String>) {
    use super::resources::{all_resources, codename};
    use crate::permissions::Action;

    let known: std::collections::HashSet<String> = all_resources()
        .iter()
        .flat_map(|r| {
            r.actions
                .iter()
                .copied()
                .filter(|a| {
                    // Mirror the matrix — only actions in `r.actions`
                    // can be toggled, so only those codenames are
                    // "matrix-owned".
                    matches!(
                        a,
                        Action::View
                            | Action::Add
                            | Action::Edit
                            | Action::Publish
                            | Action::Delete
                            | Action::Access
                            | Action::AccessAdmin
                    )
                })
                .map(|a| codename(r, a))
                .collect::<Vec<_>>()
        })
        .collect();
    let (matched, legacy): (Vec<String>, Vec<String>) =
        granted.iter().cloned().partition(|c| known.contains(c));
    (matched, legacy)
}

/// Parse the matrix POST shape (`perm[<key>][<action>]=1`) into a
/// codename list. Unknown keys / actions are silently dropped — the
/// matrix is authoritative for the resources it represents.
fn parse_matrix_form(form: &std::collections::HashMap<String, String>) -> Vec<String> {
    use super::resources::{all_resources, codename};
    use crate::permissions::Action;

    let resources = all_resources();
    let resource_by_key: std::collections::HashMap<&str, &super::resources::AdminResource> =
        resources.iter().map(|r| (r.key.as_str(), r)).collect();

    let mut out = Vec::new();
    for (raw_key, value) in form {
        // Form serializes `name="perm[<key>][<action>]"`. The bracket
        // shape stays intact in `serde_urlencoded` because it doesn't
        // try to descend nested keys — we parse manually.
        let Some(rest) = raw_key.strip_prefix("perm[") else {
            continue;
        };
        let Some((key, rest)) = rest.split_once("][") else {
            continue;
        };
        let Some(action_str) = rest.strip_suffix(']') else {
            continue;
        };
        // Checkbox semantics: present-and-truthy means granted;
        // absent means not granted. Browsers always send `1` for
        // checked boxes here.
        if value.is_empty() || value == "0" || value.eq_ignore_ascii_case("off") {
            continue;
        }
        let Some(resource) = resource_by_key.get(key) else {
            continue;
        };
        let action = match action_str {
            "view" => Action::View,
            "add" => Action::Add,
            "edit" => Action::Edit,
            "publish" => Action::Publish,
            "delete" => Action::Delete,
            "access" => Action::Access,
            "access_admin" => Action::AccessAdmin,
            _ => continue,
        };
        if !resource.actions.contains(&action) {
            continue;
        }
        out.push(codename(resource, action));
    }
    out.sort();
    out.dedup();
    out
}

/// Shared helper: replace the codename set for a role with the
/// supplied list. Idempotent — added/removed deltas only.
async fn sync_role_codenames(
    pool: &rustango::sql::Pool,
    role_id: i64,
    new_codenames: &[String],
) -> Result<(), AdminError> {
    let existing: Vec<rustango::tenancy::permissions::RolePermission> =
        rustango::tenancy::permissions::RolePermission::objects()
            .where_(rustango::tenancy::permissions::RolePermission::role_id.eq(role_id))
            .fetch(pool)
            .await?;
    let want: std::collections::BTreeSet<&str> = new_codenames.iter().map(String::as_str).collect();
    let have: std::collections::BTreeSet<&str> =
        existing.iter().map(|p| p.codename.as_str()).collect();
    // Drop the codenames that are no longer wanted.
    for p in &existing {
        if !want.contains(p.codename.as_str()) {
            p.clone().delete_pool(pool).await?;
        }
    }
    // Insert the codenames that aren't already there.
    for codename in new_codenames {
        if !have.contains(codename.as_str()) {
            let mut row = rustango::tenancy::permissions::RolePermission {
                id: rustango::sql::Auto::Unset,
                role_id,
                codename: codename.clone(),
            };
            row.insert_pool(pool).await?;
        }
    }
    Ok(())
}

/// POST /cms-admin/roles/new — create a role + sync its codenames.
/// Body shape (matrix): `name=`, `description=`, plus one
/// `perm[<resource>][<action>]=1` per granted cell.
pub async fn roles_create_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    request: crate::security_log::LoginRequest,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Form(form): Form<std::collections::HashMap<String, String>>,
) -> Result<Response, AdminError> {
    // Identity administration is superuser-only (#672).
    if let Some(redirect) = require_superuser_for_user_admin(session_user.as_ref()) {
        return Ok(redirect);
    }
    let name = form.get("name").map(|s| s.trim()).unwrap_or("").to_owned();
    if name.is_empty() {
        return Err(AdminError::Validation("Role name is required.".to_owned()));
    }
    let description = form
        .get("description")
        .map(|s| s.trim())
        .unwrap_or("")
        .to_owned();
    let mut row = rustango::tenancy::permissions::Role {
        id: rustango::sql::Auto::Unset,
        name: name.clone(),
        description,
        data: serde_json::Value::Object(serde_json::Map::new()),
    };
    row.insert_pool(tenant.pool()).await?;
    let role_id = row.id.get().copied().unwrap_or_default();
    let codenames = parse_matrix_form(&form);
    sync_role_codenames(tenant.pool(), role_id, &codenames).await?;
    let detail = crate::security_log::describe_role_change(&name, &[], &codenames)
        .unwrap_or_else(|| format!("role “{name}”: no permissions"));
    record_access_change(&tenant, session_user.as_ref(), &request, None, format!("created {detail}"))
        .await;
    redirect_named_with_message_args(
        "rcms-admin:roles",
        MsgLevel::Success,
        "Role “{name}” created.",
        &[("name", name.as_str())],
        &headers,
    )
}

/// POST /cms-admin/roles/{id}/edit — update name/description +
/// sync the codename set. Out-of-matrix legacy codenames are
/// preserved across saves.
pub async fn roles_edit_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    request: crate::security_log::LoginRequest,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
    Form(form): Form<std::collections::HashMap<String, String>>,
) -> Result<Response, AdminError> {
    // Identity administration is superuser-only (#672).
    if let Some(redirect) = require_superuser_for_user_admin(session_user.as_ref()) {
        return Ok(redirect);
    }
    let mut role = rustango::tenancy::permissions::Role::objects()
        .where_(rustango::tenancy::permissions::Role::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    let name = form.get("name").map(|s| s.trim()).unwrap_or("").to_owned();
    if name.is_empty() {
        return Err(AdminError::Validation("Role name is required.".to_owned()));
    }
    let description = form
        .get("description")
        .map(|s| s.trim())
        .unwrap_or("")
        .to_owned();
    role.name = name.clone();
    role.description = description;
    role.save_pool(tenant.pool()).await?;
    // Carry over codenames that live outside the matrix surface so
    // legacy / third-party codenames aren't silently dropped when
    // an admin saves the matrix-only form.
    let existing: Vec<rustango::tenancy::permissions::RolePermission> =
        rustango::tenancy::permissions::RolePermission::objects()
            .where_(rustango::tenancy::permissions::RolePermission::role_id.eq(id))
            .fetch(tenant.pool())
            .await?;
    let existing_codenames: Vec<String> = existing.into_iter().map(|p| p.codename).collect();
    let (_old_matrix, legacy) = split_matrix_codenames(&existing_codenames);
    let mut codenames = parse_matrix_form(&form);
    codenames.extend(legacy);
    codenames.sort();
    codenames.dedup();
    sync_role_codenames(tenant.pool(), id, &codenames).await?;
    if let Some(detail) =
        crate::security_log::describe_role_change(&name, &existing_codenames, &codenames)
    {
        record_access_change(&tenant, session_user.as_ref(), &request, None, detail).await;
    }
    redirect_named_with_message_args(
        "rcms-admin:roles",
        MsgLevel::Success,
        "Role “{name}” saved.",
        &[("name", name.as_str())],
        &headers,
    )
}

/// POST /cms-admin/roles/{id}/delete — delete the role + its
/// codenames + every UserRole row referencing it.
pub async fn roles_delete_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    request: crate::security_log::LoginRequest,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
) -> Result<Response, AdminError> {
    // Identity administration is superuser-only (#672).
    if let Some(redirect) = require_superuser_for_user_admin(session_user.as_ref()) {
        return Ok(redirect);
    }
    let role = rustango::tenancy::permissions::Role::objects()
        .where_(rustango::tenancy::permissions::Role::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    let name = role.name.clone();
    // Delete dependent rows manually — the schema FK isn't ON DELETE CASCADE.
    for p in rustango::tenancy::permissions::RolePermission::objects()
        .where_(rustango::tenancy::permissions::RolePermission::role_id.eq(id))
        .fetch(tenant.pool())
        .await?
    {
        p.delete_pool(tenant.pool()).await?;
    }
    for ur in rustango::tenancy::permissions::UserRole::objects()
        .where_(rustango::tenancy::permissions::UserRole::role_id.eq(id))
        .fetch(tenant.pool())
        .await?
    {
        ur.delete_pool(tenant.pool()).await?;
    }
    role.delete_pool(tenant.pool()).await?;
    record_access_change(
        &tenant,
        session_user.as_ref(),
        &request,
        None,
        format!("deleted role “{name}”"),
    )
    .await;
    redirect_named_with_message_args(
        "rcms-admin:roles",
        MsgLevel::Success,
        "Role “{name}” deleted.",
        &[("name", name.as_str())],
        &headers,
    )
}

/// GET /cms-admin/users/new — create form.
pub async fn users_new_form(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
) -> Result<Response, AdminError> {
    if let Some(redirect) = require_superuser_for_user_admin(session_user.as_ref()) {
        return Ok(redirect);
    }
    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "users", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("mode", "new");
    ctx.insert(
        "user",
        &serde_json::json!({
            "id": serde_json::Value::Null,
            "username": "",
            "is_superuser": false,
            "active": true,
        }),
    );
    // Roles can be given right away, not only after a second visit.
    let role_choices: Vec<serde_json::Value> = rustango::tenancy::permissions::Role::objects()
        .order_by(&[("name", false)])
        .fetch(tenant.pool())
        .await?
        .iter()
        .map(|r| {
            serde_json::json!({
                "id": r.id.get().copied().unwrap_or_default(),
                "name": r.name,
                "description": r.description,
                "assigned": false,
            })
        })
        .collect();
    ctx.insert("role_choices", &role_choices);
    render_with_csrf(&state, &headers, "rcms_admin/user_form.html", &mut ctx)
}

/// GET /cms-admin/users/{id}/edit — edit form. Password field stays
/// empty by default; leave blank to keep the existing password.
/// Includes a role-checkbox group so editors can grant / revoke
/// role memberships inline.
pub async fn users_edit_form(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
) -> Result<Response, AdminError> {
    if let Some(redirect) = require_superuser_for_user_admin(session_user.as_ref()) {
        return Ok(redirect);
    }
    render_user_edit_page(&state, &tenant, &headers, session_user.as_ref(), id, None).await
}

async fn render_user_edit_page(
    state: &super::AdminState,
    tenant: &Tenant,
    headers: &HeaderMap,
    session_user: Option<&rustango::tenancy::auth::User>,
    id: i64,
    mcp_one_time: Option<serde_json::Value>,
) -> Result<Response, AdminError> {
    let user = rustango::tenancy::auth::User::objects()
        .where_(rustango::tenancy::auth::User::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    let roles: Vec<rustango::tenancy::permissions::Role> =
        rustango::tenancy::permissions::Role::objects()
            .order_by(&[("name", false)])
            .fetch(tenant.pool())
            .await?;
    let memberships: Vec<rustango::tenancy::permissions::UserRole> =
        rustango::tenancy::permissions::UserRole::objects()
            .where_(rustango::tenancy::permissions::UserRole::user_id.eq(id))
            .fetch(tenant.pool())
            .await?;
    let assigned: std::collections::BTreeSet<i64> = memberships.iter().map(|m| m.role_id).collect();

    let role_choices: Vec<serde_json::Value> = roles
        .iter()
        .map(|r| {
            let rid = r.id.get().copied().unwrap_or_default();
            serde_json::json!({
                "id": rid,
                "name": r.name,
                "description": r.description,
                "assigned": assigned.contains(&rid),
            })
        })
        .collect();

    // #13 V2 — per-user timezone stored in `User.data.timezone`.
    // No framework column change required; the existing JSON bag
    // holds the IANA name. Empty / missing = browser-detected
    // fallback (the no-flash JS already handles that case).
    let user_timezone = user
        .data
        .get("timezone")
        .and_then(serde_json::Value::as_str)
        .unwrap_or("")
        .to_owned();
    let user_email = user_email(&user);

    let mut ctx = Context::new();
    add_chrome(&mut ctx, tenant, "users", session_user).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("mode", "edit");
    ctx.insert(
        "user",
        &serde_json::json!({
            "id": user.id.get().copied(),
            "username": user.username,
            "email": user_email,
            "is_superuser": user.is_superuser,
            "active": user.active,
            "timezone": user_timezone,
            "password_changed_at": user.password_changed_at,
            "created_at": user.created_at,
        }),
    );
    ctx.insert("role_choices", &role_choices);
    // #587 — the target user's MCP keys, for admin-managed minting
    // (the "register an agent user, give it a key" flow).
    let (keys, skills) = mcp_keys_ctx(tenant.pool(), id).await;
    ctx.insert("mcp_keys", &keys);
    ctx.insert("mcp_skills", &skills);
    if let Some(one_time) = &mcp_one_time {
        ctx.insert("mcp_one_time", one_time);
    }
    render_with_csrf(state, headers, "rcms_admin/user_form.html", &mut ctx)
}

// ------------------------------------------------------------- MCP keys (#587)

/// Parse a keys-create form: `label` (optional) + repeated `skill`
/// entries (optional scope — empty = the owner's full entitlement).
fn parse_mcp_key_form(pairs: &[(String, String)]) -> (String, Vec<String>) {
    let label = pairs
        .iter()
        .find(|(k, _)| k == "label")
        .map(|(_, v)| v.trim())
        .filter(|v| !v.is_empty())
        .unwrap_or("MCP key")
        .to_owned();
    let skills: Vec<String> = pairs
        .iter()
        .filter(|(k, v)| k == "skill" && !v.trim().is_empty())
        .map(|(_, v)| v.trim().to_owned())
        .collect();
    (label, skills)
}

/// Best-effort audit trail for key lifecycle events — explicit source
/// because the CMS admin doesn't install `audit::with_source`.
async fn audit_mcp_key(
    pool: &rustango::sql::Pool,
    actor_id: i64,
    what: &str,
    detail: serde_json::Value,
) {
    let entry = rustango::audit::PendingEntry {
        entity_table: "rustango_agents",
        entity_pk: detail
            .get("key_id")
            .and_then(serde_json::Value::as_i64)
            .map(|i| i.to_string())
            .unwrap_or_default(),
        operation: rustango::audit::AuditOp::Action,
        source: rustango::audit::AuditSource::User {
            id: actor_id.to_string(),
        },
        changes: serde_json::json!({ "event": what, "detail": detail }),
    };
    if let Err(e) = rustango::audit::emit_one_pool(pool, &entry).await {
        tracing::warn!(error = %e, "mcp key audit emit failed");
    }
}

/// Mint a user key for `owner_id`, mapping scope-validation failures to a
/// user-facing message. Returns the show-once payload for the dialog.
async fn mint_mcp_key(
    pool: &rustango::sql::Pool,
    actor_id: i64,
    owner_id: i64,
    label: &str,
    skills: &[String],
) -> Result<serde_json::Value, String> {
    match rustango::tenancy::create_user_key_pool(pool, owner_id, label, skills).await {
        Ok(issued) => {
            let key_id = issued.agent.id.get().copied().unwrap_or_default();
            audit_mcp_key(
                pool,
                actor_id,
                "created",
                serde_json::json!({ "key_id": key_id, "owner_id": owner_id, "label": label, "scope": skills }),
            )
            .await;
            Ok(serde_json::json!({ "token": issued.token, "label": label }))
        }
        Err(e) => Err(e.to_string()),
    }
}

/// POST /cms-admin/me/mcp-keys — mint a key for the signed-in user.
/// Renders the account page directly with the show-once token (no PRG —
/// the token must never ride a redirect or a flash cookie).
pub async fn account_mcp_key_create(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Form(pairs): Form<Vec<(String, String)>>,
) -> Result<Response, AdminError> {
    let Some(uid) = session_user.as_ref().and_then(|u| u.id.get().copied()) else {
        return Ok(unauthorized_no_session());
    };
    let (label, skills) = parse_mcp_key_form(&pairs);
    match mint_mcp_key(tenant.pool(), uid, uid, &label, &skills).await {
        Ok(one_time) => {
            render_account_page(
                &state,
                &tenant,
                &headers,
                session_user.as_ref(),
                Some(one_time),
            )
            .await
        }
        Err(msg) => redirect_named_with_message_args(
            "rcms-admin:account",
            MsgLevel::Error,
            "Couldn't create the key: {error}",
            &[("error", msg.as_str())],
            &headers,
        ),
    }
}

/// POST /cms-admin/me/mcp-keys/{id}/revoke — revoke one of the signed-in
/// user's keys (ownership-verified in the framework call).
pub async fn account_mcp_key_revoke(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(key_id): Path<i64>,
) -> Result<Response, AdminError> {
    let Some(uid) = session_user.as_ref().and_then(|u| u.id.get().copied()) else {
        return Ok(unauthorized_no_session());
    };
    match rustango::tenancy::revoke_user_key_pool(tenant.pool(), uid, key_id).await {
        Ok(()) => {
            audit_mcp_key(
                tenant.pool(),
                uid,
                "revoked",
                serde_json::json!({ "key_id": key_id, "owner_id": uid }),
            )
            .await;
            redirect_named_with_message_args(
                "rcms-admin:account",
                MsgLevel::Success,
                "MCP key revoked.",
                &[],
                &headers,
            )
        }
        Err(e) => {
            let msg = e.to_string();
            redirect_named_with_message_args(
                "rcms-admin:account",
                MsgLevel::Error,
                "Couldn't revoke the key: {error}",
                &[("error", msg.as_str())],
                &headers,
            )
        }
    }
}

/// POST /cms-admin/users/{id}/mcp-keys — an admin mints a key for another
/// user (the "register an agent user, give it a key" flow). Safe by
/// construction: the key's capabilities are bounded by the TARGET user's
/// entitlement at every token resolution, so this can't escalate beyond
/// what that user could already do.
pub async fn users_mcp_key_create(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(user_id): Path<i64>,
    Form(pairs): Form<Vec<(String, String)>>,
) -> Result<Response, AdminError> {
    // #646 — user-management is superuser-only. Without this the two
    // mcp-key handlers gated on "a session exists" only, so a non-superuser
    // staff account could mint a key bounded by ANY target user's (e.g. a
    // superuser's) entitlement — a privilege escalation the other seven
    // user-admin handlers already block. Mirror them.
    if let Some(redirect) = require_superuser_for_user_admin(session_user.as_ref()) {
        return Ok(redirect);
    }
    let Some(actor_id) = session_user.as_ref().and_then(|u| u.id.get().copied()) else {
        return Ok(unauthorized_no_session());
    };
    let (label, skills) = parse_mcp_key_form(&pairs);
    match mint_mcp_key(tenant.pool(), actor_id, user_id, &label, &skills).await {
        Ok(one_time) => {
            render_user_edit_page(
                &state,
                &tenant,
                &headers,
                session_user.as_ref(),
                user_id,
                Some(one_time),
            )
            .await
        }
        Err(msg) => redirect_named_with_params_and_message_args(
            "rcms-admin:users:edit",
            &[("id", user_id.to_string())],
            MsgLevel::Error,
            "Couldn't create the key: {error}",
            &[("error", msg.as_str())],
            &headers,
        ),
    }
}

/// POST /cms-admin/users/{id}/mcp-keys/{key_id}/revoke — admin revoke of
/// another user's key (ownership pinned to the target user).
pub async fn users_mcp_key_revoke(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path((user_id, key_id)): Path<(i64, i64)>,
) -> Result<Response, AdminError> {
    // #646 — user-management is superuser-only. Without this the two
    // mcp-key handlers gated on "a session exists" only, so a non-superuser
    // staff account could mint a key bounded by ANY target user's (e.g. a
    // superuser's) entitlement — a privilege escalation the other seven
    // user-admin handlers already block. Mirror them.
    if let Some(redirect) = require_superuser_for_user_admin(session_user.as_ref()) {
        return Ok(redirect);
    }
    let Some(actor_id) = session_user.as_ref().and_then(|u| u.id.get().copied()) else {
        return Ok(unauthorized_no_session());
    };
    match rustango::tenancy::revoke_user_key_pool(tenant.pool(), user_id, key_id).await {
        Ok(()) => {
            audit_mcp_key(
                tenant.pool(),
                actor_id,
                "revoked",
                serde_json::json!({ "key_id": key_id, "owner_id": user_id }),
            )
            .await;
            redirect_named_with_params_and_message_args(
                "rcms-admin:users:edit",
                &[("id", user_id.to_string())],
                MsgLevel::Success,
                "MCP key revoked.",
                &[],
                &headers,
            )
        }
        Err(e) => {
            let msg = e.to_string();
            redirect_named_with_params_and_message_args(
                "rcms-admin:users:edit",
                &[("id", user_id.to_string())],
                MsgLevel::Error,
                "Couldn't revoke the key: {error}",
                &[("error", msg.as_str())],
                &headers,
            )
        }
    }
}

/// Form payload for create + edit. Password is optional on edit
/// (empty = "don't change"); required on create (rejected with a
/// 400 if missing).
#[derive(Deserialize)]
pub struct UserForm {
    pub username: String,
    #[serde(default)]
    pub email: Option<String>,
    #[serde(default)]
    pub password: Option<String>,
    #[serde(default)]
    pub is_superuser: Option<String>,
    #[serde(default)]
    pub active: Option<String>,
}

/// Light email validation (presence of a single `@` with text
/// on both sides). Empty is allowed (email is optional). Returns the
/// trimmed value to store, or an error message.
fn validate_optional_email(raw: Option<&str>) -> Result<String, String> {
    let e = raw.map(str::trim).unwrap_or("");
    if e.is_empty() {
        return Ok(String::new());
    }
    let ok = e.split('@').count() == 2
        && e.split('@').all(|p| !p.is_empty())
        && !e.contains(char::is_whitespace);
    if ok {
        Ok(e.to_owned())
    } else {
        Err(format!("“{e}” is not a valid email address."))
    }
}

/// POST /cms-admin/users/new — create a new user. Password is
/// required; we hash via the framework's argon2id helper.
pub async fn users_create_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    request: crate::security_log::LoginRequest,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Form(pairs): Form<Vec<(String, String)>>,
) -> Result<Response, AdminError> {
    if let Some(redirect) = require_superuser_for_user_admin(session_user.as_ref()) {
        return Ok(redirect);
    }
    use rustango::sql::Auto;
    // Pairs, not the typed struct: the roles are a multi-value field
    // (see `users_edit_submit`), granted at creation too.
    let single = |k: &str| pairs.iter().find(|(n, _)| n == k).map(|(_, v)| v.clone());
    let form = UserForm {
        username: single("username").unwrap_or_default(),
        email: single("email"),
        password: single("password"),
        is_superuser: single("is_superuser"),
        active: single("active"),
    };
    let role_ids: Vec<i64> = pairs
        .iter()
        .filter(|(k, _)| k == "role_id")
        .filter_map(|(_, v)| v.parse::<i64>().ok())
        .collect();
    let username = form.username.trim().to_owned();
    let password = form
        .password
        .as_deref()
        .map(str::trim)
        .filter(|s| !s.is_empty())
        .map(str::to_owned);
    if username.is_empty() || password.is_none() {
        return Err(AdminError::Validation(
            "Username and initial password are both required.".to_owned(),
        ));
    }
    let hash = crate::passwords::hash(password.as_deref().unwrap_or_default()).await
        .map_err(|e| AdminError::Validation(format!("password hash failed: {e}")))?;
    let email = validate_optional_email(form.email.as_deref()).map_err(AdminError::Validation)?;
    // The framework's first-class `email` column is the SSO link target (the
    // admin-sso login matches the IdP's verified email against it). Set it here;
    // NULL when no email was given (that account simply can't sign in via SSO).
    let email_col = (!email.is_empty()).then(|| email.clone());

    // #435 — also mirror into the User.data bag (legacy readers: users list +
    // edit form still read `data.email`).
    let mut data = serde_json::Map::new();
    if !email.is_empty() {
        data.insert("email".to_owned(), serde_json::Value::String(email));
    }
    let mut row = rustango::tenancy::auth::User {
        id: Auto::Unset,
        username: username.clone(),
        password_hash: hash,
        email: email_col,
        is_superuser: form.is_superuser.is_some(),
        active: form.active.is_some(),
        created_at: chrono::Utc::now(),
        data: serde_json::Value::Object(data),
        password_changed_at: Some(chrono::Utc::now()),
        sessions_revoked_at: None,
    };
    row.insert_pool(tenant.pool()).await?;
    if let Some(new_id) = row.id.get().copied() {
        sync_user_roles(tenant.pool(), new_id, &role_ids).await?;
    }
    let access = user_access(tenant.pool(), &row).await?;
    record_access_change(
        &tenant,
        session_user.as_ref(),
        &request,
        Some(&row),
        crate::security_log::describe_new_user(&access),
    )
    .await;
    redirect_named_with_message_args(
        "rcms-admin:users",
        MsgLevel::Success,
        "User “{username}” created.",
        &[("username", username.as_str())],
        &headers,
    )
}

/// POST /cms-admin/users/{id}/edit — update flags + optionally
/// rotate the password (empty password field = no rotation) +
/// sync role memberships. Body parsed as `Vec<(String, String)>`
/// instead of the typed struct because role_id is a multi-value
/// form field — checkbox groups in HTML submit one entry per
/// checked box, and serde_urlencoded folds duplicates onto the
/// last write.
pub async fn users_edit_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    request: crate::security_log::LoginRequest,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
    Form(form): Form<Vec<(String, String)>>,
) -> Result<Response, AdminError> {
    if let Some(redirect) = require_superuser_for_user_admin(session_user.as_ref()) {
        return Ok(redirect);
    }
    // Pull each field out of the multi-pair vec. `find` honors the
    // first occurrence — fine for single-value fields.
    let single = |key: &str| -> Option<String> {
        form.iter().find(|(k, _)| k == key).map(|(_, v)| v.clone())
    };
    let username_raw = single("username").unwrap_or_default();
    let username = username_raw.trim().to_owned();
    if username.is_empty() {
        return Err(AdminError::Validation(
            "Username cannot be empty.".to_owned(),
        ));
    }
    let password = single("password");
    let is_superuser = form.iter().any(|(k, _)| k == "is_superuser");
    let active = form.iter().any(|(k, _)| k == "active");
    let timezone_raw = single("timezone").unwrap_or_default();
    let timezone = timezone_raw.trim().to_owned();
    // #435 — optional email, validated + stored in User.data.
    let email =
        validate_optional_email(single("email").as_deref()).map_err(AdminError::Validation)?;
    let role_ids: Vec<i64> = form
        .iter()
        .filter(|(k, _)| k == "role_id")
        .filter_map(|(_, v)| v.parse::<i64>().ok())
        .collect();

    let mut user = rustango::tenancy::auth::User::objects()
        .where_(rustango::tenancy::auth::User::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    let before = user_access(tenant.pool(), &user).await?;
    user.username = username.clone();
    user.is_superuser = is_superuser;
    user.active = active;
    // #13 V2 — persist the chosen IANA timezone (or unset key
    // entirely when blank) on the User.data JSON bag.
    {
        let map = user.data.as_object_mut().ok_or_else(|| {
            AdminError::Validation(
                "User.data is not a JSON object — refusing to overwrite.".to_owned(),
            )
        })?;
        if timezone.is_empty() {
            map.remove("timezone");
        } else {
            map.insert(
                "timezone".to_owned(),
                serde_json::Value::String(timezone.clone()),
            );
        }
        if email.is_empty() {
            map.remove("email");
        } else {
            map.insert("email".to_owned(), serde_json::Value::String(email.clone()));
        }
    }
    // Keep the first-class `email` column (the admin-sso link target) in sync
    // with the edited address — NULL clears the SSO link for this user.
    user.email = (!email.is_empty()).then(|| email.clone());

    let rotated = password.as_deref().map(str::trim).filter(|s| !s.is_empty());
    if let Some(new_pw) = rotated {
        let hash = crate::passwords::hash(new_pw).await
            .map_err(|e| AdminError::Validation(format!("password hash failed: {e}")))?;
        user.password_hash = hash;
        user.password_changed_at = Some(chrono::Utc::now());
    }
    user.save_pool(tenant.pool()).await?;
    sync_user_roles(tenant.pool(), id, &role_ids).await?;
    let after = user_access(tenant.pool(), &user).await?;
    if let Some(detail) = crate::security_log::describe_user_change(&before, &after) {
        record_access_change(&tenant, session_user.as_ref(), &request, Some(&user), detail).await;
    }
    if rotated.is_some() {
        redirect_named_with_message_args(
            "rcms-admin:users",
            MsgLevel::Success,
            "User “{username}” saved (password rotated).",
            &[("username", username.as_str())],
            &headers,
        )
    } else {
        redirect_named_with_message_args(
            "rcms-admin:users",
            MsgLevel::Success,
            "User “{username}” saved.",
            &[("username", username.as_str())],
            &headers,
        )
    }
}

/// Shared helper — replace a user's role-membership set with the
/// supplied list. Idempotent; computes adds + drops via set diff.
async fn sync_user_roles(
    pool: &rustango::sql::Pool,
    user_id: i64,
    new_role_ids: &[i64],
) -> Result<(), AdminError> {
    let existing: Vec<rustango::tenancy::permissions::UserRole> =
        rustango::tenancy::permissions::UserRole::objects()
            .where_(rustango::tenancy::permissions::UserRole::user_id.eq(user_id))
            .fetch(pool)
            .await?;
    let want: std::collections::BTreeSet<i64> = new_role_ids.iter().copied().collect();
    let have: std::collections::BTreeSet<i64> = existing.iter().map(|m| m.role_id).collect();
    for m in &existing {
        if !want.contains(&m.role_id) {
            m.clone().delete_pool(pool).await?;
        }
    }
    for role_id in new_role_ids {
        if !have.contains(role_id) {
            let mut row = rustango::tenancy::permissions::UserRole {
                id: rustango::sql::Auto::Unset,
                user_id,
                role_id: *role_id,
            };
            row.insert_pool(pool).await?;
        }
    }
    Ok(())
}

/// `user`'s flags and role names, as the security log compares them.
async fn user_access(
    pool: &rustango::sql::Pool,
    user: &rustango::tenancy::auth::User,
) -> Result<crate::security_log::Access, AdminError> {
    use rustango::tenancy::permissions::{Role, UserRole};
    let role_ids: Vec<i64> = match user.id.get().copied() {
        Some(uid) => UserRole::objects()
            .where_(UserRole::user_id.eq(uid))
            .fetch(pool)
            .await?
            .into_iter()
            .map(|m| m.role_id)
            .collect(),
        None => Vec::new(),
    };
    let roles = if role_ids.is_empty() {
        Vec::new()
    } else {
        Role::objects()
            .where_(Role::id.is_in(role_ids))
            .fetch(pool)
            .await?
            .into_iter()
            .map(|r| r.name)
            .collect()
    };
    Ok(crate::security_log::Access {
        superuser: user.is_superuser,
        active: user.active,
        roles,
    })
}

/// Log a change to who may do what, made by `actor` to `target` (a
/// user, or `None` for a role).
async fn record_access_change(
    tenant: &Tenant,
    actor: Option<&rustango::tenancy::auth::User>,
    request: &crate::security_log::LoginRequest,
    target: Option<&rustango::tenancy::auth::User>,
    detail: String,
) {
    let mut entry = SecurityEntry::new(
        crate::security_log::EVENT_PERMISSIONS_CHANGED,
        "admin",
        &request.meta,
    )
    .change(actor.map_or("", |u| u.username.as_str()), detail);
    if let Some(user) = target {
        entry = entry.user(user.id.get().copied(), &user.username);
    }
    crate::security_log::record(tenant.pool(), &tenant.org.slug, entry).await;
}

/// POST /cms-admin/users/{id}/deactivate — flip the `active` bit
/// off. Idempotent. Reversed via the edit form's checkbox.
pub async fn users_deactivate_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    request: crate::security_log::LoginRequest,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
) -> Result<Response, AdminError> {
    if let Some(redirect) = require_superuser_for_user_admin(session_user.as_ref()) {
        return Ok(redirect);
    }
    let mut user = rustango::tenancy::auth::User::objects()
        .where_(rustango::tenancy::auth::User::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    let was_active = user.active;
    user.active = false;
    user.save_pool(tenant.pool()).await?;
    if was_active {
        record_access_change(
            &tenant,
            session_user.as_ref(),
            &request,
            Some(&user),
            "active: on → off".to_owned(),
        )
        .await;
    }
    redirect_named_with_message_args(
        "rcms-admin:users",
        MsgLevel::Success,
        "User “{username}” deactivated.",
        &[("username", user.username.as_str())],
        &headers,
    )
}

/// POST /cms-admin/users/bulk — enable or disable the selected users
/// (bulk actions). `user_id` repeats once per checked row;
/// `action` is the clicked button (`enable` | `disable`). Disabling
/// your own account is skipped so an admin can't lock themselves out.
pub async fn users_bulk_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    request: crate::security_log::LoginRequest,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Form(pairs): Form<Vec<(String, String)>>,
) -> Result<Response, AdminError> {
    if let Some(redirect) = require_superuser_for_user_admin(session_user.as_ref()) {
        return Ok(redirect);
    }
    let mut ids: Vec<i64> = Vec::new();
    let mut action = String::new();
    for (k, v) in &pairs {
        match k.as_str() {
            "user_id" => {
                if let Ok(id) = v.parse::<i64>() {
                    ids.push(id);
                }
            }
            "action" => action = v.clone(),
            _ => {}
        }
    }
    let target_active = match action.as_str() {
        "enable" => true,
        "disable" => false,
        _ => {
            return redirect_named_with_message(
                "rcms-admin:users",
                MsgLevel::Error,
                "Unknown bulk action.",
                &headers,
            );
        }
    };
    if ids.is_empty() {
        return redirect_named_with_message(
            "rcms-admin:users",
            MsgLevel::Info,
            "No users selected.",
            &headers,
        );
    }
    let me = session_user.as_ref().and_then(|u| u.id.get().copied());
    let mut changed = 0usize;
    let mut skipped_self = false;
    for id in ids {
        // Never disable the acting admin — that's a self-lockout.
        if !target_active && Some(id) == me {
            skipped_self = true;
            continue;
        }
        if let Some(mut user) = rustango::tenancy::auth::User::objects()
            .where_(rustango::tenancy::auth::User::id.eq(id))
            .first(tenant.pool())
            .await?
        {
            if user.active != target_active {
                user.active = target_active;
                user.save_pool(tenant.pool()).await?;
                changed += 1;
                let detail = if target_active { "active: off → on" } else { "active: on → off" };
                record_access_change(
                    &tenant,
                    session_user.as_ref(),
                    &request,
                    Some(&user),
                    detail.to_owned(),
                )
                .await;
            }
        }
    }
    let verb = if target_active { "Enabled" } else { "Disabled" };
    let mut msg = format!("{verb} {changed} user(s).");
    if skipped_self {
        msg.push_str(" (Skipped your own account.)");
    }
    redirect_named_with_message("rcms-admin:users", MsgLevel::Success, &msg, &headers)
}

// =====================================================================
// Settings tab
// =====================================================================

pub async fn settings_page(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
) -> Result<Response, AdminError> {
    let locales: Vec<Locale> = Locale::objects()
        .order_by(&[("sort_order", false), ("code", false)])
        .fetch(tenant.pool())
        .await?;
    let media_count: Vec<Media> = Media::objects().fetch(tenant.pool()).await?;
    // #58 — themes available to the picker. We surface every row in
    // `cms_theme` plus its 4 marquee colors for a swatch preview.
    let themes: Vec<crate::theme::Theme> = crate::theme::Theme::objects()
        .order_by(&[("name", false)])
        .fetch(tenant.pool())
        .await?;
    let theme_rows: Vec<serde_json::Value> = themes
        .iter()
        .map(|t| {
            serde_json::json!({
                "id": t.id.get().copied(),
                "name": t.name,
                "slug": t.slug,
                "is_admin_default": t.is_admin_default,
                "is_default": t.is_default,
                "color_bg": t.color_bg,
                "color_surface": t.color_surface,
                "color_text_primary": t.color_text_primary,
                "color_link": t.color_link,
            })
        })
        .collect();
    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "settings", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("locales", &locales);
    ctx.insert("media_count", &media_count.len());
    ctx.insert("org_slug", &tenant.org.slug);
    ctx.insert("org_display_name", &tenant.org.display_name);
    ctx.insert("themes", &theme_rows);

    // #199 — surface the current branding asset filenames + URLs
    // so the Branding form can render "Current logo: <filename>"
    // hints next to the upload inputs.
    if let Ok(Some(row)) = crate::site_setting::get(tenant.pool(), "branding").await {
        let logo_key = row
            .value_json
            .get("logo_storage_key")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_owned();
        let favicon_key = row
            .value_json
            .get("favicon_storage_key")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_owned();
        ctx.insert("branding_logo_filename", &logo_key);
        ctx.insert("branding_favicon_filename", &favicon_key);
        ctx.insert(
            "branding_site_name",
            row.value_json.get("site_name").and_then(|v| v.as_str()).unwrap_or(""),
        );
    }

    render_with_csrf(&state, &headers, "rcms_admin/settings.html", &mut ctx)
}

/// POST /cms-admin/settings/branding — multipart upload of logo
/// and/or favicon. Files land in
/// storage key `<tenant>/branding/<name>` and the storage keys are
/// recorded in `cms_site_setting` under scope `branding`.
pub async fn settings_branding_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    mut multipart: axum::extract::Multipart,
) -> Result<Response, AdminError> {
    let mut logo_filename: Option<String> = None;
    let mut favicon_filename: Option<String> = None;
    let mut site_name: Option<String> = None;

    while let Some(field) = multipart
        .next_field()
        .await
        .map_err(|e| AdminError::Upload(format!("multipart parse: {e}")))?
    {
        let field_name = field.name().unwrap_or("").to_owned();
        let original = field.file_name().map(|s| s.to_owned());
        let bytes = field
            .bytes()
            .await
            .map_err(|e| AdminError::Upload(format!("read field {field_name}: {e}")))?;
        // A text field; empty clears it (back to the tenant's name).
        if field_name == "site_name" {
            site_name = Some(String::from_utf8_lossy(&bytes).trim().chars().take(120).collect());
            continue;
        }
        if bytes.is_empty() {
            continue;
        }
        let (slot, default_ext) = match field_name.as_str() {
            "logo" => (&mut logo_filename, "png"),
            "favicon" => (&mut favicon_filename, "ico"),
            _ => continue,
        };
        let safe_ext = original
            .as_deref()
            .and_then(|n| n.rsplit_once('.'))
            .map(|(_, ext)| ext)
            .filter(|ext| ext.len() <= 8 && ext.chars().all(|c| c.is_ascii_alphanumeric()))
            .map(str::to_ascii_lowercase)
            .unwrap_or_else(|| default_ext.to_owned());
        let storage_name = format!("{field_name}.{safe_ext}");
        crate::media_storage::disk_for(&tenant.org.slug)
            .save(
                &crate::media_storage::key(&tenant.org.slug, &format!("branding/{storage_name}")),
                &bytes,
            )
            .await
            .map_err(|e| AdminError::Upload(format!("write {field_name}: {e}")))?;
        *slot = Some(storage_name);
    }

    // Merge into the existing row so an upload of just the favicon
    // doesn't clobber a previously-uploaded logo.
    let existing = crate::site_setting::get(tenant.pool(), "branding")
        .await
        .unwrap_or_default()
        .map(|r| r.value_json)
        .unwrap_or_else(|| serde_json::json!({}));
    let mut value = existing.as_object().cloned().unwrap_or_default();
    if let Some(name) = logo_filename {
        value.insert("logo_storage_key".to_owned(), serde_json::json!(name));
    }
    if let Some(name) = favicon_filename {
        value.insert("favicon_storage_key".to_owned(), serde_json::json!(name));
    }
    match site_name {
        Some(n) if n.is_empty() => {
            value.remove("site_name");
        }
        Some(n) => {
            value.insert("site_name".to_owned(), serde_json::json!(n));
        }
        None => {}
    }
    let _ =
        crate::site_setting::upsert(tenant.pool(), "branding", serde_json::Value::Object(value))
            .await?;

    redirect_named_with_message(
        "rcms-admin:settings",
        MsgLevel::Success,
        "Branding saved. Reload to see the new logo / favicon.",
        &headers,
    )
}

/// GET /__cms-branding/logo — streams the per-tenant logo bytes.
/// Anonymous-OK: this is admin chrome but the favicon especially
/// needs to be reachable before login.
pub async fn settings_branding_serve_logo(headers: HeaderMap, tenant: Tenant) -> Response {
    serve_branding_asset(&headers, &tenant, "logo_storage_key").await
}

/// GET /__cms-branding/favicon — streams the per-tenant favicon.
pub async fn settings_branding_serve_favicon(headers: HeaderMap, tenant: Tenant) -> Response {
    serve_branding_asset(&headers, &tenant, "favicon_storage_key").await
}

async fn serve_branding_asset(headers: &HeaderMap, tenant: &Tenant, slot: &str) -> Response {
    let setting = match crate::site_setting::get(tenant.pool(), "branding").await {
        Ok(Some(s)) => s,
        _ => {
            return (axum::http::StatusCode::NOT_FOUND, "no branding set").into_response();
        }
    };
    let storage_name = match setting.value_json.get(slot).and_then(|v| v.as_str()) {
        Some(s) if !s.is_empty() => s.to_owned(),
        _ => {
            return (axum::http::StatusCode::NOT_FOUND, "asset not set").into_response();
        }
    };
    // Reject path traversal — storage_name comes from a setting we
    // wrote ourselves, but never trust round-tripped JSON.
    if storage_name.contains('/') || storage_name.contains("..") {
        return (axum::http::StatusCode::BAD_REQUEST, "invalid storage name").into_response();
    }
    let bytes = match crate::media_storage::disk_for(&tenant.org.slug)
        .load(&crate::media_storage::key(
            &tenant.org.slug,
            &format!("branding/{storage_name}"),
        ))
        .await
    {
        Ok(b) => b,
        Err(_) => {
            return (axum::http::StatusCode::NOT_FOUND, "branding bytes missing").into_response();
        }
    };
    let mime = mime_guess::from_path(&storage_name)
        .first_or_octet_stream()
        .essence_str()
        .to_owned();
    // #199 fix — the URL is stable (`/__cms-branding/logo|favicon`) and
    // the storage key can stay the same across a re-upload, so the old
    // `public, max-age=3600` (no validator) left browsers showing the
    // OLD logo/favicon for up to an hour after a change (the reported
    // "upload didn't work"). Revalidate via a content ETag instead.
    revalidated_asset(headers, mime, bytes)
}

/// Response for a small, mutable asset served from a STABLE URL
/// (branding logo/favicon, user avatar): a content-hash ETag plus
/// `Cache-Control: no-cache` (cache, but always revalidate) so a
/// re-upload is picked up immediately — no stale-for-an-hour window —
/// while unchanged bytes still `304` cheaply (`If-None-Match` honored).
fn revalidated_asset(headers: &HeaderMap, mime: String, bytes: Vec<u8>) -> Response {
    use sha2::{Digest, Sha256};
    let digest = Sha256::digest(&bytes);
    let mut etag = String::with_capacity(34);
    etag.push('"');
    for b in digest.iter().take(16) {
        use std::fmt::Write as _;
        let _ = write!(etag, "{b:02x}");
    }
    etag.push('"');
    let matches_etag = headers
        .get(axum::http::header::IF_NONE_MATCH)
        .and_then(|v| v.to_str().ok())
        .is_some_and(|inm| inm.split(',').any(|t| t.trim() == etag));
    if matches_etag {
        return (
            axum::http::StatusCode::NOT_MODIFIED,
            [
                (axum::http::header::ETAG, etag),
                (axum::http::header::CACHE_CONTROL, "no-cache".to_owned()),
            ],
        )
            .into_response();
    }
    (
        [
            (axum::http::header::CONTENT_TYPE, mime),
            (axum::http::header::CACHE_CONTROL, "no-cache".to_owned()),
            (axum::http::header::ETAG, etag),
        ],
        bytes,
    )
        .into_response()
}

/// POST /cms-admin/settings/theme — flip either `is_admin_default`
/// or `is_default` to a new theme. Form fields:
///
/// - `surface`: either `admin` or `public` — picks which flag to toggle.
/// - `theme_id`: the row to set the flag on.
///
/// The flag is exclusive per surface (one row at a time carries it),
/// so we explicitly clear it from every other row in the same pass.
#[derive(Debug, Deserialize)]
pub struct ThemeSwitchForm {
    pub surface: String,
    pub theme_id: i64,
}

pub async fn settings_theme_switch(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    Form(form): Form<ThemeSwitchForm>,
) -> Result<Response, AdminError> {
    // #174 — only `surface=admin` is reachable through the UI now.
    // The `public` value is still accepted at the API level so host
    // crates that drive public-page theming externally keep working,
    // but no editor surface posts it anymore.
    if form.surface != "admin" && form.surface != "public" {
        return Err(AdminError::Validation(format!(
            "Unknown surface `{}` — expected `admin` or `public`.",
            form.surface
        )));
    }
    let target = crate::theme::Theme::objects()
        .where_(crate::theme::Theme::id.eq(form.theme_id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(form.theme_id))?;

    // Clear the exclusive flag everywhere else, then set it on the
    // picked row. Sequential ORM writes — atomicity isn't critical
    // here (worst case a transient request sees zero rows flagged
    // and falls through to the static default).
    let all_themes: Vec<crate::theme::Theme> =
        crate::theme::Theme::objects().fetch(tenant.pool()).await?;
    for mut t in all_themes {
        let target_id = target.id.get().copied().unwrap_or_default();
        let this_id = t.id.get().copied().unwrap_or_default();
        if form.surface == "admin" {
            t.is_admin_default = this_id == target_id;
        } else {
            t.is_default = this_id == target_id;
        }
        t.save_pool(tenant.pool()).await?;
    }
    // #274 — when the picker submits via fetch() (XHR), return JSON with
    // the freshly-rendered CSS + identifying bits so the client can swap
    // the `<style data-rcms-theme>` block in place. Detection: either an
    // explicit `Accept: application/json` (preferred — modern), or the
    // legacy `X-Requested-With: XMLHttpRequest` (kept so older host
    // crates that wrap the picker can still light it up). Plain form
    // submissions keep the redirect-with-flash behavior so the picker
    // degrades gracefully with JS off.
    if wants_json_response(&headers) && form.surface == "admin" {
        let target_id = target.id.get().copied().unwrap_or_default();
        let brand_colors: Vec<crate::theme::BrandColor> = crate::theme::BrandColor::objects()
            .where_(crate::theme::BrandColor::theme_id.eq(target_id))
            .order_by(&[("sort_order", false)])
            .fetch(tenant.pool())
            .await
            .unwrap_or_default();
        let css = crate::theme::emit_css(&target, &brand_colors);
        return Ok(Json(serde_json::json!({
            "ok": true,
            "id": target_id,
            "slug": target.slug,
            "name": target.name,
            "default_mode": target.default_mode,
            "css": css,
            "message": format!("Admin theme switched to “{}”.", target.name),
        }))
        .into_response());
    }
    // #174 — admin picker redirects back to Preferences (its new
    // home); the legacy `public` surface still redirects to Settings
    // so host integrations that POST manually keep their UX.
    let redirect_target = if form.surface == "admin" {
        "rcms-admin:account"
    } else {
        "rcms-admin:settings"
    };
    let theme_flash = if form.surface == "admin" {
        "Admin theme switched to “{name}”."
    } else {
        "Public theme switched to “{name}”."
    };
    redirect_named_with_message_args(
        redirect_target,
        MsgLevel::Success,
        theme_flash,
        &[("name", target.name.as_str())],
        &headers,
    )
}

/// True when the request prefers a JSON response — either set
/// `Accept: application/json` (modern fetch with `Accept` header) or
/// the legacy `X-Requested-With: XMLHttpRequest` marker. Used by the
/// theme picker so it can opt into a live-swap JSON payload
/// without losing the no-JS redirect fallback.
fn wants_json_response(headers: &HeaderMap) -> bool {
    let accept = headers
        .get(header::ACCEPT)
        .and_then(|v| v.to_str().ok())
        .unwrap_or("");
    if accept.contains("application/json") {
        return true;
    }
    headers
        .get("x-requested-with")
        .and_then(|v| v.to_str().ok())
        .is_some_and(|v| v.eq_ignore_ascii_case("XMLHttpRequest"))
}

// =====================================================================
// Redirects tab — editor-managed 301/302 maps. Backed by
// crate::redirect::Redirect; the public router consults this table
// when a slug lookup returns None (see crate::router).
// =====================================================================

#[derive(Debug, Deserialize)]
pub struct RedirectForm {
    pub from_path: String,
    pub to_path: String,
    /// Checkbox — present when ticked, absent when not. `Some(_)` →
    /// 301 Moved Permanently. Default checked in the form template.
    #[serde(default)]
    pub is_permanent: Option<String>,
    #[serde(default)]
    pub note: Option<String>,
    /// Checkbox: serve even when a published page exists at
    /// `from_path`.
    #[serde(default)]
    pub overrides_live: Option<String>,
    /// Chooser hidden inputs; empty string when cleared.
    #[serde(default)]
    pub from_page_id: Option<String>,
    #[serde(default)]
    pub to_page_id: Option<String>,
}

impl RedirectForm {
    fn parsed_permanent(&self) -> bool {
        self.is_permanent.is_some()
    }
    fn parsed_overrides_live(&self) -> bool {
        self.overrides_live.is_some()
    }
    fn parsed_page_id(raw: &Option<String>) -> Option<i64> {
        raw.as_deref()
            .and_then(|s| s.trim().parse::<i64>().ok())
            .filter(|id| *id > 0)
    }
    fn parsed_from_page_id(&self) -> Option<i64> {
        Self::parsed_page_id(&self.from_page_id)
    }
    fn parsed_to_page_id(&self) -> Option<i64> {
        Self::parsed_page_id(&self.to_page_id)
    }
}

/// Resolve the effective from/to paths for a redirect form:
/// a picked source page snapshots its CURRENT `url_path` into
/// `from_path`; a picked destination page snapshots into `to_path`
/// (display/fallback — the serve path re-resolves it live). Returns
/// `(from_path, to_path)`; typed values pass through when no page is
/// picked.
async fn redirect_form_paths(
    pool: &rustango::sql::Pool,
    form: &RedirectForm,
) -> Result<(String, String), AdminError> {
    let mut from = form.from_path.trim().to_owned();
    let mut to = form.to_path.trim().to_owned();
    if let Some(pid) = form.parsed_from_page_id() {
        if let Some(p) = Page::objects().where_(Page::id.eq(pid)).first(pool).await? {
            from = p.url_path;
        }
    }
    if let Some(pid) = form.parsed_to_page_id() {
        if let Some(p) = Page::objects().where_(Page::id.eq(pid)).first(pool).await? {
            to = p.url_path;
        }
    }
    Ok((from, to))
}

/// Save-time shadow check: a non-override rule whose
/// `from_path` matches a PUBLISHED page will never serve (live pages
/// win). Returns `true` when shadowed so the submit handlers can
/// flash a warning instead of a plain success.
async fn redirect_is_shadowed(tenant: &Tenant, from_path: &str, overrides_live: bool) -> bool {
    if overrides_live || crate::redirect::is_wildcard(from_path) {
        // Wildcards are meant to span subtrees (which may include live
        // pages); "shadow" only applies to exact non-override rules.
        return false;
    }
    matches!(
        crate::resolver::resolve_path(tenant, from_path).await,
        Ok(Some(_))
    )
}

/// Reject a `from_path` with more than one `*`; a single glob
/// (typically `path/*` or `*/path`) is the supported shape. Also reject a
/// rule whose destination matches its own from-path.
fn redirect_pattern_error(from_path: &str, to_path: &str) -> Option<&'static str> {
    if from_path.matches('*').count() > 1 {
        Some("The from-path may contain at most one “*” wildcard (e.g. /old/* or */feed).")
    } else if crate::redirect::redirect_loops(from_path, to_path) {
        Some("That redirect points back at its own from-path, so visitors would loop.")
    } else {
        None
    }
}

/// Query for the redirect list: free-text search, status filter chip,
/// sortable column + direction, and page number.
#[derive(Debug, Deserialize, Default)]
pub struct RedirectListQuery {
    #[serde(default)]
    pub q: Option<String>,
    /// `all` | `active` | `disabled` | `stale`.
    #[serde(default)]
    pub filter: Option<String>,
    /// `from` | `last_used` | `created` | `hits` | `staleness`.
    #[serde(default)]
    pub sort: Option<String>,
    /// `asc` | `desc`.
    #[serde(default)]
    pub dir: Option<String>,
    #[serde(default)]
    pub page: Option<i64>,
}

const REDIRECTS_PER_PAGE: usize = 50;

pub async fn redirect_list(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Query(q): Query<RedirectListQuery>,
) -> Result<Response, AdminError> {
    // The redirect table is small (editor-managed), so search / sort /
    // paginate happen in-memory over one fetch — same result as
    // SQL LIMIT/OFFSET without threading dialect-specific ordering.
    let mut all: Vec<crate::redirect::Redirect> = crate::redirect::Redirect::objects()
        .fetch(tenant.pool())
        .await?;
    let now = chrono::Utc::now();

    let needle = q.q.as_deref().unwrap_or("").trim().to_lowercase();
    if !needle.is_empty() {
        all.retain(|r| {
            r.from_path.to_lowercase().contains(&needle)
                || r.to_path.to_lowercase().contains(&needle)
                || r.note.to_lowercase().contains(&needle)
        });
    }

    // Chip counts reflect the search-filtered set (before the status filter).
    let count_all = all.len();
    let count_active = all.iter().filter(|r| r.is_active).count();
    let count_disabled = all.iter().filter(|r| !r.is_active).count();
    let count_stale = all
        .iter()
        .filter(|r| matches!(crate::redirect::staleness(r, now), "stale" | "unused"))
        .count();

    let filter = q.filter.as_deref().unwrap_or("all");
    all.retain(|r| match filter {
        "active" => r.is_active,
        "disabled" => !r.is_active,
        "stale" => matches!(crate::redirect::staleness(r, now), "stale" | "unused"),
        _ => true,
    });

    let sort = q.sort.as_deref().unwrap_or("from");
    let desc = q.dir.as_deref() == Some("desc");
    all.sort_by(|a, b| {
        let o = match sort {
            "last_used" => a.last_hit_at.cmp(&b.last_hit_at),
            "created" => a.created_at.get().cmp(&b.created_at.get()),
            "hits" => a.hit_count.cmp(&b.hit_count),
            "staleness" => {
                let rank = |r: &crate::redirect::Redirect| match crate::redirect::staleness(r, now)
                {
                    "disabled" => 0u8,
                    "unused" => 1,
                    "stale" => 2,
                    _ => 3,
                };
                rank(a).cmp(&rank(b))
            }
            _ => a.from_path.cmp(&b.from_path),
        };
        if desc {
            o.reverse()
        } else {
            o
        }
    });

    let total = all.len();
    let total_pages = total.div_ceil(REDIRECTS_PER_PAGE).max(1);
    let page = q.page.unwrap_or(1).clamp(1, total_pages as i64) as usize;
    let start = (page - 1) * REDIRECTS_PER_PAGE;
    let items: Vec<crate::redirect::Redirect> = all
        .into_iter()
        .skip(start)
        .take(REDIRECTS_PER_PAGE)
        .collect();

    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "redirects", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("items", &items);
    ctx.insert("q", &needle);
    ctx.insert("filter", filter);
    ctx.insert("sort", sort);
    ctx.insert("dir", if desc { "desc" } else { "asc" });
    ctx.insert("current_page", &page);
    ctx.insert("total_pages", &total_pages);
    ctx.insert("total", &total);
    ctx.insert("count_all", &count_all);
    ctx.insert("count_active", &count_active);
    ctx.insert("count_disabled", &count_disabled);
    ctx.insert("count_stale", &count_stale);
    render_with_csrf(&state, &headers, "rcms_admin/redirect_list.html", &mut ctx)
}

pub async fn redirect_new_form(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
) -> Result<Response, AdminError> {
    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "redirects", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("mode", "new");
    ctx.insert(
        "redirect",
        &serde_json::json!({
            "from_path": "",
            "to_path": "",
            "is_permanent": true,
            "note": "",
            "hit_count": 0,
            "overrides_live": false,
            "from_page_id": null,
            "to_page_id": null,
        }),
    );
    render_with_csrf(&state, &headers, "rcms_admin/redirect_form.html", &mut ctx)
}

pub async fn redirect_new_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Form(form): Form<RedirectForm>,
) -> Result<Response, AdminError> {
    use rustango::sql::Auto;
    let (from, to) = redirect_form_paths(tenant.pool(), &form).await?;
    if from.is_empty() || to.is_empty() {
        return redirect_named_with_message(
            "rcms-admin:redirects:list",
            MsgLevel::Error,
            "From-path and to-path are both required.",
            &headers,
        );
    }
    if let Some(msg) = redirect_pattern_error(&from, &to) {
        return redirect_named_with_message(
            "rcms-admin:redirects:list",
            MsgLevel::Error,
            msg,
            &headers,
        );
    }
    let overrides_live = form.parsed_overrides_live();
    let is_permanent = form.parsed_permanent();
    let from_page_id = form.parsed_from_page_id();
    let to_page_id = form.parsed_to_page_id();
    let mut row = crate::redirect::Redirect {
        id: Auto::Unset,
        from_path: from,
        to_path: to,
        is_permanent,
        note: form.note.unwrap_or_default().trim().to_owned(),
        hit_count: 0,
        overrides_live,
        from_page_id,
        to_page_id,
        is_active: true,
        disabled_reason: String::new(),
        last_hit_at: None,
        created_at: Auto::Unset,
        updated_at: Auto::Unset,
    };
    row.insert_pool(tenant.pool()).await?;
    redirect_save_response(&tenant, &row, overrides_live, true, &headers).await
}

/// Shared post-save flash for redirect create/edit: a strong warning
/// for a wildcard override (it can shadow many live pages), a warning
/// for an exact non-override rule shadowed by a live page, else
/// success. `is_new` picks the "Added…" vs "Saved…" wording so every
/// message stays a single fully-localizable sentence.
async fn redirect_save_response(
    tenant: &Tenant,
    row: &crate::redirect::Redirect,
    overrides_live: bool,
    is_new: bool,
    headers: &HeaderMap,
) -> Result<Response, AdminError> {
    let args = &[
        ("from", row.from_path.as_str()),
        ("to", row.to_path.as_str()),
    ];
    if overrides_live && crate::redirect::is_wildcard(&row.from_path) {
        let key = if is_new {
            "Added redirect {from} → {to} — heads up: a wildcard override can shadow many live pages under this pattern. Double-check the pattern."
        } else {
            "Saved redirect {from} → {to} — heads up: a wildcard override can shadow many live pages under this pattern. Double-check the pattern."
        };
        return redirect_named_with_message_args(
            "rcms-admin:redirects:list",
            MsgLevel::Warning,
            key,
            args,
            headers,
        );
    }
    if redirect_is_shadowed(tenant, &row.from_path, overrides_live).await {
        let key = if is_new {
            "Added redirect {from} → {to} — but a published page exists at {from}, so it won't serve until that page is unpublished (or tick “Apply even when a published page exists”)."
        } else {
            "Saved redirect {from} → {to} — but a published page exists at {from}, so it won't serve until that page is unpublished (or tick “Apply even when a published page exists”)."
        };
        return redirect_named_with_message_args(
            "rcms-admin:redirects:list",
            MsgLevel::Warning,
            key,
            args,
            headers,
        );
    }
    let key = if is_new {
        "Added redirect {from} → {to}."
    } else {
        "Saved redirect {from} → {to}."
    };
    redirect_named_with_message_args(
        "rcms-admin:redirects:list",
        MsgLevel::Success,
        key,
        args,
        headers,
    )
}

// ----- #186 — snippet CSV import (bulk migration) ---

/// One row from the snippet CSV / TSV. Mirrors the cms_snippet shape:
/// required slug + title, optional body_markdown + folder_path + a
/// flat `data` map (any extra column → data.<column_name>).
#[derive(Debug)]
struct SnippetImportRow {
    slug: String,
    title: String,
    body_markdown: String,
    folder_path: String,
    data: serde_json::Map<String, serde_json::Value>,
}

fn parse_snippet_csv(text: &str) -> Vec<SnippetImportRow> {
    // Header is required (we need to know what column is what).
    // Auto-detect TSV when tabs are present.
    let delimiter = if text.contains('\t') { '\t' } else { ',' };
    let mut lines = text.lines();
    let header = match lines.next() {
        Some(h) => h,
        None => return Vec::new(),
    };
    let cols: Vec<String> = header
        .split(delimiter)
        .map(|s| s.trim().to_ascii_lowercase())
        .collect();
    // Need at least slug + title.
    let slug_idx = cols.iter().position(|c| c == "slug" || c == "key");
    let title_idx = cols.iter().position(|c| c == "title" || c == "name");
    let (Some(slug_idx), Some(title_idx)) = (slug_idx, title_idx) else {
        return Vec::new();
    };
    let body_idx = cols
        .iter()
        .position(|c| c == "body_markdown" || c == "body" || c == "content");
    let folder_idx = cols
        .iter()
        .position(|c| c == "folder_path" || c == "folder" || c == "path");
    let mut out = Vec::new();
    for raw in lines {
        let trimmed = raw.trim();
        if trimmed.is_empty() {
            continue;
        }
        let parts: Vec<&str> = trimmed.split(delimiter).map(str::trim).collect();
        let slug = parts.get(slug_idx).copied().unwrap_or("");
        let title = parts.get(title_idx).copied().unwrap_or("");
        if slug.is_empty() || title.is_empty() {
            continue;
        }
        let body_markdown = body_idx
            .and_then(|i| parts.get(i))
            .copied()
            .unwrap_or("")
            .to_owned();
        let folder_path = folder_idx
            .and_then(|i| parts.get(i))
            .copied()
            .unwrap_or("")
            .to_owned();
        // Every other column lands in `data`.
        let mut data = serde_json::Map::new();
        for (i, col) in cols.iter().enumerate() {
            if Some(i) == Some(slug_idx)
                || Some(i) == Some(title_idx)
                || Some(i) == body_idx
                || Some(i) == folder_idx
            {
                continue;
            }
            if col.is_empty() {
                continue;
            }
            let v = parts.get(i).copied().unwrap_or("");
            if !v.is_empty() {
                data.insert(col.clone(), serde_json::Value::String(v.to_owned()));
            }
        }
        out.push(SnippetImportRow {
            slug: slug.to_owned(),
            title: title.to_owned(),
            body_markdown,
            folder_path: crate::snippet::normalize_folder(&folder_path),
            data,
        });
    }
    out
}

/// GET /cms-admin/library/{type_name}/import — show upload form.
pub async fn snippet_import_form(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(type_name): Path<String>,
) -> Result<Response, AdminError> {
    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "library", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("type_name", &type_name);
    render_with_csrf(&state, &headers, "rcms_admin/snippet_import.html", &mut ctx)
}

/// POST /cms-admin/library/{type_name}/import — bulk import snippets
/// for one library type from CSV / TSV.
pub async fn snippet_import_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Path(type_name): Path<String>,
    mut multipart: axum::extract::Multipart,
) -> Result<Response, AdminError> {
    use rustango::sql::Auto;
    let mut bytes: Vec<u8> = Vec::new();
    let mut filename = String::new();
    while let Some(field) = multipart
        .next_field()
        .await
        .map_err(|e| AdminError::Upload(format!("multipart parse: {e}")))?
    {
        if field.name().unwrap_or("") == "file" {
            if let Some(name) = field.file_name() {
                filename = name.to_owned();
            }
            bytes = field
                .bytes()
                .await
                .map_err(|e| AdminError::Upload(format!("read bytes: {e}")))?
                .to_vec();
        } else {
            let _ = field.bytes().await;
        }
    }
    if bytes.is_empty() {
        return redirect_named_with_message(
            "rcms-admin:library:list",
            MsgLevel::Error,
            "No file uploaded.",
            &headers,
        );
    }
    let text = String::from_utf8_lossy(&bytes);
    let rows = parse_snippet_csv(&text);
    let total = rows.len();
    if total == 0 {
        return redirect_named_with_message(
            "rcms-admin:library:list",
            MsgLevel::Error,
            "Imported 0 snippets — CSV must have a header row with `slug` + `title` columns.",
            &headers,
        );
    }
    let mut inserted = 0usize;
    let mut skipped = 0usize;
    let _ = filename; // captured for the note below
    for row in rows {
        let mut snippet = crate::snippet::Snippet {
            id: Auto::Unset,
            type_name: type_name.clone(),
            folder_path: row.folder_path,
            slug: row.slug,
            title: row.title,
            body_markdown: row.body_markdown,
            data: serde_json::Value::Object(row.data),
            created_at: Auto::Unset,
            updated_at: Auto::Unset,
        };
        match snippet.insert_pool(tenant.pool()).await {
            Ok(_) => inserted += 1,
            Err(_) => skipped += 1,
        }
    }
    let inserted_str = inserted.to_string();
    let total_str = total.to_string();
    let skipped_str = skipped.to_string();
    redirect_named_with_message_args(
        "rcms-admin:library:list",
        MsgLevel::Success,
        "Imported {inserted}/{total} snippets ({skipped} skipped — likely duplicate slugs).",
        &[
            ("inserted", inserted_str.as_str()),
            ("total", total_str.as_str()),
            ("skipped", skipped_str.as_str()),
        ],
        &headers,
    )
}

/// GET /cms-admin/redirects/import — show the upload form.
pub async fn redirect_import_form(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
) -> Result<Response, AdminError> {
    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "redirects", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    render_with_csrf(
        &state,
        &headers,
        "rcms_admin/redirect_import.html",
        &mut ctx,
    )
}

/// Parsed shape of one row from the CSV / TSV upload. Either two
/// or three fields per row: `old_path`, `new_path`, optional
/// `is_permanent` (`true` / `false`, or `301` / `302`).
#[derive(Debug)]
struct RedirectImportRow {
    from_path: String,
    to_path: String,
    is_permanent: bool,
}

fn parse_redirect_csv(text: &str) -> Vec<RedirectImportRow> {
    // Best-effort CSV / TSV. Doesn't handle quoted commas inside
    // values (RFC-4180 strict) — redirects are URL paths, no
    // commas in them. TSV detection: if any line contains a tab,
    // use tab as the delimiter; otherwise comma.
    let delimiter = if text.contains('\t') { '\t' } else { ',' };
    let mut out = Vec::new();
    for (lineno, raw) in text.lines().enumerate() {
        let trimmed = raw.trim();
        if trimmed.is_empty() {
            continue;
        }
        let parts: Vec<&str> = trimmed.split(delimiter).map(str::trim).collect();
        // Header row: if the first row's first two cells are exactly
        // `from_path` / `to_path` (case-insensitive), skip.
        if lineno == 0 {
            let h0 = parts.first().copied().unwrap_or("").to_ascii_lowercase();
            if h0 == "from_path" || h0 == "from" || h0 == "old" || h0 == "source" {
                continue;
            }
        }
        if parts.len() < 2 {
            continue;
        }
        let from = parts[0];
        let to = parts[1];
        if from.is_empty() || to.is_empty() {
            continue;
        }
        let is_permanent = parts
            .get(2)
            .map(|s| {
                let v = s.to_ascii_lowercase();
                !matches!(v.as_str(), "false" | "0" | "302" | "no")
            })
            .unwrap_or(true);
        out.push(RedirectImportRow {
            from_path: from.to_owned(),
            to_path: to.to_owned(),
            is_permanent,
        });
    }
    out
}

/// Serialize redirects to CSV: a header row + `from_path,to_path,
/// is_permanent`, with `is_permanent` rendered as `301`/`302` so the
/// importer ([`parse_redirect_csv`]) round-trips an export.
fn redirects_csv(items: &[crate::redirect::Redirect]) -> String {
    let mut out = String::from("from_path,to_path,is_permanent\n");
    for r in items {
        out.push_str(&csv_escape(&r.from_path));
        out.push(',');
        out.push_str(&csv_escape(&r.to_path));
        out.push(',');
        out.push_str(if r.is_permanent { "301" } else { "302" });
        out.push('\n');
    }
    out
}

/// GET /cms-admin/redirects/export.csv — download all redirects as a
/// CSV the importer can round-trip.
pub async fn redirect_export_csv(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
) -> Result<Response, AdminError> {
    let items: Vec<crate::redirect::Redirect> = crate::redirect::Redirect::objects()
        .order_by(&[("from_path", false)])
        .fetch(tenant.pool())
        .await?;
    let body = redirects_csv(&items);
    let filename = format!("redirects-{}.csv", chrono::Utc::now().format("%Y%m%d"));
    let mut response = ([(header::CONTENT_TYPE, "text/csv; charset=utf-8")], body).into_response();
    if let Ok(v) = HeaderValue::from_str(&format!("attachment; filename=\"{filename}\"")) {
        response
            .headers_mut()
            .insert(header::CONTENT_DISPOSITION, v);
    }
    Ok(response)
}

#[cfg(test)]
mod user_email_column_tests {
    use super::user_email;

    fn user(email: Option<&str>, data: serde_json::Value) -> rustango::tenancy::auth::User {
        rustango::tenancy::auth::User {
            id: rustango::sql::Auto::Set(1),
            username: "u".into(),
            password_hash: String::new(),
            email: email.map(ToOwned::to_owned),
            is_superuser: false,
            active: true,
            created_at: chrono::Utc::now(),
            data,
            password_changed_at: None,
            sessions_revoked_at: None,
        }
    }

    #[test]
    fn column_first_then_legacy_data_bag() {
        // A member signed up on the site: the column only.
        assert_eq!(user_email(&user(Some("a@x.example"), serde_json::json!({}))), "a@x.example");
        // An admin-made user from before: the data bag only.
        assert_eq!(user_email(&user(None, serde_json::json!({"email": "b@x.example"}))), "b@x.example");
        assert_eq!(
            user_email(&user(Some("a@x.example"), serde_json::json!({"email": "old@x.example"}))),
            "a@x.example"
        );
        assert_eq!(user_email(&user(Some("  "), serde_json::json!({}))), "");
    }
}

#[cfg(test)]
mod translation_canonical_html_tests {
    use super::translation_canonical_html;

    #[test]
    fn rich_text_is_sanitized_html_other_kinds_empty() {
        let html = translation_canonical_html("richtext", "<p>A jar</p><script>x()</script>");
        assert_eq!(html, "<p>A jar</p>");
        assert_eq!(translation_canonical_html("richtext", "  "), "");
        assert_eq!(translation_canonical_html("textarea", "<p>A jar</p>"), "");
        assert_eq!(translation_canonical_html("text", "Moon jar"), "");
    }
}

#[cfg(test)]
mod user_email_tests {
    use super::validate_optional_email;

    #[test]
    fn accepts_valid_and_empty() {
        assert_eq!(validate_optional_email(Some("a@b.com")).unwrap(), "a@b.com");
        assert_eq!(
            validate_optional_email(Some("  a@b.com ")).unwrap(),
            "a@b.com"
        ); // trimmed
        assert_eq!(validate_optional_email(None).unwrap(), ""); // optional
        assert_eq!(validate_optional_email(Some("   ")).unwrap(), ""); // blank → empty
    }

    #[test]
    fn rejects_malformed() {
        assert!(validate_optional_email(Some("nope")).is_err()); // no @
        assert!(validate_optional_email(Some("a@b@c")).is_err()); // two @
        assert!(validate_optional_email(Some("@b.com")).is_err()); // empty local
        assert!(validate_optional_email(Some("a@")).is_err()); // empty domain
        assert!(validate_optional_email(Some("a b@c.com")).is_err()); // whitespace
    }
}

#[cfg(test)]
mod copy_to_locale_tests {
    use super::copy_translatable_fields;

    #[test]
    fn selects_nonempty_canonical_fields() {
        let out = copy_translatable_fields("Home", "", "  Welcome  ");
        // `seo_title` empty → skipped; whitespace-only would be too.
        assert_eq!(
            out,
            vec![
                ("title", "Home".to_owned()),
                ("seo_description", "  Welcome  ".to_owned()),
            ]
        );
    }

    #[test]
    fn all_empty_yields_nothing_to_seed() {
        assert!(copy_translatable_fields("", "   ", "").is_empty());
    }
}

#[cfg(test)]
mod redirect_csv_tests {
    use super::*;
    use rustango::sql::Auto;

    fn row(from: &str, to: &str, permanent: bool) -> crate::redirect::Redirect {
        crate::redirect::Redirect {
            id: Auto::Unset,
            from_path: from.to_owned(),
            to_path: to.to_owned(),
            is_permanent: permanent,
            note: String::new(),
            hit_count: 0,
            overrides_live: false,
            from_page_id: None,
            to_page_id: None,
            is_active: true,
            disabled_reason: String::new(),
            last_hit_at: None,
            created_at: Auto::Unset,
            updated_at: Auto::Unset,
        }
    }

    #[test]
    fn redirects_csv_header_and_301_302_mapping() {
        let csv = redirects_csv(&[row("/old", "/new", true), row("/tmp", "/dst", false)]);
        let mut lines = csv.lines();
        assert_eq!(lines.next(), Some("from_path,to_path,is_permanent"));
        assert_eq!(lines.next(), Some("/old,/new,301"));
        assert_eq!(lines.next(), Some("/tmp,/dst,302"));
        assert_eq!(lines.next(), None);
    }

    #[test]
    fn redirects_csv_empty_is_header_only() {
        assert_eq!(redirects_csv(&[]), "from_path,to_path,is_permanent\n");
    }
}

/// POST /cms-admin/redirects/import — accepts a multipart upload of
/// CSV / TSV; bulk-inserts redirects.
pub async fn redirect_import_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    mut multipart: axum::extract::Multipart,
) -> Result<Response, AdminError> {
    use rustango::sql::Auto;
    let mut bytes: Vec<u8> = Vec::new();
    let mut filename = String::new();
    while let Some(field) = multipart
        .next_field()
        .await
        .map_err(|e| AdminError::Upload(format!("multipart parse: {e}")))?
    {
        if field.name().unwrap_or("") == "file" {
            if let Some(name) = field.file_name() {
                filename = name.to_owned();
            }
            bytes = field
                .bytes()
                .await
                .map_err(|e| AdminError::Upload(format!("read bytes: {e}")))?
                .to_vec();
        } else {
            let _ = field.bytes().await;
        }
    }
    if bytes.is_empty() {
        return redirect_named_with_message(
            "rcms-admin:redirects:list",
            MsgLevel::Error,
            "No file uploaded.",
            &headers,
        );
    }
    let text = String::from_utf8_lossy(&bytes);
    let rows = parse_redirect_csv(&text);
    let total = rows.len();
    let mut inserted = 0usize;
    let mut skipped = 0usize;
    for row in rows {
        if redirect_pattern_error(&row.from_path, &row.to_path).is_some() {
            skipped += 1;
            continue;
        }
        let mut redirect = crate::redirect::Redirect {
            id: Auto::Unset,
            from_path: row.from_path,
            to_path: row.to_path,
            is_permanent: row.is_permanent,
            note: format!("Imported from {filename}"),
            hit_count: 0,
            overrides_live: false,
            from_page_id: None,
            to_page_id: None,
            is_active: true,
            disabled_reason: String::new(),
            last_hit_at: None,
            created_at: Auto::Unset,
            updated_at: Auto::Unset,
        };
        match redirect.insert_pool(tenant.pool()).await {
            Ok(_) => inserted += 1,
            Err(_) => skipped += 1,
        }
    }
    let inserted_str = inserted.to_string();
    let total_str = total.to_string();
    let skipped_str = skipped.to_string();
    redirect_named_with_message_args(
        "rcms-admin:redirects:list",
        MsgLevel::Success,
        "Imported {inserted}/{total} redirects ({skipped} skipped — likely duplicates).",
        &[
            ("inserted", inserted_str.as_str()),
            ("total", total_str.as_str()),
            ("skipped", skipped_str.as_str()),
        ],
        &headers,
    )
}

pub async fn redirect_edit_form(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
) -> Result<Response, AdminError> {
    let row = crate::redirect::Redirect::objects()
        .where_(crate::redirect::Redirect::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "redirects", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("mode", "edit");
    ctx.insert("redirect", &row);
    render_with_csrf(&state, &headers, "rcms_admin/redirect_form.html", &mut ctx)
}

pub async fn redirect_edit_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
    Form(form): Form<RedirectForm>,
) -> Result<Response, AdminError> {
    let mut row = crate::redirect::Redirect::objects()
        .where_(crate::redirect::Redirect::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    let (from, to) = redirect_form_paths(tenant.pool(), &form).await?;
    if from.is_empty() || to.is_empty() {
        return redirect_named_with_message(
            "rcms-admin:redirects:list",
            MsgLevel::Error,
            "From-path and to-path are both required.",
            &headers,
        );
    }
    if let Some(msg) = redirect_pattern_error(&from, &to) {
        return redirect_named_with_message(
            "rcms-admin:redirects:list",
            MsgLevel::Error,
            msg,
            &headers,
        );
    }
    row.from_path = from;
    row.to_path = to;
    row.is_permanent = form.parsed_permanent();
    row.overrides_live = form.parsed_overrides_live();
    row.from_page_id = form.parsed_from_page_id();
    row.to_page_id = form.parsed_to_page_id();
    row.note = form.note.unwrap_or_default().trim().to_owned();
    row.save_pool(tenant.pool()).await?;
    let overrides_live = row.overrides_live;
    redirect_save_response(&tenant, &row, overrides_live, false, &headers).await
}

/// POST /cms-admin/redirects/{id}/enable — re-enable a rule that was
/// auto-disabled when a linked page was deleted/unpublished.
pub async fn redirect_enable_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
) -> Result<Response, AdminError> {
    let mut row = crate::redirect::Redirect::objects()
        .where_(crate::redirect::Redirect::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    row.is_active = true;
    row.disabled_reason = String::new();
    row.save_pool(tenant.pool()).await?;
    redirect_named_with_message_args(
        "rcms-admin:redirects:list",
        MsgLevel::Success,
        "Re-enabled redirect {from} → {to}.",
        &[
            ("from", row.from_path.as_str()),
            ("to", row.to_path.as_str()),
        ],
        &headers,
    )
}

pub async fn redirect_delete_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Path(id): Path<i64>,
) -> Result<Response, AdminError> {
    let row = crate::redirect::Redirect::objects()
        .where_(crate::redirect::Redirect::id.eq(id))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(id))?;
    let label = format!("{} → {}", row.from_path, row.to_path);
    row.delete_pool(tenant.pool()).await?;
    redirect_named_with_message_args(
        "rcms-admin:redirects:list",
        MsgLevel::Success,
        "Deleted redirect {label}.",
        &[("label", label.as_str())],
        &headers,
    )
}

// =====================================================================
// Helpers (existing)
// =====================================================================

// ---------- helpers ----------

/// `PageType` rows whose handler reports `is_creatable() = true`.
/// Falls back to the row's `is_creatable` column when no handler is
/// registered (stale row from a removed type).
pub(crate) fn creatable_types(all: &[PageType]) -> Vec<PageType> {
    all.iter()
        .filter(|t| {
            find_handler(&t.type_name)
                .map(|h| h.is_creatable())
                .unwrap_or(t.is_creatable)
        })
        .cloned()
        .collect()
}

/// `PageType` rows allowed at the ROOT of the tree (no parent).
///
/// A type that names `allowed_parent_types` is declaring it may only live
/// under those parents, so it cannot be a root. Without this the root picker
/// offered every creatable type and the save path accepted it, letting you
/// create e.g. an `ArticlePage` (parent-restricted to `HomePage`) at the root
/// — a tree the type rules forbid. The child case has always been
/// gated by [`allowed_children_for`]; this is the missing mirror.
pub(crate) fn allowed_root_types(all: &[PageType]) -> Vec<PageType> {
    creatable_types(all)
        .into_iter()
        .filter(|t| t.allowed_parents().is_empty())
        .collect()
}

/// `PageType` rows allowed as direct children of `parent`. Consults
/// the parent's handler's `allowed_child_types` AND each candidate's
/// `allowed_parent_types`. Skip the check if either whitelist is
/// empty.
pub(crate) fn allowed_children_for(parent: &Page, all: &[PageType]) -> Vec<PageType> {
    let parent_row = all
        .iter()
        .find(|t| t.id.get().copied() == Some(parent.page_type_id));
    let parent_type_name = parent_row.map(|t| t.type_name.clone()).unwrap_or_default();
    // Rules come from the rows, so admin-made types count too (#843).
    let parent_allowed_children = parent_row.map(PageType::allowed_children).unwrap_or_default();

    creatable_types(all)
        .into_iter()
        .filter(|cand| {
            // Parent-side gate: empty list = no restriction.
            if !parent_allowed_children.is_empty()
                && !parent_allowed_children.iter().any(|t| t == &cand.type_name)
            {
                return false;
            }
            // Child-side gate.
            let allowed_parents = cand.allowed_parents();
            allowed_parents.is_empty() || allowed_parents.iter().any(|p| *p == parent_type_name)
        })
        .collect()
}

/// Empty values for a fresh create form — Tera doesn't auto-default
/// missing keys, so we pre-populate the field map.
fn empty_page_form_values() -> serde_json::Value {
    serde_json::json!({
        "title": "",
        "slug": "",
        "page_type_id": 0,
        "status": "draft",
        "seo_title": "",
        "seo_description": "",
        "robots_index": true,
        "sitemap_priority": 0.5,
        "show_in_menus": true,
        "go_live_at": serde_json::Value::Null,
        "expire_at": serde_json::Value::Null,
        "og_title": "",
        "og_description": "",
        "og_image_media_id": serde_json::Value::Null,
        "twitter_card": "summary_large_image",
    })
}

// =====================================================================
// Password reset
// =====================================================================
//
// The four screens — request form (GET), request submit (POST),
// confirm form (GET, signed-token URL), confirm submit (POST). Mounted
// by `admin::public_router` so anonymous visitors can reach them.
//
// Token model: signed-URL only (no DB table). The signing secret lives
// in `AdminState::signing_secret`, loaded once at boot. `PasswordReset`
// (rustango::auth_flows) handles issue + verify.
//
// Email model: `ConsoleMailer` for v1 — the reset URL prints to stdout
// + `tracing::info!`. Production deployments swap in `SmtpMailer` by
// editing `send_reset_email` below.
//
// Account-existence non-leak: every reset-request returns the same
// "if that account exists, check your email" page regardless of
// whether a user was found.

/// POST `/cms-admin/logout` — clear every session cookie the host
/// might have minted (bare-admin `rustango_admin_session`, tenant
/// `rustango_tenant_session`, operator `rustango_op_session`),
/// redirect to `/login`. Self-contained so the button works
/// regardless of which framework auth flow signed the user in —
/// avoids depending on the right cookie name being known here.
pub async fn cms_logout_submit(
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    tenant: Result<Tenant, rustango::extractors::TenantRejection>,
    request: crate::security_log::LoginRequest,
) -> Response {
    if let (Some(user), Ok(tenant)) = (session_user, tenant) {
        let entry = SecurityEntry::new(crate::security_log::EVENT_LOGOUT, "admin", &request.meta)
            .user(user.id.get().copied(), &user.username);
        crate::security_log::record(tenant.pool(), &tenant.org.slug, entry).await;
    }
    let mut resp = Redirect::to("/login").into_response();
    // Reset all three cookies (`Max-Age=0` clears, the empty value
    // is a fallback for browsers that only honor expiry-by-value).
    // SameSite=Lax + HttpOnly match the framework's mint shape.
    let cookies = [
        "rustango_admin_session=; Path=/; HttpOnly; SameSite=Lax; Max-Age=0",
        "rustango_tenant_session=; Path=/; HttpOnly; SameSite=Lax; Max-Age=0",
        "rustango_op_session=; Path=/; HttpOnly; SameSite=Lax; Max-Age=0",
    ];
    for c in cookies {
        if let Ok(v) = HeaderValue::from_str(c) {
            resp.headers_mut().append(header::SET_COOKIE, v);
        }
    }
    resp
}

/// GET `/login` — render the CMS-branded login form. Shadows the
/// framework's default `/login` GET (which is wired to its own
/// embedded Tera + `login.html`). POST `/login` continues to be
/// handled by the framework's auth code — its handler validates
/// `username`+`password` against `rustango_users` and sets the
/// tenant session cookie.
/// GET `/cms-admin/no-access` — destination for the `cms_admin.access`
/// gate. Reachable while signed in; doesn't require any
/// permission. The middleware adds an exception for this path so
/// the redirect doesn't loop.
pub async fn no_access_page(
    State(state): State<super::AdminState>,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
) -> Result<Response, AdminError> {
    let mut ctx = Context::new();
    ctx.insert("admin_title", &"CMS Admin");
    render_with_csrf(&state, &headers, "rcms_admin/no_access.html", &mut ctx)
}

/// GET `/cms-admin/me` — per-user appearance / text-size / accessibility
/// preferences. Replaces the sidebar-footer toggles. The
/// preferences themselves persist in `localStorage` via the existing
/// no-flash boot script + the toggle JS in `_base.html`; this view
/// is purely the surface that hosts the same toggle widgets in a
/// dedicated page.
/// POST /cms-admin/dismissibles/{key} — record dismissal of the
/// banner identified by `key` for the current user. Returns 204 No Content on success.
pub async fn dismissible_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Path(key): Path<String>,
) -> Result<Response, AdminError> {
    let Some(viewer) = session_user.as_ref() else {
        return Ok(unauthorized_no_session());
    };
    let viewer_id = viewer.id.get().copied().unwrap_or_default();
    crate::dismissible::dismiss(tenant.pool(), viewer_id, &key).await?;
    Ok((axum::http::StatusCode::NO_CONTENT, "").into_response())
}

/// GET /cms-admin/dashboard — editor home / at-a-glance screen.
/// Surfaces count widgets, recent edits,
/// items locked by the current user, and pages scheduled to publish
/// next.
pub async fn dashboard(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
) -> Result<Response, AdminError> {
    let pool = tenant.pool();
    let viewer_id = session_user.as_ref().and_then(|u| u.id.get().copied());

    // ---- counts ----
    let all_pages: Vec<Page> = Page::objects().fetch(pool).await.unwrap_or_default();
    let mut count_drafts = 0usize;
    let mut count_published = 0usize;
    let mut count_scheduled = 0usize;
    let mut count_archived = 0usize;
    for p in &all_pages {
        match p.status.as_str() {
            "draft" => count_drafts += 1,
            "published" => count_published += 1,
            "scheduled" => count_scheduled += 1,
            "archived" => count_archived += 1,
            _ => {}
        }
    }
    let snippet_count: Vec<crate::snippet::Snippet> = crate::snippet::Snippet::objects()
        .fetch(pool)
        .await
        .unwrap_or_default();
    let media_rows: Vec<Media> = Media::objects().fetch(pool).await.unwrap_or_default();
    let media_images = media_rows.iter().filter(|m| m.kind == "image").count();
    let media_docs = media_rows.len() - media_images;

    // ---- recent edits (last 10 revisions across all pages) ----
    let revs: Vec<crate::revision::Revision> = crate::revision::Revision::objects()
        .order_by(&[("created_at", true)]) // desc
        .limit(10)
        .fetch(pool)
        .await
        .unwrap_or_default();
    // Who made them, by name (one query).
    let user_ids: std::collections::BTreeSet<i64> = revs.iter().filter_map(|r| r.created_by).collect();
    let usernames: std::collections::HashMap<i64, String> = if user_ids.is_empty() {
        std::collections::HashMap::new()
    } else {
        rustango::tenancy::auth::User::objects()
            .where_(rustango::tenancy::auth::User::id.is_in(user_ids.into_iter()))
            .fetch(pool)
            .await
            .unwrap_or_default()
            .into_iter()
            .filter_map(|u| u.id.get().copied().map(|id| (id, u.username)))
            .collect()
    };
    let page_by_id: std::collections::HashMap<i64, &Page> = all_pages
        .iter()
        .filter_map(|p| p.id.get().copied().map(|id| (id, p)))
        .collect();
    let recent_edits: Vec<serde_json::Value> = revs
        .iter()
        .filter_map(|r| {
            let p = page_by_id.get(&r.page_id)?;
            Some(serde_json::json!({
                "page_id": r.page_id,
                "title": p.title,
                "url_path": p.url_path,
                "sequence": r.sequence,
                "created_at": r.created_at.get().copied(),
                "created_by": r.created_by.and_then(|id| usernames.get(&id).cloned()),
            }))
        })
        .collect();

    // ---- locked by me ----
    let locks: Vec<crate::lock::PageLock> = crate::lock::PageLock::objects()
        .fetch(pool)
        .await
        .unwrap_or_default();
    let now = chrono::Utc::now();
    let mine_locked: Vec<serde_json::Value> = locks
        .iter()
        .filter(|l| !l.is_stale(now))
        .filter(|l| Some(l.user_id) == viewer_id)
        .filter_map(|l| {
            let p = page_by_id.get(&l.page_id)?;
            Some(serde_json::json!({
                "page_id": l.page_id,
                "title": p.title,
                "url_path": p.url_path,
                "acquired_at": l.acquired_at.get().copied(),
            }))
        })
        .collect();

    // ---- waiting for your review ----
    // Pages mid-review whose current step belongs to one of the viewer's
    // roles (a superuser decides every step). Without this the reviewer
    // had no way to know someone was waiting.
    let mut review_rows: Vec<serde_json::Value> = Vec::new();
    if let Some(viewer) = session_user.as_ref() {
        let states: Vec<crate::workflow::WorkflowState> = crate::workflow::WorkflowState::objects()
            .where_(crate::workflow::WorkflowState::status.eq(crate::workflow::WorkflowStatus::InProgress.as_str().to_owned()))
            .fetch(pool)
            .await
            .unwrap_or_default();
        if !states.is_empty() {
            let my_roles: std::collections::BTreeSet<i64> = rustango::tenancy::permissions::UserRole::objects()
                .where_(rustango::tenancy::permissions::UserRole::user_id.eq(viewer_id.unwrap_or_default()))
                .fetch(pool)
                .await
                .unwrap_or_default()
                .into_iter()
                .map(|m| m.role_id)
                .collect();
            let task_ids: Vec<i64> = states.iter().filter_map(|st| st.current_task_id).collect();
            let tasks: std::collections::HashMap<i64, crate::workflow::WorkflowTask> = crate::workflow::WorkflowTask::objects()
                .where_(crate::workflow::WorkflowTask::id.is_in(task_ids.into_iter()))
                .fetch(pool)
                .await
                .unwrap_or_default()
                .into_iter()
                .filter_map(|t| t.id.get().copied().map(|id| (id, t)))
                .collect();
            for st in &states {
                let Some(task) = st.current_task_id.and_then(|id| tasks.get(&id)) else { continue };
                if !viewer.is_superuser && !my_roles.contains(&task.role_id) {
                    continue;
                }
                let Some(p) = page_by_id.get(&st.page_id) else { continue };
                review_rows.push(serde_json::json!({
                    "page_id": st.page_id,
                    "title": p.title,
                    "url_path": p.url_path,
                    "step": task.name,
                    "requested_at": st.requested_at.get().copied(),
                }));
            }
        }
    }

    // ---- scheduled next ----
    let mut scheduled: Vec<&Page> = all_pages
        .iter()
        .filter(|p| {
            p.status == "scheduled" && p.go_live_at.is_some() && p.go_live_at.unwrap() > now
        })
        .collect();
    scheduled.sort_by_key(|p| p.go_live_at);
    scheduled.truncate(5);
    let scheduled_rows: Vec<serde_json::Value> = scheduled
        .into_iter()
        .map(|p| {
            serde_json::json!({
                "page_id": p.id.get().copied().unwrap_or_default(),
                "title": p.title,
                "url_path": p.url_path,
                "go_live_at": p.go_live_at,
            })
        })
        .collect();

    // ---- plugin widgets ----
    let plugin_widgets: Vec<serde_json::Value> = crate::hooks::dashboard_widgets()
        .into_iter()
        .map(|w| {
            serde_json::json!({
                "key": w.key,
                "label": w.label,
                "icon": w.icon,
                "html": w.html,
            })
        })
        .collect();

    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "dashboard", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    ctx.insert("review_rows", &review_rows);
    ctx.insert("count_drafts", &count_drafts);
    ctx.insert("count_published", &count_published);
    ctx.insert("count_scheduled", &count_scheduled);
    ctx.insert("count_archived", &count_archived);
    ctx.insert("count_snippets", &snippet_count.len());
    ctx.insert("count_images", &media_images);
    ctx.insert("count_documents", &media_docs);
    ctx.insert("recent_edits", &recent_edits);
    ctx.insert("mine_locked", &mine_locked);
    ctx.insert("scheduled_rows", &scheduled_rows);
    ctx.insert("plugin_widgets", &plugin_widgets);
    render_with_csrf(&state, &headers, "rcms_admin/dashboard.html", &mut ctx)
}

/// GET /cms-admin/styleguide — component gallery. Lists every registered block + every widget kind so
/// editors + reviewers can see the available palette at a glance.
pub async fn styleguide(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
) -> Result<Response, AdminError> {
    let mut ctx = Context::new();
    add_chrome(&mut ctx, &tenant, "styleguide", session_user.as_ref()).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;

    // Registered blocks — one row per registration. Author-curated
    // sample data lives in the template (the trait surface doesn't
    // require blocks to ship examples).
    let blocks: Vec<serde_json::Value> = crate::block::registered_blocks()
        .map(|b| {
            serde_json::json!({
                "type_name": b.type_name(),
                "verbose_name": b.verbose_name(),
                "icon": b.icon(),
                "group": b.group(),
                "field_count": b.fields().len(),
            })
        })
        .collect();
    ctx.insert("blocks", &blocks);

    // Registered page types — second registry editors care about.
    let page_types: Vec<serde_json::Value> = crate::page_type::registered_handlers()
        .map(|h| {
            serde_json::json!({
                "type_name": h.type_name(),
                "verbose_name": h.verbose_name(),
                "icon": h.icon(),
                "description": h.description(),
            })
        })
        .collect();
    ctx.insert("page_types", &page_types);

    // Library types.
    let library_types: Vec<serde_json::Value> = crate::library::registered_handlers()
        .map(|h| {
            serde_json::json!({
                "type_name": h.type_name(),
                "verbose_name": h.verbose_name(),
            })
        })
        .collect();
    ctx.insert("library_types", &library_types);

    render_with_csrf(&state, &headers, "rcms_admin/styleguide.html", &mut ctx)
}

pub async fn account_preferences(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
) -> Result<Response, AdminError> {
    render_account_page(&state, &tenant, &headers, session_user.as_ref(), None).await
}

/// Serialize a user's MCP keys (+ per-key skill scope) and the tenant's
/// skill catalog for the key-management cards.
async fn mcp_keys_ctx(
    pool: &rustango::sql::Pool,
    owner_id: i64,
) -> (Vec<serde_json::Value>, Vec<serde_json::Value>) {
    use rustango::tenancy::{AgentGrant, AgentSkill};

    let keys = rustango::tenancy::list_user_keys_pool(pool, owner_id)
        .await
        .unwrap_or_default();
    let skills: Vec<AgentSkill> = rustango::tenancy::list_skills_pool(pool)
        .await
        .unwrap_or_default();
    let skill_name = |sid: i64| -> Option<String> {
        skills
            .iter()
            .find(|s| s.id.get().copied() == Some(sid))
            .map(|s| s.codename.clone())
    };
    let mut rows = Vec::with_capacity(keys.len());
    for key in &keys {
        let key_id = key.id.get().copied().unwrap_or_default();
        let grants: Vec<AgentGrant> = AgentGrant::objects()
            .where_(AgentGrant::agent_id.eq(key_id))
            .fetch(pool)
            .await
            .unwrap_or_default();
        let scope: Vec<String> = grants
            .iter()
            .filter_map(|g| skill_name(g.skill_id))
            .collect();
        rows.push(serde_json::json!({
            "id": key_id,
            "label": key.data.get("label").and_then(|v| v.as_str()).unwrap_or(&key.name),
            "prefix": key.secret_prefix,
            "active": key.active,
            "created_at": key.created_at.get().map(|t| t.format("%Y-%m-%d %H:%M").to_string()),
            "scope": scope,
        }));
    }
    let skill_rows: Vec<serde_json::Value> = skills
        .iter()
        .map(|s| {
            serde_json::json!({
                "codename": s.codename,
                "name": s.name,
                "description": s.description,
            })
        })
        .collect();
    (rows, skill_rows)
}

async fn render_account_page(
    state: &super::AdminState,
    tenant: &Tenant,
    headers: &HeaderMap,
    session_user: Option<&rustango::tenancy::auth::User>,
    mcp_one_time: Option<serde_json::Value>,
) -> Result<Response, AdminError> {
    // #202 — surface the current + pending email + mailer presence so
    // the Profile card can render the change-email form variant.
    let (current_email, pending_email, mailer_wired) = {
        let mailer = state.mailer.is_some();
        let user_data = session_user
            .map(|u| u.data.clone())
            .unwrap_or_else(|| serde_json::json!({}));
        let cur = session_user.map(user_email).unwrap_or_default();
        let pending = user_data
            .get("pending_email")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_owned();
        (cur, pending, mailer)
    };
    let mut ctx = Context::new();
    add_chrome(&mut ctx, tenant, "", session_user).await;
    add_admin_theme(&mut ctx, tenant.pool()).await;
    // #174 — admin theme picker lives in Preferences (was on
    // Settings). Surface every cms_theme row + its 4 marquee
    // colors for a swatch preview.
    let themes: Vec<crate::theme::Theme> = crate::theme::Theme::objects()
        .order_by(&[("name", false)])
        .fetch(tenant.pool())
        .await
        .unwrap_or_default();
    let theme_rows: Vec<serde_json::Value> = themes
        .iter()
        .map(|t| {
            serde_json::json!({
                "id": t.id.get().copied(),
                "name": t.name,
                "slug": t.slug,
                "is_admin_default": t.is_admin_default,
                "color_bg": t.color_bg,
                "color_surface": t.color_surface,
                "color_text_primary": t.color_text_primary,
                "color_link": t.color_link,
            })
        })
        .collect();
    ctx.insert("themes", &theme_rows);
    // #128 — plugin-contributed Account settings panels. Each panel is a pre-rendered HTML fragment hosts
    // contribute via `register_account_settings_panel!`.
    let account_panels: Vec<serde_json::Value> = crate::hooks::account_settings_panels()
        .into_iter()
        .map(|p| {
            serde_json::json!({
                "key": p.key,
                "label": p.label,
                "icon": p.icon,
                "html": p.html,
            })
        })
        .collect();
    ctx.insert("account_panels", &account_panels);

    // #197 — per-user notification preferences. We pass every kind
    // through with its current toggle state so the template renders
    // checked/unchecked without a second fetch. Missing rows default
    // to true (opt-out semantics).
    let pref_map = session_user
        .and_then(|u| u.id.get().copied())
        .map(|uid| crate::notification_pref::map_for_user(tenant.pool(), uid))
        .map(|fut| async move { fut.await.unwrap_or_default() });
    let pref_lookup: std::collections::HashMap<String, bool> = match pref_map {
        Some(f) => f.await,
        None => std::collections::HashMap::new(),
    };
    let notification_rows: Vec<serde_json::Value> = crate::notification_pref::ALL_KINDS
        .iter()
        .map(|(label, kind, hint)| {
            let enabled = pref_lookup.get(*kind).copied().unwrap_or(true);
            serde_json::json!({
                "label": label,
                "kind": kind,
                "hint": hint,
                "enabled": enabled,
            })
        })
        .collect();
    ctx.insert("notification_prefs", &notification_rows);

    ctx.insert("account_email", &current_email);
    ctx.insert("account_pending_email", &pending_email);
    ctx.insert("account_mailer_wired", &mailer_wired);

    // #587 — MCP keys card: the viewer's keys + the skill catalog for
    // the optional-scope picker; `mcp_one_time` carries a just-minted
    // token for the show-once dialog.
    if let Some(uid) = session_user.and_then(|u| u.id.get().copied()) {
        let (keys, skills) = mcp_keys_ctx(tenant.pool(), uid).await;
        ctx.insert("mcp_keys", &keys);
        ctx.insert("mcp_skills", &skills);
    }
    if let Some(one_time) = &mcp_one_time {
        ctx.insert("mcp_one_time", one_time);
    }

    render_with_csrf(
        state,
        headers,
        "rcms_admin/account_preferences.html",
        &mut ctx,
    )
}

/// POST /cms-admin/me/notifications — save the on/off toggles for
/// every notification kind. The form posts a checkbox per kind;
/// absent inputs mean "off". Iterates every known kind so the user
/// can disable categories from a single submit.
pub async fn account_notifications_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Form(form): Form<std::collections::HashMap<String, String>>,
) -> Result<Response, AdminError> {
    let Some(viewer) = session_user.as_ref() else {
        return Ok(unauthorized_no_session());
    };
    let Some(uid) = viewer.id.get().copied() else {
        return Ok((axum::http::StatusCode::UNAUTHORIZED, "no user id").into_response());
    };
    for (_label, kind, _hint) in crate::notification_pref::ALL_KINDS {
        let key = format!("kind_{kind}");
        let enabled = form
            .get(&key)
            .map(|v| matches!(v.as_str(), "on" | "true" | "1" | "yes"))
            .unwrap_or(false);
        crate::notification_pref::set_enabled(tenant.pool(), uid, kind, enabled).await?;
    }
    redirect_named_with_params_and_message(
        "rcms-admin:account",
        &[],
        MsgLevel::Success,
        "Notification preferences saved.",
        &headers,
    )
}

/// POST /cms-admin/me/profile — display name + avatar upload.
/// Multipart submit; absence of either field leaves the existing
/// value untouched. Avatar bytes land at
/// `./var/avatars/<tenant-slug>/<user_id>.<ext>` (slug-namespaced so
/// per-tenant user ids don't collide) and the filename is recorded in
/// `rustango_users.data.avatar_path`.
pub async fn account_profile_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    mut multipart: axum::extract::Multipart,
) -> Result<Response, AdminError> {
    let Some(viewer) = session_user.as_ref() else {
        return Ok(unauthorized_no_session());
    };
    let Some(uid) = viewer.id.get().copied() else {
        return Ok((axum::http::StatusCode::UNAUTHORIZED, "no user id").into_response());
    };

    let mut new_display_name: Option<String> = None;
    let mut new_avatar_path: Option<String> = None;
    // Namespace avatars by tenant slug. Per-tenant DBs each start user
    // ids at 1, so a flat `./var/avatars/{uid}.{ext}` collided across
    // tenants (tenant B's user #1 overwriting / exposing tenant A's).
    let avatar_dir = std::path::PathBuf::from("./var/avatars").join(&tenant.org.slug);

    while let Some(field) = multipart
        .next_field()
        .await
        .map_err(|e| AdminError::Upload(format!("multipart parse: {e}")))?
    {
        let field_name = field.name().unwrap_or("").to_owned();
        match field_name.as_str() {
            "display_name" => {
                new_display_name = Some(
                    field
                        .text()
                        .await
                        .map_err(|e| AdminError::Upload(format!("read display_name: {e}")))?
                        .trim()
                        .to_owned(),
                );
            }
            "avatar" => {
                let original = field.file_name().map(|s| s.to_owned());
                let bytes = field
                    .bytes()
                    .await
                    .map_err(|e| AdminError::Upload(format!("read avatar: {e}")))?;
                if bytes.is_empty() {
                    continue;
                }
                let ext = original
                    .as_deref()
                    .and_then(|n| n.rsplit_once('.'))
                    .map(|(_, ext)| ext)
                    .filter(|ext| ext.len() <= 8 && ext.chars().all(|c| c.is_ascii_alphanumeric()))
                    .map(str::to_ascii_lowercase)
                    .unwrap_or_else(|| "png".to_owned());
                std::fs::create_dir_all(&avatar_dir)
                    .map_err(|e| AdminError::Upload(format!("create avatar dir: {e}")))?;
                let storage_name = format!("{uid}.{ext}");
                let path = avatar_dir.join(&storage_name);
                std::fs::write(&path, &bytes)
                    .map_err(|e| AdminError::Upload(format!("write avatar: {e}")))?;
                new_avatar_path = Some(storage_name);
            }
            _ => {
                let _ = field.bytes().await;
            }
        }
    }

    // Reload the user row + merge changes into `.data`. Using the
    // pool fetch (rather than mutating the extracted `viewer`) keeps
    // the save isolated from any session-cache invariants.
    let mut user_rows: Vec<rustango::tenancy::auth::User> =
        rustango::tenancy::auth::User::objects()
            .where_(rustango::tenancy::auth::User::id.eq(uid))
            .fetch(tenant.pool())
            .await?;
    let Some(mut user) = user_rows.pop() else {
        return Ok((axum::http::StatusCode::NOT_FOUND, "user not found").into_response());
    };

    let mut data = user.data.as_object().cloned().unwrap_or_default();
    if let Some(name) = new_display_name {
        if name.is_empty() {
            data.remove("display_name");
        } else {
            data.insert("display_name".to_owned(), serde_json::json!(name));
        }
    }
    if let Some(path) = new_avatar_path {
        data.insert("avatar_path".to_owned(), serde_json::json!(path));
    }
    user.data = serde_json::Value::Object(data);
    user.save_pool(tenant.pool()).await?;

    redirect_named_with_message(
        "rcms-admin:account",
        MsgLevel::Success,
        "Profile saved.",
        &headers,
    )
}

/// Persist an editor's preferred admin UI locale to
/// `rustango_users.data.admin_lang`. Shared by the account-prefs form and
/// the sidebar switcher so both keep the durable pref in sync with the cookie.
async fn persist_user_admin_lang(
    pool: &rustango::sql::Pool,
    uid: i64,
    lang: &str,
) -> Result<(), AdminError> {
    let mut rows: Vec<rustango::tenancy::auth::User> = rustango::tenancy::auth::User::objects()
        .where_(rustango::tenancy::auth::User::id.eq(uid))
        .fetch(pool)
        .await?;
    if let Some(mut user) = rows.pop() {
        let mut data = user.data.as_object().cloned().unwrap_or_default();
        data.insert("admin_lang".to_owned(), serde_json::json!(lang));
        user.data = serde_json::Value::Object(data);
        user.save_pool(pool).await?;
    }
    Ok(())
}

/// Form body for `POST /cms-admin/me/language`.
#[derive(Debug, Deserialize)]
pub struct AccountLanguageForm {
    pub lang: String,
}

/// POST /cms-admin/me/language — set the editor's preferred admin UI locale.
/// Persists to `rustango_users.data.admin_lang` (durable + cross-device,
/// folded into negotiation by `add_user_chrome` → `render_with_csrf`) AND sets
/// the sticky `rcms_admin_lang` cookie so the change applies on the next render.
/// An unshipped code resets to English.
pub async fn account_language_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Form(form): Form<AccountLanguageForm>,
) -> Result<Response, AdminError> {
    let Some(viewer) = session_user.as_ref() else {
        return Ok(unauthorized_no_session());
    };
    let Some(uid) = viewer.id.get().copied() else {
        return Ok((axum::http::StatusCode::UNAUTHORIZED, "no user id").into_response());
    };
    let lang = if super::i18n::is_ui_locale(&form.lang) {
        form.lang.clone()
    } else {
        "en".to_owned()
    };
    persist_user_admin_lang(tenant.pool(), uid, &lang).await?;

    let mut resp = redirect_named_with_message(
        "rcms-admin:account",
        MsgLevel::Success,
        "Language updated.",
        &headers,
    )?;
    let cookie = format!(
        "{}={lang}; Path=/; SameSite=Lax; Max-Age=31536000",
        super::i18n::ADMIN_LANG_COOKIE
    );
    if let Ok(v) = HeaderValue::from_str(&cookie) {
        resp.headers_mut().append(header::SET_COOKIE, v);
    }
    Ok(resp)
}

/// Form body for `POST /cms-admin/me/email`.
#[derive(Debug, Deserialize)]
pub struct AccountEmailForm {
    pub new_email: String,
}

/// POST /cms-admin/me/email — request an email change.
///
/// When a mailer is wired, the change goes through a confirmation
/// flow: a random 32-hex token is stashed on `user.data.pending_email_*`
/// and the new address receives a confirm link. Clicking the link
/// finalizes the change.
///
/// When no mailer is wired, the change applies immediately + a banner
/// explains no confirmation email was sent.
pub async fn account_email_submit(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Form(form): Form<AccountEmailForm>,
) -> Result<Response, AdminError> {
    let Some(viewer) = session_user.as_ref() else {
        return Ok(unauthorized_no_session());
    };
    let Some(uid) = viewer.id.get().copied() else {
        return Ok((axum::http::StatusCode::UNAUTHORIZED, "no user id").into_response());
    };

    let new_email = form.new_email.trim().to_owned();
    if new_email.is_empty() || !new_email.contains('@') {
        return redirect_named_with_message(
            "rcms-admin:account",
            MsgLevel::Error,
            "Enter a valid email address.",
            &headers,
        );
    }

    let mut user = rustango::tenancy::auth::User::objects()
        .where_(rustango::tenancy::auth::User::id.eq(uid))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(uid))?;
    let mut data = user.data.as_object().cloned().unwrap_or_default();

    if state.mailer.is_none() {
        // No mailer → write directly + banner.
        data.insert("email".to_owned(), serde_json::json!(new_email));
        user.email = Some(new_email.clone());
        // Clear any in-flight confirmation token from a previous attempt.
        data.remove("pending_email");
        user.data = serde_json::Value::Object(data);
        user.save_pool(tenant.pool()).await?;
        return redirect_named_with_message(
            "rcms-admin:account",
            MsgLevel::Warning,
            "Email updated. No confirmation email was sent — the host hasn't wired a mailer.",
            &headers,
        );
    }

    // Stash the pending email so the editor sees a banner + the
    // confirm handler can replay-guard against a different change
    // having kicked off in the meantime. Drop legacy keys from the
    // previous hand-rolled token scheme so they don't shadow the
    // new flow.
    data.insert(
        "pending_email".to_owned(),
        serde_json::json!(new_email.clone()),
    );
    data.remove("pending_email_token");
    data.remove("pending_email_expires_at");
    user.data = serde_json::Value::Object(data);
    user.save_pool(tenant.pool()).await?;

    // Send the confirmation. Best-effort. The signed URL carries
    // user_id + email + TTL — `EmailVerification::verify` on the
    // confirm endpoint replaces the hand-rolled (token, expires_at)
    // round-trip we used to stash in user.data.
    if let Some(mailer) = state.mailer.as_deref() {
        let link = rustango::auth_flows::EmailVerification::issue(
            &rustango::auth_flows::LinkScope::from(&tenant),
            "/cms-admin/me/email/confirm",
            uid,
            &new_email,
            state.signing_secret.as_slice(),
            std::time::Duration::from_secs(3600),
        );
        let subject = "Confirm your CMS email change";
        let body = format!(
            "Click the link below to confirm your CMS email change to {new_email}.\n\nLink: {link}\n\nThe link expires in one hour. If you didn't request this change, ignore this email.\n",
        );
        let email = rustango::email::Email::new()
            .from((*state.mailer_from).clone())
            .to(new_email.clone())
            .subject(subject.to_owned())
            .body(body);
        if let Err(e) = mailer.send(&email).await {
            tracing::warn!(
                target: "rustango_cms::admin::account",
                user_id = uid, error = %e,
                "email-change confirmation send failed (pending_email stashed; user can re-request)"
            );
        }
    }

    redirect_named_with_message(
        "rcms-admin:account",
        MsgLevel::Success,
        "Confirmation link sent to the new email. Click it within an hour to finalize the change.",
        &headers,
    )
}

/// GET /cms-admin/me/email/confirm — verify the signed URL from
/// [`account_email_submit`] (`EmailVerification::verify`) and apply
/// the pending email change. The URL itself carries user_id + new
/// email + TTL signature — we just check the click came from the
/// account that started the change and that the change is still
/// the one the user wants to apply.
pub async fn account_email_confirm(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    axum::extract::OriginalUri(uri): axum::extract::OriginalUri,
) -> Result<Response, AdminError> {
    let Some(viewer) = session_user.as_ref() else {
        return Ok(unauthorized_no_session());
    };
    let Some(uid) = viewer.id.get().copied() else {
        return Ok((axum::http::StatusCode::UNAUTHORIZED, "no user id").into_response());
    };

    let path_and_query = uri
        .path_and_query()
        .map(|p| p.as_str())
        .unwrap_or_default()
        .to_owned();
    let (token_uid, signed_email) = match rustango::auth_flows::EmailVerification::verify(
        &rustango::auth_flows::LinkScope::from(&tenant),
        &path_and_query,
        state.signing_secret.as_slice(),
    ) {
        Ok(pair) => pair,
        Err(_) => {
            return redirect_named_with_message(
                "rcms-admin:account",
                MsgLevel::Error,
                "Confirmation link is invalid or has expired. Request a new one.",
                &headers,
            );
        }
    };
    // Clicking your own confirm link from a different session is a
    // common case (verify from inbox on phone, signed in on
    // desktop). We require the session user to match the user_id
    // baked into the signed URL — anything else is a cross-account
    // hijack attempt.
    if token_uid != uid {
        return redirect_named_with_message(
            "rcms-admin:account",
            MsgLevel::Error,
            "Confirmation link belongs to a different account. Sign in as that account to confirm.",
            &headers,
        );
    }

    let mut user = rustango::tenancy::auth::User::objects()
        .where_(rustango::tenancy::auth::User::id.eq(uid))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(uid))?;
    let mut data = user.data.as_object().cloned().unwrap_or_default();
    // Stale-link guard: if the user kicked off a different change
    // since this link was minted (or none at all), reject — the
    // signed URL is still valid but no longer the one they want.
    let pending = data
        .get("pending_email")
        .and_then(|v| v.as_str())
        .unwrap_or("");
    if pending != signed_email {
        return redirect_named_with_message(
            "rcms-admin:account",
            MsgLevel::Error,
            "This link is for a different email change than the one currently pending. Request a fresh confirmation.",
            &headers,
        );
    }

    data.insert("email".to_owned(), serde_json::json!(signed_email));
    user.email = Some(signed_email.clone());
    data.remove("pending_email");
    // Keys the earlier email-change flow stored — dropped on confirm so
    // old rows clean themselves up.
    data.remove("pending_email_token");
    data.remove("pending_email_expires_at");
    user.data = serde_json::Value::Object(data);
    user.save_pool(tenant.pool()).await?;

    redirect_named_with_message(
        "rcms-admin:account",
        MsgLevel::Success,
        "Email confirmed. Notifications go to the new address from now on.",
        &headers,
    )
}

/// Form body for `POST /cms-admin/me/password`.
#[derive(Deserialize)]
pub struct AccountPasswordForm {
    pub current_password: String,
    pub new_password: String,
    pub new_password_confirm: String,
}

/// POST /cms-admin/me/password — verify current password, hash + write
/// the new one.
pub async fn account_password_submit(
    State(_state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(session_user): rustango::extractors::SessionUser,
    Form(form): Form<AccountPasswordForm>,
) -> Result<Response, AdminError> {
    let Some(viewer) = session_user.as_ref() else {
        return Ok(unauthorized_no_session());
    };
    let Some(uid) = viewer.id.get().copied() else {
        return Ok((axum::http::StatusCode::UNAUTHORIZED, "no user id").into_response());
    };
    if form.new_password.len() < 12 {
        return redirect_named_with_message(
            "rcms-admin:account",
            MsgLevel::Error,
            "New password must be at least 12 characters.",
            &headers,
        );
    }
    if form.new_password != form.new_password_confirm {
        return redirect_named_with_message(
            "rcms-admin:account",
            MsgLevel::Error,
            "New password and confirmation don't match.",
            &headers,
        );
    }

    let mut user = rustango::tenancy::auth::User::objects()
        .where_(rustango::tenancy::auth::User::id.eq(uid))
        .first(tenant.pool())
        .await?
        .ok_or(AdminError::NotFound(uid))?;

    let verified =
        crate::passwords::verify(&form.current_password, &user.password_hash).await.unwrap_or(false);
    if !verified {
        return redirect_named_with_message(
            "rcms-admin:account",
            MsgLevel::Error,
            "Current password is incorrect.",
            &headers,
        );
    }
    let hash = crate::passwords::hash(&form.new_password).await
        .map_err(|e| AdminError::Validation(format!("password hash failed: {e}")))?;
    user.password_hash = hash;
    user.password_changed_at = Some(chrono::Utc::now());
    user.save_pool(tenant.pool()).await?;

    redirect_named_with_message(
        "rcms-admin:account",
        MsgLevel::Success,
        "Password changed. Old sessions on other devices will be signed out on next request.",
        &headers,
    )
}

/// GET /__cms-avatar/{user_id} — stream the user's avatar bytes.
/// Anonymous-OK so the sidebar avatar shows on the dashboard
/// pre-login. Image not found → 404.
pub async fn account_avatar_serve(
    headers: HeaderMap,
    tenant: Tenant,
    Path(user_id): Path<i64>,
) -> Response {
    let user_rows: Vec<rustango::tenancy::auth::User> =
        match rustango::tenancy::auth::User::objects()
            .where_(rustango::tenancy::auth::User::id.eq(user_id))
            .fetch(tenant.pool())
            .await
        {
            Ok(v) => v,
            Err(_) => {
                return (
                    axum::http::StatusCode::INTERNAL_SERVER_ERROR,
                    "user lookup failed",
                )
                    .into_response();
            }
        };
    let Some(user) = user_rows.into_iter().next() else {
        return (axum::http::StatusCode::NOT_FOUND, "user not found").into_response();
    };
    let storage_name = match user.data.get("avatar_path").and_then(|v| v.as_str()) {
        Some(s) if !s.is_empty() => s.to_owned(),
        _ => return (axum::http::StatusCode::NOT_FOUND, "no avatar").into_response(),
    };
    if storage_name.contains('/') || storage_name.contains("..") {
        return (axum::http::StatusCode::BAD_REQUEST, "invalid storage name").into_response();
    }
    // Tenant-scoped path (matches the upload) — never read another
    // tenant's avatar dir.
    let path = std::path::PathBuf::from("./var/avatars")
        .join(&tenant.org.slug)
        .join(&storage_name);
    let bytes = match std::fs::read(&path) {
        Ok(b) => b,
        Err(_) => {
            return (axum::http::StatusCode::NOT_FOUND, "avatar bytes missing").into_response()
        }
    };
    let mime = mime_guess::from_path(&storage_name)
        .first_or_octet_stream()
        .essence_str()
        .to_owned();
    // Same stable-URL staleness fix as branding: revalidate via a
    // content ETag so a changed avatar is picked up immediately.
    revalidated_asset(&headers, mime, bytes)
}

pub async fn cms_login_form(
    State(state): State<super::AdminState>,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    tenant: Tenant,
    Query(q): Query<std::collections::HashMap<String, String>>,
) -> Result<Response, AdminError> {
    let mut ctx = Context::new();
    // The framework's POST handler expects to receive the form at
    // `/login`. Set the action to a relative URL so the same shape
    // works under reverse proxies.
    ctx.insert("action", &"/login");
    ctx.insert("admin_title", &"CMS Admin");
    // #279 — make sure the CMS-branded login form always carries a
    // `next` destination so the framework's POST handler doesn't
    // fall back to `/` (the public homepage). Precedence:
    //   1. Explicit `?next=` (passed in by `with_login_required`
    //      when an anonymous request hit an admin URL).
    //   2. Default `/cms-admin/` when the user navigated to /login
    //      manually — they almost certainly want the admin, not
    //      the public site root.
    // The framework's `sanitize_next` rejects open-redirect-shaped
    // values (anything not starting with a single `/`, scheme-bearing,
    // or pointing back at /login itself), so we don't need to filter
    // here; the worst case is the framework strips ours and uses `/`.
    let next = q
        .get("next")
        .map(String::as_str)
        .filter(|s| !s.is_empty())
        .unwrap_or("/cms-admin/");
    ctx.insert("next", next);
    // Friendly error from a redirect (e.g. `?error=invalid_credentials`).
    if let Some(err) = q.get("error") {
        ctx.insert("error", err);
    }
    // SSO (admin-sso) — one "Sign in with <provider>" button per enabled
    // `SsoProvider` row (the DB-backed, admin-managed, multi-provider model
    // that replaced the flat `Org.sso_*` columns). The framework serves
    // `/login/sso/{slug}` + `/login/sso/{slug}/callback` (mints the same
    // rustango_tenant_session cookie password login does), so no auth code
    // lives here — just the buttons. Mirrors the framework's own login context.
    let sso_providers = rustango::admin::sso_provider::list_enabled(tenant.pool(), "/login").await;
    ctx.insert("sso_enabled", &!sso_providers.is_empty());
    ctx.insert("sso_providers", &sso_providers);
    // Surface the callback's failure reason (it redirects with `?sso_error=...`).
    if let Some(err) = q.get("sso_error") {
        ctx.insert("sso_error", err);
    }
    // #199 — show the tenant's uploaded logo/favicon in the login brand badge
    // (falls back to the default icon when none is set). Same source as the
    // authenticated sidebar; the `/__cms-branding/*` routes are pre-auth.
    add_branding_urls(&mut ctx, &tenant).await;
    render_with_csrf(&state, &headers, "login.html", &mut ctx)
}

/// GET `/cms-admin/password-reset` — show the username form.
pub async fn password_reset_request_form(
    State(state): State<super::AdminState>,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
) -> Result<Response, AdminError> {
    let mut ctx = Context::new();
    render_with_csrf(
        &state,
        &headers,
        "rcms_admin/password_reset_request.html",
        &mut ctx,
    )
}

/// POST `/cms-admin/password-reset` — issue a token, email the link,
/// render the "check your email" page either way (so the form never
/// confirms or denies account existence).
/// The key a password-reset link is signed with: the process secret
/// narrowed to one tenant and one password.
///
/// Tenants number their users from 1, so a link signed with the process
/// secret alone — which names only `user_id` — resets the same id on every
/// tenant. Binding the tenant slug makes a link verify only where it was
/// issued; binding the current password hash makes it die the moment the
/// password changes, so it is single-use without any cache.
/// The scope stamped on a reset link: the link only verifies on the tenant
/// it was issued for, alongside the tenant-bound key.
fn password_reset_scope(tenant_slug: &str) -> rustango::auth_flows::LinkScope {
    rustango::auth_flows::LinkScope::audience(&format!("rcms-reset:{tenant_slug}"))
}

fn password_reset_key(secret: &[u8], tenant_slug: &str, password_hash: &str) -> Vec<u8> {
    let msg = format!("rcms-password-reset\0{tenant_slug}\0{password_hash}");
    crate::signing::hmac_sha256_hex(secret, msg.as_bytes()).into_bytes()
}

/// The relative reset link (`/cms-admin/password-reset/confirm?…`) for
/// `user`, valid for an hour. Signed with a key bound to the tenant and
/// the user's current password hash, so it stops working once used.
fn password_reset_url(
    secret: &[u8],
    tenant_slug: &str,
    user: &rustango::tenancy::auth::User,
) -> String {
    let key = password_reset_key(secret, tenant_slug, &user.password_hash);
    rustango::auth_flows::PasswordReset::issue(
        &password_reset_scope(tenant_slug),
        "/cms-admin/password-reset/confirm",
        user.id.get().copied().unwrap_or(0),
        &key,
        std::time::Duration::from_secs(3600),
    )
}

/// A one-hour password-reset link for the active user `username` in the
/// tenant `pool` belongs to, or `None` when there is no such user.
///
/// For operators: it works for an account with no email on file, which
/// the reset form cannot reach. The link is relative — prefix the site's
/// origin. It is valid on any admin router of this process, which signs
/// with the same [`crate::signing::secret`].
///
/// # Errors
/// Database errors from the user lookup.
pub async fn issue_password_reset_link(
    pool: &rustango::sql::Pool,
    tenant_slug: &str,
    username: &str,
) -> Result<Option<String>, rustango::sql::ExecError> {
    let user: Option<rustango::tenancy::auth::User> = rustango::tenancy::auth::User::objects()
        .where_(rustango::tenancy::auth::User::username.eq(username.to_owned()))
        .where_(rustango::tenancy::auth::User::active.eq(true))
        .fetch(pool)
        .await?
        .into_iter()
        .next();
    let secret = crate::signing::derived_key(super::PASSWORD_RESET_KEY_PURPOSE);
    Ok(user.map(|u| password_reset_url(&secret, tenant_slug, &u)))
}

/// The active user a reset link was issued for, in this request's tenant.
///
/// The `user_id` in the link is read before it is trusted only to find
/// whose key to verify against; the link is accepted solely when it
/// verifies under that user's tenant- and password-bound key.
async fn password_reset_user(
    tenant: &Tenant,
    secret: &[u8],
    signed_url: &str,
) -> Option<rustango::tenancy::auth::User> {
    let claimed: i64 = signed_url
        .split_once('?')?
        .1
        .split('&')
        .find_map(|kv| kv.strip_prefix("user_id="))?
        .parse()
        .ok()?;
    let user = rustango::tenancy::auth::User::objects()
        .where_(rustango::tenancy::auth::User::id.eq(claimed))
        .where_(rustango::tenancy::auth::User::active.eq(true))
        .first(tenant.pool())
        .await
        .ok()??;
    let key = password_reset_key(secret, &tenant.org.slug, &user.password_hash);
    match rustango::auth_flows::PasswordReset::verify(
        &password_reset_scope(&tenant.org.slug),
        signed_url,
        &key,
    ) {
        Ok(uid) if uid == claimed => Some(user),
        _ => None,
    }
}

pub async fn password_reset_request_submit(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    request: crate::security_log::LoginRequest,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Form(form): Form<std::collections::HashMap<String, String>>,
) -> Result<Response, AdminError> {
    let username = form.get("username").cloned().unwrap_or_default();
    if !username.trim().is_empty() {
        // Look up the rustango_users row by username. Active users
        // only; soft-disabled accounts get the same silent treatment
        // as missing ones.
        let user: Option<rustango::tenancy::auth::User> = rustango::tenancy::auth::User::objects()
            .where_(rustango::tenancy::auth::User::username.eq(username.clone()))
            .where_(rustango::tenancy::auth::User::active.eq(true))
            .fetch(tenant.pool())
            .await
            .unwrap_or_default()
            .into_iter()
            .next();
        if let Some(u) = user {
            let entry = SecurityEntry::new(
                crate::security_log::EVENT_PASSWORD_RESET_REQUESTED,
                "admin",
                &request.meta,
            )
            .user(u.id.get().copied(), &u.username);
            crate::security_log::record(tenant.pool(), &tenant.org.slug, entry).await;
            // The link goes to the user's real address. The response below
            // renders "sent" either way, so the form never confirms or
            // denies that an account exists.
            let token_url =
                password_reset_url(state.signing_secret.as_slice(), &tenant.org.slug, &u);
            let to_email = Some(user_email(&u)).filter(|e| !e.is_empty());
            match (to_email, state.mailer.is_some()) {
                (Some(to), _) => {
                    send_reset_email(
                        state.mailer.as_deref(),
                        &state.mailer_from,
                        &to,
                        &u.username,
                        &token_url,
                    )
                    .await;
                }
                // No mailer: emails already go to stdout, which the page
                // tells the reader to check — so print the link there for
                // the operator too, rather than nothing at all.
                (None, false) => {
                    println!(
                        "============= password reset =============\n\
                         {} has no email on file. Reset link, valid for an hour \
                         (prefix the site's address):\n{token_url}\n\
                         ==========================================",
                        u.username
                    );
                    tracing::warn!(
                        username = %u.username,
                        "password reset for an account with no email: link printed to stdout"
                    );
                }
                (None, true) => {
                    tracing::warn!(
                        username = %u.username,
                        "password reset requested but the account has no email on file; nothing \
                         sent. Add an email to the account, or issue a link with \
                         rustango_cms::admin::issue_password_reset_link"
                    );
                }
            }
        }
    }
    let mut ctx = Context::new();
    // The stdout hint is only true when no mailer is wired.
    ctx.insert("console_mail", &state.mailer.is_none());
    render_with_csrf(
        &state,
        &headers,
        "rcms_admin/password_reset_sent.html",
        &mut ctx,
    )
}

/// GET `/cms-admin/password-reset/confirm` — verify the signed token
/// in the URL, render the new-password form when valid. Invalid /
/// expired links land on a friendly error inside the same template.
pub async fn password_reset_confirm_form(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    axum::extract::OriginalUri(uri): axum::extract::OriginalUri,
) -> Result<Response, AdminError> {
    let path_and_query = uri
        .path_and_query()
        .map(|p| p.as_str())
        .unwrap_or_default()
        .to_owned();
    let mut ctx = Context::new();
    match password_reset_user(&tenant, state.signing_secret.as_slice(), &path_and_query).await {
        Some(_) => {
            // Pass the signed URL through so the POST handler can
            // re-verify before writing the new password.
            ctx.insert("signed_token", &path_and_query);
            render_with_csrf(
                &state,
                &headers,
                "rcms_admin/password_reset_confirm.html",
                &mut ctx,
            )
        }
        None => {
            ctx.insert(
                "error",
                &"This reset link is invalid or has expired. Request a fresh one.",
            );
            render_with_csrf(
                &state,
                &headers,
                "rcms_admin/password_reset_request.html",
                &mut ctx,
            )
        }
    }
}

/// POST `/cms-admin/password-reset/confirm` — verify the token + new
/// password fields, hash the password, write to `rustango_users`,
/// render the "all set" page.
pub async fn password_reset_confirm_submit(
    State(state): State<super::AdminState>,
    tenant: Tenant,
    headers: HeaderMap,
    request: crate::security_log::LoginRequest,
    rustango::extractors::SessionUser(_session_user): rustango::extractors::SessionUser,
    Form(form): Form<std::collections::HashMap<String, String>>,
) -> Result<Response, AdminError> {
    let token = form.get("token").cloned().unwrap_or_default();
    let password = form.get("password").cloned().unwrap_or_default();
    let password_confirm = form.get("password_confirm").cloned().unwrap_or_default();

    let mut ctx = Context::new();
    let render_confirm_with_error = |state: &super::AdminState,
                                     headers: &HeaderMap,
                                     token: &str,
                                     msg: &str|
     -> Result<Response, AdminError> {
        let mut c = Context::new();
        c.insert("signed_token", &token);
        c.insert("error", &msg);
        render_with_csrf(
            state,
            headers,
            "rcms_admin/password_reset_confirm.html",
            &mut c,
        )
    };

    if password.len() < 12 {
        return render_confirm_with_error(
            &state,
            &headers,
            &token,
            "Password must be at least 12 characters.",
        );
    }
    if password != password_confirm {
        return render_confirm_with_error(
            &state,
            &headers,
            &token,
            "Password confirmation didn't match.",
        );
    }

    // Tenant- and password-bound (#673): a link from another tenant, or one
    // already used to change this password, does not verify.
    let Some(mut user) = password_reset_user(&tenant, state.signing_secret.as_slice(), &token).await
    else {
        ctx.insert(
            "error",
            &"This reset link is invalid or has expired. Request a fresh one.",
        );
        return render_with_csrf(
            &state,
            &headers,
            "rcms_admin/password_reset_request.html",
            &mut ctx,
        );
    };
    let new_hash = match crate::passwords::hash(&password).await {
        Ok(h) => h,
        Err(e) => {
            tracing::warn!(error = %e, "password hash failed");
            return render_confirm_with_error(
                &state,
                &headers,
                &token,
                "Could not save the new password. Try again.",
            );
        }
    };
    user.password_hash = new_hash;
    if user.save_pool(tenant.pool()).await.is_err() {
        return render_confirm_with_error(
            &state,
            &headers,
            &token,
            "Could not save the new password. Try again.",
        );
    }
    // Proving control of the account's mail lifts a lock a guesser set.
    crate::security_log::unlock(&tenant.org.slug, &user.username).await;
    let entry = SecurityEntry::new(crate::security_log::EVENT_PASSWORD_RESET, "admin", &request.meta)
        .user(user.id.get().copied(), &user.username);
    crate::security_log::record(tenant.pool(), &tenant.org.slug, entry).await;

    render_with_csrf(
        &state,
        &headers,
        "rcms_admin/password_reset_done.html",
        &mut ctx,
    )
}

/// Send the reset email via `ConsoleMailer`. Prints the URL to stdout
/// in dev; production deployments swap in `SmtpMailer` here.
///
/// The `rustango_users` table doesn't carry an email column, so the
/// "to" address is synthesized from the username for now. Replace
/// with `user.email` once the host extends the user model.
/// Deliver the reset link to the user's real address through
/// the host-configured mailer (`router_with_mailer`), falling back to
/// the console mailer in dev setups with no mailer wired.
async fn send_reset_email(
    mailer: Option<&dyn rustango::email::Mailer>,
    mailer_from: &str,
    to_email: &str,
    username: &str,
    reset_url: &str,
) {
    let from = if mailer_from.trim().is_empty() {
        "noreply@cms.local"
    } else {
        mailer_from
    };
    let email = rustango::email::Email::new()
        .to(to_email)
        .from(from)
        .subject("Reset your CMS password")
        .body(format!(
            "Hi {username},\n\n\
             We received a request to reset your CMS password.\n\
             Click the link below within the next hour to set a new one:\n\n\
             {reset_url}\n\n\
             If you didn't request this, you can safely ignore this email.\n"
        ));
    let result = match mailer {
        Some(m) => m.send(&email).await,
        None => {
            let console = rustango::email::ConsoleMailer;
            <rustango::email::ConsoleMailer as rustango::email::Mailer>::send(&console, &email)
                .await
        }
    };
    if let Err(e) = result {
        tracing::warn!(error = %e, username = %username, "password-reset mail send failed");
    }
    tracing::info!(username = %username, "issued password-reset link");
}

#[cfg(test)]
mod tests {
    //! Role-permission matrix translation tests. Exercise the
    //! pure helpers — no DB, no Tera.
    use super::*;

    fn reset_user(password_hash: &str) -> rustango::tenancy::auth::User {
        rustango::tenancy::auth::User {
            id: rustango::sql::Auto::Set(7),
            username: "admin".to_owned(),
            password_hash: password_hash.to_owned(),
            email: None,
            is_superuser: true,
            active: true,
            created_at: chrono::Utc::now(),
            data: serde_json::json!({}),
            password_changed_at: None,
            sessions_revoked_at: None,
        }
    }

    /// The link an operator gets (or the console prints for an account
    /// with no email) is the one the confirm page accepts — and it dies
    /// once the password changes.
    #[test]
    fn a_reset_link_verifies_until_the_password_changes() {
        let secret = b"test-secret";
        let url = password_reset_url(secret, "blog", &reset_user("hash-1"));
        assert!(url.starts_with("/cms-admin/password-reset/confirm?user_id=7&"), "{url}");
        let key = password_reset_key(secret, "blog", "hash-1");
        let blog = password_reset_scope("blog");
        assert_eq!(rustango::auth_flows::PasswordReset::verify(&blog, &url, &key).ok(), Some(7));
        let after_change = password_reset_key(secret, "blog", "hash-2");
        assert!(rustango::auth_flows::PasswordReset::verify(&blog, &url, &after_change).is_err());
        let other_tenant = password_reset_key(secret, "shop", "hash-1");
        let shop = password_reset_scope("shop");
        assert!(rustango::auth_flows::PasswordReset::verify(&shop, &url, &other_tenant).is_err());
    }

    #[test]
    fn parse_matrix_form_translates_brackets_to_codenames() {
        let mut form = std::collections::HashMap::new();
        form.insert("name".to_owned(), "Editor".to_owned());
        form.insert("perm[pages][view]".to_owned(), "1".to_owned());
        form.insert("perm[pages][add]".to_owned(), "1".to_owned());
        form.insert("perm[pages][edit]".to_owned(), "1".to_owned());
        // Unchecked → not present in the form. View-only.
        form.insert("perm[history][view]".to_owned(), "1".to_owned());
        // Inapplicable cell — `history` doesn't accept `edit`. Must be
        // dropped silently.
        form.insert("perm[history][edit]".to_owned(), "1".to_owned());
        // Off-state checkbox shouldn't produce a grant.
        form.insert("perm[redirects][delete]".to_owned(), "0".to_owned());
        // Unknown resource key — dropped.
        form.insert("perm[bogus][view]".to_owned(), "1".to_owned());
        // Malformed key — dropped.
        form.insert("perm[pages]".to_owned(), "1".to_owned());

        let codenames = parse_matrix_form(&form);
        assert!(codenames.contains(&"cms_page.view".to_owned()));
        assert!(codenames.contains(&"cms_page.add".to_owned()));
        assert!(codenames.contains(&"cms_page.edit".to_owned()));
        assert!(codenames.contains(&"cms_history.view".to_owned()));
        assert!(!codenames.contains(&"cms_history.edit".to_owned()));
        assert!(!codenames.contains(&"cms_redirect.delete".to_owned()));
        // No bogus / malformed leakage.
        assert!(!codenames.iter().any(|c| c.contains("bogus")));
    }

    #[test]
    fn split_matrix_codenames_isolates_legacy() {
        let granted = vec![
            "cms_page.view".to_owned(),
            "cms_page.edit".to_owned(),
            "rustango_roles.view".to_owned(),
            "acme_custom.do_thing".to_owned(),
            "free_form_string".to_owned(),
        ];
        let (matched, legacy) = split_matrix_codenames(&granted);
        assert!(matched.contains(&"cms_page.view".to_owned()));
        assert!(matched.contains(&"cms_page.edit".to_owned()));
        assert!(matched.contains(&"rustango_roles.view".to_owned()));
        assert!(legacy.contains(&"acme_custom.do_thing".to_owned()));
        assert!(legacy.contains(&"free_form_string".to_owned()));
    }

    #[test]
    fn build_permission_matrix_marks_granted_cells() {
        let granted = vec![
            "cms_page.view".to_owned(),
            "cms_page.add".to_owned(),
            "cms_media.view".to_owned(),
        ];
        let matrix = build_permission_matrix(&granted);
        let arr = matrix.as_array().expect("matrix is an array of sections");
        // Find the Content section → Pages row → granted list.
        let pages_row = arr
            .iter()
            .flat_map(|s| s["rows"].as_array().unwrap().iter())
            .find(|r| r["key"] == "pages")
            .expect("pages row present");
        let granted_actions: Vec<&str> = pages_row["granted"]
            .as_array()
            .unwrap()
            .iter()
            .map(|v| v.as_str().unwrap())
            .collect();
        assert!(granted_actions.contains(&"view"));
        assert!(granted_actions.contains(&"add"));
        assert!(!granted_actions.contains(&"edit"));
        assert!(!granted_actions.contains(&"delete"));
    }

    #[test]
    fn round_trip_form_to_codenames_to_matrix() {
        // Submit a matrix; reverse-render it; expect every checked cell
        // to come back as `granted`.
        let mut form = std::collections::HashMap::new();
        form.insert("perm[pages][view]".to_owned(), "1".to_owned());
        form.insert("perm[pages][publish]".to_owned(), "1".to_owned());
        form.insert("perm[media][view]".to_owned(), "1".to_owned());
        let codenames = parse_matrix_form(&form);
        let matrix = build_permission_matrix(&codenames);

        let pages = matrix
            .as_array()
            .unwrap()
            .iter()
            .flat_map(|s| s["rows"].as_array().unwrap().iter())
            .find(|r| r["key"] == "pages")
            .unwrap();
        let granted: Vec<&str> = pages["granted"]
            .as_array()
            .unwrap()
            .iter()
            .map(|v| v.as_str().unwrap())
            .collect();
        assert_eq!(granted, vec!["view", "publish"]);
    }

    // -------- #117 InlinePanel extractor --------

    fn sample_spec() -> crate::page_type::InlinePanelSpec {
        crate::page_type::InlinePanelSpec::new(
            "team",
            "Team",
            vec![
                crate::page_type::ExtensionField {
                    name: "name".to_owned(),
                    label: "Name".to_owned(),
                    kind: crate::page_type::ExtensionFieldKind::Text,
                    value: String::new(),
                    help: String::new(),
                    max_length: None,
                },
                crate::page_type::ExtensionField {
                    name: "role".to_owned(),
                    label: "Role".to_owned(),
                    kind: crate::page_type::ExtensionFieldKind::Text,
                    value: String::new(),
                    help: String::new(),
                    max_length: None,
                },
            ],
        )
    }

    #[test]
    fn inline_extractor_groups_by_row_index() {
        let spec = sample_spec();
        let mut form = std::collections::HashMap::new();
        form.insert("inline__team__0__name".to_owned(), "Ada".to_owned());
        form.insert("inline__team__0__role".to_owned(), "Engineer".to_owned());
        form.insert("inline__team__1__name".to_owned(), "Bob".to_owned());
        form.insert("inline__team__1__role".to_owned(), "Designer".to_owned());
        let rows = extract_inline_panel_rows(&form, &spec);
        assert_eq!(rows.len(), 2);
        assert_eq!(rows[0]["name"], "Ada");
        assert_eq!(rows[1]["role"], "Designer");
    }

    #[test]
    fn inline_extractor_drops_tombstoned_rows() {
        let spec = sample_spec();
        let mut form = std::collections::HashMap::new();
        form.insert("inline__team__0__name".to_owned(), "Ada".to_owned());
        form.insert("inline__team__1__name".to_owned(), "Bob".to_owned());
        form.insert("inline__team__1____deleted".to_owned(), "1".to_owned());
        let rows = extract_inline_panel_rows(&form, &spec);
        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0]["name"], "Ada");
    }

    #[test]
    fn inline_extractor_ignores_unknown_field_names() {
        let spec = sample_spec();
        let mut form = std::collections::HashMap::new();
        form.insert("inline__team__0__name".to_owned(), "Ada".to_owned());
        form.insert("inline__team__0__bogus".to_owned(), "x".to_owned());
        let rows = extract_inline_panel_rows(&form, &spec);
        assert_eq!(rows.len(), 1);
        assert!(!rows[0].contains_key("bogus"));
    }

    #[test]
    fn inline_extractor_ignores_unrelated_form_keys() {
        let spec = sample_spec();
        let mut form = std::collections::HashMap::new();
        form.insert("title".to_owned(), "irrelevant".to_owned());
        form.insert("inline__other_panel__0__name".to_owned(), "x".to_owned());
        let rows = extract_inline_panel_rows(&form, &spec);
        assert!(rows.is_empty());
    }

    /// The page-edit submit handler funnels the raw posted
    /// form through an allow-list before re-deserializing as
    /// `PageForm`. The bug guarded here was that schedule fields
    /// (`go_live_at`, `expire_at`) and `show_in_menus` were missing
    /// from that allow-list, so any datetime the editor set on the
    /// Promote tab was silently dropped on save — and never made it
    /// to the scheduled-pages report. Lock these in so a future
    /// trim of the array can't reintroduce the regression.
    #[test]
    fn page_form_canonical_keys_include_schedule_and_menu_fields() {
        assert!(
            PAGE_FORM_CANONICAL_KEYS.contains(&"go_live_at"),
            "go_live_at must be in canonical keys — see #268"
        );
        assert!(
            PAGE_FORM_CANONICAL_KEYS.contains(&"expire_at"),
            "expire_at must be in canonical keys — see #268"
        );
        assert!(
            PAGE_FORM_CANONICAL_KEYS.contains(&"show_in_menus"),
            "show_in_menus must be in canonical keys — see #268"
        );
    }
}

#[cfg(all(test, feature = "sqlite"))]
mod media_picker_tests {
    //! `media_picker_payload` filtering / pagination / collection-tree
    //! tests over an in-memory sqlite pool (mirrors redirect.rs's
    //! `mem_pool` pattern).
    use super::{media_picker_payload, MediaPickerQuery};
    use crate::media::{Media, MediaCollection};
    use rustango::core::Model as _;
    use rustango::sql::{Auto, FetcherPool as _, Pool};

    async fn mem_pool() -> Pool {
        let pool = Pool::connect("sqlite::memory:")
            .await
            .expect("in-memory sqlite");
        for schema in [&Media::SCHEMA, &MediaCollection::SCHEMA] {
            let ddl = rustango::migrate::ddl::create_table_if_not_exists_sql_with_dialect(
                pool.dialect(),
                schema,
            );
            for stmt in ddl.split(';').map(str::trim).filter(|s| !s.is_empty()) {
                rustango::sql::raw_execute_pool(&pool, stmt, Vec::new())
                    .await
                    .expect("ddl");
            }
        }
        pool
    }

    async fn mk_collection(pool: &Pool, name: &str, parent_id: Option<i64>, sort: i32) -> i64 {
        MediaCollection {
            id: Auto::Unset,
            name: name.to_owned(),
            parent_id,
            sort_order: sort,
            created_at: Auto::Unset,
        }
        .insert_pool(pool)
        .await
        .expect("collection insert");
        MediaCollection::objects()
            .fetch(pool)
            .await
            .expect("fetch")
            .into_iter()
            .find(|c| c.name == name)
            .and_then(|c| c.id.get().copied())
            .expect("collection id")
    }

    async fn mk_media(
        pool: &Pool,
        title: &str,
        filename: &str,
        kind: &str,
        collection_id: Option<i64>,
        alt: &str,
    ) {
        Media {
            id: Auto::Unset,
            filename: filename.to_owned(),
            content_hash: format!("{:0>64}", title.len()),
            mime: if kind == "image" {
                "image/png"
            } else {
                "application/pdf"
            }
            .to_owned(),
            size: 123,
            kind: kind.to_owned(),
            width: (kind == "image").then_some(100),
            height: (kind == "image").then_some(80),
            storage_key: format!("k-{filename}"),
            title: title.to_owned(),
            alt_text: alt.to_owned(),
            description: String::new(),
            uploaded_by: None,
            collection_id,
            focal_point_x: None,
            focal_point_y: None,
            uploaded_at: Auto::Unset,
        }
        .insert_pool(pool)
        .await
        .expect("media insert");
    }

    #[tokio::test]
    async fn default_kind_excludes_documents() {
        let pool = mem_pool().await;
        mk_media(&pool, "Photo", "p.png", "image", None, "").await;
        mk_media(&pool, "Spec", "s.pdf", "document", None, "").await;
        let body = media_picker_payload(&pool, &MediaPickerQuery::default())
            .await
            .expect("payload");
        let items = body["items"].as_array().expect("items");
        assert_eq!(items.len(), 1);
        assert_eq!(items[0]["kind"], "image");
        // kind=any opts back in.
        let q = MediaPickerQuery {
            kind: Some("any".to_owned()),
            ..Default::default()
        };
        let body = media_picker_payload(&pool, &q).await.expect("payload");
        assert_eq!(body["items"].as_array().expect("items").len(), 2);
    }

    #[tokio::test]
    async fn collection_zero_means_uncategorized_only() {
        let pool = mem_pool().await;
        let cid = mk_collection(&pool, "Root", None, 0).await;
        mk_media(&pool, "In", "in.png", "image", Some(cid), "").await;
        mk_media(&pool, "Loose", "loose.png", "image", None, "").await;
        let q = MediaPickerQuery {
            collection: Some(0),
            ..Default::default()
        };
        let body = media_picker_payload(&pool, &q).await.expect("payload");
        let items = body["items"].as_array().expect("items");
        assert_eq!(items.len(), 1);
        assert_eq!(items[0]["title"], "Loose");
        assert_eq!(body["uncategorized_count"], 1);
        // Exact collection id narrows to its members.
        let q = MediaPickerQuery {
            collection: Some(cid),
            ..Default::default()
        };
        let body = media_picker_payload(&pool, &q).await.expect("payload");
        assert_eq!(body["items"].as_array().expect("items").len(), 1);
        assert_eq!(body["items"][0]["title"], "In");
    }

    #[tokio::test]
    async fn search_matches_alt_text() {
        let pool = mem_pool().await;
        mk_media(
            &pool,
            "IMG_0001",
            "a.png",
            "image",
            None,
            "sunset glow over harbour",
        )
        .await;
        mk_media(&pool, "IMG_0002", "b.png", "image", None, "office desk").await;
        let q = MediaPickerQuery {
            q: Some("Sunset".to_owned()),
            ..Default::default()
        };
        let body = media_picker_payload(&pool, &q).await.expect("payload");
        let items = body["items"].as_array().expect("items");
        assert_eq!(items.len(), 1);
        assert_eq!(items[0]["title"], "IMG_0001");
    }

    #[tokio::test]
    async fn pagination_math_and_has_more() {
        let pool = mem_pool().await;
        for i in 0..5 {
            mk_media(
                &pool,
                &format!("M{i}"),
                &format!("m{i}.png"),
                "image",
                None,
                "",
            )
            .await;
        }
        let q = MediaPickerQuery {
            limit: Some(2),
            offset: Some(0),
            ..Default::default()
        };
        let body = media_picker_payload(&pool, &q).await.expect("payload");
        assert_eq!(body["items"].as_array().expect("items").len(), 2);
        assert_eq!(body["total"], 5);
        assert_eq!(body["has_more"], true);
        let q = MediaPickerQuery {
            limit: Some(2),
            offset: Some(4),
            ..Default::default()
        };
        let body = media_picker_payload(&pool, &q).await.expect("payload");
        assert_eq!(body["items"].as_array().expect("items").len(), 1);
        assert_eq!(body["has_more"], false);
        // Offset past the end is clamped, not a panic.
        let q = MediaPickerQuery {
            limit: Some(2),
            offset: Some(99),
            ..Default::default()
        };
        let body = media_picker_payload(&pool, &q).await.expect("payload");
        assert_eq!(body["items"].as_array().expect("items").len(), 0);
    }

    #[tokio::test]
    async fn collections_come_back_in_dfs_order_with_depth() {
        let pool = mem_pool().await;
        let root_a = mk_collection(&pool, "Alpha", None, 0).await;
        let _sub = mk_collection(&pool, "Alpha Child", Some(root_a), 0).await;
        let _root_b = mk_collection(&pool, "Beta", None, 1).await;
        mk_media(&pool, "In A", "a.png", "image", Some(root_a), "").await;
        let body = media_picker_payload(&pool, &MediaPickerQuery::default())
            .await
            .expect("payload");
        let cols = body["collections"].as_array().expect("collections");
        let names: Vec<&str> = cols.iter().map(|c| c["name"].as_str().unwrap()).collect();
        assert_eq!(names, ["Alpha", "Alpha Child", "Beta"]);
        assert_eq!(cols[0]["depth"], 0);
        assert_eq!(cols[1]["depth"], 1);
        assert_eq!(cols[2]["depth"], 0);
        assert_eq!(cols[0]["count"], 1);
        assert_eq!(cols[2]["count"], 0);
    }

    #[tokio::test]
    async fn ids_lookup_resolves_any_kind() {
        // The label-hydration pass resolves exact ids — including
        // non-image kinds, which the default filter would drop.
        let pool = mem_pool().await;
        mk_media(&pool, "Photo", "p.png", "image", None, "").await;
        mk_media(&pool, "Spec", "s.pdf", "document", None, "").await;
        let all = media_picker_payload(
            &pool,
            &MediaPickerQuery {
                kind: Some("any".to_owned()),
                ..Default::default()
            },
        )
        .await
        .expect("payload");
        let ids: Vec<i64> = all["items"]
            .as_array()
            .unwrap()
            .iter()
            .map(|i| i["id"].as_i64().unwrap())
            .collect();
        let q = MediaPickerQuery {
            ids: Some(
                ids.iter()
                    .map(|i| i.to_string())
                    .collect::<Vec<_>>()
                    .join(","),
            ),
            ..Default::default()
        };
        let body = media_picker_payload(&pool, &q).await.expect("payload");
        let items = body["items"].as_array().expect("items");
        assert_eq!(
            items.len(),
            2,
            "document resolves too when ids are explicit"
        );
        // Explicit kind still narrows an ids lookup.
        let q = MediaPickerQuery {
            ids: Some(
                ids.iter()
                    .map(|i| i.to_string())
                    .collect::<Vec<_>>()
                    .join(","),
            ),
            kind: Some("image".to_owned()),
            ..Default::default()
        };
        let body = media_picker_payload(&pool, &q).await.expect("payload");
        assert_eq!(body["items"].as_array().expect("items").len(), 1);
    }

    #[tokio::test]
    async fn image_items_carry_rendition_thumb_urls() {
        let pool = mem_pool().await;
        mk_media(&pool, "Pic", "p.png", "image", None, "").await;
        let body = media_picker_payload(&pool, &MediaPickerQuery::default())
            .await
            .expect("payload");
        let item = &body["items"][0];
        let thumb = item["thumb_url"].as_str().expect("thumb_url");
        assert!(
            thumb.starts_with("/__media__/fill-320x320"),
            "spec-first URL: {thumb}"
        );
        assert!(thumb.contains("v="), "cache-buster present: {thumb}");
        let preview = item["preview_url"].as_str().expect("preview_url");
        assert!(preview.contains("max-640x480"), "{preview}");
    }
}

#[cfg(all(test, feature = "sqlite"))]
mod forms_list_tests {
    //! `forms_list_rows` row-model tests over an in-memory sqlite pool.
    use super::forms_list_rows;
    use crate::forms::submit::FormEntry;
    use crate::snippet::Snippet;
    use rustango::core::Model as _;
    use rustango::sql::{Auto, FetcherPool as _, Pool};

    async fn mem_pool() -> Pool {
        let pool = Pool::connect("sqlite::memory:")
            .await
            .expect("in-memory sqlite");
        for schema in [&Snippet::SCHEMA, &FormEntry::SCHEMA] {
            let ddl = rustango::migrate::ddl::create_table_if_not_exists_sql_with_dialect(
                pool.dialect(),
                schema,
            );
            for stmt in ddl.split(';').map(str::trim).filter(|s| !s.is_empty()) {
                rustango::sql::raw_execute_pool(&pool, stmt, Vec::new())
                    .await
                    .expect("ddl");
            }
        }
        pool
    }

    async fn mk_form(pool: &Pool, slug: &str, title: &str, fields: usize) -> i64 {
        let field_json: Vec<serde_json::Value> = (0..fields)
            .map(|i| serde_json::json!({"id": format!("f{i}"), "key": format!("k{i}"), "type": "text", "label": "F"}))
            .collect();
        Snippet {
            id: Auto::Unset,
            type_name: "form".to_owned(),
            folder_path: String::new(),
            slug: slug.to_owned(),
            title: title.to_owned(),
            body_markdown: String::new(),
            data: serde_json::json!({
                "settings": {},
                "pages": [{"id": "p1", "sections": [{"id": "s1", "rows": [{"id": "r1",
                    "columns": [{"id": "c1", "width": 12, "fields": field_json}]}]}]}]
            }),
            created_at: Auto::Unset,
            updated_at: Auto::Unset,
        }
        .insert_pool(pool)
        .await
        .expect("form insert");
        Snippet::objects()
            .fetch(pool)
            .await
            .expect("fetch")
            .into_iter()
            .find(|s| s.slug == slug)
            .and_then(|s| s.id.get().copied())
            .expect("form id")
    }

    async fn mk_entry(pool: &Pool, form_id: i64) {
        FormEntry {
            id: Auto::Unset,
            form_snippet_id: form_id,
            form_version: 1,
            source_page_id: 0,
            source_url: "/contact".to_owned(),
            data_json: serde_json::json!({"k0": "hi"}),
            locale: "en".to_owned(),
            ip: String::new(),
            submitted_at: Auto::Unset,
        }
        .insert_pool(pool)
        .await
        .expect("entry insert");
    }

    #[tokio::test]
    async fn rows_carry_field_and_submission_counts() {
        let pool = mem_pool().await;
        let a = mk_form(&pool, "contact", "Contact", 3).await;
        let b = mk_form(&pool, "rsvp", "RSVP", 1).await;
        mk_entry(&pool, a).await;
        mk_entry(&pool, a).await;
        let rows = forms_list_rows(&pool).await.expect("rows");
        assert_eq!(rows.len(), 2);
        let by_slug = |slug: &str| rows.iter().find(|r| r["slug"] == slug).expect("row");
        let ra = by_slug("contact");
        assert_eq!(ra["fields"], 3);
        assert_eq!(ra["submissions"], 2);
        assert!(
            ra["last_submission"].as_str().unwrap().len() >= 16,
            "has a timestamp"
        );
        assert_eq!(ra["build_url"], format!("/cms-admin/forms/{a}/build"));
        assert_eq!(
            ra["submissions_url"],
            format!("/cms-admin/forms/{a}/submissions")
        );
        let rb = by_slug("rsvp");
        assert_eq!(rb["fields"], 1);
        assert_eq!(rb["submissions"], 0);
        assert_eq!(rb["last_submission"], "");
        assert_eq!(rb["delete_url"], format!("/cms-admin/library/{b}/delete"));
    }

    #[tokio::test]
    async fn only_form_snippets_appear() {
        let pool = mem_pool().await;
        mk_form(&pool, "contact", "Contact", 1).await;
        Snippet {
            id: Auto::Unset,
            type_name: "branding".to_owned(),
            folder_path: String::new(),
            slug: "site-branding".to_owned(),
            title: "Branding".to_owned(),
            body_markdown: String::new(),
            data: serde_json::json!({}),
            created_at: Auto::Unset,
            updated_at: Auto::Unset,
        }
        .insert_pool(&pool)
        .await
        .expect("snippet insert");
        let rows = forms_list_rows(&pool).await.expect("rows");
        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0]["slug"], "contact");
    }

    #[test]
    fn user_admin_is_superuser_only() {
        // #642 — user management sets is_superuser / active / roles from
        // the form, so the whole surface is restricted to superusers.
        // A superuser proceeds; a signed-in non-superuser is bounced to
        // no-access; an anonymous caller to login. A regression that drops
        // the guard (or lets a non-superuser through) fails here.
        assert_eq!(super::user_admin_denial_target(Some(true)), None);
        assert_eq!(
            super::user_admin_denial_target(Some(false)),
            Some("/cms-admin/no-access")
        );
        assert_eq!(super::user_admin_denial_target(None), Some("/login"));
    }

    #[test]
    fn mcp_key_admin_handlers_enforce_the_superuser_gate() {
        // #646 — the two /cms-admin/users/{id}/mcp-keys handlers must carry
        // the same superuser gate as every other user-admin handler. They
        // once gated on "a session exists" only, so a non-superuser could
        // mint a key bounded by a superuser's entitlement. Pin the wiring so
        // a future edit can't silently drop it again (the gap that untested
        // wiring hid the first time). Runs in the default suite — no DB.
        let src = include_str!("handlers.rs");
        for name in ["users_mcp_key_create", "users_mcp_key_revoke"] {
            let start = src
                .find(&format!("pub async fn {name}("))
                .unwrap_or_else(|| panic!("handler {name} not found"));
            // Bound the scan to this fn's body: up to the next top-level fn.
            let rest = &src[start + 1..];
            let end = rest.find("\npub async fn ").unwrap_or(rest.len());
            assert!(
                rest[..end].contains("require_superuser_for_user_admin(session_user.as_ref())"),
                "{name} is missing the superuser gate (#646)"
            );
        }
    }
}

#[cfg(test)]
mod password_reset_key_tests {
    use super::{password_reset_key, password_reset_scope};
    use rustango::auth_flows::PasswordReset;
    use std::time::Duration;

    const SECRET: &[u8] = b"process-wide-signing-secret";
    const BASE: &str = "/cms-admin/password-reset/confirm";

    fn link(tenant: &str, hash: &str) -> String {
        PasswordReset::issue(
            &password_reset_scope(tenant),
            BASE,
            1,
            &password_reset_key(SECRET, tenant, hash),
            Duration::from_secs(3600),
        )
    }

    #[test]
    fn a_link_verifies_for_the_tenant_and_password_it_was_issued_for() {
        let url = link("acme", "$argon2id$v=19$old");
        let key = password_reset_key(SECRET, "acme", "$argon2id$v=19$old");
        assert_eq!(PasswordReset::verify(&password_reset_scope("acme"), &url, &key).ok(), Some(1));
    }

    #[test]
    fn a_link_issued_on_one_tenant_does_not_verify_on_another() {
        // User ids restart at 1 per tenant: this is the takeover in #673.
        let url = link("acme", "$argon2id$v=19$same");
        let other = password_reset_key(SECRET, "globex", "$argon2id$v=19$same");
        assert!(PasswordReset::verify(&password_reset_scope("globex"), &url, &other).is_err());
    }

    #[test]
    fn a_link_stops_verifying_once_the_password_changes() {
        let url = link("acme", "$argon2id$v=19$before");
        let after = password_reset_key(SECRET, "acme", "$argon2id$v=19$after");
        assert!(PasswordReset::verify(&password_reset_scope("acme"), &url, &after).is_err());
    }
}

#[cfg(all(test, feature = "sqlite"))]
mod publish_gate_tests {
    //! A save that puts a page live takes the publish right,
    //! judged on the status it lands on after go-live derivation.
    use super::{apply_page_edit, PageEditActor, PageEditRefusal};
    use crate::page::{Page, PageStatus};
    use crate::page_type_model::PageType;
    use rustango::core::Column as _;
    use rustango::sql::{Auto, Pool};

    async fn mem_pool() -> Pool {
        let pool = Pool::connect("sqlite::memory:").await.expect("in-memory sqlite");
        for entry in inventory::iter::<rustango::core::ModelEntry> {
            if !entry.schema.managed {
                continue;
            }
            let ddl = rustango::migrate::ddl::create_table_if_not_exists_sql_with_dialect(
                pool.dialect(),
                entry.schema,
            );
            for stmt in ddl.split(';').map(str::trim).filter(|s| !s.is_empty()) {
                rustango::sql::raw_execute_pool(&pool, stmt, Vec::new())
                    .await
                    .expect("ddl");
            }
        }
        pool
    }

    async fn draft(pool: &Pool) -> Page {
        let mut pt = PageType {
            id: Auto::Unset,
            app_label: "cms".to_owned(),
            type_name: "TestPage".to_owned(),
            verbose_name: "Test page".to_owned(),
            default_template: "page.html".to_owned(),
            view_mode: "auto".to_owned(),
            is_creatable: true,
            allowed_parent_types: serde_json::json!([]),
            allowed_child_types: serde_json::json!([]),
            workflow: String::new(),
            created_at: Auto::Unset,
            updated_at: Auto::Unset,
        };
        pt.save_pool(pool).await.expect("page type");
        let mut p = draft_template(pt.id.get().copied().expect("type id"));
        p.save_pool(pool).await.expect("page");
        p
    }

    fn draft_template(type_id: i64) -> Page {
        Page {
            id: Auto::Unset,
            page_type_id: type_id,
            title: "Draft".to_owned(),
            slug: "draft".to_owned(),
            path: "0001/".to_owned(),
            url_path: "/draft".to_owned(),
            preview_path: String::new(),
            template_override: String::new(),
            depth: 1,
            parent_id: None,
            locale_variant_of: None,
            alias_of: None,
            theme_id: None,
            sort_order: 0,
            status: PageStatus::Draft.as_str().to_owned(),
            published_at: None,
            last_published_at: None,
            go_live_at: None,
            expire_at: None,
            seo_title: String::new(),
            seo_description: String::new(),
            robots_index: true,
            sitemap_priority: 0.5,
            show_in_menus: true,
            og_title: String::new(),
            og_description: String::new(),
            og_image_media_id: None,
            twitter_card: "summary".to_owned(),
            notification_pre_published_sent: false,
            created_at: Auto::Unset,
            updated_at: Auto::Unset,
        }
    }

    async fn save(
        pool: &Pool,
        page: &Page,
        status: &str,
        go_live_days: Option<i64>,
        superuser: bool,
    ) -> Result<String, &'static str> {
        let mut form = crate::page_form::prefill(pool, page).await;
        form.insert("status".into(), status.to_owned());
        match go_live_days {
            Some(d) => form.insert(
                "go_live_at".into(),
                (chrono::Utc::now() + chrono::Duration::days(d))
                    .format("%Y-%m-%dT%H:%M")
                    .to_string(),
            ),
            None => form.remove("go_live_at"),
        };
        // A user with no roles: edit reached the handler, publish is not held.
        let actor = PageEditActor { id: 999, username: "editor".to_owned(), is_superuser: superuser };
        let invalidator: std::sync::Arc<dyn crate::cache_invalidate::PageCacheInvalidator> =
            std::sync::Arc::new(crate::cache_invalidate::Noop);
        let id = page.id.get().copied().expect("id");
        match apply_page_edit(pool, "t", &invalidator, None, "", false, id, &form, Some(&actor))
            .await
            .expect("save")
        {
            Ok(o) => Ok(o.page.status),
            Err(PageEditRefusal::PublishDenied) => Err("publish denied"),
            Err(PageEditRefusal::ReviewRequired { .. }) => Err("review required"),
            Err(_) => Err("other refusal"),
        }
    }

    async fn stored_status(pool: &Pool, page: &Page) -> String {
        Page::objects()
            .where_(Page::id.eq(page.id.get().copied().expect("id")))
            .first(pool)
            .await
            .expect("query")
            .expect("page")
            .status
    }

    async fn stored_theme(pool: &Pool, page: &Page) -> Option<i64> {
        Page::objects()
            .where_(Page::id.eq(page.id.get().copied().expect("id")))
            .first(pool)
            .await
            .expect("query")
            .expect("page")
            .theme_id
    }

    /// #902 — the editor has no theme input, so its save must keep a theme
    /// set some other way; only a posted (empty) `theme_id` changes it.
    #[tokio::test]
    async fn an_editor_save_keeps_the_page_theme() {
        let pool = mem_pool().await;
        let mut page = draft(&pool).await;
        let mut theme = crate::theme::tests::fixture_theme();
        theme.save_pool(&pool).await.expect("theme");
        let theme_id = theme.id.get().copied().expect("theme id");
        page.theme_id = Some(theme_id);
        page.save_pool(&pool).await.expect("set theme");

        let invalidator: std::sync::Arc<dyn crate::cache_invalidate::PageCacheInvalidator> =
            std::sync::Arc::new(crate::cache_invalidate::Noop);
        let id = page.id.get().copied().expect("id");
        let mut form = crate::page_form::prefill(&pool, &page).await;
        form.remove("theme_id");
        assert!(apply_page_edit(&pool, "t", &invalidator, None, "", false, id, &form, None).await.expect("save").is_ok());
        assert_eq!(stored_theme(&pool, &page).await, Some(theme_id), "a form without the field keeps it");

        form.insert("theme_id".into(), String::new());
        assert!(apply_page_edit(&pool, "t", &invalidator, None, "", false, id, &form, None).await.expect("save").is_ok());
        assert_eq!(stored_theme(&pool, &page).await, None, "posted empty clears it");
    }

    /// Publish a page-builder body with one text field, `headline`.
    async fn publish_headline_schema(pool: &Pool, type_id: i64) {
        let document = serde_json::json!({
            "nodes": [{ "kind": "field", "key": "headline", "label": "Headline", "widget": "text" }]
        });
        rustango::sql::raw_execute_pool(
            pool,
            "INSERT INTO cms_page_type_schema (page_type_id, status, version, document) \
             VALUES (?, 'published', 1, ?)",
            vec![
                rustango::core::SqlValue::I64(type_id),
                rustango::core::SqlValue::Json(document),
            ],
        )
        .await
        .expect("schema");
    }

    /// #857 — the new-page form carries the type's own fields, not only
    /// Title, Slug and Status.
    #[tokio::test]
    async fn the_new_page_form_shows_the_type_fields() {
        let pool = mem_pool().await;
        let type_id = draft(&pool).await.page_type_id;
        publish_headline_schema(&pool, type_id).await;
        let mut tera = tera::Tera::default();
        crate::admin::register_templates(&mut tera).expect("templates");
        let mut ctx = tera::Context::new();
        super::insert_type_fields(&mut ctx, &tera, &pool, None, Vec::new(), 0, type_id, None).await;
        let body = ctx
            .get("builder_body_html")
            .and_then(serde_json::Value::as_str)
            .expect("builder body on the new form")
            .to_owned();
        assert!(body.contains("pb__headline"), "headline input missing: {body}");
    }

    /// #857 — what the new-page form posts for the type's fields is saved
    /// with the page, not dropped until a second save.
    #[tokio::test]
    async fn creating_a_page_saves_its_type_fields() {
        let pool = mem_pool().await;
        let page = draft(&pool).await;
        publish_headline_schema(&pool, page.page_type_id).await;
        let page_type = PageType::objects()
            .where_(PageType::id.eq(page.page_type_id))
            .first(&pool)
            .await
            .expect("type query");
        let id = page.id.get().copied().expect("id");
        let form = std::collections::HashMap::from([("pb__headline".to_owned(), "Hello".to_owned())]);
        super::save_type_content(&pool, page_type.as_ref(), page.page_type_id, id, &form)
            .await
            .expect("save");
        let data = crate::page_builder::model::data_for_page(&pool, id)
            .await
            .expect("builder data query")
            .expect("builder data row")
            .data;
        assert_eq!(data["headline"], "Hello", "{data}");
    }

    #[tokio::test]
    async fn going_live_by_any_route_needs_the_publish_right() {
        let pool = mem_pool().await;
        let page = draft(&pool).await;
        for (status, go_live) in [
            ("archived", None),
            ("published", None),
            // Draft plus a future go-live is parked as scheduled.
            ("draft", Some(1)),
            // Scheduled with a go-live already past: the sweep publishes it.
            ("scheduled", Some(-1)),
        ] {
            assert_eq!(
                save(&pool, &page, status, go_live, false).await,
                Err("publish denied"),
                "{status} / {go_live:?}"
            );
            assert_eq!(stored_status(&pool, &page).await, "draft", "nothing written");
        }
        assert_eq!(
            save(&pool, &page, "scheduled", None, false).await,
            Ok("draft".to_owned()),
            "scheduled without a date is parked as a draft, so it is allowed"
        );
        assert_eq!(
            save(&pool, &page, "draft", Some(1), true).await,
            Ok("scheduled".to_owned()),
            "the publish right schedules"
        );
    }

    /// A page type with a review workflow goes live only through it: a
    /// save straight to Published is refused for everyone but a
    /// superuser — the owner's review can't be skipped.
    /// Bind `type_id` to a one-step "Owner review" workflow.
    async fn bind_review(pool: &Pool, type_id: i64, reapproval: bool) -> crate::workflow::Workflow {
        let mut wf = crate::workflow::Workflow {
            id: Auto::Unset,
            name: "Owner review".to_owned(),
            description: String::new(),
            active: true,
            require_reapproval_on_edit: reapproval,
            created_at: Auto::Unset,
            updated_at: Auto::Unset,
        };
        wf.save_pool(pool).await.expect("workflow");
        rustango::sql::raw_execute_pool(pool, "INSERT INTO rustango_roles (id, name, description) VALUES (1, 'Owner', '')", Vec::new())
            .await
            .expect("role");
        let mut task = crate::workflow::WorkflowTask {
            id: Auto::Unset,
            workflow_id: wf.id.get().copied().expect("id"),
            name: "Owner checks".to_owned(),
            role_id: 1,
            sort_order: 10,
            kind: "group_approval".to_owned(),
            webhook_url: String::new(),
        };
        task.save_pool(pool).await.expect("task");
        let mut pt = PageType::objects()
            .where_(PageType::id.eq(type_id))
            .first(pool)
            .await
            .expect("q")
            .expect("type");
        pt.workflow = "Owner review".to_owned();
        pt.save_pool(pool).await.expect("bind");
        wf
    }

    #[tokio::test]
    async fn a_review_workflow_cannot_be_published_around() {
        let pool = mem_pool().await;
        let page = draft(&pool).await;
        let mut wf = bind_review(&pool, page.page_type_id, false).await;

        assert_eq!(save(&pool, &page, "published", None, false).await, Err("review required"));
        assert_eq!(save(&pool, &page, "draft", Some(1), false).await, Err("review required"), "nor scheduled");
        assert_eq!(stored_status(&pool, &page).await, "draft");
        assert_eq!(save(&pool, &page, "draft", None, false).await, Ok("draft".to_owned()), "drafts still save");
        assert_eq!(save(&pool, &page, "published", None, true).await, Ok("published".to_owned()), "the owner can");

        // Renaming the workflow moves the page type along: still reviewed.
        let other = draft_named(&pool, page.page_type_id, "second").await;
        wf.name = "Studio review".to_owned();
        wf.save_pool(&pool).await.expect("rename");
        assert_eq!(crate::workflow::rebind_page_types(&pool, "Owner review", "Studio review").await.expect("rebind"), 1);
        assert_eq!(save(&pool, &other, "published", None, false).await, Err("review required"));
    }

    /// Save `page` with a new title; `actor` None is the CMS's own save.
    async fn retitle(pool: &Pool, page: &Page, title: &str, actor: Option<&PageEditActor>) -> super::PageEditOutcome {
        let mut form = crate::page_form::prefill(pool, page).await;
        form.insert("title".into(), title.to_owned());
        let invalidator: std::sync::Arc<dyn crate::cache_invalidate::PageCacheInvalidator> =
            std::sync::Arc::new(crate::cache_invalidate::Noop);
        let id = page.id.get().copied().expect("id");
        match apply_page_edit(pool, "t", &invalidator, None, "", false, id, &form, actor).await.expect("save") {
            Ok(o) => o,
            Err(_) => panic!("refused"),
        }
    }

    async fn stored_title(pool: &Pool, page: &Page) -> String {
        Page::objects()
            .where_(Page::id.eq(page.id.get().copied().expect("id")))
            .first(pool)
            .await
            .expect("query")
            .expect("page")
            .title
    }

    /// With re-approval on, an editor's save of a live page waits for the
    /// review: visitors keep the live version until the approval applies
    /// it; cancelling the review drops it; a superuser's save supersedes it.
    #[tokio::test]
    async fn a_live_page_edit_waits_for_review() {
        let pool = mem_pool().await;
        let page = draft(&pool).await;
        bind_review(&pool, page.page_type_id, true).await;
        assert_eq!(save(&pool, &page, "published", None, true).await, Ok("published".to_owned()));
        let id = page.id.get().copied().expect("id");
        let mut mia = rustango::tenancy::auth::User {
            id: Auto::Unset,
            username: "mia".to_owned(),
            password_hash: String::new(),
            email: None,
            is_superuser: false,
            active: true,
            created_at: chrono::Utc::now(),
            data: serde_json::json!({}),
            password_changed_at: None,
            sessions_revoked_at: None,
        };
        mia.save_pool(&pool).await.expect("user");
        let editor = PageEditActor { id: mia.id.get().copied().expect("id"), username: "mia".to_owned(), is_superuser: false };

        let out = retitle(&pool, &page, "Proposed", Some(&editor)).await;
        assert!(out.held_for_review && out.reapproval_kicked);
        assert_eq!(stored_title(&pool, &page).await, "Draft", "the live page is unchanged");
        let held = crate::pending_change::for_page(&pool, id).await.expect("q").expect("held");
        assert_eq!(held.form_map().get("title").map(String::as_str), Some("Proposed"));
        let mut state = crate::workflow::active_state_for_page(&pool, id).await.expect("q").expect("review started");

        // The approval applies it through the normal save path.
        let tasks = crate::workflow::tasks_for(&pool, state.workflow_id).await.expect("tasks");
        crate::workflow::approve_current(&pool, &mut state, &tasks, editor.id, "").await.expect("approve");
        let applied = retitle(&pool, &page, "Proposed", None).await;
        assert!(!applied.held_for_review);
        assert_eq!(stored_title(&pool, &page).await, "Proposed");

        // Cancelling the review drops a held change.
        retitle(&pool, &page, "Second try", Some(&editor)).await;
        let mut state = crate::workflow::active_state_for_page(&pool, id).await.expect("q").expect("review restarted");
        crate::workflow::cancel(&pool, &mut state, editor.id).await.expect("cancel");
        assert!(crate::pending_change::for_page(&pool, id).await.expect("q").is_none());

        // A superuser's direct save supersedes one.
        retitle(&pool, &page, "Third try", Some(&editor)).await;
        let owner = PageEditActor { id: 1, username: "owner".to_owned(), is_superuser: true };
        assert!(!retitle(&pool, &page, "Owner's", Some(&owner)).await.held_for_review);
        assert_eq!(stored_title(&pool, &page).await, "Owner's");
        assert!(crate::pending_change::for_page(&pool, id).await.expect("q").is_none());
    }

    async fn draft_named(pool: &Pool, type_id: i64, slug: &str) -> Page {
        let mut p = draft_template(type_id);
        slug.clone_into(&mut p.slug);
        p.path = "0002/".to_owned();
        p.url_path = format!("/{slug}");
        p.save_pool(pool).await.expect("page");
        p
    }
}

#[cfg(test)]
mod active_upload_tests {
    use super::refuse_active_upload;

    #[test]
    fn page_like_types_are_refused_and_media_is_not() {
        for bad in ["text/html", "application/xhtml+xml", "text/javascript", "application/xml"] {
            assert!(refuse_active_upload(bad).is_err(), "{bad}");
        }
        for ok in ["image/png", "image/svg+xml", "application/pdf", "text/plain", "video/mp4"] {
            assert!(refuse_active_upload(ok).is_ok(), "{ok}");
        }
    }
}