puerto 0.29.0

Puerto: la CLI de un framework full-stack DDD en Rust. Genera proyectos con Clean Architecture, la inyección de dependencias cableada sola y soporte de SQLx, sin cablear nada a mano. Antes, puerto-framework.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
use crate::puerto_toml::{Field, ValueObjectDefinition};

#[derive(Debug)]
pub struct TypeMapping {
    pub rust_type: &'static str,
    pub sql_type: &'static str,
    pub sql_nullable: bool,
    // The OpenAPI schema is derived by poem from the Rust type; these two record what it derives,
    // so the registry stays the one table describing a type across Rust, SQL and the API docs.
    #[allow(dead_code)]
    pub openapi_type: &'static str,
    #[allow(dead_code)]
    pub openapi_format: Option<&'static str>,
    pub default_expr: &'static str,
    pub needs_import: Option<&'static str>,
}

const TYPE_REGISTRY: &[TypeMapping] = &[
    TypeMapping {
        rust_type: "String",
        sql_type: "TEXT",
        sql_nullable: false,
        openapi_type: "string",
        openapi_format: None,
        default_expr: r#""example".to_string()"#,
        needs_import: None,
    },
    TypeMapping {
        rust_type: "i64",
        sql_type: "BIGINT",
        sql_nullable: false,
        openapi_type: "integer",
        openapi_format: Some("int64"),
        default_expr: "42",
        needs_import: None,
    },
    TypeMapping {
        rust_type: "bool",
        sql_type: "BOOLEAN",
        sql_nullable: false,
        openapi_type: "boolean",
        openapi_format: None,
        default_expr: "true",
        needs_import: None,
    },
    TypeMapping {
        rust_type: "f64",
        sql_type: "DOUBLE",
        sql_nullable: false,
        openapi_type: "number",
        openapi_format: Some("double"),
        default_expr: "1.5",
        needs_import: None,
    },
    TypeMapping {
        rust_type: "Option<String>",
        sql_type: "TEXT",
        sql_nullable: true,
        openapi_type: "string",
        openapi_format: None,
        default_expr: "None",
        needs_import: None,
    },
    TypeMapping {
        rust_type: "Option<i64>",
        sql_type: "BIGINT",
        sql_nullable: true,
        openapi_type: "integer",
        openapi_format: Some("int64"),
        default_expr: "None",
        needs_import: None,
    },
    TypeMapping {
        rust_type: "Option<bool>",
        sql_type: "BOOLEAN",
        sql_nullable: true,
        openapi_type: "boolean",
        openapi_format: None,
        default_expr: "None",
        needs_import: None,
    },
    TypeMapping {
        rust_type: "Option<f64>",
        sql_type: "DOUBLE",
        sql_nullable: true,
        openapi_type: "number",
        openapi_format: Some("double"),
        default_expr: "None",
        needs_import: None,
    },
    TypeMapping {
        rust_type: "Uuid",
        sql_type: "UUID",
        sql_nullable: false,
        openapi_type: "string",
        openapi_format: Some("uuid"),
        default_expr: "Uuid::new_v4()",
        needs_import: Some("uuid::Uuid"),
    },
    TypeMapping {
        rust_type: "Option<Uuid>",
        sql_type: "UUID",
        sql_nullable: true,
        openapi_type: "string",
        openapi_format: Some("uuid"),
        default_expr: "None",
        needs_import: Some("uuid::Uuid"),
    },
    TypeMapping {
        rust_type: "DateTime<Utc>",
        sql_type: "TIMESTAMPTZ",
        sql_nullable: false,
        openapi_type: "string",
        openapi_format: Some("date-time"),
        default_expr: "Utc::now()",
        needs_import: Some("chrono::{DateTime, Utc}"),
    },
    TypeMapping {
        rust_type: "Option<DateTime<Utc>>",
        sql_type: "TIMESTAMPTZ",
        sql_nullable: true,
        openapi_type: "string",
        openapi_format: Some("date-time"),
        default_expr: "None",
        needs_import: Some("chrono::{DateTime, Utc}"),
    },
    TypeMapping {
        rust_type: "Vec<String>",
        sql_type: "TEXT[]",
        sql_nullable: false,
        openapi_type: "array",
        openapi_format: None,
        default_expr: "vec![]",
        needs_import: None,
    },
    TypeMapping {
        rust_type: "Vec<i64>",
        sql_type: "BIGINT[]",
        sql_nullable: false,
        openapi_type: "array",
        openapi_format: None,
        default_expr: "vec![]",
        needs_import: None,
    },
    TypeMapping {
        rust_type: "HashMap<String, String>",
        sql_type: "JSONB",
        sql_nullable: false,
        openapi_type: "object",
        openapi_format: None,
        default_expr: "HashMap::new()",
        needs_import: Some("std::collections::HashMap"),
    },
];

/// A `vec:ref`'s type (Fase 32): only with `referencia`, so not in [`TYPE_REGISTRY`]'s list. Its
/// SQL type is never a column: the ids live in a join table.
const VEC_REFERENCE: TypeMapping = TypeMapping {
    rust_type: "Vec<Uuid>",
    sql_type: "UUID[]",
    sql_nullable: false,
    openapi_type: "array",
    openapi_format: None,
    default_expr: "vec![]",
    needs_import: None,
};

pub fn resolve_type(field_type: &str) -> Result<&'static TypeMapping, String> {
    if field_type == VEC_REFERENCE.rust_type {
        return Ok(&VEC_REFERENCE);
    }
    TYPE_REGISTRY
        .iter()
        .find(|m| m.rust_type == field_type)
        .ok_or_else(|| {
            let valid: Vec<&str> = TYPE_REGISTRY.iter().map(|m| m.rust_type).collect();
            format!(
                "tipo de campo no admitido '{}'. Los admitidos: {}",
                field_type,
                valid.join(", ")
            )
        })
}

pub fn validate_fields(fields: &[Field]) -> Result<(), String> {
    // A composite value object (Fase 31) has no registry type: its parts do.
    for field in fields.iter().filter(|f| !is_composite_vo(f)) {
        resolve_type(&field.field_type)?;
        plain_vec_of_ids(field)?;
    }
    Ok(())
}

/// A `Vec<Uuid>` is a `vec:ref` (Fase 32), never a plain field.
pub fn plain_vec_of_ids(field: &Field) -> Result<(), String> {
    if field.field_type == VEC_REFERENCE.rust_type && field.references.is_none() {
        return Err(format!(
            "el campo '{}' es un Vec<Uuid> sin referencia: una lista de ids es un \
             vec:ref:<Entidad>, que los guarda en una tabla intermedia",
            field.name
        ));
    }
    Ok(())
}

/// A field of a composite value object (Fase 31): its parts, one column each.
pub fn is_composite_vo(field: &Field) -> bool {
    !field.parts.is_empty()
}

/// The columns a field is stored in: itself, or one per part of a composite value object (Fase 31),
/// `<campo>_<parte>` of the part's type. What the rows, the migration and the Params hold.
pub fn columns(field: &Field) -> Vec<Field> {
    if !is_composite_vo(field) {
        return vec![field.clone()];
    }
    field
        .parts
        .iter()
        .map(|part| Field {
            name: format!("{}_{}", field.name, part.name),
            field_type: part.field_type.clone(),
            label: part.label.clone(),
            ..Default::default()
        })
        .collect()
}

pub fn is_value_object(field: &Field) -> bool {
    field.value_object.is_some()
}

pub fn is_option_vo(field: &Field) -> bool {
    field.value_object.is_some() && field.field_type.starts_with("Option<")
}

pub fn is_vec_vo(field: &Field) -> bool {
    field.value_object.is_some() && field.field_type.starts_with("Vec<")
}

pub fn is_enum_vo(field: &Field) -> bool {
    field.value_object_kind.as_deref() == Some("enum")
}

/// A value object's class with a rule (27.16): `email` or `dinero`; `None` for any other.
pub fn vo_class(field: &Field) -> Option<&'static str> {
    let kind = field.value_object_kind.as_deref()?;
    crate::puerto_toml::VO_CLASSES
        .iter()
        .find(|(class, _)| *class == kind)
        .map(|(class, _)| *class)
}

pub fn is_shared_vo(field: &Field, shared_vos: &[ValueObjectDefinition]) -> bool {
    field
        .value_object
        .as_deref()
        .is_some_and(|vo| shared_vos.iter().any(|d| d.name == vo))
}

/// A reference to another entity (ADR-0022): its id is what the field holds.
pub fn is_reference(field: &Field) -> bool {
    field.references.is_some()
}

/// A `vec:ref` (Fase 32): the ids of a set of rows of its target, in a join table, not a column.
pub fn is_vec_reference(field: &Field) -> bool {
    is_reference(field) && field.field_type.starts_with("Vec<")
}

/// The join table of a `vec:ref` (Fase 32) and the names the repository maps back to its field:
/// `pedidos_etiquetas (pedido_id, etiqueta_id)`; a self reference's are `origen_id`, `destino_id`.
#[derive(Debug, Clone, serde::Serialize)]
pub struct JoinTable {
    pub table: String,
    pub owner_column: String,
    pub target_column: String,
    pub target_table: String,
    pub owner_constraint: String,
    pub target_constraint: String,
    pub index: String,
}

pub fn join_table(entity_snake: &str, field: &Field) -> JoinTable {
    use crate::generators::naming::{pascal_to_snake, pluralize};
    let target = pascal_to_snake(field.references.as_deref().unwrap_or_default());
    let table = format!("{}_{}", pluralize(entity_snake), field.name);
    let (owner_column, target_column) = if target == entity_snake {
        ("origen_id".to_string(), "destino_id".to_string())
    } else {
        (format!("{entity_snake}_id"), format!("{target}_id"))
    };
    JoinTable {
        owner_constraint: format!("{table}_{owner_column}_fkey"),
        target_constraint: format!("{table}_{target_column}_fkey"),
        index: format!("{table}_{target_column}_idx"),
        target_table: pluralize(&target),
        table,
        owner_column,
        target_column,
    }
}

