oas3-gen 0.1.0

A rust type generator for OpenAPI v3.1.x specification.
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
#![allow(dead_code)]

use std::{
  cmp::Ordering,
  collections::{BTreeMap, BTreeSet},
};

use any_ascii::any_ascii;
use inflections::{Inflect, case::to_pascal_case};
use oas3::{
  Spec,
  spec::{ObjectOrReference, ObjectSchema, Operation, Parameter, ParameterIn, Schema, SchemaType, SchemaTypeSet},
};
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use regex::Regex;
use serde_json::Number;

pub fn doc_comment_lines(input: &str) -> Vec<String> {
  let normalized = input.replace("\\n", "\n");
  normalized
    .lines()
    .map(|line| {
      if line.is_empty() {
        "/// ".to_string()
      } else {
        format!("/// {}", line)
      }
    })
    .collect()
}

pub fn doc_comment_block(input: &str) -> String {
  doc_comment_lines(input).join("\n")
}

/// Convert a schema name to a valid Rust identifier (for field names)
pub fn to_rust_ident(name: &str) -> String {
  let cleaned = name.replace(['-', '.', ' '], "_");

  // Check if it's a Rust keyword and prefix with r# if needed
  match cleaned.as_str() {
    "as" | "break" | "const" | "continue" | "crate" | "else" | "enum" | "extern" | "false" | "fn" | "for" | "if"
    | "impl" | "in" | "let" | "loop" | "match" | "mod" | "move" | "mut" | "pub" | "ref" | "return" | "self"
    | "Self" | "static" | "struct" | "super" | "trait" | "true" | "type" | "unsafe" | "use" | "where" | "while"
    | "async" | "await" | "dyn" | "abstract" | "become" | "box" | "do" | "final" | "macro" | "override" | "priv"
    | "typeof" | "unsized" | "virtual" | "yield" | "try" => format!("r#{}", cleaned),
    _ => cleaned,
  }
}

/// Convert a schema name to a valid Rust type name (PascalCase)
pub fn to_rust_type_name(name: &str) -> String {
  let pascal = any_ascii(name).to_pascal_case();

  // Check if it's a Rust keyword and prefix with r# if needed
  match pascal.as_str() {
    "Self" | "Type" => format!("r#{}", pascal),
    _ => pascal,
  }
}

/// Detect if all field names follow a consistent naming pattern
/// Returns the serde rename_all value if consistent, None otherwise
pub fn detect_naming_pattern(fields: &[(String, String)]) -> Option<&'static str> {
  if fields.is_empty() {
    return None;
  }

  // Check if all fields are snake_case
  let all_snake_case = fields.iter().all(|(original, rust_name)| {
    original.contains('_') || original.contains('-') && to_rust_ident(original) == *rust_name
  });

  if all_snake_case {
    return Some("snake_case");
  }

  // Check if all fields are camelCase
  let all_camel_case = fields.iter().all(|(original, _)| {
    original.chars().next().map(|c| c.is_lowercase()).unwrap_or(false) && original.chars().any(|c| c.is_uppercase())
  });

  if all_camel_case {
    return Some("camelCase");
  }

  None
}

#[derive(Debug)]
pub struct SchemaGraph {
  /// All schemas from the OpenAPI spec
  schemas: BTreeMap<String, ObjectSchema>,
  /// Dependency graph: schema_name -> [schemas it references]
  dependencies: BTreeMap<String, BTreeSet<String>>,
  /// Schemas that are part of cycles
  cyclic_schemas: BTreeSet<String>,
  /// Reference to the original spec for resolution
  spec: Spec,
}

impl SchemaGraph {
  pub fn new(spec: Spec) -> anyhow::Result<Self> {
    let mut graph = Self {
      schemas: BTreeMap::new(),
      dependencies: BTreeMap::new(),
      cyclic_schemas: BTreeSet::new(),
      spec,
    };

    // Extract all schemas from components/schemas
    if let Some(components) = &graph.spec.components {
      for (name, schema_ref) in &components.schemas {
        if let Ok(schema) = schema_ref.resolve(&graph.spec) {
          graph.schemas.insert(name.clone(), schema);
        }
      }
    }

    Ok(graph)
  }

  /// Get a schema by name
  pub fn get_schema(&self, name: &str) -> Option<&ObjectSchema> {
    self.schemas.get(name)
  }

  /// Get all schema names
  pub fn schema_names(&self) -> Vec<&String> {
    self.schemas.keys().collect()
  }

  /// Get the spec reference
  pub fn spec(&self) -> &Spec {
    &self.spec
  }

  /// Extract schema name from a $ref string
  pub fn extract_ref_name(ref_string: &str) -> Option<String> {
    // Format: "#/components/schemas/SchemaName"
    ref_string.strip_prefix("#/components/schemas/").map(|s| s.to_string())
  }

  /// Build the dependency graph by analyzing all schema references
  pub fn build_dependencies(&mut self) {
    let schema_names: Vec<String> = self.schemas.keys().cloned().collect();

    for schema_name in schema_names {
      let mut deps = BTreeSet::new();
      if let Some(schema) = self.schemas.get(&schema_name) {
        self.collect_dependencies(schema, &mut deps);
      }
      self.dependencies.insert(schema_name, deps);
    }
  }

  /// Recursively collect all schema dependencies from a schema
  fn collect_dependencies(&self, schema: &ObjectSchema, deps: &mut BTreeSet<String>) {
    // Check properties
    for prop_schema in schema.properties.values() {
      // Try to resolve the property schema and extract dependencies
      if let Ok(resolved) = prop_schema.resolve(&self.spec) {
        // Check if this is a reference to another schema by looking at the title
        if let Some(ref title) = resolved.title {
          deps.insert(title.clone());
        }
        // Recursively collect from inline schemas
        self.collect_dependencies(&resolved, deps);
      }
    }

    // Check oneOf
    for one_of_schema in &schema.one_of {
      if let Ok(resolved) = one_of_schema.resolve(&self.spec) {
        if let Some(ref title) = resolved.title {
          deps.insert(title.clone());
        }
        self.collect_dependencies(&resolved, deps);
      }
    }

    // Check anyOf
    for any_of_schema in &schema.any_of {
      if let Ok(resolved) = any_of_schema.resolve(&self.spec) {
        if let Some(ref title) = resolved.title {
          deps.insert(title.clone());
        }
        self.collect_dependencies(&resolved, deps);
      }
    }

    // Check allOf
    for all_of_schema in &schema.all_of {
      if let Ok(resolved) = all_of_schema.resolve(&self.spec) {
        if let Some(ref title) = resolved.title {
          deps.insert(title.clone());
        }
        self.collect_dependencies(&resolved, deps);
      }
    }
  }

  /// Detect cycles in the schema dependency graph using DFS
  pub fn detect_cycles(&mut self) -> Vec<Vec<String>> {
    let mut visited = BTreeSet::new();
    let mut rec_stack = BTreeSet::new();
    let mut cycles = Vec::new();
    let mut path = Vec::new();

    let schema_names: Vec<String> = self.schemas.keys().cloned().collect();

    for schema_name in schema_names {
      if !visited.contains(&schema_name) {
        self.dfs_detect_cycle(&schema_name, &mut visited, &mut rec_stack, &mut path, &mut cycles);
      }
    }

    // Mark all schemas involved in cycles
    for cycle in &cycles {
      for schema_name in cycle {
        self.cyclic_schemas.insert(schema_name.clone());
      }
    }

    cycles
  }

  /// DFS helper for cycle detection
  fn dfs_detect_cycle(
    &self,
    node: &str,
    visited: &mut BTreeSet<String>,
    rec_stack: &mut BTreeSet<String>,
    path: &mut Vec<String>,
    cycles: &mut Vec<Vec<String>>,
  ) {
    visited.insert(node.to_string());
    rec_stack.insert(node.to_string());
    path.push(node.to_string());

    if let Some(deps) = self.dependencies.get(node) {
      for dep in deps {
        if !visited.contains(dep) {
          self.dfs_detect_cycle(dep, visited, rec_stack, path, cycles);
        } else if rec_stack.contains(dep) {
          // Found a cycle! Extract the cycle from the path
          if let Some(cycle_start) = path.iter().position(|n| n == dep) {
            let cycle: Vec<String> = path[cycle_start..].to_vec();
            cycles.push(cycle);
          }
        }
      }
    }

    path.pop();
    rec_stack.remove(node);
  }

  /// Check if a schema is part of a cycle
  pub fn is_cyclic(&self, schema_name: &str) -> bool {
    self.cyclic_schemas.contains(schema_name)
  }

  /// Get dependencies of a schema
  pub fn get_dependencies(&self, schema_name: &str) -> Option<&BTreeSet<String>> {
    self.dependencies.get(schema_name)
  }
}

#[derive(Debug, Clone)]
pub enum RustType {
  Struct(StructDef),
  Enum(EnumDef),
  TypeAlias(TypeAliasDef),
}

impl RustType {
  pub fn type_name(&self) -> &str {
    match self {
      RustType::Struct(def) => &def.name,
      RustType::Enum(def) => &def.name,
      RustType::TypeAlias(def) => &def.name,
    }
  }
}

