djogi 0.1.0-alpha.2

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

use crate::model::Model;
use crate::pg::decode::FromJoinedPgRow;
use crate::query::PortablePredicateError;
use crate::query::condition::Condition;
use crate::query::field::FieldRef;
use crate::query::order::OrderExpr;
use crate::query::q::{CompoundOp, Q};
use crate::relation::path::RelationPath;
use crate::relation::prefetch::{ErasedPrefetch, prefetch_loader};
use crate::relation::select_related::{ErasedSelectRelated, child_descriptor, join_decoder};
use std::hash::Hash;
use std::marker::PhantomData;

/// `DISTINCT` mode for a QuerySet.
///
/// `None` emits a plain `SELECT ...`. `Plain` emits `SELECT DISTINCT ...`.
/// `On(cols)` emits `SELECT DISTINCT ON (col_a, col_b) ...` — the Postgres
/// extension that keeps the first row per `(col_a, col_b)` tuple, where
/// "first" is determined by the query's `ORDER BY`.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum DistinctMode {
    /// `SELECT ...` — no DISTINCT clause.
    #[default]
    None,
    /// `SELECT DISTINCT ...`.
    Plain,
    /// `SELECT DISTINCT ON (col_a, col_b) ...` — Postgres extension.
    /// Column names are macro-baked `&'static str` literals, never user input.
    On(Vec<&'static str>),
}

/// Type-erased binding from a [`QuerySet`] to a [`sassi::Punnu`] for the
/// post-fetch cache hook.
///
/// # What
///
/// Implementors carry a concrete `sassi::Punnu<T>` and expose a single
/// async [`CacheTarget::insert`] hook that the terminal methods call
/// once per fetched row. The trait is `pub(crate)` because it exists
/// purely to keep the `T: sassi::Cacheable` bound off the
/// [`QuerySet`] struct (`Punnu<T: Cacheable>` would otherwise force
/// the bound onto every `impl<T: Model> QuerySet<T>` block via rustc's
/// well-formedness check).
///
/// # Why a trait object, not `Option<Punnu<T>>`
///
/// See the doc on [`QuerySet::cache_target`] — the upshot is that
/// naming `Punnu<T>` directly in the `QuerySet` field set would
/// propagate `T: sassi::Cacheable` everywhere the struct is named.
/// Type-erasing the handle through this trait keeps that bound
/// localised to [`QuerySet::cache`], which is the only place it
/// actually matters.
///
/// # Why async-fn-in-trait
///
/// `Punnu::insert` is async (it write-throughs to any attached L2
/// backend), so the hook must be async too. The Send bound on the
/// returned future matches the `+ Send` shape every QuerySet terminal
/// already returns — terminals can `.await` the insert from any
/// multi-thread executor.
pub(crate) trait CacheTarget<T>: Send + Sync {
    /// Insert one row into the bound Punnu. Returns a `+ Send` future
    /// that resolves to `()` (errors are logged and swallowed inside
    /// the implementor — see [`PunnuCacheTarget::insert`] — so the
    /// fetch terminal is never aborted by a cache-side failure).
    ///
    /// Takes `&T` (not `T`) because the terminal still owns the
    /// fetched `Vec<T>` and returns it to the caller — the cache
    /// hook is a side-effect on a borrow, not a transfer of
    /// ownership. The implementor clones internally inside the
    /// wrapper where the necessary `T: Clone` bound is satisfied
    /// (see [`PunnuCacheTarget`] — the wrapper requires
    /// `T: Cacheable + Clone`); routing the clone through the
    /// trait keeps the `T: Clone` bound off every terminal-method
    /// signature in `terminal.rs`, preserving the pre-T7.3 surface
    /// for adopters who never call `.cache(...)`.
    ///
    /// `&self` (not `&mut`) because `Punnu::insert` does. Returning
    /// a boxed future keeps the trait object-safe.
    fn insert<'a>(
        &'a self,
        value: &'a T,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send + 'a>>;
}

/// Concrete [`CacheTarget`] backed by a `sassi::Punnu<T>`.
///
/// # Why a wrapper, not a blanket impl on `Punnu<T>`
///
/// `Punnu<T>` lives in the sibling `sassi` crate; the orphan rule
/// forbids implementing a djogi-owned trait on a sassi-owned type
/// outside djogi without a wrapper. The wrapper is also where errors
/// from `Punnu::insert` get logged-and-swallowed — see the impl below
/// for why "do not propagate cache-side errors out of a fetch
/// terminal" is the load-bearing contract.
pub(crate) struct PunnuCacheTarget<T: crate::types::Cacheable> {
    punnu: sassi::Punnu<T>,
}

impl<T: crate::types::Cacheable> PunnuCacheTarget<T> {
    /// Wrap a `sassi::Punnu<T>` for use as a [`CacheTarget`].
    pub(crate) fn new(punnu: sassi::Punnu<T>) -> Self {
        Self { punnu }
    }
}

impl<T: crate::types::Cacheable + Clone> CacheTarget<T> for PunnuCacheTarget<T> {
    fn insert<'a>(
        &'a self,
        value: &'a T,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send + 'a>> {
        // Clone the Punnu handle (cheap: `Arc::clone` on
        // `Arc<PunnuInner<T>>`) and the row (delegated to the user-
        // supplied `Clone` impl — required at `.cache(...)` call
        // time via the `T: Clone` bound on `QuerySet::cache`).
        // Cloning is required because `Punnu::insert(T)` takes `T`
        // by value — sassi's identity-map semantics own the
        // inserted value in an `Arc<T>` once it lands in the L1
        // snapshot.
        let punnu = self.punnu.clone();
        let value = value.clone();
        Box::pin(async move {
            // Per Cluster 8δ granular plan §3 commit T7.3 risk note
            // (line 148): "Errors from `insert` (e.g.,
            // `InsertError::Conflict` under `OnConflict::Reject`)
            // MUST NOT abort the fetch; log via `tracing::warn!` and
            // continue. Do NOT add `?` to the insert."
            //
            // The terminal contract is "fetch returned rows;
            // cache-side write-through is best-effort". A conflict
            // under `OnConflict::Reject`, an L2-backend
            // serialization failure, an LRU pressure spike — none of
            // these change what Postgres said the rows are. Logging
            // the error preserves observability without breaking the
            // caller.
            if let Err(e) = punnu.insert(value).await {
                tracing::warn!(
                    target: "djogi::cache",
                    error = ?e,
                    "Punnu::insert failed during QuerySet::cache hook; continuing",
                );
            }
        })
    }
}

/// Lazy query builder. Nothing hits the database until a terminal method
/// (added in Task 6) is called.
///
/// See the module-level documentation for design rationale, variance, and
/// short-circuit semantics.
pub struct QuerySet<T: Model> {
    /// Accumulated filter tree. Starts as
    /// [`Q::always_true()`](crate::query::Q) — the vacuous identity
    /// — and grows via AND as `filter` / `exclude` / `filter_struct` /
    /// `exclude_struct` are chained.
    ///
    /// # Substrate
    ///
    /// As of Cluster 8γ Stage 2 (T6.9), the queryset's filter
    /// substrate is the [`Q<T>`](crate::query::Q) algebra rather than
    /// the legacy [`Condition`] tree. Phase 8eta PR2b made SQL emission
    /// walk `Q<T>` directly: legacy [`Condition`] payloads still
    /// round-trip as `Q::Condition(_)`, while trusted portable
    /// predicates stay as `Q::Portable(_)` and emit through the
    /// portable SQL walker. Character-for-character SQL parity with the
    /// pre-flip queryset remains the load-bearing contract for legacy
    /// condition paths.
    pub(crate) condition: Q<T>,
    /// Ordering expressions in emission order. `order_by` appends; it does
    /// not replace.
    pub(crate) ordering: Vec<OrderExpr>,
    /// `true` when user code explicitly called [`QuerySet::order_by`].
    ///
    /// `QuerySet::new()` may seed `ordering` from
    /// [`Model::default_order_by`], especially for proxy models.
    /// Mutating terminals should reject only *explicit* `order_by(...)`
    /// tails, not model-default ordering.
    pub(crate) has_explicit_ordering: bool,
    /// DISTINCT mode — see [`DistinctMode`].
    pub(crate) distinct: DistinctMode,
    /// SQL `LIMIT` — `None` means no limit. `i64` to match Postgres.
    pub(crate) limit: Option<i64>,
    /// SQL `OFFSET` — `None` means no offset. `i64` to match Postgres.
    pub(crate) offset: Option<i64>,
    // EMPTY CONTRACT: terminal methods return the empty result (`Vec::new`,
    // `None`, `0`, `false`, etc.) WITHOUT issuing SQL when `self.is_empty`
    // is `true`. For mutation terminals (`update`/`delete` families), this
    // short-circuit runs AFTER unsupported-state validation, matching the
    // validate-then-short-circuit contract in `insert_select.rs`.
    // This is the point of `QuerySet::none()` — authorization / feature-flag
    // branches can short-circuit the DB round-trip without a special-cased
    // `if` at the call site.
    //
    // Grep marker: TASK6:empty_contract
    //
    /// Short-circuit flag — `true` means terminal methods return the
    /// empty result without a DB round-trip. Set only by
    /// [`QuerySet::none`].
    pub(crate) is_empty: bool,
    /// Registered prefetch paths — one entry per call to
    /// [`QuerySet::prefetch`]. Consumed by
    /// [`QuerySet::fetch_all_prefetched`](crate::query::QuerySet::fetch_all_prefetched)
    /// to run a stitching query per path after the main result set
    /// comes back. Deduplicated on registration: calling `.prefetch(path)`
    /// twice with the same [`RelationPath::source_column`] is a no-op on
    /// the second call. Kept separate from `condition`/`ordering`/etc. so
    /// a plain `.fetch_all(...)` on a queryset with prefetches compiles
    /// and behaves exactly as if no prefetch was registered — prefetches
    /// only take effect on the dedicated terminal.
    pub(crate) prefetch_paths: Vec<ErasedPrefetch>,
    /// Registered `select_related` paths — one entry per call to
    /// [`QuerySet::select_related`]. Consumed by
    /// [`QuerySet::fetch_all_joined`](crate::query::QuerySet::fetch_all_joined)
    /// to emit `LEFT JOIN` clauses and aliased child columns on the main
    /// query (no follow-up round trips — that's the whole point of
    /// `select_related` over `prefetch`). Deduplicated on registration
    /// by `source_column` in the same way `prefetch_paths` is: a second
    /// `.select_related(path)` for the same source column is a no-op.
    /// Kept separate from `prefetch_paths` because the two emission
    /// strategies are structurally different — one expands the
    /// `SELECT` list + adds a JOIN, the other fans out into a per-
    /// path follow-up query — and combining them would leak that
    /// distinction into the type.
    pub(crate) select_related_paths: Vec<ErasedSelectRelated>,
    /// Row-level lock mode — Phase 4 Task 7 shipped the `ForUpdate*`
    /// family; djogi#104 added the `ForShare*` family. Default
    /// [`LockMode::None`] emits no tail; non-default variants append
    /// `FOR UPDATE` / `FOR SHARE` (optionally `NOWAIT` / `SKIP LOCKED`)
    /// to the SELECT. See [`crate::query::lock`] for the full behaviour
    /// table and the pool-backed footgun note.
    pub(crate) lock: crate::query::lock::LockMode,
    /// Optional Punnu binding — Cluster 8δ T7.3. When `Some(handle)`,
    /// every terminal method that produces user-facing rows
    /// (`fetch_all` / `first` / `fetch_one`) inserts each fetched row
    /// into the bound Punnu via [`sassi::Punnu::insert`] post-fetch,
    /// before returning the value to the caller. Identity-map
    /// semantics + the configured [`sassi::OnConflict`] policy from
    /// sassi apply.
    ///
    /// # Why a type-erased handle, not `Option<Punnu<T>>` directly
    ///
    /// `sassi::Punnu<T>` is defined `Punnu<T: Cacheable>`, so naming
    /// the type in a field of `QuerySet<T: Model>` would force
    /// `T: Cacheable` onto every existing `impl<T: Model> QuerySet<T>`
    /// block in the crate (the bound propagates structurally through
    /// rustc's well-formedness check). Type-erasing the binding
    /// through [`CacheTarget`] keeps the existing surface — every
    /// non-cache builder, every terminal, every Clone / Debug impl —
    /// unchanged for non-cacheable `T`. The bound `T: Cacheable` is
    /// applied only on the [`QuerySet::cache`](Self::cache) builder
    /// where it actually matters.
    ///
    /// # Why `Arc`-cheap cloning
    ///
    /// `sassi::Punnu<T>` is `Arc`-internal
    /// (`sassi-reference/sassi/src/punnu/pool.rs` lines 112–122 — the
    /// struct holds a single `Arc<PunnuInner<T>>` and `Clone` clones
    /// the `Arc`). The boxed [`CacheTarget`] underneath similarly
    /// clones a single `Arc<dyn ...>` handle. Binding a queryset to a
    /// Punnu therefore records "this QuerySet feeds that Punnu
    /// instance" rather than taking a snapshot of the pool's contents.
    ///
    /// # Why outside the SQL emit path
    ///
    /// The SQL emitter ([`crate::query::sql::build_select`] and
    /// friends) never reads this field. The cache modifier is purely
    /// additive: SQL output, query plan, and the lifetime of the
    /// QuerySet are unchanged whether `cache_target` is `None` or
    /// `Some(_)`. The post-fetch hook fires exactly once per terminal
    /// call.
    pub(crate) cache_target: Option<std::sync::Arc<dyn CacheTarget<T>>>,
    /// Covariant `T` tag; never owns or borrows a `T`.
    _model: PhantomData<fn() -> T>,
}

/// A queryset whose accumulated predicate tree has been proven reducible to
/// Sassi's portable [`BasicPredicate`](sassi::BasicPredicate) algebra.
///
/// Constructed only by [`QuerySet::try_portable`]. The stored predicate is
/// produced by a trusted borrow-walk over `Q<T>`; there is no public
/// constructor accepting a raw Sassi predicate, so downstream code cannot
/// pair forged field names with unrelated extractors and then feed the result
/// into a cache boundary.
pub struct PortableQuerySet<T: Model> {
    inner: QuerySet<T>,
    portable_predicate: sassi::BasicPredicate<T>,
}

/// Cached terminal wrapper for a portable queryset.
///
/// The wrapper owns the database queryset and borrows the target Punnu only
/// long enough to clone Sassi's cheap handle before delegating to the existing
/// QuerySet terminal implementations.
pub struct CachedPortableQuerySet<'p, T: Model + crate::types::Cacheable + Clone> {
    inner: PortableQuerySet<T>,
    punnu: &'p sassi::Punnu<T>,
}

fn basic_predicate_kind<T: Model>(predicate: &sassi::BasicPredicate<T>) -> &'static str {
    match predicate {
        sassi::BasicPredicate::True => "True",
        sassi::BasicPredicate::False => "False",
        sassi::BasicPredicate::Field(_) => "Field",
        sassi::BasicPredicate::And(_) => "And",
        sassi::BasicPredicate::Or(_) => "Or",
        sassi::BasicPredicate::Not(_) => "Not",
        sassi::BasicPredicate::Xor(_, _) => "Xor",
        _ => "<unknown>",
    }
}

impl<T: Model> std::fmt::Debug for PortableQuerySet<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("PortableQuerySet")
            .field("inner", &self.inner)
            .field(
                "portable_predicate",
                &basic_predicate_kind(&self.portable_predicate),
            )
            .finish()
    }
}

impl<T: Model> PortableQuerySet<T> {
    /// Drop the portable-cache guarantee and return to normal SQL queryset
    /// execution.
    #[must_use = "querysets are lazy — dropping one silently omits the query"]
    pub fn into_query_set(self) -> QuerySet<T> {
        self.inner
    }

    /// Borrow the underlying SQL queryset.
    pub fn as_query_set(&self) -> &QuerySet<T> {
        &self.inner
    }

    /// Borrow the Sassi predicate proven at the cache boundary.
    pub fn predicate(&self) -> &sassi::BasicPredicate<T> {
        &self.portable_predicate
    }

    /// Run the queryset and return every matching row. Equivalent to
    /// [`QuerySet::fetch_all`]; the portable wrapper delegates without
    /// rebinding any cache target so the query reaches the database
    /// unchanged. Pair with [`PortableQuerySet::cache`] when you want
    /// the returned rows mirrored into a [`sassi::Punnu`].
    pub fn fetch_all<'ctx>(
        self,
        ctx: &'ctx mut crate::DjogiContext,
    ) -> impl std::future::Future<Output = Result<Vec<T>, crate::DjogiError>> + Send + 'ctx
    where
        T: crate::pg::decode::FromPgRow + Send + Unpin + 'ctx,
    {
        self.inner.fetch_all(ctx)
    }

    /// Run the queryset and return exactly one matching row. Delegates to
    /// [`QuerySet::fetch_one`]; mirrors the same `NotFound`/`MultipleFound`
    /// errors as the underlying queryset.
    pub fn fetch_one<'ctx>(
        self,
        ctx: &'ctx mut crate::DjogiContext,
    ) -> impl std::future::Future<Output = Result<T, crate::DjogiError>> + Send + 'ctx
    where
        T: crate::pg::decode::FromPgRow + Send + Unpin + 'ctx,
    {
        self.inner.fetch_one(ctx)
    }

    /// Run the queryset and return the first matching row, or `None` if no
    /// rows match. Delegates to [`QuerySet::first`].
    pub fn first<'ctx>(
        self,
        ctx: &'ctx mut crate::DjogiContext,
    ) -> impl std::future::Future<Output = Result<Option<T>, crate::DjogiError>> + Send + 'ctx
    where
        T: crate::pg::decode::FromPgRow + Send + Unpin + 'ctx,
    {
        self.inner.first(ctx)
    }

    /// Run the queryset and return the row count. Delegates to
    /// [`QuerySet::count`]; cache binding is intentionally not applied
    /// because `COUNT(*)` does not return rows the identity map could
    /// absorb.
    pub fn count<'ctx>(
        self,
        ctx: &'ctx mut crate::DjogiContext,
    ) -> impl std::future::Future<Output = Result<i64, crate::DjogiError>> + Send + 'ctx
    where
        T: 'ctx,
    {
        self.inner.count(ctx)
    }
}

impl<T: Model + crate::types::Cacheable + Clone> PortableQuerySet<T> {
    /// Bind a [`sassi::Punnu`] to the queryset so that rows returned by the
    /// next terminal call are mirrored into the Punnu's identity map.
    ///
    /// The returned [`CachedPortableQuerySet`] borrows `punnu` only long
    /// enough to clone Sassi's cheap `Arc`-backed handle when the terminal
    /// fires; the queryset itself stays owned. Cache binding only applies
    /// to row-returning terminals (`fetch_all`, `fetch_one`, `first`);
    /// `count` runs unchanged because it returns no rows.
    #[must_use = "querysets are lazy — dropping one silently omits the query"]
    pub fn cache(self, punnu: &sassi::Punnu<T>) -> CachedPortableQuerySet<'_, T> {
        CachedPortableQuerySet { inner: self, punnu }
    }
}

impl<T: Model + crate::types::Cacheable + Clone> CachedPortableQuerySet<'_, T> {
    /// Run the queryset, return every matching row, and mirror the rows
    /// into the bound Punnu's identity map.
    ///
    /// Cache binding happens at the moment of the terminal call: the
    /// captured `&Punnu<T>` is cloned into the queryset's cache target,
    /// and the existing [`QuerySet::fetch_all`] terminal pipeline upserts
    /// rows under the same on-commit hook used elsewhere.
    pub fn fetch_all<'ctx>(
        self,
        ctx: &'ctx mut crate::DjogiContext,
    ) -> impl std::future::Future<Output = Result<Vec<T>, crate::DjogiError>> + Send + 'ctx
    where
        T: crate::pg::decode::FromPgRow + Send + Unpin + 'ctx,
    {
        let qs = self.inner.into_query_set().bind_cache(self.punnu.clone());
        qs.fetch_all(ctx)
    }

    /// Run the queryset, return exactly one matching row, and mirror that
    /// row into the bound Punnu. Same error semantics as
    /// [`QuerySet::fetch_one`].
    pub fn fetch_one<'ctx>(
        self,
        ctx: &'ctx mut crate::DjogiContext,
    ) -> impl std::future::Future<Output = Result<T, crate::DjogiError>> + Send + 'ctx
    where
        T: crate::pg::decode::FromPgRow + Send + Unpin + 'ctx,
    {
        let qs = self.inner.into_query_set().bind_cache(self.punnu.clone());
        qs.fetch_one(ctx)
    }

    /// Run the queryset, return the first matching row (or `None`), and
    /// mirror that row — when present — into the bound Punnu.
    pub fn first<'ctx>(
        self,
        ctx: &'ctx mut crate::DjogiContext,
    ) -> impl std::future::Future<Output = Result<Option<T>, crate::DjogiError>> + Send + 'ctx
    where
        T: crate::pg::decode::FromPgRow + Send + Unpin + 'ctx,
    {
        let qs = self.inner.into_query_set().bind_cache(self.punnu.clone());
        qs.first(ctx)
    }

    /// Run the queryset and return the row count.
    ///
    /// Cache binding is intentionally not applied — `COUNT(*)` returns no
    /// rows for the identity map to absorb. Adopters wanting both a count
    /// and the rows themselves should issue separate `fetch_all` and
    /// `count` calls; the framework does not synthesise a count from a
    /// cached row vector because the count must reflect the database, not
    /// the locally cached subset.
    pub fn count<'ctx>(
        self,
        ctx: &'ctx mut crate::DjogiContext,
    ) -> impl std::future::Future<Output = Result<i64, crate::DjogiError>> + Send + 'ctx
    where
        T: 'ctx,
    {
        self.inner.into_query_set().count(ctx)
    }
}