/// The id type a reference holds — `CustomerId` for `references = "Customer"`.
pub fn reference_id(field: &Field) -> Option<String> {
    field
        .references
        .as_deref()
        .map(|target| format!("Id{target}"))
}

/// The path of a reference's id type, which lives in its owner's domain crate (ADR-0023 § 3):
/// `dominio_customer::modelo::CustomerId`.
pub fn reference_import(field: &Field) -> Option<String> {
    let target = field.references.as_deref()?;
    let snake = crate::generators::naming::pascal_to_snake(target);
    Some(format!("dominio_{snake}::modelo::Id{target}"))
}

/// What naming the field's **domain** type needs from the registry: nothing for a reference,
/// whose type is the target's id (see [`reference_import`]).
pub fn type_import(field: &Field) -> Option<&'static str> {
    if is_reference(field) {
        return None;
    }
    resolve_type(&field.field_type).ok()?.needs_import
}

/// The import a **value literal** of this field needs — as opposed to `needs_import`, which is what
/// naming the field's *type* needs.
///
/// Test Props/Params literals and Object Mother defaults write `Utc::now()`, `Uuid::new_v4()` or
/// `HashMap::new()`, never the type itself, so a `DateTime<Utc>` field needs `chrono::Utc` only —
/// importing `chrono::{DateTime, Utc}` there was an `unused_imports` warning (an error under the
/// generated project's `make lint`). `Option<…>` / `Vec<…>` literals are `None` / `vec![]`, and
/// enum VOs a variant path: none needs a primitive import.
pub fn literal_import(field: &Field) -> Option<&'static str> {
    // A reference's literal is its id type's (`CustomerId::generate()`): see [`reference_import`].
    if is_enum_vo(field) || is_reference(field) {
        return None;
    }
    match field.field_type.as_str() {
        "DateTime<Utc>" => Some("chrono::Utc"),
        "Uuid" => Some("uuid::Uuid"),
        "HashMap<String, String>" => Some("std::collections::HashMap"),
        _ => None,
    }
}

/// Whether a literal of this field names its value object (`Name::new(…)`, `Status::Active`).
/// `Option<VO>` / `Vec<VO>` literals are `None` / `vec![]` and do not.
pub fn literal_names_vo(field: &Field) -> bool {
    is_value_object(field) && !is_option_vo(field) && !is_vec_vo(field)
}

/// A value object's path: a shared one is `nucleo`'s; a local one, its domain crate's — `crate`
/// from inside it (`in_domain`).
pub fn vo_import_path(
    field: &Field,
    snake: &str,
    shared_vos: &[ValueObjectDefinition],
    in_domain: bool,
) -> String {
    let vo = field.value_object.as_deref().unwrap_or_default();
    if is_shared_vo(field, shared_vos) {
        format!("nucleo::valores::{vo}")
    } else if in_domain {
        format!("crate::valores::{vo}")
    } else {
        format!("dominio_{snake}::valores::{vo}")
    }
}

pub fn vo_inner_type(field: &Field) -> String {
    if field.field_type.starts_with("Option<") {
        field.field_type[7..field.field_type.len() - 1].to_string()
    } else if field.field_type.starts_with("Vec<") {
        field.field_type[4..field.field_type.len() - 1].to_string()
    } else {
        field.field_type.clone()
    }
}

pub fn field_rust_type(field: &Field) -> String {
    if let Some(id) = reference_id(field) {
        return if field.field_type.starts_with("Option<") {
            format!("Option<{id}>")
        } else if field.field_type.starts_with("Vec<") {
            format!("Vec<{id}>")
        } else {
            id
        };
    }
    match &field.value_object {
        Some(vo) => {
            if field.field_type.starts_with("Option<") {
                format!("Option<{}>", vo)
            } else if field.field_type.starts_with("Vec<") {
                format!("Vec<{}>", vo)
            } else {
                vo.clone()
            }
        }
        None => field.field_type.clone(),
    }
}

/// What a template needs to know about one field. The `.tera` files iterate over these and own the
/// shape of the generated code; the decisions (which VO shape, which literal) are made here once.
#[derive(Debug, serde::Serialize)]
pub struct FieldCtx {
    pub name: String,
    /// The name on the wire (`customerId`): DTOs say it with `rename_all`, a query parameter by name.
    pub camel: String,
    /// `unique = true`: a partial unique index in `--db` projects.
    pub unique: bool,
    /// `privado = true` (auth projects): `None` in the DTO a public route gives a caller without a
    /// session.
    pub private: bool,
    /// The type in domain structs: the VO name, `Option<Vo>`, `Vec<Vo>`, or the primitive.
    pub rust_type: String,
    pub is_vo: bool,
    pub is_option_vo: bool,
    pub is_vec_vo: bool,
    pub is_enum_vo: bool,
    pub is_shared_vo: bool,
    /// The VO name (`""` for a primitive field) and its snake_case form.
    pub vo: String,
    pub vo_snake: String,
    /// A primitive `String`: `new()` rejects it empty.
    pub required_string: bool,
    /// A plain `String` VO (not enum, `Option` or `Vec`): `Vo::new("")` must fail.
    pub string_vo: bool,
    /// A valid default value, see [`props_literal`].
    pub props_literal: String,
    /// The value the Pg integration tests seed, see [`seed_literal`].
    pub seed_literal: String,
    /// The primitive (registry) type, as persistence rows and params hold it.
    pub field_type: String,
    /// A `HashMap<String, String>` field: a JSONB column wrapped in `sqlx::types::Json`.
    pub is_map: bool,
    /// A `Vec<…>` column (primitive or VO): sqlx binds it as a slice.
    pub is_array: bool,
    /// The primitive read out of a borrowed entity, see [`field_value_accessor`].
    pub accessor: String,
    /// Reading the primitive out of a borrowed request body needs `.clone()`.
    pub needs_clone: bool,
    /// The domain value built from a persistence row (`row.{name}`), see [`field_vo_conversion`].
    pub from_row: String,
    /// The domain value built from a use case param (`params.{name}`).
    pub from_params: String,
    /// The Params value read out of the mother's props (`props.{name}`) in the use case tests: the
    /// primitive, but a reference's id type.
    pub from_props: String,
    /// The registry default for the primitive type — what request bodies in route tests hold.
    pub default_literal: String,
    /// What Params literals in tests hold: [`Self::default_literal`], but a reference's id type.
    pub params_literal: String,
    /// The type in use case Params: the primitive, but a reference's id type.
    pub params_type: String,
    /// The Params value built from a borrowed request body (`body.{name}`) in the routes.
    pub from_body: String,
    /// A reference (ADR-0022): the target entity (`""` otherwise) and its snake_case form.
    pub is_ref: bool,
    /// A `vec:ref` (Fase 32): a set of ids in a join table, not a column.
    pub is_vec_ref: bool,
    pub ref_target: String,
    pub ref_target_snake: String,
    /// An enum VO's first variant (`""` otherwise): the valid value tests pass for it.
    pub first_variant: String,
    /// Object Mother: what `{Entity}Mother` stores in its `Option<…>` slot, the parameter type of
    /// `with_{field}` (`&str` for strings, converted with `.to_string()`), and the value `build()`
    /// puts in the Props — the slot, or a valid default.
    pub mother_storage: String,
    pub mother_param: String,
    pub mother_build: String,
    /// A field the list filters by (`Filtro<Entity>`): a reference (18.4) or the owner (27.18), with
    /// its type in the filter and the value Postgres binds.
    pub filters: bool,
    pub filter_type: String,
    pub filter_bind: String,
    /// What `crear` asks for (27.17): neither a `por_defecto` nor a `generado` field.
    pub in_create: bool,
    /// What `actualizar` asks for: not a `generado` field, which keeps its value.
    pub in_update: bool,
    pub generated: bool,
    /// The domain value `crear` gives a field it does not ask for: its default
    /// (`EstadoEntrada::Pendiente`) or `<Entity>::generar_<field>()`, in its value object.
    pub server_value: String,
    /// A composite value object (Fase 31): its columns, one per part — what the rows, the Params
    /// and the SQL hold (empty for any other field, which is its own column) — and the DTO type,
    /// `Dto<Vo><Entity>`, that the API nests it in (the primitive for any other field).
    pub is_composite: bool,
    pub columns: Vec<FieldCtx>,
    pub dto_type: String,
    /// A column of a composite value object: the part's own name (`calle`); `""` otherwise.
    pub part: String,
}

/// One column of a composite value object (Fase 31): a primitive field, read out of the entity's
/// value object by its getter and out of a request body's nested DTO.
fn composite_column(field: &Field, part: &Field, column: &Field, pascal: &str) -> FieldCtx {
    let getter = |owner: &str| {
        let read = format!("{owner}.{}.{}()", field.name, part.name);
        match part.field_type.as_str() {
            "String" => format!("{read}.to_string()"),
            "Option<String>" => format!("{read}.map(str::to_string)"),
            _ => read,
        }
    };
    let body = format!("cuerpo.{}.{}", field.name, part.name);
    FieldCtx {
        accessor: getter("entidad"),
        from_props: getter("props"),
        from_body: if field_needs_clone(&part.field_type) {
            format!("{body}.clone()")
        } else {
            body
        },
        part: part.name.clone(),
        ..field_ctx(column, pascal, &[])
    }
}