/// Metadata about an API operation (for tracking, not direct code generation)
#[derive(Debug, Clone)]
pub struct OperationInfo {
  pub operation_id: String,
  pub method: String,
  pub path: String,
  pub summary: Option<String>,
  pub description: Option<String>,
  pub request_type: Option<String>,
  pub response_type: Option<String>,
}

#[derive(Debug, Clone)]
pub struct StructDef {
  pub name: String,
  pub docs: Vec<String>,
  pub fields: Vec<FieldDef>,
  pub derives: Vec<String>,
  pub serde_attrs: Vec<String>,
}

#[derive(Debug, Clone)]
pub struct FieldDef {
  pub name: String,
  pub docs: Vec<String>,
  pub rust_type: TypeRef,
  pub optional: bool,
  pub serde_attrs: Vec<String>,
  pub validation_attrs: Vec<String>,
  pub regex_validation: Option<String>,
  pub default_value: Option<serde_json::Value>,
}

#[derive(Debug, Clone)]
pub struct TypeRef {
  pub base_type: String,
  pub boxed: bool,
  pub nullable: bool,
  pub is_array: bool,
}

impl TypeRef {
  pub fn new(base_type: impl Into<String>) -> Self {
    Self {
      base_type: base_type.into(),
      boxed: false,
      nullable: false,
      is_array: false,
    }
  }

  pub fn with_option(mut self) -> Self {
    self.nullable = true;
    self
  }

  pub fn with_vec(mut self) -> Self {
    self.is_array = true;
    self
  }

  pub fn with_boxed(mut self) -> Self {
    self.boxed = true;
    self
  }

  /// Get the full Rust type string
  pub fn to_rust_type(&self) -> String {
    let mut result = self.base_type.clone();

    if self.boxed {
      result = format!("Box<{}>", result);
    }

    if self.is_array {
      result = format!("Vec<{}>", result);
    }

    if self.nullable {
      result = format!("Option<{}>", result);
    }

    result
  }
}

#[derive(Debug, Clone)]
pub struct EnumDef {
  pub name: String,
  pub docs: Vec<String>,
  pub variants: Vec<VariantDef>,
  pub discriminator: Option<String>,
  pub derives: Vec<String>,
  pub serde_attrs: Vec<String>,
}

#[derive(Debug, Clone)]
pub struct VariantDef {
  pub name: String,
  pub docs: Vec<String>,
  pub content: VariantContent,
  pub serde_attrs: Vec<String>,
}

#[derive(Debug, Clone)]
pub enum VariantContent {
  Unit,
  Tuple(Vec<TypeRef>),
  Struct(Vec<FieldDef>),
}

#[derive(Debug, Clone)]
pub struct TypeAliasDef {
  pub name: String,
  pub docs: Vec<String>,
  pub target: TypeRef,
}

// ============================================================================
// Schema to AST Converter
// ============================================================================

pub struct SchemaConverter<'a> {
  graph: &'a SchemaGraph,
}

impl<'a> SchemaConverter<'a> {
  pub fn new(graph: &'a SchemaGraph) -> Self {
    Self { graph }
  }

  /// Convert a schema to Rust type definitions
  /// Returns the main type and any inline types that were generated
  pub fn convert_schema(&self, name: &str, schema: &ObjectSchema) -> anyhow::Result<Vec<RustType>> {
    // Determine the type of Rust definition we need to create

    // Check if this is an enum (oneOf/anyOf)
    if !schema.one_of.is_empty() {
      return Ok(vec![self.convert_one_of_enum(name, schema)?]);
    }

    if !schema.any_of.is_empty() {
      return Ok(vec![self.convert_any_of_enum(name, schema)?]);
    }

    // Check if this is a simple enum (string with enum values)
    if !schema.enum_values.is_empty() {
      return Ok(vec![self.convert_simple_enum(name, schema, &schema.enum_values)?]);
    }

    // Check if this is a struct (object with properties)
    if !schema.properties.is_empty() {
      let (main_type, inline_types) = self.convert_struct(name, schema)?;
      let mut all_types = vec![main_type];
      all_types.extend(inline_types);
      return Ok(all_types);
    }

    // Otherwise, might be a type alias or something we can skip
    Ok(vec![])
  }

  /// Convert a schema with oneOf into a Rust enum
  fn convert_one_of_enum(&self, name: &str, schema: &ObjectSchema) -> anyhow::Result<RustType> {
    let mut variants = Vec::new();
    let mut seen_names = BTreeSet::new();

    // Get discriminator property name if present
    let discriminator_property = schema.discriminator.as_ref().map(|d| d.property_name.as_str());

    for (i, variant_schema_ref) in schema.one_of.iter().enumerate() {
      if let Ok(variant_schema) = variant_schema_ref.resolve(self.graph.spec()) {
        // Skip null variants - they're handled by making the field Option<T>
        if variant_schema.schema_type == Some(SchemaTypeSet::Single(SchemaType::Null)) {
          continue;
        }

        // Generate a good variant name
        let mut variant_name = if let Some(ref title) = variant_schema.title {
          to_pascal_case(title)
        } else {
          // Infer name from type
          self.infer_variant_name(&variant_schema, i)
        };

        // Ensure uniqueness
        if seen_names.contains(&variant_name) {
          variant_name = format!("{}{}", variant_name, i);
        }
        seen_names.insert(variant_name.clone());

        let docs = variant_schema
          .description
          .as_ref()
          .map(|d| doc_comment_lines(d))
          .unwrap_or_default();

        // Determine the variant content based on the schema type
        // For discriminated unions (with tag), we MUST use struct variants (serde requirement)
        // For non-discriminated (untagged), we can use tuple variants to avoid duplication
        let content = if discriminator_property.is_some() {
          // Has discriminator - must use struct variant for serde(tag) to work
          if !variant_schema.properties.is_empty() {
            let fields = self.convert_fields_with_exclusions(&variant_schema, discriminator_property)?;
            VariantContent::Struct(fields)
          } else {
            // Primitive or non-object - tuple variant
            let type_ref = self.schema_to_type_ref(&variant_schema)?;
            VariantContent::Tuple(vec![type_ref])
          }
        } else {
          // No discriminator - can use tuple variants to avoid duplication
          if let Some(ref title) = variant_schema.title {
            if self.graph.get_schema(title).is_some() {
              // Reference to existing schema - use tuple variant
              let type_ref = TypeRef::new(to_rust_type_name(title));
              VariantContent::Tuple(vec![type_ref])
            } else if !variant_schema.properties.is_empty() {
              // Inline object - struct variant
              let fields = self.convert_fields(&variant_schema)?;
              VariantContent::Struct(fields)
            } else {
              // Other types - tuple variant
              let type_ref = self.schema_to_type_ref(&variant_schema)?;
              VariantContent::Tuple(vec![type_ref])
            }
          } else if !variant_schema.properties.is_empty() {
            // Anonymous object - inline struct variant
            let fields = self.convert_fields(&variant_schema)?;
            VariantContent::Struct(fields)
          } else {
            // Primitive - tuple variant
            let type_ref = self.schema_to_type_ref(&variant_schema)?;
            VariantContent::Tuple(vec![type_ref])
          }
        };

        variants.push(VariantDef {
          name: to_rust_ident(&variant_name),
          docs,
          content,
          serde_attrs: vec![],
        });
      }
    }

    // Check if there's a discriminator
    let discriminator = schema.discriminator.as_ref().map(|d| d.property_name.clone());

    Ok(RustType::Enum(EnumDef {
      name: to_rust_type_name(name),
      docs: schema
        .description
        .as_ref()
        .map(|d| doc_comment_lines(d))
        .unwrap_or_default(),
      variants,
      discriminator,
      derives: vec!["Debug".into(), "Clone".into(), "Serialize".into(), "Deserialize".into()],
      serde_attrs: vec![],
    }))
  }