#[cfg(any(test, feature = "testing"))]
impl<T> CachedPortableQuerySet<'_, T>
where
    T: Model + crate::types::Cacheable + Clone + crate::pg::decode::FromPgRow,
{
    /// Render the plain `SELECT` SQL for test assertions.
    ///
    /// Mirrors [`QuerySet::render_select_sql_for_testing`] without forcing
    /// tests to unwrap the cache-bound wrapper.
    pub fn render_select_sql_for_testing(&self) -> Result<String, PortablePredicateError> {
        self.inner.as_query_set().render_select_sql_for_testing()
    }
}

impl<T: Model + crate::types::Cacheable + Clone> std::fmt::Debug for CachedPortableQuerySet<'_, T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.inner.as_query_set().fmt(f)
    }
}

// Phase 8eta PR2b — manual `Clone` for `QuerySet<T>`. `Q<T>` itself
// carries a manual `Clone` impl (no `T: Clone` propagation), so this
// just delegates `condition.clone()` directly without lowering through
// `q_to_condition_ref`. The pre-PR2b lower-and-rewrap approach forced
// every clone to flatten the trusted-portable wrapper into a legacy
// `Condition` tree, which `BasicPredicate::Field(_)` could not survive
// once the `Q::Portable` flip exposed Sassi `Field` leaves. Cloning the
// `Q<T>` directly preserves the trusted-provenance shape across clones.
impl<T: Model> Clone for QuerySet<T> {
    fn clone(&self) -> Self {
        QuerySet {
            condition: self.condition.clone(),
            ordering: self.ordering.clone(),
            has_explicit_ordering: self.has_explicit_ordering,
            distinct: self.distinct.clone(),
            limit: self.limit,
            offset: self.offset,
            is_empty: self.is_empty,
            // `ErasedPrefetch: Clone` (shallow clone — fn pointers and
            // `&'static str` are trivially copyable). Cloning preserves
            // prefetch registrations across any `if`/`else` branch that
            // keeps a partially-built queryset around.
            prefetch_paths: self.prefetch_paths.clone(),
            // `ErasedSelectRelated: Clone` for the same reason — the
            // struct carries only `&'static str`, a static slice, and
            // a fn pointer, so cloning is bit-copy-cheap.
            select_related_paths: self.select_related_paths.clone(),
            // `LockMode` is `Copy` — bit-copy is trivial.
            lock: self.lock,
            // Cluster 8δ T7.3: `Arc::clone` on the trait-object handle
            // — the underlying `Punnu<T>` is `Arc`-internal so the bind
            // semantics carry through every queryset clone. A clone of
            // a `.cache(&p)`-bound queryset still feeds the same `p`.
            cache_target: self.cache_target.clone(),
            _model: PhantomData,
        }
    }
}

// Phase 8eta PR2b — `Debug` impl uses the manual `Q<T>: Debug` walker
// directly. Pre-PR2b the impl lowered through `q_to_condition_ref` to
// avoid `T: Debug`; the new `Q<T>` manual `Debug` walker handles the
// model-bound elision in one place, so this impl just forwards. SQL
// structure visibility is preserved (the variant-walker prints
// `Q::Portable(_)` / `Q::Compound { op, parts }` etc. just like the
// legacy `Condition` shape did) without round-tripping through the
// retired bridge.
impl<T: Model> std::fmt::Debug for QuerySet<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        // Cluster 8δ T7.3: `cache_target` is intentionally excluded
        // from the Debug projection. The cache modifier is purely
        // additive on the post-fetch side (SQL output, query plan,
        // and every accumulator on the build path are byte-identical
        // whether `.cache(...)` was called or not), so the Debug
        // shape — which downstream tests grep against to check SQL
        // structure — must stay invariant under `.cache(...)`.
        f.debug_struct("QuerySet")
            .field("table", &T::table_name())
            .field("condition", &self.condition)
            .field("ordering", &self.ordering)
            .field("distinct", &self.distinct)
            .field("limit", &self.limit)
            .field("offset", &self.offset)
            .field("is_empty", &self.is_empty)
            .field("prefetch_paths", &self.prefetch_paths)
            .field("select_related_paths", &self.select_related_paths)
            .field("lock", &self.lock)
            .finish()
    }
}

#[cfg(any(test, feature = "testing"))]
impl<T> QuerySet<T>
where
    T: Model + crate::pg::decode::FromPgRow,
{
    /// Render the plain `SELECT` SQL for test assertions.
    ///
    /// Exposed only under the `testing` feature so integration tests can pin
    /// SQL-emitter invariants without coupling to the `Debug` projection.
    pub fn render_select_sql_for_testing(&self) -> Result<String, PortablePredicateError> {
        crate::query::sql::build_select(self).map(|acc| acc.into_parts().0)
    }
}

impl<T: Model> Default for QuerySet<T> {
    fn default() -> Self {
        Self::new()
    }
}

/// AND-combine an arbitrary `IntoQ<T>` term onto an existing `Q<T>`
/// substrate without flattening the existing structure.
///
/// Phase 8eta PR2b — replaces the pre-PR2b
/// `and_condition_into_q(Q<T>, Condition) -> Q<T>` helper. The pre-PR2b
/// helper lowered the existing `Q<T>` through `q_to_condition` and
/// re-wrapped the combined result as `Q::Condition(_)`, which destroyed
/// the trusted-provenance shape of any inner `Q::Portable` leaves. The
/// new helper composes through `Q<T>: BitAnd<Q<T>>` so portable leaves
/// stay reducible at the cache boundary (PR4) and `Q::Portable & Q::Portable`
/// flattens through the trusted Sassi reducer.
///
/// `Q::Portable(PortablePredicate::True)` (the unfiltered identity)
/// short-circuits to the addition unchanged so unfiltered querysets do
/// not pick up a redundant `TRUE AND ...` wrapper.
fn and_q_into_q<T: Model, A: crate::query::IntoQ<T>>(current: Q<T>, addition: A) -> Q<T> {
    let added = addition.into_q();
    if is_q_vacuously_true(&current) {
        added
    } else {
        current & added
    }
}

/// Test whether a `Q<T>` is structurally vacuously TRUE. Mirrors the
/// `query::sql::q_is_vacuously_true` helper (kept module-private there);
/// duplicating the small walker here lets `and_q_into_q` short-circuit
/// without reaching across crate-private boundaries while the legacy
/// SQL path remains in transition.
fn is_q_vacuously_true<T: Model>(q: &Q<T>) -> bool {
    use crate::query::q::CompoundOp;
    match q {
        Q::Portable(p) => match p.inner_ref() {
            sassi::BasicPredicate::True => true,
            sassi::BasicPredicate::And(parts) => parts
                .iter()
                .all(|c| matches!(c, sassi::BasicPredicate::True)),
            _ => false,
        },
        Q::Condition(c) => c.is_vacuously_true(),
        Q::Compound {
            op: CompoundOp::And,
            parts,
        } => parts.iter().all(is_q_vacuously_true),
        Q::Negated(inner) => match inner.as_ref() {
            Q::Portable(p) => matches!(p.inner_ref(), sassi::BasicPredicate::False),
            Q::Compound {
                op: CompoundOp::Or,
                parts,
            } => parts.is_empty(),
            _ => false,
        },
        _ => false,
    }
}

impl<T: Model> QuerySet<T> {
    /// Construct an empty QuerySet — or, for proxy models, one seeded
    /// with the proxy's `#[model(default_filter, default_order)]` state.
    /// Prefer `T::objects()` at call sites — it is the idiomatic
    /// spelling and reads as "all objects of this model (before
    /// filtering)".
    ///
    /// # Proxy default-filter / default-order seeding (Phase 8β T3.4)
    ///
    /// Reads [`Model::default_filter_condition`] and
    /// [`Model::default_order_by`] at construction time. Non-proxy
    /// models inherit the default trait impls (returns `None` /
    /// `Vec::new()`) so the seeded queryset is structurally identical
    /// to the pre-T3.4 surface; rustc inlines the `None` / empty `Vec`
    /// returns and folds the seeding step away on the hot path.
    ///
    /// Proxy models override the trait methods via the macro, so the
    /// seeded queryset starts with the proxy's lowered SQL fragment
    /// already in `condition` and the proxy's ordering already in
    /// `ordering`. Subsequent `.filter(...)` calls AND-compose with
    /// the default (matching Django-style semantics — the proxy
    /// filter is the prefix no adopter call can drop), and `.order_by(...)`
    /// calls APPEND to the default ordering per the existing queryset
    /// convention (`queryset.rs` lines 25–28).
    #[must_use = "querysets are lazy — dropping one silently omits the query"]
    pub fn new() -> Self {
        // Proxy default filter (8β T3.4) → Q<T> (8γ Stage 2 substrate flip).
        // `T::default_filter_condition()` returns `Option<Condition>`; wrap
        // any returned condition in `Q::Condition(c)` so it round-trips
        // through the bridge with identical SQL. Non-proxy models return
        // `None` → `Q::always_true()` (same vacuous-truth as the pre-flip
        // `Condition::True` default).
        let condition = T::default_filter_condition().map_or_else(Q::always_true, |c| {
            use crate::query::q::Q;
            Q::Condition(c)
        });
        let ordering = T::default_order_by();
        QuerySet {
            condition,
            ordering,
            has_explicit_ordering: false,
            distinct: DistinctMode::None,
            limit: None,
            offset: None,
            is_empty: false,
            prefetch_paths: Vec::new(),
            select_related_paths: Vec::new(),
            lock: crate::query::lock::LockMode::None,
            // Cluster 8δ T7.3: opt-in. The default queryset has no
            // cache binding; `.cache(&p)` (defined in a separate
            // `impl<T: Model + sassi::Cacheable>` block) sets this
            // to `Some(_)`.
            cache_target: None,
            _model: PhantomData,
        }
    }

    /// Performs an `INNER JOIN LATERAL` against the provided `inner` queryset.
    ///
    /// Returns a [`LateralQuerySet`] that evaluates to `(L, R)` tuples.
    /// Parent rows (`L`) are dropped from the result if the `inner`
    /// queryset returns no rows for that parent.
    ///
    /// Inside `inner`, you can use `OuterRef::as_lateral_outer_expr()` to
    /// refer to columns from this outer queryset.
    pub fn join_lateral<R: Model>(
        self,
        inner: QuerySet<R>,
    ) -> crate::query::lateral::LateralQuerySet<T, R, crate::query::lateral::InnerLateral> {
        crate::query::lateral::LateralQuerySet {
            outer: self,
            inner,
            _mode: std::marker::PhantomData,
        }
    }

    /// Performs a `LEFT JOIN LATERAL` against the provided `inner` queryset.
    ///
    /// Returns a [`LateralQuerySet`] that evaluates to `(L, Option<R>)` tuples.
    /// Parent rows (`L`) are preserved with `None` if the `inner`
    /// queryset returns no rows for that parent.
    ///
    /// Inside `inner`, you can use `OuterRef::as_lateral_outer_expr()` to
    /// refer to columns from this outer queryset.
    pub fn left_join_lateral<R: Model>(
        self,
        inner: QuerySet<R>,
    ) -> crate::query::lateral::LateralQuerySet<T, R, crate::query::lateral::LeftLateral> {
        crate::query::lateral::LateralQuerySet {
            outer: self,
            inner,
            _mode: std::marker::PhantomData,
        }
    }

    /// Structural empty QuerySet — every terminal method (Task 6) short-
    /// circuits to the empty result without touching the database.
    ///
    /// Takes `self` as an instance transform (matching Django's
    /// `queryset.none()` ergonomics) so `Post::objects().none()` compiles
    /// and reads naturally. Any filters / ordering / limits already
    /// accumulated on `self` are discarded — the returned queryset is a
    /// fresh [`QuerySet::new()`] with `is_empty = true`. From-scratch
    /// construction is spelled `QuerySet::<T>::new().none()`.
    ///
    /// Useful for authorization / feature-flag branches:
    ///
    /// ```ignore
    /// let qs = if user.is_authenticated {
    ///     Post::objects().filter(|f| f.published.eq(true))
    /// } else {
    ///     Post::objects().none()
    /// };
    /// ```
    #[must_use = "querysets are lazy — dropping one silently omits the query"]
    pub fn none(self) -> Self {
        // `self` is intentionally ignored — `.none()` is a structural reset,
        // not a conjunction with existing filters. Returning a fresh empty-
        // flagged QuerySet keeps the semantics obvious: "no matter what was
        // chained before, this matches zero rows."
        let _ = self;
        let mut qs = Self::new();
        qs.is_empty = true;
        qs
    }

    /// Add a typed filter closure to the condition tree, AND-ed with whatever
    /// already accumulated. The closure receives a default-constructed
    /// `T::Fields` (a ZST) and returns any [`IntoQ`](crate::query::IntoQ)
    /// predicate.
    ///
    /// ```ignore
    /// Post::objects().filter(|f| f.published().eq(true))
    /// ```
    ///
    /// Ordinary generated root fields now return Djogi-owned portable
    /// predicates, so `f.col().eq(...)` / `icontains(...)` filters can pass the
    /// cache and refresh portability gates. PostgreSQL-specific predicates
    /// remain valid database filters through
    /// [`DjogiField::explicit_pg_predicate`](crate::query::field::DjogiField::explicit_pg_predicate)
    /// and are rejected by cache boundaries.
    ///
    /// Use [`filter_struct`](Self::filter_struct) for closure-free dynamic
    /// builders, macro-generated `{Model}Filter` values, or direct `Q<T>`
    /// composition outside a field closure. Legacy raw [`Condition`] values
    /// still lift into `Q::Condition(_)` for byte-for-byte SQL parity.
    #[must_use = "querysets are lazy — dropping one silently omits the query"]
    pub fn filter<F, P>(mut self, f: F) -> Self
    where
        F: FnOnce(T::Fields) -> P,
        P: crate::query::IntoQ<T>,
    {
        let added = f(T::Fields::default());
        self.condition = and_q_into_q(self.condition, added);
        self
    }

    /// AND an expression-IR predicate onto the condition tree.
    ///
    /// The closure receives a default-constructed `T::Fields` handle
    /// and must return an [`Expr<bool>`](crate::expr::Expr) — i.e. a
    /// comparison produced by the `eq` / `neq` / `gt` / `gte` / `lt` /
    /// `lte` methods on `Expr<T>`. The returned expression is wrapped
    /// in [`Condition::Expr`] and AND-ed onto `self.condition`; the
    /// SQL emitter walks the expression via
    /// [`crate::expr::sql::emit_expr`] instead of the Phase 2
    /// column-vs-literal leaf emitter.
    ///
    /// # When to reach for `filter_expr` over `filter`
    ///
    /// [`QuerySet::filter`] (Phase 2) accepts predicates where the RHS
    /// is always a literal — `f.balance.lt(100i64)`. `filter_expr`
    /// generalises both sides: either operand can be a column ref, a
    /// literal, or an arithmetic expression. Use it for:
    ///
    /// - Field-vs-field comparisons (`balance < overdraft_limit`).
    /// - Arithmetic predicates (`balance + pending_credit > 0`).
    /// - Predicates that build on [`crate::expr::Expr`] composition —
    ///   future tasks extend this surface with aggregates, subqueries,
    ///   and `CASE` (Phase 4 Tasks 4/5).
    ///
    /// The two methods compose: a queryset may have any mix of
    /// `filter` and `filter_expr` clauses, and every call is AND-ed
    /// onto the same tree.
    ///
    /// ```ignore
    /// use djogi::prelude::*;
    ///
    /// let overdrawn = Account::objects()
    ///     .filter_expr(|f| f.balance().as_expr().lt(f.overdraft_limit().as_expr()))
    ///     .fetch_all(&mut ctx).await?;
    /// ```
    #[must_use = "querysets are lazy — dropping one silently omits the query"]
    pub fn filter_expr<F>(mut self, f: F) -> Self
    where
        F: FnOnce(T::Fields) -> crate::expr::Expr<bool>,
    {
        let expr = f(T::Fields::default());
        // Phase 8eta PR2b: `IntoQ<T> for Expr<bool>` lifts to
        // `Q::Expression(_)` so this method shares the same generalised
        // composition surface as `filter`. Kept as a distinct method
        // for readability — the type signature documents the intent
        // (this filter is an expression-IR boolean).
        self.condition = and_q_into_q(self.condition, expr);
        self
    }

    /// Add a typed filter closure **negated** (wrapped in SQL `NOT`), AND-ed
    /// onto the existing tree. Equivalent to Django's `QuerySet.exclude()`.
    ///
    /// ```ignore
    /// Post::objects().exclude(|f| f.title().eq("draft".to_string()))
    /// ```
    ///
    /// See [`filter`](Self::filter) for the current portable-vs-SQL-only
    /// routing. Pure portable predicates are negated inside the trusted
    /// portable algebra so cache and refresh gates can still reduce them;
    /// SQL-only predicates are wrapped as `Q::Negated(...)`.
    #[must_use = "querysets are lazy — dropping one silently omits the query"]
    pub fn exclude<F, P>(mut self, f: F) -> Self
    where
        F: FnOnce(T::Fields) -> P,
        P: crate::query::IntoQ<T>,
    {
        let added = f(T::Fields::default()).into_q();
        // `exclude_struct`-style negation dispatch (Phase 8eta PR2b
        // PR2 Step 4): if the returned `Q<T>` is purely portable, push
        // the negation into the trusted `PortablePredicate<T>` (so
        // `!Portable(p)` rides Sassi's `Not` and double-negation
        // collapses in place). Otherwise wrap as `Q::Negated(...)`.
        // This preserves portability for portable predicates and keeps
        // the cache boundary's audit visibility intact.
        let negated = match added {
            Q::Portable(p) => Q::Portable(!p),
            other => !other,
        };
        self.condition = and_q_into_q(self.condition, negated);
        self
    }

    /// AND a [`Q<T>`] predicate onto the condition tree.
    ///
    /// Accepts any [`IntoQ<T>`] — `Q<T>` directly, a Djogi-owned
    /// [`PortablePredicate`](crate::query::PortablePredicate), an
    /// `Expr<bool>`, a legacy [`Condition`](crate::query::Condition), or a
    /// `{Model}Filter` programmatic builder (the macro emits an
    /// `IntoQ<#model>` impl alongside the existing
    /// [`ModelFilter`](crate::query::ModelFilter)). Raw
    /// `sassi::BasicPredicate<T>` is deliberately excluded: it can pair a
    /// forged SQL column name with an unrelated Rust extractor.
    ///
    /// Legacy `Condition` inputs still produce character-for-character SQL
    /// parity with the pre-Cluster-8γ `Condition` substrate. `{Model}Filter`
    /// inputs keep `FilterClause` as their single source of truth and lazily
    /// reconstruct portable Q leaves for conservative bool/string equality and
    /// membership clauses; unsupported fields, wrapped/optional shapes, value
    /// mismatches, and SQL-only operators fall back to `Q::Condition`.
    ///
    /// Empty `{Model}Filter` bodies short-circuit — no AND-ing, no vacuous
    /// `TRUE` sub-tree. Single portable clauses remain a single Q leaf; single
    /// fallback clauses remain a plain `Condition::Leaf` inside
    /// `Q::Condition(_)`, so SQL emission avoids redundant parentheses.
    ///
    /// This is the closure-free sibling of [`QuerySet::filter`]. Use this
    /// method from shell bindings, admin UIs, any dynamic assembler that can't
    /// write a `|f|` closure at compile time, and any new caller composing a
    /// `Q<T>` directly through the public algebra.
    ///
    /// ```ignore
    /// // ModelFilter — closure-free
    /// let filter = PostFilter::new()
    ///     .published(Lookup::Eq(true))
    ///     .view_count(Lookup::Gte(50i32));
    /// let rows = Post::objects().filter_struct(filter).fetch_all(&pool).await?;
    ///
    /// // Q<T> — public algebra
    /// let q: Q<Post> = Q::Ilike(Post::fields().title, "rust%".into());
    /// let rows = Post::objects().filter_struct(q).fetch_all(&pool).await?;
    /// ```
    #[must_use = "querysets are lazy — dropping one silently omits the query"]
    pub fn filter_struct<F: crate::query::IntoQ<T>>(mut self, filter: F) -> Self {
        // Phase 8eta PR2b: AND the incoming `Q<T>` directly without
        // round-tripping through `q_to_condition`. The vacuous-truth
        // short-circuit fires before `Q<T>::bitand` so an empty filter
        // (e.g. an unfiltered `{Model}Filter` body) does not pick up a
        // synthetic `TRUE AND ...` wrapper at the SQL emit time.
        let q = filter.into_q();
        if is_q_vacuously_true(&q) {
            return self;
        }
        self.condition = and_q_into_q(self.condition, q);
        self
    }

    /// AND the **negation** of a [`Q<T>`] predicate onto the condition
    /// tree. The struct-API counterpart of [`QuerySet::exclude`] —
    /// the closure-free version of `.exclude(|f| ...)`.
    ///
    /// Pure portable predicates are negated inside
    /// [`PortablePredicate`](crate::query::PortablePredicate), so Sassi can
    /// preserve Rust-side evaluability and simplify identities (`NOT TRUE`
    /// becomes false, `NOT FALSE` becomes true). Non-portable predicates are
    /// wrapped in `Q::Negated` and render as SQL `NOT (...)`.
    ///
    /// Unlike [`QuerySet::filter_struct`], an empty filter is **not**
    /// short-circuited — `NOT TRUE` is `FALSE`, and silently dropping it would
    /// produce a different result set.
    ///
    /// Sister method to [`QuerySet::filter_struct`]; the two compose
    /// freely:
    ///
    /// ```ignore
    /// Post::objects()
    ///     .filter_struct(PostFilter::new().published(Lookup::Eq(true)))
    ///     .exclude_struct(PostFilter::new().title(Lookup::Eq("draft".to_string())))
    /// ```
    #[must_use = "querysets are lazy — dropping one silently omits the query"]
    pub fn exclude_struct<F: crate::query::IntoQ<T>>(mut self, filter: F) -> Self {
        let q = filter.into_q();
        // Phase 8eta PR2b — negation dispatch:
        //
        // - Pure-portable `Q::Portable(p)`: push the negation into the
        //   trusted predicate so the stored shape stays
        //   `Q::Portable(!p)`. Sassi's `Not` reducer collapses double
        //   negation in place, which keeps cache-boundary portability
        //   gates from producing `Q::Negated(Q::Portable(_))` shells.
        // - Anything else: wrap as `Q::Negated(Box<Q<T>>)`. The SQL
        //   emitter renders this as `NOT (...)`; cache-boundary
        //   portability checks (PR4) can distinguish the two negations.
        //
        // No vacuous-truth short-circuit — `NOT TRUE` is `FALSE`, and
        // silently dropping it would change the result set.
        let negated = match q {
            Q::Portable(p) => Q::Portable(!p),
            other => !other,
        };
        self.condition = and_q_into_q(self.condition, negated);
        self
    }

