sqlmodel-core 0.2.0

Core types and traits for SQLModel Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
//! Runtime validation helpers for SQLModel.
//!
//! This module provides validation functions that can be called from
//! generated validation code (via the `#[derive(Validate)]` macro).
//!
//! It also provides `model_validate()` functionality for creating and
//! validating models from various input types (similar to Pydantic).

use std::collections::HashMap;
use std::sync::OnceLock;

use regex::Regex;
use serde::de::DeserializeOwned;

use crate::Value;
use crate::error::{ValidationError, ValidationErrorKind};

/// Thread-safe regex cache for compiled patterns.
///
/// This avoids recompiling regex patterns on every validation call.
/// Patterns are compiled lazily on first use and cached for the lifetime
/// of the program.
struct RegexCache {
    cache: std::sync::RwLock<std::collections::HashMap<String, Regex>>,
}

impl RegexCache {
    fn new() -> Self {
        Self {
            cache: std::sync::RwLock::new(std::collections::HashMap::new()),
        }
    }

    fn get_or_compile(&self, pattern: &str) -> Result<Regex, regex::Error> {
        // Fast path: check if already cached
        // Use unwrap_or_else to recover from poisoned lock (another thread panicked)
        {
            let cache = self.cache.read().unwrap_or_else(|e| e.into_inner());
            if let Some(regex) = cache.get(pattern) {
                return Ok(regex.clone());
            }
        }

        // Slow path: compile and cache
        let regex = Regex::new(pattern)?;
        {
            let mut cache = self.cache.write().unwrap_or_else(|e| e.into_inner());
            cache.insert(pattern.to_string(), regex.clone());
        }
        Ok(regex)
    }
}

/// Global regex cache singleton.
fn regex_cache() -> &'static RegexCache {
    static CACHE: OnceLock<RegexCache> = OnceLock::new();
    CACHE.get_or_init(RegexCache::new)
}

/// Check if a string matches a regex pattern.
///
/// This function is designed to be called from generated validation code.
/// It caches compiled regex patterns for efficiency.
///
/// # Arguments
///
/// * `value` - The string to validate
/// * `pattern` - The regex pattern to match against
///
/// # Returns
///
/// `true` if the value matches the pattern, `false` otherwise.
/// Returns `false` if the pattern is invalid (logs a warning).
///
/// # Example
///
/// ```ignore
/// use sqlmodel_core::validate::matches_pattern;
///
/// assert!(matches_pattern("test@example.com", r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"));
/// assert!(!matches_pattern("invalid", r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"));
/// ```
pub fn matches_pattern(value: &str, pattern: &str) -> bool {
    match regex_cache().get_or_compile(pattern) {
        Ok(regex) => regex.is_match(value),
        Err(e) => {
            // Log the error but don't panic - validation should be resilient
            tracing::warn!(
                pattern = pattern,
                error = %e,
                "Invalid regex pattern in validation, treating as non-match"
            );
            false
        }
    }
}

/// Validate a regex pattern at compile time (for use in proc macros).
///
/// Returns an error message if the pattern is invalid, None if valid.
pub fn validate_pattern(pattern: &str) -> Option<String> {
    match Regex::new(pattern) {
        Ok(_) => None,
        Err(e) => Some(format!("invalid regex pattern: {e}")),
    }
}

// ============================================================================
// Built-in Validators
// ============================================================================

/// Validate a credit card number using the Luhn algorithm.
///
/// The Luhn algorithm (also known as the "modulus 10" algorithm) is a simple
/// checksum formula used to validate identification numbers such as credit card
/// numbers, IMEI numbers, and others.
///
/// # Algorithm
///
/// 1. Starting from the rightmost digit (check digit) and moving left,
///    double the value of every second digit.
/// 2. If the result of doubling is greater than 9, subtract 9.
/// 3. Sum all the digits.
/// 4. The total modulo 10 must equal 0.
///
/// # Arguments
///
/// * `value` - The credit card number as a string (may contain spaces or hyphens)
///
/// # Returns
///
/// `true` if the number is valid according to the Luhn algorithm, `false` otherwise.
///
/// # Example
///
/// ```ignore
/// use sqlmodel_core::validate::is_valid_credit_card;
///
/// assert!(is_valid_credit_card("4539578763621486"));  // Valid Visa
/// assert!(is_valid_credit_card("4539-5787-6362-1486")); // With dashes
/// assert!(is_valid_credit_card("4539 5787 6362 1486")); // With spaces
/// assert!(!is_valid_credit_card("1234567890123456")); // Invalid
/// ```
pub fn is_valid_credit_card(value: &str) -> bool {
    // Remove all non-digit characters (spaces, hyphens, etc.)
    let digits: Vec<u32> = value
        .chars()
        .filter(|c| c.is_ascii_digit())
        .filter_map(|c| c.to_digit(10))
        .collect();

    // Credit card numbers are typically 13-19 digits
    if digits.len() < 13 || digits.len() > 19 {
        return false;
    }

    // Luhn algorithm
    let mut sum = 0u32;
    let len = digits.len();

    for (i, &digit) in digits.iter().enumerate() {
        // Count from right: rightmost is position 1 (odd)
        // We double every second digit starting from the second-to-last
        let position_from_right = len - i;
        let is_double_position = position_from_right % 2 == 0;

        let value = if is_double_position {
            let doubled = digit * 2;
            if doubled > 9 { doubled - 9 } else { doubled }
        } else {
            digit
        };

        sum += value;
    }

    sum % 10 == 0
}

// ============================================================================
// Model Validation (model_validate)
// ============================================================================

/// Input types for model_validate().
///
/// Supports creating models from various input formats.
#[derive(Debug, Clone)]
pub enum ValidateInput {
    /// A HashMap of field names to values.
    Dict(HashMap<String, Value>),
    /// A JSON string to parse.
    Json(String),
    /// A serde_json::Value for direct deserialization.
    JsonValue(serde_json::Value),
}

impl From<HashMap<String, Value>> for ValidateInput {
    fn from(map: HashMap<String, Value>) -> Self {
        ValidateInput::Dict(map)
    }
}

impl From<String> for ValidateInput {
    fn from(json: String) -> Self {
        ValidateInput::Json(json)
    }
}

impl From<&str> for ValidateInput {
    fn from(json: &str) -> Self {
        ValidateInput::Json(json.to_string())
    }
}

impl From<serde_json::Value> for ValidateInput {
    fn from(value: serde_json::Value) -> Self {
        ValidateInput::JsonValue(value)
    }
}

/// Options for model_validate().
///
/// Controls the validation behavior.
#[derive(Debug, Clone, Default)]
pub struct ValidateOptions {
    /// If true, use strict type coercion (no implicit conversions).
    pub strict: bool,
    /// If true, read from object attributes (ORM mode).
    /// Currently unused - reserved for future from_attributes support.
    pub from_attributes: bool,
    /// Optional context dictionary passed to custom validators.
    pub context: Option<HashMap<String, serde_json::Value>>,
    /// Additional values to merge into the result after parsing.
    pub update: Option<HashMap<String, serde_json::Value>>,
}

impl ValidateOptions {
    /// Create new default options.
    pub fn new() -> Self {
        Self::default()
    }

    /// Enable strict mode (no implicit type conversions).
    pub fn strict(mut self) -> Self {
        self.strict = true;
        self
    }

    /// Enable from_attributes mode (read from object attributes).
    pub fn from_attributes(mut self) -> Self {
        self.from_attributes = true;
        self
    }

    /// Set context for custom validators.
    pub fn with_context(mut self, context: HashMap<String, serde_json::Value>) -> Self {
        self.context = Some(context);
        self
    }

    /// Set values to merge into result.
    pub fn with_update(mut self, update: HashMap<String, serde_json::Value>) -> Self {
        self.update = Some(update);
        self
    }
}

/// Result type for model_validate operations.
pub type ValidateResult<T> = std::result::Result<T, ValidationError>;

/// Trait for models that support model_validate().
///
/// This is typically implemented via derive macro or blanket impl
/// for models that implement Deserialize.
pub trait ModelValidate: Sized {
    /// Create and validate a model from input.
    ///
    /// # Arguments
    ///
    /// * `input` - The input to validate (Dict, Json, or JsonValue)
    /// * `options` - Validation options
    ///
    /// # Returns
    ///
    /// The validated model or validation errors.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use sqlmodel_core::validate::{ModelValidate, ValidateInput, ValidateOptions};
    ///
    /// let user = User::model_validate(
    ///     r#"{"name": "Alice", "age": 30}"#,
    ///     ValidateOptions::default()
    /// )?;
    /// ```
    fn model_validate(
        input: impl Into<ValidateInput>,
        options: ValidateOptions,
    ) -> ValidateResult<Self>;

    /// Create and validate a model from JSON string with default options.
    fn model_validate_json(json: &str) -> ValidateResult<Self> {
        Self::model_validate(json, ValidateOptions::default())
    }

    /// Create and validate a model from a HashMap with default options.
    fn model_validate_dict(dict: HashMap<String, Value>) -> ValidateResult<Self> {
        Self::model_validate(dict, ValidateOptions::default())
    }
}

/// Blanket implementation of ModelValidate for types that implement DeserializeOwned.
///
/// This provides model_validate() for any model that can be deserialized from JSON.
impl<T: DeserializeOwned> ModelValidate for T {
    fn model_validate(
        input: impl Into<ValidateInput>,
        options: ValidateOptions,
    ) -> ValidateResult<Self> {
        let input = input.into();

        // Convert input to serde_json::Value
        let mut json_value = match input {
            ValidateInput::Dict(dict) => {
                // Convert HashMap<String, Value> to serde_json::Value
                let map: serde_json::Map<String, serde_json::Value> = dict
                    .into_iter()
                    .map(|(k, v)| (k, value_to_json(v)))
                    .collect();
                serde_json::Value::Object(map)
            }
            ValidateInput::Json(json_str) => serde_json::from_str(&json_str).map_err(|e| {
                let mut err = ValidationError::new();
                err.add(
                    "_json",
                    ValidationErrorKind::Custom,
                    format!("Invalid JSON: {e}"),
                );
                err
            })?,
            ValidateInput::JsonValue(value) => value,
        };

        // Apply update values if provided
        if let Some(update) = options.update {
            if let serde_json::Value::Object(ref mut map) = json_value {
                for (key, value) in update {
                    map.insert(key, value);
                }
            }
        }

        // Deserialize with appropriate strictness
        if options.strict {
            // In strict mode, we use serde's strict deserialization
            // (default behavior - no implicit conversions)
            serde_json::from_value(json_value).map_err(|e| {
                let mut err = ValidationError::new();
                err.add(
                    "_model",
                    ValidationErrorKind::Custom,
                    format!("Validation failed: {e}"),
                );
                err
            })
        } else {
            // Non-strict mode - same for now, but could add coercion logic
            serde_json::from_value(json_value).map_err(|e| {
                let mut err = ValidationError::new();
                err.add(
                    "_model",
                    ValidationErrorKind::Custom,
                    format!("Validation failed: {e}"),
                );
                err
            })
        }
    }
}

// ============================================================================
// Model Dump (model_dump)
// ============================================================================

/// Output mode for model_dump().
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum DumpMode {
    /// JSON-compatible types (strings, numbers, booleans, null)
    #[default]
    Json,
    /// Rust native types (preserves Value variants)
    Python,
}