  /// Convert a schema with anyOf into an untagged Rust enum
  fn convert_any_of_enum(&self, name: &str, schema: &ObjectSchema) -> anyhow::Result<RustType> {
    // Check if this is a string enum with const values pattern (common for forward-compatible enums)
    let has_freeform_string = schema.any_of.iter().any(|s| {
      if let Ok(resolved) = s.resolve(self.graph.spec()) {
        resolved.const_value.is_none() && resolved.schema_type == Some(SchemaTypeSet::Single(SchemaType::String))
      } else {
        false
      }
    });

    let const_values: Vec<_> = schema
      .any_of
      .iter()
      .filter_map(|s| {
        if let Ok(resolved) = s.resolve(self.graph.spec()) {
          resolved
            .const_value
            .as_ref()
            .map(|v| (v.clone(), resolved.description.clone()))
        } else {
          None
        }
      })
      .collect();

    // Special case: freeform string + const values = forward-compatible enum
    if has_freeform_string && !const_values.is_empty() {
      return self.convert_string_enum_with_catchall(name, schema, &const_values);
    }

    // Otherwise, treat as a regular untagged enum
    let mut variants = Vec::new();
    let mut seen_names = BTreeSet::new();

    for (i, variant_schema_ref) in schema.any_of.iter().enumerate() {
      if let Ok(variant_schema) = variant_schema_ref.resolve(self.graph.spec()) {
        // Skip null variants - they're handled by making the field Option<T>
        if variant_schema.schema_type == Some(SchemaTypeSet::Single(SchemaType::Null)) {
          continue;
        }

        // Generate a good variant name
        let mut variant_name = if let Some(ref title) = variant_schema.title {
          to_pascal_case(title)
        } else {
          // Infer name from type
          self.infer_variant_name(&variant_schema, i)
        };

        // Ensure uniqueness
        if seen_names.contains(&variant_name) {
          variant_name = format!("{}{}", variant_name, i);
        }
        seen_names.insert(variant_name.clone());

        let docs = variant_schema
          .description
          .as_ref()
          .map(|d| doc_comment_lines(d))
          .unwrap_or_default();

        // Determine variant content - prefer tuple variants for existing schemas
        let content = if let Some(ref title) = variant_schema.title {
          // If this variant has a title and matches an existing schema, use tuple variant
          if self.graph.get_schema(title).is_some() {
            let type_ref = TypeRef::new(to_rust_type_name(title));
            VariantContent::Tuple(vec![type_ref])
          } else if !variant_schema.properties.is_empty() {
            // Inline object without matching schema - create struct variant
            let fields = self.convert_fields(&variant_schema)?;
            VariantContent::Struct(fields)
          } else {
            // Other types - tuple variant
            let type_ref = self.schema_to_type_ref(&variant_schema)?;
            VariantContent::Tuple(vec![type_ref])
          }
        } else if !variant_schema.properties.is_empty() {
          // Anonymous object - create inline struct variant
          let fields = self.convert_fields(&variant_schema)?;
          VariantContent::Struct(fields)
        } else {
          // Not an object - create tuple variant wrapping the type
          let type_ref = self.schema_to_type_ref(&variant_schema)?;
          VariantContent::Tuple(vec![type_ref])
        };

        variants.push(VariantDef {
          name: to_rust_ident(&variant_name),
          docs,
          content,
          serde_attrs: vec![],
        });
      }
    }

    Ok(RustType::Enum(EnumDef {
      name: to_rust_type_name(name),
      docs: schema
        .description
        .as_ref()
        .map(|d| doc_comment_lines(d))
        .unwrap_or_default(),
      variants,
      discriminator: None,
      derives: vec!["Debug".into(), "Clone".into(), "Serialize".into(), "Deserialize".into()],
      serde_attrs: vec!["untagged".into()],
    }))
  }

  /// Convert a string enum with const values + a catch-all for unknown strings
  fn convert_string_enum_with_catchall(
    &self,
    name: &str,
    schema: &ObjectSchema,
    const_values: &[(serde_json::Value, Option<String>)],
  ) -> anyhow::Result<RustType> {
    let mut variants = Vec::new();

    // Add a variant for each const value
    for (value, description) in const_values {
      if let Some(str_val) = value.as_str() {
        // Convert the const value to a variant name
        // e.g., "claude-3-7-sonnet-latest" -> "Claude37SonnetLatest"
        let variant_name = to_pascal_case(&str_val.replace(['-', '.'], "_"));

        let docs = description.as_ref().map(|d| doc_comment_lines(d)).unwrap_or_default();

        variants.push(VariantDef {
          name: variant_name,
          docs,
          content: VariantContent::Unit,
          serde_attrs: vec![format!("rename = \"{}\"", str_val)],
        });
      }
    }

    // Add the catch-all variant for unknown strings
    variants.push(VariantDef {
      name: "Other".to_string(),
      docs: vec!["/// Any other string value".to_string()],
      content: VariantContent::Tuple(vec![TypeRef::new("String")]),
      serde_attrs: vec!["untagged".to_string()],
    });

    Ok(RustType::Enum(EnumDef {
      name: to_rust_ident(name),
      docs: schema
        .description
        .as_ref()
        .map(|d| doc_comment_lines(d))
        .unwrap_or_default(),
      variants,
      discriminator: None,
      derives: vec![
        "Debug".into(),
        "Clone".into(),
        "PartialEq".into(),
        "Eq".into(),
        "Serialize".into(),
        "Deserialize".into(),
      ],
      serde_attrs: vec![],
    }))
  }

  /// Infer a variant name from the schema type
  fn infer_variant_name(&self, schema: &ObjectSchema, index: usize) -> String {
    // Check if it's an enum
    if !schema.enum_values.is_empty() {
      return "Enum".to_string();
    }

    // Check the schema type
    if let Some(ref schema_type) = schema.schema_type {
      match schema_type {
        SchemaTypeSet::Single(typ) => match typ {
          SchemaType::String => "String".to_string(),
          SchemaType::Number => "Number".to_string(),
          SchemaType::Integer => "Integer".to_string(),
          SchemaType::Boolean => "Boolean".to_string(),
          SchemaType::Array => "Array".to_string(),
          SchemaType::Object => "Object".to_string(),
          SchemaType::Null => "Null".to_string(),
        },
        SchemaTypeSet::Multiple(_) => "Mixed".to_string(),
      }
    } else {
      // Fallback
      format!("Variant{}", index)
    }
  }

  /// Convert a simple string enum
  fn convert_simple_enum(
    &self,
    name: &str,
    schema: &ObjectSchema,
    enum_values: &[serde_json::Value],
  ) -> anyhow::Result<RustType> {
    let mut variants = Vec::new();

    for value in enum_values {
      if let Some(str_val) = value.as_str() {
        let variant_name = to_pascal_case(str_val);
        variants.push(VariantDef {
          name: variant_name,
          docs: vec![],
          content: VariantContent::Unit,
          serde_attrs: vec![format!("rename = \"{}\"", str_val)],
        });
      }
    }

    Ok(RustType::Enum(EnumDef {
      name: to_rust_ident(name),
      docs: schema
        .description
        .as_ref()
        .map(|d| doc_comment_lines(d))
        .unwrap_or_default(),
      variants,
      discriminator: None,
      derives: vec!["Debug".into(), "Clone".into(), "Serialize".into(), "Deserialize".into()],
      serde_attrs: vec![],
    }))
  }

  /// Convert an object schema to a Rust struct
  /// Returns the struct and any inline types that were generated
  fn convert_struct(&self, name: &str, schema: &ObjectSchema) -> anyhow::Result<(RustType, Vec<RustType>)> {
    let (mut fields, inline_types) = self.convert_fields_with_inline_types(name, schema)?;

    // Detect if all fields follow a consistent naming pattern for serde rename_all
    let mut serde_attrs = vec![];

    // Collect fields that have rename attributes
    let renamed_fields: Vec<_> = fields
      .iter()
      .filter_map(|f| {
        f.serde_attrs
          .iter()
          .find(|attr| attr.starts_with("rename = "))
          .map(|attr| {
            // Extract the original name from rename = "original-name"
            let start = attr.find('"').unwrap() + 1;
            let end = attr.rfind('"').unwrap();
            attr[start..end].to_string()
          })
      })
      .collect();

    // If we have renamed fields, check if they all follow a consistent pattern
    if !renamed_fields.is_empty() {
      let all_kebab = renamed_fields.iter().all(|name| name.contains('-'));

      if all_kebab {
        // Use rename_all = "kebab-case" for all fields
        // serde will automatically convert snake_case Rust names to kebab-case
        serde_attrs.push("rename_all = \"kebab-case\"".to_string());

        // Remove individual rename attributes since rename_all handles it
        for field in &mut fields {
          field.serde_attrs.retain(|attr| !attr.starts_with("rename = "));
        }
      }
    }

    // Only add serde(default) at struct level if ALL fields have defaults or are Option/Vec
    // Otherwise we get compilation errors when trying to Default::default() complex types
    let all_fields_defaultable = fields.iter().all(|f| {
      f.default_value.is_some()
        || f.rust_type.nullable
        || f.rust_type.is_array
        || matches!(
          f.rust_type.base_type.as_str(),
          "String"
            | "bool"
            | "i8"
            | "i16"
            | "i32"
            | "i64"
            | "i128"
            | "isize"
            | "u8"
            | "u16"
            | "u32"
            | "u64"
            | "u128"
            | "usize"
            | "f32"
            | "f64"
            | "serde_json::Value"
        )
    });

    if all_fields_defaultable && fields.iter().any(|f| f.default_value.is_some()) {
      serde_attrs.push("default".to_string());
    }

    let struct_type = RustType::Struct(StructDef {
      name: to_rust_type_name(name),
      docs: schema
        .description
        .as_ref()
        .map(|d| doc_comment_lines(d))
        .unwrap_or_default(),
      fields,
      derives: vec![
        "Debug".into(),
        "Clone".into(),
        "Serialize".into(),
        "Deserialize".into(),
        "Validate".into(),
      ],
      serde_attrs,
    });

    Ok((struct_type, inline_types))
  }