    /// Append one or more ordering expressions. Later `order_by` calls
    /// **append** to the existing ordering rather than replacing it, matching
    /// Django semantics: library code can add a stable tiebreaker without
    /// clobbering the caller's primary ordering.
    ///
    /// The closure can return either a single `OrderExpr` or a
    /// `Vec<OrderExpr>` — the `Into<Vec<OrderExpr>>` bound bridges both.
    ///
    /// ```ignore
    /// Post::objects().order_by(|f| f.view_count.desc())
    /// Post::objects().order_by(|f| vec![f.published.desc(), f.title.asc()])
    /// ```
    #[must_use = "querysets are lazy — dropping one silently omits the query"]
    pub fn order_by<F, O>(mut self, f: F) -> Self
    where
        F: FnOnce(T::Fields) -> O,
        O: Into<Vec<OrderExpr>>,
    {
        self.has_explicit_ordering = true;
        let exprs: Vec<OrderExpr> = f(T::Fields::default()).into();
        // Django-style append: library code can add tiebreakers without
        // clobbering the caller's primary ordering. NOT SeaORM-style
        // replace — swapping this to `self.ordering = exprs;` silently
        // breaks any composition layer that relies on stable secondary
        // sort keys. If a replace semantic is ever needed, add a distinct
        // `reorder_by` method rather than mutating this one.
        self.ordering.extend(exprs);
        self
    }

    /// Reject SELECT-only read-tail modifiers on mutating terminals.
    ///
    /// Bulk UPDATE / DELETE currently emit only `WHERE` state; allowing
    /// `limit`, `offset`, `distinct`, row locks, explicit `order_by`,
    /// `prefetch`, `select_related`, or `cache_target` would silently
    /// ignore caller intent.
    pub(crate) fn validate_mutation_read_tail(
        &self,
        terminal: &str,
    ) -> Result<(), crate::DjogiError> {
        let mut rejected: Vec<&'static str> = Vec::new();
        if self.limit.is_some() {
            rejected.push("limit");
        }
        if self.offset.is_some() {
            rejected.push("offset");
        }
        if !matches!(self.distinct, DistinctMode::None) {
            rejected.push("distinct");
        }
        if self.lock != crate::query::lock::LockMode::None {
            rejected.push("row locks");
        }
        if self.has_explicit_ordering {
            rejected.push("order_by");
        }
        if !self.prefetch_paths.is_empty() {
            rejected.push("prefetch");
        }
        if !self.select_related_paths.is_empty() {
            rejected.push("select_related");
        }
        if self.cache_target.is_some() {
            rejected.push("cache_target");
        }
        if rejected.is_empty() {
            return Ok(());
        }
        let rejected = rejected.join(", ");
        Err(crate::DjogiError::Validation(format!(
            "{terminal}() does not support queryset read-tail modifiers ({rejected}); remove them before calling {terminal}()"
        )))
    }

    /// Apply SQL `LIMIT n`. Replaces any prior `limit` value.
    ///
    /// Takes `u64` at the API boundary so negative values are not
    /// representable — the builder can never be put into an invalid state.
    /// Internally stored as `Option<i64>` to match `tokio_postgres`'s BIGINT
    /// bind type; the cast is guarded by a `debug_assert!` so any pathological
    /// `n > i64::MAX` case (impossible at query scale in practice) trips
    /// in debug builds.
    #[must_use = "querysets are lazy — dropping one silently omits the query"]
    pub fn limit(mut self, n: u64) -> Self {
        debug_assert!(
            n <= i64::MAX as u64,
            "QuerySet::limit(n = {n}) overflows i64 — Postgres bind type is BIGINT"
        );
        self.limit = Some(n as i64);
        self
    }

    /// Apply SQL `OFFSET n`. Replaces any prior `offset` value.
    ///
    /// Takes `u64` for the same reason as [`QuerySet::limit`] — negative
    /// offsets are meaningless and now impossible to construct.
    #[must_use = "querysets are lazy — dropping one silently omits the query"]
    pub fn offset(mut self, n: u64) -> Self {
        debug_assert!(
            n <= i64::MAX as u64,
            "QuerySet::offset(n = {n}) overflows i64 — Postgres bind type is BIGINT"
        );
        self.offset = Some(n as i64);
        self
    }

    /// Append `FOR UPDATE` to the emitted SELECT — acquire an exclusive
    /// row-level lock on every selected row for the duration of the
    /// enclosing transaction.
    ///
    /// # Footgun — wrap in `atomic()`
    ///
    /// A `FOR UPDATE` lock is scoped to the active transaction. A
    /// pool-backed context auto-commits each statement, so
    /// `Post::objects().select_for_update().fetch_all(&mut pool_ctx)`
    /// acquires the lock and releases it the instant the implicit
    /// transaction closes — **no mutual exclusion** against a concurrent
    /// writer between the fetch and the subsequent `save`. Every
    /// correctness-sensitive use of `select_for_update` MUST sit inside
    /// an [`atomic()`](crate::transaction::atomic) scope.
    ///
    /// # Chaining with `nowait` / `skip_locked`
    ///
    /// Use [`QuerySet::nowait`] or [`QuerySet::skip_locked`] AFTER
    /// `select_for_update` to pick the contention behaviour. Calling
    /// either without first calling `select_for_update` promotes the
    /// lock to `FOR UPDATE NOWAIT` / `FOR UPDATE SKIP LOCKED`
    /// respectively — they imply the base lock.
    #[must_use = "querysets are lazy — dropping one silently omits the query"]
    pub fn select_for_update(mut self) -> Self {
        self.lock = crate::query::lock::LockMode::ForUpdate;
        self
    }

    /// Promote the SELECT lock to `FOR UPDATE NOWAIT` — acquire the
    /// lock if available, else return immediately with Postgres
    /// SQLSTATE `55P03` (`lock_not_available`), which terminals
    /// classify as [`DjogiError::LockConflict`](crate::DjogiError::LockConflict).
    ///
    /// Callable standalone (implies `select_for_update`). Combining
    /// with [`skip_locked`](QuerySet::skip_locked) — last call wins.
    #[must_use = "querysets are lazy — dropping one silently omits the query"]
    pub fn nowait(mut self) -> Self {
        self.lock = crate::query::lock::LockMode::ForUpdateNowait;
        self
    }

    /// Promote the SELECT lock to `FOR UPDATE SKIP LOCKED` — silently
    /// skip rows locked by another session and return only the
    /// unlocked rows.
    ///
    /// The idiomatic shape for work-queue consumers: multiple workers
    /// can pull jobs concurrently without blocking each other.
    /// Callable standalone (implies `select_for_update`). Combining
    /// with [`nowait`](QuerySet::nowait) — last call wins.
    #[must_use = "querysets are lazy — dropping one silently omits the query"]
    pub fn skip_locked(mut self) -> Self {
        self.lock = crate::query::lock::LockMode::ForUpdateSkipLocked;
        self
    }

    /// Append `FOR SHARE` to the emitted SELECT — acquire a shared
    /// (non-exclusive) row-level lock on every selected row for the
    /// duration of the enclosing transaction. Multiple concurrent
    /// readers may take the same `FOR SHARE` lock; writers (`UPDATE` /
    /// `DELETE`) block until the lock is released.
    ///
    /// Use this when several sessions need to read-and-decide against
    /// the same row without any of them intending to write
    /// (e.g., two services verifying an account balance before routing
    /// a transfer). For read-then-write inside the same transaction,
    /// reach for [`select_for_update`](QuerySet::select_for_update)
    /// instead — `FOR SHARE` does not protect against a peer reader
    /// upgrading to a writer.
    ///
    /// # Footgun — wrap in `atomic()`
    ///
    /// A `FOR SHARE` lock is scoped to the active transaction. A
    /// pool-backed context auto-commits each statement, so
    /// `Account::objects().select_for_share().fetch_one(&mut pool_ctx)`
    /// acquires the lock and releases it the instant the implicit
    /// transaction closes — **no mutual exclusion** against a
    /// concurrent writer between the fetch and the subsequent decide
    /// step. Every correctness-sensitive use of `select_for_share`
    /// MUST sit inside an [`atomic()`](crate::transaction::atomic)
    /// scope.
    ///
    /// # Chaining with contention variants
    ///
    /// `select_for_share` only sets the base FOR SHARE mode. To pick
    /// up `NOWAIT` / `SKIP LOCKED` semantics, call the dedicated
    /// FOR SHARE methods [`for_share_nowait`](QuerySet::for_share_nowait)
    /// or [`for_share_skip_locked`](QuerySet::for_share_skip_locked)
    /// directly. The historical [`nowait`](QuerySet::nowait) /
    /// [`skip_locked`](QuerySet::skip_locked) modifiers
    /// **unconditionally promote to the FOR UPDATE family** for
    /// backward compatibility — chaining them after `select_for_share`
    /// silently swaps the base lock back to FOR UPDATE, which is a
    /// footgun. Use the FOR SHARE-named modifiers when the base lock
    /// is FOR SHARE.
    ///
    /// djogi#104.
    #[must_use = "querysets are lazy — dropping one silently omits the query"]
    pub fn select_for_share(mut self) -> Self {
        self.lock = crate::query::lock::LockMode::ForShare;
        self
    }

    /// Set the SELECT lock to `FOR SHARE NOWAIT` — acquire a shared
    /// (non-exclusive) row lock if available, else return immediately
    /// with Postgres SQLSTATE `55P03` (`lock_not_available`), which
    /// terminals classify as
    /// [`DjogiError::LockConflict`](crate::DjogiError::LockConflict).
    ///
    /// Callable standalone (implies `select_for_share`). Combining
    /// with [`for_share_skip_locked`](QuerySet::for_share_skip_locked)
    /// — last call wins.
    ///
    /// See [`select_for_share`](QuerySet::select_for_share) for the
    /// pool-backed footgun and the rationale for keeping the FOR
    /// SHARE and FOR UPDATE contention modifiers on separate methods.
    /// djogi#104.
    #[must_use = "querysets are lazy — dropping one silently omits the query"]
    pub fn for_share_nowait(mut self) -> Self {
        self.lock = crate::query::lock::LockMode::ForShareNowait;
        self
    }

    /// Set the SELECT lock to `FOR SHARE SKIP LOCKED` — silently skip
    /// rows currently locked exclusively by another session and return
    /// only the unlocked rows under a shared lock.
    ///
    /// The FOR SHARE analogue of
    /// [`skip_locked`](QuerySet::skip_locked): useful for multi-reader
    /// scans that should make progress on the unlocked subset without
    /// blocking on rows another session is mid-write on.
    ///
    /// Callable standalone (implies `select_for_share`). Combining
    /// with [`for_share_nowait`](QuerySet::for_share_nowait) — last
    /// call wins.
    ///
    /// See [`select_for_share`](QuerySet::select_for_share) for the
    /// pool-backed footgun. djogi#104.
    #[must_use = "querysets are lazy — dropping one silently omits the query"]
    pub fn for_share_skip_locked(mut self) -> Self {
        self.lock = crate::query::lock::LockMode::ForShareSkipLocked;
        self
    }

    /// Switch to `SELECT DISTINCT ...`. Overrides any prior `distinct_on`.
    #[must_use = "querysets are lazy — dropping one silently omits the query"]
    pub fn distinct(mut self) -> Self {
        self.distinct = DistinctMode::Plain;
        self
    }

    /// Switch to Postgres' `SELECT DISTINCT ON (cols...) ...`. The closure
    /// returns either a single [`FieldRef`] or a tuple of up to six
    /// `FieldRef`s; column order matters because Postgres uses the first row
    /// per `(cols...)` tuple according to the query's `ORDER BY`.
    ///
    /// Overrides any prior `distinct`/`distinct_on`.
    #[must_use = "querysets are lazy — dropping one silently omits the query"]
    pub fn distinct_on<F, R>(mut self, f: F) -> Self
    where
        F: FnOnce(T::Fields) -> R,
        R: IntoDistinctColumns,
    {
        let cols = f(T::Fields::default()).into_distinct_columns();
        self.distinct = DistinctMode::On(cols);
        self
    }

    /// Register a single-hop prefetch against `path`. The target rows
    /// are materialised in a follow-up SQL query after the main
    /// [`fetch_all_prefetched`](crate::query::QuerySet::fetch_all_prefetched)
    /// executes; each main row is wrapped in a
    /// [`PrefetchedRow<T>`](crate::relation::PrefetchedRow) that exposes
    /// its resolved targets via
    /// [`PrefetchedRow::get`](crate::relation::PrefetchedRow::get).
    ///
    /// Calling `.prefetch(path)` twice with the same path is idempotent
    /// — the second registration is a no-op by `source_column` equality.
    /// This matches the natural expectation ("I asked for the same
    /// relation twice; please don't run two queries") and makes
    /// composition-site chaining (library code + caller both registering
    /// the same prefetch) free of surprises.
    ///
    /// # Why the bounds split across `Source::Pk` and `Target::Pk`
    ///
    /// Prefetch stitches via a `LEFT JOIN` keyed on `Source::Pk`; the
    /// `IN (...)` bind needs `Source::Pk: Encode + Type`, and the
    /// HashMap that routes targets back to parents needs it `Eq + Hash
    /// + Clone`. `Target::Pk` is not used for filtering — only for the
    /// NULL-probe in the stitching query, which goes through the raw-
    /// value path and does not require any `Target::Pk` bounds beyond
    /// what [`Model`] already guarantees.
    ///
    /// `Target` itself picks up `FromRow + Clone + Unpin` so the loader
    /// can decode the `t.*` columns and mint a per-parent owned copy;
    /// see the header comment on
    /// [`crate::relation::prefetch`] for the rationale behind the Clone
    /// bound (not on `Model` itself, just on the prefetch path).
    ///
    /// ```ignore
    /// let rows: Vec<PrefetchedRow<Vehicle>> = Vehicle::objects()
    ///     .filter(|f| f.make.eq("Toyota"))
    ///     .prefetch(VehicleRelated::owner())
    ///     .fetch_all_prefetched(&pool).await?;
    ///
    /// for row in &rows {
    ///     let owner: &Owner = row.get(VehicleRelated::owner()).unwrap();
    ///     println!("{} owned by {}", row.row.make, owner.name);
    /// }
    /// ```
    #[must_use = "querysets are lazy — dropping one silently omits the query"]
    pub fn prefetch<Target>(mut self, path: RelationPath<T, Target>) -> Self
    where
        T::Pk: postgres_types::ToSql
            + for<'r> postgres_types::FromSql<'r>
            + Eq
            + Hash
            + Clone
            + Send
            + Sync
            + 'static,
        Target: Model + crate::pg::decode::FromPgRow + Clone + Send + Unpin + 'static,
    {
        // Idempotent registration: if a prefetch for this source column
        // is already registered, don't append a duplicate. Duplicate
        // entries would each fire their own loader, costing an extra
        // round trip for no correctness gain — and would potentially
        // overwrite each other's stitched entries in a nondeterministic
        // order. The dedup key is `source_column` because the
        // `RelationPath` type parameters pin `Source`/`Target` at the
        // type level; two paths with the same source column pointing at
        // the same target are the same relation by construction.
        if self
            .prefetch_paths
            .iter()
            .any(|p| p.source_column == path.source_column())
        {
            return self;
        }
        self.prefetch_paths.push(ErasedPrefetch {
            source_column: path.source_column(),
            parent_table: T::table_name(),
            loader: prefetch_loader::<T, Target>,
        });
        self
    }

    /// Register a single-hop `select_related` against `path`. The target
    /// rows are materialised via a `LEFT JOIN` on the main query — no
    /// follow-up round trip, unlike
    /// [`prefetch`](QuerySet::prefetch). Each registered path is
    /// consumed by
    /// [`fetch_all_joined`](crate::query::QuerySet::fetch_all_joined),
    /// which returns `Vec<JoinedRow<T>>` exposing the joined target(s)
    /// via the same typed [`RelationPath`] the caller passed in.
    ///
    /// Calling `.select_related(path)` twice with the same path is
    /// idempotent — the second registration is a no-op by
    /// [`RelationPath::source_column`] equality, matching the
    /// dedup rule on `prefetch`. No `Vec` spam, no duplicate join in
    /// the emitted SQL.
    ///
    /// # Why the bounds on `Child`
    ///
    /// The `select_related` emitter aliases every child column in the
    /// `SELECT` list under a `rel_{source_column}.{col}` prefix. The
    /// [`FromJoinedPgRow`](crate::pg::decode::FromJoinedPgRow) bound on
    /// `Child` captures the decoder that reads those aliased columns
    /// back into a concrete `Child` instance — the macro-emitted
    /// sibling of `FromRow` that takes a prefix parameter. Without
    /// this bound the emitter would have no way to decode the
    /// child side of the join.
    ///
    /// `Child: Send + Sync + 'static` is the erasure contract — the
    /// decoded child is boxed as `Box<dyn Any + Send + Sync>` so it can
    /// share storage with heterogeneous child types on a single
    /// queryset (`.select_related(owner).select_related(fuel_type)`).
    ///
    /// # Multi-relation per queryset
    ///
    /// Multiple `.select_related(...)` calls on the same queryset stack —
    /// each produces its own `LEFT JOIN` with a `rel_{source_column}`
    /// alias. Aliases never collide because source columns are unique
    /// per parent model by construction. Multi-**hop** `select_related`
    /// (chained targets) is not supported: [`RelationPath`] only
    /// carries a single hop at the type level.
    ///
    /// ```ignore
    /// let rows: Vec<JoinedRow<Vehicle>> = Vehicle::objects()
    ///     .filter(|f| f.make.eq("Tesla"))
    ///     .select_related(VehicleRelated::owner())
    ///     .fetch_all_joined(&pool).await?;
    ///
    /// for row in &rows {
    ///     let owner: &Owner = row.get(VehicleRelated::owner()).unwrap();
    ///     println!("{} owned by {}", row.row.make, owner.name);
    /// }
    /// ```
    #[must_use = "querysets are lazy — dropping one silently omits the query"]
    pub fn select_related<Child>(mut self, path: RelationPath<T, Child>) -> Self
    where
        Child: Model + FromJoinedPgRow + Send + Sync + 'static,
    {
        // Idempotent registration: if a select_related for this
        // source column is already registered, don't append a
        // duplicate. Duplicate entries would emit two identical
        // `LEFT JOIN` clauses with the same alias — Postgres would
        // raise a "table name specified more than once" error on
        // execution, turning a silent repeat into a runtime failure
        // far from the call site. The dedup key is `source_column`
        // because the `RelationPath` type parameters pin `Source` /
        // `Child` at the type level; two paths with the same source
        // column pointing at the same child are the same relation by
        // construction.
        if self
            .select_related_paths
            .iter()
            .any(|p| p.source_column == path.source_column())
        {
            return self;
        }

        self.select_related_paths.push(ErasedSelectRelated {
            source_column: path.source_column(),
            child_table: path.target_table(),
            decoder: join_decoder::<Child>,
            // `child_descriptor::<Child>` coerces to a plain `fn` pointer
            // that the SELECT-list emitter uses to read every child
            // column name. Going through the descriptor (rather than a
            // pre-projected `Vec<&'static str>`) avoids an allocation
            // per `.select_related(...)` call and lets later phases
            // pull richer metadata off the same hook.
            child_descriptor: child_descriptor::<Child>,
        });
        self
    }

    /// Structural emptiness check — `true` only for querysets built via
    /// [`QuerySet::none`]. Used by Task 6's terminal methods to short-
    /// circuit the DB round-trip.
    ///
    /// `pub(crate)` because it is an implementation detail of the terminal
    /// methods, not user-facing API; users who need "does this queryset
    /// actually match rows?" should call `.exists()`, which also runs
    /// the real SQL.
    pub(crate) fn is_empty(&self) -> bool {
        self.is_empty
    }

    /// Group this queryset by one or more key columns, transitioning into
    /// [`crate::query::grouped::GroupedQuerySet<T, K>`].
    ///
    /// The closure receives a default-constructed `T::Fields` handle and must
    /// return one `FieldRef<T, V>` (arity 1) or a tuple of `FieldRef`s (arity
    /// 2..=4) — any type that implements
    /// [`crate::query::grouped::IntoGroupKeyTuple`].
    ///
    /// `GroupedQuerySet` has no terminals. Call `.annotate(...)` on the result
    /// to attach aggregate expressions and enter the terminal-bearing
    /// [`crate::query::grouped::GroupedAnnotatedQuerySet`] state. Premature
    /// `.fetch_all` on `GroupedQuerySet` is a compile error.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let rows: Vec<(i64, i64)> = Txn::objects()
    ///     .group_by(|f| f.org_id())
    ///     .annotate(|f| f.amount().sum())
    ///     .fetch_all(&mut ctx).await?;
    /// ```
    #[must_use = "grouped queries are lazy — dropping one silently omits the query"]
    pub fn group_by<F, K>(self, f: F) -> crate::query::grouped::GroupedQuerySet<T, K>
    where
        F: FnOnce(T::Fields) -> K,
        K: crate::query::grouped::IntoGroupKeyTuple,
    {
        let keys = f(T::Fields::default());
        crate::query::grouped::GroupedQuerySet {
            qs: self,
            keys,
            grouping: crate::query::grouped::GroupingMode::Plain,
            #[cfg(feature = "spatial")]
            spatial_source: None,
            _k: std::marker::PhantomData,
        }
    }