/// Options for model_dump() and model_dump_json().
///
/// Controls the serialization behavior.
#[derive(Debug, Clone, Default)]
pub struct DumpOptions {
    /// Output mode: Json or Python (Rust native).
    ///
    /// In Python/Pydantic, `mode='json'` produces JSON-compatible values while
    /// `mode='python'` preserves native Python types (e.g., datetime objects).
    ///
    /// In this crate, `model_dump()`/`sql_model_dump()` return `serde_json::Value`,
    /// so both modes currently produce equivalent JSON-value output.
    pub mode: DumpMode,
    /// Only include these fields (if Some)
    pub include: Option<std::collections::HashSet<String>>,
    /// Exclude these fields
    pub exclude: Option<std::collections::HashSet<String>>,
    /// Use field aliases in output.
    ///
    /// When true, `sql_model_dump()` will rename fields to their
    /// `serialization_alias` (or `alias` as fallback) in the output.
    pub by_alias: bool,
    /// Exclude fields that were not explicitly set.
    ///
    /// In Pydantic, this depends on tracking which fields were explicitly
    /// provided at construction time (distinct from `exclude_defaults`).
    ///
    /// Rust structs do not retain "field set" metadata by default, so this
    /// option is rejected at runtime to avoid silently producing incorrect output.
    ///
    /// Pydantic semantics: If a field has a default value and the user explicitly
    /// sets it to that default, it should still be included (it was "set").
    /// With `exclude_defaults`, it would be excluded regardless of whether
    /// it was explicitly provided.
    pub exclude_unset: bool,
    /// Exclude fields with default values
    pub exclude_defaults: bool,
    /// Exclude fields with None/null values
    pub exclude_none: bool,
    /// Exclude computed fields (for future computed_field support)
    pub exclude_computed_fields: bool,
    /// Enable round-trip mode (preserves types for re-parsing).
    ///
    /// Pydantic can alter serialization to ensure a dump can be fed back into
    /// validation and reproduce the same model. This crate does not yet implement
    /// tagged round-trip encoding, so this flag is currently accepted as a no-op.
    pub round_trip: bool,
    /// Indentation for JSON output (None = compact, Some(n) = n spaces)
    pub indent: Option<usize>,
}

impl DumpOptions {
    /// Create new default options.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set output mode to JSON.
    pub fn json(mut self) -> Self {
        self.mode = DumpMode::Json;
        self
    }

    /// Set output mode to Python (Rust native).
    pub fn python(mut self) -> Self {
        self.mode = DumpMode::Python;
        self
    }

    /// Set fields to include.
    pub fn include(mut self, fields: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.include = Some(fields.into_iter().map(Into::into).collect());
        self
    }

    /// Set fields to exclude.
    pub fn exclude(mut self, fields: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.exclude = Some(fields.into_iter().map(Into::into).collect());
        self
    }

    /// Enable by_alias mode.
    pub fn by_alias(mut self) -> Self {
        self.by_alias = true;
        self
    }

    /// Enable exclude_unset mode.
    pub fn exclude_unset(mut self) -> Self {
        self.exclude_unset = true;
        self
    }

    /// Enable exclude_defaults mode.
    pub fn exclude_defaults(mut self) -> Self {
        self.exclude_defaults = true;
        self
    }

    /// Enable exclude_none mode.
    pub fn exclude_none(mut self) -> Self {
        self.exclude_none = true;
        self
    }

    /// Enable exclude_computed_fields mode.
    pub fn exclude_computed_fields(mut self) -> Self {
        self.exclude_computed_fields = true;
        self
    }

    /// Enable round_trip mode.
    pub fn round_trip(mut self) -> Self {
        self.round_trip = true;
        self
    }

    /// Set indentation for JSON output.
    ///
    /// When set, JSON output will be pretty-printed with the specified number
    /// of spaces for indentation. When None (default), JSON is compact.
    pub fn indent(mut self, spaces: usize) -> Self {
        self.indent = Some(spaces);
        self
    }
}

/// Result type for model_dump operations.
pub type DumpResult = std::result::Result<serde_json::Value, serde_json::Error>;

pub(crate) fn dump_options_unsupported(msg: impl Into<String>) -> serde_json::Error {
    serde_json::Error::io(std::io::Error::new(
        std::io::ErrorKind::InvalidInput,
        msg.into(),
    ))
}

/// Trait for models that support model_dump().
///
/// This is typically implemented via blanket impl for models that implement Serialize.
pub trait ModelDump {
    /// Serialize a model to a JSON value.
    ///
    /// # Arguments
    ///
    /// * `options` - Dump options controlling serialization behavior
    ///
    /// # Returns
    ///
    /// A serde_json::Value representing the serialized model.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use sqlmodel_core::validate::{ModelDump, DumpOptions};
    ///
    /// let json = user.model_dump(DumpOptions::default())?;
    /// ```
    fn model_dump(&self, options: DumpOptions) -> DumpResult;

    /// Serialize a model to a JSON string with default options.
    fn model_dump_json(&self) -> std::result::Result<String, serde_json::Error> {
        let value = self.model_dump(DumpOptions::default())?;
        serde_json::to_string(&value)
    }

    /// Serialize a model to a pretty-printed JSON string.
    fn model_dump_json_pretty(&self) -> std::result::Result<String, serde_json::Error> {
        let value = self.model_dump(DumpOptions::default())?;
        serde_json::to_string_pretty(&value)
    }

    /// Serialize a model to a JSON string with full options support.
    ///
    /// This method supports all DumpOptions including the `indent` option:
    /// - `indent: None` - compact JSON output
    /// - `indent: Some(n)` - pretty-printed with n spaces indentation
    ///
    /// # Example
    ///
    /// ```ignore
    /// use sqlmodel_core::validate::{ModelDump, DumpOptions};
    ///
    /// // Compact JSON with exclusions
    /// let json = user.model_dump_json_with_options(
    ///     DumpOptions::default().exclude(["password"])
    /// )?;
    ///
    /// // Pretty-printed with 4-space indent
    /// let json = user.model_dump_json_with_options(
    ///     DumpOptions::default().indent(4)
    /// )?;
    /// ```
    fn model_dump_json_with_options(
        &self,
        options: DumpOptions,
    ) -> std::result::Result<String, serde_json::Error> {
        let value = self.model_dump(DumpOptions {
            indent: None, // Don't pass indent to model_dump (it returns Value, not String)
            ..options.clone()
        })?;

        match options.indent {
            Some(spaces) => {
                let indent_bytes = " ".repeat(spaces).into_bytes();
                let formatter = serde_json::ser::PrettyFormatter::with_indent(&indent_bytes);
                let mut writer = Vec::new();
                let mut ser = serde_json::Serializer::with_formatter(&mut writer, formatter);
                serde::Serialize::serialize(&value, &mut ser)?;
                // serde_json always produces valid UTF-8, but propagate error instead of panicking
                String::from_utf8(writer).map_err(|e| {
                    serde_json::Error::io(std::io::Error::new(
                        std::io::ErrorKind::InvalidData,
                        format!("UTF-8 encoding error: {e}"),
                    ))
                })
            }
            None => serde_json::to_string(&value),
        }
    }
}

/// Blanket implementation of ModelDump for types that implement Serialize.
impl<T: serde::Serialize> ModelDump for T {
    fn model_dump(&self, options: DumpOptions) -> DumpResult {
        if options.exclude_unset {
            return Err(dump_options_unsupported(
                "DumpOptions.exclude_unset requires fields_set tracking; use SqlModelValidate::sql_model_validate_tracked(...) or the tracked!(Type { .. }) macro",
            ));
        }
        if options.by_alias || options.exclude_defaults || options.exclude_computed_fields {
            return Err(dump_options_unsupported(
                "DumpOptions.by_alias/exclude_defaults/exclude_computed_fields require SqlModelDump",
            ));
        }

        // First, serialize to JSON value
        let mut value = serde_json::to_value(self)?;

        // Apply options
        if let serde_json::Value::Object(ref mut map) = value {
            // Apply include filter
            if let Some(ref include) = options.include {
                map.retain(|k, _| include.contains(k));
            }

            // Apply exclude filter
            if let Some(ref exclude) = options.exclude {
                map.retain(|k, _| !exclude.contains(k));
            }

            // Apply exclude_none filter
            if options.exclude_none {
                map.retain(|_, v| !v.is_null());
            }

            // Note: This is the generic ModelDump implementation for Serialize types.
            // Model-aware transforms are implemented by SqlModelDump and rejected above.
        }

        Ok(value)
    }
}

/// Convert a Value to serde_json::Value.
fn value_to_json(value: Value) -> serde_json::Value {
    match value {
        Value::Null => serde_json::Value::Null,
        Value::Bool(b) => serde_json::Value::Bool(b),
        Value::TinyInt(i) => serde_json::Value::Number(i.into()),
        Value::SmallInt(i) => serde_json::Value::Number(i.into()),
        Value::Int(i) => serde_json::Value::Number(i.into()),
        Value::BigInt(i) => serde_json::Value::Number(i.into()),
        Value::Float(f) => serde_json::Number::from_f64(f64::from(f))
            .map_or(serde_json::Value::Null, serde_json::Value::Number),
        Value::Double(f) => serde_json::Number::from_f64(f)
            .map_or(serde_json::Value::Null, serde_json::Value::Number),
        Value::Decimal(s) => serde_json::Value::String(s),
        Value::Text(s) => serde_json::Value::String(s),
        Value::Bytes(b) => {
            // Encode bytes as hex string
            use std::fmt::Write;
            let hex = b
                .iter()
                .fold(String::with_capacity(b.len() * 2), |mut acc, byte| {
                    let _ = write!(acc, "{byte:02x}");
                    acc
                });
            serde_json::Value::String(hex)
        }
        // Date is i32 (days since epoch) - convert to number
        Value::Date(d) => serde_json::Value::Number(d.into()),
        // Time is i64 (microseconds since midnight)
        Value::Time(t) => serde_json::Value::Number(t.into()),
        // Timestamp is i64 (microseconds since epoch)
        Value::Timestamp(ts) => serde_json::Value::Number(ts.into()),
        // TimestampTz is i64 (microseconds since epoch, UTC)
        Value::TimestampTz(ts) => serde_json::Value::Number(ts.into()),
        // UUID is [u8; 16] - format as UUID string with dashes
        Value::Uuid(u) => {
            use std::fmt::Write;
            let hex = u.iter().fold(String::with_capacity(32), |mut acc, b| {
                let _ = write!(acc, "{b:02x}");
                acc
            });
            // Format as UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
            let formatted = format!(
                "{}-{}-{}-{}-{}",
                &hex[0..8],
                &hex[8..12],
                &hex[12..16],
                &hex[16..20],
                &hex[20..32]
            );
            serde_json::Value::String(formatted)
        }
        Value::Json(j) => j,
        Value::Array(arr) => serde_json::Value::Array(arr.into_iter().map(value_to_json).collect()),
        Value::Default => serde_json::Value::Null,
    }
}

// ============================================================================
// Alias-Aware Validation and Serialization
// ============================================================================

use crate::Model;

/// Apply validation aliases to JSON input.
///
/// This transforms input keys that match validation_alias or alias to their
/// corresponding field names, enabling deserialization to work correctly.
///
/// # Arguments
///
/// * `json` - The JSON value to transform (modified in place)
/// * `fields` - The field metadata containing alias information
pub fn apply_validation_aliases(json: &mut serde_json::Value, fields: &[crate::FieldInfo]) {
    if let serde_json::Value::Object(map) = json {
        // Build a mapping from alias -> field_name
        let mut alias_map: HashMap<&str, &str> = HashMap::new();
        for field in fields {
            // validation_alias takes precedence for input
            if let Some(alias) = field.validation_alias {
                alias_map.insert(alias, field.name);
            }
            // Regular alias also works for input
            if let Some(alias) = field.alias {
                alias_map.entry(alias).or_insert(field.name);
            }
        }

        // Collect keys that need to be renamed
        let renames: Vec<(String, &str)> = map
            .keys()
            .filter_map(|k| alias_map.get(k.as_str()).map(|v| (k.clone(), *v)))
            .collect();

        // Apply renames
        for (old_key, new_key) in renames {
            if let Some(value) = map.remove(&old_key) {
                // Only insert if the target key doesn't already exist
                map.entry(new_key.to_string()).or_insert(value);
            }
        }
    }
}

