autumn-cli 0.7.0

CLI tool for the Autumn web framework
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
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
//! Field-type DSL parser for `autumn generate`.
//!
//! Turns command-line tokens like `title:String`, `tags:Vec<u8>`, or
//! `published:Option<bool>` into a structured [`Field`] that knows both its
//! Rust type (for the `#[model]` struct) and its SQL type (for the migration).

use autumn_web::config::DatabaseBackend;

use super::GenerateError;
use super::naming;

/// Rust type a `{translatable}` column lowers to (issue #1384).
const TRANSLATED_RUST_TYPE: &str = "autumn_web::i18n::Translated";

/// SQL literal a translatable column defaults to: the empty JSON container.
const EMPTY_TRANSLATION_CONTAINER: &str = "'{}'";

/// The constraint modifiers a field carried in a trailing `{…}` block
/// (`title:String{min=3,max=120}`, `contact:String{email}`,
/// `age:i32{min=0,max=130}`, `post:references{label:title}`).
///
/// These fan out to BOTH a server-side `#[validate(…)]` rule (issue #1388,
/// see [`Field::validation_attrs`]) and a matching client-side HTML5
/// constraint on the generated form input, from a single declaration.
/// `label` is the odd one out: it's a `references`-only display-column
/// override (issue #1146) with no `#[validate]`/HTML5 fan-out.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct FieldConstraints {
    /// `min=N`. For `String`/`Text` this is a `length` minimum; for numeric
    /// fields it is a `range` minimum. Stored as the raw numeric token.
    pub min: Option<String>,
    /// `max=N`. `length` maximum (`String`/`Text`) or `range` maximum
    /// (numeric). Stored as the raw numeric token.
    pub max: Option<String>,
    /// `email` — `#[validate(email)]` + `type="email"`. `String`/`Text` only.
    pub email: bool,
    /// `url` — `#[validate(url)]` + `type="url"`. `String`/`Text` only.
    pub url: bool,
    /// `label:col` — the `references` display column an index/show view and a
    /// `belongs_to` `<select>` label render from (issue #1146). `references`
    /// only; never a `#[validate]`/HTML5 constraint.
    pub label: Option<String>,
    /// `from:col` — the source field a `slug` field auto-derives from on
    /// create when the submitted value is blank (issue #1260), e.g.
    /// `slug:slug{from:title}`. `slug` only; never a `#[validate]`/HTML5
    /// constraint.
    pub from: Option<String>,
    /// `encrypted` / `encrypted:deterministic` — at-rest attribute encryption
    /// (issue #1340), re-emitted as an `#[encrypted(…)]` attribute on the
    /// generated model field. `String`/`Text` only, and never nullable — see
    /// [`EncryptedMode`]. `None` for an ordinary plaintext column.
    pub encrypted: Option<EncryptedMode>,
    /// `scope:col` — the parent foreign-key column a `position` field's
    /// contiguous ordering is scoped to (issue #1358), e.g.
    /// `rank:position{scope:board_id}`. `position` only; `None` means the
    /// position is a single sequence over the whole table. Never a
    /// `#[validate]`/HTML5 constraint.
    pub scope: Option<String>,
    /// `translatable` — per-locale content storage (issue #1384), re-emitted
    /// as a `#[translatable]` attribute on the generated model field and an
    /// `autumn_web::i18n::Translated` Rust type. `String`/`Text` only, never
    /// nullable, and never combined with `{encrypted}`, `:unique`, or a state
    /// machine. `false` for an ordinary monolingual column.
    pub translatable: bool,
}

impl FieldConstraints {
    /// True when no modifier that fans out to a `#[validate(...)]` rule was
    /// declared (issue #1388's `min`/`max`/`email`/`url`).
    #[must_use]
    pub const fn validation_attrs_are_empty(&self) -> bool {
        self.min.is_none() && self.max.is_none() && !self.email && !self.url
    }

    /// True when no constraint modifier was declared.
    #[must_use]
    pub const fn is_empty(&self) -> bool {
        self.min.is_none()
            && self.max.is_none()
            && !self.email
            && !self.url
            && self.label.is_none()
            && self.from.is_none()
            && self.encrypted.is_none()
            && self.scope.is_none()
            && !self.translatable
    }
}

/// The at-rest encryption mode a field's `{encrypted}` modifier declares
/// (issue #1340). Mirrors `autumn_web::encryption::Mode` and the
/// `#[encrypted(...)]` attribute the `#[model]` macro parses.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EncryptedMode {
    /// `{encrypted}` (or the explicit `{encrypted:randomized}`) — randomized
    /// AEAD, a fresh nonce per write. The safe default: equal plaintexts
    /// produce different ciphertext, so nothing leaks through the column — at
    /// the cost of making equality lookups (`WHERE col = ?`, `find_by_<col>`,
    /// a `UNIQUE` index) impossible.
    Randomized,
    /// `{encrypted:deterministic}` — the nonce is derived from the plaintext,
    /// so equal plaintexts produce equal ciphertext. Equality lookups and
    /// `UNIQUE` indexes work; the tradeoff is that an observer of the database
    /// can tell which rows share a value. An explicit opt-in by design.
    Deterministic,
}

impl EncryptedMode {
    /// Parse the value half of an `encrypted:<mode>` modifier.
    ///
    /// `admin_visible` and `versioned_ciphertext` are real `#[encrypted(...)]`
    /// options but deliberately not DSL-expressible: the point of generator
    /// wiring is that a scaffolded encrypted column is redacted in the admin
    /// and kept out of version history by default, so opting back out is a
    /// deliberate hand-edit. They get their own message rather than being
    /// lumped in with a typo.
    fn parse(value: &str) -> Result<Self, String> {
        match value {
            "deterministic" => Ok(Self::Deterministic),
            "randomized" => Ok(Self::Randomized),
            opt @ ("admin_visible" | "versioned_ciphertext") => Err(format!(
                "the `{opt}` encryption option is not expressible in the field DSL — a \
                 generated encrypted column is redacted in the admin and kept out of version \
                 history by default. Declare `{{encrypted}}` here and add \
                 `#[encrypted({opt})]` to the generated model field by hand if you need it."
            )),
            other => Err(format!(
                "unknown encryption mode '{other}'; supported: deterministic, randomized \
                 (the default). Use `{{encrypted:deterministic}}` when the column needs \
                 `find_by`/`exists_by` equality lookups or a UNIQUE index."
            )),
        }
    }
}

/// A single allowed edge in a field's state machine (issue #1326).
///
/// Mirrors the `from -> to[: guard]` grammar the `#[state_machine(transitions(…))]`
/// attribute macro accepts (see `autumn-macros`): `from`/`to` are bare state
/// identifiers and `guard`, when present, is the plain name of a `&self -> bool`
/// method that must return `true` for the edge to be taken.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StateTransition {
    /// The state this edge starts from.
    pub from: String,
    /// The state this edge ends at.
    pub to: String,
    /// Optional guard: the name of a `&self` bool method gating the edge.
    pub guard: Option<String>,
}

/// The parsed state machine declared on a field via the DSL's trailing
/// `:states(…)` modifier (issue #1326). Carries the ordered set of allowed
/// [`StateTransition`] edges, which the model generator re-emits as a
/// `#[state_machine(transitions(…))]` attribute on the field.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StateMachine {
    /// The allowed transitions, in declaration order.
    pub transitions: Vec<StateTransition>,
}

/// A single field parsed from the command line.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Field {
    /// Column / struct field name (`snake_case`).
    pub name: String,
    /// Underlying type, ignoring `Option` wrapping.
    pub kind: FieldKind,
    /// True when the field was given as `Option<…>`.
    pub nullable: bool,
    /// The declared variant tokens (`snake_case`) for [`FieldKind::Enum`]
    /// fields, in declaration order. Empty for every other kind.
    pub variants: Vec<String>,
    /// True when the field was given a trailing `:unique` modifier
    /// (`email:String:unique`), or was later marked unique via `--unique`
    /// (see [`super::model::apply_unique_flags`]). Emits a `CREATE UNIQUE
    /// INDEX` in the migration instead of the plain, non-unique `--index`
    /// output (see [`super::schema_edit::unique_index_sql`]).
    pub unique: bool,
    /// Constraint modifiers from a trailing `{…}` block (issue #1388 /
    /// #1146). Empty ([`FieldConstraints::is_empty`]) for a bare `name:Type`.
    pub constraints: FieldConstraints,
    /// The state machine declared via a trailing `:states(…)` modifier
    /// (issue #1326), or `None` for an ordinary field. Only valid on a
    /// non-nullable `String`/`Text` field — the model generator re-emits it
    /// as a `#[state_machine(transitions(…))]` attribute.
    pub state_machine: Option<StateMachine>,
}

impl Field {
    /// The Rust type for the `#[model]` struct.
    ///
    /// For [`FieldKind::Enum`], this is the generated enum's `PascalCase`
    /// name (see [`Field::enum_type_name`]) rather than [`FieldKind::rust_type`]'s
    /// `String` storage-representation fallback.
    #[must_use]
    pub fn rust_type(&self) -> String {
        // #1384: a `{translatable}` column is a per-locale container in Rust,
        // not a `String`. The SQL type is unchanged (`TEXT`) — the container
        // is stored as a JSON object — so only the Rust type moves.
        if self.constraints.translatable {
            return TRANSLATED_RUST_TYPE.to_owned();
        }
        let inner = self
            .enum_type_name()
            .unwrap_or_else(|| self.kind.rust_type().to_owned());
        if self.nullable {
            format!("Option<{inner}>")
        } else {
            inner
        }
    }

    /// Whether this column stores per-locale content (issue #1384) — declared
    /// with a `{translatable}` modifier, re-emitted as `#[translatable]`.
    #[must_use]
    pub const fn is_translatable(&self) -> bool {
        self.constraints.translatable
    }

    /// The SQL `DEFAULT` clause this field's storage needs, without the
    /// `DEFAULT` keyword, or `None` for a column with no default.
    ///
    /// Only `{translatable}` uses it today: the container column is `NOT NULL`
    /// and defaults to the empty JSON object, which is what makes the
    /// `ADD COLUMN` an ordinary *safe* migration (`autumn migrate check`
    /// classifies `ADD COLUMN … NOT NULL DEFAULT <constant>` as safe) rather
    /// than a table-rewriting backfill.
    #[must_use]
    pub const fn sql_default(&self) -> Option<&'static str> {
        if self.constraints.translatable {
            Some(EMPTY_TRANSLATION_CONTAINER)
        } else {
            None
        }
    }

    /// For [`FieldKind::Enum`] fields, the `PascalCase` name of the generated
    /// Rust enum (`status` -> `Status`). `None` for every other field kind.
    #[must_use]
    pub fn enum_type_name(&self) -> Option<String> {
        self.kind.is_enum().then(|| naming::pascal(&self.name))
    }

    /// Returns `true` for a closed-set `enum{…}` field.
    #[must_use]
    pub const fn is_enum(&self) -> bool {
        self.kind.is_enum()
    }

    /// The Diesel `schema.rs` type token (always a single identifier).
    #[must_use]
    pub fn schema_type(&self) -> String {
        let inner = self.kind.schema_type();
        if self.nullable {
            format!("Nullable<{inner}>")
        } else {
            inner.to_string()
        }
    }

    /// The SQL column type, without nullability suffix.
    #[must_use]
    pub const fn sql_type(&self) -> &'static str {
        self.kind.sql_type()
    }

    /// The SQL column type used in `CREATE TABLE`/`ADD COLUMN`, including any
    /// type parameters. Identical to [`Field::sql_type`] for every kind
    /// except [`FieldKind::Decimal`], whose `precision`/`scale` can't be
    /// represented in `sql_type`'s `&'static str` return type — this method
    /// renders the full `NUMERIC(precision,scale)` instead.
    #[must_use]
    pub fn sql_column_type(&self) -> String {
        match self.kind {
            FieldKind::Decimal { precision, scale } => format!("NUMERIC({precision},{scale})"),
            _ => self.sql_type().to_owned(),
        }
    }

    /// The `CREATE TABLE`/`ADD COLUMN` column type for the target `backend`
    /// (issue #1614). Identical to [`Field::sql_column_type`] for Postgres.
    /// On `SQLite`, `Decimal` maps to `TEXT` (see [`FieldKind::sqlite_sql_type`])
    /// — its `precision`/`scale` are not carried into the column type because
    /// `SQLite` has no fixed-precision `NUMERIC`; the exact value round-trips
    /// through `rust_decimal`'s text representation instead.
    #[must_use]
    pub fn sql_column_type_for(&self, backend: DatabaseBackend) -> String {
        // Postgres stays byte-for-byte identical to `sql_column_type`, including
        // the exact `NUMERIC(precision,scale)` rendering for decimals. Mirrors
        // `schema_type_for`'s structure.
        if backend == DatabaseBackend::Postgres {
            return self.sql_column_type();
        }
        // On SQLite every kind maps to the plain per-backend column type;
        // `Decimal` collapses to `TEXT` (see `FieldKind::sqlite_sql_type`) with
        // no `(p,s)` suffix, since SQLite has no fixed-precision NUMERIC.
        self.kind.sql_type_for(backend).to_owned()
    }

    /// The Diesel `schema.rs` type token for the target `backend` (issue
    /// #1614), including any `Nullable<…>` wrapping. Identical to
    /// [`Field::schema_type`] for Postgres.
    #[must_use]
    pub fn schema_type_for(&self, backend: DatabaseBackend) -> String {
        // Postgres stays byte-for-byte identical to `schema_type`.
        if backend == DatabaseBackend::Postgres {
            return self.schema_type();
        }
        let inner = self.kind.schema_type_for(backend);
        if self.nullable {
            format!("Nullable<{inner}>")
        } else {
            inner.to_string()
        }
    }

    /// `"NULL"` or `"NOT NULL"` to append in the migration.
    #[must_use]
    pub const fn sql_nullability(&self) -> &'static str {
        if self.nullable { "NULL" } else { "NOT NULL" }
    }

    /// The server-side `#[validate(…)]` argument list this field's
    /// constraint modifiers (issue #1388) fan out to — e.g.
    /// `["length(min = 3, max = 120)"]`, `["email"]`, `["range(min = 0, max = 130)"]`.
    ///
    /// `String`/`Text` fields map `min`/`max` to `length` and honor
    /// `email`/`url`; numeric fields map `min`/`max` to `range`. Float
    /// (`f32`/`f64`) range bounds are emitted with a decimal point so the
    /// generated comparison type-checks against the field's own type. Every
    /// other kind (and the `references`-only `label`) contributes nothing.
    /// Empty when the field declared no fan-out constraints.
    #[must_use]
    pub fn validation_attrs(&self) -> Vec<String> {
        let c = &self.constraints;
        let mut out = Vec::new();
        match self.kind {
            // `RichText` shares `Text`'s length rules; its `email`/`url`
            // constraints are rejected at parse time, so the flags below are
            // always false for it.
            FieldKind::String | FieldKind::Text | FieldKind::RichText => {
                if c.min.is_some() || c.max.is_some() {
                    out.push(format!(
                        "length({})",
                        min_max_args(c.min.as_ref(), c.max.as_ref(), false)
                    ));
                }
                if c.email {
                    out.push("email".to_owned());
                }
                if c.url {
                    out.push("url".to_owned());
                }
            }
            FieldKind::I32 | FieldKind::I64 | FieldKind::F32 | FieldKind::F64
                if c.min.is_some() || c.max.is_some() =>
            {
                let is_float = matches!(self.kind, FieldKind::F32 | FieldKind::F64);
                out.push(format!(
                    "range({})",
                    min_max_args(c.min.as_ref(), c.max.as_ref(), is_float)
                ));
            }
            _ => {}
        }
        out
    }

    /// The at-rest encryption mode declared via a `{encrypted}` /
    /// `{encrypted:deterministic}` modifier (issue #1340), or `None` for an
    /// ordinary plaintext column.
    #[must_use]
    pub const fn encrypted_mode(&self) -> Option<EncryptedMode> {
        self.constraints.encrypted
    }

    /// Whether this column is stored encrypted at rest, in either mode.
    #[must_use]
    pub const fn is_encrypted(&self) -> bool {
        self.constraints.encrypted.is_some()
    }

    /// Whether this column is *randomized*-encrypted — the mode that makes
    /// equality lookups (`find_by_<col>`, `WHERE col = ?`, a `UNIQUE` index)
    /// impossible, because a fresh nonce per write means equal plaintexts never
    /// produce equal ciphertext. The generators refuse to emit those lookups
    /// for such a column (issue #1340 AC6), mirroring the runtime's
    /// `EncryptionError::RandomizedEqualityLookup`.
    #[must_use]
    pub const fn is_randomized_encrypted(&self) -> bool {
        matches!(self.constraints.encrypted, Some(EncryptedMode::Randomized))
    }

    /// For a [`FieldKind::References`] field, the referenced table name —
    /// the `_id` suffix is stripped from the column name and the remainder
    /// is pluralised via [`naming::pluralize`] (`post_id` -> `posts`).
    ///
    /// Returns `None` for every other field kind.
    #[must_use]
    pub fn reference_table(&self) -> Option<String> {
        if !self.kind.is_reference() {
            return None;
        }
        let base = self.name.strip_suffix("_id").unwrap_or(&self.name);
        Some(naming::pluralize(base))
    }
}

/// The supported field types. Mirrors the documented public surface in the
/// `autumn generate --help` output.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FieldKind {
    /// `String` — `TEXT`.
    String,
    /// `Text` (alias for `String`) — `TEXT`.
    Text,
    /// `richtext` — user-submitted Markdown, stored as `TEXT` (issue #1255).
    ///
    /// Storage-identical to [`FieldKind::Text`]: the column holds the Markdown
    /// **source**, never rendered HTML, so the Rust type is `String`, the
    /// diesel token is `Text`, and the SQL column type is `TEXT` on both
    /// backends. The distinction is entirely in the generated UI:
    ///
    /// - the form renders `autumn_web::form::rich_text_area_htmx_with_token_field`
    ///   — a Markdown editor with a no-JavaScript syntax toolbar and an htmx
    ///   live preview — instead of a bare `<textarea>`;
    /// - the `show` view renders the value through
    ///   `autumn_web::markdown::render_user_content`, which disables raw-HTML
    ///   passthrough and applies an allowlist sanitizer, instead of emitting
    ///   the source as escaped text;
    /// - the scaffold enables `autumn-web`'s `markdown` feature so both of
    ///   those resolve.
    ///
    /// The `{email}`/`{url}` format constraints are rejected on this kind
    /// (a Markdown body cannot satisfy a single-line format validator). The
    /// `{min}`/`{max}` length bounds are accepted and emit the same server-side
    /// `#[validate(length(…))]` rule as `Text`; unlike `Text` they emit no
    /// client-side `minlength`/`maxlength`, because the editor is rendered by
    /// `rich_text_area`, which takes no HTML5 constraint attributes.
    RichText,
    /// `i32` — `INTEGER`.
    I32,
    /// `i64` — `BIGINT`.
    I64,
    /// `bool` — `BOOLEAN`.
    Bool,
    /// `f32` — `REAL`.
    F32,
    /// `f64` — `DOUBLE PRECISION`.
    F64,
    /// `Uuid` — `UUID`.
    Uuid,
    /// `NaiveDateTime` — `TIMESTAMP`.
    NaiveDateTime,
    /// `DateTime` — `TIMESTAMPTZ`.
    DateTime,
    /// `Vec<u8>` / `Bytea` — `BYTEA`.
    Bytea,
    /// `Blob` stored as `JSONB` — a file attachment with direct-upload support.
    ///
    /// Maps to a Postgres `JSONB` column that stores the `autumn_web::storage::Blob`
    /// metadata (key, content-type, byte-size, etag). The bytes themselves live
    /// in the configured storage backend (local disk or S3-compatible).
    ///
    /// Always emitted as `Option<autumn_web::storage::Blob>` in the model struct
    /// so the attachment is optional by default. Wrap in `Option<Attachment>` to
    /// be explicit, or leave as `Attachment` (equivalent: nullable is the default
    /// and safe choice for file fields).
    Attachment,
    /// `json` (alias `jsonb`) — arbitrary structured data stored as a Postgres
    /// `JSONB` column (issue #1341). Unlike [`Attachment`](Self::Attachment),
    /// this maps directly to bare `serde_json::Value` — no wrapper struct —
    /// because diesel itself already implements `FromSql`/`ToSql<Jsonb, Pg>`
    /// **and** `<Json, Sqlite>` for `serde_json::Value` (diesel 2.3+, behind
    /// the `serde_json` + `sqlite` cargo features, both already
    /// unconditionally enabled by this workspace's `db` feature). So — unlike
    /// `Decimal`/`Uuid`, whose Rust types are foreign to `autumn-web` and
    /// whose `SQLite` conversion is genuinely blocked by the orphan rule — no
    /// custom `autumn-web` conversion code is needed on either backend.
    ///
    /// Ordinary nullable convention applies (unlike `Attachment`, this is
    /// **not** implicitly nullable): a bare `json` field is `NOT NULL`;
    /// `Option<json>` is nullable. On `SQLite` the column is `TEXT` storing
    /// plain-text JSON via diesel's `Json` sql-type — deliberately **not**
    /// diesel's `Jsonb` sql-type on `SQLite`, which uses a proprietary binary
    /// encoding unrelated to the human-readable "TEXT storing JSON" this
    /// field promises.
    Json,
    /// `references` — a foreign-key column (`i64`/`BIGINT`), matching the
    /// default `i64` primary-key convention. The DSL rewrites the declared
    /// field name to end in `_id` (`post:references` -> `post_id`) and the
    /// referenced table is derived from the base name via [`naming::pluralize`]
    /// (`post` -> `posts`). See [`Field::reference_table`].
    References,
    /// `enum{a,b,c}` — a closed-set column. Stored as `TEXT` with a `CHECK`
    /// constraint enumerating the allowed values (see
    /// [`create_table_sql_with_metadata_and_id`](super::schema_edit::create_table_sql_with_metadata_and_id)),
    /// and rendered in the `#[model]` struct as a generated Rust enum rather
    /// than the bare `String` [`FieldKind::rust_type`] reports here — see
    /// [`Field::rust_type`] and [`Field::enum_type_name`], which override this
    /// storage-representation fallback with the real generated type name.
    Enum,
    /// `decimal{precision,scale}` (default `{12,2}` when the modifier is
    /// omitted) — an exact-precision `NUMERIC(precision,scale)` column.
    /// Unlike `f32`/`f64`, this never introduces binary-float rounding error,
    /// making it the correct choice for money and other exact-decimal values.
    /// Rendered in the `#[model]` struct as `rust_decimal::Decimal` (see
    /// [`FieldKind::rust_type`]) — the same decimal type the `autumn` runtime
    /// crate already re-exports and uses for its `number_to_currency` helper.
    Decimal {
        /// Total number of significant digits (`NUMERIC(precision, _)`).
        precision: u32,
        /// Number of digits after the decimal point (`NUMERIC(_, scale)`).
        scale: u32,
    },
    /// `slug{from:col}` — a human-readable, URL-safe routing key auto-derived
    /// from another field (issue #1260), e.g. `slug:slug{from:title}`.
    /// Storage-identical to [`FieldKind::String`] (`TEXT`, `String`), but
    /// always [`Field::unique`] (so it falls into the existing `unique`-field
    /// `UNIQUE INDEX` and `find_by_slug` repository machinery from issue
    /// #1032 for free) and never [`Field::nullable`] — a record with no slug
    /// would have no URL. The source field it derives from lives in
    /// [`FieldConstraints::from`], parsed from the mandatory `{from:...}`
    /// modifier. On create, a blank submitted value is auto-derived via
    /// [`autumn_web::slug::slugify`] and made unique with a deterministic
    /// `-2`, `-3`, ... suffix on collision; the scaffold's `show`/`edit`/
    /// `update`/`delete` routes resolve the record by this field instead of
    /// `id`.
    Slug,
    /// `position` (optionally `position{scope:col}`) — a server-managed
    /// contiguous ordering column (issue #1358), e.g. `rank:position` or,
    /// scoped to a parent, `rank:position{scope:board_id}`. Storage-wise a
    /// plain `i64`/`BIGINT` column (matching the default `i64` primary-key
    /// convention), but never nullable — every row belongs somewhere in its
    /// ordering — and never hand-assigned: the generated repository assigns
    /// the next contiguous value on insert, compacts on delete, and exposes
    /// `move_to`/`move_before`/`move_after`/`move_up`/`move_down` helpers, so
    /// the field is excluded from the generated `New*`/`Update*` structs the
    /// same way `id`/timestamps are. The optional [`FieldConstraints::scope`]
    /// keeps a separate contiguous `0..len-1` sequence per distinct value of
    /// the named column (typically a `references` foreign key). At most one
    /// `position` field is allowed per model.
    Position,
    /// `commentable` (e.g. `comments:commentable`) — threaded, **polymorphic**
    /// comments on this model (issue #1367).
    ///
    /// Storage-wise this is the counter-cache *source* column: a plain
    /// `i64`/`BIGINT`, never nullable (the maintenance is `c = c + 1`, and
    /// `NULL + 1` is `NULL`), never hand-assigned, and — like
    /// [`Position`](Self::Position) — excluded from the generated
    /// `New*`/`Update*` structs and the create/edit form, because the
    /// framework maintains it inside each comment's own transaction. The
    /// declared field name is normalised to `{singular}_count`
    /// (`comments:commentable` → `comment_count`), matching `#[model]`'s
    /// `{snake(child)}_count` convention.
    ///
    /// The rest of the feature is not a column at all: the token additionally
    /// emits the shared `comments(commentable_type, commentable_id, parent_id,
    /// …)` migration — once per project, since the table is shared — and
    /// `#[commentable(...)]` on the generated `#[model]`, which is what brings
    /// the repository's `add_comment`/`comment_thread`/`delete_comment`
    /// helpers into existence. At most one `commentable` field per model.
    Commentable,
}