    /// Enter grouped state with ROLLUP semantics. Emits
    /// `GROUP BY ROLLUP (<keys>)` — Postgres expands this to include all
    /// hierarchical subtotals and the grand total in one pass.
    ///
    /// This is a convenience entry point equivalent to
    /// `.group_by(f)` followed by setting the grouping mode to
    /// `GroupingMode::Rollup`. Call `.annotate(...)` on the result to
    /// attach aggregate expressions.
    ///
    /// # Example
    ///
    /// ```ignore
    /// // Emits: GROUP BY ROLLUP (org_id)
    /// // Produces subtotals per org_id plus the grand total in one query.
    /// let rows = Txn::objects()
    ///     .rollup(|f| f.org_id())
    ///     .annotate(|f| f.amount().sum())
    ///     .fetch_all(&mut ctx).await?;
    /// ```
    #[must_use = "grouped queries are lazy — dropping one silently omits the query"]
    pub fn rollup<F, K>(self, f: F) -> crate::query::grouped::GroupedQuerySet<T, K>
    where
        F: FnOnce(T::Fields) -> K,
        K: crate::query::grouped::IntoGroupKeyTuple,
    {
        let mut gq = self.group_by(f);
        gq.grouping = crate::query::grouped::GroupingMode::Rollup;
        gq
    }

    /// Enter grouped state with CUBE semantics. Emits
    /// `GROUP BY CUBE (<keys>)` — Postgres expands this to all 2^n subsets
    /// of the key columns, covering every possible grouping combination.
    ///
    /// This is a convenience entry point equivalent to
    /// `.group_by(f)` followed by setting the grouping mode to
    /// `GroupingMode::Cube`. Call `.annotate(...)` on the result to
    /// attach aggregate expressions.
    ///
    /// # Example
    ///
    /// ```ignore
    /// // Emits: GROUP BY CUBE (org_id, region_id)
    /// // Produces subtotals for (org_id, region_id), (org_id), (region_id),
    /// // and the grand total — all four combinations.
    /// let rows = Txn::objects()
    ///     .cube(|f| (f.org_id(), f.region_id()))
    ///     .annotate(|f| f.amount().sum())
    ///     .fetch_all(&mut ctx).await?;
    /// ```
    #[must_use = "grouped queries are lazy — dropping one silently omits the query"]
    pub fn cube<F, K>(self, f: F) -> crate::query::grouped::GroupedQuerySet<T, K>
    where
        F: FnOnce(T::Fields) -> K,
        K: crate::query::grouped::IntoGroupKeyTuple,
    {
        let mut gq = self.group_by(f);
        gq.grouping = crate::query::grouped::GroupingMode::Cube;
        gq
    }

    /// Enter grouped state with GROUPING SETS semantics. Takes a closure
    /// that returns `[&'static str; N]` — each element becomes one
    /// single-column grouping set. Emits `GROUP BY GROUPING SETS ((col_a),
    /// (col_b), ...)`.
    ///
    /// The key type is `()` — there are no statically-typed key columns to
    /// decode because each row's "key" depends on which grouping set matched.
    /// Call `.annotate(...)` on the result to attach aggregate expressions;
    /// grouping-set column values are accessible via raw row access on the
    /// rows returned by `.fetch_all`.
    ///
    /// Arity-1 per set (one column per set). Multi-column sets are a
    /// future extension.
    ///
    /// # Example
    ///
    /// ```ignore
    /// // Emits: GROUP BY GROUPING SETS ((org_id), (region))
    /// // Each result row is grouped by exactly one of the listed columns.
    /// let rows = Txn::objects()
    ///     .group_by_sets(|_| ["org_id", "region"])
    ///     .annotate(|f| f.amount().sum())
    ///     .fetch_all(&mut ctx).await?;
    /// ```
    #[must_use = "grouped queries are lazy — dropping one silently omits the query"]
    pub fn group_by_sets<F, const N: usize>(
        self,
        f: F,
    ) -> crate::query::grouped::GroupedQuerySet<T, ()>
    where
        F: FnOnce(T::Fields) -> [&'static str; N],
    {
        let cols = f(T::Fields::default());
        let sets: Vec<Vec<&'static str>> = cols.iter().map(|c| vec![*c]).collect();
        crate::query::grouped::GroupedQuerySet {
            qs: self,
            keys: (),
            grouping: crate::query::grouped::GroupingMode::Sets(sets),
            #[cfg(feature = "spatial")]
            spatial_source: None,
            _k: std::marker::PhantomData,
        }
    }

    /// `GROUP BY GROUPING SETS (...)` — explicit multi-column sets,
    /// one tuple of columns per set. Cluster E T11.
    ///
    /// Accepts a closure that returns `Vec<Vec<&'static str>>` —
    /// outer Vec is the list of sets; inner Vec is the list of
    /// columns in each set. An empty inner Vec is the "grand total"
    /// set (no GROUP BY columns; one row aggregating all input).
    ///
    /// Adopters extract column names from `T::Fields` accessors via
    /// each `FieldRef`'s `.column()` method:
    ///
    /// ```ignore
    /// // Equivalent SQL:
    /// //   GROUP BY GROUPING SETS ((region, dept), (region), ())
    /// // Each result row is grouped by exactly one of the listed
    /// // tuples; the empty tuple yields the grand-total row.
    /// let rows = Sales::objects()
    ///     .grouping_sets(|f| vec![
    ///         vec![f.region().column(), f.dept().column()],
    ///         vec![f.region().column()],
    ///         vec![],
    ///     ])
    ///     .annotate(|f| f.amount().sum())
    ///     .fetch_all(&mut ctx).await?;
    /// ```
    ///
    /// Use [`Self::group_by_sets`] for the simpler arity-1-per-set
    /// shape (one column per set, no nested tuples). Use
    /// [`Self::rollup`] / [`Self::cube`] for hierarchical subtotal
    /// patterns.
    ///
    /// # Detecting subtotal rows
    ///
    /// Pair with [`crate::query::field::FieldRef::grouping`] (T10)
    /// inside `.annotate(...)` to flag which dimensions were rolled
    /// up in each result row:
    ///
    /// ```ignore
    /// .annotate(|f| (
    ///     f.amount().sum(),
    ///     f.region().grouping(),    // 1 if region rolled up, else 0
    ///     f.dept().grouping(),
    /// ))
    /// ```
    ///
    /// # Why `Vec<Vec<...>>` rather than typed tuple-of-tuples
    ///
    /// A typed signature like `qs.grouping_sets((set1), (set2), ...)`
    /// would need a `IntoGroupingSets` trait implemented for tuples
    /// of varying inner arity — not expressible in stable Rust without
    /// macros. The runtime `Vec<Vec<&'static str>>` shape preserves
    /// flexibility (sets of differing arities mixed freely) at the
    /// cost of one `vec![...]` allocation per call site. For the
    /// typical analytics-dashboard adopter who builds the sets once
    /// and runs the query repeatedly, the allocation is trivial.
    #[must_use = "grouped queries are lazy — dropping one silently omits the query"]
    pub fn grouping_sets<F>(self, f: F) -> crate::query::grouped::GroupedQuerySet<T, ()>
    where
        F: FnOnce(T::Fields) -> Vec<Vec<&'static str>>,
    {
        let sets = f(T::Fields::default());
        crate::query::grouped::GroupedQuerySet {
            qs: self,
            keys: (),
            grouping: crate::query::grouped::GroupingMode::Sets(sets),
            #[cfg(feature = "spatial")]
            spatial_source: None,
            _k: std::marker::PhantomData,
        }
    }

    // ── Tree-recursive transitions (Phase 8-Zero Cluster B2 — T9) ───────────
    //
    // `tree_descendants` / `tree_ancestors` consume the queryset and
    // return a [`RecursiveQuerySet<T>`], which has its own filter /
    // ordering / search-mode builders and its own terminals. The
    // accumulated `condition` / `ordering` / `limit` / etc. on `self`
    // are intentionally **discarded**: a recursive walk is anchored
    // by `id = $root`, and additional filters / orderings only make
    // sense when re-applied through `RecursiveQuerySet`'s builder
    // surface (where the emitter knows whether a predicate goes into
    // the recursive term's `WHERE` or the outer projection's `ORDER
    // BY`). The `let _ = self;` makes the discard explicit.

    /// Walk the self-FK chain downward — every row whose ancestor
    /// chain reaches `root_id`. Returns a typed [`RecursiveQuerySet<T>`]
    /// that carries `tree_descendants` semantics (recursive term:
    /// `child.<edge_col> = parent.id`).
    ///
    /// Works for **any** model `T` with at least one self-FK edge,
    /// without requiring `#[model(tree_edge = "...")]`. The caller
    /// supplies a typed [`RelationPath<T, T>`] picked from the
    /// macro-emitted `{T}Related::<edge>()` accessor — the type-level
    /// pinning means a `RelationPath<Vehicle, Vehicle>` cannot be
    /// passed where the queryset is over `Post`.
    ///
    /// For models that declare `#[model(tree_edge = "...")]`, prefer
    /// the inherent sugar [`Model::tree_descendants`] which resolves
    /// the column from the descriptor automatically.
    #[must_use = "querysets are lazy — dropping one silently omits the query"]
    pub fn tree_descendants(
        self,
        edge: crate::relation::RelationPath<T, T>,
        root_id: T::Pk,
    ) -> crate::query::recursive::RecursiveQuerySet<T>
    where
        T::Pk: postgres_types::ToSql + Sync + Send + 'static,
    {
        // Discard accumulated state — a recursive walk is anchored by
        // root_id and rebuilds its own filter / ordering pipeline.
        let _ = self;
        crate::query::recursive::RecursiveQuerySet::from_path(
            edge,
            root_id,
            crate::query::recursive::RecursiveDirection::Descendants,
        )
    }

    /// Walk the self-FK chain upward — every row reached by following
    /// the FK from `node_id` toward the root. Sibling of
    /// [`tree_descendants`](Self::tree_descendants) with the recursive
    /// term flipped to `parent.<edge_col> = child.id`.
    ///
    /// Same descriptor / typed-path requirements as `tree_descendants`;
    /// the inherent sugar [`Model::tree_ancestors`] is the
    /// `tree_edge`-declared shortcut.
    #[must_use = "querysets are lazy — dropping one silently omits the query"]
    pub fn tree_ancestors(
        self,
        edge: crate::relation::RelationPath<T, T>,
        node_id: T::Pk,
    ) -> crate::query::recursive::RecursiveQuerySet<T>
    where
        T::Pk: postgres_types::ToSql + Sync + Send + 'static,
    {
        let _ = self;
        crate::query::recursive::RecursiveQuerySet::from_path(
            edge,
            node_id,
            crate::query::recursive::RecursiveDirection::Ancestors,
        )
    }

    /// Spatial LEFT JOIN GROUP BY: group rows by which region of `R` contains
    /// them.
    ///
    /// Emits:
    ///
    /// ```sql
    /// SELECT r.<pk-col> AS rk0, <aggregates>
    /// FROM <t-table> AS t
    /// LEFT JOIN <r-table> AS r ON ST_Contains(r.<r-geo-col>, t.<t-geo-col>)
    /// GROUP BY r.<pk-col>
    /// ```
    ///
    /// The `LEFT JOIN` gives users the unassigned bucket —
    /// `RegionKey { region_pk: None }` — so rows that fall outside all known
    /// regions are visible in the result, not silently dropped (which an
    /// `INNER JOIN` would do).
    ///
    /// ## Runtime warning
    ///
    /// If `R` has no GiST index on its geography column, this method warns
    /// once per process via `tracing::warn!`. A spatial JOIN without a GiST
    /// index performs a full table scan on `R` for every row in `T`, scaling
    /// as O(|T| × |R|). Add `#[model(index = ...)]` on the region model's
    /// geography field or declare an `IndexSpec` with `IndexType::Gist`.
    ///
    /// ## Type parameters
    ///
    /// - `F` — closure that picks the geography column on `T`.
    /// - `G` — the concrete geography type (e.g. `GeoPoint`, `Polygon`).
    /// - `R` — the region model. Must have at least one `Geography`-typed field
    ///   in its descriptor.
    ///
    /// ## Panics
    ///
    /// Panics at call time if `R`'s descriptor contains no
    /// `FieldSqlType::Geography` field. This is a programming error (missing
    /// geo column on the region model), not a runtime condition, so a panic is
    /// appropriate — the same way an out-of-bounds slice index is.
    #[cfg(feature = "spatial")]
    #[must_use = "grouped queries are lazy — dropping one silently omits the query"]
    pub fn group_by_region<F, G, R, S>(
        self,
        field: F,
        _regions: QuerySet<R>,
    ) -> crate::query::grouped::GroupedQuerySet<T, crate::query::spatial_grouping::RegionKey<R>>
    where
        F: FnOnce(T::Fields) -> S,
        S: crate::query::field::IntoSqlField<T, G>,
        G: crate::geo::GeographyValue,
        R: Model,
        R::Pk: for<'a> postgres_types::FromSql<'a> + Send + Unpin + 'static,
    {
        // ── Missing-GiST warning ─────────────────────────────────────────────
        // Fire once per process via `std::sync::Once` so logs aren't flooded
        // even when the same queryset is constructed in a hot loop.
        if !R::descriptor().has_gist_on_geography() {
            static ONCE: std::sync::Once = std::sync::Once::new();
            ONCE.call_once(|| {
                tracing::warn!(
                    target: "djogi::spatial",
                    model = R::table_name(),
                    "group_by_region called against a region model with no GiST index on a \
                     geography column; spatial JOINs without GiST scale linearly in both \
                     table sizes — add IndexType::Gist on the region model's geography field \
                     or declare an IndexSpec with extension_dependency = Some(\"postgis\")"
                );
            });
        }

        // ── Identify the data-side geo column ────────────────────────────────
        // PR3: `IntoSqlField` accepts both legacy `FieldRef<T, G>` and the
        // post-flip root accessor return type `DjogiField<T, G>`. The trait
        // is sealed so downstream code cannot smuggle hand-rolled column
        // strings; both implementers forward the validated column metadata
        // produced by `__make_field_ref`. Spatial group keys are not
        // predicate boundaries — adopters keep `f.location()` direct
        // without an `.explicit_pg_predicate()` step.
        let t_geo_col = field(T::Fields::default()).into_sql_field().column();

        // ── Identify the region-side geo column ──────────────────────────────
        // Walk the region model's descriptor to find the first Geography-typed
        // field. Panics if none exists — that is a programming error (the
        // caller named a non-spatial model as the region model).
        let r_geo_col = R::descriptor()
            .fields
            .iter()
            .find(|f| {
                matches!(
                    f.sql_type,
                    crate::descriptor::FieldSqlType::Geography { .. }
                )
            })
            .map(|f| f.name)
            .expect(
                "region model R must have at least one Geography-typed field; \
                 add a GeoPoint / Polygon / … field before calling group_by_region",
            );

        // ── PK column for the region model ───────────────────────────────────
        let r_pk_col = R::descriptor()
            .pk_column()
            .expect("region model R must have a primary key");

        // ── Build the spatial join spec ───────────────────────────────────────
        let spec = crate::query::spatial_grouping::SpatialJoinSpec {
            t_geo_col,
            r_table: R::table_name(),
            r_geo_col,
            r_pk_col,
        };

        let keys = crate::query::spatial_grouping::RegionKey::<R> {
            region_pk: None,
            r_pk_col: Some(r_pk_col),
            _phantom: std::marker::PhantomData,
        };

        crate::query::grouped::GroupedQuerySet {
            qs: self,
            keys,
            grouping: crate::query::grouped::GroupingMode::Plain,
            spatial_source: Some(crate::query::grouped::SpatialGroupSource::Join(spec)),
            _k: std::marker::PhantomData,
        }
    }

    /// Sugar for `group_by_region(..).annotate(|_| id_field.count_star())`.
    ///
    /// Returns a `GroupedAnnotatedQuerySet` that counts rows per region
    /// (including an unassigned bucket for rows outside all regions).
    ///
    /// The count aggregate is `COUNT(*)` on the data table alias `t`.
    /// Callers who need a different aggregate (e.g. `SUM(amount)`) use
    /// `group_by_region` directly and call `.annotate` themselves.
    ///
    /// ## Type parameters
    ///
    /// Same bounds as [`QuerySet::group_by_region`].
    #[cfg(feature = "spatial")]
    #[must_use = "grouped queries are lazy — dropping one silently omits the query"]
    pub fn count_by_region<F, G, R, S>(
        self,
        field: F,
        regions: QuerySet<R>,
    ) -> crate::query::grouped::GroupedAnnotatedQuerySet<
        T,
        crate::query::spatial_grouping::RegionKey<R>,
        crate::expr::AggregateExpr<i64>,
    >
    where
        F: FnOnce(T::Fields) -> S,
        S: crate::query::field::IntoSqlField<T, G>,
        G: crate::geo::GeographyValue,
        R: Model,
        R::Pk: for<'a> postgres_types::FromSql<'a> + Send + Unpin + 'static,
    {
        // Build a `count_star` aggregate on the `id` column as the proxy
        // receiver — the aggregate emitter uses `COUNT(*)` regardless of
        // the column name (see `AggOp::CountStar` emitter in expr/sql.rs).
        self.group_by_region(field, regions)
            .annotate(|_| crate::query::field::FieldRef::<T, i64>::new("id").count_star())
    }

    /// DBSCAN density-based clustering: group nearby points into clusters
    /// without specifying the number of clusters in advance.
    ///
    /// Emits:
    ///
    /// ```sql
    /// SELECT ST_ClusterDBSCAN(t.<col>::geometry, $eps, $minpoints) OVER () AS cluster_id,
    ///        <aggregates>
    /// FROM <table> AS t
    /// [WHERE ...]
    /// GROUP BY cluster_id
    /// ```
    ///
    /// `cluster_id = NULL` for noise points (isolated rows with fewer than
    /// `minpoints` neighbours within `eps`). With the default `min_points = 1`
    /// every row is a core point of its own cluster, so noise never appears.
    ///
    /// # Filter ordering
    ///
    /// SQL evaluates `WHERE` before window functions, so `.filter(...)` calls
    /// chained *before* `cluster_by_proximity` prune points **from the DBSCAN
    /// input** — the clustering sees only the survivors, which can produce
    /// different cluster ids than clustering the full set and filtering
    /// after. Use `.having(...)` (evaluated after `GROUP BY cluster_id`) when
    /// you want to filter on aggregate output without changing which rows
    /// participate in the clustering.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let counts = Store::objects()
    ///     .cluster_by_proximity(|f| f.location(), ClusterRadius::meters(500.0).min_points(3))
    ///     .annotate(|f| f.id.count_star())
    ///     .fetch_all(&mut ctx).await?;
    /// ```
    ///
    /// # Type parameters
    ///
    /// - `F` — closure that resolves the geography column from `T::Fields`.
    /// - `G` — concrete geography type (e.g. `GeoPoint`, `Polygon`).
    #[cfg(feature = "spatial")]
    #[must_use = "grouped queries are lazy — dropping one silently omits the query"]
    pub fn cluster_by_proximity<F, G, S>(
        self,
        field: F,
        radius: crate::query::spatial_grouping::ClusterRadius,
    ) -> crate::query::grouped::GroupedQuerySet<T, crate::query::spatial_grouping::ClusterId>
    where
        F: FnOnce(T::Fields) -> S,
        S: crate::query::field::IntoSqlField<T, G>,
        G: crate::geo::GeographyValue,
    {
        // PR3: accept legacy `FieldRef<T, G>` and the post-flip root
        // accessor return type `DjogiField<T, G>`. Spatial cluster keys
        // are not predicate boundaries — `f.location()` stays direct
        // without an `.explicit_pg_predicate()` step.
        let t_geo_col = field(T::Fields::default()).into_sql_field().column();
        let spec = crate::query::spatial_grouping::ClusterSpec {
            t_geo_col,
            eps_degrees: radius.eps_degrees,
            minpoints: radius.minpoints,
        };
        crate::query::grouped::GroupedQuerySet {
            qs: self,
            keys: crate::query::spatial_grouping::ClusterId(None),
            grouping: crate::query::grouped::GroupingMode::Plain,
            spatial_source: Some(crate::query::grouped::SpatialGroupSource::Cluster(spec)),
            _k: std::marker::PhantomData,
        }
    }

    /// Geohash grid bucketing: assign each row to a spatial cell of the
    /// chosen [`GeohashPrecision`] and group by that cell.
    ///
    /// Emits:
    ///
    /// ```sql
    /// SELECT ST_GeoHash(t.<col>::geometry, $precision) AS geohash, <aggregates>
    /// FROM <table> AS t
    /// [WHERE ...]
    /// GROUP BY geohash
    /// ```
    ///
    /// Geohash strings are prefix-ordered: a `P5` key is a prefix of any
    /// `P6` key that falls in the same parent cell — coarser re-aggregation
    /// is possible via string truncation without re-querying. Nullable
    /// geography columns (`Option<G>`) bucket NULLs into `GeohashKey(None)`;
    /// non-nullable columns never produce `None`.
    ///
    /// # Filter ordering
    ///
    /// SQL evaluates `WHERE` before the `ST_GeoHash` projection, so
    /// `.filter(...)` calls chained *before* `bucket_by_cell` prune points
    /// **from the bucketing input** — only the survivors are assigned a
    /// geohash and aggregated. Use `.having(...)` (evaluated after
    /// `GROUP BY geohash`) when you want to filter on aggregate output
    /// without affecting which rows are bucketed.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let heatmap = Store::objects()
    ///     .bucket_by_cell(|f| f.location(), GeohashPrecision::P5)
    ///     .annotate(|f| f.id.count_star())
    ///     .fetch_all(&mut ctx).await?;
    /// ```
    ///
    /// # Type parameters
    ///
    /// - `F` — closure that resolves the geography column from `T::Fields`.
    /// - `G` — concrete geography type (e.g. `GeoPoint`).
    #[cfg(feature = "spatial")]
    #[must_use = "grouped queries are lazy — dropping one silently omits the query"]
    pub fn bucket_by_cell<F, G, S>(
        self,
        field: F,
        precision: crate::query::spatial_grouping::GeohashPrecision,
    ) -> crate::query::grouped::GroupedQuerySet<T, crate::query::spatial_grouping::GeohashKey>
    where
        F: FnOnce(T::Fields) -> S,
        S: crate::query::field::IntoSqlField<T, G>,
        G: crate::geo::GeographyValue,
    {
        // PR3: accept legacy `FieldRef<T, G>` and the post-flip root
        // accessor return type `DjogiField<T, G>`. Geohash bucket keys
        // are not predicate boundaries — `f.location()` stays direct
        // without an `.explicit_pg_predicate()` step.
        let t_geo_col = field(T::Fields::default()).into_sql_field().column();
        let spec = crate::query::spatial_grouping::GeohashSpec {
            t_geo_col,
            precision: precision.as_i32(),
        };
        crate::query::grouped::GroupedQuerySet {
            qs: self,
            keys: crate::query::spatial_grouping::GeohashKey(None),
            grouping: crate::query::grouped::GroupingMode::Plain,
            spatial_source: Some(crate::query::grouped::SpatialGroupSource::Geohash(spec)),
            _k: std::marker::PhantomData,
        }
    }
}

// Phase 8 §T2.3 — manual `.not_deleted()` helper for `SoftDeletable`
// models.
//
// **Spec lock (line 971, RESOLVED 2026-05-03, lens, locked):**
// automatic default-filter composition is deferred to Phase 8γ T6
// once the `Q<T>` substrate lands. T2.3 ships only the manual helper
// — adopters must call `.not_deleted()` explicitly on each
// `objects()` chain that should exclude soft-deleted rows. 8γ will
// replace this method with auto-composition under the new substrate.
//
// **Design notes:**
//
// 1. The bound is `M: crate::SoftDeletable` (re-exported through
//    `crate::compose`). The trait already implies `M: Model` via its
//    super-bound, so a separate `Model` bound is redundant.
//
// 2. The leaf is constructed by hand via [`Leaf::new`] (a
//    crate-internal constructor) rather than going through
//    [`FieldRef::is_null`]. Two reasons:
//
//    - The macro-generated `T::Fields` ZST does not expose a
//      `deleted_at()` accessor on every model — only on those whose
//      `#[model]` attribute injected the column. `SoftDeletable`-
//      deriving models declare the column themselves (Path B), which
//      means there's no compile-time guarantee that
//      `T::Fields::default().deleted_at` exists at the type level.
//    - The column name reads from `<M as SoftDeletable>::COLUMN`
//      (defaults to `"deleted_at"`; T2.6 added the trait const).
//      Reading via the trait surface lets a future column-override
//      path (e.g. `#[model(soft_deletable(column = "trashed_at"))]`)
//      flow through `.not_deleted()` automatically — the helper is
//      not a hard-coded literal anymore.
//
// 3. The `'static` bound on `M` mirrors the bounds present on the
//    other terminal-method impls below (`fetch_all`, `count`, etc.)
//    — every `T::Fields::default()` call site already requires it,
//    so adding the same bound here keeps the impl block coherent
//    when chained.
impl<M: crate::SoftDeletable + 'static> QuerySet<M> {
    /// Filter to rows where `deleted_at IS NULL` — the manual
    /// soft-delete exclusion helper.
    ///
    /// **Manual today; auto-composed in 8γ T6.** Phase 8α T2.6 ships
    /// this helper only; adopters who want soft-deleted rows excluded
    /// must call `.not_deleted()` on every `objects()` chain. Phase
    /// 8γ T6 will land automatic default-filter composition once the
    /// `Q<T>` substrate is in place — at which point this helper
    /// becomes redundant on the default code path. The method name
    /// will likely be retained as a no-op or as the explicit reverse
    /// of an `_insecurely()` bypass; see spec line 971 for the
    /// migration plan.
    ///
    /// ```ignore
    /// // Soft-deletable model with the attribute on `#[model]`:
    /// #[model(table = "posts", soft_deletable)]
    /// pub struct Post {
    ///     pub title: String,
    ///     pub deleted_at: Option<djogi::DateTime>,
    /// }
    ///
    /// // Exclude trashed rows explicitly:
    /// let live = Post::objects()
    ///     .not_deleted()
    ///     .fetch_all(&mut ctx)
    ///     .await?;
    /// ```
    #[must_use = "querysets are lazy — dropping one silently omits the query"]
    pub fn not_deleted(mut self) -> Self {
        // Construct the `<column> IS NULL` leaf directly. `Leaf::new`
        // is `pub(crate)`, so this is the canonical in-crate path — no
        // need to route through the typed `FieldRef::is_null` (which
        // would require the column to be exposed on the macro-emitted
        // `T::Fields` ZST and would also pin the column type at
        // compile time, defeating the convention-by-name model the
        // `SoftDeletable` trait uses for its getter).
        //
        // Phase 8α T2.6: read the column name through `<M as
        // SoftDeletable>::COLUMN` rather than a hard-coded `"deleted_at"`
        // string. The trait const defaults to `"deleted_at"` (canonical
        // case) but a future per-model rename can override the const
        // at the `impl` level — `.not_deleted()` picks up the override
        // automatically without changing this call site.
        let leaf = crate::query::condition::Leaf::new(
            <M as crate::SoftDeletable>::COLUMN,
            crate::query::condition::LookupOp::IsNull,
            crate::query::condition::FilterValue::Null,
        );
        // Phase 8eta PR2b: route through the new `and_q_into_q` helper.
        // `Condition` lifts to `Q::Condition(_)` via the sealed `IntoQ`
        // impl, preserving the legacy SQL emission shape this caller
        // depends on.
        self.condition = and_q_into_q(self.condition, Condition::Leaf(leaf));
        self
    }
}