/// Apply serialization aliases to JSON output.
///
/// This transforms output keys from field names to their serialization_alias
/// or alias, enabling proper JSON output format.
///
/// # Arguments
///
/// * `json` - The JSON value to transform (modified in place)
/// * `fields` - The field metadata containing alias information
pub fn apply_serialization_aliases(json: &mut serde_json::Value, fields: &[crate::FieldInfo]) {
    if let serde_json::Value::Object(map) = json {
        // Build a mapping from field_name -> output_alias
        let mut alias_map: HashMap<&str, &str> = HashMap::new();
        for field in fields {
            // serialization_alias takes precedence for output
            if let Some(alias) = field.serialization_alias {
                alias_map.insert(field.name, alias);
            } else if let Some(alias) = field.alias {
                // Regular alias is fallback for output
                alias_map.insert(field.name, alias);
            }
        }

        // Collect keys that need to be renamed
        let renames: Vec<(String, &str)> = map
            .keys()
            .filter_map(|k| alias_map.get(k.as_str()).map(|v| (k.clone(), *v)))
            .collect();

        // Apply renames
        for (old_key, new_key) in renames {
            if let Some(value) = map.remove(&old_key) {
                map.insert(new_key.to_string(), value);
            }
        }
    }
}

/// Model-aware validation that supports field aliases.
///
/// Unlike the generic `ModelValidate`, this trait uses the `Model::fields()`
/// metadata to transform aliased input keys to their actual field names
/// before deserialization.
///
/// # Example
///
/// ```ignore
/// #[derive(Model, Serialize, Deserialize)]
/// struct User {
///     #[sqlmodel(validation_alias = "userName")]
///     name: String,
/// }
///
/// // Input with alias key works
/// let user = User::sql_model_validate(r#"{"userName": "Alice"}"#)?;
/// assert_eq!(user.name, "Alice");
/// ```
pub trait SqlModelValidate: Model + DeserializeOwned + Sized {
    /// Create and validate a model from input, applying validation aliases.
    fn sql_model_validate(
        input: impl Into<ValidateInput>,
        options: ValidateOptions,
    ) -> ValidateResult<Self> {
        let input = input.into();

        // Convert input to serde_json::Value
        let mut json_value = match input {
            ValidateInput::Dict(dict) => {
                let map: serde_json::Map<String, serde_json::Value> = dict
                    .into_iter()
                    .map(|(k, v)| (k, value_to_json(v)))
                    .collect();
                serde_json::Value::Object(map)
            }
            ValidateInput::Json(json_str) => serde_json::from_str(&json_str).map_err(|e| {
                let mut err = ValidationError::new();
                err.add(
                    "_json",
                    ValidationErrorKind::Custom,
                    format!("Invalid JSON: {e}"),
                );
                err
            })?,
            ValidateInput::JsonValue(value) => value,
        };

        // Apply validation aliases before deserialization
        apply_validation_aliases(&mut json_value, Self::fields());

        // Apply update values if provided
        if let Some(update) = options.update {
            if let serde_json::Value::Object(ref mut map) = json_value {
                for (key, value) in update {
                    map.insert(key, value);
                }
            }
        }

        // Deserialize
        serde_json::from_value(json_value).map_err(|e| {
            let mut err = ValidationError::new();
            err.add(
                "_model",
                ValidationErrorKind::Custom,
                format!("Validation failed: {e}"),
            );
            err
        })
    }

    /// Create and validate a model from input, also tracking which fields were explicitly set.
    ///
    /// This enables Pydantic-compatible `exclude_unset` behavior when dumping via `TrackedModel`.
    fn sql_model_validate_tracked(
        input: impl Into<ValidateInput>,
        options: ValidateOptions,
    ) -> ValidateResult<crate::TrackedModel<Self>> {
        let input = input.into();

        let mut json_value = match input {
            ValidateInput::Dict(dict) => {
                let map: serde_json::Map<String, serde_json::Value> = dict
                    .into_iter()
                    .map(|(k, v)| (k, value_to_json(v)))
                    .collect();
                serde_json::Value::Object(map)
            }
            ValidateInput::Json(json_str) => serde_json::from_str(&json_str).map_err(|e| {
                let mut err = ValidationError::new();
                err.add(
                    "_json",
                    ValidationErrorKind::Custom,
                    format!("Invalid JSON: {e}"),
                );
                err
            })?,
            ValidateInput::JsonValue(value) => value,
        };

        apply_validation_aliases(&mut json_value, Self::fields());

        if let Some(update) = options.update {
            if let serde_json::Value::Object(ref mut map) = json_value {
                for (key, value) in update {
                    map.insert(key, value);
                }
            }
        }

        // Compute fields_set from the (post-alias) object keys.
        let mut fields_set = crate::FieldsSet::empty(Self::fields().len());
        if let serde_json::Value::Object(ref map) = json_value {
            for (idx, field) in Self::fields().iter().enumerate() {
                if map.contains_key(field.name) {
                    fields_set.set(idx);
                }
            }
        }

        let model = serde_json::from_value(json_value).map_err(|e| {
            let mut err = ValidationError::new();
            err.add(
                "_model",
                ValidationErrorKind::Custom,
                format!("Validation failed: {e}"),
            );
            err
        })?;

        Ok(crate::TrackedModel::new(model, fields_set))
    }

    /// Create and validate a model from JSON string with default options.
    fn sql_model_validate_json(json: &str) -> ValidateResult<Self> {
        Self::sql_model_validate(json, ValidateOptions::default())
    }

    /// Create and validate a model from a HashMap with default options.
    fn sql_model_validate_dict(dict: HashMap<String, Value>) -> ValidateResult<Self> {
        Self::sql_model_validate(dict, ValidateOptions::default())
    }
}

/// Blanket implementation for all Model types that implement DeserializeOwned.
impl<T: Model + DeserializeOwned> SqlModelValidate for T {}

/// Model-aware dump that supports field aliases and computed field exclusion.
///
/// Unlike the generic `ModelDump`, this trait uses the `Model::fields()`
/// metadata to transform field names to their serialization aliases
/// in the output and to handle computed fields properly.
///
/// # Example
///
/// ```ignore
/// #[derive(Model, Serialize, Deserialize)]
/// struct User {
///     #[sqlmodel(serialization_alias = "userName")]
///     name: String,
///     #[sqlmodel(computed)]
///     full_name: String, // Derived field, not in DB
/// }
///
/// let user = User { name: "Alice".to_string(), full_name: "Alice Smith".to_string() };
/// let json = user.sql_model_dump(DumpOptions::default().by_alias())?;
/// assert_eq!(json["userName"], "Alice");
///
/// // Exclude computed fields
/// let json = user.sql_model_dump(DumpOptions::default().exclude_computed_fields())?;
/// assert!(json.get("full_name").is_none());
/// ```
pub trait SqlModelDump: Model + serde::Serialize {
    /// Serialize a model to a JSON value, optionally applying aliases.
    fn sql_model_dump(&self, options: DumpOptions) -> DumpResult {
        if options.exclude_unset {
            return Err(dump_options_unsupported(
                "DumpOptions.exclude_unset requires fields_set tracking; use SqlModelValidate::sql_model_validate_tracked(...) or the tracked!(Type { .. }) macro",
            ));
        }

        // First, serialize to JSON value
        let mut value = serde_json::to_value(self)?;

        // Apply options that work on original field names BEFORE alias renaming
        if let serde_json::Value::Object(ref mut map) = value {
            // Always honor per-field exclude flag (Pydantic Field(exclude=True) semantics).
            for field in Self::fields() {
                if field.exclude {
                    map.remove(field.name);
                }
            }

            // Exclude computed fields if requested (must happen before alias renaming)
            if options.exclude_computed_fields {
                let computed_field_names: std::collections::HashSet<&str> = Self::fields()
                    .iter()
                    .filter(|f| f.computed)
                    .map(|f| f.name)
                    .collect();
                map.retain(|k, _| !computed_field_names.contains(k.as_str()));
            }

            // Exclude fields with default values if requested
            if options.exclude_defaults {
                for field in Self::fields() {
                    if let Some(default_json) = field.default_json {
                        if let Some(current_value) = map.get(field.name) {
                            // Parse the default JSON and compare
                            if let Ok(default_value) =
                                serde_json::from_str::<serde_json::Value>(default_json)
                            {
                                if current_value == &default_value {
                                    map.remove(field.name);
                                }
                            }
                        }
                    }
                }
            }
        }

        // Apply serialization aliases if by_alias is set
        if options.by_alias {
            apply_serialization_aliases(&mut value, Self::fields());
        }

        // Apply remaining options (include/exclude work on the final key names)
        if let serde_json::Value::Object(ref mut map) = value {
            // Apply include filter
            if let Some(ref include) = options.include {
                map.retain(|k, _| include.contains(k));
            }

            // Apply exclude filter
            if let Some(ref exclude) = options.exclude {
                map.retain(|k, _| !exclude.contains(k));
            }

            // Apply exclude_none filter
            if options.exclude_none {
                map.retain(|_, v| !v.is_null());
            }
        }

        Ok(value)
    }

    /// Serialize a model to a JSON string with default options.
    fn sql_model_dump_json(&self) -> std::result::Result<String, serde_json::Error> {
        let value = self.sql_model_dump(DumpOptions::default())?;
        serde_json::to_string(&value)
    }

    /// Serialize a model to a pretty-printed JSON string.
    fn sql_model_dump_json_pretty(&self) -> std::result::Result<String, serde_json::Error> {
        let value = self.sql_model_dump(DumpOptions::default())?;
        serde_json::to_string_pretty(&value)
    }

    /// Serialize with aliases to a JSON string.
    fn sql_model_dump_json_by_alias(&self) -> std::result::Result<String, serde_json::Error> {
        let value = self.sql_model_dump(DumpOptions::default().by_alias())?;
        serde_json::to_string(&value)
    }

    /// Serialize a model to a JSON string with full options support.
    ///
    /// This method supports all DumpOptions including the `indent` option:
    /// - `indent: None` - compact JSON output
    /// - `indent: Some(n)` - pretty-printed with n spaces indentation
    ///
    /// Compared to `model_dump_json_with_options`, this method also applies
    /// Model-specific transformations like serialization aliases.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use sqlmodel_core::validate::{SqlModelDump, DumpOptions};
    ///
    /// // With aliases and 2-space indent
    /// let json = user.sql_model_dump_json_with_options(
    ///     DumpOptions::default().by_alias().indent(2)
    /// )?;
    /// ```
    fn sql_model_dump_json_with_options(
        &self,
        options: DumpOptions,
    ) -> std::result::Result<String, serde_json::Error> {
        let value = self.sql_model_dump(DumpOptions {
            indent: None, // Don't pass indent to sql_model_dump (it returns Value, not String)
            ..options.clone()
        })?;

        match options.indent {
            Some(spaces) => {
                let indent_bytes = " ".repeat(spaces).into_bytes();
                let formatter = serde_json::ser::PrettyFormatter::with_indent(&indent_bytes);
                let mut writer = Vec::new();
                let mut ser = serde_json::Serializer::with_formatter(&mut writer, formatter);
                serde::Serialize::serialize(&value, &mut ser)?;
                // serde_json always produces valid UTF-8, but propagate error instead of panicking
                String::from_utf8(writer).map_err(|e| {
                    serde_json::Error::io(std::io::Error::new(
                        std::io::ErrorKind::InvalidData,
                        format!("UTF-8 encoding error: {e}"),
                    ))
                })
            }
            None => serde_json::to_string(&value),
        }
    }
}

/// Blanket implementation for all Model types that implement Serialize.
impl<T: Model + serde::Serialize> SqlModelDump for T {}

// ============================================================================
// Model Update (sqlmodel_update)
// ============================================================================

/// Input types for sqlmodel_update().
///
/// Supports updating models from various input formats.
#[derive(Debug, Clone)]
pub enum UpdateInput {
    /// A HashMap of field names to JSON values.
    Dict(HashMap<String, serde_json::Value>),
    /// A serde_json::Value for direct updating.
    JsonValue(serde_json::Value),
}