  fn extract_validation_pattern<'s>(&self, prop_name: &str, schema: &'s ObjectSchema) -> Option<&'s String> {
    match (schema.schema_type.as_ref(), schema.pattern.as_ref()) {
      (Some(SchemaTypeSet::Single(SchemaType::String)), Some(pattern)) => {
        if Regex::new(pattern).is_ok() {
          Some(pattern)
        } else {
          eprintln!(
            "Warning: Invalid regex pattern '{}' for property '{}'",
            pattern, prop_name
          );
          None
        }
      }
      _ => None,
    }
  }

  fn render_number(is_float: bool, num: &Number) -> String {
    if is_float {
      if num.to_string().contains(".") {
        num.to_string()
      } else {
        format!("{}.0", num)
      }
    } else {
      format!("{}i64", num.as_i64().unwrap_or_default())
    }
  }

  /// Extract validation attributes from an OpenAPI schema
  fn extract_validation_attrs(&self, _prop_name: &str, is_required: bool, schema: &ObjectSchema) -> Vec<String> {
    let mut attrs = Vec::new();

    // Handle format-based validation
    if let Some(ref format) = schema.format {
      match format.as_str() {
        "email" => attrs.push("email".to_string()),
        "uri" | "url" => attrs.push("url".to_string()),
        _ => {}
      }
    }

    if let Some(ref schema_type) = schema.schema_type {
      if matches!(
        schema_type,
        SchemaTypeSet::Single(SchemaType::Number) | SchemaTypeSet::Single(SchemaType::Integer)
      ) {
        let mut parts = Vec::<String>::new();
        let is_float = matches!(schema_type, SchemaTypeSet::Single(SchemaType::Number));

        // exclusive_minimum
        if let Some(exclusive_min) = schema
          .exclusive_minimum
          .as_ref()
          .map(|v| format!("exclusive_min = {}", Self::render_number(is_float, v)))
        {
          parts.push(exclusive_min);
        }

        // exclusive_maximum
        if let Some(exclusive_max) = schema
          .exclusive_maximum
          .as_ref()
          .map(|v| format!("exclusive_max = {}", Self::render_number(is_float, v)))
        {
          parts.push(exclusive_max);
        }

        // minimum
        if let Some(min) = schema
          .minimum
          .as_ref()
          .map(|v| format!("min = {}", Self::render_number(is_float, v)))
        {
          parts.push(min);
        }

        // maximum
        if let Some(max) = schema
          .maximum
          .as_ref()
          .map(|v| format!("max = {}", Self::render_number(is_float, v)))
        {
          parts.push(max);
        }

        if !parts.is_empty() {
          attrs.push(format!("range({})", parts.join(", ")));
        }
      }

      // string length validation
      if matches!(schema_type, SchemaTypeSet::Single(SchemaType::String)) && schema.enum_values.is_empty() {
        if let (Some(min), Some(max)) = (schema.min_length, schema.max_length) {
          attrs.push(format!("length(min = {min}, max = {max})"));
        } else if let Some(min) = schema.min_length {
          attrs.push(format!("length(min = {min})"));
        } else if let Some(max) = schema.max_length {
          attrs.push(format!("length(max = {max})"));
        } else if is_required {
          // Require non-empty string for required fields
          attrs.push("length(min = 1)".to_string());
        }
      }

      // array length validation
      if matches!(schema_type, SchemaTypeSet::Single(SchemaType::Array)) {
        if let (Some(min), Some(max)) = (schema.min_items, schema.max_items) {
          attrs.push(format!("length(min = {min}, max = {max})"));
        } else if let Some(min) = schema.min_items {
          attrs.push(format!("length(min = {min})"));
        } else if let Some(max) = schema.max_items {
          attrs.push(format!("length(max = {max})"));
        }
      }
    }

    attrs
  }

  /// Extract default value from an OpenAPI schema
  fn extract_default_value(&self, schema: &ObjectSchema) -> Option<serde_json::Value> {
    schema.default.clone()
  }

  /// Convert schema properties to struct fields, excluding specified field names
  fn convert_fields_with_exclusions(
    &self,
    schema: &ObjectSchema,
    exclude_field: Option<&str>,
  ) -> anyhow::Result<Vec<FieldDef>> {
    let mut fields = Vec::new();

    let mut properties: Vec<_> = schema.properties.iter().collect();
    properties.sort_by(|(a, _), (b, _)| a.cmp(b));

    for (prop_name, prop_schema_ref) in properties {
      // Skip excluded fields (e.g., discriminator fields)
      if let Some(exclude) = exclude_field
        && prop_name == exclude
      {
        continue;
      }

      // Check if this is a direct $ref first
      let rust_type = if let ObjectOrReference::Ref { ref_path, .. } = prop_schema_ref {
        // Extract type name directly from the reference
        if let Some(ref_name) = SchemaGraph::extract_ref_name(ref_path) {
          TypeRef::new(to_rust_type_name(&ref_name))
        } else {
          // Fallback to resolution
          if let Ok(prop_schema) = prop_schema_ref.resolve(self.graph.spec()) {
            self.schema_to_type_ref(&prop_schema)?
          } else {
            TypeRef::new("serde_json::Value")
          }
        }
      } else {
        // Inline schema - resolve and convert
        if let Ok(prop_schema) = prop_schema_ref.resolve(self.graph.spec()) {
          self.schema_to_type_ref(&prop_schema)?
        } else {
          TypeRef::new("serde_json::Value")
        }
      };

      let is_required = schema.required.contains(prop_name);
      let optional = !is_required;

      let mut serde_attrs = vec![];
      // Add rename if the property name is not a valid Rust identifier or uses snake_case
      if prop_name.contains('-') || prop_name.contains('.') {
        serde_attrs.push(format!("rename = \"{}\"", prop_name));
      }

      // Add skip_serializing_if for optional fields
      if optional {
        serde_attrs.push("skip_serializing_if = \"Option::is_none\"".to_string());
      }

      // Extract validation attributes and default value from resolved schema
      let (docs, validation_attrs, regex_validation, default_value) =
        if let Ok(prop_schema) = prop_schema_ref.resolve(self.graph.spec()) {
          let docs = prop_schema
            .description
            .as_ref()
            .map(|d| doc_comment_lines(d))
            .unwrap_or_default();
          let validation = self.extract_validation_attrs(prop_name, is_required, &prop_schema);
          let regex_validation = self.extract_validation_pattern(prop_name, &prop_schema);
          let default = self.extract_default_value(&prop_schema);
          (docs, validation, regex_validation.cloned(), default)
        } else {
          (vec![], vec![], None, None)
        };

      // Don't double-wrap: if the type is already nullable, don't wrap again
      let final_type = if optional && !rust_type.nullable {
        rust_type.with_option()
      } else {
        rust_type
      };

      fields.push(FieldDef {
        name: to_rust_ident(prop_name),
        docs,
        rust_type: final_type,
        optional,
        serde_attrs,
        validation_attrs,
        regex_validation,
        default_value,
      });
    }

    Ok(fields)
  }

  /// Convert schema properties to struct fields (convenience wrapper)
  fn convert_fields(&self, schema: &ObjectSchema) -> anyhow::Result<Vec<FieldDef>> {
    self.convert_fields_with_exclusions(schema, None)
  }

  /// Convert schema properties to struct fields, generating inline enum types for anyOf unions
  fn convert_fields_with_inline_types(
    &self,
    parent_name: &str,
    schema: &ObjectSchema,
  ) -> anyhow::Result<(Vec<FieldDef>, Vec<RustType>)> {
    let mut fields = Vec::new();
    let mut inline_types = Vec::new();

    let mut properties: Vec<_> = schema.properties.iter().collect();
    properties.sort_by(|(a, _), (b, _)| a.cmp(b));

    for (prop_name, prop_schema_ref) in properties {
      // Check if this is a direct $ref first
      let rust_type = if let ObjectOrReference::Ref { ref_path, .. } = prop_schema_ref {
        // Extract type name directly from the reference
        if let Some(ref_name) = SchemaGraph::extract_ref_name(ref_path) {
          TypeRef::new(to_rust_type_name(&ref_name))
        } else {
          // Fallback to resolution
          if let Ok(prop_schema) = prop_schema_ref.resolve(self.graph.spec()) {
            self.schema_to_type_ref(&prop_schema)?
          } else {
            TypeRef::new("serde_json::Value")
          }
        }
      } else {
        // Inline schema - resolve and convert
        if let Ok(prop_schema) = prop_schema_ref.resolve(self.graph.spec()) {
          // Special handling for inline anyOf unions
          // Check if this is just a nullable pattern (anyOf with null)
          let has_null = prop_schema.any_of.iter().any(|v| {
            if let Ok(resolved) = v.resolve(self.graph.spec()) {
              resolved.schema_type == Some(SchemaTypeSet::Single(SchemaType::Null))
            } else {
              false
            }
          });

          // If anyOf has null and exactly 2 variants, it's just an optional type

          if !prop_schema.any_of.is_empty() && has_null && prop_schema.any_of.len() == 2 {
            // Extract the non-null type
            let mut found_type = None;
            for variant_ref in &prop_schema.any_of {
              // Check if it's a $ref first (before resolving)
              if let ObjectOrReference::Ref { ref_path, .. } = variant_ref {
                if let Some(ref_name) = SchemaGraph::extract_ref_name(ref_path) {
                  found_type = Some(TypeRef::new(to_rust_type_name(&ref_name)));
                  break;
                }
              } else if let Ok(resolved) = variant_ref.resolve(self.graph.spec())
                && resolved.schema_type != Some(SchemaTypeSet::Single(SchemaType::Null))
              {
                // Found the actual type - return it (it will be wrapped in Option later)
                found_type = Some(self.schema_to_type_ref(&resolved)?);
                break;
              }
            }
            // Use found type or fallback
            found_type.unwrap_or_else(|| self.schema_to_type_ref(&prop_schema).unwrap())
          } else if !prop_schema.any_of.is_empty()
            && (prop_schema.title.is_none()
              || prop_schema
                .title
                .as_ref()
                .map(|t| self.graph.get_schema(t).is_none())
                .unwrap_or(true))
          {
            // Generate inline enum for non-nullable anyOf unions
            let enum_name = format!("{}{}", parent_name, to_pascal_case(prop_name));
            let enum_type = self.convert_any_of_enum(&enum_name, &prop_schema)?;
            inline_types.push(enum_type);
            TypeRef::new(to_rust_type_name(&enum_name))
          } else {
            self.schema_to_type_ref(&prop_schema)?
          }
        } else {
          TypeRef::new("serde_json::Value")
        }
      };

      let is_required = schema.required.contains(prop_name);
      let optional = !is_required;

      let mut serde_attrs = vec![];
      // Add rename if the property name is not a valid Rust identifier or uses snake_case
      if prop_name.contains('-') || prop_name.contains('.') {
        serde_attrs.push(format!("rename = \"{}\"", prop_name));
      }

      // Add skip_serializing_if for optional fields
      if optional {
        serde_attrs.push("skip_serializing_if = \"Option::is_none\"".to_string());
      }

      // Extract validation attributes and default value from resolved schema
      let (docs, validation_attrs, regex_validation, default_value) =
        if let Ok(prop_schema) = prop_schema_ref.resolve(self.graph.spec()) {
          let docs = prop_schema
            .description
            .as_ref()
            .map(|d| doc_comment_lines(d))
            .unwrap_or_default();
          let required = schema.required.contains(prop_name);
          let validation = self.extract_validation_attrs(prop_name, required, &prop_schema);
          let regex_validation = self.extract_validation_pattern(prop_name, &prop_schema);
          let default = self.extract_default_value(&prop_schema);
          (docs, validation, regex_validation.cloned(), default)
        } else {
          (vec![], vec![], None, None)
        };

      // Don't double-wrap: if the type is already nullable, don't wrap again
      let final_type = if optional && !rust_type.nullable {
        rust_type.with_option()
      } else {
        rust_type
      };

      fields.push(FieldDef {
        name: to_rust_ident(prop_name),
        docs,
        rust_type: final_type,
        optional,
        serde_attrs,
        validation_attrs,
        regex_validation,
        default_value,
      });
    }

    Ok((fields, inline_types))
  }

  /// Convert an OpenAPI schema to a TypeRef (exposed for OperationConverter)
  pub fn schema_to_type_ref(&self, schema: &ObjectSchema) -> anyhow::Result<TypeRef> {
    // First priority: Check the schema type - if it has a concrete type, use that
    // This prevents title conflicts (e.g., a string field titled "Message" being confused with Message struct)
    if let Some(ref schema_type) = schema.schema_type {
      // If it has a concrete type AND properties/oneOf/anyOf, it might be a complex type
      // Only use title-based lookup for objects without explicit primitive types
      if !matches!(schema_type, SchemaTypeSet::Single(SchemaType::Object)) {
        // It's a primitive type - continue to primitive type handling below
      } else if let Some(ref title) = schema.title
        && self.graph.get_schema(title).is_some()
        && !schema.properties.is_empty()
      {
        // It's an object with a title that matches a schema and has properties
        let is_cyclic = self.graph.is_cyclic(title);
        let mut type_ref = TypeRef::new(to_rust_type_name(title));
        if is_cyclic {
          type_ref = type_ref.with_boxed();
        }
        return Ok(type_ref);
      }
    } else if let Some(ref title) = schema.title
      && self.graph.get_schema(title).is_some()
      && !schema.properties.is_empty()
    {
      // No explicit type, but has title matching a schema and has properties - likely a reference
      let is_cyclic = self.graph.is_cyclic(title);
      let mut type_ref = TypeRef::new(to_rust_type_name(title));
      if is_cyclic {
        type_ref = type_ref.with_boxed();
      }
      return Ok(type_ref);
    }

    // Check for inline oneOf/anyOf - detect nullable pattern
    if !schema.one_of.is_empty() || !schema.any_of.is_empty() {
      let variants = if !schema.one_of.is_empty() {
        &schema.one_of
      } else {
        &schema.any_of
      };

      // Check if this is the nullable pattern: anyOf/oneOf with [T, null]
      let has_null = variants.iter().any(|v| {
        if let Ok(resolved) = v.resolve(self.graph.spec()) {
          resolved.schema_type == Some(SchemaTypeSet::Single(SchemaType::Null))
        } else {
          false
        }
      });

      if has_null && variants.len() == 2 {
        // This is a nullable type - extract the non-null variant
        for variant_ref in variants {
          // Check if it's a direct $ref first
          if let ObjectOrReference::Ref { ref_path, .. } = variant_ref
            && let Some(ref_name) = SchemaGraph::extract_ref_name(ref_path)
          {
            return Ok(TypeRef::new(to_rust_type_name(&ref_name)).with_option());
          }

          // Otherwise resolve
          if let Ok(resolved) = variant_ref.resolve(self.graph.spec()) {
            // Skip null types
            if resolved.schema_type == Some(SchemaTypeSet::Single(SchemaType::Null)) {
              continue;
            }

            // Found the actual type - recurse to get it
            let inner_type = self.schema_to_type_ref(&resolved)?;
            return Ok(inner_type.with_option());
          }
        }
      }

      // Try to extract type from the first non-null, non-string variant (for non-nullable unions)
      // Prefer complex types (arrays, objects) over simple types (strings)
      let mut fallback_type: Option<TypeRef> = None;

      for variant_ref in variants {
        // Check if it's a direct $ref
        if let ObjectOrReference::Ref { ref_path, .. } = variant_ref
          && let Some(ref_name) = SchemaGraph::extract_ref_name(ref_path)
        {
          return Ok(TypeRef::new(to_rust_type_name(&ref_name)));
        }

        // Try resolving
        if let Ok(resolved) = variant_ref.resolve(self.graph.spec()) {
          // Skip null types
          if resolved.schema_type == Some(SchemaTypeSet::Single(SchemaType::Null)) {
            continue;
          }

          // Handle array types specially
          if resolved.schema_type == Some(SchemaTypeSet::Single(SchemaType::Array)) {
            // Check array items for oneOf
            if let Some(ref items_box) = resolved.items
              && let Schema::Object(items_ref) = items_box.as_ref()
              && let Ok(items_schema) = items_ref.resolve(self.graph.spec())
            {
              // Items have oneOf - extract first ref
              if !items_schema.one_of.is_empty() {
                for one_of_ref in &items_schema.one_of {
                  if let ObjectOrReference::Ref { ref_path, .. } = one_of_ref
                    && let Some(ref_name) = SchemaGraph::extract_ref_name(ref_path)
                  {
                    return Ok(TypeRef::new(format!("Vec<{}>", to_rust_type_name(&ref_name))));
                  }
                }
              }
            }
          }

          // Save string types as fallback but prefer arrays/objects
          if resolved.schema_type == Some(SchemaTypeSet::Single(SchemaType::String)) && fallback_type.is_none() {
            fallback_type = Some(TypeRef::new("String"));
            continue;
          }

          // Check for nested oneOf (common pattern)
          if !resolved.one_of.is_empty() {
            for nested_ref in &resolved.one_of {
              if let ObjectOrReference::Ref { ref_path, .. } = nested_ref
                && let Some(ref_name) = SchemaGraph::extract_ref_name(ref_path)
              {
                return Ok(TypeRef::new(to_rust_type_name(&ref_name)));
              }
            }
          }

          // Use title if available
          if let Some(ref variant_title) = resolved.title
            && self.graph.get_schema(variant_title).is_some()
          {
            return Ok(TypeRef::new(to_rust_type_name(variant_title)));
          }
        }
      }

      // Use fallback if we found one
      if let Some(t) = fallback_type {
        return Ok(t);
      }

      // Fall through if we couldn't resolve to a concrete type
    }

    // Check schema type for primitives
    // This handles inline primitive types
    if let Some(ref schema_type) = schema.schema_type {
      match schema_type {
        SchemaTypeSet::Single(typ) => {
          let base_type = match typ {
            SchemaType::String => "String",
            SchemaType::Number => "f64",
            SchemaType::Integer => "i64",
            SchemaType::Boolean => "bool",
            SchemaType::Array => {
              // Handle array items
              if let Some(ref items_box) = schema.items
                && let Schema::Object(items_ref) = items_box.as_ref()
              {
                // Check if this is a $ref first
                if let ObjectOrReference::Ref { ref_path, .. } = items_ref.as_ref() {
                  // Extract the type name from the reference
                  if let Some(ref_name) = SchemaGraph::extract_ref_name(ref_path) {
                    return Ok(TypeRef::new(format!("Vec<{}>", to_rust_type_name(&ref_name))));
                  }
                }

                // Otherwise resolve and check for oneOf/anyOf in items
                if let Ok(items_schema) = items_ref.resolve(self.graph.spec()) {
                  // If items have oneOf, extract the first ref type
                  if !items_schema.one_of.is_empty() {
                    for one_of_ref in &items_schema.one_of {
                      if let ObjectOrReference::Ref { ref_path, .. } = one_of_ref
                        && let Some(ref_name) = SchemaGraph::extract_ref_name(ref_path)
                      {
                        return Ok(TypeRef::new(format!("Vec<{}>", to_rust_type_name(&ref_name))));
                      }
                    }
                  }

                  // Regular item type conversion
                  let item_type = self.schema_to_type_ref(&items_schema)?;
                  return Ok(TypeRef::new(format!("Vec<{}>", item_type.to_rust_type())));
                }
              }
              return Ok(TypeRef::new("Vec<serde_json::Value>"));
            }
            SchemaType::Object => {
              // Object without a matching schema reference
              return Ok(TypeRef::new("serde_json::Value"));
            }
            SchemaType::Null => {
              return Ok(TypeRef::new("()").with_option());
            }
          };
          return Ok(TypeRef::new(base_type));
        }
        SchemaTypeSet::Multiple(_) => {
          // Handle nullable types - check if it's a simple nullable pattern
          return Ok(TypeRef::new("serde_json::Value"));
        }
      }
    }

    // Default to serde_json::Value for schemas without type or title
    Ok(TypeRef::new("serde_json::Value"))
  }
}