/// [`FieldCtx::server_value`] (27.17); `""` for a field `crear` asks for.
fn server_value(field: &Field, pascal: &str, shared_vos: &[ValueObjectDefinition]) -> String {
    // 27.18: the owner is who creates it.
    if field.owner {
        return "params.actor.id_usuario".to_string();
    }
    let primitive = if field.generated {
        format!("{pascal}::generar_{}()", field.name)
    } else if let Some(value) = &field.default {
        if is_enum_vo(field) {
            return format!(
                "{}::{value}",
                field.value_object.as_deref().unwrap_or_default()
            );
        }
        match field.field_type.as_str() {
            "String" => format!("{value:?}.to_string()"),
            "f64" => value
                .parse::<f64>()
                .map_or_else(|_| value.clone(), |v| format!("{v:?}")),
            _ => value.clone(),
        }
    } else {
        return String::new();
    };
    let Some(vo) = field.value_object.as_deref() else {
        return primitive;
    };
    if is_shared_vo(field, shared_vos) {
        format!("{vo}::new({primitive}).map_err(|_| Error{pascal}::{vo}Invalido)?")
    } else {
        format!("{vo}::new({primitive})?")
    }
}

pub fn field_ctx(field: &Field, pascal: &str, shared_vos: &[ValueObjectDefinition]) -> FieldCtx {
    let vo = field.value_object.clone().unwrap_or_default();
    let is_map = field.field_type == "HashMap<String, String>";
    let default_literal = if is_composite_vo(field) {
        // Fase 31: the API's nested DTO, each part with the registry's value.
        let parts: Vec<String> = field
            .parts
            .iter()
            .map(|p| format!("{}: {}", p.name, field_ctx(p, pascal, &[]).default_literal))
            .collect();
        format!("Dto{vo}{pascal} {{ {} }}", parts.join(", "))
    } else {
        example_literal(field, &field.field_type).unwrap_or_else(|| {
            resolve_type(&field.field_type)
                .map_or("", |m| m.default_expr)
                .to_string()
        })
    };
    let ref_target = field.references.clone().unwrap_or_default();
    FieldCtx {
        camel: crate::generators::naming::snake_to_camel(&field.name),
        params_literal: if is_reference(field) {
            props_literal(field)
        } else {
            default_literal.clone()
        },
        params_type: if is_reference(field) {
            field_rust_type(field)
        } else {
            field.field_type.clone()
        },
        from_body: match reference_id(field) {
            Some(id) if field.field_type.starts_with("Option<") => {
                format!("cuerpo.{}.map({id}::from_uuid)", field.name)
            }
            Some(id) if is_vec_reference(field) => format!(
                "cuerpo.{}.iter().copied().map({id}::from_uuid).collect()",
                field.name
            ),
            Some(id) => format!("{id}::from_uuid(cuerpo.{})", field.name),
            None if field_needs_clone(&field.field_type) => {
                format!("cuerpo.{}.clone()", field.name)
            }
            None => format!("cuerpo.{}", field.name),
        },
        is_ref: is_reference(field),
        is_vec_ref: is_vec_reference(field),
        ref_target_snake: crate::generators::naming::pascal_to_snake(&ref_target),
        ref_target,
        unique: field.unique,
        private: field.private,
        seed_literal: seed_literal(field),
        field_type: field.field_type.clone(),
        is_map,
        is_array: field.field_type.starts_with("Vec<"),
        accessor: field_value_accessor(field, "entidad"),
        needs_clone: field_needs_clone(&field.field_type),
        from_params: field_vo_conversion(field, "params.", pascal, shared_vos),
        from_props: if is_reference(field) || !is_value_object(field) {
            format!("props.{}", field.name)
        } else {
            field_value_accessor(field, "props")
        },
        default_literal,
        first_variant: field
            .enum_variants
            .as_deref()
            .and_then(<[String]>::first)
            .cloned()
            .unwrap_or_default(),
        from_row: if is_map {
            format!("fila.{}.0", field.name)
        } else if let Some(id) = reference_id(field) {
            if field.field_type.starts_with("Option<") {
                format!("fila.{}.map({id}::from_uuid)", field.name)
            } else if is_vec_reference(field) {
                format!(
                    "fila.{}.into_iter().map({id}::from_uuid).collect()",
                    field.name
                )
            } else {
                format!("{id}::from_uuid(fila.{})", field.name)
            }
        } else {
            field_vo_conversion(field, "fila.", pascal, shared_vos)
        },
        name: field.name.clone(),
        rust_type: field_rust_type(field),
        is_vo: is_value_object(field),
        is_option_vo: is_option_vo(field),
        is_vec_vo: is_vec_vo(field),
        is_enum_vo: is_enum_vo(field),
        is_shared_vo: is_shared_vo(field, shared_vos),
        vo_snake: crate::generators::naming::pascal_to_snake(&vo),
        vo: vo.clone(),
        required_string: field.field_type == "String" && !is_value_object(field),
        string_vo: is_value_object(field) && field.field_type == "String" && !is_enum_vo(field),
        props_literal: props_literal(field),
        mother_storage: mother_storage(field),
        mother_param: match mother_storage(field).as_str() {
            "String" => "&str".to_string(),
            storage => storage.to_string(),
        },
        mother_build: mother_build(field),
        filters: is_reference(field) || field.owner,
        filter_type: match reference_id(field) {
            Some(id) => format!("Option<{id}>"),
            None => "Option<uuid::Uuid>".to_string(),
        },
        filter_bind: if field.owner {
            format!("filtro.{}", field.name)
        } else {
            format!("filtro.{}.map(|id| id.valor())", field.name)
        },
        in_create: !field.generated && field.default.is_none() && !field.owner,
        in_update: !field.generated && !field.owner,
        generated: field.generated,
        server_value: server_value(field, pascal, shared_vos),
        is_composite: is_composite_vo(field),
        columns: field
            .parts
            .iter()
            .zip(columns(field))
            .map(|(part, column)| composite_column(field, part, &column, pascal))
            .collect(),
        dto_type: if is_composite_vo(field) {
            format!("Dto{vo}{pascal}")
        } else {
            field.field_type.clone()
        },
        part: String::new(),
    }
}

/// Inserts `fields` and what crear and actualizar ask for of them (27.17).
pub fn insert_field_lists(ctx: &mut tera::Context, fields: &[FieldCtx]) {
    let create: Vec<&FieldCtx> = fields.iter().filter(|f| f.in_create).collect();
    let update: Vec<&FieldCtx> = fields.iter().filter(|f| f.in_update).collect();
    ctx.insert("fields", fields);
    ctx.insert("create_fields", &create);
    ctx.insert("update_fields", &update);
}

/// The Object Mother stores every field as `Option<…>`, so an `Option` primitive is stored as its
/// inner type and an `Option<Vo>` as the VO.
fn mother_storage(field: &Field) -> String {
    if is_vec_reference(field) {
        field_rust_type(field)
    } else if let Some(id) = reference_id(field) {
        id
    } else if is_value_object(field) && !is_vec_vo(field) {
        field.value_object.clone().unwrap_or_default()
    } else if !is_value_object(field) && field.field_type.starts_with("Option<") {
        vo_inner_type(field)
    } else {
        field_rust_type(field)
    }
}

fn mother_build(field: &Field) -> String {
    let slot = format!("self.{}", field.name);
    // 27.18: the mother's rows are the nil user's — the `propietario()` of the use cases' tests.
    if field.owner {
        return format!("{slot}.unwrap_or_default()");
    }
    if field.field_type.starts_with("Option<") {
        return slot;
    }
    if is_vec_vo(field) || is_vec_reference(field) {
        return format!("{slot}.unwrap_or_default()");
    }
    if let Some(id) = reference_id(field) {
        return format!("{slot}.unwrap_or_else({id}::generar)");
    }
    if is_value_object(field) {
        return if is_enum_vo(field) {
            format!("{slot}.unwrap_or({})", props_literal(field))
        } else {
            format!("{slot}.unwrap_or_else(|| {})", props_literal(field))
        };
    }
    match field.field_type.as_str() {
        "String" => format!("{slot}.unwrap_or_else(|| {})", props_literal(field)),
        "i64" | "bool" | "f64" => format!("{slot}.unwrap_or({})", props_literal(field)),
        "Uuid" => format!("{slot}.unwrap_or_else(Uuid::new_v4)"),
        "DateTime<Utc>" => format!("{slot}.unwrap_or_else(Utc::now)"),
        _ => format!("{slot}.unwrap_or_default()"),
    }
}

/// A valid value for the field in a Props literal: `None` / `vec![]` for `Option` / `Vec` VOs, the
/// first variant of an enum VO, `Vo::new(default)` for a plain VO, the registry default otherwise.
///
/// **The one place that decides a Props field literal.** The four VO shapes must be handled in the
/// same order everywhere: when the validation-test builders carried their own copy of this
/// branching they omitted `Option`/`Vec`, emitting `Tag::new(vec![])` and `Mid::new(None)` —
/// generated tests that did not compile.
pub fn props_literal(field: &Field) -> String {
    literal_with(field, |ty| {
        example_literal(field, ty)
            .unwrap_or_else(|| resolve_type(ty).map_or("", |m| m.default_expr).to_string())
    })
}

/// The `ejemplo` of the field's shared value object (27.15) as a Rust literal of `ty`, its
/// primitive; `None` without one. An `f64` gets its decimal point (`12` → `12.0`).
fn example_literal(field: &Field, ty: &str) -> Option<String> {
    let example = example_of(field)?;
    match ty {
        "String" => Some(format!("{example:?}.to_string()")),
        "i64" | "bool" => Some(example.to_string()),
        "f64" => example
            .parse::<f64>()
            .ok()
            .map(|value| format!("{value:?}")),
        _ => None,
    }
}

/// The example an `email` value object uses without one of its own (27.16).
const EMAIL_EXAMPLE: &str = "ana@ejemplo.es";

/// The field's example: its shared value object's (27.15), or its class's.
fn example_of(field: &Field) -> Option<&str> {
    field
        .example
        .as_deref()
        .or_else(|| (vo_class(field) == Some("email")).then_some(EMAIL_EXAMPLE))
}

/// [`example_literal`] in TypeScript, for the web's mothers.
fn example_ts(field: &Field) -> Option<String> {
    let example = example_of(field)?;
    match vo_inner_type(field).as_str() {
        "String" => Some(ts_string(example)),
        "i64" | "f64" | "bool" => Some(example.to_string()),
        _ => None,
    }
}