impl From<HashMap<String, serde_json::Value>> for UpdateInput {
    fn from(map: HashMap<String, serde_json::Value>) -> Self {
        UpdateInput::Dict(map)
    }
}

impl From<serde_json::Value> for UpdateInput {
    fn from(value: serde_json::Value) -> Self {
        UpdateInput::JsonValue(value)
    }
}

impl From<HashMap<String, Value>> for UpdateInput {
    fn from(map: HashMap<String, Value>) -> Self {
        let json_map: HashMap<String, serde_json::Value> = map
            .into_iter()
            .map(|(k, v)| (k, value_to_json(v)))
            .collect();
        UpdateInput::Dict(json_map)
    }
}

/// Options for sqlmodel_update().
#[derive(Debug, Clone, Default)]
pub struct UpdateOptions {
    /// Only update these fields (if Some). Other fields in the source are ignored.
    pub update_fields: Option<std::collections::HashSet<String>>,
}

impl UpdateOptions {
    /// Create new default options.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set fields to update.
    pub fn update_fields(mut self, fields: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.update_fields = Some(fields.into_iter().map(Into::into).collect());
        self
    }
}

/// Trait for models that support sqlmodel_update().
///
/// This enables updating a model instance from a dictionary or another model's values.
///
/// # Example
///
/// ```ignore
/// use sqlmodel_core::validate::{SqlModelUpdate, UpdateInput, UpdateOptions};
///
/// let mut user = User { id: 1, name: "Alice".to_string(), age: 30 };
///
/// // Update from a HashMap
/// user.sqlmodel_update(
///     HashMap::from([("name".to_string(), serde_json::json!("Bob"))]),
///     UpdateOptions::default()
/// )?;
/// assert_eq!(user.name, "Bob");
///
/// // Update only specific fields
/// user.sqlmodel_update(
///     HashMap::from([
///         ("name".to_string(), serde_json::json!("Carol")),
///         ("age".to_string(), serde_json::json!(25))
///     ]),
///     UpdateOptions::default().update_fields(["name"])
/// )?;
/// assert_eq!(user.name, "Carol");
/// assert_eq!(user.age, 30); // age was not updated
/// ```
pub trait SqlModelUpdate: Model + serde::Serialize + DeserializeOwned {
    /// Update a model instance from input.
    ///
    /// This method merges values from the input into the current model.
    /// Only fields present in the input (and allowed by `update_fields` option)
    /// are updated.
    ///
    /// # Arguments
    ///
    /// * `input` - The source of update values (Dict or JsonValue)
    /// * `options` - Update options controlling which fields to update
    ///
    /// # Returns
    ///
    /// Ok(()) if the update succeeds, or a validation error if the resulting
    /// model fails validation.
    fn sqlmodel_update(
        &mut self,
        input: impl Into<UpdateInput>,
        options: UpdateOptions,
    ) -> ValidateResult<()> {
        let input = input.into();

        // Convert input to a map
        let update_map = match input {
            UpdateInput::Dict(map) => map,
            UpdateInput::JsonValue(value) => {
                if let serde_json::Value::Object(map) = value {
                    map.into_iter().collect()
                } else {
                    let mut err = ValidationError::new();
                    err.add(
                        "_update",
                        ValidationErrorKind::Custom,
                        "Update input must be an object".to_string(),
                    );
                    return Err(err);
                }
            }
        };

        // Serialize current model to JSON
        let mut current = serde_json::to_value(&*self).map_err(|e| {
            let mut err = ValidationError::new();
            err.add(
                "_model",
                ValidationErrorKind::Custom,
                format!("Failed to serialize model: {e}"),
            );
            err
        })?;

        // Get valid field names from model metadata
        let valid_fields: std::collections::HashSet<&str> =
            Self::fields().iter().map(|f| f.name).collect();

        // Update the current JSON with new values
        if let serde_json::Value::Object(ref mut current_map) = current {
            for (key, value) in update_map {
                // Check if field is valid
                if !valid_fields.contains(key.as_str()) {
                    let mut err = ValidationError::new();
                    err.add(
                        &key,
                        ValidationErrorKind::Custom,
                        format!("Unknown field: {key}"),
                    );
                    return Err(err);
                }

                // Check if field is allowed by update_fields option
                if let Some(ref allowed) = options.update_fields {
                    if !allowed.contains(&key) {
                        continue; // Skip fields not in update_fields
                    }
                }

                // Update the field
                current_map.insert(key, value);
            }
        }

        // Deserialize back to model (this also validates)
        let updated: Self = serde_json::from_value(current).map_err(|e| {
            let mut err = ValidationError::new();
            err.add(
                "_model",
                ValidationErrorKind::Custom,
                format!("Update failed validation: {e}"),
            );
            err
        })?;

        // Replace self with the updated model
        *self = updated;

        Ok(())
    }

    /// Update a model instance from a HashMap with default options.
    fn sqlmodel_update_dict(
        &mut self,
        dict: HashMap<String, serde_json::Value>,
    ) -> ValidateResult<()> {
        self.sqlmodel_update(dict, UpdateOptions::default())
    }

    /// Copy non-None/non-null values from another model into this one.
    ///
    /// This is useful for partial updates where you have a "patch" model
    /// with only the fields that should be updated (non-None values).
    ///
    /// # Arguments
    ///
    /// * `source` - The source model to copy values from
    /// * `options` - Update options controlling which fields to update
    ///
    /// # Example
    ///
    /// ```ignore
    /// let mut user = User { id: 1, name: "Alice".to_string(), age: Some(30) };
    /// let patch = User { id: 0, name: "Bob".to_string(), age: None };
    ///
    /// // Only update name, skip None age
    /// user.sqlmodel_update_from(&patch, UpdateOptions::default())?;
    /// assert_eq!(user.name, "Bob");
    /// assert_eq!(user.age, Some(30)); // Unchanged because patch.age is None
    /// ```
    fn sqlmodel_update_from(&mut self, source: &Self, options: UpdateOptions) -> ValidateResult<()>
    where
        Self: Sized,
    {
        // Serialize source to JSON
        let source_json = serde_json::to_value(source).map_err(|e| {
            let mut err = ValidationError::new();
            err.add(
                "_source",
                ValidationErrorKind::Custom,
                format!("Failed to serialize source model: {e}"),
            );
            err
        })?;

        // Filter out null values (None fields)
        let update_map: HashMap<String, serde_json::Value> =
            if let serde_json::Value::Object(map) = source_json {
                map.into_iter().filter(|(_, v)| !v.is_null()).collect()
            } else {
                let mut err = ValidationError::new();
                err.add(
                    "_source",
                    ValidationErrorKind::Custom,
                    "Source model must serialize to an object".to_string(),
                );
                return Err(err);
            };

        self.sqlmodel_update(update_map, options)
    }
}

/// Blanket implementation for all Model types that implement Serialize + DeserializeOwned.
impl<T: Model + serde::Serialize + DeserializeOwned> SqlModelUpdate for T {}

#[cfg(test)]
mod tests {
    use super::*;
    use serde::{Deserialize, Serialize};

    #[test]
    fn test_matches_email_pattern() {
        let email_pattern = r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$";

        assert!(matches_pattern("test@example.com", email_pattern));
        assert!(matches_pattern("user.name+tag@domain.org", email_pattern));
        assert!(!matches_pattern("invalid", email_pattern));
        assert!(!matches_pattern("@example.com", email_pattern));
        assert!(!matches_pattern("test@", email_pattern));
    }

    #[test]
    fn test_matches_url_pattern() {
        let url_pattern = r"^https?://[^\s/$.?#].[^\s]*$";

        assert!(matches_pattern("https://example.com", url_pattern));
        assert!(matches_pattern("http://example.com/path", url_pattern));
        assert!(!matches_pattern("ftp://example.com", url_pattern));
        assert!(!matches_pattern("not a url", url_pattern));
    }

    #[test]
    fn test_matches_phone_pattern() {
        let phone_pattern = r"^\+?[1-9]\d{1,14}$";

        assert!(matches_pattern("+12025551234", phone_pattern));
        assert!(matches_pattern("12025551234", phone_pattern));
        assert!(!matches_pattern("0123456789", phone_pattern)); // Can't start with 0
        assert!(!matches_pattern("abc", phone_pattern));
    }

    #[test]
    fn test_matches_uuid_pattern() {
        let uuid_pattern =
            r"^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$";

        assert!(matches_pattern(
            "550e8400-e29b-41d4-a716-446655440000",
            uuid_pattern
        ));
        assert!(matches_pattern(
            "550E8400-E29B-41D4-A716-446655440000",
            uuid_pattern
        ));
        assert!(!matches_pattern("invalid-uuid", uuid_pattern));
        assert!(!matches_pattern(
            "550e8400e29b41d4a716446655440000",
            uuid_pattern
        ));
    }

    #[test]
    fn test_matches_alphanumeric_pattern() {
        let alphanumeric_pattern = r"^[a-zA-Z0-9]+$";

        assert!(matches_pattern("abc123", alphanumeric_pattern));
        assert!(matches_pattern("ABC", alphanumeric_pattern));
        assert!(matches_pattern("123", alphanumeric_pattern));
        assert!(!matches_pattern("abc-123", alphanumeric_pattern));
        assert!(!matches_pattern("hello world", alphanumeric_pattern));
    }

    #[test]
    fn test_invalid_pattern_returns_false() {
        // Invalid regex pattern (unclosed bracket)
        let invalid_pattern = r"[unclosed";
        assert!(!matches_pattern("anything", invalid_pattern));
    }

    #[test]
    fn test_validate_pattern_valid() {
        assert!(validate_pattern(r"^[a-z]+$").is_none());
        assert!(validate_pattern(r"^\d{4}-\d{2}-\d{2}$").is_none());
    }

    #[test]
    fn test_validate_pattern_invalid() {
        let result = validate_pattern(r"[unclosed");
        assert!(result.is_some());
        assert!(result.unwrap().contains("invalid regex pattern"));
    }

    #[test]
    fn test_regex_caching() {
        let pattern = r"^test\d+$";

        // First call compiles the regex
        assert!(matches_pattern("test123", pattern));

        // Second call should use cached regex
        assert!(matches_pattern("test456", pattern));
        assert!(!matches_pattern("invalid", pattern));
    }

    #[test]
    fn test_empty_string() {
        let pattern = r"^.+$"; // At least one character
        assert!(!matches_pattern("", pattern));

        let empty_allowed = r"^.*$"; // Zero or more characters
        assert!(matches_pattern("", empty_allowed));
    }

    #[test]
    fn test_special_characters() {
        let pattern = r"^[a-z]+$";
        assert!(!matches_pattern("hello<script>", pattern));
        assert!(!matches_pattern("test'; DROP TABLE users;--", pattern));
    }

    // =========================================================================
    // model_validate tests
    // =========================================================================

    #[derive(Debug, Clone, PartialEq, serde::Deserialize, serde::Serialize)]
    struct TestUser {
        name: String,
        age: i32,
        #[serde(default)]
        active: bool,
    }

    #[test]
    fn test_model_validate_from_json() {
        let json = r#"{"name": "Alice", "age": 30}"#;
        let user: TestUser = TestUser::model_validate_json(json).unwrap();
        assert_eq!(user.name, "Alice");
        assert_eq!(user.age, 30);
        assert!(!user.active); // default
    }

    #[test]
    fn test_model_validate_from_json_value() {
        let json_value = serde_json::json!({"name": "Bob", "age": 25, "active": true});
        let user: TestUser =
            TestUser::model_validate(json_value, ValidateOptions::default()).unwrap();
        assert_eq!(user.name, "Bob");
        assert_eq!(user.age, 25);
        assert!(user.active);
    }

    #[test]
    fn test_model_validate_from_dict() {
        let mut dict = HashMap::new();
        dict.insert("name".to_string(), Value::Text("Charlie".to_string()));
        dict.insert("age".to_string(), Value::Int(35));
        dict.insert("active".to_string(), Value::Bool(true));

        let user: TestUser = TestUser::model_validate_dict(dict).unwrap();
        assert_eq!(user.name, "Charlie");
        assert_eq!(user.age, 35);
        assert!(user.active);
    }