impl FieldKind {
    /// Rust type token used inside `#[model]` structs.
    ///
    /// For [`Attachment`](Self::Attachment), always returns the inner `Blob`
    /// type. Nullability wrapping (`Option<…>`) is applied by [`Field::rust_type`].
    #[must_use]
    pub const fn rust_type(self) -> &'static str {
        match self {
            // `Enum`'s "String" here is a storage-representation fallback
            // only — `Field::rust_type()` overrides it with the generated
            // enum's real type name.
            Self::String | Self::Text | Self::RichText | Self::Enum | Self::Slug => "String",
            Self::I32 => "i32",
            // `References` and `Position` are always `i64`, matching the
            // default `i64` PK convention.
            Self::I64 | Self::References | Self::Position | Self::Commentable => "i64",
            Self::Bool => "bool",
            Self::F32 => "f32",
            Self::F64 => "f64",
            Self::Uuid => "uuid::Uuid",
            Self::NaiveDateTime => "chrono::NaiveDateTime",
            Self::DateTime => "chrono::DateTime<chrono::Utc>",
            Self::Bytea => "Vec<u8>",
            Self::Attachment => "autumn_web::storage::Blob",
            Self::Json => "serde_json::Value",
            Self::Decimal { .. } => "rust_decimal::Decimal",
        }
    }

    /// Diesel `table!` schema type token.
    #[must_use]
    pub const fn schema_type(self) -> &'static str {
        match self {
            Self::String | Self::Text | Self::RichText | Self::Enum | Self::Slug => "Text",
            Self::I32 => "Int4",
            Self::I64 | Self::References | Self::Position | Self::Commentable => "Int8",
            Self::Bool => "Bool",
            Self::F32 => "Float4",
            Self::F64 => "Float8",
            Self::Uuid => "Uuid",
            Self::NaiveDateTime => "Timestamp",
            Self::DateTime => "Timestamptz",
            Self::Bytea => "Bytea",
            Self::Attachment | Self::Json => "Jsonb",
            Self::Decimal { .. } => "Numeric",
        }
    }

    /// `PostgreSQL` column type, without `NOT NULL` / `NULL`.
    #[must_use]
    pub const fn sql_type(self) -> &'static str {
        match self {
            Self::String | Self::Text | Self::RichText | Self::Enum | Self::Slug => "TEXT",
            Self::I32 => "INTEGER",
            Self::I64 | Self::References | Self::Position | Self::Commentable => "BIGINT",
            Self::Bool => "BOOLEAN",
            Self::F32 => "REAL",
            Self::F64 => "DOUBLE PRECISION",
            Self::Uuid => "UUID",
            Self::NaiveDateTime => "TIMESTAMP",
            Self::DateTime => "TIMESTAMPTZ",
            Self::Bytea => "BYTEA",
            Self::Attachment | Self::Json => "JSONB",
            Self::Decimal { .. } => "NUMERIC",
        }
    }

    /// `SQLite` column type, without `NOT NULL` / `NULL` (`SQLite` foundation,
    /// issue #1614). `SQLite`'s storage classes are few and forgiving, so every
    /// DSL kind maps to a working column type — nothing is rejected at
    /// generate time on this axis (see [`FieldKind::sql_type_for`]). Notable
    /// differences from the Postgres mapping:
    ///
    /// - `bool` -> `INTEGER` (`SQLite` has no boolean type; `0`/`1`).
    /// - `f32`/`f64` -> `REAL` (`SQLite` `REAL` is always 8-byte double).
    /// - `Uuid` -> `TEXT` (no native `uuid`; stored as its canonical string).
    /// - `NaiveDateTime`/`DateTime` -> `TEXT` (ISO-8601 / RFC 3339 string;
    ///   `SQLite` has no dedicated timestamp type and its date functions operate
    ///   on ISO-8601 text).
    /// - `Bytea` -> `BLOB`.
    /// - `Attachment` -> `TEXT` (the `Blob` metadata JSON, stored as text
    ///   rather than Postgres `JSONB`).
    /// - `json`/`jsonb` -> `TEXT` (issue #1341) — plain-text JSON, via
    ///   diesel's own `Json` sql-type (see [`FieldKind::sqlite_schema_type`]),
    ///   not Postgres `JSONB`.
    /// - `Decimal` -> `TEXT` (`SQLite` `NUMERIC` affinity coerces to REAL/INTEGER
    ///   and would lose exactness; `rust_decimal` round-trips losslessly through
    ///   text).
    #[must_use]
    #[allow(
        clippy::match_same_arms,
        reason = "every FieldKind is listed explicitly to document the complete SQLite \
                  column mapping (AC #4), even where several kinds share a storage type"
    )]
    pub const fn sqlite_sql_type(self) -> &'static str {
        match self {
            Self::String | Self::Text | Self::RichText | Self::Enum | Self::Slug => "TEXT",
            Self::I32
            | Self::I64
            | Self::References
            | Self::Position
            | Self::Commentable
            | Self::Bool => "INTEGER",
            Self::F32 | Self::F64 => "REAL",
            Self::Uuid => "TEXT",
            Self::NaiveDateTime | Self::DateTime => "TEXT",
            Self::Bytea => "BLOB",
            Self::Attachment | Self::Json => "TEXT",
            Self::Decimal { .. } => "TEXT",
        }
    }

    /// The SQL column type for the target `backend` (issue #1614). Postgres
    /// keeps [`FieldKind::sql_type`] byte-for-byte; `SQLite` uses
    /// [`FieldKind::sqlite_sql_type`].
    #[must_use]
    pub const fn sql_type_for(self, backend: DatabaseBackend) -> &'static str {
        match backend {
            DatabaseBackend::Postgres => self.sql_type(),
            DatabaseBackend::Sqlite => self.sqlite_sql_type(),
        }
    }

    /// Diesel `table!` schema type token for a `SQLite` target (issue #1614).
    ///
    /// The Postgres-only diesel sql-types (`Timestamptz`, `Jsonb`, `Uuid`,
    /// `Bytea`, `Numeric`) are not implemented for diesel's `SQLite` backend, so
    /// they are remapped to types `SQLite` does support: `Uuid`/`Attachment`/
    /// `Decimal` -> `Text`, `NaiveDateTime` -> `Timestamp`, `DateTime<Utc>` ->
    /// `TimestamptzSqlite`, `Bytea` -> `Binary`.
    ///
    /// `NaiveDateTime` maps to the core `diesel::sql_types::Timestamp` — a
    /// backend-agnostic sql-type that is exported without diesel's `sqlite`
    /// feature and for which diesel implements `FromSql`/`ToSql<Timestamp,
    /// Sqlite>` targeting `NaiveDateTime`, so it compiles in a generated app.
    ///
    /// `DateTime<Utc>` maps to diesel's `TimestamptzSqlite` sql-type (issue
    /// #1924). diesel implements `FromSql`/`ToSql<TimestamptzSqlite, Sqlite>`
    /// for `DateTime<Utc>` under its `sqlite` + `chrono` features. A generated
    /// `SQLite` app depends on `autumn-web` with the `sqlite` feature, which
    /// pulls in `diesel/sqlite` (and `diesel/chrono`) by cargo feature
    /// unification, so both `TimestamptzSqlite` and the conversion resolve — the
    /// value round-trips as an RFC 3339 UTC string (`SQLite` `TEXT` affinity),
    /// which sorts and compares chronologically.
    ///
    /// `Attachment` (`autumn_web::storage::Blob`) maps to `Text` and stores the
    /// `Blob` metadata as its JSON body; `autumn-web` provides
    /// `FromSql`/`ToSql<Text, Sqlite>` + `AsExpression`/`FromSqlRow<Text>` for
    /// `Blob` under its `sqlite` feature (issue #1924).
    ///
    /// `json`/`jsonb` (issue #1341) maps to diesel's own `Json` sql-type —
    /// deliberately distinct from both `Text` (no `serde_json::Value`
    /// conversion) and `Jsonb` (diesel's `SQLite` `Jsonb` uses a proprietary
    /// binary encoding, not plain text). diesel itself implements
    /// `FromSql`/`ToSql<Json, Sqlite> for serde_json::Value` (diesel 2.3+,
    /// `serde_json` + `sqlite` features, both already unconditionally on via
    /// this workspace's `db` feature), storing plain-text JSON as `TEXT` — no
    /// `autumn-web` conversion code needed, unlike `Attachment`.
    ///
    /// `Uuid`, `Decimal`, and `Enum` still list a nominal `Text` remapping here
    /// for documentation completeness, but they are rejected at generate time
    /// (see [`FieldKind::sqlite_has_diesel_conversion`]) — their Rust types are
    /// foreign to `autumn-web`, so the orphan rule forbids adding the required
    /// `Sqlite` conversions without a per-field `serialize_as`/`deserialize_as`
    /// wrapper (tracked as follow-up work) — so this token never reaches a
    /// generated `schema.rs`.
    #[must_use]
    #[allow(
        clippy::match_same_arms,
        reason = "every FieldKind is listed explicitly to document the complete SQLite \
                  diesel-type mapping (AC #4), even where several kinds share a type"
    )]
    pub const fn sqlite_schema_type(self) -> &'static str {
        match self {
            Self::String | Self::Text | Self::RichText | Self::Enum | Self::Slug => "Text",
            Self::I32 => "Int4",
            Self::I64 | Self::References | Self::Position | Self::Commentable => "Int8",
            Self::Bool => "Bool",
            Self::F32 => "Float4",
            Self::F64 => "Float8",
            Self::Uuid => "Text",
            // `NaiveDateTime` -> core `Timestamp` (ungated, compiles).
            Self::NaiveDateTime => "Timestamp",
            // `DateTime<Utc>` -> `TimestamptzSqlite` (diesel's sqlite+chrono
            // conversion, available via the app's `autumn-web` sqlite feature).
            Self::DateTime => "TimestamptzSqlite",
            Self::Bytea => "Binary",
            Self::Attachment => "Text",
            // Diesel's own `Json` sql-type — NOT `Text` (no built-in
            // `serde_json::Value` conversion) and NOT `Jsonb` (SQLite's
            // proprietary binary encoding). See this method's doc comment.
            Self::Json => "Json",
            Self::Decimal { .. } => "Text",
        }
    }

    /// Whether this kind's rendered Rust model type has a working diesel
    /// `FromSql`/`ToSql` on diesel's `SQLite` backend in a generated app's
    /// feature set (diesel `sqlite` + `chrono`, without `uuid`/`numeric`) —
    /// determined empirically (issue #1614 AC #4; conversions wired in #1924).
    ///
    /// Now supported on `SQLite` (issue #1924):
    /// - `NaiveDateTime` via the core, ungated `Timestamp` sql-type.
    /// - `DateTime<Utc>` via diesel's `TimestamptzSqlite` sql-type — its
    ///   `sqlite`+`chrono` conversion resolves through the app's `autumn-web`
    ///   `sqlite` feature (RFC 3339 UTC text, chronologically sortable).
    /// - `Attachment` (`autumn_web::storage::Blob`) — `autumn-web` provides
    ///   `FromSql`/`ToSql<Text, Sqlite>` + `AsExpression`/`FromSqlRow<Text>` for
    ///   `Blob` under its `sqlite` feature (`Blob` is a local type, so these
    ///   impls are orphan-rule-legal), storing the metadata JSON as `TEXT`.
    /// - `json`/`jsonb` (issue #1341) — `serde_json::Value` is a *foreign*
    ///   type, but diesel itself (not `autumn-web`) already implements
    ///   `FromSql`/`ToSql<Json, Sqlite>` for it, so there is no orphan-rule
    ///   obstacle at all — unlike `Uuid`/`Decimal` below, which are genuinely
    ///   blocked because neither diesel nor `autumn-web` provides the impl.
    ///
    /// Still rejected (their Rust types are foreign to `autumn-web`, so the
    /// orphan rule forbids `autumn-web` from adding a `Sqlite` `FromSql`/`ToSql`
    /// directly, and diesel/`rust_decimal`'s built-in impls are Postgres-only):
    /// - `Uuid` (`uuid::Uuid`) and `Decimal` (`rust_decimal::Decimal`) — these
    ///   need a per-field `serialize_as`/`deserialize_as` wrapper threaded
    ///   through the `#[model]` macro (including nullable columns), tracked as
    ///   follow-up work under issue #1924.
    /// - `Enum` fields render an enum whose only generated diesel conversion is
    ///   `ToSql`/`FromSql<Text, diesel::pg::Pg>` (see `render_enum_decl`), i.e.
    ///   Postgres-only, with no `Text`/`Sqlite` impl.
    ///
    /// Rather than emit uncompilable code, the still-unsupported kinds are
    /// rejected at generate time.
    #[must_use]
    pub const fn sqlite_has_diesel_conversion(self) -> bool {
        !matches!(self, Self::Uuid | Self::Decimal { .. } | Self::Enum)
    }

    /// The diesel `table!` schema type token for the target `backend`
    /// (issue #1614). Postgres keeps [`FieldKind::schema_type`] byte-for-byte;
    /// `SQLite` uses [`FieldKind::sqlite_schema_type`].
    #[must_use]
    pub const fn schema_type_for(self, backend: DatabaseBackend) -> &'static str {
        match backend {
            DatabaseBackend::Postgres => self.schema_type(),
            DatabaseBackend::Sqlite => self.sqlite_schema_type(),
        }
    }

    /// The DSL token a user writes for this kind, for error messages that need
    /// to name the field's *declared* type rather than its Rust storage type.
    ///
    /// [`FieldKind::rust_type`] is the wrong thing to report in a diagnostic
    /// about the DSL: `richtext`, `enum{…}` and `slug{…}` all render as
    /// `String` in Rust, so "only applies to `String` fields … got a `String`
    /// field" is what a user would see for `body:richtext{encrypted}`.
    #[must_use]
    pub const fn dsl_token(self) -> &'static str {
        match self {
            Self::String => "String",
            Self::Text => "Text",
            Self::RichText => "richtext",
            Self::I32 => "i32",
            Self::I64 => "i64",
            Self::Bool => "bool",
            Self::F32 => "f32",
            Self::F64 => "f64",
            Self::Uuid => "Uuid",
            Self::NaiveDateTime => "NaiveDateTime",
            Self::DateTime => "DateTime",
            Self::Bytea => "Bytea",
            Self::Attachment => "Attachment",
            Self::Json => "json",
            Self::References => "references",
            Self::Enum => "enum{…}",
            Self::Decimal { .. } => "decimal{…}",
            Self::Slug => "slug{…}",
            Self::Position => "position",
            Self::Commentable => "commentable",
        }
    }

    /// Returns `true` for field kinds that represent file attachments (blobs).
    ///
    /// Used by the scaffold generator to detect fields that need multipart
    /// upload handling instead of the standard form-encoded path.
    #[must_use]
    pub const fn is_attachment(self) -> bool {
        matches!(self, Self::Attachment)
    }

    /// Returns `true` for a foreign-key `references` field.
    #[must_use]
    pub const fn is_reference(self) -> bool {
        matches!(self, Self::References)
    }

    /// Returns `true` for a closed-set `enum{…}` field.
    #[must_use]
    pub const fn is_enum(self) -> bool {
        matches!(self, Self::Enum)
    }

    /// Returns `true` for a `richtext` field (issue #1255).
    ///
    /// Used by the scaffold generator to pick the Markdown editor control and
    /// the sanitizing `show`-view render, and to enable `autumn-web`'s
    /// `markdown` feature on the project. Storage-wise the column is
    /// indistinguishable from [`FieldKind::Text`].
    #[must_use]
    pub const fn is_rich_text(self) -> bool {
        matches!(self, Self::RichText)
    }

    /// Returns `true` for a `slug{from:col}` routing-key field (issue #1260).
    ///
    /// Used by the scaffold generator to key `show`/`edit`/`update`/`delete`
    /// routes and generated links off this field instead of `id`, and to
    /// auto-derive its value from [`FieldConstraints::from`] on create.
    #[must_use]
    pub const fn is_slug(self) -> bool {
        matches!(self, Self::Slug)
    }

    /// Returns `true` for an exact-precision `decimal` field.
    ///
    /// Used by the scaffold/model generators to know when the project needs
    /// the `rust_decimal` dependency wired in.
    #[must_use]
    pub const fn is_decimal(self) -> bool {
        matches!(self, Self::Decimal { .. })
    }

    /// Returns `true` for a server-managed `position` ordering field (issue
    /// #1358).
    ///
    /// Used by the model/migration/repository/scaffold generators to: exclude
    /// the column from `New*`/`Update*` structs, emit its `NOT NULL BIGINT`
    /// column plus auto-index, wire `position(column = "...", scope = "...")`
    /// into the generated `#[repository(...)]` attribute, and order the
    /// scaffold index view by it with Move up/down buttons.
    #[must_use]
    pub const fn is_position(self) -> bool {
        matches!(self, Self::Position)
    }

    /// Returns `true` for a `commentable` polymorphic-comments field (issue
    /// #1367).
    ///
    /// Used by the model/migration/scaffold generators to: keep the counter
    /// column out of `New*`/`Update*` structs and the create/edit form, write
    /// `#[commentable(...)]` onto the generated model, and emit the shared
    /// `comments` table migration.
    #[must_use]
    pub const fn is_commentable(self) -> bool {
        matches!(self, Self::Commentable)
    }

    /// Returns `true` for a column the *framework* assigns and maintains, so
    /// no generated form, `New*` struct or `Update*` struct may offer it.
    ///
    /// One predicate rather than a growing `is_position() || is_commentable()
    /// || …` chain at every call site — a new server-managed kind is then a
    /// one-line change here instead of a hunt through the generators.
    #[must_use]
    pub const fn is_server_managed(self) -> bool {
        matches!(self, Self::Position | Self::Commentable)
    }
}

/// The supported primary-key types for `autumn generate model --id`.
///
/// Defaults to `BigSerial` (today's `BIGSERIAL`/`i64` behaviour). `Uuid`
/// opts the model into a `UUID PRIMARY KEY DEFAULT gen_random_uuid()` column.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum IdType {
    /// `BIGSERIAL PRIMARY KEY` — sequential auto-increment integer (default).
    #[default]
    BigSerial,
    /// `UUID PRIMARY KEY DEFAULT gen_random_uuid()` — non-enumerable.
    Uuid,
}

impl IdType {
    /// Rust type for the `#[id]` struct field.
    #[must_use]
    pub const fn rust_type(self) -> &'static str {
        match self {
            Self::BigSerial => "i64",
            Self::Uuid => "uuid::Uuid",
        }
    }

    /// Diesel `table!` schema type token.
    #[must_use]
    pub const fn schema_type(self) -> &'static str {
        match self {
            Self::BigSerial => "Int8",
            Self::Uuid => "Uuid",
        }
    }

    /// The SQL fragment that appears after the column name in `CREATE TABLE`.
    #[must_use]
    pub const fn pk_sql(self) -> &'static str {
        match self {
            Self::BigSerial => "BIGSERIAL PRIMARY KEY",
            Self::Uuid => "UUID PRIMARY KEY DEFAULT gen_random_uuid()",
        }
    }

    /// The primary-key SQL fragment for the target `backend` (issue #1614).
    /// Postgres keeps [`IdType::pk_sql`] byte-for-byte. On `SQLite`:
    ///
    /// - `BigSerial` -> `INTEGER PRIMARY KEY AUTOINCREMENT` — an alias of the
    ///   `rowid` that never reuses a freed value (`SQLite` has no `BIGSERIAL`).
    /// - `Uuid` -> `TEXT PRIMARY KEY` — `SQLite` has no `uuid` type nor a
    ///   `gen_random_uuid()` default, so the application supplies the id.
    #[must_use]
    pub const fn pk_sql_for(self, backend: DatabaseBackend) -> &'static str {
        match backend {
            DatabaseBackend::Postgres => self.pk_sql(),
            DatabaseBackend::Sqlite => match self {
                Self::BigSerial => "INTEGER PRIMARY KEY AUTOINCREMENT",
                Self::Uuid => "TEXT PRIMARY KEY",
            },
        }
    }

    /// Diesel `table!` schema type token for the primary key on the target
    /// `backend` (issue #1614). Postgres keeps [`IdType::schema_type`]; `SQLite`
    /// remaps `Uuid` (unsupported by diesel's `SQLite` backend) to `Text`.
    #[must_use]
    pub const fn schema_type_for(self, backend: DatabaseBackend) -> &'static str {
        match backend {
            DatabaseBackend::Postgres => self.schema_type(),
            DatabaseBackend::Sqlite => match self {
                Self::BigSerial => "Int8",
                Self::Uuid => "Text",
            },
        }
    }

    /// An optional migration comment documenting trade-offs. Only `Uuid`
    /// returns `Some`, pointing developers toward the `UUIDv7` upgrade path.
    #[must_use]
    pub const fn migration_comment_for(self, backend: DatabaseBackend) -> Option<&'static str> {
        match backend {
            // The comment documents the Postgres-only `gen_random_uuid()` /
            // `uuid_generate_v7()` upgrade path, which does not apply to a
            // SQLite target (no such functions; the app supplies the id).
            DatabaseBackend::Postgres => self.migration_comment(),
            DatabaseBackend::Sqlite => None,
        }
    }

    #[must_use]
    pub const fn migration_comment(self) -> Option<&'static str> {
        match self {
            Self::BigSerial => None,
            Self::Uuid => Some(
                "-- gen_random_uuid() produces UUIDv4 (random, built into Postgres 13+).\n\
                 -- Trade-off: random UUIDs hurt B-tree insert locality on large tables.\n\
                 -- To switch to a time-ordered UUIDv7 (better locality, same privacy):\n\
                 --   1. Enable pgcrypto or use the pg_uuidv7 extension.\n\
                 --   2. Replace DEFAULT gen_random_uuid() with DEFAULT uuid_generate_v7().\n",
            ),
        }
    }

    /// Parse a CLI `--id` value into an [`IdType`].
    ///
    /// Accepts `uuid` (case-insensitive) and `bigint`/`bigserial`/`i64`.
    ///
    /// # Errors
    /// Returns [`GenerateError::Config`] for unknown values, with a message
    /// listing the accepted tokens (AC7).
    pub fn parse(s: &str) -> Result<Self, GenerateError> {
        match s.to_ascii_lowercase().as_str() {
            "uuid" => Ok(Self::Uuid),
            "bigint" | "bigserial" | "i64" => Ok(Self::BigSerial),
            other => Err(GenerateError::Config(format!(
                "unknown --id value '{other}'; accepted values are: uuid, bigint"
            ))),
        }
    }
}

/// Comma-separated list of supported types, for error messages and `--help`.
pub const SUPPORTED_TYPES: &str = "String, Text, richtext, i32, i64, bool, f32, f64, \
    Uuid, NaiveDateTime, DateTime, Vec<u8>, Bytea, Attachment, json (alias jsonb), references, \
    enum{a,b,…}, decimal{precision,scale}, slug{from:col}, position (optionally \
    position{scope:col}), commentable, Option<…>, :unique, String{encrypted}, \
    String{encrypted:deterministic}, String{translatable}";

/// The DSL field kinds that map to a working diesel `SQLite` conversion
/// (issue #1614 AC #4; #1924) — the complement of the kinds
/// [`FieldKind::sqlite_has_diesel_conversion`] rejects (`Uuid`, `Decimal`,
/// `Enum`). Used in the generate-time rejection message so the user knows which
/// field kinds a `SQLite` app supports today.
pub const SQLITE_SUPPORTED_KINDS: &str = "String, Text, richtext, i32, i64, bool, f32, f64, \
    NaiveDateTime, DateTime, Vec<u8>, Bytea, Attachment, json (alias jsonb), references, \
    slug{from:col}, position (optionally position{scope:col}), commentable, Option<…>, :unique";

/// Comma-separated list of supported Postgres column types (`udt_name`), for
/// the `db pull` introspection error message.
pub const SQL_SUPPORTED_TYPES: &str = "text, varchar, bpchar (-> String), int4 (-> i32), \
    int8 (-> i64), bool, float4 (-> f32), float8 (-> f64), uuid, timestamp, timestamptz, bytea";

/// Inverse of [`FieldKind::sql_type`] / [`FieldKind::schema_type`]: map a
/// Postgres `udt_name` (the concrete catalog type identifier such as `int4`,
/// `int8`, `text`, `timestamptz`) to the [`FieldKind`] the generators use.
///
/// This is the introspection direction used by `autumn db pull`. `text`,
/// `varchar`, and `bpchar` all collapse to the canonical [`FieldKind::String`]
/// (the DSL's `String`/`Text` aliases both render `String` in Rust and `Text`
/// in `schema.rs`, so the round-trip with a greenfield-generated model stays
/// byte-identical).
///
/// Returns `None` for types outside the documented surface so the caller can
/// fail loudly with a column-named error rather than silently dropping it.
///
/// Like `jsonb` (which round-trips as a plain `FieldKind::Json`, losing the
/// distinction from a hand-written `json` column), a `position` column has
/// no distinguishing catalog type of its own — it's a plain `int4`/`int8`
/// NOT NULL column plus an index and a pair of triggers. `db pull` therefore
/// introspects it back as an ordinary [`FieldKind::I32`]/[`FieldKind::I64`],
/// not [`FieldKind::Position`]; this is expected, not a bug — there is no
/// SQL-level marker to recover the `position`-ness from.
#[must_use]
pub fn sql_type_to_field_kind(udt_name: &str) -> Option<FieldKind> {
    match udt_name {
        "text" | "varchar" | "bpchar" => Some(FieldKind::String),
        "int4" => Some(FieldKind::I32),
        "int8" => Some(FieldKind::I64),
        "bool" => Some(FieldKind::Bool),
        "float4" => Some(FieldKind::F32),
        "float8" => Some(FieldKind::F64),
        "uuid" => Some(FieldKind::Uuid),
        "timestamp" => Some(FieldKind::NaiveDateTime),
        "timestamptz" => Some(FieldKind::DateTime),
        "bytea" => Some(FieldKind::Bytea),
        // `jsonb` is intentionally unsupported: although the DSL maps
        // `Attachment` -> `JSONB`, the inverse is ambiguous. A brownfield
        // `jsonb` column is usually arbitrary application JSON, not an Autumn
        // `Blob`, and introspection cannot tell them apart (both report
        // `udt_name = jsonb`). Mapping it to `Attachment` would emit a
        // `Blob` field that fails to deserialize real JSON rows, so we leave
        // it unsupported rather than risk corrupting brownfield data. Issue
        // #1341 deepens the same ambiguity rather than resolving it: `json`
        // fields ALSO forward-map to `JSONB`, so a pulled `jsonb` column could
        // now be either a `Blob` or a `serde_json::Value` — still no way to
        // tell, so this stays unsupported on purpose (`db pull` is forward-
        // generation tooling; reverse introspection of a JSONB column is out
        // of this issue's scope).
        _ => None,
    }
}