pub struct OperationConverter<'a> {
  schema_converter: &'a SchemaConverter<'a>,
  spec: &'a Spec,
}

impl<'a> OperationConverter<'a> {
  pub fn new(schema_converter: &'a SchemaConverter<'a>, spec: &'a Spec) -> Self {
    Self { schema_converter, spec }
  }

  /// Convert an operation to request and response types
  pub fn convert_operation(
    &self,
    _operation_id: &str,
    method: &str,
    path: &str,
    operation: &Operation,
  ) -> anyhow::Result<(Vec<RustType>, OperationInfo)> {
    let mut types = Vec::new();

    // Generate a base name for the operation
    let base_name = if let Some(ref op_id) = operation.operation_id {
      to_pascal_case(op_id)
    } else {
      // Fallback: use method + sanitized path
      let path_part = path.replace('/', "_").replace(['{', '}'], "");
      to_pascal_case(&format!("{}_{}", method, path_part))
    };

    // Generate request type if needed
    let request_type_name = if !operation.parameters.is_empty() || operation.request_body.is_some() {
      let request_name = format!("{}Request", base_name);
      let request_struct = self.create_request_struct(&request_name, operation)?;
      types.push(RustType::Struct(request_struct));
      Some(request_name)
    } else {
      None
    };

    // Extract primary response type (typically 200/201 response)
    // Don't generate response enums - let HTTP clients use http::StatusCode
    let response_type_name = if let Some(ref responses) = operation.responses {
      // Look for successful response (200, 201, etc.)
      responses
        .iter()
        .find(|(code, _)| code.starts_with('2'))
        .or_else(|| responses.iter().next())
        .and_then(|(_, response_ref)| {
          if let Ok(response) = response_ref.resolve(self.spec) {
            self.extract_response_schema_name(&response)
          } else {
            None
          }
        })
        .map(|name| to_rust_type_name(&name))
    } else {
      None
    };

    let op_info = OperationInfo {
      operation_id: operation.operation_id.clone().unwrap_or_else(|| base_name.clone()),
      method: method.to_string(),
      path: path.to_string(),
      summary: operation.summary.clone(),
      description: operation.description.clone(),
      request_type: request_type_name,
      response_type: response_type_name,
    };

    Ok((types, op_info))
  }