    #[test]
    fn test_model_validate_invalid_json() {
        let json = r#"{"name": "Invalid"}"#; // missing required 'age' field
        let result: ValidateResult<TestUser> = TestUser::model_validate_json(json);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(!err.is_empty());
    }

    #[test]
    fn test_model_validate_malformed_json() {
        let json = r#"{"name": "Alice", age: 30}"#; // invalid JSON syntax
        let result: ValidateResult<TestUser> = TestUser::model_validate_json(json);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(
            err.errors
                .iter()
                .any(|e| e.message.contains("Invalid JSON"))
        );
    }

    #[test]
    fn test_model_validate_with_update() {
        let json = r#"{"name": "Original", "age": 20}"#;
        let mut update = HashMap::new();
        update.insert("name".to_string(), serde_json::json!("Updated"));

        let options = ValidateOptions::new().with_update(update);
        let user: TestUser = TestUser::model_validate(json, options).unwrap();
        assert_eq!(user.name, "Updated"); // overridden by update
        assert_eq!(user.age, 20);
    }

    #[test]
    fn test_model_validate_strict_mode() {
        let json = r#"{"name": "Alice", "age": 30}"#;
        let options = ValidateOptions::new().strict();
        let user: TestUser = TestUser::model_validate(json, options).unwrap();
        assert_eq!(user.name, "Alice");
        assert_eq!(user.age, 30);
    }

    #[test]
    fn test_validate_options_builder() {
        let mut context = HashMap::new();
        context.insert("key".to_string(), serde_json::json!("value"));

        let options = ValidateOptions::new()
            .strict()
            .from_attributes()
            .with_context(context.clone());

        assert!(options.strict);
        assert!(options.from_attributes);
        assert!(options.context.is_some());
        assert_eq!(
            options.context.unwrap().get("key"),
            Some(&serde_json::json!("value"))
        );
    }

    #[test]
    fn test_validate_input_from_conversions() {
        // From String
        let input: ValidateInput = "{}".to_string().into();
        assert!(matches!(input, ValidateInput::Json(_)));

        // From &str
        let input: ValidateInput = "{}".into();
        assert!(matches!(input, ValidateInput::Json(_)));

        // From serde_json::Value
        let input: ValidateInput = serde_json::json!({}).into();
        assert!(matches!(input, ValidateInput::JsonValue(_)));

        // From HashMap
        let map: HashMap<String, Value> = HashMap::new();
        let input: ValidateInput = map.into();
        assert!(matches!(input, ValidateInput::Dict(_)));
    }

    #[test]
    fn test_value_to_json_conversions() {
        assert_eq!(value_to_json(Value::Null), serde_json::Value::Null);
        assert_eq!(value_to_json(Value::Bool(true)), serde_json::json!(true));
        assert_eq!(value_to_json(Value::Int(42)), serde_json::json!(42));
        assert_eq!(value_to_json(Value::BigInt(100)), serde_json::json!(100));
        assert_eq!(
            value_to_json(Value::Text("hello".to_string())),
            serde_json::json!("hello")
        );
        // UUID is [u8; 16]
        let uuid_bytes: [u8; 16] = [
            0x55, 0x0e, 0x84, 0x00, 0xe2, 0x9b, 0x41, 0xd4, 0xa7, 0x16, 0x44, 0x66, 0x55, 0x44,
            0x00, 0x00,
        ];
        assert_eq!(
            value_to_json(Value::Uuid(uuid_bytes)),
            serde_json::json!("550e8400-e29b-41d4-a716-446655440000")
        );

        // Array conversion
        let arr = vec![Value::Int(1), Value::Int(2), Value::Int(3)];
        assert_eq!(
            value_to_json(Value::Array(arr)),
            serde_json::json!([1, 2, 3])
        );
    }

    // =========================================================================
    // model_dump tests
    // =========================================================================

    #[derive(Debug, Clone, PartialEq, serde::Deserialize, serde::Serialize)]
    struct TestProduct {
        name: String,
        price: f64,
        #[serde(skip_serializing_if = "Option::is_none")]
        description: Option<String>,
    }

    #[test]
    fn test_model_dump_default() {
        let product = TestProduct {
            name: "Widget".to_string(),
            price: 19.99,
            description: Some("A useful widget".to_string()),
        };
        let json = product.model_dump(DumpOptions::default()).unwrap();
        assert_eq!(json["name"], "Widget");
        assert_eq!(json["price"], 19.99);
        assert_eq!(json["description"], "A useful widget");
    }

    #[test]
    fn test_model_dump_json() {
        let product = TestProduct {
            name: "Gadget".to_string(),
            price: 29.99,
            description: None,
        };
        let json_str = product.model_dump_json().unwrap();
        assert!(json_str.contains("Gadget"));
        assert!(json_str.contains("29.99"));
    }

    #[test]
    fn test_model_dump_json_pretty() {
        let product = TestProduct {
            name: "Gadget".to_string(),
            price: 29.99,
            description: None,
        };
        let json_str = product.model_dump_json_pretty().unwrap();
        // Pretty print should have newlines
        assert!(json_str.contains('\n'));
        assert!(json_str.contains("Gadget"));
    }

    #[test]
    fn test_model_dump_json_with_options_compact() {
        let product = TestProduct {
            name: "Widget".to_string(),
            price: 19.99,
            description: Some("A widget".to_string()),
        };

        // Compact JSON (no indent)
        let json_str = product
            .model_dump_json_with_options(DumpOptions::default())
            .unwrap();
        assert!(!json_str.contains('\n')); // No newlines in compact mode
        assert!(json_str.contains("Widget"));
        assert!(json_str.contains("19.99"));
    }

    #[test]
    fn test_model_dump_json_with_options_indent() {
        let product = TestProduct {
            name: "Widget".to_string(),
            price: 19.99,
            description: Some("A widget".to_string()),
        };

        // 2-space indentation
        let json_str = product
            .model_dump_json_with_options(DumpOptions::default().indent(2))
            .unwrap();
        assert!(json_str.contains('\n')); // Has newlines
        assert!(json_str.contains("  \"name\"")); // 2-space indent
        assert!(json_str.contains("Widget"));

        // 4-space indentation
        let json_str = product
            .model_dump_json_with_options(DumpOptions::default().indent(4))
            .unwrap();
        assert!(json_str.contains("    \"name\"")); // 4-space indent
    }

    #[test]
    fn test_model_dump_json_with_options_combined() {
        let product = TestProduct {
            name: "Widget".to_string(),
            price: 19.99,
            description: Some("A widget".to_string()),
        };

        // Combine indent with exclude
        let json_str = product
            .model_dump_json_with_options(DumpOptions::default().exclude(["price"]).indent(2))
            .unwrap();
        assert!(json_str.contains('\n')); // Has newlines
        assert!(json_str.contains("Widget"));
        assert!(!json_str.contains("19.99")); // price is excluded
    }

    #[test]
    fn test_dump_options_indent_builder() {
        let options = DumpOptions::new().indent(4);
        assert_eq!(options.indent, Some(4));

        // Can combine with other options
        let options2 = DumpOptions::new()
            .indent(2)
            .by_alias()
            .exclude(["password"]);
        assert_eq!(options2.indent, Some(2));
        assert!(options2.by_alias);
        assert!(options2.exclude.unwrap().contains("password"));
    }

    #[test]
    fn test_model_dump_include() {
        let product = TestProduct {
            name: "Widget".to_string(),
            price: 19.99,
            description: Some("A widget".to_string()),
        };
        let options = DumpOptions::new().include(["name"]);
        let json = product.model_dump(options).unwrap();
        assert!(json.get("name").is_some());
        assert!(json.get("price").is_none());
        assert!(json.get("description").is_none());
    }

    #[test]
    fn test_model_dump_exclude() {
        let product = TestProduct {
            name: "Widget".to_string(),
            price: 19.99,
            description: Some("A widget".to_string()),
        };
        let options = DumpOptions::new().exclude(["description"]);
        let json = product.model_dump(options).unwrap();
        assert!(json.get("name").is_some());
        assert!(json.get("price").is_some());
        assert!(json.get("description").is_none());
    }

    #[test]
    fn test_model_dump_exclude_none() {
        let product = TestProduct {
            name: "Widget".to_string(),
            price: 19.99,
            description: None,
        };
        // Note: serde skip_serializing_if already handles this
        // But we can still test the exclude_none flag
        let options = DumpOptions::new().exclude_none();
        let json = product.model_dump(options).unwrap();
        assert!(json.get("name").is_some());
        // description would be None, but serde already skips it
    }

    #[test]
    fn test_dump_options_builder() {
        let options = DumpOptions::new()
            .json()
            .include(["name", "age"])
            .exclude(["password"])
            .by_alias()
            .exclude_none()
            .exclude_defaults()
            .round_trip();

        assert_eq!(options.mode, DumpMode::Json);
        assert!(options.include.is_some());
        assert!(options.exclude.is_some());
        assert!(options.by_alias);
        assert!(options.exclude_none);
        assert!(options.exclude_defaults);
        assert!(options.round_trip);
    }

    #[test]
    fn test_dump_mode_default() {
        assert_eq!(DumpMode::default(), DumpMode::Json);
    }

    #[test]
    fn test_model_dump_include_exclude_combined() {
        let user = TestUser {
            name: "Alice".to_string(),
            age: 30,
            active: true,
        };
        // Include name and age, but exclude age
        let options = DumpOptions::new().include(["name", "age"]).exclude(["age"]);
        let json = user.model_dump(options).unwrap();
        // Include is applied first, then exclude
        assert!(json.get("name").is_some());
        assert!(json.get("age").is_none());
        assert!(json.get("active").is_none());
    }

    #[test]
    fn test_model_dump_accepts_python_mode_and_round_trip() {
        let product = TestProduct {
            name: "Widget".to_string(),
            price: 19.99,
            description: Some("A useful widget".to_string()),
        };
        let json = product
            .model_dump(DumpOptions::default().python().round_trip())
            .unwrap();

        assert_eq!(json["name"], "Widget");
        assert_eq!(json["price"], 19.99);
        assert_eq!(json["description"], "A useful widget");
    }

    // ========================================================================
    // Alias Tests
    // ========================================================================

    use crate::{FieldInfo, Row, SqlType};

    /// Test model with aliases for validation and serialization tests.
    #[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
    struct TestAliasedUser {
        id: i64,
        name: String,
        email: String,
    }

    impl Model for TestAliasedUser {
        const TABLE_NAME: &'static str = "users";
        const PRIMARY_KEY: &'static [&'static str] = &["id"];