/// Parse a single CLI token of the form `name:Type`.
///
/// # Errors
/// Returns [`GenerateError::InvalidField`] if the token is malformed or the
/// type is not in the supported set.
#[allow(
    clippy::too_many_lines,
    reason = "a linear name→modifier→type→constraint parse; the early-return \
              validation guards read more clearly inline than split across helpers"
)]
#[allow(
    clippy::literal_string_with_formatting_args,
    reason = "the `{from:<field>}`/`{from:title}` mentions in the slug error message are the \
              DSL's own constraint-modifier syntax, not format-string placeholders"
)]
pub fn parse_field(token: &str) -> Result<Field, GenerateError> {
    let (name, rest) = token
        .split_once(':')
        .ok_or_else(|| GenerateError::InvalidField {
            token: token.to_owned(),
            reason: "expected `name:Type` (missing colon)".into(),
        })?;

    let name = name.trim();
    // A trailing `:unique` modifier marks the column for a `CREATE UNIQUE
    // INDEX` in the migration (issue #1032), e.g. `email:String:unique`.
    // Split it off the *end* first: a `references` `{label:col}` constraint
    // modifier (issue #1146) also contains a colon, but it lives inside the
    // trailing `{…}` block, so stripping a literal `:unique` suffix — never
    // the colon inside the braces — stays unambiguous. Any other stray
    // colon outside the braces is caught as an unknown modifier below.
    let rest = rest.trim();
    // Peel a trailing `:states(from -> to, …)` state-machine modifier (issue
    // #1326) *before* the `:unique` split below, so its internal `->`, `,`,
    // and guard `:` tokens are never mistaken for a `:unique` suffix. The
    // clause is paren-delimited (like the `{…}` constraint block is
    // brace-delimited), so it stays unambiguous against the other modifiers.
    let (rest, state_machine_body) = split_state_machine_modifier(rest);
    let state_machine = match state_machine_body {
        Some(body) => {
            Some(
                parse_state_machine(body).map_err(|reason| GenerateError::InvalidField {
                    token: token.to_owned(),
                    reason,
                })?,
            )
        }
        None => None,
    };
    let (ty, unique) = match rest.rsplit_once(':') {
        // A trailing `:unique` (whitespace-tolerant) is the UNIQUE-index
        // modifier. Its colon is always the last one — a `references`
        // `{label:col}` colon sits *inside* the braces before it, so
        // `rsplit_once` never mistakes the label colon for `:unique`.
        Some((before, modifier)) if modifier.trim() == "unique" => (before.trim(), true),
        // Any other trailing `:segment` (including a label colon inside a
        // `{…}` block) is left on `ty` for the constraint/type parsing below
        // to interpret or reject — not an error yet.
        _ => (rest, false),
    };

    if name.is_empty() {
        return Err(GenerateError::InvalidField {
            token: token.to_owned(),
            reason: "field name is empty".into(),
        });
    }
    if !is_valid_ident(name) {
        return Err(GenerateError::InvalidField {
            token: token.to_owned(),
            reason: format!("'{name}' is not a valid snake_case identifier"),
        });
    }
    if is_rust_keyword(name) {
        return Err(GenerateError::InvalidField {
            token: token.to_owned(),
            reason: format!("'{name}' is a Rust keyword and cannot be used as a struct field name"),
        });
    }

    if let Some((variants, nullable)) =
        parse_enum_type(ty).map_err(|reason| GenerateError::InvalidField {
            token: token.to_owned(),
            reason,
        })?
    {
        if state_machine.is_some() {
            return Err(GenerateError::InvalidField {
                token: token.to_owned(),
                reason: STATE_MACHINE_STRING_ONLY.to_owned(),
            });
        }
        return Ok(Field {
            name: name.to_owned(),
            kind: FieldKind::Enum,
            nullable,
            variants,
            unique,
            constraints: FieldConstraints::default(),
            state_machine: None,
        });
    }

    if let Some((precision, scale, nullable)) =
        parse_decimal_type(ty).map_err(|reason| GenerateError::InvalidField {
            token: token.to_owned(),
            reason,
        })?
    {
        if state_machine.is_some() {
            return Err(GenerateError::InvalidField {
                token: token.to_owned(),
                reason: STATE_MACHINE_STRING_ONLY.to_owned(),
            });
        }
        return Ok(Field {
            name: name.to_owned(),
            kind: FieldKind::Decimal { precision, scale },
            nullable,
            variants: Vec::new(),
            unique,
            constraints: FieldConstraints::default(),
            state_machine: None,
        });
    }

    // Fall-through: a scalar / `references` type, optionally carrying a
    // trailing `{…}` constraint-modifier block (issue #1388 validation +
    // HTML5 constraints; issue #1146 `references` `{label:col}`). `enum{…}`
    // and `decimal{…}` were handled above — their braces are part of the
    // *type*, so they never reach this generic modifier split.
    let (base_ty, modifier_body) = split_constraint_modifier(ty);

    // A leftover `{` means an unbalanced brace block (e.g. a shell that
    // brace-expanded `String{min=3,max=120}` into two arguments, leaving a
    // fragment like `String{min=3`). Any leftover `:` outside the braces is a
    // stray trailing modifier (the only supported one, `:unique`, was already
    // consumed above; a `references` label colon lives inside the braces).
    if base_ty.contains('{') {
        return Err(GenerateError::InvalidField {
            token: token.to_owned(),
            reason: format!(
                "malformed constraint modifier in '{ty}' (unbalanced braces). If you typed \
                 this in bash or zsh, quote the whole token so the shell doesn't brace-expand \
                 it, e.g. 'title:String{{min=3,max=120}}'."
            ),
        });
    }
    if let Some((_, bad)) = base_ty.rsplit_once(':') {
        return Err(GenerateError::InvalidField {
            token: token.to_owned(),
            reason: format!(
                "unknown field modifier '{}'; the only bare modifier is 'unique' — other \
                 constraints go in a trailing `{{…}}` block (e.g. 'title:String{{min=3,max=120}}')",
                bad.trim()
            ),
        });
    }

    let (kind, nullable) = parse_type(base_ty).ok_or_else(|| GenerateError::InvalidField {
        token: token.to_owned(),
        reason: format!("unsupported type '{base_ty}'. Supported: {SUPPORTED_TYPES}"),
    })?;

    let constraints = match modifier_body {
        Some(body) => {
            parse_field_constraints(body, kind).map_err(|reason| GenerateError::InvalidField {
                token: token.to_owned(),
                reason,
            })?
        }
        None => FieldConstraints::default(),
    };

    // A state machine (issue #1326) is only meaningful on a plain, non-nullable
    // `String`/`Text` column — the `#[state_machine]` macro requires the field's
    // Rust type to be exactly `String`, and the generated `transition_*` methods
    // operate on `&str`. Reject it anywhere else with a clear message rather than
    // emitting an attribute the macro will refuse to compile.
    if state_machine.is_some() && (nullable || !matches!(kind, FieldKind::String | FieldKind::Text))
    {
        return Err(GenerateError::InvalidField {
            token: token.to_owned(),
            reason: STATE_MACHINE_STRING_ONLY.to_owned(),
        });
    }

    // `references` fields always end in `_id` — `post:references` resolves to
    // the column `post_id`. Tolerate an already-suffixed name (`post_id:references`)
    // rather than doubling the suffix.
    let name = if kind == FieldKind::References && !name.ends_with("_id") {
        format!("{name}_id")
    } else if kind == FieldKind::Commentable {
        // `comments:commentable` names the *feature*, not the column. The
        // column it actually adds is the counter-cache source, whose name
        // follows `#[model]`'s `{snake(child)}_count` convention — so the
        // declared name is singularised and suffixed, and an already-suffixed
        // name is left alone rather than doubled.
        if name.ends_with("_count") {
            name.to_owned()
        } else {
            format!("{}_count", naming::singularize(name))
        }
    } else {
        name.to_owned()
    };

    // `commentable` is a whole feature behind one token, not a value the caller
    // supplies: the counter it adds is maintained by the framework inside each
    // comment's transaction, so it can be neither nullable (`NULL + 1` is
    // `NULL`) nor `:unique` (every model starts at 0), and it takes no `{…}`
    // modifiers at all.
    if kind == FieldKind::Commentable {
        if nullable {
            return Err(GenerateError::InvalidField {
                token: token.to_owned(),
                reason: "a `commentable` field cannot be nullable — it is the \
                         framework-maintained comment counter, and `NULL + 1` is `NULL`"
                    .into(),
            });
        }
        if unique {
            return Err(GenerateError::InvalidField {
                token: token.to_owned(),
                reason: "a `commentable` field cannot be `:unique` — it is a comment \
                         counter, and every model starts at 0"
                    .into(),
            });
        }
        if !constraints.is_empty() {
            return Err(GenerateError::InvalidField {
                token: token.to_owned(),
                reason: "a `commentable` field takes no `{…}` modifiers — configure the \
                         association on the generated model's `#[commentable(...)]` \
                         attribute instead"
                    .into(),
            });
        }
    }

    // A `slug` field (issue #1260) is always the record's routing key: it
    // can never be nullable (every record needs a URL) and always needs a
    // `{from:...}` modifier naming the field it auto-derives from on
    // create — enforced here rather than left to a downstream codegen panic.
    if kind == FieldKind::Slug {
        if nullable {
            return Err(GenerateError::InvalidField {
                token: token.to_owned(),
                reason: "a `slug` field cannot be nullable — it is the record's routing key \
                         and every record needs a URL"
                    .into(),
            });
        }
        if constraints.from.is_none() {
            return Err(GenerateError::InvalidField {
                token: token.to_owned(),
                reason: "a `slug` field requires a `{from:<field>}` modifier naming its \
                         source field, e.g. `slug:slug{from:title}`"
                    .into(),
            });
        }
    }
    // A slug is implicitly unique (it's the routing key) — falls into the
    // existing `unique`-field `UNIQUE INDEX` and `find_by_slug` repository
    // machinery (issue #1032) for free, whether or not `:unique` was typed.
    let unique = unique || kind == FieldKind::Slug;

    // A `position` field (issue #1358) can never be nullable — every row has
    // a place in its ordering — and can never be `:unique` — the whole point
    // is a dense, repository-maintained `0..len-1` sequence per scope, not a
    // one-off distinct value.
    if kind == FieldKind::Position {
        if nullable {
            return Err(GenerateError::InvalidField {
                token: token.to_owned(),
                reason: "a `position` field cannot be nullable — every row has a place in its \
                         ordering"
                    .into(),
            });
        }
        if unique {
            return Err(GenerateError::InvalidField {
                token: token.to_owned(),
                reason: "a `position` field cannot be `:unique` — the repository maintains a \
                         dense `0..len-1` sequence per scope, not a one-off distinct value"
                    .into(),
            });
        }
    }

    // ── `{encrypted}` cross-checks (issue #1340) ────────────────────────────
    // The kind allowlist already ran in `set_encrypted_constraint`; what's left
    // are the checks that need the *whole* parsed field.
    if let Some(mode) = constraints.encrypted {
        // `#[encrypted]` supports non-null `String` columns in v1 — the diesel
        // `serialize_as`/`deserialize_as` wrappers are not threaded through
        // `Option<String>`. Reject here rather than emit a model the macro
        // refuses to compile.
        if nullable {
            return Err(GenerateError::InvalidField {
                token: token.to_owned(),
                reason: "an `encrypted` field cannot be nullable — `#[encrypted]` supports \
                         non-null `String` columns in v1. Drop the `Option<…>` (store an \
                         empty string for \"unset\")."
                    .into(),
            });
        }
        // The concrete blocker: the scaffold's generated transition handler
        // writes the new state with a RAW `diesel::update(...).set(col.eq(
        // new_state))`, which never passes through the `serialize_as` wrapper
        // that encrypts the column — so every transition would store PLAINTEXT
        // in a column the reader then tries to decrypt. (Encrypting a state
        // column is also semantically wrong: it is a low-entropy closed set, so
        // randomized mode makes state queries impossible and deterministic mode
        // leaks every row's state through ciphertext equality.) Refuse the pair
        // rather than half-support it.
        if state_machine.is_some() {
            return Err(GenerateError::InvalidField {
                token: token.to_owned(),
                reason: "an `encrypted` field cannot also declare a `states(…)` state machine: \
                         the generated transition handler writes the new state with a raw \
                         `UPDATE … SET col = 'state'` that bypasses encryption, so transitions \
                         would store plaintext in the encrypted column. (A state column is also \
                         a low-entropy closed set, where randomized mode makes state queries \
                         impossible and deterministic mode leaks every row's state.)"
                    .into(),
            });
        }
        // AC6: a randomized column can never satisfy an equality lookup, so a
        // `UNIQUE` index over it enforces nothing and the free `find_by_<col>`
        // the generators derive from `unique` fails at runtime with
        // `EncryptionError::RandomizedEqualityLookup`. Fail here, where the fix
        // is one word away.
        if unique && mode == EncryptedMode::Randomized {
            return Err(GenerateError::InvalidField {
                token: token.to_owned(),
                reason: randomized_equality_lookup_reason(&name, "is `unique`"),
            });
        }
    }

    // ── `{translatable}` cross-checks (issue #1384) ─────────────────────────
    // The kind allowlist already ran while parsing the modifier; these need the
    // whole parsed field.
    if constraints.translatable {
        // The container itself models "no translation" as an empty map, so a
        // nullable column would give two spellings for one state — and the
        // `#[model]` macro refuses `Option<Translated>` for the same reason.
        if nullable {
            return Err(GenerateError::InvalidField {
                token: token.to_owned(),
                reason: "a `translatable` field cannot be nullable — the per-locale container \
                         already represents \"no translation\" as an empty map. Drop the \
                         `Option<…>`."
                    .into(),
            });
        }
        // The column holds one opaque ciphertext envelope under `{encrypted}`,
        // which has no per-locale structure to resolve against.
        if constraints.encrypted.is_some() {
            return Err(GenerateError::InvalidField {
                token: token.to_owned(),
                reason: "a `translatable` field cannot also be `encrypted`: an encrypted column \
                         stores one opaque envelope, with no per-locale structure to resolve. \
                         Keep a separate encrypted column for the secret."
                    .into(),
            });
        }
        // A UNIQUE index (and the free `find_by_<col>` the generators derive
        // from it) would compare whole JSON containers, so two records with
        // identical text translated into different locale sets never collide.
        if unique {
            return Err(GenerateError::InvalidField {
                token: token.to_owned(),
                reason: "a `translatable` field cannot be `:unique`: the index would compare \
                         whole per-locale containers, so identical text translated into \
                         different locale sets would never collide, and the derived \
                         `find_by_<col>` lookup could never match. Put the uniqueness on a \
                         non-translatable column (e.g. a `slug`)."
                    .into(),
            });
        }
        // The `{min}/{max}/{email}/{url}` fan-out emits `#[validate(length(..))]`
        // / `#[validate(email)]` on the field, and `validator` implements those
        // rules for `String`-shaped types only — there is no impl for a
        // per-locale container, so the generated model would not compile. (And
        // even with one, "which locale is this rule about?" has no single
        // answer.) Refuse here, where the fix is one token away.
        let validation_modifiers = !constraints.validation_attrs_are_empty();
        if validation_modifiers {
            return Err(GenerateError::InvalidField {
                token: token.to_owned(),
                reason: "a `translatable` field cannot carry `min`/`max`/`email`/`url` \
                         constraints: those emit a `#[validate(...)]` rule that `validator` \
                         implements for strings only, and a per-locale container has one \
                         value per locale rather than one string to measure. Validate the \
                         individual translations in your own edit handler."
                    .into(),
            });
        }
        // The scaffold's transition handler writes the state with a raw
        // `UPDATE … SET col = 'state'`, which would replace the whole container
        // with a bare state name. A state is also locale-invariant by
        // definition.
        if state_machine.is_some() {
            return Err(GenerateError::InvalidField {
                token: token.to_owned(),
                reason: "a `translatable` field cannot also declare a `states(…)` state \
                         machine: a state name is locale-invariant, and the generated \
                         transition handler's raw `UPDATE … SET col = 'state'` would replace \
                         the whole per-locale container."
                    .into(),
            });
        }
    }

    Ok(Field {
        name,
        kind,
        nullable,
        variants: Vec::new(),
        unique,
        constraints,
        state_machine,
    })
}

/// The shared explanation for every generate-time refusal of an equality
/// lookup against a **randomized**-encrypted column (issue #1340 AC6).
///
/// `what` is a short verb phrase naming the thing that would perform the
/// lookup — "is `unique`", "has a `--query` derived equality lookup" — so each
/// call site reads naturally while the diagnosis and the fix stay identical.
/// The wording deliberately mirrors the runtime's
/// `EncryptionError::RandomizedEqualityLookup`, so a developer who has met one
/// recognises the other.
#[must_use]
pub(super) fn randomized_equality_lookup_reason(field: &str, what: &str) -> String {
    format!(
        "field '{field}' {what}, but `{{encrypted}}` is randomized: a fresh nonce per write \
         means equal plaintexts never produce equal ciphertext, so the lookup can never match \
         (the runtime raises `EncryptionError::RandomizedEqualityLookup`) and a UNIQUE index \
         over it would enforce nothing. Declare it `{{encrypted:deterministic}}` to support \
         `find_by`/`exists_by` equality lookups and real uniqueness, or drop the lookup."
    )
}

/// Split a scalar/`references` type token into its base type and the body of a
/// trailing `{…}` constraint-modifier block, if present. `("String{min=3}",)`
/// -> `("String", Some("min=3"))`; a token with no (or an unbalanced) block
/// yields `(ty, None)` — the caller then rejects a leftover `{` as malformed.
fn split_constraint_modifier(ty: &str) -> (&str, Option<&str>) {
    let ty = ty.trim();
    if let Some(open) = ty.find('{')
        && ty.ends_with('}')
    {
        let close = ty.len() - 1;
        if close > open {
            return (ty[..open].trim_end(), Some(ty[open + 1..close].trim()));
        }
    }
    (ty, None)
}

/// The single error message used when a `:states(…)` state-machine modifier
/// (issue #1326) is declared on anything but a plain, non-nullable
/// `String`/`Text` field. Shared so the enum, decimal, and scalar branches of
/// [`parse_field`] can never drift.
const STATE_MACHINE_STRING_ONLY: &str =
    "a `states(…)` state machine is only supported on a non-nullable `String`/`Text` field";

/// Split a field's post-`name:` remainder into its leading part and the body of
/// a trailing `:states(…)` state-machine modifier (issue #1326), if present.
///
/// `"String:states(a -> b)"` -> `("String", Some("a -> b"))`;
/// `"String:unique:states(a -> b)"` -> `("String:unique", Some("a -> b"))`;
/// a remainder with no such clause yields `(rest, None)`. The clause is
/// paren-delimited and must be the final modifier (the `)` closes the token),
/// mirroring the trailing-modifier style of `:unique` and the `{…}` block.
fn split_state_machine_modifier(rest: &str) -> (&str, Option<&str>) {
    let rest = rest.trim();
    if let Some(pos) = rest.find(":states(")
        && rest.ends_with(')')
    {
        let before = rest[..pos].trim_end();
        let inner = rest[pos + ":states(".len()..rest.len() - 1].trim();
        return (before, Some(inner));
    }
    (rest, None)
}

/// Parse the inner body of a `:states(…)` modifier — a comma-separated list of
/// `from -> to` edges, each with an optional `: guard` plain-identifier suffix
/// (issue #1326). Mirrors the `#[state_machine(transitions(…))]` macro grammar
/// so the generator can re-emit the parsed set verbatim.
///
/// # Errors
/// Returns a human-readable message when the list is empty, an edge is missing
/// its `->`, or any state/guard token is not a valid `snake_case` identifier.
fn parse_state_machine(body: &str) -> Result<StateMachine, String> {
    let mut transitions = Vec::new();
    for segment in body.split(',') {
        let segment = segment.trim();
        // Tolerate a trailing comma (`a -> b,`) exactly like the macro grammar.
        if segment.is_empty() {
            continue;
        }
        let (from, to_and_guard) = segment.split_once("->").ok_or_else(|| {
            format!("malformed transition '{segment}'; expected `from -> to` (missing `->`)")
        })?;
        // Split an optional `: guard` off the right-hand side. The states clause
        // is peeled whole before any `:unique` handling, so this colon is
        // unambiguously the guard separator.
        let (to, guard) = match to_and_guard.split_once(':') {
            Some((to, guard)) => (to.trim(), Some(guard.trim())),
            None => (to_and_guard.trim(), None),
        };
        let from = from.trim();
        validate_state_token(from, "state")?;
        validate_state_token(to, "state")?;
        if let Some(guard) = guard {
            validate_state_token(guard, "guard")?;
        }
        transitions.push(StateTransition {
            from: from.to_owned(),
            to: to.to_owned(),
            guard: guard.map(str::to_owned),
        });
    }
    if transitions.is_empty() {
        return Err(
            "empty `states(…)` modifier; declare at least one `from -> to` transition".to_owned(),
        );
    }
    Ok(StateMachine { transitions })
}

/// Validate one state name or guard name from a `:states(…)` modifier: it must
/// be a non-empty `snake_case` identifier and not a Rust keyword, since the
/// generator emits states as bare identifiers (and guards as method names)
/// inside the `#[state_machine(transitions(…))]` attribute.
fn validate_state_token(token: &str, kind: &str) -> Result<(), String> {
    if token.is_empty() {
        return Err(format!("empty {kind} name in `states(…)` modifier"));
    }
    if !is_valid_ident(token) || is_rust_keyword(token) {
        return Err(format!(
            "'{token}' is not a valid {kind} name; expected a plain snake_case identifier such \
             as `draft` or `can_publish`"
        ));
    }
    Ok(())
}

/// Format the `min = …, max = …` argument list shared by `length(…)` and
/// `range(…)` `#[validate]` attributes. `float` range bounds are emitted with
/// a decimal point (`0` -> `0.0`) so the generated comparison type-checks
/// against an `f32`/`f64` field.
fn min_max_args(min: Option<&String>, max: Option<&String>, float: bool) -> String {
    let fmt = |raw: &str| -> String {
        if float && !raw.contains(['.', 'e', 'E']) {
            format!("{raw}.0")
        } else {
            raw.to_owned()
        }
    };
    let mut args = Vec::with_capacity(2);
    if let Some(min) = min {
        args.push(format!("min = {}", fmt(min)));
    }
    if let Some(max) = max {
        args.push(format!("max = {}", fmt(max)));
    }
    args.join(", ")
}

/// Ensure a reparsed float's shortest-round-trip string is a valid Rust *float*
/// literal for a `#[validate(range(...))]` bound and the HTML5 `min`/`max`
/// attributes: a whole-number value (`1` from `1`/`+1`/`1.0`, or `1000` from
/// `1e3`) must carry a decimal point (`1.0`), since a bare integer literal is
/// not accepted where an `f32`/`f64` bound is expected. Values that already
/// contain `.`/`e`/`E` (`0.5`, `1.5e3`) are left as-is. Input is assumed finite
/// (the caller rejects `inf`/`NaN`).
fn canonical_float_literal(reparsed: &str) -> String {
    if reparsed.contains(['.', 'e', 'E']) {
        reparsed.to_owned()
    } else {
        format!("{reparsed}.0")
    }
}