  /// Create a request struct from operation parameters and body
  fn create_request_struct(&self, name: &str, operation: &Operation) -> anyhow::Result<StructDef> {
    let mut fields = Vec::new();

    // Process parameters
    let mut params: Vec<_> = operation
      .parameters
      .iter()
      .filter_map(|param_ref| param_ref.resolve(self.spec).ok())
      .collect();

    params.sort_by(|a, b| {
      let rank = |loc: &ParameterIn| match loc {
        ParameterIn::Path => 0u8,
        ParameterIn::Query => 1,
        ParameterIn::Header => 2,
        ParameterIn::Cookie => 3,
      };

      match rank(&a.location).cmp(&rank(&b.location)) {
        Ordering::Equal => a.name.cmp(&b.name),
        other => other,
      }
    });

    for param in params {
      let field = self.convert_parameter(&param)?;
      fields.push(field);
    }

    // Process request body
    if let Some(ref body_ref) = operation.request_body
      && let Ok(body) = body_ref.resolve(self.spec)
    {
      // Extract schema from the first content type (usually application/json)
      if let Some((_content_type, media_type)) = body.content.iter().next()
        && let Some(ref schema_ref) = media_type.schema
        && let Ok(schema) = schema_ref.resolve(self.spec)
      {
        let body_type = self.schema_converter.schema_to_type_ref(&schema)?;
        let is_required = body.required.unwrap_or(false);
        let validation_attrs = self
          .schema_converter
          .extract_validation_attrs(name, is_required, &schema);
        let regex_validation = self.schema_converter.extract_validation_pattern(name, &schema).cloned();
        let default_value = self.schema_converter.extract_default_value(&schema);

        let mut serde_attrs = vec![];
        if !is_required {
          serde_attrs.push("skip_serializing_if = \"Option::is_none\"".to_string());
        }

        fields.push(FieldDef {
          name: "body".to_string(),
          docs: body
            .description
            .as_ref()
            .map(|d| doc_comment_lines(d))
            .unwrap_or_default(),
          rust_type: if is_required {
            body_type
          } else {
            body_type.with_option()
          },
          optional: !is_required,
          serde_attrs,
          validation_attrs,
          regex_validation,
          default_value,
        });
      }
    }

    let docs = operation
      .description
      .as_ref()
      .or(operation.summary.as_ref())
      .map(|d| doc_comment_lines(d))
      .unwrap_or_default();

    // Detect consistent naming pattern for rename_all
    let mut serde_attrs = vec![];
    let renamed_fields: Vec<_> = fields
      .iter()
      .filter_map(|f| {
        f.serde_attrs
          .iter()
          .find(|attr| attr.starts_with("rename = "))
          .map(|attr| {
            let start = attr.find('"').unwrap() + 1;
            let end = attr.rfind('"').unwrap();
            attr[start..end].to_string()
          })
      })
      .collect();

    if !renamed_fields.is_empty() {
      let all_kebab = renamed_fields.iter().all(|name| name.contains('-'));
      if all_kebab {
        serde_attrs.push("rename_all = \"kebab-case\"".to_string());
        // Remove individual rename attributes
        for field in &mut fields {
          field.serde_attrs.retain(|attr| !attr.starts_with("rename = "));
        }
      }
    }

    // Only add serde(default) at struct level if ALL fields have defaults or are Option/Vec
    let all_fields_defaultable = fields.iter().all(|f| {
      f.default_value.is_some()
        || f.rust_type.nullable
        || f.rust_type.is_array
        || matches!(
          f.rust_type.base_type.as_str(),
          "String"
            | "bool"
            | "i8"
            | "i16"
            | "i32"
            | "i64"
            | "i128"
            | "isize"
            | "u8"
            | "u16"
            | "u32"
            | "u64"
            | "u128"
            | "usize"
            | "f32"
            | "f64"
            | "serde_json::Value"
        )
    });

    if all_fields_defaultable && fields.iter().any(|f| f.default_value.is_some()) {
      serde_attrs.push("default".to_string());
    }

    Ok(StructDef {
      name: to_rust_type_name(name),
      docs,
      fields,
      derives: vec![
        "Debug".into(),
        "Clone".into(),
        "Serialize".into(),
        "Deserialize".into(),
        "Validate".into(),
      ],
      serde_attrs,
    })
  }

  /// Convert a parameter to a field definition
  fn convert_parameter(&self, param: &Parameter) -> anyhow::Result<FieldDef> {
    let (rust_type, validation_attrs, regex_validation, default_value) = if let Some(ref schema_ref) = param.schema {
      if let Ok(schema) = schema_ref.resolve(self.spec) {
        let type_ref = self.schema_converter.schema_to_type_ref(&schema)?;
        let is_required = param.required.unwrap_or(false);
        let validation = self
          .schema_converter
          .extract_validation_attrs(&param.name, is_required, &schema);
        let regex_validation = self.schema_converter.extract_validation_pattern(&param.name, &schema);
        let default = self.schema_converter.extract_default_value(&schema);
        (type_ref, validation, regex_validation.cloned(), default)
      } else {
        (TypeRef::new("String"), vec![], None, None)
      }
    } else {
      (TypeRef::new("String"), vec![], None, None)
    };

    let is_required = param.required.unwrap_or(false);

    let mut serde_attrs = vec![];
    // Add rename if the parameter name is not a valid Rust identifier
    if param.name.contains('-') || param.name.contains('.') {
      serde_attrs.push(format!("rename = \"{}\"", param.name));
    }

    // Add skip_serializing_if for optional parameters
    if !is_required {
      serde_attrs.push("skip_serializing_if = \"Option::is_none\"".to_string());
    }

    // Add location hint as a comment in docs
    let location_hint = match param.location {
      ParameterIn::Path => "Path parameter",
      ParameterIn::Query => "Query parameter",
      ParameterIn::Header => "Header parameter",
      ParameterIn::Cookie => "Cookie parameter",
    };

    let mut docs = vec![format!("/// {}", location_hint)];
    if let Some(ref desc) = param.description {
      docs.extend(doc_comment_lines(desc));
    }

    Ok(FieldDef {
      name: to_rust_ident(&param.name),
      docs,
      rust_type: if is_required {
        rust_type
      } else {
        rust_type.with_option()
      },
      optional: !is_required,
      serde_attrs,
      validation_attrs,
      regex_validation,
      default_value,
    })
  }

