prax-codegen 0.10.0

Procedural macros for code generation in the Prax ORM
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
//! Implementation of the `#[derive(Model)]` macro.

use convert_case::{Case, Casing};
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use syn::spanned::Spanned;
use syn::{Data, DeriveInput, Fields, Ident, LitStr, Type, Visibility};

/// Parse and generate code for the `#[derive(Model)]` macro.
pub fn derive_model_impl(input: &DeriveInput) -> Result<TokenStream, syn::Error> {
    let name = &input.ident;
    let module_name = format_ident!("{}", name.to_string().to_case(Case::Snake));

    // Extract struct fields
    let fields = match &input.data {
        Data::Struct(data) => match &data.fields {
            Fields::Named(fields) => &fields.named,
            _ => {
                return Err(syn::Error::new_spanned(
                    input,
                    "Model derive only supports structs with named fields",
                ));
            }
        },
        _ => {
            return Err(syn::Error::new_spanned(
                input,
                "Model derive only supports structs",
            ));
        }
    };

    // Parse struct-level attributes
    let struct_attrs = parse_struct_attrs(input)?;
    let table_name = struct_attrs
        .table_name
        .unwrap_or_else(|| name.to_string().to_case(Case::Snake));

    // Parse field attributes
    let field_infos: Vec<FieldInfo> = fields.iter().map(parse_field).collect::<Result<_, _>>()?;

    // Find primary key fields
    let pk_fields: Vec<_> = field_infos
        .iter()
        .filter(|f| f.is_id)
        .map(|f| f.column_name.as_str())
        .collect();

    if pk_fields.is_empty() {
        return Err(syn::Error::new_spanned(
            input,
            "Model must have at least one field marked with #[prax(id)]",
        ));
    }

    // Generate field modules. Relation fields (`is_list`) don't map to
    // a column, so they skip the scalar-field emitters and go through
    // `relation_accessors::emit` instead.
    let field_modules: Vec<_> = field_infos
        .iter()
        .filter(|f| !f.is_list)
        .map(generate_field_module_from_derive)
        .collect();

    // Generate where param variants. `is_list` fields are relations,
    // not columns — emitting a `WhereOp` over `Vec<Related>` would try
    // to build filters over a non-scalar type and fail to compile.
    let where_variants: Vec<_> = field_infos
        .iter()
        .filter(|f| !f.is_list)
        .map(|f| {
            let variant_name = format_ident!("{}", f.name.to_string().to_case(Case::Pascal));
            let field_mod = &f.name;
            quote! { #variant_name(#field_mod::WhereOp) }
        })
        .collect();

    // Generate From<WhereParam> for Filter match arms. Mirrors the
    // same filter applied to `where_variants` above.
    let from_filter_arms: Vec<_> = field_infos
        .iter()
        .filter(|f| !f.is_list)
        .map(|f| {
            let variant_name = format_ident!("{}", f.name.to_string().to_case(Case::Pascal));
            quote! { WhereParam::#variant_name(op) => op.to_filter(), }
        })
        .collect();

    // Generate select param variants. Relation fields aren't columns,
    // so they don't show up in `SelectParam` either.
    let select_variants: Vec<_> = field_infos
        .iter()
        .filter(|f| !f.is_list)
        .map(|f| {
            let variant_name = format_ident!("{}", f.name.to_string().to_case(Case::Pascal));
            quote! { #variant_name }
        })
        .collect();

    // Generate order by param variants
    let order_variants: Vec<_> = field_infos
        .iter()
        .filter(|f| !f.is_list)
        .map(|f| {
            let variant_name = format_ident!("{}", f.name.to_string().to_case(Case::Pascal));
            quote! { #variant_name(::prax_orm::_prax_prelude::SortOrder) }
        })
        .collect();

    // Relation fields (`Vec<_>`) are handled separately by the relation
    // codegen; they're not columns and don't round-trip through FromRow.
    // Aggregate fields (`@count`/`@sum`/etc.) also have no underlying column
    // in the base table — they are excluded from COLUMNS but do participate
    // in FromRow with soft-missing defaulting (see `derive_from_row::emit`).
    // @generated fields ARE real columns and stay in COLUMNS.
    let all_columns: Vec<String> = field_infos
        .iter()
        .filter(|f| !f.is_list && f.aggregate.is_none())
        .map(|f| f.column_name.clone())
        .collect();
    let pk_columns_owned: Vec<String> = field_infos
        .iter()
        .filter(|f| f.is_id)
        .map(|f| f.column_name.clone())
        .collect();
    // Regular scalar fields: non-list, non-aggregate — deserialized normally.
    let from_row_fields: Vec<(Ident, Type, String)> = field_infos
        .iter()
        .filter(|f| !f.is_list && f.aggregate.is_none())
        .map(|f| (f.name.clone(), f.ty.clone(), f.column_name.clone()))
        .collect();
    // Aggregate fields: soft-miss the column, defaulting to 0 (Count) or None
    // (Sum/Avg/Min/Max) when the row doesn't include the projected value.
    // Tuple: (field_ident, declared_type, col_name, kind_str).
    let aggregate_from_row_fields: Vec<(Ident, Type, String, String)> = field_infos
        .iter()
        .filter_map(|f| {
            f.aggregate.as_ref().map(|(kind, _rel, _field)| {
                (
                    f.name.clone(),
                    f.ty.clone(),
                    f.column_name.clone(),
                    kind.clone(),
                )
            })
        })
        .collect();
    // Relation fields get initialized to `Default::default()` on
    // `from_row` — the `.include()` path fills them afterwards.
    let from_row_relation_fields: Vec<Ident> = field_infos
        .iter()
        .filter(|f| f.is_list)
        .map(|f| f.name.clone())
        .collect();
    // Same shape as from_row_fields plus the is_id flag; the ModelWithPk
    // emitter needs is_id to route PK fields into pk_value() and every
    // scalar field into get_column_value().
    // Aggregate fields are excluded: they have no underlying column to match.
    let model_with_pk_fields: Vec<(Ident, Type, String, bool)> = field_infos
        .iter()
        .filter(|f| !f.is_list && f.aggregate.is_none())
        .map(|f| (f.name.clone(), f.ty.clone(), f.column_name.clone(), f.is_id))
        .collect();

    // Collect generated and aggregate field metadata for the Model trait consts.
    let generated_fields: Vec<(String, String, bool)> = field_infos
        .iter()
        .filter_map(|f| {
            f.generated
                .as_ref()
                .map(|(expr, stored)| (f.name.to_string(), expr.clone(), *stored))
        })
        .collect();
    let aggregate_fields: Vec<(String, String, String, Option<String>)> = field_infos
        .iter()
        .filter_map(|f| {
            f.aggregate.as_ref().map(|(kind, rel, field)| {
                (f.name.to_string(), kind.clone(), rel.clone(), field.clone())
            })
        })
        .collect();

    let generated_fields_refs: Vec<(&str, &str, bool)> = generated_fields
        .iter()
        .map(|(f, e, s)| (f.as_str(), e.as_str(), *s))
        .collect();
    let aggregate_fields_refs: Vec<(&str, &str, &str, Option<&str>)> = aggregate_fields
        .iter()
        .map(|(f, k, r, field)| (f.as_str(), k.as_str(), r.as_str(), field.as_deref()))
        .collect();

    let model_trait_impl = super::derive_model_trait::emit(
        name,
        &name.to_string(),
        &table_name,
        &pk_columns_owned,
        &all_columns,
        &generated_fields_refs,
        &aggregate_fields_refs,
    );
    let from_row_impl = super::derive_from_row::emit(
        name,
        &from_row_fields,
        &from_row_relation_fields,
        &aggregate_from_row_fields,
    );
    let model_with_pk_impl = super::derive_model_with_pk::emit(name, &model_with_pk_fields);
    let client_impl = super::derive_client::emit(quote! { super::#name });

    // Per-relation `pub mod <field>` modules with `fetch()` and the
    // `Relation` marker. Only relation fields — scalar fields already
    // have their own `pub mod <field>` emitted by
    // `generate_field_module_from_derive`.
    let relation_mods: Vec<_> = field_infos
        .iter()
        .filter_map(|f| {
            f.relation.as_ref().map(|rel| {
                let kind = if f.is_list {
                    super::relation_accessors::RelationKindTokens::HasMany
                } else if f.is_optional {
                    super::relation_accessors::RelationKindTokens::HasOne
                } else {
                    super::relation_accessors::RelationKindTokens::BelongsTo
                };
                super::relation_accessors::emit(super::relation_accessors::RelationSpec {
                    field_name: &f.name,
                    owner: name,
                    target: &rel.target,
                    kind,
                    local_key: &rel.local_key,
                    foreign_key: &rel.foreign_key,
                })
            })
        })
        .collect();

    // Build the outgoing-relation list for the `<Model>Count` synthetic
    // struct.  Only fields that carry a `#[prax(relation(...))]` attr
    // are outgoing relations — scalar fields and aggregate fields are
    // excluded.  The field name on the parent struct (e.g., `posts`) is
    // used as the `Option<i64>` field name on `<Model>Count`.
    //
    // We collect the field names as owned Strings first so that the
    // `OutgoingRelation<'a>` borrows have a lifetime that spans the
    // `emit_count_struct` call below.
    let count_relation_names: Vec<String> = field_infos
        .iter()
        .filter(|f| f.relation.is_some())
        .map(|f| f.name.to_string())
        .collect();
    let count_struct_outgoing: Vec<super::count_struct::OutgoingRelation<'_>> =
        count_relation_names
            .iter()
            .map(|n| super::count_struct::OutgoingRelation { field_name: n })
            .collect();
    let count_struct_tokens = super::count_struct::emit_count_struct(name, &count_struct_outgoing);

    let scalar_meta: Vec<super::aggregate::ScalarFieldMeta<'_>> = field_infos
        .iter()
        .filter(|f| f.relation.is_none() && f.aggregate.is_none())
        .map(|f| super::aggregate::ScalarFieldMeta {
            ident: &f.name,
            ty: &f.ty,
            column_name: f.column_name.as_str(),
            is_numeric: super::aggregate::rust_type_is_numeric(&f.ty),
            is_sortable: super::aggregate::rust_type_is_sortable(&f.ty),
        })
        .collect();
    let aggregate_select_inputs = super::aggregate::emit_select_inputs(name, &scalar_meta);
    let aggregate_result_structs = super::aggregate::emit_result_structs(name, &scalar_meta);
    let aggregate_args = super::aggregate::emit_args_and_columns_enum(name, &scalar_meta);

    // Per-model `impl ModelRelationLoader<E>` dispatcher. Models with
    // no relations still get an impl — it errors on any unknown name,
    // preserving the uniform `ModelRelationLoader` bound on find
    // operations.
    let loader_relations: Vec<super::derive_relation_loader::LoaderRelation<'_>> = field_infos
        .iter()
        .filter_map(|f| {
            f.relation.as_ref().map(|rel| {
                let kind = if f.is_list {
                    super::derive_relation_loader::LoaderKind::HasMany
                } else {
                    super::derive_relation_loader::LoaderKind::HasOne
                };
                super::derive_relation_loader::LoaderRelation {
                    field_name: &f.name,
                    target: &rel.target,
                    kind,
                }
            })
        })
        .collect();
    let model_relation_loader_impl = super::derive_relation_loader::emit(name, &loader_relations);

    // Build WhereField list for the where_input generator.
    // Only scalar fields (no relation attr, not a Vec) are included for now.
    // Relation filter fields (ListRelationFilter / SingleRelationFilter) are
    // deferred to Task 8 when `<Model><Rel>FilterMeta` types are generated;
    // emitting them here without the corresponding FilterMeta impl would
    // produce unresolvable references like `UserPostsFilterMeta`.
    let where_fields: Vec<super::WhereField> = field_infos
        .iter()
        .filter_map(|f| {
            if f.relation.is_some() || f.is_list {
                // Relation fields: phase-2 emits the FilterMeta marker but
                // leaves the WhereInput relation-filter wiring for a later
                // phase (path-resolution work for `super::<target>::<...>`).
                None
            } else {
                let type_name = extract_inner_type_name(&f.ty);
                let category =
                    super::inputs::filter_category_for(type_name.as_deref().unwrap_or(""));
                Some(super::WhereField {
                    name: f.name.clone(),
                    column: f.column_name.clone(),
                    category,
                    nullable: f.is_optional,
                    relation_target_where_input: None,
                    is_to_many: false,
                })
            }
        })
        .collect();

    let super::WhereInputTokens {
        struct_tokens: where_input_struct,
        impl_tokens: where_input_impl,
    } = super::generate_where_input(name, &module_name, &where_fields);

    // Build UniqueColumn list for the where_unique_input generator.
    // Only scalar @id / @unique fields; relation fields are never unique keys
    // we can encode as a simple Equals lookup.
    let unique_columns: Vec<super::UniqueColumn> = field_infos
        .iter()
        .filter(|f| (f.is_id || f.is_unique) && f.relation.is_none() && !f.is_list)
        .filter_map(|f| {
            let inner = extract_inner_type_name(&f.ty);
            let cat = super::inputs::filter_category_for(inner.as_deref().unwrap_or(""))?;
            Some(super::UniqueColumn {
                variant: format_ident!("{}", f.name.to_string().to_case(Case::Pascal)),
                column: f.column_name.clone(),
                category: cat,
                enum_ident: None,
            })
        })
        .collect();

    super::inputs::where_unique_input::check_unique_column_collisions(
        &unique_columns,
        Some(&name.to_string()),
    )?;

    let (where_unique_struct, where_unique_impl) =
        super::generate_where_unique_input(name, &module_name, &unique_columns);

    // Build IncludeField list: every field that carries a relation attr.
    let include_fields: Vec<super::IncludeField> = field_infos
        .iter()
        .filter(|f| f.relation.is_some())
        .map(|f| super::IncludeField {
            name: f.name.clone(),
            relation: f.name.to_string(),
        })
        .collect();

    // Build SelectField list: all fields; relation fields are marked so
    // their column names are excluded from the SELECT column list.
    // Aggregate fields are also marked as no-column — they have no base-table
    // column and will be resolved via scalar subquery in a later phase.
    let select_fields: Vec<super::SelectField> = field_infos
        .iter()
        .map(|f| super::SelectField {
            name: f.name.clone(),
            column: f.column_name.clone(),
            is_relation: f.relation.is_some(),
            is_no_column: f.aggregate.is_some(),
        })
        .collect();

    let (include_struct, include_impl) =
        super::generate_include_input(name, &module_name, &include_fields);
    let (select_struct, select_impl) =
        super::generate_select_input(name, &module_name, &select_fields);

    // Build OrderByField list: every non-relation scalar column is sortable.
    let order_by_fields: Vec<super::OrderByInputField> = field_infos
        .iter()
        .filter(|f| f.relation.is_none() && !f.is_list)
        .map(|f| super::OrderByInputField {
            variant: format_ident!("{}", f.name.to_string().to_case(Case::Pascal)),
            column: f.column_name.clone(),
            nullable: f.is_optional,
        })
        .collect();

    let (order_by_struct, order_by_impl) =
        super::generate_order_by_input(name, &module_name, &order_by_fields);

    // Build CreateField list: scalar fields only, skipping auto-generated PKs
    // and computed/aggregate fields (@generated and @count/@sum/@avg/@min/@max).
    // Computed columns are derived by the DB or via scalar subquery — callers
    // cannot (and should not) assign values to them at create time.
    let create_fields: Vec<super::inputs::create_input::CreateField> = field_infos
        .iter()
        .filter(|f| f.relation.is_none() && !f.is_list)
        .filter(|f| !(f.is_id && f.is_auto))
        .filter(|f| f.generated.is_none() && f.aggregate.is_none())
        .filter_map(|f| {
            let inner = extract_inner_type_name(&f.ty);
            let cat = super::inputs::filter_category_for(inner.as_deref().unwrap_or(""))?;
            Some(super::inputs::create_input::CreateField {
                name: f.name.clone(),
                column: f.column_name.clone(),
                category: cat,
                nullable: f.is_optional,
                has_default: false, // phase 2: no default detection yet
                enum_ident: None,
            })
        })
        .collect();

    // Build UpdateField list: scalar fields only, skipping auto-generated PKs
    // and computed/aggregate fields (@generated and @count/@sum/@avg/@min/@max).
    // Computed columns are derived by the DB or via scalar subquery — callers
    // cannot (and should not) set them at update time.
    let update_fields: Vec<super::inputs::update_input::UpdateField> = field_infos
        .iter()
        .filter(|f| f.relation.is_none() && !f.is_list)
        .filter(|f| !(f.is_id && f.is_auto))
        .filter(|f| f.generated.is_none() && f.aggregate.is_none())
        .filter_map(|f| {
            let inner = extract_inner_type_name(&f.ty);
            let cat = super::inputs::filter_category_for(inner.as_deref().unwrap_or(""))?;
            Some(super::inputs::update_input::UpdateField {
                name: f.name.clone(),
                column: f.column_name.clone(),
                category: cat,
                nullable: f.is_optional,
                enum_ident: None,
            })
        })
        .collect();

    let super::inputs::create_input::CreateInputTokens {
        struct_tokens: create_struct,
        impl_tokens: create_input_impl,
    } = super::inputs::create_input::generate(name, &module_name, &create_fields);
    let super::inputs::update_input::UpdateInputTokens {
        struct_tokens: update_struct,
        impl_tokens: update_input_impl,
    } = super::inputs::update_input::generate(name, &module_name, &update_fields);

    // Build RelationMetaSpec list for the relation_meta generator (Task 8).
    // Parent PK is the first @id field's column name.
    let parent_pk = field_infos
        .iter()
        .find(|f| f.is_id)
        .map(|f| f.column_name.clone())
        .expect("model must have at least one #[prax(id)] field (validated upstream)");

    let relation_meta_specs: Vec<super::inputs::relation_meta::RelationMetaSpec> = field_infos
        .iter()
        .filter_map(|f| {
            let rel = f.relation.as_ref()?;
            // Child table: prefer the explicit `child_table = "..."` attr
            // (required when the target model uses `#[prax(table = "...")]`),
            // otherwise fall back to the snake-cased target ident, which
            // matches the default table-naming convention.
            let child_table = rel
                .child_table
                .clone()
                .unwrap_or_else(|| rel.target.to_string().to_case(Case::Snake));
            Some(super::inputs::relation_meta::RelationMetaSpec {
                meta_ident: format_ident!(
                    "{}{}FilterMeta",
                    name,
                    f.name.to_string().to_case(Case::Pascal)
                ),
                parent_table: table_name.clone(),
                parent_pk: parent_pk.clone(),
                child_table,
                child_fk: rel.foreign_key.clone(),
            })
        })
        .collect();

    let (relation_meta_struct, relation_meta_impl) =
        super::inputs::relation_meta::generate(&module_name, &relation_meta_specs);

    // Only emit the `WhereInput` trait impl when the model struct is pub.
    // If the struct is private (e.g., in integration tests), emitting
    // `impl WhereInput for pub UserWhereInput { type Model = PrivateUser; }`
    // triggers E0446 "private type in public interface". The struct
    // definition is always emitted; the trait impl is gated on visibility.
    let is_pub = matches!(input.vis, Visibility::Public(_));
    let gate_impl = |tokens: TokenStream| if is_pub { tokens } else { TokenStream::new() };
    let aggregate_accessors =
        super::aggregate::emit_accessors_and_extensions(name, &scalar_meta, is_pub);
    let maybe_where_input_impl = gate_impl(where_input_impl);
    let maybe_where_unique_impl = gate_impl(where_unique_impl);
    let maybe_include_impl = gate_impl(include_impl);
    let maybe_select_impl = gate_impl(select_impl);
    let maybe_order_by_impl = gate_impl(order_by_impl);
    let maybe_create_input_impl = gate_impl(create_input_impl);
    let maybe_update_input_impl = gate_impl(update_input_impl);

    Ok(quote! {
        /// Generated module for the #name model.
        pub mod #module_name {
            use super::*;

            /// Database table name.
            pub const TABLE_NAME: &str = #table_name;

            /// Primary key column(s).
            pub const PRIMARY_KEY: &[&str] = &[#(#pk_fields),*];

            impl ::prax_orm::_prax_prelude::PraxModel for #name {
                const TABLE_NAME: &'static str = TABLE_NAME;
                const PRIMARY_KEY: &'static [&'static str] = PRIMARY_KEY;
            }

            // Field modules
            #(#field_modules)*

            // Per-relation modules — emitted for every field marked
            // `#[prax(relation(...))]`. Each one defines `fetch()` +
            // `Relation` (a zero-sized `RelationMeta` marker).
            #(#relation_mods)*

            /// Where clause parameters.
            #[derive(Debug, Clone)]
            pub enum WhereParam {
                #(#where_variants,)*
                And(Vec<WhereParam>),
                Or(Vec<WhereParam>),
                Not(Box<WhereParam>),
            }

            impl From<WhereParam> for ::prax_query::filter::Filter {
                fn from(p: WhereParam) -> Self {
                    match p {
                        #(#from_filter_arms)*
                        WhereParam::And(ps) => ::prax_query::filter::Filter::And(
                            ps.into_iter().map(Into::into).collect::<Vec<_>>().into_boxed_slice()
                        ),
                        WhereParam::Or(ps) => ::prax_query::filter::Filter::Or(
                            ps.into_iter().map(Into::into).collect::<Vec<_>>().into_boxed_slice()
                        ),
                        WhereParam::Not(p) => ::prax_query::filter::Filter::Not(Box::new((*p).into())),
                    }
                }
            }

            /// Select parameters.
            #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
            pub enum SelectParam {
                #(#select_variants,)*
            }

            /// Order by parameters.
            #[derive(Debug, Clone, Copy)]
            pub enum OrderByParam {
                #(#order_variants,)*
            }

            #client_impl

            // Typed input shapes (phase 2) — struct definitions.
            // The WhereInput / WhereUniqueInput / IncludeInput / SelectInput /
            // OrderByInput trait impls are emitted outside the module (below)
            // to avoid E0446 "private type in public interface" when the model
            // struct is not `pub`.
            #where_input_struct
            #where_unique_struct
            #include_struct
            #select_struct
            #order_by_struct
            #create_struct
            #update_struct

            // Per-model aggregate select-shape input structs (phase 6):
            // <Model>{Count,Sum,Avg,Min,Max}Select.
            #aggregate_select_inputs

            // Per-model aggregate result output structs (phase 6):
            // <Model>{CountSelectResult,SumResult,AvgResult,MinResult,MaxResult,
            // AggregateResult,GroupByResult}.
            #aggregate_result_structs

            // Per-model GroupByColumn enum, AggregateArgs, GroupByArgs,
            // GroupByHaving, GroupByOrderBy (phase 6 T4).
            #aggregate_args

            // fields_set() / all_set() helpers, typed group_by_columns() on
            // Client<E>, and with_aggregate_args / with_group_by_args extensions
            // on AggregateOperation / GroupByOperation (phase 6 T5).
            #aggregate_accessors

            // Per-relation `<Model><Relation>FilterMeta` marker structs (Task 8).
            // The corresponding `impl RelationFilterMeta` blocks are emitted
            // below at crate-root scope so the path `#module_name::#marker`
            // resolves correctly.
            #relation_meta_struct
        }

        // Emit Model, FromRow, and ModelWithPk trait implementations at crate root
        #model_trait_impl
        #from_row_impl
        #model_with_pk_impl

        // Emit ModelRelationLoader<E> so every derived model — relations
        // or not — can be used on the `.include()` path uniformly.
        #model_relation_loader_impl

        // WhereInput trait impl at crate scope — only emitted when the
        // model struct is `pub` to avoid E0446. When emitted, `type Model =
        // <Model>` is valid because the model struct is directly in scope.
        #maybe_where_input_impl

        // WhereUniqueInput trait impl — same visibility gating as above.
        #maybe_where_unique_impl

        // IncludeInput trait impl — same visibility gating.
        #maybe_include_impl

        // SelectInput trait impl — same visibility gating.
        #maybe_select_impl

        // OrderByInput trait impl — same visibility gating.
        #maybe_order_by_impl

        // CreateInput / UpdateInput trait impls (phase 5a) — same
        // visibility gating as the other trait impls above.
        #maybe_create_input_impl
        #maybe_update_input_impl

        // RelationFilterMeta impls — one per declared relation field.
        // Each impl associates the zero-sized `<Model><Relation>FilterMeta`
        // marker (declared in the `pub mod` above) with the parent/child table
        // and column names required for EXISTS / NOT EXISTS lowering.
        #relation_meta_impl

        // `<Model>Count` synthetic struct — emitted only when the model has
        // at least one outgoing relation.  Contains one `pub <rel>: Option<i64>`
        // per relation.  The `_count` result-field integration is deferred to
        // Task 14 (the derive macro cannot add fields to the user's struct).
        #count_struct_tokens
    })
}

/// Struct-level attributes parsed from `#[prax(...)]`.
#[derive(Debug, Default)]
struct StructAttrs {
    table_name: Option<String>,
    schema_name: Option<String>,
}

/// Parse struct-level `#[prax(...)]` attributes.
fn parse_struct_attrs(input: &DeriveInput) -> Result<StructAttrs, syn::Error> {
    let mut attrs = StructAttrs::default();

    for attr in &input.attrs {
        if !attr.path().is_ident("prax") {
            continue;
        }

        attr.parse_nested_meta(|meta| {
            if meta.path.is_ident("table") {
                let value: LitStr = meta.value()?.parse()?;
                attrs.table_name = Some(value.value());
            } else if meta.path.is_ident("schema") {
                let value: LitStr = meta.value()?.parse()?;
                attrs.schema_name = Some(value.value());
            }
            Ok(())
        })?;
    }

    Ok(attrs)
}

/// Information about a field.
#[derive(Debug)]
#[allow(dead_code)]
struct FieldInfo {
    name: Ident,
    ty: Type,
    column_name: String,
    is_id: bool,
    is_auto: bool,
    is_unique: bool,
    is_optional: bool,
    is_list: bool,
    relation: Option<RelationAttr>,
    /// `#[prax(generated = "expr")]` / `#[prax(generated = "expr", stored)]`
    /// / `#[prax(generated = "expr", virtual)]` — (expression, stored).
    generated: Option<(String, bool)>,
    /// `#[prax(count(rel))]` / `#[prax(sum(rel.field))]` etc.
    /// Tuple: (kind_str, relation, field).
    aggregate: Option<(String, String, Option<String>)>,
}

/// Parsed `#[prax(relation(target = "...", foreign_key = "...", local_key = "..."))]`.
///
/// Relation fields are not columns — the derive filters them out of
/// every column/WhereParam/SelectParam emission path and funnels them
/// instead into the per-relation `pub mod <field>` / `Relation` pair
/// emitted by [`super::relation_accessors`] plus the per-model
/// `ModelRelationLoader` impl emitted by
/// [`super::derive_relation_loader`].
#[derive(Debug)]
struct RelationAttr {
    /// Target model type identifier — the type the relation points at.
    target: syn::Ident,
    /// Column on the target model holding the FK back to this model's
    /// PK (for `HasMany` / `HasOne`). Required.
    foreign_key: String,
    /// Column on this model referencing the target's PK (for
    /// `BelongsTo`). Defaults to `"id"`.
    local_key: String,
    /// Optional explicit child SQL table name. Required when the target
    /// model uses `#[prax(table = "...")]` to override its default name,
    /// because the derive macro has no access to the target model's
    /// attributes at expansion time. If absent, the snake-cased target
    /// ident is used (matches the default table-name convention).
    child_table: Option<String>,
}

/// Parse a field and its `#[prax(...)]` attributes.
fn parse_field(field: &syn::Field) -> Result<FieldInfo, syn::Error> {
    let name = field
        .ident
        .clone()
        .ok_or_else(|| syn::Error::new_spanned(field, "Fields must be named"))?;

    let ty = field.ty.clone();
    let mut column_name = name.to_string().to_case(Case::Snake);
    let mut is_id = false;
    let mut is_auto = false;
    let mut is_unique = false;
    let mut relation: Option<RelationAttr> = None;
    let mut generated_expr: Option<String> = None;
    let mut generated_stored: bool = true; // default: stored
    let mut aggregate: Option<(String, String, Option<String>)> = None;

    // Determine if the type is Optional or Vec
    let is_optional = is_option_type(&ty);
    let is_list = is_vec_type(&ty);

    for attr in &field.attrs {
        if !attr.path().is_ident("prax") {
            continue;
        }

        attr.parse_nested_meta(|meta| {
            if meta.path.is_ident("id") {
                is_id = true;
            } else if meta.path.is_ident("auto") {
                is_auto = true;
            } else if meta.path.is_ident("unique") {
                is_unique = true;
            } else if meta.path.is_ident("column") {
                let value: LitStr = meta.value()?.parse()?;
                column_name = value.value();
            } else if meta.path.is_ident("generated") {
                let value: LitStr = meta.value()?.parse()?;
                generated_expr = Some(value.value());
            } else if meta.path.is_ident("stored") {
                generated_stored = true;
            } else if meta.path.is_ident("virtual") {
                generated_stored = false;
            } else if meta.path.is_ident("count") {
                // #[prax(count(relation_name))]
                // Parse `(ident)` directly from the token stream.
                let content;
                syn::parenthesized!(content in meta.input);
                let rel_ident: syn::Ident = content.parse()?;
                aggregate = Some(("count".to_string(), rel_ident.to_string(), None));
            } else if meta.path.is_ident("sum")
                || meta.path.is_ident("avg")
                || meta.path.is_ident("min")
                || meta.path.is_ident("max")
            {
                // #[prax(sum(relation.field))] — parse `(ident.ident)`.
                let kind_str = meta
                    .path
                    .get_ident()
                    .map(|i| i.to_string())
                    .unwrap_or_default();
                let content;
                syn::parenthesized!(content in meta.input);
                let rel_ident: syn::Ident = content.parse()?;
                let _dot: syn::Token![.] = content.parse()?;
                let field_ident: syn::Ident = content.parse()?;
                aggregate = Some((
                    kind_str,
                    rel_ident.to_string(),
                    Some(field_ident.to_string()),
                ));
            } else if meta.path.is_ident("relation") {
                let mut target: Option<syn::Ident> = None;
                let mut fk: Option<String> = None;
                let mut lk: Option<String> = None;
                let mut child_table: Option<String> = None;
                meta.parse_nested_meta(|inner| {
                    if inner.path.is_ident("target") {
                        let s: LitStr = inner.value()?.parse()?;
                        target = Some(format_ident!("{}", s.value()));
                    } else if inner.path.is_ident("foreign_key") {
                        let s: LitStr = inner.value()?.parse()?;
                        fk = Some(s.value());
                    } else if inner.path.is_ident("local_key") {
                        let s: LitStr = inner.value()?.parse()?;
                        lk = Some(s.value());
                    } else if inner.path.is_ident("child_table") {
                        let s: LitStr = inner.value()?.parse()?;
                        child_table = Some(s.value());
                    }
                    Ok(())
                })?;
                let target = target.ok_or_else(|| {
                    syn::Error::new(meta.path.span(), "relation requires target = \"ModelName\"")
                })?;
                let foreign_key = fk.ok_or_else(|| {
                    syn::Error::new(meta.path.span(), "relation requires foreign_key = \"...\"")
                })?;
                relation = Some(RelationAttr {
                    target,
                    foreign_key,
                    local_key: lk.unwrap_or_else(|| "id".to_string()),
                    child_table,
                });
            }
            Ok(())
        })?;
    }

    let generated = generated_expr.map(|expr| (expr, generated_stored));

    Ok(FieldInfo {
        name,
        ty,
        column_name,
        is_id,
        is_auto,
        is_unique,
        is_optional,
        is_list,
        relation,
        generated,
        aggregate,
    })
}

/// Check if a type is `Option<T>`.
fn is_option_type(ty: &Type) -> bool {
    if let Type::Path(type_path) = ty
        && let Some(segment) = type_path.path.segments.first()
    {
        return segment.ident == "Option";
    }
    false
}

/// Check if a type is `Vec<T>` representing a to-many relation.
///
/// `Vec<u8>` is treated as a Bytes scalar (binary column), not a relation
/// list. Other element types map to relation lists.
fn is_vec_type(ty: &Type) -> bool {
    if let Type::Path(type_path) = ty
        && let Some(segment) = type_path.path.segments.first()
        && segment.ident == "Vec"
    {
        if let syn::PathArguments::AngleBracketed(args) = &segment.arguments
            && let Some(syn::GenericArgument::Type(Type::Path(inner_path))) = args.args.first()
            && inner_path.path.is_ident("u8")
        {
            return false;
        }
        return true;
    }
    false
}

/// Extract the last path segment name from a `syn::Type`, unwrapping one
/// layer of `Option<T>` if present.  Returns `None` for non-path types.
///
/// Used by the where_input generator to map `syn::Type` → `FilterCategory`
/// without carrying a `type_str` field in `FieldInfo`.
///
/// Note: only ONE layer of `Option<T>` is unwrapped. ORM column types
/// are never doubly nested (`Option<Option<T>>` isn't a meaningful
/// column shape), so this is a documented design assumption rather
/// than a defensive recursive unwrap.
fn extract_inner_type_name(ty: &Type) -> Option<String> {
    if let Type::Path(type_path) = ty {
        if let Some(segment) = type_path.path.segments.first()
            && segment.ident == "Option"
        {
            // Unwrap Option<T> and recurse on the inner type.
            if let syn::PathArguments::AngleBracketed(args) = &segment.arguments
                && let Some(syn::GenericArgument::Type(inner)) = args.args.first()
            {
                return extract_inner_type_name(inner);
            }
        }
        // Return the last segment as the type name (handles `chrono::DateTime<_>` → `DateTime`).
        type_path.path.segments.last().map(|s| s.ident.to_string())
    } else {
        None
    }
}

/// Field type category for conditional filter emission.
#[derive(Clone, Copy, PartialEq, Eq)]
enum TypeCategory {
    Numeric,
    String,
    Boolean,
    Other,
}

/// Classify a Rust type for filter operator emission.
///
/// Inspects the type to determine which filter operators make sense. Unwraps
/// Option<T> to classify the inner type. Returns the category that drives
/// conditional emission of comparison, string, and IN operators.
fn classify_field_type(ty: &Type) -> TypeCategory {
    // Unwrap Option<T> to get the inner type.
    let type_name = if let Type::Path(type_path) = ty {
        if let Some(segment) = type_path.path.segments.first()
            && segment.ident == "Option"
            && let syn::PathArguments::AngleBracketed(args) = &segment.arguments
            && let Some(syn::GenericArgument::Type(inner)) = args.args.first()
        {
            // Recurse to classify the inner type.
            return classify_field_type(inner);
        }
        // Not Option — extract the last segment as the type name.
        type_path.path.segments.last().map(|s| s.ident.to_string())
    } else {
        None
    };

    match type_name.as_deref() {
        Some(
            "i8" | "i16" | "i32" | "i64" | "i128" | "isize" | "u8" | "u16" | "u32" | "u64" | "u128"
            | "usize" | "f32" | "f64" | "Decimal" | "NaiveDate" | "NaiveDateTime" | "NaiveTime"
            | "DateTime",
        ) => TypeCategory::Numeric,
        Some("String" | "str") => TypeCategory::String,
        Some("bool") => TypeCategory::Boolean,
        _ => TypeCategory::Other,
    }
}

/// Generate a field module from derive macro field info.
fn generate_field_module_from_derive(field: &FieldInfo) -> TokenStream {
    let field_name = &field.name;
    let column_name = &field.column_name;
    let ty = &field.ty;
    let variant_name = format_ident!("{}", field.name.to_string().to_case(Case::Pascal));
    let is_optional = field.is_optional;
    let category = classify_field_type(ty);

    // Collect WhereOp enum variants conditionally.
    let mut variants = vec![quote! { Equals(#ty) }, quote! { Not(#ty) }];

    if is_optional {
        variants.push(quote! { IsNull });
        variants.push(quote! { IsNotNull });
    }

    match category {
        TypeCategory::Numeric => {
            variants.push(quote! { In(Vec<#ty>) });
            variants.push(quote! { NotIn(Vec<#ty>) });
            variants.push(quote! { Gt(#ty) });
            variants.push(quote! { Gte(#ty) });
            variants.push(quote! { Lt(#ty) });
            variants.push(quote! { Lte(#ty) });
        }
        TypeCategory::String => {
            variants.push(quote! { In(Vec<#ty>) });
            variants.push(quote! { NotIn(Vec<#ty>) });
            variants.push(quote! { Contains(String) });
            variants.push(quote! { StartsWith(String) });
            variants.push(quote! { EndsWith(String) });
        }
        TypeCategory::Boolean => {}
        TypeCategory::Other => {
            variants.push(quote! { In(Vec<#ty>) });
            variants.push(quote! { NotIn(Vec<#ty>) });
        }
    }

    // Collect to_filter match arms in the same conditional shape.
    let mut arms = vec![
        quote! { Self::Equals(v) => Filter::Equals(col, v.into()) },
        quote! { Self::Not(v) => Filter::NotEquals(col, v.into()) },
    ];

    if is_optional {
        arms.push(quote! { Self::IsNull => Filter::IsNull(col) });
        arms.push(quote! { Self::IsNotNull => Filter::IsNotNull(col) });
    }

    match category {
        TypeCategory::Numeric => {
            arms.push(
                quote! { Self::In(vs) => Filter::In(col, vs.into_iter().map(Into::into).collect()) },
            );
            arms.push(
                quote! { Self::NotIn(vs) => Filter::NotIn(col, vs.into_iter().map(Into::into).collect()) },
            );
            arms.push(quote! { Self::Gt(v) => Filter::Gt(col, v.into()) });
            arms.push(quote! { Self::Gte(v) => Filter::Gte(col, v.into()) });
            arms.push(quote! { Self::Lt(v) => Filter::Lt(col, v.into()) });
            arms.push(quote! { Self::Lte(v) => Filter::Lte(col, v.into()) });
        }
        TypeCategory::String => {
            arms.push(
                quote! { Self::In(vs) => Filter::In(col, vs.into_iter().map(Into::into).collect()) },
            );
            arms.push(
                quote! { Self::NotIn(vs) => Filter::NotIn(col, vs.into_iter().map(Into::into).collect()) },
            );
            arms.push(
                quote! { Self::Contains(v) => Filter::Contains(col, FilterValue::String(v)) },
            );
            arms.push(
                quote! { Self::StartsWith(v) => Filter::StartsWith(col, FilterValue::String(v)) },
            );
            arms.push(
                quote! { Self::EndsWith(v) => Filter::EndsWith(col, FilterValue::String(v)) },
            );
        }
        TypeCategory::Boolean => {}
        TypeCategory::Other => {
            arms.push(
                quote! { Self::In(vs) => Filter::In(col, vs.into_iter().map(Into::into).collect()) },
            );
            arms.push(
                quote! { Self::NotIn(vs) => Filter::NotIn(col, vs.into_iter().map(Into::into).collect()) },
            );
        }
    }

    // Collect constructor functions.
    let mut ctors = vec![quote! {
        pub fn equals(value: #ty) -> super::WhereParam {
            super::WhereParam::#variant_name(WhereOp::Equals(value))
        }

        pub fn not(value: #ty) -> super::WhereParam {
            super::WhereParam::#variant_name(WhereOp::Not(value))
        }
    }];

    if is_optional {
        ctors.push(quote! {
            pub fn is_null() -> super::WhereParam {
                super::WhereParam::#variant_name(WhereOp::IsNull)
            }

            pub fn is_not_null() -> super::WhereParam {
                super::WhereParam::#variant_name(WhereOp::IsNotNull)
            }
        });
    }

    match category {
        TypeCategory::Numeric => {
            ctors.push(quote! {
                pub fn in_(values: Vec<#ty>) -> super::WhereParam {
                    super::WhereParam::#variant_name(WhereOp::In(values))
                }

                pub fn not_in(values: Vec<#ty>) -> super::WhereParam {
                    super::WhereParam::#variant_name(WhereOp::NotIn(values))
                }

                pub fn gt(value: #ty) -> super::WhereParam {
                    super::WhereParam::#variant_name(WhereOp::Gt(value))
                }

                pub fn gte(value: #ty) -> super::WhereParam {
                    super::WhereParam::#variant_name(WhereOp::Gte(value))
                }

                pub fn lt(value: #ty) -> super::WhereParam {
                    super::WhereParam::#variant_name(WhereOp::Lt(value))
                }

                pub fn lte(value: #ty) -> super::WhereParam {
                    super::WhereParam::#variant_name(WhereOp::Lte(value))
                }
            });
        }
        TypeCategory::String => {
            ctors.push(quote! {
                pub fn in_(values: Vec<#ty>) -> super::WhereParam {
                    super::WhereParam::#variant_name(WhereOp::In(values))
                }

                pub fn not_in(values: Vec<#ty>) -> super::WhereParam {
                    super::WhereParam::#variant_name(WhereOp::NotIn(values))
                }

                pub fn contains(value: impl Into<String>) -> super::WhereParam {
                    super::WhereParam::#variant_name(WhereOp::Contains(value.into()))
                }

                pub fn starts_with(value: impl Into<String>) -> super::WhereParam {
                    super::WhereParam::#variant_name(WhereOp::StartsWith(value.into()))
                }

                pub fn ends_with(value: impl Into<String>) -> super::WhereParam {
                    super::WhereParam::#variant_name(WhereOp::EndsWith(value.into()))
                }
            });
        }
        TypeCategory::Boolean => {}
        TypeCategory::Other => {
            ctors.push(quote! {
                pub fn in_(values: Vec<#ty>) -> super::WhereParam {
                    super::WhereParam::#variant_name(WhereOp::In(values))
                }

                pub fn not_in(values: Vec<#ty>) -> super::WhereParam {
                    super::WhereParam::#variant_name(WhereOp::NotIn(values))
                }
            });
        }
    }

    quote! {
        pub mod #field_name {
            use super::*;

            pub const COLUMN: &str = #column_name;

            #[derive(Debug, Clone)]
            pub enum WhereOp {
                #(#variants,)*
            }

            impl WhereOp {
                /// Convert to prax_query::filter::Filter.
                pub fn to_filter(self) -> ::prax_query::filter::Filter {
                    use ::prax_query::filter::{Filter, FilterValue};
                    use ::std::borrow::Cow;
                    let col: Cow<'static, str> = Cow::Borrowed(COLUMN);
                    match self {
                        #(#arms,)*
                    }
                }
            }

            #(#ctors)*
        }
    }
}

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

    #[test]
    fn test_parse_simple_model() {
        let input: DeriveInput = parse_quote! {
            #[prax(table = "users")]
            struct User {
                #[prax(id, auto)]
                id: i32,
                #[prax(unique)]
                email: String,
                name: Option<String>,
            }
        };

        let result = derive_model_impl(&input);
        assert!(result.is_ok(), "Failed: {:?}", result.err());

        let code = result.unwrap().to_string();
        assert!(code.contains("pub mod user"));
        assert!(code.contains("TABLE_NAME"));
        assert!(code.contains("users"));
    }

    #[test]
    fn test_parse_model_without_id() {
        let input: DeriveInput = parse_quote! {
            struct NoId {
                name: String,
            }
        };

        let result = derive_model_impl(&input);
        assert!(result.is_err());
    }

    #[test]
    fn test_is_option_type() {
        let ty: Type = parse_quote!(Option<String>);
        assert!(is_option_type(&ty));

        let ty: Type = parse_quote!(String);
        assert!(!is_option_type(&ty));
    }

    #[test]
    fn test_is_vec_type() {
        let ty: Type = parse_quote!(Vec<i32>);
        assert!(is_vec_type(&ty));

        let ty: Type = parse_quote!(i32);
        assert!(!is_vec_type(&ty));
    }

    // ── where_input codegen tests ──────────────────────────────────────────

    /// Derives a User model with all fixture fields and checks that
    /// `UserWhereInput` is present in the generated token stream with the
    /// expected per-field entries.
    ///
    /// The struct is `pub` because the `WhereInput` trait impl is only
    /// emitted for public model structs (to avoid E0446 "private type in
    /// public interface").
    #[test]
    fn user_where_input_default_lowers_to_filter_none() {
        let input: DeriveInput = parse_quote! {
            #[prax(table = "users")]
            pub struct User {
                #[prax(id)]
                pub id: i64,
                #[prax(unique)]
                pub email: String,
                pub name: Option<String>,
                pub age: Option<i32>,
                pub active: bool,
                pub role: String,
            }
        };

        let result = derive_model_impl(&input);
        assert!(
            result.is_ok(),
            "derive_model_impl failed: {:?}",
            result.err()
        );
        let code = result.unwrap().to_string();

        // The generated module must contain the typed where-input struct.
        assert!(
            code.contains("UserWhereInput"),
            "expected UserWhereInput in generated code; got:\n{code}"
        );
        // Scalar fields must appear (BigInt for i64, String for email/role).
        assert!(
            code.contains("BigIntFilter") || code.contains("BigIntNullableFilter"),
            "expected BigIntFilter for id field"
        );
        assert!(
            code.contains("StringFilter"),
            "expected StringFilter for email/role fields"
        );
        // Nullable fields must use nullable wrappers.
        assert!(
            code.contains("StringNullableFilter"),
            "expected StringNullableFilter for name: Option<String>"
        );
        assert!(
            code.contains("IntNullableFilter"),
            "expected IntNullableFilter for age: Option<i32>"
        );
        assert!(
            code.contains("BoolFilter"),
            "expected BoolFilter for active: bool"
        );
        // Logical combinators must be present.
        assert!(code.contains("pub and"), "expected 'and' combinator field");
        assert!(code.contains("pub or"), "expected 'or' combinator field");
        assert!(code.contains("pub not"), "expected 'not' combinator field");
        // WhereInput trait impl must appear.
        assert!(
            code.contains("impl") && code.contains("WhereInput") && code.contains("into_ir"),
            "expected WhereInput trait impl with into_ir"
        );
    }

    // ── where_unique_input codegen tests ──────────────────────────────────

    /// Confirms the WhereUniqueInput enum is emitted with one variant
    /// per @id / @unique column, lowering to Filter::Equals.
    #[test]
    fn user_where_unique_input_emits_id_and_email_variants() {
        let input: DeriveInput = parse_quote! {
            #[prax(table = "users")]
            pub struct User {
                #[prax(id)]
                pub id: i64,
                #[prax(unique)]
                pub email: String,
                pub name: Option<String>,
            }
        };

        let result = derive_model_impl(&input);
        assert!(
            result.is_ok(),
            "derive_model_impl failed: {:?}",
            result.err()
        );
        let code = result.unwrap().to_string();

        // The enum + both variants must be present.
        assert!(
            code.contains("UserWhereUniqueInput"),
            "expected UserWhereUniqueInput in generated code"
        );
        assert!(code.contains("Id"), "expected Id variant");
        assert!(code.contains("Email"), "expected Email variant");
        // The WhereUniqueInput trait impl with into_ir must appear.
        assert!(
            code.contains("WhereUniqueInput") && code.contains("into_ir"),
            "expected WhereUniqueInput trait impl"
        );
        // The column literals must be present in the match arms.
        assert!(code.contains("\"id\""), "expected \"id\" column literal");
        assert!(
            code.contains("\"email\""),
            "expected \"email\" column literal"
        );
    }

    /// Models without any unique key should still emit an uninhabited
    /// WhereUniqueInput so generic bounds resolve.
    #[test]
    fn model_without_unique_key_emits_empty_where_unique_input() {
        let input: DeriveInput = parse_quote! {
            #[prax(table = "logs")]
            pub struct Log {
                // No @id, no @unique — but at least one field marked as id
                // is required by derive_model_impl. Use a non-unique sentinel.
                #[prax(id)]
                pub message: String,
            }
        };

        let result = derive_model_impl(&input);
        assert!(result.is_ok(), "derive_model_impl failed");
        let code = result.unwrap().to_string();

        // Even though `message` is `id`, the enum will have one variant.
        // For the "truly empty" case, we'd need a model without ANY id field —
        // but derive_model_impl rejects those upstream. Confirm the enum at
        // minimum exists.
        assert!(
            code.contains("LogWhereUniqueInput"),
            "expected LogWhereUniqueInput in generated code"
        );
    }

    // ── include_input codegen tests ────────────────────────────────────────

    #[test]
    fn user_include_emits_per_relation_options() {
        let input: DeriveInput = parse_quote! {
            #[prax(table = "users")]
            pub struct User {
                #[prax(id)]
                pub id: i64,
                pub email: String,
            }
        };
        let result = derive_model_impl(&input);
        assert!(result.is_ok(), "derive_model_impl failed");
        let code = result.unwrap().to_string();

        // Even without relations, the UserInclude struct must exist with the IncludeInput impl.
        assert!(
            code.contains("UserInclude"),
            "expected UserInclude struct in generated code"
        );
        assert!(
            code.contains("IncludeInput"),
            "expected IncludeInput trait impl"
        );
    }

    // ── select_input codegen tests ─────────────────────────────────────────

    #[test]
    fn user_select_emits_per_column_options() {
        let input: DeriveInput = parse_quote! {
            #[prax(table = "users")]
            pub struct User {
                #[prax(id)]
                pub id: i64,
                pub email: String,
                pub name: Option<String>,
            }
        };
        let result = derive_model_impl(&input);
        assert!(result.is_ok(), "derive_model_impl failed");
        let code = result.unwrap().to_string();

        // UserSelect struct with Option<bool> per column.
        assert!(
            code.contains("UserSelect"),
            "expected UserSelect struct in generated code"
        );
        assert!(
            code.contains("SelectInput"),
            "expected SelectInput trait impl"
        );
        // The column literals must appear in the lowering.
        assert!(code.contains("\"id\""), "expected id column literal");
        assert!(code.contains("\"email\""), "expected email column literal");
        assert!(code.contains("\"name\""), "expected name column literal");
    }

    #[test]
    fn user_where_input_email_contains_lowers_to_contains_filter() {
        let input: DeriveInput = parse_quote! {
            #[prax(table = "users")]
            pub struct User {
                #[prax(id)]
                pub id: i64,
                #[prax(unique)]
                pub email: String,
            }
        };

        let result = derive_model_impl(&input);
        assert!(
            result.is_ok(),
            "derive_model_impl failed: {:?}",
            result.err()
        );
        let code = result.unwrap().to_string();

        // The lowering for email (String column) must call into_filter("email").
        assert!(
            code.contains("\"email\""),
            "expected column name \"email\" in the into_ir lowering"
        );
        // The lowering body must check for Filter::None and push.
        assert!(
            code.contains("into_filter"),
            "expected ScalarFilter::into_filter call in into_ir"
        );
    }

    // ── order_by_input codegen tests ──────────────────────────────────────

    #[test]
    fn user_order_by_emits_per_column_variants() {
        let input: DeriveInput = parse_quote! {
            #[prax(table = "users")]
            pub struct User {
                #[prax(id)]
                pub id: i64,
                pub email: String,
                pub name: Option<String>,
            }
        };
        let result = derive_model_impl(&input);
        assert!(result.is_ok(), "derive_model_impl failed");
        let code = result.unwrap().to_string();

        // UserOrderBy enum with a variant per sortable column.
        assert!(
            code.contains("UserOrderBy"),
            "expected UserOrderBy enum in generated code"
        );
        assert!(
            code.contains("OrderByInput"),
            "expected OrderByInput trait impl"
        );
        // Variants per column.
        assert!(code.contains("Id"), "expected Id variant");
        assert!(code.contains("Email"), "expected Email variant");
        assert!(code.contains("Name"), "expected Name variant");
        // SortOrder must be referenced as the variant payload.
        assert!(code.contains("SortOrder"), "expected SortOrder payload");
    }

    // ── create_input codegen tests ────────────────────────────────────────

    #[test]
    fn user_create_input_emits_required_and_optional_fields() {
        let input: DeriveInput = parse_quote! {
            #[prax(table = "users")]
            pub struct User {
                #[prax(id, auto)]
                pub id: i64,
                #[prax(unique)]
                pub email: String,
                pub name: Option<String>,
                pub age: Option<i32>,
                pub active: bool,
            }
        };
        let result = derive_model_impl(&input);
        assert!(result.is_ok(), "derive_model_impl failed");
        let code = result.unwrap().to_string();

        // UserCreateInput must be present.
        assert!(
            code.contains("UserCreateInput"),
            "expected UserCreateInput struct in generated code"
        );
        // id is @id @auto — must NOT appear in CreateInput (DB-generated).
        // Confirm UserCreateInput doesn't surround a literal "id" payload field;
        // this is a stringy check — accept some flexibility.
        // Required scalar fields appear without Option wrap.
        // (Token-stream test: just confirm the field names appear.)
        assert!(
            code.contains("email"),
            "expected email field in UserCreateInput"
        );
        assert!(
            code.contains("active"),
            "expected active field in UserCreateInput"
        );
    }

    // ── relation_meta codegen tests ───────────────────────────────────────

    #[test]
    fn user_with_posts_relation_emits_relation_filter_meta() {
        let input: DeriveInput = parse_quote! {
            #[prax(table = "users")]
            pub struct User {
                #[prax(id)]
                pub id: i64,
                pub email: String,
                #[prax(relation(target = "Post", foreign_key = "author_id"))]
                pub posts: Vec<Post>,
            }
        };
        let result = derive_model_impl(&input);
        assert!(
            result.is_ok(),
            "derive_model_impl failed: {:?}",
            result.err()
        );
        let code = result.unwrap().to_string();

        // Marker struct and impl must be present.
        assert!(
            code.contains("UserPostsFilterMeta"),
            "expected UserPostsFilterMeta marker"
        );
        assert!(
            code.contains("RelationFilterMeta"),
            "expected RelationFilterMeta trait impl"
        );
        // Table + key constants.
        assert!(
            code.contains("\"users\""),
            "expected \"users\" as PARENT_TABLE"
        );
        assert!(
            code.contains("\"post\""),
            "expected \"post\" as CHILD_TABLE (snake_case of target \"Post\")"
        );
        assert!(
            code.contains("\"author_id\""),
            "expected \"author_id\" as CHILD_FK"
        );
        assert!(code.contains("\"id\""), "expected \"id\" as PARENT_PK");
    }

    // ── update_input codegen tests ────────────────────────────────────────

    #[test]
    fn user_update_input_emits_field_update_wrappers() {
        let input: DeriveInput = parse_quote! {
            #[prax(table = "users")]
            pub struct User {
                #[prax(id, auto)]
                pub id: i64,
                #[prax(unique)]
                pub email: String,
                pub name: Option<String>,
                pub age: Option<i32>,
                pub active: bool,
            }
        };
        let result = derive_model_impl(&input);
        assert!(result.is_ok(), "derive_model_impl failed");
        let code = result.unwrap().to_string();

        // UserUpdateInput must be present.
        assert!(
            code.contains("UserUpdateInput"),
            "expected UserUpdateInput struct in generated code"
        );
        // Update wrappers per field type.
        assert!(
            code.contains("StringFieldUpdate"),
            "expected StringFieldUpdate for email"
        );
        assert!(
            code.contains("StringNullableFieldUpdate"),
            "expected StringNullableFieldUpdate for name"
        );
        assert!(
            code.contains("IntNullableFieldUpdate"),
            "expected IntNullableFieldUpdate for age"
        );
        assert!(
            code.contains("BoolFieldUpdate"),
            "expected BoolFieldUpdate for active"
        );
    }

    // ── count_struct codegen tests ────────────────────────────────────────

    /// A model with outgoing relations must emit `<Model>Count` with one
    /// `Option<i64>` field per relation.
    #[test]
    fn user_with_relations_emits_count_struct() {
        let input: DeriveInput = parse_quote! {
            #[prax(table = "users")]
            pub struct User {
                #[prax(id)]
                pub id: i64,
                pub email: String,
                #[prax(relation(target = "Post", foreign_key = "author_id"))]
                pub posts: Vec<Post>,
                #[prax(relation(target = "Comment", foreign_key = "user_id"))]
                pub comments: Vec<Comment>,
            }
        };
        let result = derive_model_impl(&input);
        assert!(
            result.is_ok(),
            "derive_model_impl failed: {:?}",
            result.err()
        );
        let code = result.unwrap().to_string();

        // The `UserCount` struct must be present.
        assert!(
            code.contains("UserCount"),
            "expected UserCount struct in generated code; got:\n{code}"
        );
        // Each relation produces an `Option<i64>` field.
        assert!(
            code.contains("pub posts"),
            "expected `pub posts` field on UserCount"
        );
        assert!(
            code.contains("pub comments"),
            "expected `pub comments` field on UserCount"
        );
        // `Option < i64 >` (token-stream spacing) must appear.
        assert!(
            code.contains("Option < i64 >"),
            "expected Option<i64> field type on UserCount fields"
        );
        // Default derive must be present so UserCount::default() works.
        assert!(
            code.contains("Default"),
            "expected #[derive(Default)] on UserCount"
        );
    }

    /// A model with **no** outgoing relations must NOT emit `<Model>Count`.
    #[test]
    fn model_without_relations_does_not_emit_count_struct() {
        let input: DeriveInput = parse_quote! {
            #[prax(table = "posts")]
            pub struct Post {
                #[prax(id)]
                pub id: i64,
                pub title: String,
            }
        };
        let result = derive_model_impl(&input);
        assert!(
            result.is_ok(),
            "derive_model_impl failed: {:?}",
            result.err()
        );
        let code = result.unwrap().to_string();

        // The phase-5.5 relation-count substruct `struct PostCount { ... }`
        // must NOT be emitted for a relation-free model. Note the trailing
        // space discriminates it from the phase-6 aggregate types
        // `PostCountSelect` / `PostCountSelectResult`, which ARE emitted for
        // every model (count! / aggregate! work regardless of relations).
        assert!(
            !code.contains("struct PostCount "),
            "unexpected relation-count `struct PostCount` for a relation-free model"
        );
    }

    /// `<Model>Count` fields must match the relation field names exactly —
    /// not the target model names.
    #[test]
    fn count_struct_uses_field_names_not_target_model_names() {
        let input: DeriveInput = parse_quote! {
            #[prax(table = "authors")]
            pub struct Author {
                #[prax(id)]
                pub id: i64,
                pub name: String,
                // Field name is `articles`, target model is `Post`
                #[prax(relation(target = "Post", foreign_key = "author_id"))]
                pub articles: Vec<Post>,
            }
        };
        let result = derive_model_impl(&input);
        assert!(
            result.is_ok(),
            "derive_model_impl failed: {:?}",
            result.err()
        );
        let code = result.unwrap().to_string();

        assert!(
            code.contains("AuthorCount"),
            "expected AuthorCount in generated code"
        );
        // The field name on the count struct is `articles`, not `post` or `Post`.
        assert!(
            code.contains("pub articles"),
            "expected `pub articles` on AuthorCount, not the target model name"
        );
        assert!(
            !code.contains("pub post"),
            "unexpected `pub post` — count struct should use field name, not model name"
        );
    }
}