// ── Cluster 8δ T7.3 — `.cache(&punnu)` opt-in modifier ─────────────────────
//
// Bound `T: Model + sassi::Cacheable` is split into a dedicated impl
// block for the same reason `not_deleted` lives in its own block: the
// extra trait bound is opt-in. Models that don't go through
// `#[derive(Model)]` (and therefore don't pick up T7.2's auto-emitted
// `Cacheable` impl) keep the existing `impl<T: Model> QuerySet<T>`
// surface unchanged — `.cache(...)` simply doesn't compile for them,
// matching the spec contract that the cache modifier is opt-in.
//
// Why `crate::types::Cacheable` (not `sassi::Cacheable`) for the
// bound: per `feedback_macro_path_routing.md`, code that names
// trait paths routes through `crate::types` so a future sassi
// reshuffle (e.g., moving `Cacheable` to a sub-module) only has to
// update one re-export instead of every `use sassi::Cacheable;`
// site in the framework. The trait identity is the same — the
// re-export at `djogi/src/types.rs` is `pub use sassi::cacheable::Cacheable;`
// — so the bound resolves byte-identically.
//
// Spec anchors: §664 (`.cache(&punnu)` modifier; opt-in).
// Phase 8 plan §374. Granular plan
// `cluster-8delta-granular.md` §3 commit T7.3.
impl<T: Model + crate::types::Cacheable + Clone> QuerySet<T> {
    /// Internal helper: stamp this QuerySet with a type-erased Punnu cache
    /// target so the next terminal method sends each materialised row
    /// through [`sassi::Punnu::insert`] before returning to the caller.
    ///
    /// `pub(crate)` because the public adopter surface is [`QuerySet::cache`]
    /// (Phase 8eta PR4), which gates entry through the trusted portable
    /// predicate reducer. `bind_cache` runs unconditionally with no gate, so
    /// only framework code that has already proven the queryset's predicate
    /// is portable (e.g. [`CachedPortableQuerySet`]'s terminal methods) may
    /// reach it.
    #[must_use = "querysets are lazy — dropping one silently omits the query"]
    pub(crate) fn bind_cache(mut self, punnu: sassi::Punnu<T>) -> Self {
        // Wrap in the type-erased `CacheTarget` handle so the
        // queryset's struct field doesn't need a `T: Cacheable`
        // bound. `Arc::new` here boxes the wrapper once at bind
        // time; later clones of the queryset reuse the same `Arc`.
        // `Punnu::clone` itself is `Arc`-cheap, so the embedded
        // clone inside `PunnuCacheTarget::new` is also cheap.
        self.cache_target = Some(std::sync::Arc::new(PunnuCacheTarget::new(punnu)));
        self
    }

    /// Test-only mirror of [`bind_cache`](Self::bind_cache) — stamps
    /// a `cache_target` directly onto the QuerySet without going
    /// through the [`CachedPortableQuerySet`] terminal indirection.
    ///
    /// # Why exposed under `feature = "testing"`
    ///
    /// The public adopter path to a cache-bound queryset is
    /// `.cache(&punnu)`, which returns a [`CachedPortableQuerySet`] —
    /// not a [`QuerySet`]. Adopters cannot, by design, construct a
    /// `QuerySet<T>` whose `cache_target` is `Some(_)` and then pass
    /// it to a set-op builder method like
    /// [`QuerySet::union`](crate::query::QuerySet::union). Defensive
    /// validation in
    /// [`crate::query::set_op::validate_arm`](crate::query::set_op)
    /// rejects such an arm if it is ever constructed (forward
    /// compatibility, in case a future API exposes
    /// `bind_cache`-shaped surface publicly).
    ///
    /// The Phase 8.5 Cluster 4B (#101) test suite needs a way to
    /// exercise that defensive branch from outside the crate. This
    /// helper is the supported test path: a `#[doc(hidden)]`,
    /// `feature = "testing"`-gated mirror that does exactly what
    /// `bind_cache` does. Adopters who do not opt into the `testing`
    /// feature never see it.
    ///
    /// # Not part of the stable API
    ///
    /// The `for_test` suffix and `#[doc(hidden)]` flag together
    /// communicate intent: this exists for in-tree fixture wiring, not
    /// adopter use. Production code should reach for the gated public
    /// path ([`cache`](Self::cache)) or, in this set-op context, build
    /// a cache-free arm before composing the set operation.
    #[cfg(any(test, feature = "testing"))]
    #[doc(hidden)]
    #[must_use = "querysets are lazy — dropping one silently omits the query"]
    pub fn bind_cache_for_test(self, punnu: sassi::Punnu<T>) -> Self {
        self.bind_cache(punnu)
    }

    /// Bind this QuerySet to a [`sassi::Punnu`] cache, gated by the
    /// trusted portable-predicate reducer.
    ///
    /// On success returns a [`CachedPortableQuerySet`] whose terminal methods
    /// (`fetch_all`, `fetch_one`, `first`, `count`) execute the underlying
    /// SQL and additionally feed every materialised row through
    /// [`sassi::Punnu::insert`] before handing the values back to the caller.
    /// Identity-map semantics + the configured [`sassi::OnConflict`] policy
    /// from sassi apply.
    ///
    /// On failure returns the original QuerySet alongside a typed
    /// [`PortablePredicateError`] — no terminal work or cache binding has
    /// taken place. SQL-only predicates (`Q::Condition`, `Q::Ilike`,
    /// `Q::JsonbPath`, `Q::Regex`, `Q::Expression`, `Q::Array`) are rejected;
    /// cache and refresh paths must be reducible to Sassi's Rust-evaluable
    /// algebra so cache reads can apply the same predicate in memory.
    /// [`QuerySet::none`] is treated as the portable false predicate and
    /// passes the gate even though its `is_empty` short-circuit means no
    /// row will ever be inserted.
    ///
    /// # Bounds
    ///
    /// `T: Clone` is required because the post-fetch hook runs after the
    /// terminal has materialised the `Vec<T>` for the caller — the cache
    /// target needs its own copy of each row to feed into `Punnu::insert`,
    /// while the caller still gets the original. Sassi's `Cacheable` does
    /// not include `Clone` as a supertrait, so the bound is added explicitly
    /// here. Every model that goes through `#[derive(Model)]` already has
    /// `#[derive(Clone)]` in the canonical recipe so this bound is satisfied
    /// by construction for every realistic adopter.
    ///
    /// # Why opt-in
    ///
    /// The cache hook is purely additive — SQL output, query plan, and the
    /// lifetime of the QuerySet are unchanged whether `.cache(...)` was
    /// called or not. Calling `.cache(&p)` records "this QuerySet feeds that
    /// Punnu instance"; not calling it preserves the uncached fetch
    /// behaviour exactly. Adopters who don't want a cache pay zero — neither
    /// in cycles nor in API surface.
    ///
    /// # Why `&Punnu<T>` (not `Punnu<T>`)
    ///
    /// `sassi::Punnu<T>` is `Arc`-internal — `Punnu::clone` clones a single
    /// `Arc<PunnuInner<T>>`. The builder takes the punnu by reference and
    /// clones internally so the call site reads as a binding ("feed THIS
    /// punnu") rather than a transfer ("hand over your punnu"). The cloned
    /// handle shares state with the caller's, matching the bind semantics
    /// the spec requires.
    ///
    /// # Errors propagation
    ///
    /// Per-row [`sassi::Punnu::insert`] errors (e.g.
    /// [`sassi::InsertError::Conflict`] under [`sassi::OnConflict::Reject`],
    /// or an L2-backend serialization failure) are logged via
    /// `tracing::warn!` and swallowed — they do not abort the fetch.
    /// Adopters who want to observe cache-side errors subscribe to
    /// [`sassi::Punnu::events`] (which fires per-insert events including
    /// conflict outcomes) — that is the designed observability surface for
    /// cache-pool lifecycle. Routing them through the fetch return type
    /// would conflate "Postgres said something went wrong" with "the cache
    /// mirror disagreed" and break the spec's contract that the cache
    /// modifier is purely additive.
    ///
    /// Predicate-portability errors travel through the `Result` return type,
    /// not through `tracing`, because they reflect a structural choice the
    /// adopter must repair (rewrite the filter using portable predicates) —
    /// retrying the same queryset cannot turn a SQL-only predicate into a
    /// portable one.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use djogi::cache::Punnu;
    /// use djogi::prelude::*;
    ///
    /// let pool: Punnu<Post> = Punnu::<Post>::builder().build();
    /// let recent = Post::objects()
    ///     .filter(|f| f.published().eq(true))
    ///     .order_by(|f| f.created_at().desc())
    ///     .limit(20)
    ///     .cache(&pool)?              // ← portable-gated cache binding
    ///     .fetch_all(&mut ctx)
    ///     .await?;
    /// // `pool.len() == recent.len()` — the 20 rows are now in
    /// // the bound Punnu's L1 identity map, ready for `pool.get(id)`.
    /// ```
    #[must_use = "querysets are lazy — dropping one silently omits the query"]
    // `clippy::result_large_err` is silenced because the `Err` variant returns
    // ownership of `self` to the caller alongside the typed portability error
    // — the recovery shape (retry with a portable filter, or fall through to
    // ordinary SQL execution) requires the original queryset. Boxing would
    // force an allocation on every error path without changing what the
    // caller has to do with it; the established codebase idiom (see
    // `migrate/segment.rs`, `migrate/projection.rs`) keeps the allow at the
    // function boundary.
    #[allow(clippy::result_large_err)]
    pub fn cache(
        self,
        punnu: &sassi::Punnu<T>,
    ) -> Result<CachedPortableQuerySet<'_, T>, (Self, PortablePredicateError)> {
        match self.try_portable() {
            Ok(portable) => Ok(portable.cache(punnu)),
            Err((queryset, err)) => Err((queryset, err)),
        }
    }
}

/// Private marker trait used to seal [`IntoDistinctColumns`].
///
/// Only types djogi itself blesses — `FieldRef` and tuples of
/// `FieldRef` up to arity 6 — implement `Sealed`, and because the
/// module is crate-private, downstream crates cannot add their own
/// impls. That closes the identifier-smuggling route a hostile
/// downstream would otherwise have via
/// `impl IntoDistinctColumns for MyStruct { fn into_distinct_columns(self) -> Vec<&'static str> { vec!["1; DROP TABLE ..."] } }`.
mod distinct_seal {
    pub trait Sealed {}
}

/// Bridge from closure return types to the `Vec<&'static str>` of column
/// names that [`QuerySet::distinct_on`] stores. Implemented for
/// [`FieldRef`] and tuples of `FieldRef`s up to arity 6.
///
/// Expanding beyond six requires adding another
/// `impl_into_distinct_columns_tuple!` invocation below — the ceiling is
/// deliberately low because `DISTINCT ON` with more than a handful of
/// columns is a design smell, not a capacity limit Djogi cares to lift.
///
/// The trait is sealed via [`distinct_seal::Sealed`] — downstream code
/// can name `IntoDistinctColumns` as a bound (so `distinct_on` callers
/// can pass the ZST tuples returned by macro-generated field
/// accessors) but cannot implement it for their own types. Every
/// column name that reaches `into_distinct_columns` therefore traces
/// back to a sealed `FieldRef` whose constructor ran through
/// [`crate::ident::assert_plain_ident`].
pub trait IntoDistinctColumns: distinct_seal::Sealed {
    /// Flatten the receiver into the ordered list of column names Postgres
    /// will dedupe on.
    fn into_distinct_columns(self) -> Vec<&'static str>;
}

impl<M: Model, V> distinct_seal::Sealed for FieldRef<M, V> {}
impl<M: Model, V> IntoDistinctColumns for FieldRef<M, V> {
    fn into_distinct_columns(self) -> Vec<&'static str> {
        vec![self.column()]
    }
}

// PR3: `DjogiField<M, V>` is the post-flip root accessor return type.
// `DISTINCT ON` is a SQL-only emission boundary (no Punnu evaluator),
// so the wrapper forwards its column metadata through the same sealed
// trait. The bound stays sealed because both implementers route through
// the validated `__make_field_ref` / `__make_djogi_field` constructors —
// downstream code cannot smuggle column strings through this interface.
impl<M: Model, V> distinct_seal::Sealed for crate::query::field::DjogiField<M, V> {}
impl<M: Model, V> IntoDistinctColumns for crate::query::field::DjogiField<M, V> {
    fn into_distinct_columns(self) -> Vec<&'static str> {
        vec![self.column()]
    }
}

/// Generate `IntoDistinctColumns` (plus the sealed marker) for a tuple
/// of `FieldRef`s. Each type parameter stands for the tuple slot's
/// value type `V` — the model type `M` is shared across every
/// `FieldRef` in the tuple because `distinct_on` only ever sees one
/// model's columns at a time.
macro_rules! impl_into_distinct_columns_tuple {
    ($($name:ident),+) => {
        impl<M: Model, $($name),+> distinct_seal::Sealed for ($(FieldRef<M, $name>,)+) {}
        impl<M: Model, $($name),+> IntoDistinctColumns for ($(FieldRef<M, $name>,)+) {
            fn into_distinct_columns(self) -> Vec<&'static str> {
                #[allow(non_snake_case)]
                let ($($name,)+) = self;
                vec![$($name.column()),+]
            }
        }
    };
}
impl_into_distinct_columns_tuple!(A);
impl_into_distinct_columns_tuple!(A, B);
impl_into_distinct_columns_tuple!(A, B, C);
impl_into_distinct_columns_tuple!(A, B, C, D);
impl_into_distinct_columns_tuple!(A, B, C, D, E);
impl_into_distinct_columns_tuple!(A, B, C, D, E, F);

/// Generate `IntoDistinctColumns` (plus the sealed marker) for a tuple
/// of `DjogiField`s. The tuple impls mirror the `FieldRef` set above so
/// post-PR3 root closures can return `(f.col_a(), f.col_b(), ...)`
/// directly without unwrapping each accessor through `__sql_field()`.
/// Identifier safety stays sealed by the same mechanism — every
/// `DjogiField` carries a column string the validator already accepted.
macro_rules! impl_into_distinct_columns_djogi_tuple {
    ($($name:ident),+) => {
        impl<M: Model, $($name),+> distinct_seal::Sealed
            for ($(crate::query::field::DjogiField<M, $name>,)+)
        {}
        impl<M: Model, $($name),+> IntoDistinctColumns
            for ($(crate::query::field::DjogiField<M, $name>,)+)
        {
            fn into_distinct_columns(self) -> Vec<&'static str> {
                #[allow(non_snake_case)]
                let ($($name,)+) = self;
                vec![$($name.column()),+]
            }
        }
    };
}
impl_into_distinct_columns_djogi_tuple!(A);
impl_into_distinct_columns_djogi_tuple!(A, B);
impl_into_distinct_columns_djogi_tuple!(A, B, C);
impl_into_distinct_columns_djogi_tuple!(A, B, C, D);
impl_into_distinct_columns_djogi_tuple!(A, B, C, D, E);
impl_into_distinct_columns_djogi_tuple!(A, B, C, D, E, F);

// ── Cluster 8δ T8.4 — into_basic_predicate: conservative Q<T> → BasicPredicate<T> ─────
//
// Placed on `impl<T: Model> QuerySet<T>` (the base block) because extraction
// is purely structural — it walks the Q<T> condition tree without any
// DeltaSyncCacheable behaviour. The T8.3 stub lived in the
// DeltaSyncCacheable-bounded block because it was a placeholder co-located
// with refresh_into. T8.4 moves the real implementation to the correct bound.
//
// Visibility: `pub` — adopters who want to inspect whether a QuerySet is
// reducible before calling refresh_into need to call this. It is not part of
// the everyday filter API (that is QuerySet::filter / filter_struct), but it
// is the intended entry point for advanced cache-integration code that needs
// to pass a BasicPredicate filter to sassi.
//
// Implementation note — Q::Condition is always Unreducible:
//   Legacy SQL-only payloads and macro-generated `{Model}Filter` inputs route
//   through Q::Condition(_) to preserve byte-for-byte SQL parity. A freshly
//   constructed QuerySet<T>::new() starts with Q::Portable(True). Ordinary
//   closure filters whose field accessors return `PortablePredicate<T>` now
//   preserve Q::Portable / Q::Compound / Q::Negated reducible forms, so PR4
//   cache and refresh gates accept them.
//
// Path-routing note (non-emitted code):
//   Per `feedback_macro_path_routing.md`, path-routing governs macro-EMITTED
//   code only. This impl block is non-emitted framework code; it may spell
//   `sassi::BasicPredicate` directly.