/// Like [`props_literal`], with the values the Pg integration tests seed.
///
/// Differs from the registry's defaults in two ways. Timestamps are a fixed whole second:
/// `Utc::now()` has nanosecond precision and Postgres stores microseconds, so a seeded value never
/// compared equal after a round-trip. And `unique` fields get a fresh value per call: the list test
/// seeds twice, and a constant hit the unique index (`Duplicate`).
pub fn seed_literal(field: &Field) -> String {
    const FIXED_TIME: &str = "chrono::DateTime::from_timestamp(1_700_000_000, 0).unwrap()";
    literal_with(field, |ty| {
        // A unique field seeds a fresh value per call: its example is one value.
        if let Some(example) = example_literal(field, ty).filter(|_| !field.unique) {
            return example;
        }
        match (ty, field.unique) {
        ("String", true) => "Uuid::new_v4().to_string()".to_string(),
        ("i64", true) => "(Uuid::new_v4().as_u128() >> 65) as i64".to_string(),
        ("f64", true) => "(Uuid::new_v4().as_u128() >> 76) as f64".to_string(),
        ("DateTime<Utc>", true) => {
            "chrono::DateTime::from_timestamp(1_700_000_000 + (Uuid::new_v4().as_u128() >> 100) as i64, 0).unwrap()".to_string()
        }
        ("DateTime<Utc>", false) => FIXED_TIME.to_string(),
        ("HashMap<String, String>", _) => "std::collections::HashMap::new()".to_string(),
        (other, _) => resolve_type(other).map_or("", |m| m.default_expr).to_string(),
    }
    })
}

/// What the mother's `<campo>_unico()` returns for a `unico` field (Fase 27.2): the primitive
/// type and a fresh value per call. `None` where a field has no such function: not unique, a
/// reference or an enum (their seeds come from elsewhere), or a type whose default is fresh.
pub fn unique_value(field: &Field) -> Option<(&'static str, &'static str)> {
    if !field.unique || is_reference(field) || is_enum_vo(field) {
        return None;
    }
    match field.field_type.as_str() {
        // An email's (27.16) still has the form its rule asks for.
        "String" if vo_class(field) == Some("email") => Some((
            "String",
            "format!(\"{}@ejemplo.es\", &uuid::Uuid::new_v4().simple().to_string()[..12])",
        )),
        // 12 hex characters: short enough for a length rule, enough to never repeat in a test.
        "String" => Some((
            "String",
            "uuid::Uuid::new_v4().simple().to_string()[..12].to_string()",
        )),
        "i64" => Some(("i64", "(uuid::Uuid::new_v4().as_u128() >> 65) as i64")),
        "f64" => Some(("f64", "(uuid::Uuid::new_v4().as_u128() >> 76) as f64")),
        "DateTime<Utc>" => Some((
            "chrono::DateTime<chrono::Utc>",
            "chrono::DateTime::from_timestamp(1_700_000_000 + (uuid::Uuid::new_v4().as_u128() >> 100) as i64, 0)\n            .expect(\"una fecha dentro del rango de chrono\")",
        )),
        _ => None,
    }
}

/// The seed of a field with a [`unique_value`]: the mother's, in the field's value-object shape,
/// so a rule of the value object is met in one place the user owns.
pub fn unique_seed(field: &Field, pascal: &str) -> Option<String> {
    unique_value(field)?;
    Some(literal_with(field, |_| {
        format!("Madre{pascal}::{}_unico()", field.name)
    }))
}

/// The value-object shape of a literal around `value(primitive type)`.
fn literal_with(field: &Field, value: impl Fn(&str) -> String) -> String {
    let vo = field.value_object.as_deref().unwrap_or_default();
    if let Some(id) = reference_id(field) {
        if field.field_type.starts_with("Option<") {
            "None".to_string()
        } else if is_vec_reference(field) {
            "vec![]".to_string()
        } else {
            format!("{id}::generar()")
        }
    } else if is_option_vo(field) {
        "None".to_string()
    } else if is_vec_vo(field) {
        "vec![]".to_string()
    } else if is_enum_vo(field) {
        let first = field.enum_variants.as_deref().and_then(<[String]>::first);
        format!("{vo}::{}", first.map_or("", String::as_str))
    } else if is_composite_vo(field) {
        let parts: Vec<String> = field.parts.iter().map(|p| value(&p.field_type)).collect();
        format!(
            "{vo}::new({}).expect(\"{vo}::new rechazó el valor de ejemplo\")",
            parts.join(", ")
        )
    } else if is_value_object(field) {
        format!(
            "{vo}::new({}).expect(\"{vo}::new rechazó el valor de ejemplo\")",
            value(&field.field_type)
        )
    } else {
        value(&field.field_type)
    }
}

/// Whether reading a primitive field out of a borrowed struct needs `.clone()` — every registry
/// type that is not `Copy`.
pub fn field_needs_clone(field_type: &str) -> bool {
    matches!(
        field_type,
        "String" | "Option<String>" | "Vec<String>" | "Vec<i64>" | "HashMap<String, String>"
    )
}

/// The expression that reads `field` out of `owner` (a borrowed domain entity) as the primitive
/// the persistence row and the DTO hold: `.value()` / `.as_str()` for value objects, mapped over
/// `Option` / `Vec`, `.clone()` for non-`Copy` primitives. The single home of this decision tree —
/// `entity.rs` and `dto.rs` both call it.
pub fn field_value_accessor(field: &Field, owner: &str) -> String {
    let access = format!("{owner}.{}", field.name);
    let string_inner = vo_inner_type(field) == "String";
    if is_reference(field) {
        if field.field_type.starts_with("Option<") {
            format!("{access}.map(|v| v.valor())")
        } else if is_vec_reference(field) {
            format!("{access}.iter().map(|v| v.valor()).collect()")
        } else {
            format!("{access}.valor()")
        }
    } else if is_enum_vo(field) {
        format!("{access}.as_str().to_string()")
    } else if is_option_vo(field) {
        if string_inner {
            format!("{access}.as_ref().map(|v| v.valor().to_string())")
        } else {
            format!("{access}.map(|v| v.valor())")
        }
    } else if is_vec_vo(field) {
        if string_inner {
            format!("{access}.iter().map(|v| v.valor().to_string()).collect::<Vec<_>>()")
        } else {
            format!("{access}.iter().map(|v| v.valor()).collect::<Vec<_>>()")
        }
    } else if is_value_object(field) {
        if string_inner {
            format!("{access}.valor().to_string()")
        } else {
            format!("{access}.valor()")
        }
    } else if field_needs_clone(&field.field_type) {
        format!("{access}.clone()")
    } else {
        access
    }
}

/// The expression that turns `{source}{field}` (a primitive: a use case param, a DB row column)
/// into the field's domain type — parsing an enum VO, mapping over `Option` / `Vec`, constructing
/// a plain VO — with `?`. A shared VO has its own error type, mapped to the entity's
/// `Invalid{Vo}`. A primitive field is the source itself.
pub fn field_vo_conversion(
    field: &Field,
    source: &str,
    pascal: &str,
    shared_vos: &[ValueObjectDefinition],
) -> String {
    let value = format!("{source}{}", field.name);
    let Some(vo) = field.value_object.as_deref() else {
        return value;
    };
    // Fase 31: from its columns, and the part it rejects is the entity's validation code.
    if is_composite_vo(field) {
        let columns: Vec<String> = columns(field)
            .iter()
            .map(|c| format!("{source}{}", c.name))
            .collect();
        return format!(
            "{vo}::new({}).map_err(|nucleo::errores::Error{vo}::Vacio(campo)| \
             Error{pascal}::Validacion(format!(\"{}_{{campo}}_vacio\")))?",
            columns.join(", "),
            field.name
        );
    }
    let conversion = if is_enum_vo(field) {
        format!("{value}.parse::<{vo}>()")
    } else if is_option_vo(field) {
        format!("{value}.map({vo}::new).transpose()")
    } else if is_vec_vo(field) {
        format!("{value}.into_iter().map({vo}::new).collect::<Result<Vec<_>,_>>()")
    } else {
        format!("{vo}::new({value})")
    };
    if is_shared_vo(field, shared_vos) {
        format!("{conversion}.map_err(|_| Error{pascal}::{vo}Invalido)?")
    } else {
        format!("{conversion}?")
    }
}

// ── Frontend (`web/`) ─────────────────────────────────────────────────────────

/// One `<option>` of a `<select>`.
#[derive(Debug, serde::Serialize, PartialEq)]
pub struct WebOption {
    pub value: String,
    pub label: String,
}

/// What the frontend templates know about one field (ADR-0011, applied to `web/`): its names, its
/// input, and the expressions that move it between the API's types, the form and a screen
/// (ADR-0025: the screens use the API's types). Expressions name helpers of
/// `compartido/formularios.ts`; the `*_helpers` lists say which, so a file imports exactly those.
#[derive(Debug, serde::Serialize)]
pub struct WebFieldCtx {
    /// The JSON key: poem-openapi's camelCase (`inStock`).
    pub camel: String,
    /// What a screen calls it: `In stock`.
    pub label: String,
    /// `text`, `number`, `checkbox`, `datetime-local`, `textarea`, `select`, or `reference`: a
    /// select of the target's items, loaded by the form container (ADR-0022).
    pub input: &'static str,
    /// A number input that takes decimals (`step="any"`).
    pub decimal: bool,
    /// The input must be filled: not an `Option`, a `Vec`, a map or a checkbox.
    pub required: bool,
    /// A `select`'s options, after an empty one labelled `placeholder` (none for a required enum:
    /// it starts on its first variant).
    pub options: Vec<WebOption>,
    pub placeholder: String,
    /// An enum: what each variant reads as on a screen (27.9), in the views' `TEXTOS_<CAMPO>`.
    pub is_enum: bool,
    pub texts_const: String,
    /// The form value of a new, empty form.
    pub empty_form: String,
    /// The form value read out of `{entity}` (the entity's variable) and out of `peticion`. An
    /// `Option` is an optional property that the API answers as `null` when it is absent.
    pub to_form: String,
    pub request_to_form: String,
    /// The request's value read out of `valores` (the form): `undefined` for an empty optional one.
    pub from_form: String,
    /// What a list or detail screen shows for `{entity}`, as a JSX expression.
    pub display: String,
    /// A valid value, for the Object Mother: a present one for an `Option`, a non-empty one for a
    /// `Vec` or a map, so the form round-trip tests exercise the conversion.
    pub mother: String,
    pub to_form_helpers: Vec<&'static str>,
    pub from_form_helpers: Vec<&'static str>,
    pub display_helpers: Vec<&'static str>,
    /// A reference: the target's list path, which its detail page sits under (`/customers`).
    pub ref_path: String,
    /// 27.17: a `por_defecto` field is hidden when creating; a `generado` one is in no form.
    pub defaulted: bool,
    pub generated: bool,
    /// A composite value object (Fase 31): one input per part in a `<fieldset>`. The form holds each
    /// part flat (`direccionCalle`); the request, the screens and the Mother nest them.
    pub is_composite: bool,
    pub parts: Vec<WebFieldCtx>,
}