/// Parse the body of a `{…}` constraint-modifier block against the field's
/// `kind` (issue #1388 validation + HTML5 constraints; issue #1146
/// `references` `{label:col}`).
///
/// Supported per kind:
/// - `String`/`Text`: `min=N`, `max=N` (length bounds, non-negative
///   integers), `email`, `url`.
/// - `i32`/`i64`/`f32`/`f64`: `min=N`, `max=N` (range bounds).
/// - `references`: `label:col` (or `label=col`) — the display column.
///
/// Every other kind (and every unknown key/flag) is rejected with a message
/// naming the offending token, so a misspelling like `{maxx=5}` fails the
/// scaffold loudly rather than being silently dropped (issue #1388 AC5).
fn parse_field_constraints(body: &str, kind: FieldKind) -> Result<FieldConstraints, String> {
    let mut c = FieldConstraints::default();
    if body.is_empty() {
        return Err(
            "empty constraint block `{}` — remove the braces or add a constraint \
             (e.g. `{min=3,max=120}`, `{email}`, `{label:title}`)"
                .to_owned(),
        );
    }

    // `min`/`max` length bounds apply to every text-shaped column, `richtext`
    // included. The `email`/`url` *format* validators do not: a Markdown body
    // can never satisfy a single-line format rule, so accepting them would emit
    // a field no submission could ever fill (issue #1255).
    let length_bounded = matches!(
        kind,
        FieldKind::String | FieldKind::Text | FieldKind::RichText
    );
    let string_like = matches!(kind, FieldKind::String | FieldKind::Text);
    let numeric = matches!(
        kind,
        FieldKind::I32 | FieldKind::I64 | FieldKind::F32 | FieldKind::F64
    );
    let is_reference = kind.is_reference();
    let is_slug = kind.is_slug();

    for raw in body.split(',') {
        let tok = raw.trim();
        if tok.is_empty() {
            return Err("empty constraint (stray comma) in the `{…}` block".to_owned());
        }
        // `label:col` (references display column) and `from:col` (slug source
        // field) use a colon; every other key/value pair uses `=`. Bare
        // tokens (`email`, `url`) have neither.
        if let Some((key, value)) = tok.split_once('=') {
            parse_constraint_kv(&mut c, key.trim(), value.trim(), kind)?;
        } else if let Some((key, value)) = tok.split_once(':') {
            match key.trim() {
                "label" => set_label_constraint(&mut c, value.trim(), is_reference)?,
                "from" => set_from_constraint(&mut c, value.trim(), is_slug)?,
                "scope" => set_scope_constraint(&mut c, value.trim(), kind.is_position())?,
                // The kind check runs FIRST: on `count:i64{encrypted:bogus}` the
                // useful diagnosis is that `encrypted` doesn't apply to an
                // `i64` at all, not that `bogus` isn't a mode.
                "encrypted" => {
                    reject_encrypted_on_kind(kind)?;
                    reject_repeated_encrypted(&c)?;
                    c.encrypted = Some(EncryptedMode::parse(value.trim())?);
                }
                other => return Err(unknown_constraint_message(other, kind)),
            }
        } else {
            match tok {
                "email" | "url" => {
                    if !string_like {
                        return Err(format!(
                            "the `{tok}` constraint only applies to String/Text fields"
                        ));
                    }
                    if tok == "email" {
                        c.email = true;
                    } else {
                        c.url = true;
                    }
                }
                // A bare `{encrypted}` is the safe default mode, exactly like a
                // bare `#[encrypted]` attribute.
                "encrypted" => {
                    reject_encrypted_on_kind(kind)?;
                    reject_repeated_encrypted(&c)?;
                    c.encrypted = Some(EncryptedMode::Randomized);
                }
                // #1384: per-locale content storage. Bare token only — the
                // storage strategy is the framework's to choose, so unlike
                // `{encrypted}` there is no mode to spell.
                "translatable" => {
                    reject_translatable_on_kind(kind)?;
                    if c.translatable {
                        return Err(
                            "the `translatable` modifier is declared more than once — remove \
                             the duplicate"
                                .to_owned(),
                        );
                    }
                    c.translatable = true;
                }
                _ => return Err(unknown_constraint_message(tok, kind)),
            }
        }
    }

    // `email` and `url` are mutually exclusive format validators: emitting both
    // `#[validate(email)]` and `#[validate(url)]` makes the field unwritable (a
    // valid email fails `url` and vice versa), and the HTML5 renderer can only
    // pick one `type`. Reject the pair rather than silently choosing a winner,
    // which would change the author's intent (issue #1388).
    if c.email && c.url {
        return Err(
            "the `email` and `url` constraints are mutually exclusive — a value can't satisfy \
             both; keep only one"
                .to_owned(),
        );
    }

    // Cross-check the combination against the kind: length/range bounds need a
    // string or numeric field, and require min <= max when both are present.
    if (c.min.is_some() || c.max.is_some()) && !length_bounded && !numeric {
        return Err(format!(
            "min/max constraints are not supported for {} fields",
            kind.rust_type()
        ));
    }
    if let (Some(min), Some(max)) = (&c.min, &c.max) {
        // Compare at the field's CONCRETE width, never via `f64`: a large `i64`
        // pair such as `{min=9007199254740993,max=9007199254740992}` would
        // otherwise round to the same float and slip past this check, emitting
        // an impossible `range(min > max)` that rejects every submitted value
        // instead of failing generation here. Each bound already passed
        // `parse_bound` for `kind`, so these re-parses succeed exactly.
        let inverted = match kind {
            FieldKind::String | FieldKind::Text | FieldKind::RichText => {
                matches!((min.parse::<u64>(), max.parse::<u64>()), (Ok(lo), Ok(hi)) if lo > hi)
            }
            FieldKind::I32 | FieldKind::I64 => {
                matches!((min.parse::<i64>(), max.parse::<i64>()), (Ok(lo), Ok(hi)) if lo > hi)
            }
            // `f32`/`f64`: `f64` compares `f32` values exactly, so this is lossless.
            _ => matches!((min.parse::<f64>(), max.parse::<f64>()), (Ok(lo), Ok(hi)) if lo > hi),
        };
        if inverted {
            return Err(format!("min ({min}) cannot be greater than max ({max})"));
        }
    }

    Ok(c)
}

/// Handle a `key=value` constraint token (`min=`, `max=`, or a `=`-spelled
/// `label=`).
fn parse_constraint_kv(
    c: &mut FieldConstraints,
    key: &str,
    value: &str,
    kind: FieldKind,
) -> Result<(), String> {
    match key {
        "min" | "max" => {
            let bound = parse_bound(value, kind)?;
            if key == "min" {
                c.min = Some(bound);
            } else {
                c.max = Some(bound);
            }
            Ok(())
        }
        "label" => set_label_constraint(c, value, kind.is_reference()),
        "from" => set_from_constraint(c, value, kind.is_slug()),
        _ => Err(unknown_constraint_message(key, kind)),
    }
}

/// Validate and normalize a `min=`/`max=` bound for `kind`: a length bound on
/// a `String`/`Text` field must be a non-negative integer; a range bound on a
/// numeric field must be a valid number.
fn parse_bound(value: &str, kind: FieldKind) -> Result<String, String> {
    // Every arm returns the CANONICAL reparsed value, never the raw user token:
    // the stored bound is spliced verbatim into both `#[validate(range/length(…))]`
    // and the HTML5 `min`/`max`/`minlength`/`maxlength` attributes, so a token
    // that parses but isn't a valid Rust literal (`.5`, `+1`, `007`, `1.`) would
    // otherwise fail to COMPILE the generated app. Reparsing at the field's
    // concrete type and re-`to_string()`-ing yields a literal that is always
    // valid and value-preserving (issue #1388 follow-up).
    match kind {
        FieldKind::String | FieldKind::Text | FieldKind::RichText => {
            let n = value
                .parse::<u64>()
                .map_err(|_| format!("length bound '{value}' must be a non-negative integer"))?;
            if n > u64::from(u32::MAX) {
                return Err(format!(
                    "length bound '{value}' must be at most {} (HTML length attributes fit a u32)",
                    u32::MAX
                ));
            }
            Ok(n.to_string())
        }
        FieldKind::I32 => {
            // Parse at the field's CONCRETE width, not a wider `i64`: a bound
            // that overflows `i32` (e.g. `count:i32{max=3000000000}`) would
            // otherwise be emitted as `#[validate(range(max = ...))]` on an
            // `i32` field and fail to COMPILE the generated app.
            let n = value.parse::<i32>().map_err(|_| {
                format!(
                    "range bound '{value}' must be an integer within the i32 range ({}..={})",
                    i32::MIN,
                    i32::MAX
                )
            })?;
            Ok(n.to_string())
        }
        FieldKind::I64 => {
            let n = value.parse::<i64>().map_err(|_| {
                format!(
                    "range bound '{value}' must be an integer within the i64 range ({}..={})",
                    i64::MIN,
                    i64::MAX
                )
            })?;
            Ok(n.to_string())
        }
        FieldKind::F32 => {
            // Rust's float parse saturates overflow to ±∞ rather than erroring,
            // so an out-of-`f32`-range literal would compile to a non-finite
            // bound; reject non-finite explicitly, then canonicalize.
            let parsed = value
                .parse::<f32>()
                .map_err(|_| format!("range bound '{value}' must be a number"))?;
            if !parsed.is_finite() {
                return Err(format!(
                    "range bound '{value}' is out of range for an f32 field (it overflows to a non-finite value)"
                ));
            }
            Ok(canonical_float_literal(&parsed.to_string()))
        }
        FieldKind::F64 => {
            let parsed = value
                .parse::<f64>()
                .map_err(|_| format!("range bound '{value}' must be a number"))?;
            if !parsed.is_finite() {
                return Err(format!(
                    "range bound '{value}' is out of range for an f64 field (it overflows to a non-finite value)"
                ));
            }
            Ok(canonical_float_literal(&parsed.to_string()))
        }
        _ => Err(format!(
            "min/max constraints are not supported for {} fields",
            kind.rust_type()
        )),
    }
}

/// Set the `references` display-column override, rejecting `label` on a
/// non-`references` field and a label that isn't a valid `snake_case` column.
fn set_label_constraint(
    c: &mut FieldConstraints,
    value: &str,
    is_reference: bool,
) -> Result<(), String> {
    if !is_reference {
        return Err("the `label` constraint only applies to `references` fields".to_owned());
    }
    if !is_valid_ident(value) {
        return Err(format!(
            "reference label column '{value}' is not a valid snake_case identifier"
        ));
    }
    c.label = Some(value.to_owned());
    Ok(())
}

/// Set the `slug` source-field override (issue #1260), rejecting `from` on a
/// non-`slug` field and a value that isn't a valid `snake_case` identifier.
fn set_from_constraint(c: &mut FieldConstraints, value: &str, is_slug: bool) -> Result<(), String> {
    if !is_slug {
        return Err("the `from` constraint only applies to `slug` fields".to_owned());
    }
    if !is_valid_ident(value) {
        return Err(format!(
            "slug source field '{value}' is not a valid snake_case identifier"
        ));
    }
    c.from = Some(value.to_owned());
    Ok(())
}

/// Set the `position` scope column (issue #1358), rejecting `scope` on a
/// non-`position` field and a value that isn't a valid `snake_case`
/// identifier.
fn set_scope_constraint(
    c: &mut FieldConstraints,
    value: &str,
    is_position: bool,
) -> Result<(), String> {
    if !is_position {
        return Err("the `scope` constraint only applies to `position` fields".to_owned());
    }
    if !is_valid_ident(value) {
        return Err(format!(
            "position scope column '{value}' is not a valid snake_case identifier"
        ));
    }
    c.scope = Some(value.to_owned());
    Ok(())
}

/// Reject a `{encrypted}` modifier on any field kind the `#[encrypted]`
/// attribute macro does not support (issue #1340).
///
/// v1 of the encryption runtime accepts **non-null `String` columns only**
/// (`autumn-macros`' `validate_encrypted_field`): the plaintext is held in
/// memory as a `String` and round-trips through the `RandomizedText` /
/// `DeterministicText` diesel wrappers. So `Bytea`, numerics, `enum{…}`,
/// `decimal{…}`, `references`, `richtext`, `slug` and friends are refused here
/// rather than emitted as a model the macro would then refuse to compile.
/// (The issue's AC text mentions `bytea`; the runtime does not support it, so
/// the DSL says so plainly instead of generating broken code.) The nullability
/// half of that rule is enforced in [`parse_field`], which is where the
/// `Option<…>` wrapper has already been peeled off.
/// Refuse a second `encrypted` modifier in the same `{…}` block (issue #1340).
///
/// The other repeatable modifiers last-win harmlessly (`{min=1,min=2}` is just
/// a sloppier `{min=2}`), but a repeated `encrypted` silently changes the
/// *security* of the column in whichever direction the last token happens to
/// name: `{encrypted,encrypted:deterministic}` quietly upgrades a randomized
/// declaration to deterministic — trading away plaintext-equality hiding — and
/// `{encrypted:deterministic,encrypted}` quietly takes away the equality
/// lookups the author asked for. Neither should be inferred from token order.
fn reject_repeated_encrypted(c: &FieldConstraints) -> Result<(), String> {
    if c.encrypted.is_some() {
        return Err(
            "the `encrypted` modifier is declared more than once — the modes differ in what \
             they leak, so the mode must be unambiguous. Keep exactly one of `encrypted` \
             (randomized) or `encrypted:deterministic`."
                .to_owned(),
        );
    }
    Ok(())
}

fn reject_encrypted_on_kind(kind: FieldKind) -> Result<(), String> {
    if matches!(kind, FieldKind::String | FieldKind::Text) {
        return Ok(());
    }
    // Name the DSL token, not `rust_type()` — `richtext`, `enum{…}` and `slug`
    // all render as `String` in Rust, so reporting the Rust type would produce
    // the self-contradicting "only applies to `String` … Got a `String` field".
    Err(format!(
        "the `encrypted` modifier only applies to `String`/`Text` fields; \
         `#[encrypted]` supports non-null `String` columns in v1 (encrypt structured \
         data before storing it). Got a `{}` field.",
        kind.dsl_token()
    ))
}

/// `{translatable}` stores a JSON container of per-locale values in the
/// column, so the column has to be free-form text. Every other kind either has
/// no text to translate (numbers, timestamps, foreign keys) or is a machine
/// identifier whose value must stay stable across locales (`slug`, `enum`).
fn reject_translatable_on_kind(kind: FieldKind) -> Result<(), String> {
    if matches!(kind, FieldKind::String | FieldKind::Text) {
        return Ok(());
    }
    // Name the DSL token, not `rust_type()` — `richtext`, `enum{…}` and `slug`
    // all render as `String` in Rust (see `reject_encrypted_on_kind`).
    Err(format!(
        "the `translatable` modifier only applies to `String`/`Text` fields; it stores a \
         per-locale JSON container in the column, which needs free-form text. Got a `{}` \
         field.",
        kind.dsl_token()
    ))
}

/// A per-kind "unknown constraint" message that names the offending token and
/// lists what the kind *does* accept (issue #1388 AC5).
fn unknown_constraint_message(token: &str, kind: FieldKind) -> String {
    let accepted = match kind {
        FieldKind::String | FieldKind::Text => {
            "min=N, max=N, email, url, encrypted, encrypted:deterministic, translatable"
        }
        // `RichText` shares the numeric arm's accepted set, not `String`'s: it
        // takes the `min`/`max` length bounds but NOT the `email`/`url` format
        // validators, which a Markdown body could never satisfy (issue #1255).
        FieldKind::RichText | FieldKind::I32 | FieldKind::I64 | FieldKind::F32 | FieldKind::F64 => {
            "min=N, max=N"
        }
        FieldKind::References => "label:col",
        FieldKind::Slug => "from:col",
        FieldKind::Position => "scope:col",
        _ => "(none — this field type takes no constraint modifiers)",
    };
    // Reported by the DSL token the author actually typed (`commentable`),
    // not by the Rust type it happens to lower to (`i64`) — the latter reads
    // like a different field entirely.
    format!(
        "unknown constraint '{token}' for `{}` fields; supported: {accepted}",
        kind.dsl_token()
    )
}

/// Parse an `enum{a,b,c}` (optionally `Option<enum{a,b,c}>`) type token.
///
/// Returns `Ok(None)` when `ty` isn't an enum token at all, so the caller
/// falls through to [`parse_type`]/[`atomic_type`] unchanged. Returns
/// `Err(reason)` when `ty` looks like an enum token but is malformed (bad
/// variant, too few variants, unbalanced braces, …) — every reason is an
/// actionable message, consistent with the field-name guarding above.
///
/// # Errors
/// See above.
fn parse_enum_type(ty: &str) -> Result<Option<(Vec<String>, bool)>, String> {
    let (body, nullable) =
        strip_wrapper(ty, "Option").map_or((ty, false), |inner| (inner.trim(), true));

    let Some(rest) = body.strip_prefix("enum") else {
        return Ok(None);
    };
    let rest = rest.trim_start();

    let Some(inner) = rest.strip_prefix('{') else {
        // Looks like an enum token (starts with `enum`) but has no opening
        // brace. The most common cause is bash/zsh brace-expanding an
        // unquoted `enum{a,b}` before the CLI ever sees it, turning
        // `status:enum{draft,published}` into two separate arguments whose
        // surviving fragment reads like `status:enumdraft` — but `ty` could
        // also just be an unrelated typo (e.g. `enumerable`), so the message
        // covers both: the quoting hint for the shell-expansion case, and
        // the full supported-types list for a genuine typo.
        return Err(format!(
            "expected enum{{variant1,variant2,…}}. If you typed this in bash or zsh, \
             quote the token so the shell doesn't brace-expand it, \
             e.g. 'status:enum{{draft,published}}'. Supported: {SUPPORTED_TYPES}"
        ));
    };
    let Some(body_inner) = inner.strip_suffix('}') else {
        return Err("expected enum{variant1,variant2,…} (missing closing brace)".to_owned());
    };

    let mut variants: Vec<String> = Vec::new();
    let mut seen_pascal = std::collections::HashSet::new();
    for raw in body_inner.split(',') {
        let variant = raw.trim();
        if variant.is_empty() {
            return Err("enum variants cannot be empty".to_owned());
        }
        if !is_valid_ident(variant) {
            return Err(format!("'{variant}' is not a valid snake_case identifier"));
        }
        if is_rust_keyword(variant) {
            return Err(format!(
                "'{variant}' is a Rust keyword and cannot be used as an enum variant"
            ));
        }
        let pascal = naming::pascal(variant);
        // `is_valid_ident` allows a leading/all-underscore variant (e.g.
        // `_2fa`, `__`), but `pascal()` strips leading underscores without
        // introducing a letter, which can leave an empty string or a result
        // starting with a digit — neither is a valid Rust identifier, so the
        // generated enum variant would fail to compile.
        if pascal.is_empty() || pascal.starts_with(|c: char| c.is_ascii_digit()) {
            return Err(format!(
                "'{variant}' does not produce a valid Rust identifier once converted to \
                 PascalCase ('{pascal}'); rename the variant"
            ));
        }
        if !seen_pascal.insert(pascal.clone()) {
            return Err(format!(
                "duplicate enum variant '{pascal}' (variants must be distinct once converted to PascalCase)"
            ));
        }
        variants.push(variant.to_owned());
    }

    if variants.len() < 2 {
        return Err(
            "an enum needs at least two variants; use String for a free-form field".to_owned(),
        );
    }

    Ok(Some((variants, nullable)))
}

/// The maximum `decimal` precision (total significant digits) this DSL
/// accepts. Bounded by `rust_decimal::Decimal`'s own representable range —
/// a 96-bit mantissa, guaranteeing at most 28 significant digits — not by
/// Postgres's much larger `NUMERIC` limit (1000): a column declared wider
/// than `rust_decimal` can hold would compile and migrate cleanly, then fail
/// at runtime the first time a value actually needing that extra precision
/// is deserialized into the generated `#[model]` field.
const MAX_DECIMAL_PRECISION: u32 = 28;

/// Parse a `decimal` (optionally `Option<decimal>`) type token, with an
/// optional `{precision,scale}` modifier (`decimal{10,2}`). Accepts both
/// `decimal`/`Decimal` casings, consistent with `Attachment`/`attachment`.
/// Defaults to `{12,2}` — a money-shaped `NUMERIC(12,2)` — when the modifier
/// is omitted.
///
/// Returns `Ok(None)` when `ty` isn't a decimal token at all, so the caller
/// falls through to [`parse_type`]/[`atomic_type`] unchanged. Returns
/// `Err(reason)` when `ty` looks like a decimal token but is malformed
/// (missing/unbalanced braces, non-numeric precision/scale, scale exceeding
/// precision, precision outside `rust_decimal`'s representable range, …) —
/// every reason is an actionable message, consistent with the field-name
/// guarding above.
///
/// # Errors
/// See above.
fn parse_decimal_type(ty: &str) -> Result<Option<(u32, u32, bool)>, String> {
    let (body, nullable) =
        strip_wrapper(ty, "Option").map_or((ty, false), |inner| (inner.trim(), true));
    // Defensive: every caller of `parse_field` already trims the outer `ty`
    // before it reaches here, but trimming `body` again costs nothing and
    // guards against a caller that doesn't (e.g. a direct unit-test call, or
    // future refactor) — see the shell-brace-expansion hint below, which
    // depends on `body` ending exactly at `}` with no trailing whitespace.
    let body = body.trim();

    let Some(rest) = body
        .strip_prefix("decimal")
        .or_else(|| body.strip_prefix("Decimal"))
    else {
        return Ok(None);
    };
    let rest = rest.trim_start();

    if rest.is_empty() {
        // Bare `decimal`/`Decimal`: default to a money-shaped NUMERIC(12,2).
        return Ok(Some((12, 2, nullable)));
    }

    let Some(inner) = rest.strip_prefix('{') else {
        // Looks like a decimal token (starts with `decimal`/`Decimal` and
        // has more text after it) but has no opening brace. As with
        // `enum{...}`, the most common cause is bash/zsh brace-expanding an
        // unquoted `decimal{10,2}` before the CLI ever sees it, turning
        // `price:decimal{10,2}` into two separate arguments whose surviving
        // fragments read like `price:decimal10` and `price:decimal2` — but
        // `ty` could also just be an unrelated typo (e.g. `decimalize`), so
        // the message covers both: the quoting hint for the shell-expansion
        // case, and the full supported-types list for a genuine typo.
        return Err(format!(
            "expected decimal{{precision,scale}} (or bare `decimal` for the \
             default NUMERIC(12,2)). If you typed this in bash or zsh, quote \
             the token so the shell doesn't brace-expand it, e.g. \
             'price:decimal{{10,2}}'. Supported: {SUPPORTED_TYPES}"
        ));
    };
    let Some(body_inner) = inner.strip_suffix('}') else {
        return Err("expected decimal{precision,scale} (missing closing brace)".to_owned());
    };

    let parts: Vec<&str> = body_inner.split(',').map(str::trim).collect();
    let [precision_str, scale_str] = parts.as_slice() else {
        return Err(format!(
            "expected decimal{{precision,scale}} (exactly two comma-separated numbers), \
             got 'decimal{{{body_inner}}}'"
        ));
    };

    let precision: u32 = precision_str.parse().map_err(|_| {
        format!(
            "'{precision_str}' is not a valid decimal precision \
             (expected a positive integer)"
        )
    })?;
    let scale: u32 = scale_str.parse().map_err(|_| {
        format!("'{scale_str}' is not a valid decimal scale (expected a non-negative integer)")
    })?;

    if precision == 0 || precision > MAX_DECIMAL_PRECISION {
        return Err(format!(
            "decimal precision must be between 1 and {MAX_DECIMAL_PRECISION} \
             (rust_decimal's representable range), got {precision}"
        ));
    }
    if scale > precision {
        return Err(format!(
            "decimal scale ({scale}) cannot be greater than precision ({precision})"
        ));
    }

    Ok(Some((precision, scale, nullable)))
}

/// Parse a list of `name:Type` tokens.
///
/// Each token is `name:Type` with optional trailing modifiers: `:unique`
/// (issue #1032), a `{…}` constraint block (issue #1388 / #1146), and — for a
/// non-nullable `String`/`Text` field — a `:states(…)` state-machine modifier
/// (issue #1326). The state-machine clause lists `from -> to` edges, each with
/// an optional `: guard` method name, e.g.
/// `status:String:states(draft -> published: can_publish, published -> archived)`.
/// It re-emits as a `#[state_machine(transitions(…))]` attribute on the field.
///
/// # Errors
/// Bubbles up the first failed token, rejects duplicate field names —
/// emitting two entries with the same column name would produce duplicate
/// struct members and duplicate SQL columns — and (issue #1260) validates
/// every `slug{from:...}` field's cross-field constraints: at most one slug
/// field per model (it's the routing key), and its `from` must name a
/// declared `String`/`Text`/`richtext` field (declaration order doesn't
/// matter — a slug may derive from a field declared later in the list).
pub fn parse_fields(tokens: &[String]) -> Result<Vec<Field>, GenerateError> {
    let mut fields: Vec<Field> = Vec::with_capacity(tokens.len());
    for token in tokens {
        let field = parse_field(token)?;
        if let Some(prev) = fields.iter().find(|f| f.name == field.name) {
            return Err(GenerateError::InvalidField {
                token: token.clone(),
                reason: format!(
                    "duplicate field name '{name}' (previously declared as '{name}:{prev_ty}')",
                    name = field.name,
                    prev_ty = prev.rust_type()
                ),
            });
        }
        fields.push(field);
    }
    validate_slug_fields(tokens, &fields)?;
    validate_position_fields(tokens, &fields)?;
    validate_commentable_fields(tokens, &fields)?;
    Ok(fields)
}

/// Cross-field validation for every `position`/`position{scope:...}` field in
/// `fields` (issue #1358) — see [`parse_fields`]. Runs after every token has
/// parsed individually, since `scope`'s target may be declared earlier OR
/// later in the token list.
fn validate_position_fields(tokens: &[String], fields: &[Field]) -> Result<(), GenerateError> {
    let position_fields: Vec<(usize, &Field)> = fields
        .iter()
        .enumerate()
        .filter(|(_, f)| f.kind.is_position())
        .collect();
    if let [(_, first), (second_idx, second), ..] = position_fields[..] {
        return Err(GenerateError::InvalidField {
            token: tokens[second_idx].clone(),
            reason: format!(
                "only one `position` field is supported per model — found both '{}' and '{}'",
                first.name, second.name
            ),
        });
    }
    for (idx, position_field) in &position_fields {
        let Some(scope) = position_field.constraints.scope.as_deref() else {
            continue;
        };
        let token = tokens[*idx].clone();
        let Some(source) = fields.iter().find(|f| f.name == scope) else {
            return Err(GenerateError::InvalidField {
                token,
                reason: format!(
                    "position field '{}' is scoped `scope:{scope}`, but no field named \
                     '{scope}' is declared",
                    position_field.name
                ),
            });
        };
        if !source.kind.is_reference() {
            return Err(GenerateError::InvalidField {
                token,
                reason: format!(
                    "position field '{}' is scoped `scope:{scope}`, but '{scope}' is a {} \
                     field — position can only be scoped to a `references` foreign key",
                    position_field.name,
                    source.rust_type()
                ),
            });
        }
        // A nullable scope FK renders as `Option<i64>` at the Rust level,
        // but the generated triggers/`move_to` compare the scope column via
        // Postgres `hashtext(NEW."{scope}"::text)` / `"{scope}" = $1`
        // equality, both of which are SQL-NULL-strict: a NULL scope value
        // hashes to NULL (taking no advisory lock at all) and never equals
        // itself in a `WHERE` filter, so every NULL-scoped row would
        // silently get position 0 with no working compaction. Reject the
        // combination outright rather than emit ordering that quietly
        // breaks the moment a scope value is unset.
        if source.nullable {
            return Err(GenerateError::InvalidField {
                token,
                reason: format!(
                    "position field '{}' is scoped `scope:{scope}`, but '{scope}' is nullable \
                     (`Option<…>`) — a NULL scope value can't be locked or matched by the \
                     generated triggers/`move_to`. Make '{scope}' non-nullable, or drop the \
                     `references` field's `:null`.",
                    position_field.name
                ),
            });
        }
    }
    Ok(())
}