  /// Extract schema name from a response (helper)
  fn extract_response_schema_name(&self, response: &oas3::spec::Response) -> Option<String> {
    response.content.iter().next().and_then(|(_, media_type)| {
      media_type.schema.as_ref().and_then(|schema_ref| {
        if let ObjectOrReference::Ref { ref_path, .. } = schema_ref {
          SchemaGraph::extract_ref_name(ref_path)
        } else {
          None
        }
      })
    })
  }
}

pub struct CodeGenerator;

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
struct RegexKey {
  type_name: String,
  variant_name: Option<String>,
  field_name: String,
}

impl RegexKey {
  fn for_struct(type_name: &str, field_name: &str) -> Self {
    Self {
      type_name: type_name.to_string(),
      variant_name: None,
      field_name: field_name.to_string(),
    }
  }

  fn for_variant(type_name: &str, variant_name: &str, field_name: &str) -> Self {
    Self {
      type_name: type_name.to_string(),
      variant_name: Some(variant_name.to_string()),
      field_name: field_name.to_string(),
    }
  }

  fn parts(&self) -> Vec<&str> {
    let mut parts = vec![self.type_name.as_str()];
    if let Some(variant) = &self.variant_name {
      parts.push(variant.as_str());
    }
    parts.push(self.field_name.as_str());
    parts
  }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
enum TypeKind {
  Struct,
  Enum,
  Alias,
}

impl CodeGenerator {
  pub fn generate(types: &[RustType]) -> TokenStream {
    let ordered = Self::ordered_types(types);
    let (regex_consts, regex_lookup) = Self::generate_regex_constants(&ordered);
    let type_tokens: Vec<TokenStream> = ordered
      .iter()
      .map(|ty| Self::generate_type(ty, &regex_lookup))
      .collect();
    let default_impls = Self::generate_default_impls(&ordered);

    quote! {
      use serde::{Deserialize, Serialize};
      use validator::Validate;

      #regex_consts

      #(#type_tokens)*

      #default_impls
    }
  }