/// A JSON string literal (double quotes, as Biome writes them).
fn ts_string(s: &str) -> String {
    format!("\"{}\"", s.replace('\\', "\\\\").replace('"', "\\\""))
}

/// A fixed whole-minute instant: a `datetime-local` input round-trips it unchanged.
const MOTHER_DATE_TIME: &str = "\"2026-01-01T10:00:00.000Z\"";

/// The shape of a field on the frontend, which every per-field expression branches on.
#[derive(Clone, Copy, PartialEq)]
enum WebShape {
    Text,
    Number,
    /// A `dinero` value object (27.16): cents, typed in euros with a comma.
    Dinero,
    Checkbox,
    DateTime,
    List {
        numbers: bool,
    },
    Map,
    Enum,
}

fn web_shape(field: &Field) -> WebShape {
    let ty = field.field_type.as_str();
    if is_enum_vo(field) {
        WebShape::Enum
    } else if ty.starts_with("Vec<") {
        WebShape::List {
            numbers: vo_inner_type(field) == "i64",
        }
    } else if ty == "HashMap<String, String>" {
        WebShape::Map
    } else if vo_class(field) == Some("dinero") {
        WebShape::Dinero
    } else {
        match vo_inner_type(field).as_str() {
            "i64" | "f64" => WebShape::Number,
            "bool" => WebShape::Checkbox,
            "DateTime<Utc>" => WebShape::DateTime,
            _ => WebShape::Text,
        }
    }
}

/// The form text of `{owner}.{camel}`, and the helpers that expression calls. A private field is
/// absent from what a public route answers a caller without a session (Phase 20.9).
fn web_to_form(
    shape: WebShape,
    optional: bool,
    private: bool,
    access: &str,
) -> (String, Vec<&'static str>) {
    match shape {
        WebShape::DateTime => (
            format!("aFechaHoraLocal({access})"),
            vec!["aFechaHoraLocal"],
        ),
        WebShape::List { .. } => (format!("{access}.join(\", \")"), vec![]),
        WebShape::Map => (
            format!("formatearEntradas({access})"),
            vec!["formatearEntradas"],
        ),
        WebShape::Dinero => (format!("aImporte({access})"), vec!["aImporte"]),
        _ if optional => (format!("textoDe({access})"), vec!["textoDe"]),
        WebShape::Number => (format!("String({access})"), vec![]),
        _ if private => (format!("{access} ?? \"\""), vec![]),
        _ => (access.to_string(), vec![]),
    }
}

/// What a screen shows for `{owner}.{camel}`, and the helpers that expression calls.
fn web_display(shape: WebShape, optional: bool, access: &str) -> (String, Vec<&'static str>) {
    let (shown, helpers) = match shape {
        WebShape::Checkbox => (format!("{access} ? \"Sí\" : \"No\""), vec![]),
        WebShape::DateTime => (
            format!("formatearFechaHora({access})"),
            vec!["formatearFechaHora"],
        ),
        WebShape::List { .. } => (format!("{access}.join(\", \")"), vec![]),
        WebShape::Map => (
            format!("formatearEntradas({access}, \", \")"),
            vec!["formatearEntradas"],
        ),
        WebShape::Dinero => (format!("euros({access})"), vec!["euros"]),
        _ => (access.to_string(), vec![]),
    };
    match (optional, shape) {
        (true, WebShape::Checkbox | WebShape::DateTime) => {
            (format!("{access} == null ? \"—\" : {shown}"), helpers)
        }
        (true, _) => (format!("{access} ?? \"—\""), helpers),
        _ => (shown, helpers),
    }
}

/// [`web_field_ctx`] of a composite value object (Fase 31): its parts are the columns, read out of
/// and into the nested object (`cliente.direccion.calle`).
fn web_composite_ctx(field: &Field, entity_ident: &str) -> WebFieldCtx {
    use crate::generators::naming::{etiqueta, snake_to_camel};
    let camel = snake_to_camel(&field.name);
    let parts: Vec<WebFieldCtx> = field
        .parts
        .iter()
        .map(|part| {
            let column = Field {
                name: format!("{}_{}", field.name, part.name),
                field_type: part.field_type.clone(),
                label: Some(part.label.clone().unwrap_or_else(|| etiqueta(&part.name))),
                ..Default::default()
            };
            let shape = web_shape(&column);
            let optional = column.field_type.starts_with("Option<");
            let nested = |owner: &str| format!("{owner}.{camel}.{}", snake_to_camel(&part.name));
            let (to_form, to_form_helpers) =
                web_to_form(shape, optional, false, &nested(entity_ident));
            let (request_to_form, _) = web_to_form(shape, optional, false, &nested("peticion"));
            WebFieldCtx {
                to_form,
                to_form_helpers,
                request_to_form,
                ..web_field_ctx(&column, entity_ident)
            }
        })
        .collect();
    let union = |pick: fn(&WebFieldCtx) -> &Vec<&'static str>| {
        let mut names: Vec<&'static str> =
            parts.iter().flat_map(|p| pick(p).iter().copied()).collect();
        names.sort_unstable();
        names.dedup();
        names
    };
    // Expanded, one part per line: Biome keeps an object it finds expanded, and these are long.
    let nest = |value: fn(&WebFieldCtx) -> &String, indent: &str| {
        let lines: Vec<String> = parts
            .iter()
            .zip(&field.parts)
            .map(|(w, p)| format!("{indent}  {}: {},", snake_to_camel(&p.name), value(w)))
            .collect();
        format!("{{\n{}\n{indent}}}", lines.join("\n"))
    };
    WebFieldCtx {
        label: field.label.clone().unwrap_or_else(|| etiqueta(&field.name)),
        input: "composite",
        decimal: false,
        required: false,
        options: vec![],
        placeholder: String::new(),
        is_enum: false,
        texts_const: String::new(),
        empty_form: String::new(),
        to_form: String::new(),
        request_to_form: String::new(),
        // `aPeticion<Entidad>` writes it at 4 spaces, the Mother's `peticion` at 6.
        from_form: nest(|w| &w.from_form, "    "),
        mother: nest(|w| &w.mother, "      "),
        display: format!("unirCampos({entity_ident}.{camel})"),
        to_form_helpers: union(|p| &p.to_form_helpers),
        from_form_helpers: union(|p| &p.from_form_helpers),
        display_helpers: vec!["unirCampos"],
        ref_path: String::new(),
        defaulted: false,
        generated: false,
        is_composite: true,
        parts,
        camel,
    }
}

/// [`web_field_ctx`] of a `vec:ref` (Fase 32): a checkbox per option of its target, in a
/// `<fieldset>`. The form holds the ids it ticks, as the request does.
fn web_set_ctx(field: &Field, entity_ident: &str) -> WebFieldCtx {
    use crate::generators::naming::{etiqueta, pascal_to_snake, pluralize, snake_to_camel};
    let camel = snake_to_camel(&field.name);
    let target = pluralize(&pascal_to_snake(
        field.references.as_deref().unwrap_or_default(),
    ));
    WebFieldCtx {
        label: field.label.clone().unwrap_or_else(|| etiqueta(&field.name)),
        input: "references",
        decimal: false,
        required: false,
        options: vec![],
        placeholder: String::new(),
        is_enum: false,
        texts_const: String::new(),
        empty_form: "[]".to_string(),
        to_form: format!("{entity_ident}.{camel}"),
        request_to_form: format!("peticion.{camel}"),
        from_form: format!("valores.{camel}"),
        display: String::new(),
        mother: "[]".to_string(),
        to_form_helpers: vec![],
        from_form_helpers: vec![],
        display_helpers: vec![],
        ref_path: format!("/{target}"),
        defaulted: false,
        generated: false,
        is_composite: false,
        parts: vec![],
        camel,
    }
}