/// Cross-field validation for `commentable` fields (issue #1367) — see
/// [`parse_fields`].
///
/// At most one per model: a second token would mean a second counter column
/// *and* a second `#[commentable]` attribute, and `#[model]` rejects the
/// latter outright (both would generate the same `{Model}Comments` trait). The
/// DSL refuses first so the error names the token rather than surfacing as a
/// macro error in generated code the author did not write.
fn validate_commentable_fields(tokens: &[String], fields: &[Field]) -> Result<(), GenerateError> {
    let commentable: Vec<(usize, &Field)> = fields
        .iter()
        .enumerate()
        .filter(|(_, f)| f.kind.is_commentable())
        .collect();
    if let [(_, first), (second_idx, second), ..] = commentable[..] {
        return Err(GenerateError::InvalidField {
            token: tokens[second_idx].clone(),
            reason: format!(
                "only one `commentable` field is supported per model — found both '{}' and \
                 '{}'. One shared comments table already attaches to every model; a second \
                 token would add a second counter column and a second `#[commentable]` \
                 attribute, which `#[model]` rejects.",
                first.name, second.name
            ),
        });
    }
    Ok(())
}

/// Cross-field validation for every `slug{from:...}` field in `fields`
/// (issue #1260) — see [`parse_fields`]. Runs after every token has parsed
/// individually, since `from`'s target may be declared earlier OR later in
/// the token list.
fn validate_slug_fields(tokens: &[String], fields: &[Field]) -> Result<(), GenerateError> {
    // `parse_fields` builds `fields` from `tokens` one-to-one with no skips,
    // so `tokens[i]` is always the token that produced `fields[i]` — no need
    // to rediscover the index via a linear name search below.
    let slug_fields: Vec<(usize, &Field)> = fields
        .iter()
        .enumerate()
        .filter(|(_, f)| f.kind.is_slug())
        .collect();
    if let [(_, first), (second_idx, second), ..] = slug_fields[..] {
        return Err(GenerateError::InvalidField {
            token: tokens[second_idx].clone(),
            reason: format!(
                "only one `slug` field is supported per model (it's the routing key) — \
                 found both '{}' and '{}'",
                first.name, second.name
            ),
        });
    }
    for (idx, slug_field) in &slug_fields {
        // Presence of `constraints.from` is already enforced per-token in
        // `parse_field`; this is the cross-field half of that check.
        let from = slug_field
            .constraints
            .from
            .as_deref()
            .expect("parse_field rejects a slug field with no `from` constraint");
        let token = tokens[*idx].clone();
        let Some(source) = fields.iter().find(|f| f.name == from) else {
            return Err(GenerateError::InvalidField {
                token,
                reason: format!(
                    "slug field '{}' derives `from:{from}`, but no field named '{from}' is \
                     declared",
                    slug_field.name
                ),
            });
        };
        if !matches!(
            source.kind,
            FieldKind::String | FieldKind::Text | FieldKind::RichText
        ) {
            return Err(GenerateError::InvalidField {
                token,
                reason: format!(
                    "slug field '{}' derives `from:{from}`, but '{from}' is a {} field — \
                     slug can only derive from a String/Text/richtext field",
                    slug_field.name,
                    source.rust_type()
                ),
            });
        }
        // Issue #1340: a slug is derived by `slugify`ing the source value and
        // is then stored in its OWN plaintext, UNIQUE-indexed column *and* used
        // as the record's route key. Deriving one from an `{encrypted}` column
        // would therefore write a lightly-mangled copy of the secret to disk in
        // the clear (defeating the encryption entirely), leak plaintext
        // equality through the unique index, and put it in URL paths — where it
        // reaches access logs, `Referer` headers, browser history, and proxy
        // caches. Refuse the pair outright; there is no mode of encryption that
        // makes it safe.
        if source.is_encrypted() {
            return Err(GenerateError::InvalidField {
                token,
                reason: format!(
                    "slug field '{}' derives `from:{from}`, but '{from}' is `{{encrypted}}`: \
                     the slug is stored in its own PLAINTEXT column and used as the record's \
                     URL, so deriving it from an encrypted column would write the secret to \
                     disk in the clear and put it in every request path. Derive the slug from \
                     a non-encrypted field instead.",
                    slug_field.name
                ),
            });
        }
        // A nullable source renders as `Option<String>` on the generated
        // `New*` insert struct, but `autumn_web::slugify` takes `&str` — the
        // create handler's `slugify(&new.{from})` would not compile.
        if source.nullable {
            return Err(GenerateError::InvalidField {
                token,
                reason: format!(
                    "slug field '{}' derives `from:{from}`, but '{from}' is nullable \
                     (`Option<…>`) — slug can only derive from a non-nullable field",
                    slug_field.name
                ),
            });
        }
    }
    Ok(())
}

fn parse_type(ty: &str) -> Option<(FieldKind, bool)> {
    // A trailing `?` is a terser nullable marker than `Option<…>`, but is only
    // recognized for `references` (`post:references?`) — every other type
    // must use `Option<…>` for nullability, so this doesn't silently expand
    // the DSL's accepted grammar (e.g. `count:i64?` stays an error).
    if matches!(
        ty.strip_suffix('?').map(str::trim),
        Some("references" | "References")
    ) {
        return Some((FieldKind::References, true));
    }
    if let Some(inner) = strip_wrapper(ty, "Option") {
        let kind = atomic_type(inner.trim())?;
        Some((kind, true))
    } else {
        atomic_type(ty).map(|k| {
            // Attachment fields are always nullable: a file attachment is
            // almost universally optional (a post might not have a cover image),
            // and `Option<Blob>` is the idiomatic Rust representation.
            let nullable = matches!(k, FieldKind::Attachment);
            (k, nullable)
        })
    }
}

fn atomic_type(ty: &str) -> Option<FieldKind> {
    match ty {
        "String" => Some(FieldKind::String),
        "Text" => Some(FieldKind::Text),
        "i32" => Some(FieldKind::I32),
        "i64" => Some(FieldKind::I64),
        "bool" => Some(FieldKind::Bool),
        "f32" => Some(FieldKind::F32),
        "f64" => Some(FieldKind::F64),
        "Uuid" => Some(FieldKind::Uuid),
        "NaiveDateTime" => Some(FieldKind::NaiveDateTime),
        "DateTime" => Some(FieldKind::DateTime),
        "Bytea" => Some(FieldKind::Bytea),
        // Attachment / attachment: file-attachment blob stored as JSONB.
        // Accept both casing variants so `cover_image:Attachment` and
        // `cover_image:attachment` both work.
        "Attachment" | "attachment" => Some(FieldKind::Attachment),
        // References / references: foreign-key column, resolved to `_id` and
        // `BIGINT REFERENCES <table>(id)` by the callers that emit SQL.
        "References" | "references" => Some(FieldKind::References),
        // richtext (issue #1255): a TEXT column holding user-submitted Markdown
        // that the generated views render through the sanitizing
        // `markdown::render_user_content`. All three spellings are accepted so
        // the token reads naturally however the author types it.
        "richtext" | "RichText" | "rich_text" => Some(FieldKind::RichText),
        // slug (issue #1260): a URL-safe routing key auto-derived from
        // another field, e.g. `slug:slug{from:title}`.
        "slug" | "Slug" => Some(FieldKind::Slug),
        // json / jsonb (issue #1341): arbitrary structured data stored as
        // `serde_json::Value`. `jsonb` is accepted as an alias per the AC —
        // both spellings (and their PascalCase forms, consistent with
        // `Attachment`/`attachment` and `decimal`/`Decimal`) map to the same
        // kind; there is no separate non-binary "json" storage distinction on
        // the Postgres side (the column is always `JSONB`).
        "json" | "Json" | "jsonb" | "Jsonb" => Some(FieldKind::Json),
        // position (issue #1358): a server-managed contiguous ordering
        // column, e.g. `rank:position` or `rank:position{scope:board_id}`.
        "position" | "Position" => Some(FieldKind::Position),
        "commentable" | "Commentable" => Some(FieldKind::Commentable),
        _ => {
            // Allow `Vec<u8>` as a synonym for `Bytea`.
            strip_wrapper(ty, "Vec").and_then(|inner| {
                if inner.trim() == "u8" {
                    Some(FieldKind::Bytea)
                } else {
                    None
                }
            })
        }
    }
}

fn strip_wrapper<'a>(ty: &'a str, wrapper: &str) -> Option<&'a str> {
    let prefix = format!("{wrapper}<");
    let stripped = ty.strip_prefix(&prefix)?;
    stripped.strip_suffix('>')
}

pub(super) fn is_valid_ident(s: &str) -> bool {
    // A bare `_` is the reserved wildcard, not a usable field/module name
    // (`pub _: T` does not compile), so reject it explicitly.
    if s == "_" {
        return false;
    }
    let mut chars = s.chars();
    let Some(first) = chars.next() else {
        return false;
    };
    if !(first.is_ascii_lowercase() || first == '_') {
        return false;
    }
    chars.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_')
}

/// Strict and reserved Rust keywords that cannot appear as a struct field name
/// or module name without raw-identifier syntax. Rather than emitting `r#type:`
/// we reject the input so the generator never produces broken code.
///
/// Public so the resource-name validator in [`super::model`] can share the same
/// list.
pub(super) const RUST_KEYWORDS: &[&str] = &[
    "as", "async", "await", "break", "const", "continue", "crate", "do", "dyn", "else", "enum",
    "extern", "false", "fn", "for", "gen", "if", "impl", "in", "let", "loop", "match", "mod",
    "move", "mut", "pub", "ref", "return", "self", "static", "struct", "super", "trait", "true",
    "try", "type", "unsafe", "use", "where", "while", "yield", "abstract", "become", "box",
    "final", "macro", "override", "priv", "typeof", "unsized", "virtual",
];

pub(super) fn is_rust_keyword(s: &str) -> bool {
    RUST_KEYWORDS.contains(&s)
}

#[cfg(test)]
// Test inputs like `"title:String{min=3}"` / `"post:references{label:title}"`
// are literal DSL tokens passed to `parse_field`, not format strings — the
// `{…}` is the scaffold's own constraint-modifier syntax under test.
#[allow(clippy::literal_string_with_formatting_args)]
mod tests {
    use super::*;

    // ── #1384: `{translatable}` modifier ────────────────────────────────────

    #[test]
    fn translatable_modifier_parses_on_string_and_text() {
        for token in ["title:String{translatable}", "body:Text{translatable}"] {
            let f = parse_field(token).unwrap_or_else(|e| panic!("{token}: {e}"));
            assert!(f.is_translatable(), "{token}");
            // The Rust type becomes the per-locale container...
            assert_eq!(f.rust_type(), "autumn_web::i18n::Translated");
            // ...while the column stays a portable TEXT holding a JSON object.
            assert_eq!(f.sql_column_type(), "TEXT");
            assert_eq!(f.schema_type(), "Text");
            assert_eq!(f.sql_nullability(), "NOT NULL");
            assert_eq!(f.sql_default(), Some("'{}'"));
        }
    }

    #[test]
    fn translatable_modifier_is_rejected_on_non_text_kinds() {
        for token in [
            "count:i64{translatable}",
            "at:NaiveDateTime{translatable}",
            "post:references{translatable}",
        ] {
            let err = parse_field(token).unwrap_err().to_string();
            assert!(
                err.contains("translatable"),
                "{token} should name the modifier: {err}"
            );
        }
    }

    #[test]
    fn translatable_is_rejected_on_a_nullable_column() {
        // The container already models "no translation" as an empty map.
        let err = parse_field("title:Option<String>{translatable}")
            .unwrap_err()
            .to_string();
        assert!(err.contains("translatable"), "{err}");
    }

    #[test]
    fn translatable_and_encrypted_are_mutually_exclusive() {
        let err = parse_field("title:String{translatable,encrypted}")
            .unwrap_err()
            .to_string();
        assert!(err.contains("encrypted"), "{err}");
    }

    #[test]
    fn translatable_is_rejected_with_unique() {
        // A UNIQUE index over a JSON container compares whole containers.
        let err = parse_field("title:String:unique{translatable}")
            .unwrap_err()
            .to_string();
        assert!(err.contains("translatable"), "{err}");
    }

    #[test]
    fn translatable_is_rejected_with_a_state_machine() {
        let err = parse_field("status:String{translatable}:states(draft -> live)")
            .unwrap_err()
            .to_string();
        assert!(err.contains("translatable"), "{err}");
    }

    #[test]
    fn a_repeated_translatable_modifier_is_rejected() {
        let err = parse_field("title:String{translatable,translatable}")
            .unwrap_err()
            .to_string();
        assert!(err.contains("translatable"), "{err}");
    }

    #[test]
    fn non_translatable_fields_keep_their_type_and_have_no_default() {
        let f = parse_field("title:String").unwrap();
        assert!(!f.is_translatable());
        assert_eq!(f.rust_type(), "String");
        assert_eq!(f.sql_default(), None);
    }

    #[test]
    fn unknown_constraint_message_advertises_translatable() {
        let err = parse_field("title:String{bogus}").unwrap_err().to_string();
        assert!(err.contains("translatable"), "{err}");
    }

    #[test]
    fn parse_string_field() {
        let f = parse_field("title:String").unwrap();
        assert_eq!(f.name, "title");
        assert_eq!(f.kind, FieldKind::String);
        assert!(!f.nullable);
        assert_eq!(f.rust_type(), "String");
        assert_eq!(f.sql_type(), "TEXT");
        assert_eq!(f.schema_type(), "Text");
    }

    #[test]
    fn parse_text_alias() {
        let f = parse_field("body:Text").unwrap();
        assert_eq!(f.kind, FieldKind::Text);
        assert_eq!(f.rust_type(), "String");
        assert_eq!(f.sql_type(), "TEXT");
    }

    #[test]
    fn parse_optional_field() {
        let f = parse_field("description:Option<String>").unwrap();
        assert_eq!(f.kind, FieldKind::String);
        assert!(f.nullable);
        assert_eq!(f.rust_type(), "Option<String>");
        assert_eq!(f.sql_nullability(), "NULL");
        assert_eq!(f.schema_type(), "Nullable<Text>");
    }

    #[test]
    fn parse_bytea_via_vec() {
        let f = parse_field("data:Vec<u8>").unwrap();
        assert_eq!(f.kind, FieldKind::Bytea);
        assert_eq!(f.rust_type(), "Vec<u8>");
        assert_eq!(f.sql_type(), "BYTEA");
    }

    #[test]
    fn parse_bytea_alias() {
        let f = parse_field("data:Bytea").unwrap();
        assert_eq!(f.kind, FieldKind::Bytea);
    }

    #[test]
    fn parse_uuid() {
        let f = parse_field("token:Uuid").unwrap();
        assert_eq!(f.rust_type(), "uuid::Uuid");
        assert_eq!(f.sql_type(), "UUID");
    }

    #[test]
    fn parse_datetime() {
        let f = parse_field("created_at:DateTime").unwrap();
        assert_eq!(f.rust_type(), "chrono::DateTime<chrono::Utc>");
        assert_eq!(f.schema_type(), "Timestamptz");
    }

    // ── SQLite backend-aware column mapping (issue #1614) ──────────────

    /// The complete `SQLite` column-type mapping for every scalar `FieldKind`.
    /// AC #4: every kind maps to a working `SQLite` type — none is rejected.
    #[test]
    fn sqlite_sql_type_covers_every_kind() {
        let cases = [
            ("title:String", "TEXT"),
            ("body:Text", "TEXT"),
            ("count:i32", "INTEGER"),
            ("big:i64", "INTEGER"),
            ("flag:bool", "INTEGER"),
            ("ratio:f32", "REAL"),
            ("amount:f64", "REAL"),
            ("token:Uuid", "TEXT"),
            ("naive:NaiveDateTime", "TEXT"),
            ("at:DateTime", "TEXT"),
            ("data:Bytea", "BLOB"),
            ("cover:Attachment", "TEXT"),
            ("post:references", "INTEGER"),
        ];
        for (token, expected) in cases {
            let f = parse_field(token).unwrap();
            assert_eq!(
                f.sql_column_type_for(DatabaseBackend::Sqlite),
                expected,
                "SQLite column type for `{token}`"
            );
            // Postgres path must stay byte-for-byte identical to the legacy output.
            assert_eq!(
                f.sql_column_type_for(DatabaseBackend::Postgres),
                f.sql_column_type(),
                "Postgres column type for `{token}` must be unchanged"
            );
        }
    }

    #[test]
    fn sqlite_enum_and_decimal_map_to_text() {
        let e = parse_field("status:enum{draft,published}").unwrap();
        assert_eq!(e.sql_column_type_for(DatabaseBackend::Sqlite), "TEXT");
        assert_eq!(e.sql_column_type_for(DatabaseBackend::Postgres), "TEXT");

        // Decimal collapses to TEXT on SQLite (no fixed-precision NUMERIC),
        // while Postgres keeps the exact `NUMERIC(precision,scale)` rendering.
        let d = parse_field("price:decimal{10,2}").unwrap();
        assert_eq!(d.sql_column_type_for(DatabaseBackend::Sqlite), "TEXT");
        assert_eq!(
            d.sql_column_type_for(DatabaseBackend::Postgres),
            "NUMERIC(10,2)"
        );
    }

    /// The diesel `schema.rs` types on the `SQLite` path must be types diesel's
    /// `SQLite` backend implements — never the Postgres-only `Timestamptz`,
    /// `Jsonb`, `Uuid`, `Bytea`, or `Numeric`. `DateTime<Utc>` maps to the
    /// SQLite-valid `TimestamptzSqlite` (issue #1924).
    #[test]
    fn sqlite_schema_type_avoids_postgres_only_diesel_types() {
        let cases = [
            // `NaiveDateTime` maps to the core, ungated `Timestamp`; `DateTime`
            // maps to diesel's SQLite `TimestamptzSqlite` (issue #1924).
            ("at:DateTime", "TimestamptzSqlite"),
            ("naive:NaiveDateTime", "Timestamp"),
            ("token:Uuid", "Text"),
            // `Attachment` fields are auto-nullable, so `Field::schema_type_for`
            // wraps the SQLite `Text` inner type in `Nullable<…>`.
            ("cover:Attachment", "Nullable<Text>"),
            ("data:Bytea", "Binary"),
            ("price:decimal{10,2}", "Text"),
            ("big:i64", "Int8"),
            ("flag:bool", "Bool"),
        ];
        for (token, expected) in cases {
            let f = parse_field(token).unwrap();
            assert_eq!(
                f.schema_type_for(DatabaseBackend::Sqlite),
                expected,
                "SQLite schema type for `{token}`"
            );
        }
        // Nullable wrapping is preserved on the SQLite path.
        let opt = parse_field("token:Option<Uuid>").unwrap();
        assert_eq!(
            opt.schema_type_for(DatabaseBackend::Sqlite),
            "Nullable<Text>"
        );
        // No SQLite schema type may be a Postgres-only diesel type.
        // `TimestamptzSqlite` is deliberately NOT forbidden — it is a diesel
        // *SQLite* sql-type (issue #1924), not a Postgres-only one.
        for token in [
            "at:DateTime",
            "token:Uuid",
            "cover:Attachment",
            "data:Bytea",
        ] {
            let ty = parse_field(token)
                .unwrap()
                .schema_type_for(DatabaseBackend::Sqlite);
            assert!(
                !["Timestamptz", "Jsonb", "Uuid", "Bytea", "Numeric"].contains(&ty.as_str()),
                "`{token}` -> `{ty}` must not be a Postgres-only diesel type on SQLite"
            );
        }
    }

    /// After issue #1924, `Uuid`, `Decimal`, and `Enum` are the only kinds still
    /// lacking a working diesel `SQLite` conversion (their Rust types are
    /// foreign to `autumn-web` with Postgres-only impls; `Enum` renders only
    /// `Pg` `ToSql`/`FromSql`). `DateTime<Utc>` (via `TimestamptzSqlite`) and
    /// `Attachment` (via `autumn-web`'s local `Blob` `Text`/`Sqlite` impls) now
    /// round-trip, alongside `NaiveDateTime` (core `Timestamp`).
    #[test]
    fn sqlite_has_diesel_conversion_rejects_only_uuid_decimal_enum() {
        for token in [
            "title:String",
            "body:Text",
            "count:i32",
            "big:i64",
            "flag:bool",
            "ratio:f32",
            "amount:f64",
            "naive:NaiveDateTime",
            "at:DateTime",
            "cover:Attachment",
            "data:Bytea",
            "post:references",
        ] {
            assert!(
                parse_field(token)
                    .unwrap()
                    .kind
                    .sqlite_has_diesel_conversion(),
                "`{token}` must be a SQLite-supported kind"
            );
        }
        for token in [
            "token:Uuid",
            "price:decimal{10,2}",
            "status:enum{draft,published}",
        ] {
            assert!(
                !parse_field(token)
                    .unwrap()
                    .kind
                    .sqlite_has_diesel_conversion(),
                "`{token}` must have no working diesel SQLite conversion"
            );
        }
    }

    #[test]
    fn sqlite_primary_key_mapping() {
        assert_eq!(
            IdType::BigSerial.pk_sql_for(DatabaseBackend::Sqlite),
            "INTEGER PRIMARY KEY AUTOINCREMENT"
        );
        assert_eq!(
            IdType::Uuid.pk_sql_for(DatabaseBackend::Sqlite),
            "TEXT PRIMARY KEY"
        );
        // Postgres primary keys stay byte-for-byte identical.
        assert_eq!(
            IdType::BigSerial.pk_sql_for(DatabaseBackend::Postgres),
            IdType::BigSerial.pk_sql()
        );
        assert_eq!(
            IdType::Uuid.pk_sql_for(DatabaseBackend::Postgres),
            IdType::Uuid.pk_sql()
        );
        // The UUIDv7 migration comment is Postgres-only guidance.
        assert!(
            IdType::Uuid
                .migration_comment_for(DatabaseBackend::Postgres)
                .is_some()
        );
        assert!(
            IdType::Uuid
                .migration_comment_for(DatabaseBackend::Sqlite)
                .is_none()
        );
        assert_eq!(
            IdType::BigSerial.schema_type_for(DatabaseBackend::Sqlite),
            "Int8"
        );
        assert_eq!(
            IdType::Uuid.schema_type_for(DatabaseBackend::Sqlite),
            "Text"
        );
    }

    #[test]
    fn parse_naive_datetime() {
        let f = parse_field("created_at:NaiveDateTime").unwrap();
        assert_eq!(f.rust_type(), "chrono::NaiveDateTime");
        assert_eq!(f.schema_type(), "Timestamp");
    }

    #[test]
    fn parse_all_numeric_types() {
        assert_eq!(parse_field("a:i32").unwrap().sql_type(), "INTEGER");
        assert_eq!(parse_field("b:i64").unwrap().sql_type(), "BIGINT");
        assert_eq!(parse_field("c:f32").unwrap().sql_type(), "REAL");
        assert_eq!(parse_field("d:f64").unwrap().sql_type(), "DOUBLE PRECISION");
        assert_eq!(parse_field("e:bool").unwrap().sql_type(), "BOOLEAN");
    }