        fn fields() -> &'static [FieldInfo] {
            static FIELDS: &[FieldInfo] = &[
                FieldInfo::new("id", "id", SqlType::BigInt).primary_key(true),
                FieldInfo::new("name", "name", SqlType::Text)
                    .validation_alias("userName")
                    .serialization_alias("displayName"),
                FieldInfo::new("email", "email", SqlType::Text).alias("emailAddress"), // Both input and output
            ];
            FIELDS
        }

        fn to_row(&self) -> Vec<(&'static str, Value)> {
            vec![
                ("id", Value::BigInt(self.id)),
                ("name", Value::Text(self.name.clone())),
                ("email", Value::Text(self.email.clone())),
            ]
        }

        fn from_row(row: &Row) -> crate::Result<Self> {
            Ok(Self {
                id: row.get_named("id")?,
                name: row.get_named("name")?,
                email: row.get_named("email")?,
            })
        }

        fn primary_key_value(&self) -> Vec<Value> {
            vec![Value::BigInt(self.id)]
        }

        fn is_new(&self) -> bool {
            false
        }
    }

    #[test]
    fn test_apply_validation_aliases() {
        let fields = TestAliasedUser::fields();

        // Test with validation_alias
        let mut json = serde_json::json!({
            "id": 1,
            "userName": "Alice",
            "email": "alice@example.com"
        });
        apply_validation_aliases(&mut json, fields);

        // userName should be renamed to name
        assert_eq!(json["name"], "Alice");
        assert!(json.get("userName").is_none());

        // Test with regular alias
        let mut json2 = serde_json::json!({
            "id": 1,
            "name": "Bob",
            "emailAddress": "bob@example.com"
        });
        apply_validation_aliases(&mut json2, fields);

        // emailAddress should be renamed to email
        assert_eq!(json2["email"], "bob@example.com");
        assert!(json2.get("emailAddress").is_none());
    }

    #[test]
    fn test_apply_serialization_aliases() {
        let fields = TestAliasedUser::fields();

        let mut json = serde_json::json!({
            "id": 1,
            "name": "Alice",
            "email": "alice@example.com"
        });
        apply_serialization_aliases(&mut json, fields);

        // name should be renamed to displayName (serialization_alias)
        assert_eq!(json["displayName"], "Alice");
        assert!(json.get("name").is_none());

        // email should be renamed to emailAddress (regular alias)
        assert_eq!(json["emailAddress"], "alice@example.com");
        assert!(json.get("email").is_none());
    }

    #[test]
    fn test_sql_model_validate_with_validation_alias() {
        // Use validation_alias in input
        let json = r#"{"id": 1, "userName": "Alice", "email": "alice@example.com"}"#;
        let user: TestAliasedUser = TestAliasedUser::sql_model_validate_json(json).unwrap();

        assert_eq!(user.id, 1);
        assert_eq!(user.name, "Alice");
        assert_eq!(user.email, "alice@example.com");
    }

    #[test]
    fn test_sql_model_validate_with_regular_alias() {
        // Use regular alias in input
        let json = r#"{"id": 1, "name": "Bob", "emailAddress": "bob@example.com"}"#;
        let user: TestAliasedUser = TestAliasedUser::sql_model_validate_json(json).unwrap();

        assert_eq!(user.id, 1);
        assert_eq!(user.name, "Bob");
        assert_eq!(user.email, "bob@example.com");
    }

    #[test]
    fn test_sql_model_validate_with_field_name() {
        // Use actual field name (should still work)
        let json = r#"{"id": 1, "name": "Charlie", "email": "charlie@example.com"}"#;
        let user: TestAliasedUser = TestAliasedUser::sql_model_validate_json(json).unwrap();

        assert_eq!(user.id, 1);
        assert_eq!(user.name, "Charlie");
        assert_eq!(user.email, "charlie@example.com");
    }

    #[test]
    fn test_sql_model_dump_by_alias() {
        let user = TestAliasedUser {
            id: 1,
            name: "Alice".to_string(),
            email: "alice@example.com".to_string(),
        };

        let json = user
            .sql_model_dump(DumpOptions::default().by_alias())
            .unwrap();

        // name should be serialized as displayName
        assert_eq!(json["displayName"], "Alice");
        assert!(json.get("name").is_none());

        // email should be serialized as emailAddress
        assert_eq!(json["emailAddress"], "alice@example.com");
        assert!(json.get("email").is_none());
    }

    #[test]
    fn test_sql_model_dump_without_alias() {
        let user = TestAliasedUser {
            id: 1,
            name: "Alice".to_string(),
            email: "alice@example.com".to_string(),
        };

        // Without by_alias, use original field names
        let json = user.sql_model_dump(DumpOptions::default()).unwrap();

        assert_eq!(json["name"], "Alice");
        assert_eq!(json["email"], "alice@example.com");
        assert!(json.get("displayName").is_none());
        assert!(json.get("emailAddress").is_none());
    }

    #[test]
    fn test_sql_model_dump_accepts_python_mode_and_round_trip() {
        let user = TestAliasedUser {
            id: 1,
            name: "Alice".to_string(),
            email: "alice@example.com".to_string(),
        };
        let json = user
            .sql_model_dump(DumpOptions::default().python().round_trip())
            .unwrap();

        assert_eq!(json["name"], "Alice");
        assert_eq!(json["email"], "alice@example.com");
    }

    #[test]
    fn test_tracked_model_dump_accepts_python_mode_and_round_trip() {
        let user = TestAliasedUser {
            id: 1,
            name: "Alice".to_string(),
            email: "alice@example.com".to_string(),
        };
        let tracked = crate::TrackedModel::all_fields_set(user);
        let json = tracked
            .sql_model_dump(DumpOptions::default().python().round_trip())
            .unwrap();

        assert_eq!(json["name"], "Alice");
        assert_eq!(json["email"], "alice@example.com");
    }

    #[test]
    fn test_alias_does_not_overwrite_existing() {
        let fields = TestAliasedUser::fields();

        // If both alias and field name are present, field name wins
        let mut json = serde_json::json!({
            "id": 1,
            "name": "FieldName",
            "userName": "AliasName",
            "email": "test@example.com"
        });
        apply_validation_aliases(&mut json, fields);

        // Original "name" field should be preserved
        assert_eq!(json["name"], "FieldName");
        // userName should be removed (but couldn't insert because "name" exists)
        assert!(json.get("userName").is_none());
    }

    // ========================================================================
    // Computed Field Tests
    // ========================================================================

    /// Test model with computed fields.
    #[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
    struct TestUserWithComputed {
        id: i64,
        first_name: String,
        last_name: String,
        #[serde(default)]
        full_name: String, // Computed field - derived from first_name + last_name
    }

    impl Model for TestUserWithComputed {
        const TABLE_NAME: &'static str = "users";
        const PRIMARY_KEY: &'static [&'static str] = &["id"];

        fn fields() -> &'static [FieldInfo] {
            static FIELDS: &[FieldInfo] = &[
                FieldInfo::new("id", "id", SqlType::BigInt).primary_key(true),
                FieldInfo::new("first_name", "first_name", SqlType::Text),
                FieldInfo::new("last_name", "last_name", SqlType::Text),
                FieldInfo::new("full_name", "full_name", SqlType::Text).computed(true),
            ];
            FIELDS
        }

        fn to_row(&self) -> Vec<(&'static str, Value)> {
            // Computed field is NOT included in DB operations
            vec![
                ("id", Value::BigInt(self.id)),
                ("first_name", Value::Text(self.first_name.clone())),
                ("last_name", Value::Text(self.last_name.clone())),
            ]
        }

        fn from_row(row: &Row) -> crate::Result<Self> {
            Ok(Self {
                id: row.get_named("id")?,
                first_name: row.get_named("first_name")?,
                last_name: row.get_named("last_name")?,
                // Computed field initialized with Default (empty string)
                full_name: String::new(),
            })
        }

        fn primary_key_value(&self) -> Vec<Value> {
            vec![Value::BigInt(self.id)]
        }

        fn is_new(&self) -> bool {
            false
        }
    }

    #[test]
    fn test_computed_field_included_by_default() {
        let user = TestUserWithComputed {
            id: 1,
            first_name: "John".to_string(),
            last_name: "Doe".to_string(),
            full_name: "John Doe".to_string(),
        };

        // By default, computed fields ARE included in model_dump
        let json = user.sql_model_dump(DumpOptions::default()).unwrap();

        assert_eq!(json["id"], 1);
        assert_eq!(json["first_name"], "John");
        assert_eq!(json["last_name"], "Doe");
        assert_eq!(json["full_name"], "John Doe"); // Computed field is present
    }

    #[test]
    fn test_computed_field_excluded_with_option() {
        let user = TestUserWithComputed {
            id: 1,
            first_name: "John".to_string(),
            last_name: "Doe".to_string(),
            full_name: "John Doe".to_string(),
        };

        // With exclude_computed_fields, computed fields are excluded
        let json = user
            .sql_model_dump(DumpOptions::default().exclude_computed_fields())
            .unwrap();

        assert_eq!(json["id"], 1);
        assert_eq!(json["first_name"], "John");
        assert_eq!(json["last_name"], "Doe");
        assert!(json.get("full_name").is_none()); // Computed field is excluded
    }

    #[test]
    fn test_computed_field_not_in_to_row() {
        let user = TestUserWithComputed {
            id: 1,
            first_name: "Jane".to_string(),
            last_name: "Smith".to_string(),
            full_name: "Jane Smith".to_string(),
        };

        // to_row() should not include computed field (for DB INSERT/UPDATE)
        let row = user.to_row();

        // Should have 3 fields: id, first_name, last_name
        assert_eq!(row.len(), 3);
        let field_names: Vec<&str> = row.iter().map(|(name, _)| *name).collect();
        assert!(field_names.contains(&"id"));
        assert!(field_names.contains(&"first_name"));
        assert!(field_names.contains(&"last_name"));
        assert!(!field_names.contains(&"full_name")); // Computed field NOT in row
    }

    #[test]
    fn test_computed_field_select_fields_excludes() {
        let fields = TestUserWithComputed::fields();

        // Check that computed field is marked
        let computed: Vec<&FieldInfo> = fields.iter().filter(|f| f.computed).collect();
        assert_eq!(computed.len(), 1);
        assert_eq!(computed[0].name, "full_name");

        // Non-computed fields
        let non_computed: Vec<&FieldInfo> = fields.iter().filter(|f| !f.computed).collect();
        assert_eq!(non_computed.len(), 3);
    }

    #[test]
    fn test_computed_field_with_other_dump_options() {
        let user = TestUserWithComputed {
            id: 1,
            first_name: "John".to_string(),
            last_name: "Doe".to_string(),
            full_name: "John Doe".to_string(),
        };

        // Combine exclude_computed_fields with include filter
        let json = user
            .sql_model_dump(DumpOptions::default().exclude_computed_fields().include([
                "id",
                "first_name",
                "full_name",
            ]))
            .unwrap();

        // full_name is excluded because it's computed, even though in include list
        // (exclude_computed_fields is applied before include filter)
        assert!(json.get("id").is_some());
        assert!(json.get("first_name").is_some());
        assert!(json.get("full_name").is_none()); // Excluded as computed
        assert!(json.get("last_name").is_none()); // Not in include list
    }

    #[test]
    fn test_dump_options_exclude_computed_fields_builder() {
        let options = DumpOptions::new().exclude_computed_fields();
        assert!(options.exclude_computed_fields);

        // Can combine with other options
        let options2 = DumpOptions::new()
            .exclude_computed_fields()
            .by_alias()
            .exclude_none();
        assert!(options2.exclude_computed_fields);
        assert!(options2.by_alias);
        assert!(options2.exclude_none);
    }

    /// Test model with both computed fields AND serialization aliases.
    #[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
    struct TestUserWithComputedAndAlias {
        id: i64,
        first_name: String,
        #[serde(default)]
        display_name: String, // Computed field that also has an alias
    }

    impl Model for TestUserWithComputedAndAlias {
        const TABLE_NAME: &'static str = "users";
        const PRIMARY_KEY: &'static [&'static str] = &["id"];

        fn fields() -> &'static [FieldInfo] {
            static FIELDS: &[FieldInfo] = &[
                FieldInfo::new("id", "id", SqlType::BigInt).primary_key(true),
                FieldInfo::new("first_name", "first_name", SqlType::Text)
                    .serialization_alias("firstName"),
                FieldInfo::new("display_name", "display_name", SqlType::Text)
                    .computed(true)
                    .serialization_alias("displayName"),
            ];
            FIELDS
        }

        fn to_row(&self) -> Vec<(&'static str, Value)> {
            vec![
                ("id", Value::BigInt(self.id)),
                ("first_name", Value::Text(self.first_name.clone())),
            ]
        }

        fn from_row(row: &Row) -> crate::Result<Self> {
            Ok(Self {
                id: row.get_named("id")?,
                first_name: row.get_named("first_name")?,
                display_name: String::new(),
            })
        }

        fn primary_key_value(&self) -> Vec<Value> {
            vec![Value::BigInt(self.id)]
        }

        fn is_new(&self) -> bool {
            false
        }
    }

    #[test]
    fn test_exclude_computed_with_by_alias() {
        // This test verifies that computed field exclusion works correctly
        // even when combined with by_alias (which renames keys)
        let user = TestUserWithComputedAndAlias {
            id: 1,
            first_name: "John".to_string(),
            display_name: "John Doe".to_string(),
        };

        // Test with by_alias only - computed field should still appear (aliased)
        let json = user
            .sql_model_dump(DumpOptions::default().by_alias())
            .unwrap();
        assert_eq!(json["firstName"], "John"); // first_name aliased
        assert_eq!(json["displayName"], "John Doe"); // display_name aliased (computed but not excluded)
        assert!(json.get("first_name").is_none()); // Original name should not exist
        assert!(json.get("display_name").is_none()); // Original name should not exist

        // Test with exclude_computed_fields only - computed field should be excluded
        let json = user
            .sql_model_dump(DumpOptions::default().exclude_computed_fields())
            .unwrap();
        assert_eq!(json["first_name"], "John");
        assert!(json.get("display_name").is_none()); // Computed field excluded

        // Test with BOTH by_alias AND exclude_computed_fields
        // This was buggy before the fix - computed field wasn't excluded
        // because exclusion happened after aliasing
        let json = user
            .sql_model_dump(DumpOptions::default().by_alias().exclude_computed_fields())
            .unwrap();
        assert_eq!(json["firstName"], "John"); // first_name aliased
        assert!(json.get("displayName").is_none()); // Computed field excluded (even though aliased)
        assert!(json.get("display_name").is_none()); // Original name doesn't exist either
    }

    // ========================================================================
    // Exclude Defaults Tests
    // ========================================================================

    /// Test model with default values for exclude_defaults testing.
    #[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
    struct TestModelWithDefaults {
        id: i64,
        name: String,
        count: i32,    // default: 0
        active: bool,  // default: false
        score: f64,    // default: 0.0
        label: String, // default: "default"
    }

    impl Model for TestModelWithDefaults {
        const TABLE_NAME: &'static str = "test_defaults";
        const PRIMARY_KEY: &'static [&'static str] = &["id"];

        fn fields() -> &'static [FieldInfo] {
            static FIELDS: &[FieldInfo] = &[
                FieldInfo::new("id", "id", SqlType::BigInt).primary_key(true),
                FieldInfo::new("name", "name", SqlType::Text),
                FieldInfo::new("count", "count", SqlType::Integer).default_json("0"),
                FieldInfo::new("active", "active", SqlType::Boolean).default_json("false"),
                FieldInfo::new("score", "score", SqlType::Double).default_json("0.0"),
                FieldInfo::new("label", "label", SqlType::Text).default_json("\"default\""),
            ];
            FIELDS
        }

        fn to_row(&self) -> Vec<(&'static str, Value)> {
            vec![
                ("id", Value::BigInt(self.id)),
                ("name", Value::Text(self.name.clone())),
                ("count", Value::Int(self.count)),
                ("active", Value::Bool(self.active)),
                ("score", Value::Double(self.score)),
                ("label", Value::Text(self.label.clone())),
            ]
        }

        fn from_row(row: &Row) -> crate::Result<Self> {
            Ok(Self {
                id: row.get_named("id")?,
                name: row.get_named("name")?,
                count: row.get_named("count")?,
                active: row.get_named("active")?,
                score: row.get_named("score")?,
                label: row.get_named("label")?,
            })
        }

        fn primary_key_value(&self) -> Vec<Value> {
            vec![Value::BigInt(self.id)]
        }

        fn is_new(&self) -> bool {
            false
        }
    }

    #[test]
    fn test_exclude_defaults_all_at_default() {
        let model = TestModelWithDefaults {
            id: 1,
            name: "Test".to_string(),
            count: 0,                     // at default
            active: false,                // at default
            score: 0.0,                   // at default
            label: "default".to_string(), // at default
        };

        let json = model
            .sql_model_dump(DumpOptions::default().exclude_defaults())
            .unwrap();

        // id and name have no default_json, so they're always included
        assert!(json.get("id").is_some());
        assert!(json.get("name").is_some());

        // Fields at default value should be excluded
        assert!(json.get("count").is_none());
        assert!(json.get("active").is_none());
        assert!(json.get("score").is_none());
        assert!(json.get("label").is_none());
    }

    #[test]
    fn test_exclude_defaults_none_at_default() {
        let model = TestModelWithDefaults {
            id: 1,
            name: "Test".to_string(),
            count: 42,                   // not at default
            active: true,                // not at default
            score: 3.5,                  // not at default
            label: "custom".to_string(), // not at default
        };

        let json = model
            .sql_model_dump(DumpOptions::default().exclude_defaults())
            .unwrap();

        // All fields should be present since none are at defaults
        assert!(json.get("id").is_some());
        assert!(json.get("name").is_some());
        assert!(json.get("count").is_some());
        assert!(json.get("active").is_some());
        assert!(json.get("score").is_some());
        assert!(json.get("label").is_some());

        // Verify values
        assert_eq!(json["count"], 42);
        assert_eq!(json["active"], true);
        assert_eq!(json["score"], 3.5);
        assert_eq!(json["label"], "custom");
    }

    #[test]
    fn test_exclude_defaults_mixed() {
        let model = TestModelWithDefaults {
            id: 1,
            name: "Test".to_string(),
            count: 0,                    // at default
            active: true,                // not at default
            score: 0.0,                  // at default
            label: "custom".to_string(), // not at default
        };

        let json = model
            .sql_model_dump(DumpOptions::default().exclude_defaults())
            .unwrap();

        assert!(json.get("id").is_some());
        assert!(json.get("name").is_some());

        // At default - excluded
        assert!(json.get("count").is_none());
        assert!(json.get("score").is_none());

        // Not at default - included
        assert!(json.get("active").is_some());
        assert!(json.get("label").is_some());
        assert_eq!(json["active"], true);
        assert_eq!(json["label"], "custom");
    }

    #[test]
    fn test_exclude_defaults_without_flag() {
        let model = TestModelWithDefaults {
            id: 1,
            name: "Test".to_string(),
            count: 0,                     // at default
            active: false,                // at default
            score: 0.0,                   // at default
            label: "default".to_string(), // at default
        };

        // Without exclude_defaults, all fields should be included
        let json = model.sql_model_dump(DumpOptions::default()).unwrap();

        assert!(json.get("id").is_some());
        assert!(json.get("name").is_some());
        assert!(json.get("count").is_some());
        assert!(json.get("active").is_some());
        assert!(json.get("score").is_some());
        assert!(json.get("label").is_some());
    }

    #[test]
    fn test_exclude_defaults_with_by_alias() {
        // Test that exclude_defaults works correctly with by_alias

        /// Model with defaults and aliases
        #[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
        struct TestAliasWithDefaults {
            id: i64,
            count: i32,
        }

        impl Model for TestAliasWithDefaults {
            const TABLE_NAME: &'static str = "test";
            const PRIMARY_KEY: &'static [&'static str] = &["id"];

            fn fields() -> &'static [FieldInfo] {
                static FIELDS: &[FieldInfo] = &[
                    FieldInfo::new("id", "id", SqlType::BigInt).primary_key(true),
                    FieldInfo::new("count", "count", SqlType::Integer)
                        .default_json("0")
                        .serialization_alias("itemCount"),
                ];
                FIELDS
            }

            fn to_row(&self) -> Vec<(&'static str, Value)> {
                vec![
                    ("id", Value::BigInt(self.id)),
                    ("count", Value::Int(self.count)),
                ]
            }

            fn from_row(row: &Row) -> crate::Result<Self> {
                Ok(Self {
                    id: row.get_named("id")?,
                    count: row.get_named("count")?,
                })
            }

            fn primary_key_value(&self) -> Vec<Value> {
                vec![Value::BigInt(self.id)]
            }

            fn is_new(&self) -> bool {
                false
            }
        }

        // At default value
        let model_at_default = TestAliasWithDefaults { id: 1, count: 0 };
        let json = model_at_default
            .sql_model_dump(DumpOptions::default().exclude_defaults().by_alias())
            .unwrap();

        // count is at default (0), so neither count nor itemCount should appear
        assert!(json.get("count").is_none());
        assert!(json.get("itemCount").is_none());

        // Not at default
        let model_not_at_default = TestAliasWithDefaults { id: 1, count: 5 };
        let json = model_not_at_default
            .sql_model_dump(DumpOptions::default().exclude_defaults().by_alias())
            .unwrap();

        // count is not at default, should appear with alias
        assert!(json.get("count").is_none()); // Original name not present
        assert_eq!(json["itemCount"], 5); // Alias is present
    }

    #[test]
    fn test_field_info_default_json() {
        // Test the FieldInfo builder methods for default_json
        let field1 = FieldInfo::new("count", "count", SqlType::Integer).default_json("0");
        assert_eq!(field1.default_json, Some("0"));
        assert!(field1.has_default);

        let field2 =
            FieldInfo::new("name", "name", SqlType::Text).default_json_opt(Some("\"hello\""));
        assert_eq!(field2.default_json, Some("\"hello\""));
        assert!(field2.has_default);

        let field3 = FieldInfo::new("name", "name", SqlType::Text).default_json_opt(None);
        assert_eq!(field3.default_json, None);
        assert!(!field3.has_default);

        let field4 = FieldInfo::new("flag", "flag", SqlType::Boolean).has_default(true);
        assert!(field4.has_default);
        assert_eq!(field4.default_json, None); // has_default alone doesn't set default_json
    }

    // ==================== SqlModelUpdate Tests ====================

    #[test]
    fn test_sqlmodel_update_from_dict() {
        #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
        struct TestUser {
            id: i64,
            name: String,
            age: i32,
        }

        impl Model for TestUser {
            const TABLE_NAME: &'static str = "users";
            const PRIMARY_KEY: &'static [&'static str] = &["id"];

            fn fields() -> &'static [FieldInfo] {
                static FIELDS: &[FieldInfo] = &[
                    FieldInfo::new("id", "id", SqlType::BigInt).primary_key(true),
                    FieldInfo::new("name", "name", SqlType::Text),
                    FieldInfo::new("age", "age", SqlType::Integer),
                ];
                FIELDS
            }

            fn to_row(&self) -> Vec<(&'static str, Value)> {
                vec![
                    ("id", Value::BigInt(self.id)),
                    ("name", Value::Text(self.name.clone())),
                    ("age", Value::Int(self.age)),
                ]
            }

            fn from_row(row: &Row) -> crate::Result<Self> {
                Ok(Self {
                    id: row.get_named("id")?,
                    name: row.get_named("name")?,
                    age: row.get_named("age")?,
                })
            }

            fn primary_key_value(&self) -> Vec<Value> {
                vec![Value::BigInt(self.id)]
            }

            fn is_new(&self) -> bool {
                false
            }
        }

        let mut user = TestUser {
            id: 1,
            name: "Alice".to_string(),
            age: 30,
        };

        // Update name only
        let update = HashMap::from([("name".to_string(), serde_json::json!("Bob"))]);
        user.sqlmodel_update(update, UpdateOptions::default())
            .unwrap();

        assert_eq!(user.name, "Bob");
        assert_eq!(user.age, 30); // Unchanged
    }

    #[test]
    fn test_sqlmodel_update_with_update_fields_filter() {
        #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
        struct TestUser {
            id: i64,
            name: String,
            age: i32,
        }

        impl Model for TestUser {
            const TABLE_NAME: &'static str = "users";
            const PRIMARY_KEY: &'static [&'static str] = &["id"];

            fn fields() -> &'static [FieldInfo] {
                static FIELDS: &[FieldInfo] = &[
                    FieldInfo::new("id", "id", SqlType::BigInt).primary_key(true),
                    FieldInfo::new("name", "name", SqlType::Text),
                    FieldInfo::new("age", "age", SqlType::Integer),
                ];
                FIELDS
            }

            fn to_row(&self) -> Vec<(&'static str, Value)> {
                vec![
                    ("id", Value::BigInt(self.id)),
                    ("name", Value::Text(self.name.clone())),
                    ("age", Value::Int(self.age)),
                ]
            }

            fn from_row(row: &Row) -> crate::Result<Self> {
                Ok(Self {
                    id: row.get_named("id")?,
                    name: row.get_named("name")?,
                    age: row.get_named("age")?,
                })
            }

            fn primary_key_value(&self) -> Vec<Value> {
                vec![Value::BigInt(self.id)]
            }

            fn is_new(&self) -> bool {
                false
            }
        }

        let mut user = TestUser {
            id: 1,
            name: "Alice".to_string(),
            age: 30,
        };

        // Try to update both name and age, but only allow name
        let update = HashMap::from([
            ("name".to_string(), serde_json::json!("Bob")),
            ("age".to_string(), serde_json::json!(25)),
        ]);
        user.sqlmodel_update(update, UpdateOptions::default().update_fields(["name"]))
            .unwrap();

        assert_eq!(user.name, "Bob"); // Updated
        assert_eq!(user.age, 30); // Not updated because not in update_fields
    }

    #[test]
    fn test_sqlmodel_update_invalid_field_error() {
        #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
        struct TestUser {
            id: i64,
            name: String,
        }

        impl Model for TestUser {
            const TABLE_NAME: &'static str = "users";
            const PRIMARY_KEY: &'static [&'static str] = &["id"];

            fn fields() -> &'static [FieldInfo] {
                static FIELDS: &[FieldInfo] = &[
                    FieldInfo::new("id", "id", SqlType::BigInt).primary_key(true),
                    FieldInfo::new("name", "name", SqlType::Text),
                ];
                FIELDS
            }

            fn to_row(&self) -> Vec<(&'static str, Value)> {
                vec![
                    ("id", Value::BigInt(self.id)),
                    ("name", Value::Text(self.name.clone())),
                ]
            }

            fn from_row(row: &Row) -> crate::Result<Self> {
                Ok(Self {
                    id: row.get_named("id")?,
                    name: row.get_named("name")?,
                })
            }

            fn primary_key_value(&self) -> Vec<Value> {
                vec![Value::BigInt(self.id)]
            }

            fn is_new(&self) -> bool {
                false
            }
        }

        let mut user = TestUser {
            id: 1,
            name: "Alice".to_string(),
        };

        // Try to update an invalid field
        let update = HashMap::from([("invalid_field".to_string(), serde_json::json!("value"))]);
        let result = user.sqlmodel_update(update, UpdateOptions::default());

        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.errors.iter().any(|e| e.field == "invalid_field"));
    }

    #[test]
    fn test_sqlmodel_update_from_model() {
        #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
        struct TestUser {
            id: i64,
            name: String,
            email: Option<String>,
        }

        impl Model for TestUser {
            const TABLE_NAME: &'static str = "users";
            const PRIMARY_KEY: &'static [&'static str] = &["id"];

            fn fields() -> &'static [FieldInfo] {
                static FIELDS: &[FieldInfo] = &[
                    FieldInfo::new("id", "id", SqlType::BigInt).primary_key(true),
                    FieldInfo::new("name", "name", SqlType::Text),
                    FieldInfo::new("email", "email", SqlType::Text).nullable(true),
                ];
                FIELDS
            }

            fn to_row(&self) -> Vec<(&'static str, Value)> {
                vec![
                    ("id", Value::BigInt(self.id)),
                    ("name", Value::Text(self.name.clone())),
                    ("email", self.email.clone().map_or(Value::Null, Value::Text)),
                ]
            }

            fn from_row(row: &Row) -> crate::Result<Self> {
                Ok(Self {
                    id: row.get_named("id")?,
                    name: row.get_named("name")?,
                    email: row.get_named("email").ok(),
                })
            }

            fn primary_key_value(&self) -> Vec<Value> {
                vec![Value::BigInt(self.id)]
            }

            fn is_new(&self) -> bool {
                false
            }
        }

        let mut user = TestUser {
            id: 1,
            name: "Alice".to_string(),
            email: Some("alice@example.com".to_string()),
        };

        // Patch with only name set (email is None)
        let patch = TestUser {
            id: 0, // Will be ignored since we're copying non-null values
            name: "Bob".to_string(),
            email: None, // Should not overwrite existing email
        };

        user.sqlmodel_update_from(&patch, UpdateOptions::default())
            .unwrap();

        assert_eq!(user.name, "Bob"); // Updated
        assert_eq!(user.email, Some("alice@example.com".to_string())); // Not updated (patch.email was None)
    }

    #[test]
    fn test_sqlmodel_update_dict_convenience() {
        #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
        struct TestItem {
            id: i64,
            count: i32,
        }

        impl Model for TestItem {
            const TABLE_NAME: &'static str = "items";
            const PRIMARY_KEY: &'static [&'static str] = &["id"];

            fn fields() -> &'static [FieldInfo] {
                static FIELDS: &[FieldInfo] = &[
                    FieldInfo::new("id", "id", SqlType::BigInt).primary_key(true),
                    FieldInfo::new("count", "count", SqlType::Integer),
                ];
                FIELDS
            }

            fn to_row(&self) -> Vec<(&'static str, Value)> {
                vec![
                    ("id", Value::BigInt(self.id)),
                    ("count", Value::Int(self.count)),
                ]
            }

            fn from_row(row: &Row) -> crate::Result<Self> {
                Ok(Self {
                    id: row.get_named("id")?,
                    count: row.get_named("count")?,
                })
            }

            fn primary_key_value(&self) -> Vec<Value> {
                vec![Value::BigInt(self.id)]
            }

            fn is_new(&self) -> bool {
                false
            }
        }

        let mut item = TestItem { id: 1, count: 10 };

        // Use the convenience method
        item.sqlmodel_update_dict(HashMap::from([(
            "count".to_string(),
            serde_json::json!(20),
        )]))
        .unwrap();

        assert_eq!(item.count, 20);
    }

    // ========================================================================
    // Credit Card Validation Tests (Luhn Algorithm)
    // ========================================================================

    #[test]
    fn test_credit_card_valid_visa() {
        // Valid Visa test number
        assert!(is_valid_credit_card("4539578763621486"));
    }

    #[test]
    fn test_credit_card_valid_mastercard() {
        // Valid Mastercard test number
        assert!(is_valid_credit_card("5425233430109903"));
    }

    #[test]
    fn test_credit_card_valid_amex() {
        // Valid American Express test number
        assert!(is_valid_credit_card("374245455400126"));
    }

    #[test]
    fn test_credit_card_with_spaces() {
        // Spaces should be stripped
        assert!(is_valid_credit_card("4539 5787 6362 1486"));
    }

    #[test]
    fn test_credit_card_with_dashes() {
        // Dashes should be stripped
        assert!(is_valid_credit_card("4539-5787-6362-1486"));
    }

    #[test]
    fn test_credit_card_invalid_luhn() {
        // Invalid Luhn checksum
        assert!(!is_valid_credit_card("1234567890123456"));
    }

    #[test]
    fn test_credit_card_too_short() {
        // Less than 13 digits
        assert!(!is_valid_credit_card("123456789012"));
    }

    #[test]
    fn test_credit_card_too_long() {
        // More than 19 digits
        assert!(!is_valid_credit_card("12345678901234567890"));
    }

    #[test]
    fn test_credit_card_empty() {
        assert!(!is_valid_credit_card(""));
    }

    #[test]
    fn test_credit_card_non_numeric() {
        // Contains letters
        assert!(!is_valid_credit_card("453957876362abcd"));
    }

    #[test]
    fn test_credit_card_all_zeros() {
        // All zeros - 16 digits, technically passes Luhn (sum=0, 0%10=0)
        // but not a realistic card number
        assert!(is_valid_credit_card("0000000000000000"));
    }

    #[test]
    fn test_credit_card_valid_discover() {
        // Valid Discover test number
        assert!(is_valid_credit_card("6011111111111117"));
    }

    // =========================================================================
    // Nested Model Tests
    // =========================================================================

    #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
    struct Address {
        street: String,
        city: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        zip: Option<String>,
    }

    #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
    struct Person {
        name: String,
        age: i32,
        address: Address,
        #[serde(skip_serializing_if = "Option::is_none")]
        spouse: Option<Box<Person>>,
    }

    #[test]
    fn test_nested_model_dump_basic() {
        let person = Person {
            name: "Alice".to_string(),
            age: 30,
            address: Address {
                street: "123 Main St".to_string(),
                city: "Springfield".to_string(),
                zip: Some("12345".to_string()),
            },
            spouse: None,
        };

        let json = person.model_dump(DumpOptions::default()).unwrap();
        assert_eq!(json["name"], "Alice");
        assert_eq!(json["age"], 30);
        assert_eq!(json["address"]["street"], "123 Main St");
        assert_eq!(json["address"]["city"], "Springfield");
        assert_eq!(json["address"]["zip"], "12345");
    }

    #[test]
    fn test_nested_model_dump_exclude_top_level() {
        let person = Person {
            name: "Alice".to_string(),
            age: 30,
            address: Address {
                street: "123 Main St".to_string(),
                city: "Springfield".to_string(),
                zip: Some("12345".to_string()),
            },
            spouse: None,
        };

        // Exclude only applies to top-level fields
        let json = person
            .model_dump(DumpOptions::default().exclude(["age"]))
            .unwrap();
        assert!(json.get("name").is_some());
        assert!(json.get("age").is_none());
        assert!(json.get("address").is_some()); // Still present
        // Nested fields are NOT affected by top-level exclude
        assert_eq!(json["address"]["city"], "Springfield");
    }

    #[test]
    fn test_nested_model_dump_exclude_nested_limitation() {
        // NOTE: This test documents a LIMITATION.
        // In Pydantic, you can exclude nested fields with dot notation: exclude={"address.zip"}
        // Our current implementation only supports top-level field exclusion.
        let person = Person {
            name: "Alice".to_string(),
            age: 30,
            address: Address {
                street: "123 Main St".to_string(),
                city: "Springfield".to_string(),
                zip: Some("12345".to_string()),
            },
            spouse: None,
        };

        // Trying to exclude "address.zip" won't work - it treats it as a top-level field name
        let json = person
            .model_dump(DumpOptions::default().exclude(["address.zip"]))
            .unwrap();
        // address.zip is still present because we don't support nested path exclusion
        assert_eq!(json["address"]["zip"], "12345");
    }

    #[test]
    fn test_deeply_nested_model_dump() {
        let person = Person {
            name: "Alice".to_string(),
            age: 30,
            address: Address {
                street: "123 Main St".to_string(),
                city: "Springfield".to_string(),
                zip: None,
            },
            spouse: Some(Box::new(Person {
                name: "Bob".to_string(),
                age: 32,
                address: Address {
                    street: "456 Oak Ave".to_string(),
                    city: "Springfield".to_string(),
                    zip: Some("12346".to_string()),
                },
                spouse: None,
            })),
        };

        let json = person.model_dump(DumpOptions::default()).unwrap();
        assert_eq!(json["name"], "Alice");
        assert_eq!(json["spouse"]["name"], "Bob");
        assert_eq!(json["spouse"]["address"]["street"], "456 Oak Ave");
    }

    #[test]
    fn test_nested_model_exclude_none() {
        let person = Person {
            name: "Alice".to_string(),
            age: 30,
            address: Address {
                street: "123 Main St".to_string(),
                city: "Springfield".to_string(),
                zip: None, // Will be skipped by serde skip_serializing_if
            },
            spouse: None, // Will be skipped by serde skip_serializing_if
        };

        let json = person
            .model_dump(DumpOptions::default().exclude_none())
            .unwrap();
        assert!(json.get("name").is_some());
        // spouse is None and serde skips it, so it's not in the output
        assert!(json.get("spouse").is_none());
        // Note: exclude_none only affects top-level nulls in model_dump
        // Nested nulls are handled by serde's skip_serializing_if
    }
}