pub fn web_field_ctx(field: &Field, entity_ident: &str) -> WebFieldCtx {
    if is_composite_vo(field) {
        return web_composite_ctx(field, entity_ident);
    }
    if is_vec_reference(field) {
        return web_set_ctx(field, entity_ident);
    }
    let camel = crate::generators::naming::snake_to_camel(&field.name);
    // A reference is named after what it points to: `customer_id` is the `Customer` input.
    let named = match field.references {
        Some(_) => field.name.strip_suffix("_id").unwrap_or(&field.name),
        None => &field.name,
    };
    // A reference's target list path, which its detail page sits under (`/clientes`).
    let target = field.references.as_deref().map(|target| {
        crate::generators::naming::pluralize(&crate::generators::naming::pascal_to_snake(target))
    });
    let optional = field.field_type.starts_with("Option<");
    let shape = web_shape(field);
    let variants = field.enum_variants.clone().unwrap_or_default();
    let value = format!("valores.{camel}");

    let (input, mother) = match shape {
        WebShape::Enum => (
            "select",
            variants
                .first()
                .map_or("\"\"".to_string(), |v| ts_string(v)),
        ),
        WebShape::List { numbers: true } => ("text", "[42]".into()),
        WebShape::List { numbers: false } => ("text", "[\"example\"]".into()),
        WebShape::Map => ("textarea", "{ key: \"value\" }".into()),
        WebShape::Dinero => ("text", "1250".into()),
        WebShape::Number => (
            "number",
            if vo_inner_type(field) == "f64" {
                "1.5"
            } else {
                "42"
            }
            .into(),
        ),
        WebShape::Checkbox => (if optional { "select" } else { "checkbox" }, "true".into()),
        WebShape::DateTime => ("datetime-local", MOTHER_DATE_TIME.into()),
        WebShape::Text if target.is_some() => ("reference", "crypto.randomUUID()".into()),
        WebShape::Text if vo_inner_type(field) == "Uuid" => ("text", "crypto.randomUUID()".into()),
        WebShape::Text if vo_class(field) == Some("email") => ("email", "\"example\"".into()),
        WebShape::Text => ("text", "\"example\"".into()),
    };
    // A shared value object's example (27.15) is the valid value its rule accepts.
    let mother = match shape {
        WebShape::Text | WebShape::Number | WebShape::Checkbox | WebShape::Dinero => {
            example_ts(field).unwrap_or(mother)
        }
        _ => mother,
    };

    // An enum is the API's `string`: its select only offers the variants. A required text or number
    // needs no helper.
    let from_form_helper = match shape {
        WebShape::List { numbers } => Some(if numbers {
            "separarNumeros"
        } else {
            "separarLista"
        }),
        WebShape::Map => Some("leerEntradas"),
        WebShape::Dinero => Some("deImporte"),
        WebShape::DateTime if optional => Some("fechaHoraOpcional"),
        WebShape::DateTime => Some("deFechaHoraLocal"),
        WebShape::Number if optional => Some("numeroOpcional"),
        WebShape::Checkbox if optional => Some("booleanoOpcional"),
        WebShape::Text if optional => Some("textoOpcional"),
        _ => None,
    };
    let from_form = match (shape, from_form_helper) {
        (_, Some(helper)) => format!("{helper}({value})"),
        (WebShape::Number, None) => format!("Number({value})"),
        _ => value.clone(),
    };

    let options = match (shape, input) {
        (WebShape::Enum, _) => variants
            .iter()
            .map(|v| WebOption {
                value: v.clone(),
                label: field
                    .texts
                    .as_ref()
                    .and_then(|texts| texts.get(v).cloned())
                    .unwrap_or_else(|| crate::generators::naming::texto_variante(v)),
            })
            .collect(),
        (_, "select") => vec![
            WebOption {
                value: "true".into(),
                label: "Sí".into(),
            },
            WebOption {
                value: "false".into(),
                label: "No".into(),
            },
        ],
        _ => vec![],
    };

    let (to_form, to_form_helpers) = web_to_form(
        shape,
        optional,
        field.private,
        &format!("{entity_ident}.{camel}"),
    );
    // A request always holds a private field.
    let (request_to_form, _) = web_to_form(shape, optional, false, &format!("peticion.{camel}"));
    let texts_const = format!("TEXTOS_{}", field.name.to_uppercase());
    let access = format!("{entity_ident}.{camel}");
    let (display, display_helpers) = if shape == WebShape::Enum {
        // A variant the texts do not know (an older API) shows as it is.
        let shown = format!("{texts_const}[{access}] ?? {access}");
        let display = if optional {
            format!("{access} == null ? \"—\" : {shown}")
        } else {
            shown
        };
        (display, vec![])
    } else {
        web_display(shape, optional, &access)
    };

    WebFieldCtx {
        label: field
            .label
            .clone()
            .unwrap_or_else(|| crate::generators::naming::etiqueta(named)),
        input,
        decimal: vo_inner_type(field) == "f64",
        required: !optional
            && !matches!(
                shape,
                WebShape::Checkbox | WebShape::List { .. } | WebShape::Map
            ),
        // No article and no adjective: the entity's gender is unknown (Fase 22).
        placeholder: if target.is_some() && !optional {
            "Elige una opción".to_string()
        } else {
            "—".to_string()
        },
        options,
        // A required enum starts on its first variant: an empty one could not be sent. A field the
        // server defaults (27.17) starts on its default: its input is hidden when creating.
        empty_form: match (input, shape, variants.first()) {
            ("checkbox", _, _) if field.default.is_some() => {
                field.default.clone().unwrap_or_default()
            }
            _ if field.default.is_some() => ts_string(field.default.as_deref().unwrap_or_default()),
            ("checkbox", _, _) => "false".to_string(),
            (_, WebShape::Enum, Some(first)) if !optional => ts_string(first),
            _ => "\"\"".to_string(),
        },
        is_enum: shape == WebShape::Enum,
        defaulted: field.default.is_some(),
        // The owner (27.18) is in no form either, and `actualizar` keeps it.
        generated: field.generated || field.owner,
        texts_const,
        to_form,
        request_to_form,
        from_form,
        display,
        mother,
        to_form_helpers,
        from_form_helpers: from_form_helper.into_iter().collect(),
        display_helpers,
        ref_path: target
            .as_ref()
            .map_or_else(String::new, |plural| format!("/{plural}")),
        is_composite: false,
        parts: vec![],
        camel,
    }
}

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

    #[test]
    fn resolve_string() {
        let m = resolve_type("String").unwrap();
        assert_eq!(m.rust_type, "String");
        assert_eq!(m.sql_type, "TEXT");
        assert!(!m.sql_nullable);
    }

    #[test]
    fn resolve_option_string() {
        let m = resolve_type("Option<String>").unwrap();
        assert_eq!(m.rust_type, "Option<String>");
        assert_eq!(m.sql_type, "TEXT");
        assert!(m.sql_nullable);
    }

    #[test]
    fn resolve_uuid() {
        let m = resolve_type("Uuid").unwrap();
        assert_eq!(m.sql_type, "UUID");
        assert_eq!(m.needs_import, Some("uuid::Uuid"));
    }

    #[test]
    fn resolve_datetime() {
        let m = resolve_type("DateTime<Utc>").unwrap();
        assert_eq!(m.sql_type, "TIMESTAMPTZ");
        assert_eq!(m.needs_import, Some("chrono::{DateTime, Utc}"));
    }

    #[test]
    fn resolve_vec_string() {
        let m = resolve_type("Vec<String>").unwrap();
        assert_eq!(m.sql_type, "TEXT[]");
        assert!(!m.sql_nullable);
    }

    #[test]
    fn resolve_hashmap() {
        let m = resolve_type("HashMap<String, String>").unwrap();
        assert_eq!(m.sql_type, "JSONB");
        assert_eq!(m.needs_import, Some("std::collections::HashMap"));
    }

    #[test]
    fn resolve_unknown_type_errors() {
        let err = resolve_type("CustomType").unwrap_err();
        assert!(err.contains("tipo de campo no admitido 'CustomType'"));
        assert!(err.contains("String"));
    }

    #[test]
    fn validate_fields_ok() {
        use crate::puerto_toml::Field;
        let fields = vec![
            Field {
                name: "name".into(),
                field_type: "String".into(),
                unique: false,
                private: false,
                value_object: None,
                value_object_kind: None,
                enum_variants: None,
                references: None,
                label: None,
                texts: None,
                example: None,
                default: None,
                generated: false,
                owner: false,
                parts: vec![],
            },
            Field {
                name: "price".into(),
                field_type: "i64".into(),
                unique: false,
                private: false,
                value_object: None,
                value_object_kind: None,
                enum_variants: None,
                references: None,
                label: None,
                texts: None,
                example: None,
                default: None,
                generated: false,
                owner: false,
                parts: vec![],
            },
        ];
        assert!(validate_fields(&fields).is_ok());
    }

    #[test]
    fn validate_fields_unknown_errors() {
        use crate::puerto_toml::Field;
        let fields = vec![
            Field {
                name: "name".into(),
                field_type: "String".into(),
                unique: false,
                private: false,
                value_object: None,
                value_object_kind: None,
                enum_variants: None,
                references: None,
                label: None,
                texts: None,
                example: None,
                default: None,
                generated: false,
                owner: false,
                parts: vec![],
            },
            Field {
                name: "bad".into(),
                field_type: "UnknownType".into(),
                unique: false,
                private: false,
                value_object: None,
                value_object_kind: None,
                enum_variants: None,
                references: None,
                label: None,
                texts: None,
                example: None,
                default: None,
                generated: false,
                owner: false,
                parts: vec![],
            },
        ];
        let err = validate_fields(&fields).unwrap_err();
        assert!(err.contains("tipo de campo no admitido 'UnknownType'"));
    }

    fn field(field_type: &str, vo: Option<&str>) -> crate::puerto_toml::Field {
        crate::puerto_toml::Field {
            name: "x".into(),
            field_type: field_type.into(),
            value_object: vo.map(Into::into),
            ..Default::default()
        }
    }

    #[test]
    fn should_tell_option_and_vec_value_objects_apart() {
        assert!(is_option_vo(&field("Option<String>", Some("Mid"))));
        assert!(!is_option_vo(&field("String", Some("Name"))));
        assert!(is_vec_vo(&field("Vec<String>", Some("Tag"))));
        assert!(!is_vec_vo(&field("String", Some("Name"))));
    }

    #[test]
    fn should_unwrap_the_inner_type_of_option_and_vec() {
        assert_eq!(
            vo_inner_type(&field("Option<String>", Some("Mid"))),
            "String"
        );
        assert_eq!(vo_inner_type(&field("Vec<i64>", Some("Score"))), "i64");
        assert_eq!(vo_inner_type(&field("Uuid", Some("Ref"))), "Uuid");
    }

    #[test]
    fn should_import_only_what_a_value_literal_names() {
        assert_eq!(
            literal_import(&field("DateTime<Utc>", None)),
            Some("chrono::Utc")
        );
        assert_eq!(literal_import(&field("Uuid", None)), Some("uuid::Uuid"));
        assert_eq!(
            literal_import(&field("HashMap<String, String>", None)),
            Some("std::collections::HashMap")
        );
        // `None` / `vec![]` literals name no type.
        assert_eq!(literal_import(&field("Option<DateTime<Utc>>", None)), None);
        assert_eq!(literal_import(&field("String", None)), None);
    }

    #[test]
    fn should_name_the_vo_only_in_plain_and_enum_literals() {
        assert!(literal_names_vo(&field("String", Some("Name"))));
        assert!(!literal_names_vo(&field("Option<String>", Some("Mid"))));
        assert!(!literal_names_vo(&field("Vec<String>", Some("Tag"))));
        assert!(!literal_names_vo(&field("String", None)));
    }

    fn enum_field() -> crate::puerto_toml::Field {
        crate::puerto_toml::Field {
            value_object_kind: Some("enum".into()),
            enum_variants: Some(vec!["Active".into()]),
            ..field("String", Some("Status"))
        }
    }

    #[test]
    fn should_clone_only_the_heap_backed_primitives() {
        for ty in [
            "String",
            "Option<String>",
            "Vec<String>",
            "Vec<i64>",
            "HashMap<String, String>",
        ] {
            assert!(field_needs_clone(ty), "{ty} is not Copy");
        }
        for ty in [
            "i64",
            "bool",
            "f64",
            "Uuid",
            "DateTime<Utc>",
            "Option<i64>",
            "Option<bool>",
            "Option<f64>",
            "Option<DateTime<Utc>>",
        ] {
            assert!(!field_needs_clone(ty), "{ty} is Copy");
        }
    }

    #[test]
    fn should_read_the_primitive_out_of_every_value_object_shape() {
        // {plain VO, Option<VO>, Vec<VO>, primitive} × {String, i64, Uuid, DateTime}: the one place
        // that decides how a domain field becomes a primitive (entity.rs `From`, dto.rs `from_domain`).
        let cases: &[(&str, Option<&str>, &str)] = &[
            ("String", Some("V"), "entidad.x.valor().to_string()"),
            ("i64", Some("V"), "entidad.x.valor()"),
            ("Uuid", Some("V"), "entidad.x.valor()"),
            ("DateTime<Utc>", Some("V"), "entidad.x.valor()"),
            (
                "Option<String>",
                Some("V"),
                "entidad.x.as_ref().map(|v| v.valor().to_string())",
            ),
            ("Option<i64>", Some("V"), "entidad.x.map(|v| v.valor())"),
            ("Option<Uuid>", Some("V"), "entidad.x.map(|v| v.valor())"),
            (
                "Option<DateTime<Utc>>",
                Some("V"),
                "entidad.x.map(|v| v.valor())",
            ),
            (
                "Vec<String>",
                Some("V"),
                "entidad.x.iter().map(|v| v.valor().to_string()).collect::<Vec<_>>()",
            ),
            (
                "Vec<i64>",
                Some("V"),
                "entidad.x.iter().map(|v| v.valor()).collect::<Vec<_>>()",
            ),
            (
                "Vec<Uuid>",
                Some("V"),
                "entidad.x.iter().map(|v| v.valor()).collect::<Vec<_>>()",
            ),
            (
                "Vec<DateTime<Utc>>",
                Some("V"),
                "entidad.x.iter().map(|v| v.valor()).collect::<Vec<_>>()",
            ),
            ("String", None, "entidad.x.clone()"),
            ("i64", None, "entidad.x"),
            ("Uuid", None, "entidad.x"),
            ("DateTime<Utc>", None, "entidad.x"),
            ("Option<String>", None, "entidad.x.clone()"),
            ("Option<i64>", None, "entidad.x"),
            ("Vec<String>", None, "entidad.x.clone()"),
            ("HashMap<String, String>", None, "entidad.x.clone()"),
        ];
        for (ty, vo, expected) in cases {
            assert_eq!(
                field_value_accessor(&field(ty, *vo), "entidad"),
                *expected,
                "{ty} / {vo:?}"
            );
        }
        assert_eq!(
            field_value_accessor(&enum_field(), "entidad"),
            "entidad.x.as_str().to_string()"
        );
        assert_eq!(
            field_value_accessor(&field("i64", None), "cuerpo"),
            "cuerpo.x"
        );
    }

    #[test]
    fn should_describe_a_field_to_the_templates() {
        let shared = [ValueObjectDefinition {
            name: "Email".into(),
            inner_type: "String".into(),
            ..Default::default()
        }];

        let plain = field_ctx(&field("String", None), "P", &shared);
        assert!(plain.required_string && !plain.is_vo && !plain.string_vo);
        assert_eq!(
            (plain.rust_type.as_str(), plain.vo.as_str()),
            ("String", "")
        );

        let name = field_ctx(&field("String", Some("FullName")), "P", &shared);
        assert!(name.is_vo && name.string_vo && !name.required_string && !name.is_shared_vo);
        assert_eq!(
            (name.vo.as_str(), name.vo_snake.as_str()),
            ("FullName", "full_name")
        );

        let email = field_ctx(&field("String", Some("Email")), "P", &shared);
        assert!(email.is_shared_vo && email.string_vo);

        let status = field_ctx(&enum_field(), "P", &shared);
        assert!(status.is_enum_vo && status.is_vo && !status.string_vo);
        assert_eq!(status.first_variant, "Active");
        assert_eq!(status.from_params, "params.x.parse::<Status>()?");
        assert_eq!(status.default_literal, "\"example\".to_string()");

        let mid = field_ctx(&field("Option<String>", Some("Mid")), "P", &shared);
        assert!(mid.is_option_vo && !mid.is_vec_vo && !mid.string_vo);
        assert_eq!(mid.rust_type, "Option<Mid>");

        let tags = field_ctx(&field("Vec<String>", Some("Tag")), "P", &shared);
        assert!(tags.is_vec_vo && !tags.is_option_vo && !tags.string_vo);

        let count = field_ctx(&field("i64", None), "P", &shared);
        assert!(!count.required_string && !count.is_vo);
    }

    #[test]
    fn should_take_the_shared_value_object_example_wherever_a_test_needs_a_value() {
        let shared = |name: &str, ty: &str, example: &str| Field {
            name: name.into(),
            field_type: ty.into(),
            value_object: Some(crate::generators::naming::to_pascal_case(name)),
            example: Some(example.into()),
            ..Default::default()
        };
        let email = shared("email", "String", "ana@ejemplo.es");
        let ratio = shared("ratio", "f64", "12");
        let activo = shared("activo", "bool", "false");

        assert_eq!(
            props_literal(&email),
            "Email::new(\"ana@ejemplo.es\".to_string()).expect(\"Email::new rechazó el valor de ejemplo\")"
        );
        assert_eq!(
            field_ctx(&email, "Cliente", &[]).default_literal,
            "\"ana@ejemplo.es\".to_string()"
        );
        assert_eq!(field_ctx(&ratio, "Cliente", &[]).default_literal, "12.0");
        assert_eq!(field_ctx(&activo, "Cliente", &[]).default_literal, "false");
        assert!(seed_literal(&email).contains("\"ana@ejemplo.es\""));
        let unique = Field {
            unique: true,
            ..email.clone()
        };
        assert!(
            !seed_literal(&unique).contains("ana@ejemplo.es"),
            "a fresh value per seed"
        );
        assert_eq!(
            web_field_ctx(&email, "cliente").mother,
            "\"ana@ejemplo.es\""
        );
        assert_eq!(web_field_ctx(&ratio, "cliente").mother, "12");
    }

    #[test]
    fn should_write_a_valid_default_literal_for_every_field_shape() {
        assert_eq!(
            props_literal(&field("String", None)),
            "\"example\".to_string()"
        );
        assert_eq!(props_literal(&field("i64", None)), "42");
        assert_eq!(props_literal(&field("Option<String>", Some("Mid"))), "None");
        assert_eq!(props_literal(&field("Vec<String>", Some("Tag"))), "vec![]");
        assert_eq!(props_literal(&enum_field()), "Status::Active");
        assert_eq!(
            props_literal(&field("i64", Some("Age"))),
            "Age::new(42).expect(\"Age::new rechazó el valor de ejemplo\")"
        );
    }

    #[test]
    fn should_store_build_and_set_every_field_shape_in_the_object_mother() {
        // (field, storage type, `with_` parameter, `build()` value)
        let cases = [
            (
                field("String", None),
                "String",
                "&str",
                "self.x.unwrap_or_else(|| \"example\".to_string())",
            ),
            (field("i64", None), "i64", "i64", "self.x.unwrap_or(42)"),
            (
                field("Uuid", None),
                "Uuid",
                "Uuid",
                "self.x.unwrap_or_else(Uuid::new_v4)",
            ),
            (
                field("DateTime<Utc>", None),
                "DateTime<Utc>",
                "DateTime<Utc>",
                "self.x.unwrap_or_else(Utc::now)",
            ),
            (field("Option<String>", None), "String", "&str", "self.x"),
            (field("Option<i64>", None), "i64", "i64", "self.x"),
            (
                field("Vec<String>", None),
                "Vec<String>",
                "Vec<String>",
                "self.x.unwrap_or_default()",
            ),
            (
                field("HashMap<String, String>", None),
                "HashMap<String, String>",
                "HashMap<String, String>",
                "self.x.unwrap_or_default()",
            ),
            (
                field("String", Some("Name")),
                "Name",
                "Name",
                "self.x.unwrap_or_else(|| Name::new(\"example\".to_string()).expect(\"Name::new rechazó el valor de ejemplo\"))",
            ),
            (field("Option<String>", Some("Mid")), "Mid", "Mid", "self.x"),
            (
                field("Vec<String>", Some("Tag")),
                "Vec<Tag>",
                "Vec<Tag>",
                "self.x.unwrap_or_default()",
            ),
            (
                enum_field(),
                "Status",
                "Status",
                "self.x.unwrap_or(Status::Active)",
            ),
        ];
        for (f, storage, param, build) in cases {
            let ctx = field_ctx(&f, "P", &[]);
            assert_eq!(ctx.mother_storage, storage, "{}", f.field_type);
            assert_eq!(ctx.mother_param, param, "{}", f.field_type);
            assert_eq!(ctx.mother_build, build, "{}", f.field_type);
        }
    }

    #[test]
    fn should_convert_a_primitive_source_into_every_value_object_shape() {
        let shared = [ValueObjectDefinition {
            name: "V".into(),
            inner_type: "String".into(),
            ..Default::default()
        }];
        let local = |ty: &str| field_vo_conversion(&field(ty, Some("V")), "fila.", "P", &[]);
        let shared_ = |ty: &str| field_vo_conversion(&field(ty, Some("V")), "fila.", "P", &shared);

        assert_eq!(local("String"), "V::new(fila.x)?");
        assert_eq!(local("Option<String>"), "fila.x.map(V::new).transpose()?");
        assert_eq!(
            local("Vec<String>"),
            "fila.x.into_iter().map(V::new).collect::<Result<Vec<_>,_>>()?"
        );
        assert_eq!(
            field_vo_conversion(&enum_field(), "fila.", "P", &[]),
            "fila.x.parse::<Status>()?"
        );
        assert_eq!(
            shared_("String"),
            "V::new(fila.x).map_err(|_| ErrorP::VInvalido)?"
        );
        assert_eq!(
            shared_("Option<String>"),
            "fila.x.map(V::new).transpose().map_err(|_| ErrorP::VInvalido)?"
        );
        assert_eq!(
            field_vo_conversion(&field("i64", None), "fila.", "P", &[]),
            "fila.x"
        );
    }

    #[test]
    fn should_seed_fixed_timestamps_and_fresh_unique_values() {
        let unique = |ty: &str| crate::puerto_toml::Field {
            unique: true,
            ..field(ty, None)
        };
        let fixed = "chrono::DateTime::from_timestamp(1_700_000_000, 0).unwrap()";
        assert_eq!(seed_literal(&field("DateTime<Utc>", None)), fixed);
        assert_eq!(
            seed_literal(&unique("String")),
            "Uuid::new_v4().to_string()"
        );
        assert_eq!(
            seed_literal(&unique("i64")),
            "(Uuid::new_v4().as_u128() >> 65) as i64"
        );
        assert_eq!(
            seed_literal(&field("String", None)),
            "\"example\".to_string()"
        );
        assert_eq!(
            seed_literal(&field("HashMap<String, String>", None)),
            "std::collections::HashMap::new()"
        );
        assert_eq!(
            seed_literal(&field("DateTime<Utc>", Some("Joined"))),
            format!("Joined::new({fixed}).expect(\"Joined::new rechazó el valor de ejemplo\")")
        );
        assert_eq!(seed_literal(&field("Vec<String>", Some("Tag"))), "vec![]");
        assert_eq!(seed_literal(&enum_field()), "Status::Active");
    }

    // ── Frontend ──────────────────────────────────────────────────────────────

    fn named(name: &str, field_type: &str) -> crate::puerto_toml::Field {
        crate::puerto_toml::Field {
            name: name.into(),
            field_type: field_type.into(),
            ..Default::default()
        }
    }

    #[test]
    fn should_move_a_number_through_the_form_as_text() {
        let ctx = web_field_ctx(&named("unit_price", "i64"), "orderItem");

        assert_eq!(ctx.camel, "unitPrice");
        assert_eq!(ctx.label, "Unit price");
        assert_eq!(ctx.input, "number");
        assert!(!ctx.decimal && ctx.required);
        assert_eq!(ctx.to_form, "String(orderItem.unitPrice)");
        assert_eq!(ctx.request_to_form, "String(peticion.unitPrice)");
        assert_eq!(ctx.from_form, "Number(valores.unitPrice)");
        assert_eq!(ctx.display, "orderItem.unitPrice");
        assert_eq!(ctx.mother, "42");
    }

    #[test]
    fn should_give_decimals_a_step_and_booleans_a_checkbox() {
        assert!(web_field_ctx(&field("f64", None), "p").decimal);

        let flag = web_field_ctx(&field("bool", None), "p");
        assert_eq!(flag.input, "checkbox");
        assert!(!flag.required);
        assert_eq!(flag.empty_form, "false");
        assert_eq!(
            (flag.to_form.as_str(), flag.from_form.as_str()),
            ("p.x", "valores.x")
        );
        assert_eq!(flag.display, "p.x ? \"Sí\" : \"No\"");
    }

    #[test]
    fn should_keep_strings_uuids_and_value_objects_as_text() {
        let id = web_field_ctx(&field("Uuid", None), "p");
        assert_eq!(id.input, "text");
        assert_eq!(id.mother, "crypto.randomUUID()");

        let name = web_field_ctx(&field("String", Some("Name")), "p");
        assert_eq!((name.input, name.empty_form.as_str()), ("text", "\"\""));
        assert_eq!(
            (name.mother.as_str(), name.to_form.as_str()),
            ("\"example\"", "p.x")
        );
    }

    #[test]
    fn should_read_a_private_field_as_absent_and_always_send_it() {
        let email = crate::puerto_toml::Field {
            private: true,
            ..field("String", None)
        };
        let ctx = web_field_ctx(&email, "p");

        assert_eq!(ctx.to_form, "p.x ?? \"\"");
        assert_eq!(ctx.request_to_form, "peticion.x");
        assert_eq!(ctx.from_form, "valores.x");
    }

    #[test]
    fn should_read_an_absent_option_as_empty_text_and_leave_an_empty_one_out() {
        let views = web_field_ctx(&field("Option<i64>", None), "p");

        assert!(!views.required);
        assert_eq!(
            (views.to_form.as_str(), views.from_form.as_str()),
            ("textoDe(p.x)", "numeroOpcional(valores.x)")
        );
        assert_eq!(views.display, "p.x ?? \"—\"");
        assert_eq!(views.mother, "42", "present, so the round trip converts it");
        assert_eq!(views.to_form_helpers, ["textoDe"]);
        assert_eq!(views.from_form_helpers, ["numeroOpcional"]);

        let middle = web_field_ctx(&field("Option<String>", Some("MiddleName")), "p");
        assert_eq!(middle.from_form, "textoOpcional(valores.x)");
    }

    #[test]
    fn should_choose_an_optional_boolean_from_a_select() {
        let flag = web_field_ctx(&field("Option<bool>", None), "p");

        assert_eq!(flag.input, "select");
        assert_eq!(flag.empty_form, "\"\"");
        assert_eq!(
            flag.options
                .iter()
                .map(|o| o.value.as_str())
                .collect::<Vec<_>>(),
            ["true", "false"]
        );
        assert_eq!(flag.from_form, "booleanoOpcional(valores.x)");
        assert_eq!(flag.placeholder, "—");
        // The API answers `null` for an absent one, though its type says `undefined`.
        assert_eq!(flag.display, "p.x == null ? \"—\" : p.x ? \"Sí\" : \"No\"");
    }

    #[test]
    fn should_edit_a_date_time_in_local_time_and_send_iso_8601() {
        let at = web_field_ctx(&field("DateTime<Utc>", None), "p");
        assert_eq!(at.input, "datetime-local");
        assert!(at.required);
        assert_eq!(
            (at.to_form.as_str(), at.from_form.as_str()),
            ("aFechaHoraLocal(p.x)", "deFechaHoraLocal(valores.x)")
        );
        assert_eq!(at.display, "formatearFechaHora(p.x)");
        assert_eq!(at.display_helpers, ["formatearFechaHora"]);
        assert_eq!(at.mother, MOTHER_DATE_TIME);

        let expires = web_field_ctx(&field("Option<DateTime<Utc>>", None), "p");
        assert_eq!(expires.from_form, "fechaHoraOpcional(valores.x)");
        assert_eq!(
            expires.display,
            "p.x == null ? \"—\" : formatearFechaHora(p.x)"
        );
    }

    #[test]
    fn should_write_a_list_comma_separated_and_a_map_one_entry_per_line() {
        let tags = web_field_ctx(&field("Vec<String>", Some("Tag")), "p");
        assert_eq!(tags.input, "text");
        assert!(!tags.required);
        assert_eq!(
            (tags.to_form.as_str(), tags.from_form.as_str()),
            ("p.x.join(\", \")", "separarLista(valores.x)")
        );
        assert_eq!(tags.mother, "[\"example\"]");

        let scores = web_field_ctx(&field("Vec<i64>", None), "p");
        assert_eq!(scores.from_form, "separarNumeros(valores.x)");

        let meta = web_field_ctx(&field("HashMap<String, String>", None), "p");
        assert_eq!(meta.input, "textarea");
        assert_eq!(
            (meta.to_form.as_str(), meta.from_form.as_str()),
            ("formatearEntradas(p.x)", "leerEntradas(valores.x)")
        );
        assert_eq!(meta.display, "formatearEntradas(p.x, \", \")");
    }

    #[test]
    fn should_send_an_enum_as_the_string_its_select_chose() {
        let ctx = web_field_ctx(&enum_field(), "p");

        assert_eq!(ctx.input, "select");
        assert!(ctx.required);
        assert_eq!(ctx.from_form, format!("valores.{}", ctx.camel));
        // 27.9: a required enum starts on its first variant, with no empty option to choose.
        assert_eq!(ctx.empty_form, "\"Active\"");
        assert_eq!(
            ctx.options.first().map(|o| o.value.as_str()),
            Some("Active")
        );
        assert_eq!(ctx.mother, "\"Active\"");
    }
}