  fn ordered_types<'a>(types: &'a [RustType]) -> Vec<&'a RustType> {
    let mut map: BTreeMap<(TypeKind, String), &'a RustType> = BTreeMap::new();
    for ty in types {
      let key = Self::type_key(ty);
      map.entry(key).or_insert(ty);
    }
    map.into_values().collect()
  }

  fn type_key(rust_type: &RustType) -> (TypeKind, String) {
    let kind = match rust_type {
      RustType::Struct(_) => TypeKind::Struct,
      RustType::Enum(_) => TypeKind::Enum,
      RustType::TypeAlias(_) => TypeKind::Alias,
    };

    (kind, rust_type.type_name().to_string())
  }

  /// Generate regex constants for validation
  fn generate_regex_constants(types: &[&RustType]) -> (TokenStream, BTreeMap<RegexKey, String>) {
    let mut const_defs: BTreeMap<String, String> = BTreeMap::new();
    let mut lookup: BTreeMap<RegexKey, String> = BTreeMap::new();
    let mut pattern_to_const: BTreeMap<String, String> = BTreeMap::new();

    for rust_type in types {
      match rust_type {
        RustType::Struct(def) => {
          for field in &def.fields {
            let Some(pattern) = &field.regex_validation else {
              continue;
            };
            let key = RegexKey::for_struct(&def.name, &field.name);
            let pattern_key = pattern.clone();
            let const_name = match pattern_to_const.get(&pattern_key) {
              Some(existing) => existing.clone(),
              None => {
                let name = Self::regex_const_name(&key);
                pattern_to_const.insert(pattern_key.clone(), name.clone());
                const_defs.insert(name.clone(), pattern_key);
                name
              }
            };
            lookup.insert(key, const_name);
          }
        }
        RustType::Enum(def) => {
          for variant in &def.variants {
            if let VariantContent::Struct(fields) = &variant.content {
              for field in fields {
                let Some(pattern) = &field.regex_validation else {
                  continue;
                };
                let key = RegexKey::for_variant(&def.name, &variant.name, &field.name);
                let pattern_key = pattern.clone();
                let const_name = match pattern_to_const.get(&pattern_key) {
                  Some(existing) => existing.clone(),
                  None => {
                    let name = Self::regex_const_name(&key);
                    pattern_to_const.insert(pattern_key.clone(), name.clone());
                    const_defs.insert(name.clone(), pattern_key);
                    name
                  }
                };
                lookup.insert(key, const_name);
              }
            }
          }
        }
        RustType::TypeAlias(_) => {}
      }
    }

    if const_defs.is_empty() {
      return (quote! {}, lookup);
    }

    let regex_defs: Vec<TokenStream> = const_defs
      .into_iter()
      .map(|(name, pattern)| {
        let ident = format_ident!("{}", name);
        quote! {
          static #ident: std::sync::LazyLock<regex::Regex> =
            std::sync::LazyLock::new(|| regex::Regex::new(#pattern).expect("invalid regex"));
        }
      })
      .collect();

    (quote! { #(#regex_defs)* }, lookup)
  }

  fn regex_const_name(key: &RegexKey) -> String {
    let joined = key
      .parts()
      .into_iter()
      .map(any_ascii::any_ascii)
      .collect::<Vec<_>>()
      .join("_");

    format!("REGEX_{}", joined.to_constant_case())
  }

  /// Check if a type can safely use Default::default()
  fn type_can_default(type_ref: &TypeRef) -> bool {
    let base_type = &type_ref.base_type;

    // Primitive types that implement Default
    matches!(
      base_type.as_str(),
      "String"
        | "bool"
        | "i8"
        | "i16"
        | "i32"
        | "i64"
        | "i128"
        | "isize"
        | "u8"
        | "u16"
        | "u32"
        | "u64"
        | "u128"
        | "usize"
        | "f32"
        | "f64"
        | "serde_json::Value"
    ) || type_ref.nullable
      || type_ref.is_array
      || base_type.starts_with("Vec<")
      || base_type.starts_with("Option<")
  }

  /// Find the best variant to use as default for an enum
  /// Priority: Unit > Vec/Option types > Struct variants > Tuple variants with primitives
  fn find_best_default_variant(variants: &[VariantDef]) -> Option<(&VariantDef, TokenStream)> {
    // Priority 1: Unit variants
    for variant in variants {
      if matches!(variant.content, VariantContent::Unit) {
        let variant_name = format_ident!("{}", variant.name);
        return Some((variant, quote! { Self::#variant_name }));
      }
    }

    // Priority 2: Tuple variants with Vec or Option (always defaultable)
    for variant in variants {
      if let VariantContent::Tuple(types) = &variant.content
        && types.len() == 1
        && (types[0].is_array || types[0].nullable)
      {
        let variant_name = format_ident!("{}", variant.name);
        return Some((variant, quote! { Self::#variant_name(Default::default()) }));
      }
    }

    // Priority 3: Struct variants with defaultable fields
    for variant in variants {
      if let VariantContent::Struct(fields) = &variant.content {
        // Check if all fields can be defaulted
        let all_defaultable = fields
          .iter()
          .all(|f| f.default_value.is_some() || Self::type_can_default(&f.rust_type));

        if all_defaultable {
          let variant_name = format_ident!("{}", variant.name);
          let field_inits: Vec<TokenStream> = fields
            .iter()
            .map(|field| {
              let field_name = format_ident!("{}", field.name);
              if let Some(ref default_val) = field.default_value {
                let value_expr = Self::json_value_to_rust_expr(default_val, &field.rust_type);
                quote! { #field_name: #value_expr }
              } else {
                quote! { #field_name: Default::default() }
              }
            })
            .collect();
          return Some((variant, quote! { Self::#variant_name { #(#field_inits),* } }));
        }
      }
    }

    // Priority 4: Tuple variants with primitive types
    for variant in variants {
      if let VariantContent::Tuple(types) = &variant.content
        && types.iter().all(Self::type_can_default)
      {
        let variant_name = format_ident!("{}", variant.name);
        let defaults: Vec<TokenStream> = types.iter().map(|_| quote! { Default::default() }).collect();
        return Some((variant, quote! { Self::#variant_name(#(#defaults),*) }));
      }
    }

    // No suitable variant found
    None
  }

  /// Generate impl Default blocks for structs and enums
  fn generate_default_impls(types: &[&RustType]) -> TokenStream {
    let mut impls: Vec<(proc_macro2::Ident, TokenStream)> = Vec::new();

    for rust_type in types {
      match rust_type {
        RustType::Struct(def) => {
          let has_defaults = def.fields.iter().any(|f| f.default_value.is_some());

          if has_defaults {
            let all_fields_can_default = def
              .fields
              .iter()
              .all(|f| f.default_value.is_some() || Self::type_can_default(&f.rust_type));

            if all_fields_can_default {
              let struct_name = format_ident!("{}", def.name);

              let field_inits: Vec<TokenStream> = def
                .fields
                .iter()
                .map(|field| {
                  let field_name = format_ident!("{}", field.name);

                  if let Some(ref default_val) = field.default_value {
                    let value_expr = Self::json_value_to_rust_expr(default_val, &field.rust_type);
                    quote! { #field_name: #value_expr }
                  } else {
                    quote! { #field_name: Default::default() }
                  }
                })
                .collect();

              impls.push((struct_name, quote! { Self { #(#field_inits),* } }));
            }
          }
        }
        RustType::Enum(def) => {
          if let Some((_variant, default_expr)) = Self::find_best_default_variant(&def.variants) {
            let enum_name = format_ident!("{}", def.name);
            impls.push((enum_name, default_expr));
          }
        }
        RustType::TypeAlias(_) => {}
      }
    }

    if impls.is_empty() {
      return quote! {};
    }

    let macro_def = quote! {
      macro_rules! impl_default {
        ($type:ident = $body:expr) => {
          impl Default for $type {
            fn default() -> Self {
              $body
            }
          }
        };
      }
    };

    let macro_calls: Vec<TokenStream> = impls
      .into_iter()
      .map(|(ident, body)| quote! { impl_default!(#ident = #body); })
      .collect();

    quote! {
      #macro_def

      #(#macro_calls)*
    }
  }

  /// Convert a JSON value to a Rust expression
  fn json_value_to_rust_expr(value: &serde_json::Value, rust_type: &TypeRef) -> TokenStream {
    let base_expr = match value {
      serde_json::Value::String(s) => {
        quote! { #s.to_string() }
      }
      serde_json::Value::Number(n) => {
        if let Some(i) = n.as_i64() {
          quote! { #i }
        } else if let Some(f) = n.as_f64() {
          quote! { #f }
        } else {
          quote! { Default::default() }
        }
      }
      serde_json::Value::Bool(b) => {
        quote! { #b }
      }
      serde_json::Value::Null => {
        quote! { None }
      }
      serde_json::Value::Array(_) | serde_json::Value::Object(_) => {
        // For complex types, use Default::default()
        quote! { Default::default() }
      }
    };

    // Wrap in Some() if the type is Option<T>
    if rust_type.nullable && !matches!(value, serde_json::Value::Null) {
      quote! { Some(#base_expr) }
    } else {
      base_expr
    }
  }

  fn generate_type(rust_type: &RustType, regex_lookup: &BTreeMap<RegexKey, String>) -> TokenStream {
    match rust_type {
      RustType::Struct(def) => Self::generate_struct(def, regex_lookup),
      RustType::Enum(def) => Self::generate_enum(def, regex_lookup),
      RustType::TypeAlias(def) => Self::generate_type_alias(def),
    }
  }

  fn generate_struct(def: &StructDef, regex_lookup: &BTreeMap<RegexKey, String>) -> TokenStream {
    let name = format_ident!("{}", def.name);
    let docs = Self::generate_docs(&def.docs);
    let derives = Self::generate_derives(&def.derives);
    let serde_attrs = Self::generate_serde_attrs(&def.serde_attrs);
    let fields = Self::generate_fields_with_visibility(&def.name, None, &def.fields, true, true, regex_lookup);

    quote! {
      #docs
      #derives
      #serde_attrs
      pub struct #name {
        #(#fields),*
      }
    }
  }

  fn generate_enum(def: &EnumDef, regex_lookup: &BTreeMap<RegexKey, String>) -> TokenStream {
    let name = format_ident!("{}", def.name);
    let docs = Self::generate_docs(&def.docs);
    let derives = Self::generate_derives(&def.derives);
    let serde_attrs = Self::generate_enum_serde_attrs(def);
    let variants = Self::generate_variants(&def.name, &def.variants, regex_lookup);

    quote! {
      #docs
      #derives
      #serde_attrs
      pub enum #name {
        #(#variants),*
      }
    }
  }

  fn generate_type_alias(def: &TypeAliasDef) -> TokenStream {
    let name = format_ident!("{}", def.name);
    let docs = Self::generate_docs(&def.docs);
    let target = Self::parse_type_string(&def.target.to_rust_type());

    quote! {
      #docs
      pub type #name = #target;
    }
  }

  fn generate_docs(docs: &[String]) -> TokenStream {
    if docs.is_empty() {
      return quote! {};
    }

    let doc_lines: Vec<TokenStream> = docs
      .iter()
      .map(|line| {
        // Remove the /// prefix that was added earlier
        let clean_line = line.strip_prefix("/// ").unwrap_or(line);
        quote! { #[doc = #clean_line] }
      })
      .collect();

    quote! { #(#doc_lines)* }
  }

  fn generate_derives(derives: &[String]) -> TokenStream {
    if derives.is_empty() {
      return quote! {};
    }

    let derive_idents: Vec<_> = derives.iter().map(|d| format_ident!("{}", d)).collect();

    quote! {
      #[derive(#(#derive_idents),*)]
    }
  }

  fn generate_serde_attrs(attrs: &[String]) -> TokenStream {
    if attrs.is_empty() {
      return quote! {};
    }

    let attr_tokens: Vec<TokenStream> = attrs
      .iter()
      .map(|attr| {
        let attr_str = attr.as_str();
        let tokens: TokenStream = attr_str.parse().unwrap_or_else(|_| quote! {});
        quote! { #[serde(#tokens)] }
      })
      .collect();

    quote! { #(#attr_tokens)* }
  }

  fn generate_validation_attrs(regex_const: Option<&str>, attrs: &[String]) -> TokenStream {
    if attrs.is_empty() && regex_const.is_none() {
      return quote! {};
    }

    let mut combined = attrs.to_owned();

    if let Some(const_name) = regex_const {
      combined.push(format!("regex(path = \"{}\")", const_name));
    }

    let attr_tokens: Vec<TokenStream> = combined
      .iter()
      .map(|attr| attr.parse().unwrap_or_else(|_| quote! {}))
      .collect();

    quote! { #[validate(#(#attr_tokens),*)] }
  }

  fn generate_enum_serde_attrs(def: &EnumDef) -> TokenStream {
    let mut attrs = Vec::new();

    // Add discriminator tag if present
    if let Some(ref discriminator) = def.discriminator {
      attrs.push(quote! { tag = #discriminator });
    }

    // Add other serde attributes
    for attr in &def.serde_attrs {
      if let Ok(tokens) = attr.parse::<TokenStream>() {
        attrs.push(tokens);
      }
    }

    if attrs.is_empty() {
      return quote! {};
    }

    quote! {
      #[serde(#(#attrs),*)]
    }
  }

  fn generate_fields_with_visibility(
    type_name: &str,
    variant_name: Option<&str>,
    fields: &[FieldDef],
    add_pub: bool,
    include_validation: bool,
    regex_lookup: &BTreeMap<RegexKey, String>,
  ) -> Vec<TokenStream> {
    fields
      .iter()
      .map(|field| {
        let name = format_ident!("{}", field.name);
        let docs = Self::generate_docs(&field.docs);
        let serde_attrs = Self::generate_serde_attrs(&field.serde_attrs);

        // Only include validation for struct fields, not enum variant fields
        let regex_const = if include_validation && field.regex_validation.is_some() {
          let key = match variant_name {
            Some(variant) => RegexKey::for_variant(type_name, variant, &field.name),
            None => RegexKey::for_struct(type_name, &field.name),
          };
          regex_lookup.get(&key).map(|s| s.as_str())
        } else {
          None
        };

        let validation_attrs = if include_validation {
          Self::generate_validation_attrs(regex_const, &field.validation_attrs)
        } else {
          quote! {}
        };
        let type_tokens = Self::parse_type_string(&field.rust_type.to_rust_type());

        if add_pub {
          quote! {
            #docs
            #serde_attrs
            #validation_attrs
            pub #name: #type_tokens
          }
        } else {
          quote! {
            #docs
            #serde_attrs
            #validation_attrs
            #name: #type_tokens
          }
        }
      })
      .collect()
  }

  fn generate_variants(
    type_name: &str,
    variants: &[VariantDef],
    regex_lookup: &BTreeMap<RegexKey, String>,
  ) -> Vec<TokenStream> {
    variants
      .iter()
      .map(|variant| {
        let name = format_ident!("{}", variant.name);
        let docs = Self::generate_docs(&variant.docs);
        let serde_attrs = Self::generate_serde_attrs(&variant.serde_attrs);

        let content = match &variant.content {
          VariantContent::Unit => quote! {},
          VariantContent::Tuple(types) => {
            let type_tokens: Vec<_> = types
              .iter()
              .map(|t| Self::parse_type_string(&t.to_rust_type()))
              .collect();
            quote! { ( #(#type_tokens),* ) }
          }
          VariantContent::Struct(fields) => {
            // Enum variant fields should not have 'pub' keyword or validation attributes
            let field_tokens =
              Self::generate_fields_with_visibility(type_name, Some(&variant.name), fields, false, false, regex_lookup);
            quote! { { #(#field_tokens),* } }
          }
        };

        quote! {
          #docs
          #serde_attrs
          #name #content
        }
      })
      .collect()
  }

  /// Parse a type string into a TokenStream
  /// This is a simple parser that handles basic Rust types
  fn parse_type_string(type_str: &str) -> TokenStream {
    // For now, just parse it directly - this works for most cases
    type_str.parse().unwrap_or_else(|_| quote! { serde_json::Value })
  }
}