fn cache_invalid(kind: &'static str) -> PortablePredicateError {
    PortablePredicateError::CacheInvalidNode { kind }
}

fn try_reduce_q_ref_to_basic<T: crate::model::Model>(
    q: &Q<T>,
) -> Result<sassi::BasicPredicate<T>, PortablePredicateError> {
    match q {
        Q::Portable(p) => Ok(p.clone().into_inner()),
        Q::Compound { op, parts } => {
            let mut reduced_parts = Vec::with_capacity(parts.len());
            for part in parts {
                reduced_parts.push(try_reduce_q_ref_to_basic(part)?);
            }
            #[allow(unreachable_patterns)]
            match op {
                CompoundOp::And => Ok(sassi::BasicPredicate::And(reduced_parts)),
                CompoundOp::Or => Ok(sassi::BasicPredicate::Or(reduced_parts)),
                _ => Err(cache_invalid("Compound::<unknown>")),
            }
        }
        Q::Xor(left, right) => Ok(sassi::BasicPredicate::Xor(
            Box::new(try_reduce_q_ref_to_basic(left)?),
            Box::new(try_reduce_q_ref_to_basic(right)?),
        )),
        Q::Negated(inner) => Ok(sassi::BasicPredicate::Not(Box::new(
            try_reduce_q_ref_to_basic(inner)?,
        ))),
        Q::Condition(_) => Err(cache_invalid("Condition")),
        Q::Ilike(_, _) => Err(cache_invalid("Ilike")),
        Q::Regex(_, _, _) => Err(cache_invalid("Regex")),
        Q::Expression(_) => Err(cache_invalid("Expression")),
        Q::Array(_) => Err(cache_invalid("Array")),
        Q::JsonbPath(_) => Err(cache_invalid("JsonbPath")),
        #[allow(unreachable_patterns)]
        _ => Err(cache_invalid("Q::<unknown>")),
    }
}

fn validate_portable_sql_emit<T: crate::model::Model>(
    predicate: &sassi::BasicPredicate<T>,
) -> Result<(), PortablePredicateError> {
    let mut acc = crate::pg::accumulator::SqlAccumulator::new("");
    // The predicate was produced by `try_reduce_q_ref_to_basic`, which
    // only descends `Q::Portable` / `Q::Compound` / `Q::Xor` / `Q::Negated`.
    // Every Field leaf therefore came out of a `PortablePredicate<T>`
    // (the trusted-construction boundary); JSON leaves transited the
    // MirJzSON builder. Pass `JsonTrust::Trusted` so the walker does
    // not reject Djogi-built JSON predicates during the gate.
    crate::query::portable::emit_basic_predicate::<T>(
        &mut acc,
        predicate,
        crate::query::SqlEmitContext::root(),
        crate::query::portable::JsonTrust::Trusted,
    )
}

impl<T: crate::model::Model> QuerySet<T> {
    /// Validate that this queryset is safe to use as a Punnu cache boundary.
    ///
    /// Ordinary SQL-only predicates remain valid for database execution, but
    /// cache and refresh paths must be reducible to Sassi's Rust-evaluable
    /// predicate algebra. `QuerySet::none()` is treated as the portable
    /// false predicate rather than as an unfiltered query.
    pub fn is_portable(&self) -> Result<(), PortablePredicateError> {
        let portable_predicate = if self.is_empty {
            sassi::BasicPredicate::False
        } else {
            try_reduce_q_ref_to_basic(&self.condition)?
        };
        validate_portable_sql_emit::<T>(&portable_predicate)
    }

    /// Convert this queryset into the portable cache-boundary wrapper.
    ///
    /// On failure the original queryset is returned unchanged with the typed
    /// portability error; no terminal work or refresh task has started.
    // `clippy::result_large_err` allowed for the same reason as `cache`/
    // `refresh_into`: the `Err` variant returns ownership of `self` so the
    // caller can recover from a portability rejection. See the rationale on
    // [`QuerySet::cache`].
    #[allow(clippy::result_large_err)]
    pub fn try_portable(self) -> Result<PortableQuerySet<T>, (Self, PortablePredicateError)> {
        let portable_predicate = if self.is_empty {
            sassi::BasicPredicate::False
        } else {
            match try_reduce_q_ref_to_basic(&self.condition) {
                Ok(predicate) => predicate,
                Err(err) => return Err((self, err)),
            }
        };
        if let Err(err) = validate_portable_sql_emit::<T>(&portable_predicate) {
            return Err((self, err));
        }
        Ok(PortableQuerySet {
            inner: self,
            portable_predicate,
        })
    }

    /// Add a Djogi-trusted portable predicate closure to the query.
    ///
    /// This is the cache-safe sibling of [`QuerySet::filter`]: the closure
    /// must return a sealed [`IntoPortablePredicate`](crate::query::IntoPortablePredicate)
    /// value, so SQL-only `Condition`, `Expr<bool>`, and raw Sassi predicates
    /// cannot enter by accident.
    #[must_use = "querysets are lazy — dropping one silently omits the query"]
    pub fn portable_filter<F, P>(mut self, f: F) -> Self
    where
        F: FnOnce(T::Fields) -> P,
        P: crate::query::IntoPortablePredicate<T>,
    {
        let predicate = f(T::Fields::default()).into_portable_predicate();
        self.condition = and_q_into_q(self.condition, predicate);
        self
    }

    /// Attempt to extract a [`sassi::BasicPredicate<T>`] from this QuerySet's
    /// filter tree.
    ///
    /// Returns `Some(predicate)` when the entire `Q<T>` condition tree is
    /// reducible to a `BasicPredicate<T>`. Returns `None` and emits a
    /// `tracing::warn!` when any node is unreducible.
    ///
    /// # Reducible variants
    ///
    /// | Q variant | Reduces to |
    /// |---|---|
    /// | `Q::Portable(p)` | `p.into_inner()` — the inner [`sassi::BasicPredicate<T>`] is cloned out of the borrow so the walker does not require `T: Clone` |
    /// | `Q::Compound { And, all_reducible_parts }` | `BasicPredicate::And(parts)` |
    /// | `Q::Compound { Or, all_reducible_parts }` | `BasicPredicate::Or(parts)` |
    /// | `Q::Xor(left, right)` | `BasicPredicate::Xor(Box::new(reduced_left), Box::new(reduced_right))` |
    /// | `Q::Negated(reducible_inner)` | `BasicPredicate::Not(Box::new(reduced_inner))` — inner walked recursively, so `Q::Negated(Q::Compound{And, basics})` reduces too |
    /// | `QuerySet::none()` (`is_empty == true`) | `BasicPredicate::False` |
    ///
    /// # Unreducible variants (always → `None`)
    ///
    /// `Q::Ilike`, `Q::JsonbPath`, `Q::Regex`, `Q::Expression`,
    /// `Q::Array`, `Q::Condition`.
    ///
    /// `Q::Condition` covers SQL-only payloads, including generated
    /// `{Model}Filter` clauses that fall outside the conservative portable
    /// mapping. Direct `Q<T>`, `PortablePredicate<T>`, ordinary generated
    /// field-accessor closure filters, and portable generated filter clauses
    /// stay reducible; a fresh `QuerySet::new()` (no filters) starts as
    /// `Q::Portable(True)` and is reducible.
    ///
    /// # When to use this
    ///
    /// This is the inspection-style sibling of [`QuerySet::try_portable`].
    /// [`QuerySet::cache`] and [`QuerySet::refresh_into`] (Phase 8eta PR4)
    /// take the `Result`-returning route through `try_portable` so
    /// non-portable querysets are rejected with a typed error before any
    /// terminal work or refresh subscription begins. Reach for
    /// `into_basic_predicate` when you want to inspect a queryset's
    /// reducibility without consuming it through the cache/refresh boundary
    /// — for example in tests or framework-internal cache-integration code
    /// that already knows it owns the queryset.
    ///
    /// # Visibility note
    ///
    /// This is `pub` for testability and to give framework-internal cache-
    /// integration code a named entry point. It is **not** an inspection
    /// API: the method consumes `self`, so callers cannot "inspect first,
    /// then refresh_into" — the QuerySet is moved by either call.
    /// `QuerySet::clone()` preserves the reducible shape after Phase
    /// 8eta PR2b because `Q<T>` has a manual clone implementation that
    /// does not lower through `q_to_condition_ref`. A cloned portable
    /// queryset may therefore be inspected or refreshed without losing
    /// its trusted predicate structure.
    pub fn into_basic_predicate(self) -> Option<sassi::BasicPredicate<T>> {
        if self.is_empty {
            return Some(sassi::BasicPredicate::False);
        }
        match try_reduce_q_ref_to_basic(&self.condition) {
            Ok(predicate) => Some(predicate),
            Err(PortablePredicateError::CacheInvalidNode { kind }) => {
                tracing::warn!(
                    target: "djogi::cache",
                    model = std::any::type_name::<T>(),
                    reason = kind,
                    "QuerySet condition has non-portable predicates and cannot be \
                     reduced to a Sassi BasicPredicate. The cache and refresh paths \
                     reject non-portable querysets with a typed error; restructure \
                     the filter using only Djogi portable predicate operations to \
                     pass the cache/refresh boundary.",
                );
                None
            }
            Err(err) => {
                tracing::warn!(
                    target: "djogi::cache",
                    model = std::any::type_name::<T>(),
                    error = ?err,
                    "QuerySet condition could not be reduced to a portable cache predicate.",
                );
                None
            }
        }
    }
}

// ── Cluster 8δ T8.3 — delta-sync refresh subscription ────────────────────────
//
// `refresh_into` lives in its own impl block (separate from the base
// `impl<T: Model>` block and the `impl<T: Model + Cacheable + Clone>` cache
// block) because it requires the stricter combined bound
// `T: Model + DeltaSyncCacheable + Send + Sync + 'static`. Widening any
// existing block's bound would cascade to methods that have no need of
// `DeltaSyncCacheable`, breaking the clean opt-in layering.
//
// `into_basic_predicate` was previously stubbed here in T8.3 but has been
// moved to `impl<T: Model> QuerySet<T>` in T8.4 — extraction is purely
// structural (no DeltaSyncCacheable behaviour needed).
//
// Path-routing note (non-emitted code):
//   Per `feedback_macro_path_routing.md`, path-routing governs macro-EMITTED
//   code only. This impl block is non-emitted framework code; it may spell
//   `sassi::Punnu`, `sassi::DeltaRefreshHandle`, `crate::auth::AuthContext`,
//   `crate::pg::pool::DjogiPool`, and `sassi::BasicPredicate` directly.
impl<T> QuerySet<T>
where
    T: crate::model::Model
        + sassi::DeltaSyncCacheable
        + crate::pg::decode::FromPgRow
        + crate::cache::DjogiDeltaSyncMeta
        + Send
        + Sync
        + 'static,
    T::Watermark: tokio_postgres::types::ToSql + tokio_postgres::types::FromSqlOwned + Sync,
    T::Id: tokio_postgres::types::ToSql + Sync,
{
    /// Bind this QuerySet to a Punnu and start a delta-sync refresh subscription.
    ///
    /// On success, the fetcher owns a clone of the pool, the AuthContext by
    /// value, and the QuerySet's trusted BasicPredicate filter. SQL-only
    /// predicates return `Err((self, PortablePredicateError))` before any
    /// subscription starts. The fetcher NEVER captures `&mut DjogiContext`.
    ///
    /// # T8.5 — real SQL path
    ///
    /// The fetcher's `fetch_delta` body now issues real SQL on every tick.
    /// Each tick acquires a fresh connection from the pool, constructs a
    /// `DjogiContext` with the captured `AuthContext` (auth-locked-to-
    /// subscription per spec §677), and runs
    /// `SELECT <columns> FROM <table> WHERE <watermark_col> >= $1
    ///  [OR id IN ($2, …)] ORDER BY <watermark_col>` for delta ticks.
    /// Full baseline ticks with a portable filter push that filter into SQL.
    ///
    /// # T8.8 — refresh knobs (spec §674)
    ///
    /// The returned `DeltaRefreshHandle<T>` exposes two adopter-facing knobs
    /// from sassi's native API — no djogi-side wrappers required:
    ///
    /// - **`with_eviction_recovery(bool)`** — when enabled, LRU evictions of
    ///   IDs this subscription has observed are passed to the fetcher as
    ///   `DeltaQuery::recover_ids` on a later delta tick. Opt in via
    ///   `handle.with_eviction_recovery(true)`.
    ///
    /// - **`with_periodic_full_refresh(Option<NonZeroUsize>)`** — schedule
    ///   full (non-delta) refreshes every N ticks. `Some(n)` makes every nth
    ///   scheduled tick use `since = None`. Opt in via
    ///   `handle.with_periodic_full_refresh(NonZeroUsize::new(10))`.
    ///
    /// Both methods return `Self` for chaining:
    ///
    /// ```text
    /// let handle = MyModel::objects()
    ///     .refresh_into(&punnu, pool, auth)?
    ///     .with_eviction_recovery(true)
    ///     .with_periodic_full_refresh(NonZeroUsize::new(10));
    /// ```
    ///
    /// Additionally, the fetcher always monitors the Punnu event stream for
    /// LRU eviction events and emits a one-shot `tracing::warn!` on
    /// `djogi::cache` the first time an eviction is observed (spec §674
    /// Knob 1 — always-on, no adopter opt-in required).
    ///
    /// # Portable filter gate and full-baseline pushdown
    ///
    /// `refresh_into` first runs `try_portable()`. Reducible predicates are
    /// carried as `BasicPredicate<T>`; SQL-only `Q<T>` arms are rejected. The
    /// fetcher pushes non-trivial portable filters into SQL only on full
    /// baseline ticks (`since == None` and no eviction-recovery ids). Delta
    /// ticks ignore the filter at SQL time so changed rows that transitioned
    /// out of the predicate can still be upserted or tombstoned correctly.
    /// `QuerySet::none()` is preserved as a structural empty subscription:
    /// update ticks return empty deltas without querying the source table.
    ///
    /// # Interval placeholder
    ///
    /// The 30 s interval is a placeholder. T8.6 may add a builder for
    /// caller-supplied interval; see spec §672 review.
    // `clippy::result_large_err` is silenced for the same reason as
    // [`QuerySet::cache`]: the `Err` variant carries the original queryset
    // back to the caller for recovery. Boxing here would force allocations
    // on every rejection without changing the recovery shape.
    #[allow(clippy::result_large_err)]
    pub fn refresh_into(
        self,
        punnu: &sassi::Punnu<T>,
        pool: crate::pg::pool::DjogiPool,
        auth: crate::auth::AuthContext,
    ) -> Result<sassi::DeltaRefreshHandle<T>, (Self, PortablePredicateError)> {
        match self.try_portable() {
            Ok(portable) => Ok(portable.refresh_into(punnu, pool, auth)),
            Err((queryset, err)) => Err((queryset, err)),
        }
    }
}