    #[test]
    fn unknown_type_rejected() {
        let err = parse_field("price:Money").unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("Money"));
        assert!(msg.contains("Supported:"));
    }

    #[test]
    fn missing_colon_rejected() {
        let err = parse_field("title").unwrap_err();
        assert!(err.to_string().contains("missing colon"));
    }

    #[test]
    fn empty_name_rejected() {
        let err = parse_field(":String").unwrap_err();
        assert!(err.to_string().contains("empty"));
    }

    #[test]
    fn pascal_case_name_rejected() {
        let err = parse_field("Title:String").unwrap_err();
        assert!(err.to_string().contains("snake_case"));
    }

    #[test]
    fn rust_keyword_field_name_rejected() {
        // `pub type: String` would be a Rust syntax error.
        let err = parse_field("type:String").unwrap_err();
        assert!(err.to_string().contains("Rust keyword"));
    }

    #[test]
    fn other_keywords_also_rejected() {
        for kw in ["fn", "match", "struct", "self", "impl", "ref", "move"] {
            let token = format!("{kw}:String");
            assert!(
                parse_field(&token).is_err(),
                "expected '{kw}' to be rejected"
            );
        }
    }

    #[test]
    fn nested_option_is_unsupported() {
        // Option<Option<String>> is intentionally not part of the surface.
        let err = parse_field("x:Option<Option<String>>").unwrap_err();
        assert!(err.to_string().contains("unsupported type"));
    }

    #[test]
    fn vec_of_other_types_rejected() {
        let err = parse_field("xs:Vec<i32>").unwrap_err();
        assert!(err.to_string().contains("unsupported type"));
    }

    #[test]
    fn parse_multiple_fields() {
        let tokens = vec!["title:String".into(), "count:i64".into()];
        let fs = parse_fields(&tokens).unwrap();
        assert_eq!(fs.len(), 2);
        assert_eq!(fs[0].name, "title");
        assert_eq!(fs[1].name, "count");
    }

    #[test]
    fn duplicate_field_names_rejected() {
        // `title:String title:Text` would emit two `title` columns.
        let tokens = vec!["title:String".into(), "title:Text".into()];
        let err = parse_fields(&tokens).unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("duplicate"),
            "expected duplicate error, got: {msg}"
        );
        assert!(msg.contains("title"));
    }

    #[test]
    fn whitespace_around_tokens_tolerated() {
        let f = parse_field(" name : String ").unwrap();
        assert_eq!(f.name, "name");
        assert_eq!(f.kind, FieldKind::String);
    }

    // ── slug cross-field validation (issue #1260) ──────────────────────────

    #[test]
    fn parse_fields_accepts_slug_deriving_from_earlier_string_field() {
        let tokens = vec!["title:String".into(), "slug:slug{from:title}".into()];
        let fs = parse_fields(&tokens).unwrap();
        assert_eq!(fs[1].constraints.from.as_deref(), Some("title"));
    }

    #[test]
    fn parse_fields_accepts_slug_deriving_from_later_string_field() {
        // Declaration order shouldn't matter for the `from` reference.
        let tokens = vec!["slug:slug{from:title}".into(), "title:String".into()];
        assert!(parse_fields(&tokens).is_ok());
    }

    #[test]
    fn parse_fields_rejects_slug_from_unknown_field() {
        let tokens = vec!["slug:slug{from:headline}".into()];
        let err = parse_fields(&tokens).unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("headline"), "unexpected error: {msg}");
    }

    #[test]
    fn parse_fields_rejects_slug_from_non_string_field() {
        let tokens = vec!["count:i32".into(), "slug:slug{from:count}".into()];
        let err = parse_fields(&tokens).unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("count"), "unexpected error: {msg}");
    }

    #[test]
    fn parse_fields_rejects_slug_from_nullable_source_field() {
        // `new.{from}` would be `Option<String>` on the generated `New*`
        // insert struct — `autumn_web::slugify(&new.{from})` doesn't compile
        // against that type, so this must be rejected at generate time
        // rather than emit uncompilable code.
        let tokens = vec![
            "title:Option<String>".into(),
            "slug:slug{from:title}".into(),
        ];
        let err = parse_fields(&tokens).unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("title") && msg.contains("nullable"),
            "unexpected error: {msg}"
        );
    }

    #[test]
    fn parse_fields_accepts_slug_deriving_from_text_field() {
        let tokens = vec!["body:Text".into(), "slug:slug{from:body}".into()];
        assert!(parse_fields(&tokens).is_ok());
    }

    #[test]
    fn parse_fields_accepts_slug_deriving_from_richtext_field() {
        let tokens = vec!["body:richtext".into(), "slug:slug{from:body}".into()];
        assert!(parse_fields(&tokens).is_ok());
    }

    #[test]
    fn parse_fields_rejects_more_than_one_slug_field() {
        // A slug is the model's routing key -- only one makes sense.
        let tokens = vec![
            "title:String".into(),
            "slug:slug{from:title}".into(),
            "slug2:slug{from:title}".into(),
        ];
        let err = parse_fields(&tokens).unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("slug"), "unexpected error: {msg}");
    }

    // ── position cross-field validation (issue #1358) ───────────────────────

    #[test]
    fn parse_fields_accepts_position_scoped_to_earlier_reference_field() {
        let tokens = vec![
            "board:references".into(),
            "rank:position{scope:board_id}".into(),
        ];
        let fs = parse_fields(&tokens).unwrap();
        assert_eq!(fs[1].constraints.scope.as_deref(), Some("board_id"));
    }

    #[test]
    fn parse_fields_accepts_position_scoped_to_later_reference_field() {
        // Declaration order shouldn't matter for the `scope` reference.
        let tokens = vec![
            "rank:position{scope:board_id}".into(),
            "board:references".into(),
        ];
        assert!(parse_fields(&tokens).is_ok());
    }

    #[test]
    fn parse_fields_rejects_position_scope_unknown_field() {
        let tokens = vec!["rank:position{scope:board_id}".into()];
        let err = parse_fields(&tokens).unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("board_id"), "unexpected error: {msg}");
    }

    #[test]
    fn parse_fields_rejects_position_scope_non_reference_field() {
        let tokens = vec![
            "board_id:i64".into(),
            "rank:position{scope:board_id}".into(),
        ];
        let err = parse_fields(&tokens).unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("board_id"), "unexpected error: {msg}");
    }

    #[test]
    fn parse_fields_rejects_position_scope_nullable_reference_field() {
        let tokens = vec![
            "board:references?".into(),
            "rank:position{scope:board_id}".into(),
        ];
        let err = parse_fields(&tokens).unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("board_id"), "unexpected error: {msg}");
        assert!(msg.contains("nullable"), "unexpected error: {msg}");
    }

    #[test]
    fn parse_fields_rejects_more_than_one_position_field() {
        let tokens = vec!["rank:position".into(), "rank2:position".into()];
        let err = parse_fields(&tokens).unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("position"), "unexpected error: {msg}");
    }

    #[test]
    fn parse_fields_accepts_unscoped_position_field() {
        let tokens = vec!["rank:position".into()];
        let fs = parse_fields(&tokens).unwrap();
        assert_eq!(fs[0].constraints.scope, None);
    }

    // ── RED: Attachment field kind ──────────────────────────────────────────

    #[test]
    fn parse_attachment_pascal() {
        let f = parse_field("cover_image:Attachment").unwrap();
        assert_eq!(f.kind, FieldKind::Attachment);
        assert!(f.nullable, "attachment fields must default to nullable");
    }

    #[test]
    fn parse_attachment_lowercase() {
        let f = parse_field("cover_image:attachment").unwrap();
        assert_eq!(f.kind, FieldKind::Attachment);
    }

    // ── richtext (issue #1255) ─────────────────────────────────────────────

    #[test]
    fn parse_richtext_token() {
        let f = parse_field("body:richtext").unwrap();
        assert_eq!(f.kind, FieldKind::RichText);
        assert!(!f.nullable);
        assert_eq!(f.name, "body");
    }

    #[test]
    fn parse_richtext_accepts_documented_spellings() {
        for token in ["body:richtext", "body:RichText", "body:rich_text"] {
            assert_eq!(
                parse_field(token).unwrap().kind,
                FieldKind::RichText,
                "{token} should parse as RichText"
            );
        }
    }

    #[test]
    fn richtext_stores_markdown_source_in_a_text_column() {
        // The column holds the Markdown *source*, not rendered HTML — so it is
        // an ordinary TEXT/String column everywhere in the storage stack.
        let f = parse_field("body:richtext").unwrap();
        assert_eq!(f.rust_type(), "String");
        assert_eq!(f.sql_type(), "TEXT");
        assert_eq!(f.schema_type(), "Text");
        assert_eq!(
            f.sql_column_type_for(DatabaseBackend::Sqlite),
            "TEXT",
            "richtext must work on the SQLite backend too"
        );
        assert_eq!(f.schema_type_for(DatabaseBackend::Sqlite), "Text");
        assert!(FieldKind::RichText.sqlite_has_diesel_conversion());
    }

    #[test]
    fn optional_richtext_parses() {
        let f = parse_field("body:Option<richtext>").unwrap();
        assert_eq!(f.kind, FieldKind::RichText);
        assert!(f.nullable);
        assert_eq!(f.rust_type(), "Option<String>");
        assert_eq!(f.schema_type(), "Nullable<Text>");
    }

    #[test]
    fn richtext_is_rich_text_predicate() {
        assert!(FieldKind::RichText.is_rich_text());
        assert!(!FieldKind::Text.is_rich_text());
        assert!(!FieldKind::String.is_rich_text());
    }

    #[test]
    fn richtext_accepts_length_constraints() {
        let f = parse_field("body:richtext{min=10,max=50000}").unwrap();
        assert_eq!(f.validation_attrs(), vec!["length(min = 10, max = 50000)"]);
    }

    #[test]
    fn richtext_rejects_format_constraints() {
        // `email`/`url` are single-line format validators; a Markdown body can
        // never satisfy them, so accepting them would emit an unwritable field.
        for token in ["body:richtext{email}", "body:richtext{url}"] {
            let err = parse_field(token).unwrap_err().to_string();
            assert!(
                err.contains("email") || err.contains("url"),
                "{token} should be rejected, got: {err}"
            );
        }
    }

    #[test]
    fn richtext_rejects_state_machine_modifier() {
        let err = parse_field("body:richtext:states(draft -> live)")
            .unwrap_err()
            .to_string();
        assert!(err.contains("state machine"), "{err}");
    }

    #[test]
    fn richtext_supports_unique_modifier_like_other_text_columns() {
        let f = parse_field("body:richtext:unique").unwrap();
        assert_eq!(f.kind, FieldKind::RichText);
        assert!(f.unique);
    }

    #[test]
    fn richtext_appears_in_supported_types_constants() {
        assert!(
            SUPPORTED_TYPES.contains("richtext"),
            "SUPPORTED_TYPES must list richtext"
        );
        assert!(
            SQLITE_SUPPORTED_KINDS.contains("richtext"),
            "SQLITE_SUPPORTED_KINDS must list richtext — it is a plain TEXT column"
        );
    }

    #[test]
    fn attachment_rust_type_is_blob() {
        let f = parse_field("cover_image:Attachment").unwrap();
        assert_eq!(f.rust_type(), "Option<autumn_web::storage::Blob>");
    }

    #[test]
    fn attachment_sql_type_is_jsonb() {
        let f = parse_field("cover_image:Attachment").unwrap();
        assert_eq!(f.sql_type(), "JSONB");
    }

    #[test]
    fn attachment_schema_type_is_jsonb() {
        let f = parse_field("cover_image:Attachment").unwrap();
        assert_eq!(f.schema_type(), "Nullable<Jsonb>");
    }

    #[test]
    fn attachment_is_attachment_returns_true() {
        assert!(FieldKind::Attachment.is_attachment());
        assert!(!FieldKind::String.is_attachment());
        assert!(!FieldKind::Uuid.is_attachment());
    }

    #[test]
    fn optional_attachment_parses() {
        let f = parse_field("avatar:Option<Attachment>").unwrap();
        assert_eq!(f.kind, FieldKind::Attachment);
        assert!(f.nullable);
        assert_eq!(f.rust_type(), "Option<autumn_web::storage::Blob>");
    }

    #[test]
    fn attachment_in_list_of_fields() {
        let tokens = vec![
            "title:String".into(),
            "cover_image:Attachment".into(),
            "count:i64".into(),
        ];
        let fields = parse_fields(&tokens).unwrap();
        assert_eq!(fields.len(), 3);
        assert_eq!(fields[1].kind, FieldKind::Attachment);
    }

    #[test]
    fn attachment_appears_in_supported_types_constant() {
        assert!(
            SUPPORTED_TYPES.contains("Attachment"),
            "SUPPORTED_TYPES must list Attachment"
        );
    }

    // ── RED: json/jsonb field kind (issue #1341) ────────────────────────────

    #[test]
    fn parse_json_lowercase() {
        let f = parse_field("config:json").unwrap();
        assert_eq!(f.kind, FieldKind::Json);
        assert_eq!(f.name, "config");
        assert!(
            !f.nullable,
            "unlike Attachment, a json field is not implicitly nullable"
        );
    }

    #[test]
    fn parse_json_pascal_case() {
        assert_eq!(parse_field("config:Json").unwrap().kind, FieldKind::Json);
    }

    #[test]
    fn parse_jsonb_alias_lowercase() {
        assert_eq!(parse_field("config:jsonb").unwrap().kind, FieldKind::Json);
    }

    #[test]
    fn parse_jsonb_alias_pascal_case() {
        assert_eq!(parse_field("config:Jsonb").unwrap().kind, FieldKind::Json);
    }

    #[test]
    fn json_rust_type_is_serde_json_value() {
        let f = parse_field("config:json").unwrap();
        assert_eq!(f.rust_type(), "serde_json::Value");
    }

    #[test]
    fn json_sql_type_is_jsonb() {
        let f = parse_field("config:json").unwrap();
        assert_eq!(f.sql_type(), "JSONB");
    }

    #[test]
    fn json_schema_type_is_jsonb() {
        let f = parse_field("config:json").unwrap();
        assert_eq!(f.schema_type(), "Jsonb");
    }

    #[test]
    fn json_sqlite_sql_type_is_text() {
        // AC1: "SQLite dev/test backend: TEXT storing JSON".
        let f = parse_field("config:json").unwrap();
        assert_eq!(f.sql_column_type_for(DatabaseBackend::Sqlite), "TEXT");
    }

    #[test]
    fn json_sqlite_schema_type_is_diesels_json_not_text_or_jsonb() {
        // Diesel ships `FromSql`/`ToSql<Json, Sqlite> for serde_json::Value`
        // (diesel 2.3+, `serde_json` + `sqlite` features — both already
        // unconditionally on in this workspace). Using diesel's *bare* `Text`
        // sql-type here would not compile (no `serde_json::Value` conversion
        // for it), and diesel's `Jsonb` on SQLite uses a proprietary binary
        // encoding, not the plain-text JSON the AC calls for — so this must be
        // `Json`, distinct from both.
        let f = parse_field("config:json").unwrap();
        assert_eq!(f.schema_type_for(DatabaseBackend::Sqlite), "Json");
    }

    #[test]
    fn json_sqlite_has_diesel_conversion_is_true() {
        // Unlike `Decimal`/`Uuid`/`Enum`, `serde_json::Value`'s SQLite
        // conversion is implemented by diesel itself, so no orphan-rule
        // wrapper is needed and the kind is fully SQLite-supported.
        assert!(FieldKind::Json.sqlite_has_diesel_conversion());
    }

    #[test]
    fn optional_json_parses() {
        let f = parse_field("config:Option<json>").unwrap();
        assert_eq!(f.kind, FieldKind::Json);
        assert!(f.nullable);
        assert_eq!(f.rust_type(), "Option<serde_json::Value>");
        assert_eq!(f.schema_type(), "Nullable<Jsonb>");
        assert_eq!(f.schema_type_for(DatabaseBackend::Sqlite), "Nullable<Json>");
    }

    #[test]
    fn optional_jsonb_alias_parses() {
        let f = parse_field("config:Option<jsonb>").unwrap();
        assert_eq!(f.kind, FieldKind::Json);
        assert!(f.nullable);
    }

    #[test]
    fn unique_json_field_parses() {
        // Postgres's `jsonb` type has a default B-tree operator class, so a
        // `UNIQUE INDEX` over a json column is legitimate — not rejected the
        // way a randomized-encrypted column's equality lookup is.
        let f = parse_field("config:json:unique").unwrap();
        assert_eq!(f.kind, FieldKind::Json);
        assert!(f.unique);
    }

    #[test]
    fn json_in_list_of_fields() {
        let tokens = vec![
            "title:String".into(),
            "config:json".into(),
            "count:i64".into(),
        ];
        let fields = parse_fields(&tokens).unwrap();
        assert_eq!(fields.len(), 3);
        assert_eq!(fields[1].kind, FieldKind::Json);
    }

    #[test]
    fn json_appears_in_supported_types_constant() {
        assert!(
            SUPPORTED_TYPES.contains("json"),
            "SUPPORTED_TYPES must list json/jsonb"
        );
    }

    #[test]
    fn json_appears_in_sqlite_supported_kinds_constant() {
        // Unlike Decimal/Uuid/Enum, json IS SQLite-supported.
        assert!(
            SQLITE_SUPPORTED_KINDS.contains("json"),
            "SQLITE_SUPPORTED_KINDS must list json — diesel supports it natively"
        );
    }

    #[test]
    fn json_dsl_token_is_json() {
        assert_eq!(FieldKind::Json.dsl_token(), "json");
    }

    #[test]
    fn json_stays_unmapped_for_db_pull_inverse() {
        // Same ambiguity as Attachment (both forward-map to `jsonb`): the
        // inverse can't tell a `Blob` column from a raw `Json` column, so
        // `db pull` introspection leaves `jsonb`/`json` unsupported rather
        // than guessing (see `sql_type_inverse_rejects_unknown_types`, which
        // already locks this and is unchanged by this feature).
        assert!(sql_type_to_field_kind("jsonb").is_none());
        assert!(sql_type_to_field_kind("json").is_none());
    }

    // ── Inverse mapping (db pull introspection, issue #975) ─────────────────

    #[test]
    fn sql_type_inverse_maps_all_supported_udt_names() {
        // (udt_name, expected kind, expected rust_type, expected schema_type)
        let cases: &[(&str, FieldKind, &str, &str)] = &[
            ("text", FieldKind::String, "String", "Text"),
            ("varchar", FieldKind::String, "String", "Text"),
            ("bpchar", FieldKind::String, "String", "Text"),
            ("int4", FieldKind::I32, "i32", "Int4"),
            ("int8", FieldKind::I64, "i64", "Int8"),
            ("bool", FieldKind::Bool, "bool", "Bool"),
            ("float4", FieldKind::F32, "f32", "Float4"),
            ("float8", FieldKind::F64, "f64", "Float8"),
            ("uuid", FieldKind::Uuid, "uuid::Uuid", "Uuid"),
            (
                "timestamp",
                FieldKind::NaiveDateTime,
                "chrono::NaiveDateTime",
                "Timestamp",
            ),
            (
                "timestamptz",
                FieldKind::DateTime,
                "chrono::DateTime<chrono::Utc>",
                "Timestamptz",
            ),
            ("bytea", FieldKind::Bytea, "Vec<u8>", "Bytea"),
        ];
        for (udt, kind, rust, schema) in cases {
            let mapped = sql_type_to_field_kind(udt)
                .unwrap_or_else(|| panic!("'{udt}' must map to a FieldKind"));
            assert_eq!(mapped, *kind, "kind mismatch for {udt}");
            assert_eq!(mapped.rust_type(), *rust, "rust_type mismatch for {udt}");
            assert_eq!(
                mapped.schema_type(),
                *schema,
                "schema_type mismatch for {udt}"
            );
        }
    }

    #[test]
    fn sql_type_inverse_preserves_i64_for_int8() {
        // i64 PKs must round-trip as i64 (AC3).
        assert_eq!(sql_type_to_field_kind("int8"), Some(FieldKind::I64));
        assert_eq!(sql_type_to_field_kind("int8").unwrap().rust_type(), "i64");
    }

    #[test]
    fn sql_type_inverse_rejects_unknown_types() {
        // Unmapped SQL types must be reported, never silently dropped (AC2).
        // `jsonb` is deliberately unsupported: the inverse of `Attachment ->
        // JSONB` is ambiguous (arbitrary JSON vs an Autumn `Blob`), so pulling
        // it must not silently produce a `Blob` field.
        for udt in ["numeric", "jsonb", "json", "inet", "money", "point"] {
            assert!(
                sql_type_to_field_kind(udt).is_none(),
                "'{udt}' is outside the documented surface and must not map"
            );
        }
    }

    #[test]
    fn bare_underscore_is_not_a_valid_ident() {
        // `pub _: T` is the reserved wildcard and does not compile.
        assert!(!is_valid_ident("_"));
        assert!(parse_field("_:String").is_err());
        // But `_`-prefixed names remain valid.
        assert!(is_valid_ident("_internal"));
    }

    #[test]
    fn sql_type_inverse_round_trips_forward_sql_types() {
        // Every non-Attachment FieldKind's forward sql_type() (lowercased,
        // base name) must invert back to an equivalent kind, guaranteeing the
        // db-pull inverse stays in lockstep with the generate forward map.
        for (kind, udt) in [
            (FieldKind::String, "text"),
            (FieldKind::I32, "int4"),
            (FieldKind::I64, "int8"),
            (FieldKind::Bool, "bool"),
            (FieldKind::F32, "float4"),
            (FieldKind::F64, "float8"),
            (FieldKind::Uuid, "uuid"),
            (FieldKind::NaiveDateTime, "timestamp"),
            (FieldKind::DateTime, "timestamptz"),
            (FieldKind::Bytea, "bytea"),
        ] {
            let back = sql_type_to_field_kind(udt).unwrap();
            assert_eq!(back.rust_type(), kind.rust_type());
            assert_eq!(back.schema_type(), kind.schema_type());
        }
    }

    // ── IdType (primary-key type, issue #1400) ─────────────────────────────

    #[test]
    fn id_type_default_is_bigserial() {
        assert_eq!(IdType::default(), IdType::BigSerial);
    }

    #[test]
    fn id_type_bigserial_mappings() {
        let id = IdType::BigSerial;
        assert_eq!(id.rust_type(), "i64");
        assert_eq!(id.schema_type(), "Int8");
        assert_eq!(id.pk_sql(), "BIGSERIAL PRIMARY KEY");
        assert!(id.migration_comment().is_none());
    }

    #[test]
    fn id_type_uuid_mappings() {
        let id = IdType::Uuid;
        assert_eq!(id.rust_type(), "uuid::Uuid");
        assert_eq!(id.schema_type(), "Uuid");
        assert_eq!(id.pk_sql(), "UUID PRIMARY KEY DEFAULT gen_random_uuid()");
        let comment = id
            .migration_comment()
            .expect("uuid should have a trade-off comment");
        assert!(
            comment.contains("UUIDv7"),
            "comment should mention UUIDv7: {comment}"
        );
    }

    #[test]
    fn id_type_parse_accepts_uuid_case_insensitive() {
        for token in ["uuid", "Uuid", "UUID"] {
            assert_eq!(
                IdType::parse(token).unwrap(),
                IdType::Uuid,
                "'{token}' should parse to Uuid"
            );
        }
    }

    #[test]
    fn id_type_parse_accepts_bigint_aliases() {
        for token in ["bigint", "bigserial", "i64", "BigInt"] {
            assert_eq!(
                IdType::parse(token).unwrap(),
                IdType::BigSerial,
                "'{token}' should parse to BigSerial"
            );
        }
    }

    // ── references field kind (issue #1026) ────────────────────────────────

    #[test]
    fn parse_references_resolves_column_name_to_id() {
        let f = parse_field("post:references").unwrap();
        assert_eq!(f.name, "post_id");
        assert_eq!(f.kind, FieldKind::References);
        assert!(!f.nullable);
    }

    #[test]
    fn parse_references_pascal_case_also_accepted() {
        let f = parse_field("post:References").unwrap();
        assert_eq!(f.name, "post_id");
        assert_eq!(f.kind, FieldKind::References);
    }

    #[test]
    fn parse_references_does_not_double_suffix_already_named_column() {
        let f = parse_field("post_id:references").unwrap();
        assert_eq!(f.name, "post_id");
    }

    #[test]
    fn parse_references_multi_word_base_name() {
        let f = parse_field("blog_post:references").unwrap();
        assert_eq!(f.name, "blog_post_id");
        assert_eq!(f.reference_table().as_deref(), Some("blog_posts"));
    }

    #[test]
    fn references_rust_type_is_i64() {
        let f = parse_field("post:references").unwrap();
        assert_eq!(f.rust_type(), "i64");
    }

    #[test]
    fn references_sql_type_is_bigint() {
        let f = parse_field("post:references").unwrap();
        assert_eq!(f.sql_type(), "BIGINT");
        assert_eq!(f.sql_nullability(), "NOT NULL");
    }

    #[test]
    fn references_schema_type_is_int8() {
        let f = parse_field("post:references").unwrap();
        assert_eq!(f.schema_type(), "Int8");
    }

    #[test]
    fn references_target_table_derived_via_pluralize() {
        let f = parse_field("post:references").unwrap();
        assert_eq!(f.reference_table().as_deref(), Some("posts"));
    }

    #[test]
    fn references_nullable_form_with_question_mark() {
        let f = parse_field("post:references?").unwrap();
        assert_eq!(f.name, "post_id");
        assert!(f.nullable);
        assert_eq!(f.rust_type(), "Option<i64>");
        assert_eq!(f.schema_type(), "Nullable<Int8>");
        assert_eq!(f.sql_nullability(), "NULL");
    }

    #[test]
    fn references_nullable_form_tolerates_internal_whitespace() {
        // `parse_field` trims the raw type text before `parse_type` sees it,
        // but a CLI token could still carry whitespace before the `?` (e.g.
        // `"post: references ?"` after the name/type split trims only the
        // outer edges); this must still resolve to a nullable reference
        // rather than "unsupported type".
        let f = parse_field("post: references ?").unwrap();
        assert_eq!(f.kind, FieldKind::References);
        assert!(f.nullable);
    }

    #[test]
    fn question_mark_suffix_is_not_a_general_nullability_marker() {
        // `?` is only recognized for `references` — every other type must use
        // `Option<…>`. Otherwise this silently expands the DSL's grammar
        // (undocumented and untested) for every field kind.
        for token in [
            "count:i64?",
            "flag:bool?",
            "title:String?",
            "data:Vec<u8>?",
            "id:Uuid?",
        ] {
            let err = parse_field(token).unwrap_err();
            assert!(
                err.to_string().contains("unsupported type"),
                "expected '{token}' to be rejected like before: {err}"
            );
        }
    }

    #[test]
    fn references_is_reference_predicate() {
        assert!(FieldKind::References.is_reference());
        assert!(!FieldKind::I64.is_reference());
    }

    #[test]
    fn non_reference_field_has_no_reference_table() {
        let f = parse_field("title:String").unwrap();
        assert_eq!(f.reference_table(), None);
    }

    #[test]
    fn references_appears_in_supported_types_constant() {
        assert!(
            SUPPORTED_TYPES.contains("references"),
            "SUPPORTED_TYPES must list references"
        );
    }

    // ── constraint modifiers (issue #1388 validation + #1146 label) ─────────

    #[test]
    fn parse_string_length_constraint() {
        let f = parse_field("title:String{min=3,max=120}").unwrap();
        assert_eq!(f.name, "title");
        assert_eq!(f.kind, FieldKind::String);
        assert_eq!(f.constraints.min.as_deref(), Some("3"));
        assert_eq!(f.constraints.max.as_deref(), Some("120"));
        assert_eq!(f.validation_attrs(), vec!["length(min = 3, max = 120)"]);
    }

    #[test]
    fn parse_string_email_constraint() {
        let f = parse_field("contact:String{email}").unwrap();
        assert!(f.constraints.email);
        assert_eq!(f.validation_attrs(), vec!["email"]);
    }

    #[test]
    fn parse_string_url_constraint() {
        let f = parse_field("homepage:String{url}").unwrap();
        assert!(f.constraints.url);
        assert_eq!(f.validation_attrs(), vec!["url"]);
    }

    #[test]
    fn email_and_url_together_are_rejected() {
        // Both format validators on one field make it unwritable (a valid email
        // fails `url` and vice versa), so the pair is rejected at parse time
        // rather than silently picking a winner.
        let err = parse_field("contact:String{email,url}").unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("mutually exclusive"),
            "must explain the conflict: {msg}"
        );
        assert!(msg.contains("contact"), "must name the field: {msg}");
    }

    #[test]
    fn parse_numeric_range_constraint() {
        let f = parse_field("age:i32{min=0,max=130}").unwrap();
        assert_eq!(f.kind, FieldKind::I32);
        assert_eq!(f.validation_attrs(), vec!["range(min = 0, max = 130)"]);
    }

    #[test]
    fn i32_range_bound_within_range_is_accepted() {
        let f = parse_field("count:i32{max=1000000}").unwrap();
        assert_eq!(f.kind, FieldKind::I32);
        assert_eq!(f.constraints.max.as_deref(), Some("1000000"));
        assert_eq!(f.validation_attrs(), vec!["range(max = 1000000)"]);
    }

    #[test]
    fn i32_range_bound_exceeding_i32_max_is_rejected() {
        // `3000000000` > i32::MAX (~2.147e9): parsed at the field's concrete
        // width so it fails generation with an actionable error rather than
        // emitting `#[validate(range(max = 3000000000))]` on an `i32` field,
        // which would fail to COMPILE the generated app.
        let err = parse_field("count:i32{max=3000000000}").unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("3000000000"), "must name the bad bound: {msg}");
        assert!(msg.contains("i32"), "must name the field type: {msg}");
        assert!(
            msg.contains("2147483647"),
            "must state the valid range: {msg}"
        );
    }

    #[test]
    fn i64_accepts_a_bound_that_would_overflow_i32() {
        // The same literal is fine on an i64 field.
        let f = parse_field("count:i64{max=3000000000}").unwrap();
        assert_eq!(f.kind, FieldKind::I64);
        assert_eq!(f.validation_attrs(), vec!["range(max = 3000000000)"]);
    }

    #[test]
    fn f32_range_bound_overflowing_the_type_is_rejected() {
        // Rust parses an out-of-f32-range literal to +∞ rather than erroring,
        // so reject non-finite bounds explicitly.
        let err = parse_field("ratio:f32{max=1e40}").unwrap_err();
        assert!(err.to_string().contains("f32"), "{}", err);
    }

    #[test]
    fn i64_min_greater_than_max_beyond_f64_precision_is_rejected() {
        // `min` is exactly one greater than `max`, but both round to the same
        // `f64` (they exceed 2^53). Comparing at the concrete `i64` width must
        // still catch the inversion, rather than emitting an impossible
        // `range(min > max)` that rejects every submitted value.
        let err = parse_field("count:i64{min=9007199254740993,max=9007199254740992}").unwrap_err();
        assert!(
            err.to_string().contains("cannot be greater than"),
            "must report the inverted range: {err}"
        );
    }

    #[test]
    fn i64_valid_large_range_is_accepted() {
        let f = parse_field("count:i64{min=1,max=9007199254740992}").unwrap();
        assert_eq!(f.kind, FieldKind::I64);
        assert_eq!(
            f.validation_attrs(),
            vec!["range(min = 1, max = 9007199254740992)"]
        );
    }

    #[test]
    fn float_leading_dot_bound_is_canonicalized_to_a_valid_literal() {
        // `.5` parses as a number but is not a valid Rust float literal; the
        // stored/emitted bound must be `0.5`, and a whole-number float bound
        // must carry a decimal point (`1` -> `1.0`).
        let f = parse_field("ratio:f64{min=.5,max=1}").unwrap();
        assert_eq!(f.constraints.min.as_deref(), Some("0.5"));
        assert_eq!(f.constraints.max.as_deref(), Some("1.0"));
        assert_eq!(f.validation_attrs(), vec!["range(min = 0.5, max = 1.0)"]);
    }

    #[test]
    fn integer_signed_and_zero_padded_bounds_are_canonicalized() {
        // `+1` and `007` parse but aren't the literal we want to splice; the
        // stored/emitted bound must be the canonical decimal (`1`, `7`).
        let f = parse_field("count:i32{min=+1,max=007}").unwrap();
        assert_eq!(f.constraints.min.as_deref(), Some("1"));
        assert_eq!(f.constraints.max.as_deref(), Some("7"));
        assert_eq!(f.validation_attrs(), vec!["range(min = 1, max = 7)"]);
    }

    #[test]
    fn integer_bounds_within_range_are_emitted_verbatim() {
        // Regression: ordinary in-range integer bounds are unchanged.
        assert_eq!(
            parse_field("age:i32{min=0,max=130}")
                .unwrap()
                .validation_attrs(),
            vec!["range(min = 0, max = 130)"]
        );
    }

    #[test]
    fn parse_float_range_constraint_emits_decimal_literals() {
        // An integer bound on an f64 field must be emitted with a decimal
        // point, or the generated `#[validate(range(...))]` comparison fails
        // to type-check against the f64 field.
        let f = parse_field("ratio:f64{min=0,max=1}").unwrap();
        assert_eq!(f.validation_attrs(), vec!["range(min = 0.0, max = 1.0)"]);
    }

    #[test]
    fn parse_min_only_constraint() {
        let f = parse_field("title:String{min=3}").unwrap();
        assert_eq!(f.constraints.min.as_deref(), Some("3"));
        assert_eq!(f.constraints.max, None);
        assert_eq!(f.validation_attrs(), vec!["length(min = 3)"]);
    }

    #[test]
    fn parse_reference_label_constraint() {
        let f = parse_field("post:references{label:title}").unwrap();
        assert_eq!(f.name, "post_id");
        assert_eq!(f.kind, FieldKind::References);
        assert_eq!(f.constraints.label.as_deref(), Some("title"));
        // `references` never fans out to a `#[validate]` rule.
        assert!(f.validation_attrs().is_empty());
    }

    #[test]
    fn reference_label_does_not_break_unique_detection() {
        // The colon inside `{label:title}` must not be mistaken for a
        // `:unique` modifier split.
        let f = parse_field("post:references{label:title}").unwrap();
        assert!(!f.unique);
    }

    #[test]
    fn constraint_composes_with_unique_modifier() {
        let f = parse_field("email:String{email}:unique").unwrap();
        assert!(f.unique);
        assert!(f.constraints.email);
    }

    // ── slug (issue #1260) ───────────────────────────────────────────────

    #[test]
    fn parse_slug_field_with_from() {
        let f = parse_field("slug:slug{from:title}").unwrap();
        assert_eq!(f.name, "slug");
        assert_eq!(f.kind, FieldKind::Slug);
        assert_eq!(f.constraints.from.as_deref(), Some("title"));
        assert_eq!(f.rust_type(), "String");
        assert_eq!(f.sql_type(), "TEXT");
    }

    #[test]
    fn slug_field_is_implicitly_unique() {
        // A slug is the record's routing key, so it must be unique even
        // without an explicit `:unique` modifier — this is what lets it
        // fall into the existing `unique`-field migration/repository
        // machinery (issue #1032) for free.
        let f = parse_field("slug:slug{from:title}").unwrap();
        assert!(f.unique);
    }

    #[test]
    fn slug_field_explicit_unique_modifier_is_harmless() {
        let f = parse_field("slug:slug{from:title}:unique").unwrap();
        assert!(f.unique);
        assert_eq!(f.constraints.from.as_deref(), Some("title"));
    }

    #[test]
    fn slug_field_is_never_nullable() {
        // A slug is always the routing key; a nullable slug would mean some
        // records have no URL. `NOT NULL` is unconditional (AC3), so reject
        // rather than silently drop the modifier.
        let err = parse_field("slug:Option<slug>{from:title}").unwrap_err();
        assert!(
            err.to_string().contains("slug") && err.to_string().contains("nullable"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn slug_field_requires_from_modifier() {
        let err = parse_field("slug:slug").unwrap_err();
        assert!(err.to_string().contains("from"), "unexpected error: {err}");
    }

    #[test]
    fn slug_field_rejects_empty_constraint_block() {
        let err = parse_field("slug:slug{}").unwrap_err();
        assert!(err.to_string().contains("empty"), "unexpected error: {err}");
    }

    #[test]
    fn from_constraint_only_applies_to_slug_fields() {
        let err = parse_field("title:String{from:body}").unwrap_err();
        assert!(err.to_string().contains("from"), "unexpected error: {err}");
    }

    #[test]
    fn slug_field_rejects_non_ident_from_value() {
        let err = parse_field("slug:slug{from:not a field}").unwrap_err();
        assert!(err.to_string().contains("from"), "unexpected error: {err}");
    }

    #[test]
    fn slug_field_rejects_min_max_constraints() {
        // Sanity: `slug` doesn't accidentally pick up unrelated constraints.
        let err = parse_field("slug:slug{from:title,min=3}").unwrap_err();
        assert!(err.to_string().contains("min"), "unexpected error: {err}");
    }

    #[test]
    fn bare_unique_modifier_still_parses() {
        let f = parse_field("email:String:unique").unwrap();
        assert!(f.unique);
        assert!(f.constraints.is_empty());
    }

    // ── `position` ordering field (issue #1358) ─────────────────────────────

    #[test]
    fn parse_position_field() {
        let f = parse_field("rank:position").unwrap();
        assert_eq!(f.name, "rank");
        assert_eq!(f.kind, FieldKind::Position);
        assert!(!f.nullable);
        assert!(!f.unique);
        assert_eq!(f.constraints.scope, None);
        assert_eq!(f.rust_type(), "i64");
        assert_eq!(f.sql_type(), "BIGINT");
        assert_eq!(f.kind.sqlite_sql_type(), "INTEGER");
    }

    #[test]
    fn parse_position_field_with_scope() {
        let f = parse_field("rank:position{scope:board_id}").unwrap();
        assert_eq!(f.kind, FieldKind::Position);
        assert_eq!(f.constraints.scope.as_deref(), Some("board_id"));
    }

    #[test]
    fn position_field_is_never_nullable() {
        let err = parse_field("rank:Option<position>").unwrap_err();
        assert!(
            err.to_string().contains("position") && err.to_string().contains("nullable"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn position_field_rejects_unique_modifier() {
        let err = parse_field("rank:position:unique").unwrap_err();
        assert!(
            err.to_string().contains("unique"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn scope_constraint_only_applies_to_position_fields() {
        let err = parse_field("title:String{scope:board_id}").unwrap_err();
        assert!(err.to_string().contains("scope"), "unexpected error: {err}");
    }

    #[test]
    fn position_field_rejects_non_ident_scope_value() {
        let err = parse_field("rank:position{scope:not a field}").unwrap_err();
        assert!(err.to_string().contains("scope"), "unexpected error: {err}");
    }

    #[test]
    fn position_field_rejects_empty_constraint_block() {
        let err = parse_field("rank:position{}").unwrap_err();
        assert!(err.to_string().contains("empty"), "unexpected error: {err}");
    }

    #[test]
    fn position_field_rejects_min_max_constraints() {
        let err = parse_field("rank:position{min=0}").unwrap_err();
        assert!(err.to_string().contains("min"), "unexpected error: {err}");
    }

    #[test]
    fn position_field_accepts_pascal_case_token() {
        let f = parse_field("rank:Position").unwrap();
        assert_eq!(f.kind, FieldKind::Position);
    }

    #[test]
    fn supported_types_documents_position() {
        assert!(
            SUPPORTED_TYPES.contains("position"),
            "SUPPORTED_TYPES must list position"
        );
        assert!(
            SQLITE_SUPPORTED_KINDS.contains("position"),
            "SQLITE_SUPPORTED_KINDS must list position — it is a plain INTEGER column"
        );
    }

    // ── `{encrypted}` at-rest column encryption (issue #1340) ───────────────

    #[test]
    fn parse_encrypted_modifier_defaults_to_randomized() {
        // AC1: a bare `{encrypted}` marks the column at-rest encrypted in the
        // safe default mode — randomized AEAD, matching bare `#[encrypted]`.
        let f = parse_field("token:String{encrypted}").unwrap();
        assert_eq!(f.name, "token");
        assert_eq!(f.kind, FieldKind::String);
        assert_eq!(f.encrypted_mode(), Some(EncryptedMode::Randomized));
        assert!(f.is_encrypted());
        assert!(f.is_randomized_encrypted());
    }

    #[test]
    fn parse_encrypted_deterministic_modifier() {
        // AC2: the deterministic variant is expressible and maps to the
        // `#[encrypted(deterministic)]` attribute, which keeps `find_by`/
        // `exists_by` equality lookups working.
        let f = parse_field("email:String{encrypted:deterministic}").unwrap();
        assert_eq!(f.encrypted_mode(), Some(EncryptedMode::Deterministic));
        assert!(f.is_encrypted());
        assert!(!f.is_randomized_encrypted());
    }

    #[test]
    fn parse_encrypted_randomized_modifier_is_explicit_default() {
        // The `#[encrypted]` attribute macro accepts an explicit `randomized`
        // option; the DSL mirrors it so a reader can spell out the choice.
        let f = parse_field("token:String{encrypted:randomized}").unwrap();
        assert_eq!(f.encrypted_mode(), Some(EncryptedMode::Randomized));
    }

    #[test]
    fn encrypted_modifier_accepts_the_text_alias() {
        let f = parse_field("notes:Text{encrypted}").unwrap();
        assert_eq!(f.kind, FieldKind::Text);
        assert!(f.is_encrypted());
    }

    #[test]
    fn encrypted_modifier_composes_with_validation_constraints() {
        // Validation runs on the in-memory PLAINTEXT (the model field is a
        // plain `String`), so length/format rules compose normally.
        let f = parse_field("email:String{encrypted:deterministic,max=254,email}").unwrap();
        assert_eq!(f.encrypted_mode(), Some(EncryptedMode::Deterministic));
        assert_eq!(
            f.validation_attrs(),
            vec!["length(max = 254)".to_owned(), "email".to_owned()]
        );
    }

    #[test]
    fn unencrypted_field_reports_no_encryption() {
        let f = parse_field("title:String").unwrap();
        assert_eq!(f.encrypted_mode(), None);
        assert!(!f.is_encrypted());
        assert!(!f.is_randomized_encrypted());
    }

    #[test]
    fn encrypted_only_block_is_not_an_empty_constraint_set() {
        // `is_empty()` means "no constraint modifier was declared"; a field
        // carrying only `{encrypted}` has one.
        let f = parse_field("token:String{encrypted}").unwrap();
        assert!(!f.constraints.is_empty());
        // …but it contributes no `#[validate(...)]` rule.
        assert!(f.validation_attrs().is_empty());
    }

    #[test]
    fn encrypted_modifier_on_nullable_field_is_rejected() {
        // R1 / macro limit: `#[encrypted]` is only supported on non-null
        // `String` columns in v1, so reject rather than emit a model the
        // `#[model]` macro refuses to compile.
        let err = parse_field("token:Option<String>{encrypted}").unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("encrypted"), "must name the modifier: {msg}");
        assert!(msg.contains("nullable"), "must explain the limit: {msg}");
    }

    #[test]
    fn encrypted_modifier_on_non_string_kinds_is_rejected() {
        // R2: every non-`String`/`Text` kind is rejected at parse time with a
        // message naming the v1 limitation — including `Bytea` (and its
        // `Vec<u8>` spelling), which the issue's AC text mentions but the
        // runtime does not support.
        //
        // The assertion is on the REASON and the kind it names, never on the
        // echoed token: `GenerateError::InvalidField` renders as
        // `invalid field '{token}': {reason}`, so a token containing the word
        // "encrypted" would satisfy `contains("encrypted")` for ANY failure —
        // including an unrelated parse error that never reached this check.
        for (token, kind_name) in [
            ("payload:Bytea{encrypted}", "Bytea"),
            ("payload:Vec<u8>{encrypted}", "Bytea"),
            ("count:i64{encrypted}", "i64"),
            ("ratio:f64{encrypted}", "f64"),
            ("flag:bool{encrypted}", "bool"),
            ("at:DateTime{encrypted}", "DateTime"),
            ("body:richtext{encrypted}", "richtext"),
            ("post:references{encrypted}", "references"),
            ("handle:slug{from:title,encrypted}", "slug{…}"),
        ] {
            let err = parse_field(token).unwrap_err();
            let msg = err.to_string();
            assert!(
                msg.contains("the `encrypted` modifier only applies to `String`/`Text` fields"),
                "`{token}` must be rejected by the kind guard: {msg}"
            );
            // `richtext`, `slug{…}` and `enum{…}` all render as `String` in
            // Rust, so the message must name the DECLARED kind or it reads
            // "only applies to String … got a String field".
            assert!(
                msg.contains(&format!("Got a `{kind_name}` field")),
                "`{token}` must name the declared kind `{kind_name}`: {msg}"
            );
        }
    }

    #[test]
    fn encrypted_modifier_on_nullable_text_alias_is_rejected() {
        // The `Option<Text>` spelling hits the same non-null rule as
        // `Option<String>`.
        let err = parse_field("notes:Option<Text>{encrypted}").unwrap_err();
        assert!(
            err.to_string().contains("cannot be nullable"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn encrypted_modifier_tolerates_whitespace_around_the_mode() {
        let f = parse_field("email:String{ encrypted : deterministic }").unwrap();
        assert_eq!(f.encrypted_mode(), Some(EncryptedMode::Deterministic));
    }

    #[test]
    fn repeated_encrypted_modifier_is_rejected() {
        // Last-wins would SILENTLY change what the column leaks: the first form
        // below would upgrade a randomized declaration to deterministic, the
        // second would take away the equality lookups the author asked for.
        for token in [
            "token:String{encrypted,encrypted:deterministic}",
            "token:String{encrypted:deterministic,encrypted}",
            "token:String{encrypted,encrypted}",
        ] {
            let err = parse_field(token).unwrap_err();
            let msg = err.to_string();
            assert!(
                msg.contains("declared more than once"),
                "`{token}` must be refused as ambiguous: {msg}"
            );
        }
    }

    #[test]
    fn encrypted_mode_error_prefers_the_kind_diagnosis() {
        // On `count:i64{encrypted:bogus}` the useful diagnosis is that
        // `encrypted` does not apply to an `i64` at all — not that `bogus`
        // is not a mode.
        let err = parse_field("count:i64{encrypted:bogus}").unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("only applies to `String`/`Text` fields"),
            "unexpected error: {msg}"
        );
    }

    #[test]
    fn encrypted_modifier_on_enum_field_is_rejected() {
        // `enum{…}`'s braces are part of the TYPE, so `enum{a,b}{encrypted}`
        // never even reaches the constraint parser — the variant list swallows
        // the second block and fails as a bad variant name. That is a fine
        // outcome (the token IS malformed), but it proves nothing about
        // encryption, so assert the case that does: an `enum` field reaching
        // the kind guard through the `Option<…>`-free scalar path.
        let err = parse_field("status:enum{draft,published}{encrypted}").unwrap_err();
        assert!(
            err.to_string()
                .contains("not a valid snake_case identifier")
                || err
                    .to_string()
                    .contains("only applies to `String`/`Text` fields"),
            "unexpected error: {err}"
        );
        // The kind guard itself refuses `Enum`, named as the DSL token.
        assert!(
            reject_encrypted_on_kind(FieldKind::Enum)
                .unwrap_err()
                .contains("Got a `enum{…}` field")
        );
    }

    #[test]
    fn encrypted_modifier_rejects_unknown_mode() {
        // R16: a typo must not silently degrade to the randomized default.
        let err = parse_field("token:String{encrypted:determinstic}").unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("determinstic"), "must name the token: {msg}");
        assert!(
            msg.contains("deterministic") && msg.contains("randomized"),
            "must list the accepted modes: {msg}"
        );
    }

    #[test]
    fn encrypted_modifier_rejects_attribute_only_options() {
        // `admin_visible` / `versioned_ciphertext` are real `#[encrypted(...)]`
        // options but deliberately outside the DSL: the whole point of the
        // generator wiring is that a scaffolded encrypted column is redacted in
        // the admin by default. Point at the hand-edit instead of silently
        // accepting or silently dropping them.
        for opt in ["admin_visible", "versioned_ciphertext"] {
            let err = parse_field(&format!("token:String{{encrypted:{opt}}}")).unwrap_err();
            let msg = err.to_string();
            assert!(msg.contains(opt), "must name the option: {msg}");
            assert!(
                msg.contains("#[encrypted"),
                "must point at the attribute to hand-edit: {msg}"
            );
        }
    }

    #[test]
    fn encrypted_modifier_with_state_machine_is_rejected() {
        // R10: a `:states(…)` column is a low-entropy closed set. Randomized
        // encryption makes state queries impossible; deterministic encryption
        // leaks the state of every row through ciphertext equality. Neither is
        // what the author meant.
        let err = parse_field("status:String{encrypted}:states(draft -> published)").unwrap_err();
        let msg = err.to_string();
        // Assert the REASON, not the echoed token — which contains both
        // "encrypted" and "states" for free.
        assert!(
            msg.contains("bypasses encryption"),
            "must name the concrete blocker (the transition handler's raw write): {msg}"
        );
    }

    #[test]
    fn randomized_encrypted_unique_field_is_rejected() {
        // AC6: `{encrypted}` + `:unique` would emit a `CREATE UNIQUE INDEX`
        // over randomized ciphertext (which can never collide, so it enforces
        // nothing) and a `find_by_<col>` that fails at RUNTIME with
        // `EncryptionError::RandomizedEqualityLookup`. Fail at generate time
        // instead, pointing at the deterministic mode.
        let err = parse_field("token:String{encrypted}:unique").unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("token"), "must name the field: {msg}");
        assert!(
            msg.contains("deterministic"),
            "must point at the fix: {msg}"
        );
    }

    #[test]
    fn deterministic_encrypted_unique_field_is_allowed() {
        // Deterministic ciphertext is stable, so a UNIQUE index over it really
        // does enforce plaintext uniqueness and `find_by_<col>` works.
        let f = parse_field("email:String{encrypted:deterministic}:unique").unwrap();
        assert!(f.unique);
        assert_eq!(f.encrypted_mode(), Some(EncryptedMode::Deterministic));
    }

    #[test]
    fn slug_cannot_derive_from_an_encrypted_field() {
        // A slug is stored in its own PLAINTEXT, unique-indexed column and used
        // as the route key, so deriving one from an encrypted column would put
        // a lightly-mangled copy of the secret on disk in the clear and in
        // every URL. Neither mode makes that safe.
        for mode in ["encrypted", "encrypted:deterministic"] {
            let err = parse_fields(&[
                format!("api_token:String{{{mode}}}"),
                "handle:slug{from:api_token}".to_owned(),
            ])
            .unwrap_err();
            let msg = err.to_string();
            assert!(msg.contains("api_token"), "must name the source: {msg}");
            assert!(msg.contains("encrypted"), "must name the reason: {msg}");
        }
        // Deriving from a plaintext sibling is still fine on the same model.
        let fields = parse_fields(&[
            "title:String".to_owned(),
            "api_token:String{encrypted}".to_owned(),
            "handle:slug{from:title}".to_owned(),
        ])
        .unwrap();
        assert_eq!(fields.len(), 3);
    }

    #[test]
    fn encrypted_column_sql_type_is_unbounded_text() {
        // AC4: the stored value is a base64 ciphertext envelope, always longer
        // than the plaintext. `TEXT` is unbounded on both backends, so the
        // envelope always fits.
        let f = parse_field("token:String{encrypted}").unwrap();
        assert_eq!(f.sql_column_type(), "TEXT");
        assert_eq!(
            f.sql_column_type_for(autumn_web::config::DatabaseBackend::Sqlite),
            "TEXT"
        );
        assert_eq!(f.schema_type(), "Text");
        assert_eq!(f.rust_type(), "String");
    }

    #[test]
    fn supported_types_help_mentions_the_encrypted_modifier() {
        // AC7 (docs): the `--help`/error-message surface advertises the token.
        assert!(
            SUPPORTED_TYPES.contains("encrypted"),
            "supported-type help must advertise the encrypted modifier: {SUPPORTED_TYPES}"
        );
    }

    #[test]
    fn unknown_constraint_modifier_is_rejected_by_name() {
        // AC5: a misspelled modifier fails loudly, naming the token.
        let err = parse_field("title:String{maxx=5}").unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("maxx"), "must name the bad token: {msg}");
    }

    #[test]
    fn email_constraint_on_numeric_field_is_rejected() {
        let err = parse_field("age:i32{email}").unwrap_err();
        assert!(err.to_string().contains("email"));
    }

    #[test]
    fn label_constraint_on_non_reference_is_rejected() {
        let err = parse_field("title:String{label:foo}").unwrap_err();
        assert!(err.to_string().contains("references"));
    }

    #[test]
    fn min_greater_than_max_is_rejected() {
        let err = parse_field("title:String{min=10,max=3}").unwrap_err();
        assert!(err.to_string().contains("min"));
    }

    #[test]
    fn non_integer_length_bound_is_rejected() {
        let err = parse_field("title:String{max=abc}").unwrap_err();
        assert!(err.to_string().contains("abc"));
    }

    #[test]
    fn constraint_on_unconstrainable_kind_is_rejected() {
        // A bool field takes no `{…}` modifiers.
        let err = parse_field("active:bool{min=0}").unwrap_err();
        assert!(err.to_string().contains("not supported") || err.to_string().contains("bool"));
    }

    #[test]
    fn empty_constraint_block_is_rejected() {
        let err = parse_field("title:String{}").unwrap_err();
        assert!(err.to_string().contains("empty"));
    }

    #[test]
    fn nullable_string_with_constraint_parses() {
        let f = parse_field("bio:Option<String>{max=200}").unwrap();
        assert!(f.nullable);
        assert_eq!(f.constraints.max.as_deref(), Some("200"));
        assert_eq!(f.validation_attrs(), vec!["length(max = 200)"]);
    }

    #[test]
    fn bare_field_has_empty_constraints() {
        let f = parse_field("title:String").unwrap();
        assert!(f.constraints.is_empty());
        assert!(f.validation_attrs().is_empty());
    }

    #[test]
    fn id_type_parse_rejects_unknown_with_accepted_values_listed() {
        for bad in ["guid", "serial4", "int", "ulid"] {
            let err = IdType::parse(bad).unwrap_err();
            let msg = err.to_string();
            assert!(
                msg.contains(bad),
                "error must echo the bad value '{bad}': {msg}"
            );
            assert!(msg.contains("uuid"), "error must list 'uuid': {msg}");
            assert!(msg.contains("bigint"), "error must list 'bigint': {msg}");
        }
    }

    // ------------------------------------------------------------------
    // enum field kind (issue #1030)
    // ------------------------------------------------------------------

    #[test]
    fn parse_enum_field() {
        let f = parse_field("status:enum{draft,published,archived}").unwrap();
        assert_eq!(f.name, "status");
        assert_eq!(f.kind, FieldKind::Enum);
        assert_eq!(f.variants, vec!["draft", "published", "archived"]);
        assert!(!f.nullable);
        assert_eq!(f.rust_type(), "Status");
        assert_eq!(f.enum_type_name().as_deref(), Some("Status"));
        assert_eq!(f.schema_type(), "Text");
        assert_eq!(f.sql_type(), "TEXT");
        assert_eq!(f.sql_nullability(), "NOT NULL");
        assert!(f.is_enum());
        assert!(FieldKind::Enum.is_enum());
        assert!(!FieldKind::String.is_enum());
    }

    #[test]
    fn parse_enum_field_multiword_name_pascalizes_type() {
        let f = parse_field("review_state:enum{open,closed}").unwrap();
        assert_eq!(f.rust_type(), "ReviewState");
        assert_eq!(f.enum_type_name().as_deref(), Some("ReviewState"));
    }

    #[test]
    fn parse_enum_field_trims_variant_whitespace() {
        let f = parse_field("status:enum{ draft , published }").unwrap();
        assert_eq!(f.variants, vec!["draft", "published"]);
    }

    #[test]
    fn parse_nullable_enum_field() {
        let f = parse_field("status:Option<enum{draft,published}>").unwrap();
        assert_eq!(f.kind, FieldKind::Enum);
        assert!(f.nullable);
        assert_eq!(f.rust_type(), "Option<Status>");
        assert_eq!(f.schema_type(), "Nullable<Text>");
        assert_eq!(f.sql_nullability(), "NULL");
    }

    #[test]
    fn enum_rejects_non_ident_variant() {
        // `2fa` cannot become a Rust enum variant (`2Fa` is not an identifier).
        let err = parse_field("status:enum{2fa,ok}").unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("2fa"), "must name the bad variant: {msg}");
        assert!(
            msg.contains("identifier"),
            "must explain the identifier rule: {msg}"
        );
    }

    #[test]
    fn enum_rejects_variant_pascalizing_to_leading_digit() {
        // `_2fa` passes `is_valid_ident` (leading `_` is allowed), but
        // `pascal("_2fa")` strips the leading underscore and capitalizes
        // nothing before the digit, producing `2fa` — not a valid Rust
        // identifier, so the generated enum variant fails to compile.
        let err = parse_field("status:enum{_2fa,ok}").unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("_2fa"), "must name the bad variant: {msg}");
        assert!(
            msg.contains("identifier"),
            "must explain the identifier rule: {msg}"
        );
    }

    #[test]
    fn enum_rejects_variant_pascalizing_to_empty() {
        // `__` passes `is_valid_ident` (all underscores are allowed chars),
        // but `pascal("__")` produces an empty string — not a valid Rust
        // identifier.
        let err = parse_field("status:enum{__,ok}").unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("__"), "must name the bad variant: {msg}");
        assert!(
            msg.contains("identifier"),
            "must explain the identifier rule: {msg}"
        );
    }

    #[test]
    fn enum_rejects_uppercase_variant() {
        let err = parse_field("status:enum{Draft,ok}").unwrap_err();
        assert!(err.to_string().contains("snake_case identifier"));
    }

    #[test]
    fn enum_rejects_keyword_variant() {
        // Consistent with field-name guarding: never emit code needing r#…
        let err = parse_field("status:enum{type,ok}").unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("type"), "must name the bad variant: {msg}");
        assert!(msg.contains("keyword"), "must explain why: {msg}");
    }

    #[test]
    fn enum_rejects_duplicate_variants() {
        let err = parse_field("status:enum{draft,draft}").unwrap_err();
        assert!(err.to_string().contains("duplicate"));
    }

    #[test]
    fn enum_rejects_pascal_colliding_variants() {
        // `in_review` and `in__review` both pascalize to `InReview`.
        let err = parse_field("status:enum{in_review,in__review}").unwrap_err();
        assert!(err.to_string().contains("InReview"));
    }

    #[test]
    fn enum_rejects_empty_body() {
        let err = parse_field("status:enum{}").unwrap_err();
        assert!(err.to_string().contains("variant"));
    }

    #[test]
    fn enum_rejects_single_variant() {
        let err = parse_field("status:enum{draft}").unwrap_err();
        assert!(err.to_string().contains("at least two"));
    }

    #[test]
    fn enum_rejects_unclosed_brace() {
        let err = parse_field("status:enum{draft,published").unwrap_err();
        assert!(err.to_string().contains("enum{"));
    }

    #[test]
    fn enum_error_hints_about_shell_brace_expansion() {
        // bash/zsh expand an unquoted `enum{a,b}` into `enuma enumb`, so the
        // token the CLI actually receives looks like `status:enumdraft`. Point
        // the user at quoting instead of a bare "unsupported type".
        let err = parse_field("status:enumdraft").unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("quote"), "must suggest quoting: {msg}");
    }

    #[test]
    fn enum_prefixed_typo_still_lists_supported_types() {
        // A type name that happens to start with `enum` but isn't a genuine
        // shell-mangled enum token (e.g. a typo like `enumerable`) must still
        // see the full supported-types list, not just the brace-expansion
        // hint — the two audiences (a real shell-expansion victim and a
        // plain typo) get one message that serves both.
        let err = parse_field("status:enumerable").unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("quote"), "got: {msg}");
        assert!(msg.contains("Supported:"), "got: {msg}");
        assert!(msg.contains("String"), "got: {msg}");
    }

    #[test]
    fn enum_appears_in_supported_types_constant() {
        assert!(
            SUPPORTED_TYPES.contains("enum{"),
            "SUPPORTED_TYPES must list enum{{…}}"
        );
    }

    // ── `unique` field modifier (issue #1032) ───────────────────────────────

    #[test]
    fn parse_unique_string_field() {
        let f = parse_field("email:String:unique").unwrap();
        assert_eq!(f.name, "email");
        assert_eq!(f.kind, FieldKind::String);
        assert!(f.unique);
        assert!(!f.nullable);
    }

    #[test]
    fn parse_non_unique_field_defaults_to_false() {
        let f = parse_field("title:String").unwrap();
        assert!(!f.unique);
    }

    #[test]
    fn parse_unique_option_field_keeps_nullable_and_unique() {
        let f = parse_field("nickname:Option<String>:unique").unwrap();
        assert!(f.nullable);
        assert!(f.unique);
    }

    #[test]
    fn parse_unique_i64_field() {
        let f = parse_field("external_id:i64:unique").unwrap();
        assert_eq!(f.kind, FieldKind::I64);
        assert!(f.unique);
    }

    #[test]
    fn parse_unique_enum_field() {
        let f = parse_field("slug:enum{a,b}:unique").unwrap();
        assert_eq!(f.kind, FieldKind::Enum);
        assert!(f.unique);
    }

    #[test]
    fn parse_unique_references_field() {
        let f = parse_field("profile:references:unique").unwrap();
        assert_eq!(f.name, "profile_id");
        assert_eq!(f.kind, FieldKind::References);
        assert!(f.unique);
    }

    #[test]
    fn parse_unknown_modifier_is_rejected() {
        let err = parse_field("email:String:bogus").unwrap_err();
        assert!(
            err.to_string().contains("bogus") && err.to_string().contains("unique"),
            "got: {err}"
        );
    }

    #[test]
    fn parse_unique_trims_whitespace_around_modifier() {
        let f = parse_field("email:String: unique ").unwrap();
        assert!(f.unique);
    }

    #[test]
    fn unique_appears_in_supported_types_constant() {
        assert!(
            SUPPORTED_TYPES.contains("unique"),
            "SUPPORTED_TYPES must document the :unique modifier"
        );
    }

    // ── state machine modifier (issue #1326) ────────────────────────────────

    #[test]
    fn parse_field_without_state_machine_defaults_to_none() {
        let f = parse_field("title:String").unwrap();
        assert!(f.state_machine.is_none());
    }

    #[test]
    fn parse_state_machine_modifier_captures_transitions() {
        let f = parse_field(
            "status:String:states(draft -> published: can_publish, published -> archived)",
        )
        .unwrap();
        assert_eq!(f.kind, FieldKind::String);
        assert!(!f.nullable);
        let sm = f.state_machine.expect("state machine should be parsed");
        assert_eq!(
            sm.transitions,
            vec![
                StateTransition {
                    from: "draft".to_owned(),
                    to: "published".to_owned(),
                    guard: Some("can_publish".to_owned()),
                },
                StateTransition {
                    from: "published".to_owned(),
                    to: "archived".to_owned(),
                    guard: None,
                },
            ]
        );
    }

    #[test]
    fn parse_state_machine_tolerates_trailing_comma_and_whitespace() {
        let f = parse_field("status:String:states( draft -> published , )").unwrap();
        let sm = f.state_machine.unwrap();
        assert_eq!(sm.transitions.len(), 1);
        assert_eq!(sm.transitions[0].from, "draft");
        assert_eq!(sm.transitions[0].to, "published");
        assert!(sm.transitions[0].guard.is_none());
    }

    #[test]
    fn state_machine_composes_with_unique_and_text() {
        let f = parse_field("stage:Text:unique:states(a -> b)").unwrap();
        assert_eq!(f.kind, FieldKind::Text);
        assert!(f.unique);
        assert_eq!(f.state_machine.unwrap().transitions.len(), 1);
    }

    #[test]
    fn state_machine_on_nullable_string_is_rejected() {
        let err = parse_field("status:Option<String>:states(a -> b)").unwrap_err();
        assert!(err.to_string().contains("non-nullable"), "got: {err}");
    }

    #[test]
    fn state_machine_on_non_string_field_is_rejected() {
        let err = parse_field("count:i64:states(a -> b)").unwrap_err();
        assert!(err.to_string().contains("String"), "got: {err}");
    }

    #[test]
    fn state_machine_on_enum_field_is_rejected() {
        let err = parse_field("status:enum{a,b}:states(a -> b)").unwrap_err();
        assert!(err.to_string().contains("String"), "got: {err}");
    }

    #[test]
    fn empty_state_machine_modifier_is_rejected() {
        let err = parse_field("status:String:states()").unwrap_err();
        assert!(err.to_string().contains("at least one"), "got: {err}");
    }

    #[test]
    fn state_machine_missing_arrow_is_rejected() {
        let err = parse_field("status:String:states(draft published)").unwrap_err();
        assert!(err.to_string().contains("->"), "got: {err}");
    }

    #[test]
    fn state_machine_invalid_state_name_is_rejected() {
        let err = parse_field("status:String:states(Draft -> published)").unwrap_err();
        assert!(err.to_string().contains("state name"), "got: {err}");
    }

    // ── decimal field kind (issue #1038) ────────────────────────────────────

    #[test]
    fn parse_decimal_field_lowercase() {
        let f = parse_field("price:decimal").unwrap();
        assert_eq!(f.name, "price");
        assert_eq!(
            f.kind,
            FieldKind::Decimal {
                precision: 12,
                scale: 2
            }
        );
        assert!(!f.nullable);
    }

    #[test]
    fn parse_decimal_field_pascal_case() {
        let f = parse_field("price:Decimal").unwrap();
        assert_eq!(
            f.kind,
            FieldKind::Decimal {
                precision: 12,
                scale: 2
            }
        );
    }

    #[test]
    fn decimal_defaults_to_precision_12_scale_2() {
        let f = parse_field("price:decimal").unwrap();
        assert_eq!(f.sql_column_type(), "NUMERIC(12,2)");
    }

    #[test]
    fn decimal_custom_precision_and_scale() {
        let f = parse_field("price:decimal{10,2}").unwrap();
        assert_eq!(
            f.kind,
            FieldKind::Decimal {
                precision: 10,
                scale: 2
            }
        );
        assert_eq!(f.sql_column_type(), "NUMERIC(10,2)");
    }

    #[test]
    fn decimal_rust_type_is_rust_decimal() {
        let f = parse_field("price:decimal").unwrap();
        assert_eq!(f.rust_type(), "rust_decimal::Decimal");
    }

    #[test]
    fn decimal_schema_type_is_numeric() {
        let f = parse_field("price:decimal").unwrap();
        assert_eq!(f.schema_type(), "Numeric");
    }

    #[test]
    fn decimal_sql_type_is_numeric() {
        let f = parse_field("price:decimal").unwrap();
        assert_eq!(f.sql_type(), "NUMERIC");
    }

    #[test]
    fn optional_decimal_parses() {
        let f = parse_field("balance:Option<decimal>").unwrap();
        assert_eq!(
            f.kind,
            FieldKind::Decimal {
                precision: 12,
                scale: 2
            }
        );
        assert!(f.nullable);
        assert_eq!(f.rust_type(), "Option<rust_decimal::Decimal>");
        assert_eq!(f.schema_type(), "Nullable<Numeric>");
        assert_eq!(f.sql_nullability(), "NULL");
    }

    #[test]
    fn optional_decimal_with_precision_scale_parses() {
        let f = parse_field("balance:Option<decimal{10,2}>").unwrap();
        assert_eq!(
            f.kind,
            FieldKind::Decimal {
                precision: 10,
                scale: 2
            }
        );
        assert!(f.nullable);
        assert_eq!(f.sql_column_type(), "NUMERIC(10,2)");
    }

    #[test]
    fn unique_decimal_field_parses() {
        let f = parse_field("price:decimal:unique").unwrap();
        assert_eq!(
            f.kind,
            FieldKind::Decimal {
                precision: 12,
                scale: 2
            }
        );
        assert!(f.unique);
    }

    #[test]
    fn decimal_rejects_non_numeric_precision() {
        let err = parse_field("price:decimal{abc,2}").unwrap_err();
        assert!(err.to_string().contains("precision"));
    }

    #[test]
    fn decimal_rejects_non_numeric_scale() {
        let err = parse_field("price:decimal{10,xyz}").unwrap_err();
        assert!(err.to_string().contains("scale"));
    }

    #[test]
    fn decimal_rejects_scale_greater_than_precision() {
        let err = parse_field("price:decimal{2,10}").unwrap_err();
        assert!(err.to_string().contains("scale"));
    }

    #[test]
    fn decimal_rejects_zero_precision() {
        let err = parse_field("price:decimal{0,0}").unwrap_err();
        assert!(err.to_string().contains("precision"));
    }

    #[test]
    fn decimal_rejects_precision_over_rust_decimal_max() {
        // 29 exceeds rust_decimal::Decimal's 28-significant-digit range, even
        // though Postgres's own NUMERIC would happily hold it — the DSL caps
        // at what the generated struct field can actually represent, not
        // what the column type technically permits.
        let err = parse_field("price:decimal{29,2}").unwrap_err();
        assert!(err.to_string().contains("precision"));
    }

    #[test]
    fn decimal_accepts_precision_at_rust_decimal_max() {
        let f = parse_field("price:decimal{28,2}").unwrap();
        assert_eq!(
            f.kind,
            FieldKind::Decimal {
                precision: 28,
                scale: 2
            }
        );
    }

    #[test]
    fn decimal_rejects_missing_closing_brace() {
        let err = parse_field("price:decimal{10,2").unwrap_err();
        assert!(err.to_string().contains("decimal{"));
    }

    #[test]
    fn decimal_error_hints_about_shell_brace_expansion() {
        // bash/zsh expand an unquoted `decimal{10,2}` into two separate
        // words, so the token the CLI actually receives looks like
        // `price:decimal10` — point the user at quoting, same as `enum{...}`.
        let err = parse_field("price:decimal10").unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("quote"), "must suggest quoting: {msg}");
        assert!(msg.contains("Supported:"), "got: {msg}");
    }

    #[test]
    fn decimal_rejects_wrong_number_of_brace_args() {
        let err = parse_field("price:decimal{10}").unwrap_err();
        assert!(err.to_string().contains("precision,scale"));
    }

    #[test]
    fn decimal_prefixed_typo_still_lists_supported_types() {
        // A type name that happens to start with `decimal` but isn't a
        // genuine shell-mangled decimal token (e.g. a typo like
        // `decimalize`) must still see the full supported-types list, not
        // just the brace-expansion hint — mirrors enum's
        // `enum_prefixed_typo_still_lists_supported_types` precedent.
        let err = parse_field("price:decimalize").unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("quote"), "got: {msg}");
        assert!(msg.contains("Supported:"), "got: {msg}");
        assert!(msg.contains("String"), "got: {msg}");
    }

    #[test]
    fn parse_decimal_type_tolerates_untrimmed_input() {
        // Defensive-in-depth (PR review, gemini-code-assist): `parse_field`
        // already trims `ty` before any type parser sees it, so this isn't
        // reachable through the public `parse_field` API today — but
        // `parse_decimal_type` re-trims `body` itself too, so a direct call
        // (or a future caller that doesn't pre-trim) with trailing
        // whitespace after the closing brace still parses instead of
        // failing `strip_suffix('}')`.
        assert_eq!(
            parse_decimal_type("decimal{10,2} ").unwrap(),
            Some((10, 2, false))
        );
    }

    #[test]
    fn decimal_appears_in_supported_types_constant() {
        assert!(
            SUPPORTED_TYPES.contains("decimal"),
            "SUPPORTED_TYPES must list decimal{{p,s}}"
        );
    }

    #[test]
    fn decimal_in_list_of_fields() {
        let tokens = vec!["title:String".into(), "price:decimal".into()];
        let fields = parse_fields(&tokens).unwrap();
        assert_eq!(fields.len(), 2);
        assert_eq!(
            fields[1].kind,
            FieldKind::Decimal {
                precision: 12,
                scale: 2
            }
        );
    }

    #[test]
    fn decimal_numeric_stays_unmapped_for_db_pull_inverse() {
        // Precision/scale can't be reconstructed from `numeric` udt_name alone
        // (same precedent as `jsonb`/Attachment), so `db pull` introspection
        // deliberately leaves it unsupported rather than guessing.
        assert!(sql_type_to_field_kind("numeric").is_none());
    }
}

/// Parity tests locking `dsl::FieldKind` / `dsl::IdType` to the canonical
/// `autumn-schema-core` IR (declarative schema wave, tracking issue #1975).
///
/// This module is the **drift lock**: for every `FieldKind` / `IdType` it
/// asserts the schema-core equivalent yields byte-identical Rust / diesel / SQL
/// mappings on both backends. If either side changes a mapping without the
/// other agreeing, one of these assertions fails. It exercises no `dsl.rs`
/// runtime behaviour — it only reads the existing mapping functions.
#[cfg(test)]
mod schema_core_parity {
    use super::{Field, FieldConstraints, FieldKind, IdType};
    use autumn_schema_core::{Backend, ColumnType, IdKind};
    use autumn_web::config::DatabaseBackend;

    /// Build a minimal, non-null `Field` wrapping `kind` so we can call the
    /// `Field`-level `sql_column_type_for` — the method that renders the full
    /// `NUMERIC(precision,scale)` for decimals (the bare `FieldKind::sql_type_for`
    /// returns only `"NUMERIC"`), i.e. the exact string that reaches DDL.
    fn field_of(kind: FieldKind, variants: Vec<String>) -> Field {
        Field {
            name: "col".to_owned(),
            kind,
            nullable: false,
            variants,
            unique: false,
            constraints: FieldConstraints::default(),
            state_machine: None,
        }
    }