impl<T> PortableQuerySet<T>
where
    T: crate::model::Model
        + sassi::DeltaSyncCacheable
        + crate::pg::decode::FromPgRow
        + crate::cache::DjogiDeltaSyncMeta
        + Send
        + Sync
        + 'static,
    T::Watermark: tokio_postgres::types::ToSql + tokio_postgres::types::FromSqlOwned + Sync,
    T::Id: tokio_postgres::types::ToSql + Sync,
{
    /// Construct a sassi delta-refresh subscription from a queryset whose
    /// portable-predicate gate has already passed.
    ///
    /// Infallible sibling of [`QuerySet::refresh_into`] — the public
    /// `QuerySet::refresh_into` runs `try_portable()` first and either
    /// delegates here on success or returns `Err((self, err))` without
    /// touching the Punnu. Adopters who already hold a [`PortableQuerySet`]
    /// (e.g. from an explicit `try_portable()` inspection) can call this
    /// directly to skip the redundant gate.
    pub fn refresh_into(
        self,
        punnu: &sassi::Punnu<T>,
        pool: crate::pg::pool::DjogiPool,
        auth: crate::auth::AuthContext,
    ) -> sassi::DeltaRefreshHandle<T> {
        // Trivially-true reductions (`BasicPredicate::True`) carry no
        // pushdown work — emitting `WHERE TRUE` on full-baseline ticks
        // would just bloat the SQL and slow planner work. Strip them
        // here so the fetcher's `Option<BasicPredicate<T>>` field
        // accurately means "non-trivial filter to push into SQL on
        // full-baseline ticks". A fresh `QuerySet<T>` initialises
        // `condition` as `Q::Portable(True)`, which the reducer hands
        // straight through as `BasicPredicate::True`.
        let PortableQuerySet {
            inner,
            portable_predicate,
        } = self;
        let empty = inner.is_empty;
        let filter = if empty {
            None
        } else {
            match portable_predicate {
                sassi::BasicPredicate::True => None,
                predicate => Some(predicate),
            }
        };
        // Capture the Punnu's event broadcast receiver before starting the
        // delta refresh. Each `refresh_into` call gets its own independent
        // receiver — per the `(Punnu, Subscription)` scope in spec §674 Knob 1.
        // Events emitted BEFORE this line (e.g., earlier `punnu.insert()` calls)
        // are not visible to this subscription's receiver; only events fired
        // from this point forward are observed. That is the correct contract:
        // the warn is meant to surface LRU pressure that occurs WHILE the
        // subscription is running.
        let events_rx = std::sync::Mutex::new(punnu.events());
        let fetcher = crate::query::refresh::DjogiDeltaFetcher::<T> {
            pool,
            auth,
            empty,
            filter,
            lru_warn_issued: std::sync::atomic::AtomicBool::new(false),
            events_rx,
            // Pattern 2 outbox-tombstone watermark — first tick on an
            // events-bearing model initialises it to wall-clock `now()`.
            outbox_watermark: std::sync::Mutex::new(None),
            _model: std::marker::PhantomData,
        };
        // [CHECK] Default 30s interval is a placeholder; T8.6 may add a
        // builder for caller-supplied interval. Pin via spec §672 review.
        let interval = std::time::Duration::from_secs(30);
        punnu.start_delta_refresh(interval, fetcher)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::descriptor::ModelDescriptor;
    use crate::pg::decode::{FromJoinedPgRow, FromPgRow};
    use crate::relation::{RelationKind, RelationPath};

    // Minimal `Model` impl for builder-shape tests. Mirrors the `Fake` model
    // used in `query::field`'s unit tests — keeps QuerySet builder tests in
    // this file independent of the `#[model]` macro expansion path.
    struct Fake;
    impl crate::model::__sealed::Sealed for Fake {}
    #[allow(clippy::manual_async_fn)]
    impl Model for Fake {
        type Pk = i64;
        type Fields = ();
        fn table_name() -> &'static str {
            "fake"
        }
        fn pk_value(&self) -> &Self::Pk {
            unreachable!("not called in QuerySet unit tests")
        }
        fn descriptor() -> &'static ModelDescriptor {
            unreachable!("not called in QuerySet unit tests")
        }
        fn get(
            _ctx: &mut crate::context::DjogiContext,
            _id: Self::Pk,
        ) -> impl std::future::Future<Output = Result<Self, crate::DjogiError>> + Send {
            async { unreachable!() }
        }
        fn create(
            _ctx: &mut crate::context::DjogiContext,
            _v: Self,
        ) -> impl std::future::Future<Output = Result<Self, crate::DjogiError>> + Send {
            async { unreachable!() }
        }
        fn save<'ctx>(
            &'ctx mut self,
            _ctx: &'ctx mut crate::context::DjogiContext,
        ) -> impl std::future::Future<Output = Result<(), crate::DjogiError>> + Send + 'ctx
        {
            async { unreachable!() }
        }
        fn delete(
            self,
            _ctx: &mut crate::context::DjogiContext,
        ) -> impl std::future::Future<Output = Result<(), crate::DjogiError>> + Send {
            async { unreachable!() }
        }
        fn refresh_from_db<'ctx>(
            &'ctx self,
            _ctx: &'ctx mut crate::context::DjogiContext,
        ) -> impl std::future::Future<Output = Result<Self, crate::DjogiError>> + Send + 'ctx
        {
            async { unreachable!() }
        }
    }

    // Cluster 8γ Stage 2 (T6.9): `qs.condition` is `Q<T>` post-flip.
    // Tests that pattern-match on the legacy `Condition` shape lower
    // through the bridge first. After Phase 8eta PR2b this bridge is a
    // compatibility oracle for legacy parity, not the production SQL
    // emitter's input.

    #[test]
    fn new_queryset_has_no_filters() {
        let qs: QuerySet<Fake> = QuerySet::new();
        assert!(matches!(
            crate::query::q::q_to_condition_ref(&qs.condition),
            Condition::True
        ));
        assert!(qs.ordering.is_empty());
        assert!(matches!(qs.distinct, DistinctMode::None));
        assert_eq!(qs.limit, None);
        assert_eq!(qs.offset, None);
        assert!(!qs.is_empty());
    }

    #[test]
    fn none_marks_queryset_empty() {
        // `none()` is an instance method: from-scratch construction goes
        // through `new().none()`, but `qs.none()` also works and is the
        // spelling documented at the module level.
        let qs: QuerySet<Fake> = QuerySet::<Fake>::new().none();
        assert!(qs.is_empty());
    }

    #[test]
    fn none_discards_prior_filters() {
        use crate::query::condition::{FilterValue, Leaf};
        // Any filters chained before `.none()` are structurally discarded —
        // the resulting queryset is always a clean empty-flagged state.
        let qs: QuerySet<Fake> = QuerySet::new()
            .filter(|_| Condition::Leaf(Leaf::eq_raw("a", FilterValue::Bool(true))))
            .none();
        assert!(qs.is_empty());
        assert!(matches!(
            crate::query::q::q_to_condition_ref(&qs.condition),
            Condition::True
        ));
    }

    #[test]
    fn filter_ands_onto_condition_tree() {
        use crate::query::condition::{FilterValue, Leaf};
        let qs: QuerySet<Fake> =
            QuerySet::new().filter(|_| Condition::Leaf(Leaf::eq_raw("a", FilterValue::Bool(true))));
        assert!(matches!(
            crate::query::q::q_to_condition_ref(&qs.condition),
            Condition::Leaf(_)
        ));
        // Second filter should AND with the first.
        let qs2 = qs.filter(|_| Condition::Leaf(Leaf::eq_raw("b", FilterValue::Bool(false))));
        match crate::query::q::q_to_condition(qs2.condition) {
            Condition::And(parts) => assert_eq!(parts.len(), 2),
            other => panic!("expected And, got {other:?}"),
        }
    }

    #[test]
    fn exclude_wraps_in_not() {
        use crate::query::condition::{FilterValue, Leaf};
        let qs: QuerySet<Fake> = QuerySet::new()
            .exclude(|_| Condition::Leaf(Leaf::eq_raw("a", FilterValue::Bool(true))));
        // True AND NOT(leaf) → NOT(leaf) thanks to Condition::and's identity folding.
        assert!(matches!(
            crate::query::q::q_to_condition(qs.condition),
            Condition::Not(_)
        ));
    }

    // ── T6.7 — `IntoQ<T>` + `filter_struct(Q<T>)` + `exclude_struct(Q<T>)` ────
    //
    // Locks the substrate-aware filter API: trusted `IntoQ<T>` impls
    // (Q<T> directly, PortablePredicate<T>, or `{Model}Filter`
    // through the macro-emitted bridge) compose at the `Q<T>` layer.
    // Tests that compare against `Condition` explicitly lower through
    // the legacy bridge as a parity oracle.

    /// `filter_struct` accepts `Q<T>` directly. Pure-Portable Q lowers
    /// through the bridge only in this test assertion; production
    /// filtering stores the original `Q<T>` shape.
    #[test]
    fn filter_struct_accepts_q_directly() {
        use crate::query::Q;

        let q: Q<Fake> = Q::always_true();
        let qs: QuerySet<Fake> = QuerySet::new().filter_struct(q);
        // True is vacuously-true → short-circuit returns self unchanged.
        assert!(matches!(
            crate::query::q::q_to_condition(qs.condition),
            Condition::True
        ));
    }

    /// `filter_struct` over a non-vacuous Q ANDs at the `Q<T>` layer.
    /// The assertion lowers afterward through the legacy bridge to pin
    /// parity with the old false shape.
    #[test]
    fn filter_struct_q_negated_ands_onto_tree() {
        use crate::query::Q;

        // Q::always_false() lowers to `Or(empty)` —
        // structurally non-vacuous, so it AND-s through.
        let q: Q<Fake> = Q::always_false();
        let qs: QuerySet<Fake> = QuerySet::new().filter_struct(q);
        match crate::query::q::q_to_condition(qs.condition) {
            Condition::Or(v) => assert!(v.is_empty(), "expected Or(empty)"),
            other => panic!("expected Condition::Or(empty), got {other:?}"),
        }
    }

    /// `exclude_struct` over a portable false predicate simplifies to true
    /// through Sassi. Locks the semantic contract for pure-portable
    /// predicates: negation stays portable rather than being forced through
    /// a `Condition::Not` shell.
    #[test]
    fn exclude_struct_false_simplifies_to_true() {
        use crate::query::Q;

        let q: Q<Fake> = Q::always_false();
        let qs: QuerySet<Fake> = QuerySet::new().exclude_struct(q);
        assert!(matches!(
            crate::query::q::q_to_condition(qs.condition),
            Condition::True
        ));
    }

    /// `exclude_struct` does **not** short-circuit on
    /// vacuously-true filters. `NOT TRUE` is `FALSE`; silently
    /// dropping it would change the result set.
    #[test]
    fn exclude_struct_does_not_short_circuit_on_vacuous_true() {
        use crate::query::Q;

        let q: Q<Fake> = Q::always_true();
        let qs: QuerySet<Fake> = QuerySet::new().exclude_struct(q);
        // True AND NOT(True) → False.
        match crate::query::q::q_to_condition(qs.condition) {
            Condition::Or(parts) => assert!(parts.is_empty(), "expected false"),
            other => panic!("expected Condition::Or(empty), got {other:?}"),
        }
    }

    /// `PortablePredicate<T>` lifts through `IntoQ<T>::into_q` so
    /// `.filter_struct(my_predicate)` reads naturally without naming
    /// `Q::Portable(_)` at the callsite.
    #[test]
    fn filter_struct_accepts_portable_predicate_directly() {
        use crate::query::PortablePredicate;

        let predicate: PortablePredicate<Fake> = PortablePredicate::always_false();
        let qs: QuerySet<Fake> = QuerySet::new().filter_struct(predicate);
        match crate::query::q::q_to_condition(qs.condition) {
            Condition::Or(v) => assert!(v.is_empty()),
            other => panic!("expected Condition::Or(empty), got {other:?}"),
        }
    }

    /// **T6.9 substrate-flip lock.** `QuerySet<T>::condition` is `Q<T>`
    /// post-flip, not `Condition`. Type-level assertion: a fresh
    /// queryset's condition must be a `Q<T>` shape and lower to
    /// `Condition::True` through the bridge.
    #[test]
    fn queryset_condition_field_is_q_t() {
        let qs: QuerySet<Fake> = QuerySet::new();
        // Type-level: `qs.condition` is `Q<Fake>`, not `Condition`.
        let _: &crate::query::Q<Fake> = &qs.condition;
        // Lowering round-trip: `Q::always_true()` lowers to `Condition::True`,
        // preserving SQL parity with the pre-flip queryset.
        let lowered = crate::query::q::q_to_condition(qs.condition);
        assert!(matches!(lowered, Condition::True));
    }

    #[test]
    fn limit_and_offset_round_trip() {
        let qs: QuerySet<Fake> = QuerySet::new().limit(10).offset(20);
        assert_eq!(qs.limit, Some(10));
        assert_eq!(qs.offset, Some(20));
    }

    #[test]
    fn distinct_plain_sets_mode() {
        let qs: QuerySet<Fake> = QuerySet::new().distinct();
        assert!(matches!(qs.distinct, DistinctMode::Plain));
    }

    #[test]
    fn clone_is_structural() {
        let qs: QuerySet<Fake> = QuerySet::new().limit(5);
        let qs2 = qs.clone();
        assert_eq!(qs.limit, qs2.limit);
    }

    // ── Phase 8β T3.4 — proxy default-filter / default-order seeding ─────
    //
    // A second hand-rolled `Model` impl that overrides the new trait
    // methods so the `QuerySet::new()` seeding path can be exercised
    // without requiring the proc macro. The tests below assert that:
    //
    // - A proxy-shaped model whose `default_filter_condition` returns
    //   `Some(...)` seeds the queryset's `condition` field with that
    //   value (not `Condition::True`).
    // - A proxy-shaped model whose `default_order_by` returns a
    //   non-empty `Vec<OrderExpr>` seeds the `ordering` field.
    // - User `.filter(...)` calls AND-compose with the seeded condition
    //   (the proxy filter is the prefix no adopter call can drop).
    // - User `.order_by(...)` calls APPEND to the seeded ordering
    //   (matches the existing queryset-level append convention).
    // - The non-proxy `Fake` model above remains structurally identical
    //   to its pre-T3.4 shape — no `RawSql` leakage when the trait
    //   default impls (`None` / `Vec::new()`) are used.

    /// A proxy-shaped model. The hand-rolled impl overrides
    /// `default_filter_condition` and `default_order_by`; everything
    /// else mirrors `Fake`'s `unreachable!()` body.
    #[derive(Clone)]
    struct FakeProxy;
    impl crate::model::__sealed::Sealed for FakeProxy {}
    #[allow(clippy::manual_async_fn)]
    impl Model for FakeProxy {
        type Pk = i64;
        type Fields = ();
        fn table_name() -> &'static str {
            "fake_proxy"
        }
        fn pk_value(&self) -> &Self::Pk {
            unreachable!("not called in QuerySet unit tests")
        }
        fn descriptor() -> &'static crate::descriptor::ModelDescriptor {
            unreachable!("not called in QuerySet unit tests")
        }
        fn default_filter_condition() -> Option<Condition> {
            // Mirrors the macro emission path — `Condition::__from_raw_sql_fragment`
            // is the constructor the macro uses for the lowered SQL
            // fragment. Using a static string here keeps the test self-
            // contained without dragging the macro-emission pipeline in.
            Some(Condition::__from_raw_sql_fragment("active = TRUE"))
        }
        fn default_order_by() -> Vec<crate::query::OrderExpr> {
            vec![crate::query::OrderExpr::__from_macro_column(
                "created_at",
                crate::query::Direction::Desc,
                crate::query::NullsOrder::Default,
            )]
        }
        fn get(
            _ctx: &mut crate::context::DjogiContext,
            _id: Self::Pk,
        ) -> impl std::future::Future<Output = Result<Self, crate::DjogiError>> + Send {
            async { unreachable!() }
        }
        fn create(
            _ctx: &mut crate::context::DjogiContext,
            _v: Self,
        ) -> impl std::future::Future<Output = Result<Self, crate::DjogiError>> + Send {
            async { unreachable!() }
        }
        fn save<'ctx>(
            &'ctx mut self,
            _ctx: &'ctx mut crate::context::DjogiContext,
        ) -> impl std::future::Future<Output = Result<(), crate::DjogiError>> + Send + 'ctx
        {
            async { unreachable!() }
        }
        fn delete(
            self,
            _ctx: &mut crate::context::DjogiContext,
        ) -> impl std::future::Future<Output = Result<(), crate::DjogiError>> + Send {
            async { unreachable!() }
        }
        fn refresh_from_db<'ctx>(
            &'ctx self,
            _ctx: &'ctx mut crate::context::DjogiContext,
        ) -> impl std::future::Future<Output = Result<Self, crate::DjogiError>> + Send + 'ctx
        {
            async { unreachable!() }
        }
    }

    impl crate::types::Cacheable for FakeProxy {
        type Id = i64;
        type Fields = ();

        fn id(&self) -> Self::Id {
            0
        }

        fn fields() -> Self::Fields {}
    }

    impl FromPgRow for FakeProxy {
        const COLUMNS: &'static [&'static str] = &[];
        const COLUMN_LIST: &'static str = "";

        fn from_pg_row(_row: &tokio_postgres::Row) -> Result<Self, crate::DjogiError> {
            unreachable!("not called in QuerySet unit tests")
        }
    }

    impl FromJoinedPgRow for FakeProxy {
        fn from_joined_pg_row(
            _row: &tokio_postgres::Row,
            _prefix: &str,
        ) -> Result<Self, crate::DjogiError> {
            unreachable!("not called in QuerySet unit tests")
        }
    }

    /// `QuerySet::new()` seeds `condition` from
    /// `Model::default_filter_condition` when the proxy override
    /// returns `Some(...)`.
    ///
    /// Cluster 8γ Stage 2 (T6.9): `qs.condition` is `Q<T>` — lower
    /// through the bridge to assert the legacy shape the SQL emitter
    /// actually sees.
    #[test]
    fn proxy_queryset_seeds_default_filter() {
        let qs: QuerySet<FakeProxy> = QuerySet::new();
        match crate::query::q::q_to_condition_ref(&qs.condition) {
            Condition::RawSql(s) => assert_eq!(s.as_str(), "active = TRUE"),
            other => panic!("expected RawSql variant, got {other:?}"),
        }
    }

    /// `QuerySet::new()` seeds `ordering` from
    /// `Model::default_order_by` when the proxy override returns a
    /// non-empty Vec.
    #[test]
    fn proxy_queryset_seeds_default_order() {
        let qs: QuerySet<FakeProxy> = QuerySet::new();
        assert_eq!(qs.ordering.len(), 1);
        match &qs.ordering[0] {
            crate::query::OrderExpr::Column {
                column, direction, ..
            } => {
                assert_eq!(*column, "created_at");
                assert!(matches!(direction, crate::query::Direction::Desc));
            }
            #[allow(unreachable_patterns)]
            other => panic!("expected Column variant, got {other:?}"),
        }
    }

    /// User `.filter(...)` AND-composes with the seeded default filter —
    /// the proxy condition stays as the prefix and the user's leaf is
    /// appended via the standard `Condition::and()` flatten path.
    ///
    /// Cluster 8γ Stage 2 (T6.9): `qs.condition` is `Q<T>` — lower
    /// through the bridge so the assertion still inspects the shape
    /// the SQL emitter renders.
    #[test]
    fn proxy_filter_ands_with_default() {
        use crate::query::condition::{FilterValue, Leaf};
        let qs: QuerySet<FakeProxy> = QuerySet::<FakeProxy>::new()
            .filter(|_| Condition::Leaf(Leaf::eq_raw("price", FilterValue::I64(100))));
        match crate::query::q::q_to_condition_ref(&qs.condition) {
            Condition::And(parts) => {
                assert_eq!(parts.len(), 2, "expected proxy filter AND user filter");
                assert!(matches!(parts[0], Condition::RawSql(_)));
                assert!(matches!(parts[1], Condition::Leaf(_)));
            }
            other => panic!("expected And, got {other:?}"),
        }
    }

    /// User `.order_by(...)` APPENDS to the seeded default ordering —
    /// the proxy ordering stays as the prefix and the user's expression
    /// is pushed onto the end. Matches the existing queryset convention
    /// (`queryset.rs:25-28`).
    #[test]
    fn proxy_order_by_appends_to_default() {
        let user_order = crate::query::OrderExpr::__from_macro_column(
            "id",
            crate::query::Direction::Asc,
            crate::query::NullsOrder::Default,
        );
        let qs: QuerySet<FakeProxy> = QuerySet::<FakeProxy>::new().order_by(|_| user_order);
        assert_eq!(qs.ordering.len(), 2);
        // Prefix: the seeded default.
        match &qs.ordering[0] {
            crate::query::OrderExpr::Column { column, .. } => assert_eq!(*column, "created_at"),
            #[allow(unreachable_patterns)]
            other => panic!("expected Column, got {other:?}"),
        }
        // Suffix: the user's `.order_by(...)`.
        match &qs.ordering[1] {
            crate::query::OrderExpr::Column { column, .. } => assert_eq!(*column, "id"),
            #[allow(unreachable_patterns)]
            other => panic!("expected Column, got {other:?}"),
        }
    }

    /// Mutation read-tail guard must allow model/proxy default ordering.
    /// Default ordering is seeded by `QuerySet::new()` and should not be
    /// treated as an explicit user-specified `order_by`.
    #[test]
    fn mutation_guard_allows_proxy_default_ordering() {
        let qs: QuerySet<FakeProxy> = QuerySet::new();
        assert!(
            qs.validate_mutation_read_tail("delete").is_ok(),
            "proxy default ordering must not trip mutation guard"
        );
    }

    /// Calling `.order_by(...)` marks ordering as explicit and must trip
    /// mutation guard, even when proxy default ordering is also present.
    #[test]
    fn mutation_guard_rejects_explicit_order_by() {
        let user_order = crate::query::OrderExpr::__from_macro_column(
            "id",
            crate::query::Direction::Asc,
            crate::query::NullsOrder::Default,
        );
        let qs: QuerySet<FakeProxy> = QuerySet::<FakeProxy>::new().order_by(|_| user_order);
        let err = qs
            .validate_mutation_read_tail("delete")
            .expect_err("explicit order_by should be rejected for mutation terminals");
        match err {
            crate::DjogiError::Validation(msg) => {
                assert!(
                    msg.contains("order_by"),
                    "validation should mention order_by, got: {msg}"
                );
            }
            other => panic!("expected Validation, got {other:?}"),
        }
    }

    #[test]
    fn mutation_guard_rejects_other_read_tail_modifiers() {
        for (qs, expected) in [
            (QuerySet::<Fake>::new().offset(10), "offset"),
            (QuerySet::<Fake>::new().distinct(), "distinct"),
            (QuerySet::<Fake>::new().select_for_update(), "row locks"),
        ] {
            let err = qs
                .validate_mutation_read_tail("delete")
                .expect_err("read-tail modifier should be rejected for mutation terminals");
            match err {
                crate::DjogiError::Validation(msg) => {
                    assert!(
                        msg.contains(expected),
                        "validation should mention {expected}, got: {msg}"
                    );
                }
                other => panic!("expected Validation, got {other:?}"),
            }
        }
    }

    fn fake_relation_path() -> RelationPath<Fake, FakeProxy> {
        RelationPath::new("proxy_id", "fake_proxy", RelationKind::ForeignKey)
    }

    #[test]
    fn mutation_guard_rejects_prefetch_paths() {
        let qs: QuerySet<Fake> = QuerySet::new().prefetch(fake_relation_path());
        let err = qs
            .validate_mutation_read_tail("delete")
            .expect_err("prefetch should be rejected for mutation terminals");
        match err {
            crate::DjogiError::Validation(msg) => {
                assert!(
                    msg.contains("prefetch"),
                    "validation should mention prefetch, got: {msg}"
                );
            }
            other => panic!("expected Validation, got {other:?}"),
        }
    }

    #[test]
    fn mutation_guard_rejects_select_related_paths() {
        let qs: QuerySet<Fake> = QuerySet::new().select_related(fake_relation_path());
        let err = qs
            .validate_mutation_read_tail("update")
            .expect_err("select_related should be rejected for mutation terminals");
        match err {
            crate::DjogiError::Validation(msg) => {
                assert!(
                    msg.contains("select_related"),
                    "validation should mention select_related, got: {msg}"
                );
            }
            other => panic!("expected Validation, got {other:?}"),
        }
    }

    #[test]
    fn mutation_guard_rejects_cache_target() {
        let punnu = crate::cache::Punnu::<FakeProxy>::builder().build();
        let qs: QuerySet<FakeProxy> = QuerySet::new().bind_cache_for_test(punnu);
        let err = qs
            .validate_mutation_read_tail("delete")
            .expect_err("cache_target should be rejected for mutation terminals");
        match err {
            crate::DjogiError::Validation(msg) => {
                assert!(
                    msg.contains("cache_target"),
                    "validation should mention cache_target, got: {msg}"
                );
            }
            other => panic!("expected Validation, got {other:?}"),
        }
    }

    /// The non-proxy `Fake` model is structurally unchanged by T3.4 —
    /// `default_filter_condition` returns `None` (default impl) and
    /// `default_order_by` returns the empty `Vec`, so the seeded queryset
    /// is identical to the pre-T3.4 shape (`Condition::True` + empty
    /// ordering).
    ///
    /// Cluster 8γ Stage 2 (T6.9): the substrate is now `Q<T>`. For
    /// `default_filter_condition() == None`, `QuerySet::new()` seeds
    /// `Q::always_true()` (== `Q::Portable(True)`), which
    /// the bridge lowers to the legacy `Condition::True` — preserving
    /// the pre-flip emission contract.
    #[test]
    fn non_proxy_queryset_unchanged_by_t3_4() {
        let qs: QuerySet<Fake> = QuerySet::new();
        assert!(matches!(
            crate::query::q::q_to_condition_ref(&qs.condition),
            Condition::True
        ));
        assert!(qs.ordering.is_empty());
    }

    // ── T11: group_by_region / count_by_region type-dispatch tests ────────────
    //
    // These tests confirm the entry-point signatures compile and return the
    // expected type shapes. The SQL emission shape is tested in sql.rs.

    // ── P1-2 once-warn counter test ───────────────────────────────────────────
    //
    // Verifies that calling `group_by_region` against an unindexed region model
    // emits at most one `tracing::warn!` regardless of how many times the method
    // is called. The guard uses `std::sync::Once` which is process-wide; the
    // counter is captured with `tracing_test::traced_test` scoped to this test's
    // thread-local subscriber.
    //
    // `ONCE` fires at most once per process run. `#[traced_test]` captures the
    // warn if it fires inside this test invocation and zero otherwise. The
    // assertion uses `<=` rather than `==` because test parallelism may cause
    // another unindexed call (in a different test) to consume the `Once` first —
    // the bound of at most one is still the invariant being checked.

    /// A region model with a geography field but NO GiST index — triggers the
    /// once-per-process warn path in `group_by_region`.
    #[cfg(feature = "spatial")]
    struct FakeUnindexedRegion;
    #[cfg(feature = "spatial")]
    impl crate::model::__sealed::Sealed for FakeUnindexedRegion {}
    #[cfg(feature = "spatial")]
    #[allow(clippy::manual_async_fn)]
    impl Model for FakeUnindexedRegion {
        type Pk = i64;
        type Fields = ();
        fn table_name() -> &'static str {
            "unindexed_regions"
        }
        fn pk_value(&self) -> &i64 {
            unreachable!()
        }
        fn descriptor() -> &'static ModelDescriptor {
            use crate::descriptor::{
                FieldDescriptor, FieldSqlType, GeographySubtype, PkType, field_descriptor,
                model_descriptor,
            };
            static FIELDS: &[FieldDescriptor] = &[FieldDescriptor {
                ..field_descriptor(
                    "boundary",
                    FieldSqlType::Geography {
                        subtype: GeographySubtype::Polygon,
                        srid: 4326,
                    },
                    false,
                )
            }];
            static DESC: ModelDescriptor = ModelDescriptor {
                ..model_descriptor(
                    "FakeUnindexedRegion",
                    "unindexed_regions",
                    PkType::HeerId,
                    FIELDS,
                )
            };
            &DESC
        }
        fn get(
            _ctx: &mut crate::context::DjogiContext,
            _id: i64,
        ) -> impl std::future::Future<Output = Result<Self, crate::DjogiError>> + Send {
            async { unreachable!() }
        }
        fn create(
            _ctx: &mut crate::context::DjogiContext,
            _v: Self,
        ) -> impl std::future::Future<Output = Result<Self, crate::DjogiError>> + Send {
            async { unreachable!() }
        }
        fn save<'ctx>(
            &'ctx mut self,
            _ctx: &'ctx mut crate::context::DjogiContext,
        ) -> impl std::future::Future<Output = Result<(), crate::DjogiError>> + Send + 'ctx
        {
            async { unreachable!() }
        }
        fn delete(
            self,
            _ctx: &mut crate::context::DjogiContext,
        ) -> impl std::future::Future<Output = Result<(), crate::DjogiError>> + Send {
            async { unreachable!() }
        }
        fn refresh_from_db<'ctx>(
            &'ctx self,
            _ctx: &'ctx mut crate::context::DjogiContext,
        ) -> impl std::future::Future<Output = Result<Self, crate::DjogiError>> + Send + 'ctx
        {
            async { unreachable!() }
        }
    }

    /// Verifies the once-warn guard: `group_by_region` must emit at most one
    /// `warn!` for an unindexed region model even when called multiple times.
    ///
    /// A custom `tracing::Subscriber` counts `WARN`-level events from the
    /// `djogi::spatial` target. The `std::sync::Once` guard is process-wide,
    /// so the warn fires in whichever test invocation happens first;
    /// the counter captures it if it fires inside this test. The assertion
    /// bounds the count to `<= 1`, which is the invariant regardless of
    /// test ordering.
    #[cfg(feature = "spatial")]
    #[test]
    fn group_by_region_warns_at_most_once_for_unindexed_region() {
        use crate::geo::GeoPoint;
        use crate::query::field::FieldRef;
        use std::sync::atomic::{AtomicUsize, Ordering};

        // Per-invocation counter — never accumulates across tests because
        // each test sees its own stack-allocated `AtomicUsize`.
        let warn_count = std::sync::Arc::new(AtomicUsize::new(0));

        // Minimal `Subscriber` that counts WARN events from "djogi::spatial".
        struct WarnCountSub {
            count: std::sync::Arc<AtomicUsize>,
        }
        impl tracing::Subscriber for WarnCountSub {
            fn enabled(&self, _: &tracing::Metadata<'_>) -> bool {
                true
            }
            fn new_span(&self, _: &tracing::span::Attributes<'_>) -> tracing::span::Id {
                tracing::span::Id::from_u64(1)
            }
            fn record(&self, _: &tracing::span::Id, _: &tracing::span::Record<'_>) {}
            fn record_follows_from(&self, _: &tracing::span::Id, _: &tracing::span::Id) {}
            fn event(&self, event: &tracing::Event<'_>) {
                if *event.metadata().level() == tracing::Level::WARN
                    && event.metadata().target() == "djogi::spatial"
                {
                    self.count.fetch_add(1, Ordering::Relaxed);
                }
            }
            fn enter(&self, _: &tracing::span::Id) {}
            fn exit(&self, _: &tracing::span::Id) {}
        }

        let subscriber = WarnCountSub {
            count: warn_count.clone(),
        };
        let _guard = tracing::subscriber::set_default(subscriber);

        // Call group_by_region twice — only the first call (in this process)
        // should fire the tracing::warn!. The second is a no-op via Once.
        let _g1 = QuerySet::<Fake>::new().group_by_region(
            |_| FieldRef::<Fake, GeoPoint>::new("location"),
            QuerySet::<FakeUnindexedRegion>::new(),
        );
        let _g2 = QuerySet::<Fake>::new().group_by_region(
            |_| FieldRef::<Fake, GeoPoint>::new("location"),
            QuerySet::<FakeUnindexedRegion>::new(),
        );

        // The Once guard ensures at most one warn fires across the whole process.
        // If this test runs first on the unindexed path: count == 1.
        // If another test fired the Once before us: count == 0.
        // Both satisfy the invariant "never > 1".
        let count = warn_count.load(Ordering::Relaxed);
        assert!(
            count <= 1,
            "expected at most 1 warn from group_by_region (Once guard), got {count}"
        );
    }

    /// A `FakeIndexedRegion` that returns a descriptor with a GiST index on a
    /// Geography field — `group_by_region` should NOT emit a warning.
    #[cfg(feature = "spatial")]
    struct FakeIndexedRegion;
    #[cfg(feature = "spatial")]
    impl crate::model::__sealed::Sealed for FakeIndexedRegion {}
    #[cfg(feature = "spatial")]
    #[allow(clippy::manual_async_fn)]
    impl Model for FakeIndexedRegion {
        type Pk = i64;
        type Fields = ();
        fn table_name() -> &'static str {
            "indexed_regions"
        }
        fn pk_value(&self) -> &i64 {
            unreachable!()
        }
        fn descriptor() -> &'static ModelDescriptor {
            use crate::descriptor::{
                FieldDescriptor, FieldSqlType, GeographySubtype, IndexColumnSpec, IndexKind,
                IndexSpec, IndexTarget, IndexType, PkType, field_descriptor, model_descriptor,
            };
            static FIELDS: &[FieldDescriptor] = &[FieldDescriptor {
                ..field_descriptor(
                    "boundary",
                    FieldSqlType::Geography {
                        subtype: GeographySubtype::Polygon,
                        srid: 4326,
                    },
                    false,
                )
            }];
            static BOUNDARY_COLS: &[IndexColumnSpec] = &[IndexColumnSpec::simple("boundary")];
            static INDEXES: &[IndexSpec] = &[IndexSpec {
                name: "idx_regions_boundary_gist",
                target: IndexTarget::Columns(BOUNDARY_COLS),
                kind: IndexKind::NonUnique,
                index_type: IndexType::Gist,
                predicate: None,
                include: &[],
                nulls_not_distinct: false,
                requires_out_of_transaction: true,
                extension_dependency: Some("postgis"),
            }];
            static DESC: ModelDescriptor = ModelDescriptor {
                indexes: INDEXES,
                ..model_descriptor(
                    "FakeIndexedRegion",
                    "indexed_regions",
                    PkType::HeerId,
                    FIELDS,
                )
            };
            &DESC
        }
        fn get(
            _ctx: &mut crate::context::DjogiContext,
            _id: i64,
        ) -> impl std::future::Future<Output = Result<Self, crate::DjogiError>> + Send {
            async { unreachable!() }
        }
        fn create(
            _ctx: &mut crate::context::DjogiContext,
            _v: Self,
        ) -> impl std::future::Future<Output = Result<Self, crate::DjogiError>> + Send {
            async { unreachable!() }
        }
        fn save<'ctx>(
            &'ctx mut self,
            _ctx: &'ctx mut crate::context::DjogiContext,
        ) -> impl std::future::Future<Output = Result<(), crate::DjogiError>> + Send + 'ctx
        {
            async { unreachable!() }
        }
        fn delete(
            self,
            _ctx: &mut crate::context::DjogiContext,
        ) -> impl std::future::Future<Output = Result<(), crate::DjogiError>> + Send {
            async { unreachable!() }
        }
        fn refresh_from_db<'ctx>(
            &'ctx self,
            _ctx: &'ctx mut crate::context::DjogiContext,
        ) -> impl std::future::Future<Output = Result<Self, crate::DjogiError>> + Send + 'ctx
        {
            async { unreachable!() }
        }
    }

    /// `group_by_region` returns a `GroupedQuerySet<T, RegionKey<R>>`.
    /// This compile-pass test verifies the type shape is as specified.
    #[cfg(feature = "spatial")]
    #[test]
    fn group_by_region_returns_grouped_queryset_with_region_key() {
        use crate::geo::GeoPoint;
        use crate::query::field::FieldRef;
        use crate::query::grouped::GroupedQuerySet;
        use crate::query::spatial_grouping::RegionKey;

        let qs: QuerySet<Fake> = QuerySet::new();
        let regions: QuerySet<FakeIndexedRegion> = QuerySet::new();
        let _grouped: GroupedQuerySet<Fake, RegionKey<FakeIndexedRegion>> =
            qs.group_by_region(|_| FieldRef::<Fake, GeoPoint>::new("location"), regions);
    }

    /// `count_by_region` returns a `GroupedAnnotatedQuerySet<T, RegionKey<R>, AggregateExpr<i64>>`.
    #[cfg(feature = "spatial")]
    #[test]
    fn count_by_region_returns_grouped_annotated_queryset() {
        use crate::expr::AggregateExpr;
        use crate::geo::GeoPoint;
        use crate::query::field::FieldRef;
        use crate::query::grouped::GroupedAnnotatedQuerySet;
        use crate::query::spatial_grouping::RegionKey;

        let qs: QuerySet<Fake> = QuerySet::new();
        let regions: QuerySet<FakeIndexedRegion> = QuerySet::new();
        let _gaq: GroupedAnnotatedQuerySet<Fake, RegionKey<FakeIndexedRegion>, AggregateExpr<i64>> =
            qs.count_by_region(|_| FieldRef::<Fake, GeoPoint>::new("location"), regions);
    }

    /// Calling `group_by_region` twice on the same model compiles — verifies
    /// that constructing two querysets does not conflict on the `Once`-based
    /// warn guard. The `std::sync::Once` is process-wide and non-resettable;
    /// the second call is a no-op without deadlock.
    #[cfg(feature = "spatial")]
    #[test]
    fn group_by_region_twice_does_not_deadlock() {
        use crate::geo::GeoPoint;
        use crate::query::field::FieldRef;

        let _g1 = QuerySet::<Fake>::new().group_by_region(
            |_| FieldRef::<Fake, GeoPoint>::new("location"),
            QuerySet::<FakeIndexedRegion>::new(),
        );
        let _g2 = QuerySet::<Fake>::new().group_by_region(
            |_| FieldRef::<Fake, GeoPoint>::new("location"),
            QuerySet::<FakeIndexedRegion>::new(),
        );
        // If we got here without a deadlock or panic, the Once guard is correct.
    }

    // ── T12: cluster_by_proximity / bucket_by_cell type-dispatch tests ────────

    /// `cluster_by_proximity` returns `GroupedQuerySet<T, ClusterId>`.
    #[cfg(feature = "spatial")]
    #[test]
    fn cluster_by_proximity_returns_grouped_queryset_with_cluster_id() {
        use crate::geo::GeoPoint;
        use crate::query::field::FieldRef;
        use crate::query::grouped::GroupedQuerySet;
        use crate::query::spatial_grouping::{ClusterId, ClusterRadius};
        // Type-level check: the return type must be `GroupedQuerySet<Fake, ClusterId>`.
        let _g: GroupedQuerySet<Fake, ClusterId> = QuerySet::<Fake>::new().cluster_by_proximity(
            |_| FieldRef::<Fake, GeoPoint>::new("location"),
            ClusterRadius::meters(500.0).min_points(3),
        );
    }

    /// `bucket_by_cell` returns `GroupedQuerySet<T, GeohashKey>`.
    #[cfg(feature = "spatial")]
    #[test]
    fn bucket_by_cell_returns_grouped_queryset_with_geohash_key() {
        use crate::geo::GeoPoint;
        use crate::query::field::FieldRef;
        use crate::query::grouped::GroupedQuerySet;
        use crate::query::spatial_grouping::{GeohashKey, GeohashPrecision};
        let _g: GroupedQuerySet<Fake, GeohashKey> = QuerySet::<Fake>::new().bucket_by_cell(
            |_| FieldRef::<Fake, GeoPoint>::new("location"),
            GeohashPrecision::P5,
        );
    }

    // ── T8.4 — into_basic_predicate: conservative Q<T>→BasicPredicate<T> walk ──
    //
    // These tests set `qs.condition` directly (via `pub(crate)` access) to
    // exercise every reducible and unreducible shape. The integration test
    // (`phase8_t8_4_basic_predicate_extraction.rs`) covers the externally
    // observable legacy-filter behavior (`Condition` path → None + warn,
    // unfiltered QuerySet → Some(True)).

    /// A fresh `QuerySet::new()` starts as `Q::Portable(True)`.
    /// `into_basic_predicate` must return `Some(BasicPredicate::True)`.
    #[test]
    fn into_basic_predicate_unfiltered_returns_true() {
        let qs: QuerySet<Fake> = QuerySet::new();
        // Verify the initial condition is portable before calling.
        assert!(
            matches!(&qs.condition, Q::Portable(_)),
            "unfiltered QuerySet must start as Q::Portable (substrate regression?)",
        );
        let result = qs.into_basic_predicate();
        assert!(
            matches!(result, Some(sassi::BasicPredicate::True)),
            "unfiltered QuerySet should reduce to Some(BasicPredicate::True)"
        );
    }

    /// A QuerySet with `condition = Q::Portable(False)` reduces
    /// to `Some(BasicPredicate::False)`. Verifies the `Q::Portable(p)` arm works
    /// for non-True sentinels.
    #[test]
    fn into_basic_predicate_portable_false_reduces() {
        let mut qs: QuerySet<Fake> = QuerySet::new();
        qs.condition = Q::always_false();
        let result = qs.into_basic_predicate();
        assert!(
            matches!(result, Some(sassi::BasicPredicate::False)),
            "Q::Portable(False) should reduce to Some(BasicPredicate::False)"
        );
    }

    /// A QuerySet with `Q::Compound { And, [Portable(True), Portable(False)] }`
    /// reduces to `Some(BasicPredicate::And(vec![True, False]))`.
    ///
    /// Verifies the Compound-And arm walks all parts and assembles the
    /// `BasicPredicate::And` aggregator.
    #[test]
    fn into_basic_predicate_compound_and_reduces() {
        use crate::query::q::CompoundOp;
        let mut qs: QuerySet<Fake> = QuerySet::new();
        qs.condition = Q::Compound {
            op: CompoundOp::And,
            parts: vec![Q::always_true(), Q::always_false()],
        };
        let result = qs.into_basic_predicate();
        match result {
            Some(sassi::BasicPredicate::And(parts)) => {
                assert_eq!(parts.len(), 2, "And predicate should have exactly 2 parts");
                assert!(matches!(parts[0], sassi::BasicPredicate::True));
                assert!(matches!(parts[1], sassi::BasicPredicate::False));
            }
            other => panic!(
                "expected Some(BasicPredicate::And([True, False])), got {}",
                if other.is_some() {
                    "Some(non-And variant)"
                } else {
                    "None"
                }
            ),
        }
    }

    /// A QuerySet with `Q::Compound { Or, [Portable(True), Portable(False)] }`
    /// reduces to `Some(BasicPredicate::Or(vec![True, False]))`.
    ///
    /// Verifies the Compound-Or arm.
    #[test]
    fn into_basic_predicate_compound_or_reduces() {
        use crate::query::q::CompoundOp;
        let mut qs: QuerySet<Fake> = QuerySet::new();
        qs.condition = Q::Compound {
            op: CompoundOp::Or,
            parts: vec![Q::always_true(), Q::always_false()],
        };
        let result = qs.into_basic_predicate();
        match result {
            Some(sassi::BasicPredicate::Or(parts)) => {
                assert_eq!(parts.len(), 2, "Or predicate should have exactly 2 parts");
            }
            _ => panic!("expected Some(BasicPredicate::Or([True, False]))"),
        }
    }

    /// `Q::Negated(Q::Portable(p))` reduces to `Some(BasicPredicate::Not(Box::new(p)))`.
    ///
    /// Verifies the Negated arm: pure-Basic inner wrapped in Not.
    #[test]
    fn into_basic_predicate_negated_portable_reduces() {
        let mut qs: QuerySet<Fake> = QuerySet::new();
        qs.condition = Q::Negated(Box::new(Q::always_true()));
        let result = qs.into_basic_predicate();
        match result {
            Some(sassi::BasicPredicate::Not(inner)) => {
                assert!(
                    matches!(*inner, sassi::BasicPredicate::True),
                    "inner of Not should be True"
                );
            }
            _ => panic!("expected Some(BasicPredicate::Not(True))"),
        }
    }

    /// `Q::Compound { And, [Portable(True), Ilike(...)] }` is Unreducible — the
    /// Ilike part is SQL-only. Returns `None`. Verifies short-circuit on
    /// first unreducible part.
    #[test]
    fn into_basic_predicate_compound_with_ilike_refuses() {
        use crate::query::field::FieldRef;
        use crate::query::q::CompoundOp;
        let mut qs: QuerySet<Fake> = QuerySet::new();
        qs.condition = Q::Compound {
            op: CompoundOp::And,
            parts: vec![
                Q::always_true(),
                // ILIKE is SQL-only — cannot be expressed as BasicPredicate.
                Q::Ilike(FieldRef::<Fake, String>::new("label"), "foo%".to_string()),
            ],
        };
        let result = qs.into_basic_predicate();
        assert!(
            result.is_none(),
            "Q::Compound containing Q::Ilike must reduce to None"
        );
    }

    /// `Q::Condition(...)` is always Unreducible. Ordinary
    /// `.filter(|f| f.col().eq(...))` closures and portable generated
    /// `{Model}Filter` clauses produce `Q::Portable`, but SQL-only generated
    /// filter clauses and any closure that hand-rolls a raw `Condition` still
    /// route through `Q::Condition(_)`. This test pins that SQL-only escape
    /// hatch.
    #[test]
    fn into_basic_predicate_legacy_condition_refuses() {
        use crate::query::condition::{FilterValue, Leaf};
        let qs: QuerySet<Fake> =
            QuerySet::new().filter(|_| Condition::Leaf(Leaf::eq_raw("a", FilterValue::Bool(true))));
        // The closure returns a raw `Condition`, which the legacy
        // `IntoQ for Condition` shim wraps as `Q::Condition(...)`.
        assert!(
            matches!(&qs.condition, Q::Condition(_)),
            "raw Condition payloads must reach the queryset as Q::Condition (regression in and_condition_into_q?)",
        );
        let result = qs.into_basic_predicate();
        assert!(
            result.is_none(),
            "Q::Condition (legacy filter path) must reduce to None"
        );
    }

    /// `Q::Negated(Q::Condition(...))` is Unreducible — the inner is not Basic.
    /// Returns `None`. Verifies that Negated propagates Unreducible from inner.
    #[test]
    fn into_basic_predicate_negated_non_basic_refuses() {
        use crate::query::condition::{FilterValue, Leaf};
        let inner_condition = Condition::Leaf(Leaf::eq_raw("x", FilterValue::Bool(false)));
        let mut qs: QuerySet<Fake> = QuerySet::new();
        qs.condition = Q::Negated(Box::new(Q::Condition(inner_condition)));
        let result = qs.into_basic_predicate();
        assert!(
            result.is_none(),
            "Q::Negated(Q::Condition(...)) must reduce to None"
        );
    }

    /// `Q::Xor(Q::Portable(True), Q::Portable(False))` reduces to
    /// `BasicPredicate::Xor(...)`.
    #[test]
    fn into_basic_predicate_xor_reduces() {
        let mut qs: QuerySet<Fake> = QuerySet::new();
        qs.condition = Q::Xor(Box::new(Q::always_true()), Box::new(Q::always_false()));
        let result = qs.into_basic_predicate();
        match result {
            Some(sassi::BasicPredicate::Xor(left, right)) => {
                assert!(matches!(*left, sassi::BasicPredicate::True));
                assert!(matches!(*right, sassi::BasicPredicate::False));
            }
            _ => panic!("expected Some(BasicPredicate::Xor(True, False))"),
        }
    }

    // ── PR4 — portable cache/refresh gate API ─────────────────────────────────
    //
    // These tests pin the new public-surface invariants introduced when
    // `QuerySet::cache` and `QuerySet::refresh_into` flipped to `Result`-
    // returning gates. The integration test `phase8eta_pr4_cache_refresh_gate`
    // covers the database-backed behavior; these unit tests cover the
    // structural reduction without a DB round-trip so regressions surface
    // even when the integration suite is skipped.

    /// `QuerySet::none()` flips `is_empty = true`. Both `try_portable` and
    /// `is_portable` must accept it as a portable-false predicate so adopters
    /// can short-circuit authorization branches through `.none().cache(...)`.
    #[test]
    fn try_portable_accepts_none_as_portable_false() {
        let qs: QuerySet<Fake> = QuerySet::new().none();
        assert!(qs.is_portable().is_ok());
        let portable = qs.try_portable().expect("none() must satisfy try_portable");
        assert!(matches!(portable.predicate(), sassi::BasicPredicate::False));
        // `into_query_set` round-trips back to a queryset that still
        // short-circuits at terminals via `is_empty`.
        assert!(portable.into_query_set().is_empty);
    }

    /// `try_portable` on a fresh `QuerySet::new()` (Q::Portable(True))
    /// reduces and the resulting `PortableQuerySet::predicate()` is True.
    #[test]
    fn try_portable_unfiltered_yields_true_predicate() {
        let qs: QuerySet<Fake> = QuerySet::new();
        let portable = qs
            .try_portable()
            .expect("unfiltered queryset must satisfy try_portable");
        assert!(matches!(portable.predicate(), sassi::BasicPredicate::True));
    }

    /// `try_portable` returns the typed `CacheInvalidNode` error and hands
    /// the original queryset back unchanged when the condition tree carries
    /// SQL-only legacy `Q::Condition` payloads.
    #[test]
    fn try_portable_rejects_legacy_condition_payload() {
        use crate::query::condition::{FilterValue, Leaf};
        let qs: QuerySet<Fake> =
            QuerySet::new().filter(|_| Condition::Leaf(Leaf::eq_raw("a", FilterValue::Bool(true))));
        // `is_portable` reports the same classification through the
        // borrow-style API.
        assert!(matches!(
            qs.is_portable(),
            Err(PortablePredicateError::CacheInvalidNode { kind: "Condition" }),
        ));
        let (recovered, err) = qs
            .try_portable()
            .expect_err("legacy Condition must fail try_portable");
        assert!(matches!(
            err,
            PortablePredicateError::CacheInvalidNode { kind: "Condition" }
        ));
        // The recovered queryset still carries the legacy condition so the
        // caller can rewrite it without losing the original filter.
        assert!(matches!(&recovered.condition, Q::Condition(_)));
    }
}