    #[test]
    fn field_kind_mappings_match_schema_core() {
        // (FieldKind, schema-core ColumnType, enum variants for the Field).
        // `References` maps to `Int64` — a foreign key is a `Column` property in
        // the IR, not a distinct column type. `Enum` compares the `String`/`TEXT`
        // storage fallback (the concrete enum type is a later-slice concern).
        let cases: Vec<(FieldKind, ColumnType, Vec<String>)> = vec![
            (FieldKind::String, ColumnType::Text, vec![]),
            (FieldKind::Text, ColumnType::Text, vec![]),
            // `RichText` is a presentation/UX distinction only — it stores the
            // Markdown *source*, so every storage mapping is `Text`'s.
            (FieldKind::RichText, ColumnType::Text, vec![]),
            (FieldKind::I32, ColumnType::Int32, vec![]),
            (FieldKind::I64, ColumnType::Int64, vec![]),
            (FieldKind::References, ColumnType::Int64, vec![]),
            (FieldKind::Bool, ColumnType::Bool, vec![]),
            (FieldKind::F32, ColumnType::Float32, vec![]),
            (FieldKind::F64, ColumnType::Float64, vec![]),
            (FieldKind::Uuid, ColumnType::Uuid, vec![]),
            (FieldKind::NaiveDateTime, ColumnType::Timestamp, vec![]),
            (FieldKind::DateTime, ColumnType::TimestampTz, vec![]),
            (FieldKind::Bytea, ColumnType::Bytes, vec![]),
            (FieldKind::Attachment, ColumnType::Attachment, vec![]),
            (FieldKind::Json, ColumnType::Json, vec![]),
            (
                FieldKind::Decimal {
                    precision: 12,
                    scale: 2,
                },
                ColumnType::Decimal {
                    precision: 12,
                    scale: 2,
                },
                vec![],
            ),
            (
                FieldKind::Enum,
                ColumnType::Enum {
                    variants: vec!["a".to_owned(), "b".to_owned(), "c".to_owned()],
                },
                vec!["a".to_owned(), "b".to_owned(), "c".to_owned()],
            ),
        ];

        for (fk, ct, variants) in cases {
            let field = field_of(fk, variants);

            // Rust `#[model]` type (storage fallback for Enum: `String`).
            assert_eq!(
                fk.rust_type(),
                ct.rust_type(),
                "rust_type parity for {fk:?}"
            );

            // Diesel `schema.rs` token, both backends.
            assert_eq!(
                fk.schema_type_for(DatabaseBackend::Postgres),
                ct.diesel_type(Backend::Postgres),
                "pg diesel token parity for {fk:?}"
            );
            assert_eq!(
                fk.schema_type_for(DatabaseBackend::Sqlite),
                ct.diesel_type(Backend::Sqlite),
                "sqlite diesel token parity for {fk:?}"
            );

            // SQL DDL type, both backends — via the `Field`-level renderer so a
            // decimal's `NUMERIC(precision,scale)` is compared in full.
            assert_eq!(
                field.sql_column_type_for(DatabaseBackend::Postgres),
                ct.sql_type(Backend::Postgres),
                "pg sql type parity for {fk:?}"
            );
            assert_eq!(
                field.sql_column_type_for(DatabaseBackend::Sqlite),
                ct.sql_type(Backend::Sqlite),
                "sqlite sql type parity for {fk:?}"
            );

            // SQLite diesel-conversion eligibility.
            assert_eq!(
                fk.sqlite_has_diesel_conversion(),
                ct.sqlite_has_diesel_conversion(),
                "sqlite diesel-conversion parity for {fk:?}"
            );
        }
    }

    #[test]
    fn id_type_mappings_match_schema_core() {
        for (idt, idk) in [
            (IdType::BigSerial, IdKind::BigSerial),
            (IdType::Uuid, IdKind::Uuid),
        ] {
            assert_eq!(
                idt.rust_type(),
                idk.rust_type(),
                "id rust_type parity for {idt:?}"
            );
            for (db, be) in [
                (DatabaseBackend::Postgres, Backend::Postgres),
                (DatabaseBackend::Sqlite, Backend::Sqlite),
            ] {
                assert_eq!(
                    idt.pk_sql_for(db),
                    idk.pk_sql(be),
                    "pk_sql parity for {idt:?} on {db:?}"
                );
                assert_eq!(
                    idt.schema_type_for(db),
                    idk.diesel_type(be),
                    "id diesel token parity for {idt:?} on {db:?}"
                );
            }
        }
    }
}