proa-cli 0.1.3

CLI for scaffolding, inspecting, formatting, and validating Proa web projects
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
// Copyright Proa Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use std::{
    env, fs,
    path::{Path, PathBuf},
};

use anyhow::{bail, Context, Result};
use clap::ValueEnum;
use serde::Serialize;
use sha2::{Digest, Sha256};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, ValueEnum)]
pub enum DryRunFormat {
    Text,
    Json,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, ValueEnum)]
pub enum ServerIntegration {
    Axum,
    None,
}

impl std::fmt::Display for ServerIntegration {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Axum => f.write_str("axum"),
            Self::None => f.write_str("none"),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, ValueEnum)]
pub enum DocsIntegration {
    None,
    #[value(name = "proa_docs")]
    ProaDocs,
}

impl std::fmt::Display for DocsIntegration {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::None => f.write_str("none"),
            Self::ProaDocs => f.write_str("proa_docs"),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, ValueEnum)]
pub enum DeployTarget {
    None,
    Coolify,
}

impl std::fmt::Display for DeployTarget {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::None => f.write_str("none"),
            Self::Coolify => f.write_str("coolify"),
        }
    }
}

pub struct NewSiteArgs {
    pub name: String,
    pub template: String,
    pub destination: Option<PathBuf>,
    pub copy: Option<String>,
    pub empty: bool,
    pub server: ServerIntegration,
    pub tailwind: bool,
    pub no_tailwind: bool,
    pub docs: DocsIntegration,
    pub blog: bool,
    pub no_blog: bool,
    pub deploy: DeployTarget,
    pub skip_install: bool,
    pub no_git: bool,
    pub agents_md: bool,
    pub no_agents_md: bool,
    pub dry_run: Option<DryRunFormat>,
    pub force: bool,
    pub yes: bool,
}

pub struct NewSiteOptions {
    pub name: String,
    pub root: PathBuf,
    pub template: String,
    pub empty: bool,
    pub server: ServerIntegration,
    pub tailwind: bool,
    pub docs: DocsIntegration,
    pub blog: bool,
    pub deploy: DeployTarget,
    pub skip_install: bool,
    pub no_git: bool,
    pub agents_md: bool,
    pub dry_run: Option<DryRunFormat>,
    pub force: bool,
    pub yes: bool,
}

impl NewSiteOptions {
    pub fn from_args(args: NewSiteArgs) -> Result<Self> {
        if args.copy.is_some() {
            bail!("--copy is reserved for template copying but is not implemented yet");
        }
        if args.tailwind && args.no_tailwind {
            bail!("--tailwind and --no-tailwind cannot be combined");
        }
        if args.blog && args.no_blog {
            bail!("--blog and --no-blog cannot be combined");
        }
        if args.agents_md && args.no_agents_md {
            bail!("--agents-md and --no-agents-md cannot be combined");
        }
        if args.deploy == DeployTarget::Coolify && args.server != ServerIntegration::Axum {
            bail!("--deploy coolify requires --server axum");
        }
        if args.empty && args.deploy != DeployTarget::None {
            bail!("--empty cannot be combined with --deploy");
        }

        let destination = args.destination.unwrap_or(env::current_dir()?);
        let name_path = PathBuf::from(&args.name);
        let root = if name_path.is_absolute() {
            name_path
        } else {
            destination.join(name_path)
        };

        let mut docs = args.docs;
        let mut blog = args.blog;
        let mut tailwind = !args.no_tailwind;
        let mut agents_md = !args.no_agents_md;

        if args.empty {
            docs = DocsIntegration::None;
            blog = false;
            tailwind = false;
            agents_md = false;
        }

        Ok(Self {
            name: args.name,
            root,
            template: args.template,
            empty: args.empty,
            server: args.server,
            tailwind,
            docs,
            blog,
            deploy: args.deploy,
            skip_install: args.skip_install,
            no_git: args.no_git,
            agents_md,
            dry_run: args.dry_run,
            force: args.force,
            yes: args.yes,
        })
    }
}

pub struct GenerateComponentOptions {
    pub name: String,
    pub root: Option<PathBuf>,
    pub folder: bool,
    pub recipe: bool,
    pub children: bool,
    pub asynchronous: bool,
    pub markdown: bool,
    pub no_test: bool,
    pub dry_run: Option<DryRunFormat>,
    pub force: bool,
}

pub struct GenerateLayoutOptions {
    pub name: String,
    pub root: Option<PathBuf>,
    pub document: bool,
    pub section: bool,
    pub replace_root: bool,
    pub dry_run: Option<DryRunFormat>,
    pub force: bool,
}

pub struct GenerateDataOptions {
    pub name: String,
    pub root: Option<PathBuf>,
    pub dry_run: Option<DryRunFormat>,
    pub force: bool,
}

#[derive(Debug, Default, Serialize)]
pub struct ScaffoldPlan {
    pub root: PathBuf,
    pub operations: Vec<PlanOperation>,
    pub warnings: Vec<String>,
    pub follow_up: Vec<String>,
}

#[derive(Debug, Clone, Serialize)]
#[serde(tag = "action", rename_all = "snake_case")]
pub enum PlanOperation {
    WriteFile {
        path: PathBuf,
        description: String,
        bytes: usize,
        #[serde(skip)]
        content: String,
    },
    AppendLine {
        path: PathBuf,
        description: String,
        line: String,
    },
}

#[derive(Debug, Serialize)]
pub struct SiteCheckReport {
    pub root: PathBuf,
    pub errors: Vec<String>,
    pub warnings: Vec<String>,
}

#[derive(Debug, Serialize)]
pub struct RouteInfo {
    pub method: &'static str,
    pub path: String,
    pub kind: &'static str,
    pub representations: String,
    pub file: PathBuf,
}

pub fn new_site(options: NewSiteOptions) -> Result<()> {
    new_site_with_tailwind_ready(options, false)
}

pub(crate) fn new_site_with_tailwind_ready(
    options: NewSiteOptions,
    tailwind_ready: bool,
) -> Result<()> {
    let mut plan = ScaffoldPlan::new(options.root.clone());
    let project_name = package_name(
        options
            .root
            .file_name()
            .and_then(|name| name.to_str())
            .unwrap_or(&options.name),
    );
    let title = title_case(&project_name);
    let has_docs = options.docs == DocsIntegration::ProaDocs;
    let has_blog = options.blog;
    let has_axum = options.server == ServerIntegration::Axum;
    // Font wiring needs the generated crate root (src/main.rs) to include the
    // build-generated module, so it is tied to the Axum integration.
    let has_fonts = !options.empty && has_axum;
    let has_coolify = options.deploy == DeployTarget::Coolify;

    if options.template != "basic" && options.template != "marketing" && options.template != "docs"
    {
        plan.warnings.push(format!(
            "unknown template `{}`; using the basic site shape",
            options.template
        ));
    }

    plan.write_file(
        options.root.join("Cargo.toml"),
        cargo_toml(&project_name, &options),
    );
    plan.write_file(
        options.root.join(crate::config::CONFIG_FILE),
        crate::config::generated_project_config(options.tailwind)?,
    );
    plan.write_file(
        options.root.join(crate::config::LOCK_FILE),
        crate::config::generated_component_lock()?,
    );
    plan.write_file(options.root.join(".cargo/config.toml"), cargo_config_toml());
    plan.write_file(options.root.join("scripts/dev.sh"), dev_script());
    plan.write_file(options.root.join("scripts/build.sh"), build_script());
    plan.write_file(
        options.root.join("scripts/watch-lock.sh"),
        watch_lock_script(),
    );
    plan.write_file(
        options.root.join("README.md"),
        readme(&project_name, options.deploy),
    );
    plan.write_file(options.root.join(".gitignore"), gitignore());
    if options.agents_md {
        plan.write_file(options.root.join("AGENTS.md"), agents_md());
    }

    plan.write_file(options.root.join("src/components/mod.rs"), components_mod());
    plan.write_file(
        options.root.join("src/layouts/mod.rs"),
        "pub mod root;\n".to_string(),
    );
    plan.write_file(options.root.join("src/layouts/root.rs"), root_layout());
    plan.write_file(
        options.root.join("src/data/mod.rs"),
        "pub mod nav;\npub mod routes;\n".to_string(),
    );
    plan.write_file(options.root.join("src/data/nav.rs"), nav_data());
    plan.write_file(
        options.root.join("src/data/routes.rs"),
        routes_data(has_docs, has_blog),
    );
    plan.write_file(
        options.root.join("src/pages/mod.rs"),
        pages_mod(has_docs, has_blog),
    );
    plan.write_file(
        options.root.join("src/pages/home.rs"),
        home_page(&title, has_axum),
    );
    plan.write_file(options.root.join("public/favicon.svg"), favicon_svg());

    if has_axum {
        plan.write_file(
            options.root.join("src/main.rs"),
            main_rs(has_fonts, has_docs, has_blog),
        );
        plan.write_file(
            options.root.join("src/static_assets.rs"),
            static_assets_rs(has_fonts),
        );
    } else {
        plan.follow_up
            .push("wire src/pages::home::render() into your custom server integration".to_string());
    }

    if options.tailwind {
        plan.write_file(options.root.join("styles/input.css"), tailwind_input_css());
        plan.write_file(
            options.root.join("public/styles.css"),
            generated_styles_placeholder(),
        );
        if !tailwind_ready {
            plan.follow_up.push(
                "install Tailwind CSS CLI v4 with the Proa installer (`--tailwind-only`) or Homebrew (`brew install tailwindcss`) before building assets".to_string(),
            );
        }
        plan.follow_up
            .push("run bash scripts/dev.sh to build assets and start the server".to_string());
    } else {
        plan.write_file(options.root.join("public/styles.css"), plain_css());
    }

    if has_docs {
        plan.write_file(options.root.join("src/docs/index.mdx"), docs_index());
        plan.write_file(
            options.root.join("src/docs/getting-started.mdx"),
            docs_getting_started(),
        );
        plan.write_file(options.root.join("src/pages/docs.rs"), docs_page(has_axum));
    }

    if has_blog {
        plan.write_file(
            options.root.join("src/blog/first-post.mdx"),
            blog_first_post(),
        );
        plan.write_file(options.root.join("src/pages/blog.rs"), blog_page(has_axum));
    }

    if has_fonts || has_docs || has_blog {
        plan.write_file(
            options.root.join("build.rs"),
            build_rs(has_fonts, has_docs, has_blog),
        );
    }

    if has_coolify {
        plan.write_file(options.root.join("Dockerfile"), dockerfile(&project_name));
        plan.write_file(options.root.join(".dockerignore"), dockerignore());
        plan.write_file(
            options.root.join("coolify.env.example"),
            coolify_env_example(),
        );
        plan.write_file(
            options.root.join("COOLIFY.md"),
            coolify_readme(&project_name),
        );
        plan.follow_up.push(
            "configure Coolify with Dockerfile build, port 3000, and /healthz health check"
                .to_string(),
        );
        plan.follow_up.push(
            "set up wildcard DNS and Coolify Preview Deployments for pull request URLs".to_string(),
        );
    }

    if !options.skip_install {
        plan.follow_up
            .push("run proa build before deploying".to_string());
    }
    if options.no_git {
        plan.warnings
            .push("git initialization skipped by --no-git".to_string());
    }

    finish_plan(plan, options.dry_run, options.force)
}

pub fn generate_component(options: GenerateComponentOptions) -> Result<()> {
    let root = resolve_root(options.root)?;
    let target = ModuleTarget::new(&root, "components", &options.name)?;
    let struct_name = pascal_case(&target.leaf);
    let data_attr = format!("data-{}", kebab_case(&target.leaf));
    let mut plan = ScaffoldPlan::new(root.clone());

    target.plan_mod_updates(&mut plan);

    if options.folder || options.recipe {
        let dir = target.file_path.with_extension("");
        plan.write_file(
            dir.join("mod.rs"),
            if options.recipe {
                format!(
                    "mod {};\nmod recipe;\n\npub use {}::{};\npub use recipe::{}Recipe;\n",
                    target.leaf, target.leaf, struct_name, struct_name
                )
            } else {
                format!(
                    "mod {};\n\npub use {}::{};\n",
                    target.leaf, target.leaf, struct_name
                )
            },
        );
        plan.write_file(
            dir.join(format!("{}.rs", target.leaf)),
            component_source(
                &struct_name,
                &data_attr,
                options.children,
                options.asynchronous,
                options.markdown,
                options.recipe,
                !options.no_test,
            ),
        );
        if options.recipe {
            plan.write_file(dir.join("recipe.rs"), component_recipe_source(&struct_name));
        }
    } else {
        plan.write_file(
            target.file_path,
            component_source(
                &struct_name,
                &data_attr,
                options.children,
                options.asynchronous,
                options.markdown,
                false,
                !options.no_test,
            ),
        );
    }

    finish_plan(plan, options.dry_run, options.force)
}

pub fn generate_layout(options: GenerateLayoutOptions) -> Result<()> {
    if options.document && options.section {
        bail!("--document and --section cannot be combined");
    }

    let root = resolve_root(options.root)?;
    let target = ModuleTarget::new(&root, "layouts", &options.name)?;
    let struct_name = format!("{}Layout", pascal_case(&target.leaf));
    let mut plan = ScaffoldPlan::new(root.clone());

    if options.document {
        let mut current_roots = Vec::new();
        let layouts = root.join("src/layouts");
        if layouts.is_dir() {
            for path in rust_files(&layouts)? {
                if path != target.file_path
                    && fs::read_to_string(&path)
                        .unwrap_or_default()
                        .contains("// proa:document-root")
                {
                    current_roots.push(path);
                }
            }
        }
        if !current_roots.is_empty() && !options.replace_root {
            bail!(
                "a document root is already marked at {}; rerun with --replace-root",
                display_rel(&root, &current_roots[0])
            );
        }
        for path in current_roots {
            let source = fs::read_to_string(&path)?
                .replace("// proa:document-root", "// proa:document-layout");
            plan.write_file(path, source);
        }
    }

    target.plan_mod_updates(&mut plan);
    let source = if options.document {
        document_layout_source(&struct_name)
    } else {
        section_layout_source(&struct_name)
    };
    plan.write_file(target.file_path, source);

    finish_plan(plan, options.dry_run, options.force || options.replace_root)
}

pub fn generate_data(options: GenerateDataOptions) -> Result<()> {
    let root = resolve_root(options.root)?;
    let target = ModuleTarget::new(&root, "static_data", &options.name)?;
    let mut plan = ScaffoldPlan::new(root);

    target.plan_mod_updates(&mut plan);
    plan.write_file(target.file_path, data_source(&target.leaf));

    finish_plan(plan, options.dry_run, options.force)
}

pub fn site_check(root: Option<PathBuf>) -> Result<SiteCheckReport> {
    let root = resolve_root(root)?;
    let mut report = SiteCheckReport {
        root: root.clone(),
        errors: Vec::new(),
        warnings: Vec::new(),
    };

    let config_value = fs::read_to_string(root.join(crate::config::CONFIG_FILE))
        .ok()
        .and_then(|source| serde_json::from_str::<serde_json::Value>(&source).ok());
    let application_first = config_value
        .as_ref()
        .and_then(|value| value.get("application"))
        .is_some();
    let template = config_value
        .as_ref()
        .and_then(|value| value.pointer("/application/template"))
        .and_then(serde_json::Value::as_str)
        .unwrap_or("")
        .to_string();
    let origin_template = template == "origin";
    let required: &[&str] = if origin_template {
        &[
            "src/lib.rs",
            "src/main.rs",
            "src/components/mod.rs",
            "src/layouts/mod.rs",
            "public/styles.css",
        ]
    } else if application_first {
        &[
            "src/pages/mod.rs",
            "src/components/mod.rs",
            "src/layouts/mod.rs",
            "src/endpoints/mod.rs",
            "src/routes.rs",
            "src/app.rs",
            "src/config.rs",
            "src/error.rs",
            "src/static_assets.rs",
        ]
    } else {
        &[
            "src/pages/mod.rs",
            "src/components/mod.rs",
            "src/layouts/mod.rs",
            "src/data/mod.rs",
        ]
    };
    for rel in required {
        if !root.join(rel).exists() {
            report.errors.push(format!("missing {rel}"));
        }
    }

    if root.join("src/islands").exists() {
        report
            .errors
            .push("src/islands should not be generated for this site convention".to_string());
    }

    if application_first {
        // Capability checks follow application code after users replace starter pages.
        let sources = rust_files(&root.join("src"))?
            .into_iter()
            .map(fs::read_to_string)
            .collect::<std::io::Result<Vec<_>>>()?;
        let has_source = |needle: &str| sources.iter().any(|source| source.contains(needle));
        if domain_root_is_empty(&root.join("src/models"))
            || domain_root_is_empty(&root.join("src/services"))
        {
            report.errors.push(
                "empty domain roots must not be generated; create models/services on demand"
                    .to_string(),
            );
        }
        let database = config_value
            .as_ref()
            .and_then(|value| value.pointer("/database/driver"))
            .and_then(serde_json::Value::as_str);
        if database == Some("sqlite") {
            let manual = config_value
                .as_ref()
                .and_then(|value| value.pointer("/database/integration"))
                .and_then(serde_json::Value::as_str)
                == Some("manual");
            let required_database_files: &[&str] = if manual {
                &["src/db.rs", "data/.gitkeep", "migrations/.gitkeep"]
            } else {
                &[
                    "src/db.rs",
                    "src/loader.rs",
                    "data/.gitkeep",
                    "migrations/.gitkeep",
                ]
            };
            for rel in required_database_files {
                if !root.join(rel).exists() {
                    report
                        .errors
                        .push(format!("SQLite capability is missing {rel}"));
                }
            }
            if manual {
                report.warnings.push(
                    "SQLite is installed with manual integration pending; add `mod db;`, connect the pool, and place it in application state"
                        .to_string(),
                );
            } else {
                if !has_source("DataResolver::request_scope") {
                    report.errors.push(
                        "SQLite application does not create a request-scoped DataResolver"
                            .to_string(),
                    );
                }
            }
        }
        let markdown = config_value
            .as_ref()
            .and_then(|value| value.pointer("/representations/markdown/enabled"))
            .and_then(serde_json::Value::as_bool)
            .unwrap_or(false);
        if markdown {
            if origin_template {
                let library = fs::read_to_string(root.join("src/lib.rs")).unwrap_or_default();
                for contract in [
                    "MarkdownAlternate::new(\"/index.md\")",
                    "explicit_markdown(",
                ] {
                    if !library.contains(contract) {
                        report.errors.push(format!(
                            "Markdown representation contract is missing `{contract}`"
                        ));
                    }
                }
                let page = fs::read_to_string(root.join("src/components/atlas_page.rs"))
                    .unwrap_or_default();
                if !page.contains("MdRenderSync") {
                    report.errors.push(
                        "Markdown representation contract is missing `MdRenderSync`".to_string(),
                    );
                }
            } else {
                for contract in ["MdRenderSync", "explicit_markdown("] {
                    let has_contract = has_source(contract)
                        || match contract {
                            "MdRenderSync" => has_source("MdRender<"),
                            "explicit_markdown(" => {
                                has_source("explicit_markdown_async_with_loader(")
                            }
                            _ => false,
                        };
                    if !has_contract {
                        report.errors.push(format!(
                            "Markdown representation contract is missing `{contract}`"
                        ));
                    }
                }
            }
            let layout = fs::read_to_string(root.join("src/layouts/root.rs")).unwrap_or_default();
            if layout.contains("MdRender") {
                report
                    .errors
                    .push("RootDocument must remain HTML-only".to_string());
            }
        }
        if let Ok(validation) = crate::config::validate_project_config(&root) {
            report.errors.extend(validation.errors);
            report.warnings.extend(validation.warnings);
        }
        for rel in [
            ".vscode/extensions.json",
            ".vscode/settings.json",
            ".vscode/tasks.json",
        ] {
            let path = root.join(rel);
            if path.exists()
                && fs::read_to_string(&path)
                    .ok()
                    .and_then(|source| serde_json::from_str::<serde_json::Value>(&source).ok())
                    .is_none()
            {
                report.errors.push(format!("{rel} is not valid JSON"));
            }
        }
        validate_component_hashes(&root, &mut report)?;
        validate_migration_pairs(&root, &mut report)?;
    }

    let pages_dir = root.join("src/pages");
    if pages_dir.exists() {
        for file in rust_files(&pages_dir)? {
            if file.file_name().and_then(|name| name.to_str()) == Some("mod.rs") {
                continue;
            }
            let source =
                fs::read_to_string(&file).with_context(|| format!("read {}", file.display()))?;
            if !source.contains("fn handler") && !source.contains("fn render") {
                // proa-perf-ok: CLI validation builds diagnostic messages outside SSR render paths
                report.errors.push(format!(
                    "{} does not expose a handler or render function",
                    display_rel(&root, &file)
                ));
            }
        }
    }

    Ok(report)
}

fn domain_root_is_empty(path: &Path) -> bool {
    if !path.is_dir() {
        return false;
    }
    fs::read_dir(path)
        .ok()
        .into_iter()
        .flatten()
        .filter_map(Result::ok)
        .all(|entry| entry.file_name() == "mod.rs")
}

fn validate_component_hashes(root: &Path, report: &mut SiteCheckReport) -> Result<()> {
    let lock_path = root.join(crate::config::LOCK_FILE);
    if !lock_path.is_file() {
        return Ok(());
    }
    let lock: serde_json::Value = serde_json::from_str(&fs::read_to_string(&lock_path)?)?;
    let Some(components) = lock
        .get("components")
        .and_then(serde_json::Value::as_object)
    else {
        return Ok(());
    };
    for (component, value) in components {
        let Some(files) = value.get("files").and_then(serde_json::Value::as_object) else {
            continue;
        };
        for (relative, expected) in files {
            let path = root.join(relative);
            let actual = fs::read(&path)
                .ok()
                .map(|bytes| format!("sha256:{:x}", Sha256::digest(bytes)));
            if actual.as_deref() != expected.as_str() {
                report.errors.push(format!(
                    "component {component} has local drift in {relative}"
                ));
            }
        }
    }
    Ok(())
}

fn validate_migration_pairs(root: &Path, report: &mut SiteCheckReport) -> Result<()> {
    let directory = root.join("migrations");
    if !directory.is_dir() {
        return Ok(());
    }
    let mut up = std::collections::BTreeSet::new();
    let mut down = std::collections::BTreeSet::new();
    for entry in fs::read_dir(directory)? {
        let name = entry?.file_name().to_string_lossy().into_owned();
        if let Some(base) = name.strip_suffix(".up.sql") {
            up.insert(base.to_string());
        } else if let Some(base) = name.strip_suffix(".down.sql") {
            down.insert(base.to_string());
        }
    }
    for missing in up.difference(&down) {
        report
            .errors
            .push(format!("migration {missing} is missing its .down.sql pair"));
    }
    for missing in down.difference(&up) {
        report
            .errors
            .push(format!("migration {missing} is missing its .up.sql pair"));
    }
    Ok(())
}

pub fn print_site_check(report: &SiteCheckReport, json: bool) -> Result<()> {
    if json {
        println!("{}", serde_json::to_string_pretty(report)?);
        return Ok(());
    }

    if report.errors.is_empty() {
        println!("Proa site check passed for {}", report.root.display());
    } else {
        for error in &report.errors {
            eprintln!("error: {error}");
        }
    }
    for warning in &report.warnings {
        eprintln!("warning: {warning}");
    }
    Ok(())
}

pub fn site_routes(root: Option<PathBuf>) -> Result<Vec<RouteInfo>> {
    let root = resolve_root(root)?;
    let config = fs::read_to_string(root.join(crate::config::CONFIG_FILE))
        .ok()
        .and_then(|source| serde_json::from_str::<serde_json::Value>(&source).ok());
    if config
        .as_ref()
        .and_then(|value| value.get("application"))
        .is_some()
    {
        let markdown = config
            .as_ref()
            .and_then(|value| value.pointer("/representations/markdown/enabled"))
            .and_then(serde_json::Value::as_bool)
            .unwrap_or(false);
        let mut routes = vec![RouteInfo {
            method: "GET",
            path: "/".to_string(),
            kind: "page",
            representations: if markdown {
                "html · md:/index.md".to_string()
            } else {
                "html".to_string()
            },
            file: PathBuf::from("src/pages/home.rs"),
        }];
        if markdown {
            routes.push(RouteInfo {
                method: "GET",
                path: "/index.md".to_string(),
                kind: "representation",
                representations: "markdown".to_string(),
                file: PathBuf::from("src/pages/home.rs"),
            });
        }
        for path in ["/healthz", "/readyz"] {
            routes.push(RouteInfo {
                method: "GET",
                path: path.to_string(),
                kind: "endpoint",
                representations: "json".to_string(),
                file: PathBuf::from("src/endpoints/health.rs"),
            });
        }
        let generated_routes = fs::read_to_string(root.join("src/routes.rs")).unwrap_or_default();
        for line in generated_routes.lines() {
            let trimmed = line.trim();
            if !trimmed.starts_with("Route::endpoint(\"") {
                continue;
            }
            let Some(route_path) = trimmed
                .strip_prefix("Route::endpoint(\"")
                .and_then(|rest| rest.split('\"').next())
            else {
                continue;
            };
            if route_path == "/" || routes.iter().any(|route| route.path == route_path) {
                continue;
            }
            let (kind, module, method) = if let Some(rest) = trimmed.split("pages::").nth(1) {
                ("page", rest.split("::").next().unwrap_or("unknown"), "GET")
            } else if let Some(rest) = trimmed.split("endpoints::").nth(1) {
                let method = ["get", "post", "put", "patch", "delete"]
                    .into_iter()
                    .find(|method| trimmed.contains(&format!("routing::{method}(")))
                    .unwrap_or("get");
                (
                    "endpoint",
                    rest.split("::").next().unwrap_or("unknown"),
                    match method {
                        "post" => "POST",
                        "put" => "PUT",
                        "patch" => "PATCH",
                        "delete" => "DELETE",
                        _ => "GET",
                    },
                )
            } else {
                continue;
            };
            let markdown_alias = format!("{route_path}.md");
            let has_markdown = kind == "page"
                && generated_routes.contains(&format!(".route(\"{markdown_alias}\""));
            routes.push(RouteInfo {
                method,
                path: route_path.to_string(),
                kind,
                representations: if has_markdown {
                    format!("html · md:{markdown_alias}")
                } else if kind == "page" {
                    "html".to_string()
                } else {
                    "response".to_string()
                },
                file: PathBuf::from(format!("src/{}s/{module}.rs", kind)),
            });
            if has_markdown {
                routes.push(RouteInfo {
                    method: "GET",
                    path: markdown_alias,
                    kind: "representation",
                    representations: "markdown".to_string(),
                    file: PathBuf::from(format!("src/pages/{module}.rs")),
                });
            }
        }
        routes.sort_by(|left, right| left.path.cmp(&right.path));
        return Ok(routes);
    }
    let pages_dir = root.join("src/pages");
    let mut routes = Vec::new();
    if !pages_dir.exists() {
        return Ok(routes);
    }

    for file in rust_files(&pages_dir)? {
        if file.file_name().and_then(|name| name.to_str()) == Some("mod.rs") {
            continue;
        }
        let rel = file.strip_prefix(&pages_dir).unwrap_or(&file);
        routes.push(RouteInfo {
            method: "GET",
            path: route_path_for_page(rel),
            kind: "page",
            representations: "html".to_string(),
            file: display_rel_path(&root, &file),
        });
    }
    routes.sort_by(|a, b| a.path.cmp(&b.path));
    Ok(routes)
}

pub fn print_routes(routes: &[RouteInfo], json: bool) -> Result<()> {
    if json {
        println!("{}", serde_json::to_string_pretty(routes)?);
        return Ok(());
    }

    println!("METHOD  PATH                             KIND            REPRESENTATIONS             SOURCE");
    for route in routes {
        println!(
            "{:<7} {:<32} {:<15} {:<27} {}",
            route.method,
            route.path,
            route.kind,
            route.representations,
            route.file.display()
        );
    }
    Ok(())
}

impl ScaffoldPlan {
    fn new(root: PathBuf) -> Self {
        Self {
            root,
            operations: Vec::new(),
            warnings: Vec::new(),
            follow_up: Vec::new(),
        }
    }

    fn write_file(&mut self, path: PathBuf, content: String) {
        self.operations.push(PlanOperation::WriteFile {
            description: "write file".to_string(),
            path,
            bytes: content.len(),
            content,
        });
    }

    fn append_line(&mut self, path: PathBuf, line: String, description: impl Into<String>) {
        self.operations.push(PlanOperation::AppendLine {
            path,
            line,
            description: description.into(),
        });
    }
}

fn finish_plan(plan: ScaffoldPlan, dry_run: Option<DryRunFormat>, force: bool) -> Result<()> {
    if let Some(format) = dry_run {
        match format {
            DryRunFormat::Text => print_plan(&plan),
            DryRunFormat::Json => println!("{}", serde_json::to_string_pretty(&plan)?),
        }
        return Ok(());
    }

    preflight(&plan, force)?;
    apply_plan(&plan, force)?;
    print_plan(&plan);
    Ok(())
}

fn print_plan(plan: &ScaffoldPlan) {
    for operation in &plan.operations {
        match operation {
            PlanOperation::WriteFile { path, .. } => println!("create {}", path.display()),
            PlanOperation::AppendLine { path, .. } => println!("update {}", path.display()),
        }
    }
    for warning in &plan.warnings {
        eprintln!("warning: {warning}");
    }
    if !plan.follow_up.is_empty() {
        println!("\nNext steps:");
        for step in &plan.follow_up {
            println!("- {step}");
        }
    }
}

fn preflight(plan: &ScaffoldPlan, force: bool) -> Result<()> {
    if force {
        return Ok(());
    }

    for operation in &plan.operations {
        if let PlanOperation::WriteFile { path, .. } = operation {
            if path.exists() {
                bail!(
                    "{} already exists (use --force to overwrite)",
                    path.display()
                );
            }
        }
    }
    Ok(())
}

fn apply_plan(plan: &ScaffoldPlan, force: bool) -> Result<()> {
    // Journal each file's prior state so a mid-plan failure rolls everything
    // back instead of leaving a half-written project.
    let mut journal: Vec<(PathBuf, Option<String>)> = Vec::new();
    let outcome = apply_operations(plan, force, &mut journal);
    if outcome.is_err() {
        rollback(&journal);
    }
    outcome
}

fn apply_operations(
    plan: &ScaffoldPlan,
    force: bool,
    journal: &mut Vec<(PathBuf, Option<String>)>,
) -> Result<()> {
    for operation in &plan.operations {
        match operation {
            PlanOperation::WriteFile { path, content, .. } => {
                if path.exists() && !force {
                    bail!(
                        "{} already exists (use --force to overwrite)",
                        path.display()
                    );
                }
                if let Some(parent) = path.parent() {
                    fs::create_dir_all(parent)
                        .with_context(|| format!("create {}", parent.display()))?;
                }
                journal.push((path.clone(), fs::read_to_string(path).ok()));
                fs::write(path, content).with_context(|| format!("write {}", path.display()))?;
            }
            PlanOperation::AppendLine { path, line, .. } => {
                if let Some(parent) = path.parent() {
                    fs::create_dir_all(parent)
                        .with_context(|| format!("create {}", parent.display()))?;
                }
                let mut current = fs::read_to_string(path).unwrap_or_default();
                if !current
                    .lines()
                    .any(|existing| existing.trim() == line.trim())
                {
                    journal.push((path.clone(), fs::read_to_string(path).ok()));
                    if !current.is_empty() && !current.ends_with('\n') {
                        current.push('\n');
                    }
                    current.push_str(line);
                    current.push('\n');
                    fs::write(path, current)
                        .with_context(|| format!("update {}", path.display()))?;
                }
            }
        }
    }
    Ok(())
}

fn rollback(journal: &[(PathBuf, Option<String>)]) {
    for (path, prior) in journal.iter().rev() {
        let restored = match prior {
            Some(content) => fs::write(path, content),
            None => fs::remove_file(path),
        };
        if restored.is_err() {
            eprintln!(
                "warning: could not roll back {} after a failed scaffold",
                path.display()
            );
        }
    }
}

struct ModuleTarget {
    leaf: String,
    file_path: PathBuf,
    mod_updates: Vec<(PathBuf, String)>,
}

impl ModuleTarget {
    fn new(root: &Path, bucket: &str, raw_name: &str) -> Result<Self> {
        let parts: Vec<String> = raw_name
            .split('/')
            .filter(|part| !part.is_empty())
            .map(snake_case)
            .collect();
        if parts.is_empty() {
            bail!("name cannot be empty");
        }
        let leaf = parts.last().cloned().unwrap();
        let base = root.join("src").join(bucket);
        let mut dir = base.clone();
        let mut mod_updates = Vec::new();

        for (index, part) in parts.iter().enumerate() {
            let mod_path = if index == 0 {
                base.join("mod.rs")
            } else {
                base.join(parts[..index].join("/")).join("mod.rs")
            };
            mod_updates.push((mod_path, format!("pub mod {part};")));
            if index + 1 < parts.len() {
                dir.push(part);
            }
        }

        Ok(Self {
            leaf,
            file_path: dir.join(format!("{}.rs", parts.last().unwrap())),
            mod_updates,
        })
    }

    fn plan_mod_updates(&self, plan: &mut ScaffoldPlan) {
        for (path, line) in &self.mod_updates {
            plan.append_line(path.clone(), line.clone(), "add module export");
        }
    }
}

pub fn resolve_root(root: Option<PathBuf>) -> Result<PathBuf> {
    if let Some(root) = root {
        return Ok(root);
    }

    let mut current = env::current_dir()?;
    loop {
        if current.join("Cargo.toml").exists() && current.join("src").exists() {
            return Ok(current);
        }
        if !current.pop() {
            bail!("could not find a Proa project root (pass --root)");
        }
    }
}

fn rust_files(root: &Path) -> Result<Vec<PathBuf>> {
    let mut files = Vec::new();
    collect_rust_files(root, &mut files)?;
    Ok(files)
}

fn collect_rust_files(root: &Path, files: &mut Vec<PathBuf>) -> Result<()> {
    for entry in fs::read_dir(root).with_context(|| format!("read {}", root.display()))? {
        let entry = entry?;
        let path = entry.path();
        if path.is_dir() {
            collect_rust_files(&path, files)?;
        } else if path.extension().and_then(|ext| ext.to_str()) == Some("rs") {
            files.push(path);
        }
    }
    Ok(())
}

fn route_path_for_page(rel: &Path) -> String {
    let mut parts: Vec<String> = rel
        .with_extension("")
        .components()
        .filter_map(|component| component.as_os_str().to_str().map(str::to_string))
        .collect();
    if parts.last().is_some_and(|part| part == "index") {
        parts.pop();
    }
    if parts.len() == 1 && parts[0] == "home" {
        return "/".to_string();
    }
    if parts.is_empty() {
        "/".to_string()
    } else {
        format!("/{}", parts.join("/").replace('_', "-"))
    }
}

fn display_rel(root: &Path, path: &Path) -> String {
    display_rel_path(root, path).display().to_string()
}

fn display_rel_path(root: &Path, path: &Path) -> PathBuf {
    path.strip_prefix(root).unwrap_or(path).to_path_buf()
}

fn package_name(value: &str) -> String {
    kebab_case(value)
}

fn snake_case(value: &str) -> String {
    normalize_case(value, '_')
}

fn kebab_case(value: &str) -> String {
    normalize_case(value, '-')
}

fn normalize_case(value: &str, separator: char) -> String {
    let mut out = String::new();
    let mut last_was_sep = false;
    for ch in value.chars() {
        if ch.is_ascii_alphanumeric() {
            out.push(ch.to_ascii_lowercase());
            last_was_sep = false;
        } else if !last_was_sep && !out.is_empty() {
            out.push(separator);
            last_was_sep = true;
        }
    }
    while out.ends_with(separator) {
        out.pop();
    }
    if out.is_empty() {
        out.push_str("generated");
    }
    if out.starts_with(|ch: char| ch.is_ascii_digit()) {
        out.insert(0, separator);
    }
    out
}

fn pascal_case(value: &str) -> String {
    let mut out = String::new();
    for part in value
        .split(|ch: char| !ch.is_ascii_alphanumeric())
        .filter(|part| !part.is_empty())
    {
        let mut chars = part.chars();
        if let Some(first) = chars.next() {
            out.push(first.to_ascii_uppercase());
            for ch in chars {
                out.push(ch.to_ascii_lowercase());
            }
        }
    }
    if out.is_empty() {
        out.push_str("Generated");
    }
    if out.starts_with(|ch: char| ch.is_ascii_digit()) {
        out.insert_str(0, "Generated");
    }
    out
}

fn title_case(value: &str) -> String {
    value
        .split(['-', '_'])
        .filter(|part| !part.is_empty())
        .map(|part| {
            let mut chars = part.chars();
            match chars.next() {
                Some(first) => format!("{}{}", first.to_ascii_uppercase(), chars.as_str()),
                None => String::new(),
            }
        })
        .collect::<Vec<_>>()
        .join(" ")
}

fn fill(mut template: String, replacements: &[(&str, &str)]) -> String {
    for (needle, value) in replacements {
        template = template.replace(needle, value);
    }
    template
}

fn cargo_toml(project_name: &str, options: &NewSiteOptions) -> String {
    // Mirrors `new_site`: font wiring is tied to the generated Axum crate
    // root, since the include! boilerplate lives in src/main.rs.
    let has_fonts = !options.empty && options.server == ServerIntegration::Axum;
    let mut deps = vec![
        "proa_core = \"0.3.0\"".to_string(),
        "proa_macros = \"0.3.0\"".to_string(),
    ];
    if has_fonts {
        deps.extend([
            "proa_fonts = \"0.1.0\"".to_string(),
            "proa_fonts_macros = \"0.1.0\"".to_string(),
        ]);
    }
    if options.server == ServerIntegration::Axum {
        deps.extend([
            "proa_framework_axum = \"0.3.0\"".to_string(),
            "anyhow = \"1\"".to_string(),
            "axum = \"0.7\"".to_string(),
            "tokio = { version = \"1\", features = [\"macros\", \"rt-multi-thread\"] }".to_string(),
            "tower = \"0.5\"".to_string(),
            "tower-http = { version = \"0.6\", features = [\"fs\", \"set-header\", \"trace\"] }"
                .to_string(),
            "tracing = \"0.1\"".to_string(),
            "tracing-subscriber = \"0.3\"".to_string(),
        ]);
    }
    if options.docs == DocsIntegration::ProaDocs || options.blog {
        deps.push("proa_docs = { version = \"0.2.0\", features = [\"axum\"] }".to_string());
    }

    let has_docs_build = options.docs == DocsIntegration::ProaDocs || options.blog;
    let mut build_deps = String::new();
    if has_fonts || has_docs_build {
        build_deps.push_str("\n[build-dependencies]\n");
        if has_fonts {
            build_deps.push_str("proa_fonts_build = \"0.1.0\"\n");
        }
        if has_docs_build {
            build_deps.push_str("proa_docs_build = \"0.2.0\"\n");
        }
    }

    format!(
        "[package]\nname = \"{project_name}\"\nversion = \"0.1.0\"\nedition = \"2021\"\n\n[dependencies]\n{}\n{build_deps}\n[profile.release]\nlto = \"thin\"\ncodegen-units = 1\nstrip = true\n",
        deps.join("\n")
    )
}

fn cargo_config_toml() -> String {
    "# Fetch Git dependencies using your Git credential helpers and proxy settings.\n[net]\ngit-fetch-with-cli = true\n".to_string()
}

fn dev_script() -> String {
    include_str!("../templates/dev.sh").to_string()
}

fn build_script() -> String {
    include_str!("../templates/build.sh").to_string()
}

fn watch_lock_script() -> String {
    include_str!("../templates/watch-lock.sh").to_string()
}

fn readme(project_name: &str, deploy: DeployTarget) -> String {
    let deploy_section = if deploy == DeployTarget::Coolify {
        "\n## Deploy\n\nThis project includes a Coolify deployment preset. Read `COOLIFY.md`, then deploy with the generated `Dockerfile`.\n"
    } else {
        ""
    };
    format!(
        "# {}\n\nGenerated with `proa new site`.\n\nDevelop with a watching server:\n\n```bash\nbash scripts/dev.sh\n```\n\nBuild the release binary:\n\n```bash\nbash scripts/build.sh\n```\n{}",
        title_case(project_name),
        deploy_section
    )
}

fn gitignore() -> String {
    "/target\n/public/styles.css\n.env\n".to_string()
}

fn agents_md() -> String {
    "# Agent instructions\n\nFollow the Proa site structure: reusable render components in `src/components/`, route surfaces in `src/pages/`, shared shells in `src/layouts/`, and route/nav data in `src/data/`.\n\nAuthor ordinary synchronous components with explicit `impl<L: DataLoader> WebRenderSync<L>` blocks. Their `fn render(self, cx: &mut WebContext<L>) -> Result<(), WriteError>` implementations should end in `html_sync! { ... }.render(cx)`. Add `C: WebRenderSync<L>` for generic child renderables. `WebContext<L>` owns the default `RenderBuffer`; only lower-level custom-writer integrations need the second `B` parameter. When a component actually awaits, implement the canonical `WebRender<L>` signature directly and return `RenderOutcome::pending(async move { ... })`; bridge synchronous children only at that async composition boundary.\n\nOpaque SSR components use no RSJS attribute. Put `#[rsjs(component)]` on a real render-trait impl only when it must participate in analyzed composition, and use `#[rsjs(component, client)]` when that component originates signals, event handlers, queries, mutations, shared-state acquisition, node refs, or browser work. Do not use inherent render shorthand, `async fn render` sugar, or component `clients(...)` allowlists; annotate each portable browser helper once with `#[rsjs::client]`.\n\nInside an analyzed component, a direct inbound read such as `self.label` is eligible to remain live when a parent supplies a live value. A `signal(self.value)` initializer samples that inbound value once for the child-owned signal. Use `rsjs::server_only(...)` or `rsjs::server_attr(...)` only when the contract intentionally takes a per-render server snapshot.\n\nRun `proa site check` after structural changes.\n".to_string()
}

fn components_mod() -> String {
    "// Reusable Proa render components live here.\n".to_string()
}

fn pages_mod(has_docs: bool, has_blog: bool) -> String {
    let mut out = "pub mod home;\n".to_string();
    if has_docs {
        out.push_str("pub mod docs;\n");
    }
    if has_blog {
        out.push_str("pub mod blog;\n");
    }
    out
}

fn routes_data(has_docs: bool, has_blog: bool) -> String {
    let mut routes = vec!["\"/\""];
    if has_docs {
        routes.push("\"/docs\"");
        routes.push("\"/docs/*path\"");
    }
    if has_blog {
        routes.push("\"/blog\"");
        routes.push("\"/blog/*path\"");
    }
    format!("pub const ROUTES: &[&str] = &[{}];\n", routes.join(", "))
}

fn nav_data() -> String {
    "pub struct NavItem {\n    pub label: &'static str,\n    pub href: &'static str,\n}\n\npub const NAV_ITEMS: &[NavItem] = &[\n    NavItem { label: \"Home\", href: \"/\" },\n];\n".to_string()
}

fn root_layout() -> String {
    r#"use crate::data::nav::NAV_ITEMS;
use proa_core::{raw_text_ok, DataLoader, WebContext, WebRenderSync, WriteError};
use proa_macros::html_sync;

pub struct PageMeta {
    pub title: &'static str,
    pub description: &'static str,
    pub path: &'static str,
}

pub struct RootDocument<C> {
    pub meta: PageMeta,
    pub content: C,
}

struct SiteNav;

impl<L: DataLoader> WebRenderSync<L> for SiteNav {
    fn render(self, cx: &mut WebContext<L>) -> Result<(), WriteError> {
        html_sync! {
            {for item in NAV_ITEMS {
                html_sync! {
                    <a class="text-sm text-zinc-600 hover:text-zinc-900" href={item.href}>
                        {item.label}
                    </a>
                }
            }}
        }
        .render(cx)
    }
}

impl<L, C> WebRenderSync<L> for RootDocument<C>
where
    L: DataLoader,
    C: WebRenderSync<L>,
{
    fn render(self, cx: &mut WebContext<L>) -> Result<(), WriteError> {
        html_sync! {
            <!DOCTYPE html>
            <html lang="en">
                <head>
                    <meta charset="utf-8" />
                    <meta name="viewport" content="width=device-width, initial-scale=1" />
                    <title>{raw_text_ok(self.meta.title)}</title>
                    <meta name="description" content={self.meta.description} />
                    <meta name="proa-route" content={self.meta.path} />
                    <link rel="icon" href="/static/favicon.svg" type="image/svg+xml" />
                    <link rel="stylesheet" href="/static/styles.css" />
                </head>
                <body>
                    <header class="mx-auto max-w-4xl px-6 pt-8">
                        <nav class="flex items-center gap-6">{SiteNav}</nav>
                    </header>
                    {self.content}
                </body>
            </html>
        }
        .render(cx)
    }
}
"#
    .to_string()
}

// proa-perf-ok(scope): trusted scaffold emitter assembles Rust source, not response HTML
fn home_page(title: &str, axum: bool) -> String {
    let response = if axum {
        "use axum::response::Html;\n\npub async fn handler() -> Html<String> {\n    // proa-perf-ok: top-level Axum response finalization owns the HTML String\n    Html(render())\n}\n\n"
    } else {
        ""
    };

    fill(
        format!(
            r#"{}use proa_core::{{
    render_to_string, DataLoader, WebContext, WebRenderSync, WriteError,
}};
use proa_macros::html_sync;

use crate::layouts::root::{{PageMeta, RootDocument}};

pub fn render() -> String {{
    // proa-perf-ok: generated scaffold owns the top-level HTML String at the HTTP boundary
    render_to_string(RootDocument {{
        meta: PageMeta {{
            title: "__TITLE__",
            description: "A Proa site.",
            path: "/",
        }},
        content: HomePage,
    }})
}}

struct HomePage;

impl<L: DataLoader> WebRenderSync<L> for HomePage {{
    fn render(self, cx: &mut WebContext<L>) -> Result<(), WriteError> {{
        html_sync! {{
            <main class="mx-auto max-w-4xl px-6 py-16">
                <p class="font-mono text-sm uppercase tracking-[0.18em] text-zinc-500">"Proa"</p>
                <h1 class="mt-4 text-5xl font-semibold tracking-[-0.04em]">"__TITLE__"</h1>
                <p class="mt-4 text-lg leading-8 text-zinc-600">
                    "Edit src/pages/home.rs to start building."
                </p>
            </main>
        }}
        .render(cx)
    }}
}}

#[cfg(test)]
mod tests {{
    #[test]
    fn home_page_renders() {{
        let html = super::render();
        assert!(html.starts_with("<!DOCTYPE html>"));
        assert!(html.contains("__TITLE__"));
        assert!(crate::data::routes::ROUTES.contains(&"/"));
    }}
}}
"#,
            response
        ),
        &[("__TITLE__", title)],
    )
}

fn docs_page(axum: bool) -> String {
    docs_like_page("DOCS", "docs", "/docs", axum)
}

fn blog_page(axum: bool) -> String {
    docs_like_page("BLOG", "blog", "/blog", axum)
}

fn docs_like_page(const_name: &str, dir: &str, base_path: &str, axum: bool) -> String {
    let template = if axum {
        r#"use axum::{extract::Path, response::Response};
use proa_docs::Docs;

static __CONST__: Docs = proa_docs::include_docs!("__DIR__");

pub async fn handler() -> Response {
    proa_docs::adapter::axum::response(&__CONST__, "__BASE__")
}

pub async fn path_handler(Path(path): Path<String>) -> Response {
    let path = if path.trim_matches('/').is_empty() {
        "__BASE__".to_string()
    } else {
        format!("__BASE__/{}", path.trim_matches('/'))
    };
    proa_docs::adapter::axum::response(&__CONST__, &path)
}
"#
    } else {
        r#"use proa_docs::{Docs, DocsResponse};

static __CONST__: Docs = proa_docs::include_docs!("__DIR__");

pub fn render(path: &str) -> DocsResponse {
    proa_docs::render(&__CONST__, path)
}
"#
    };
    fill(
        template.to_string(),
        &[
            ("__CONST__", const_name),
            ("__DIR__", dir),
            ("__BASE__", base_path),
        ],
    )
}

fn main_rs(has_fonts: bool, has_docs: bool, has_blog: bool) -> String {
    let fonts_mod = if has_fonts {
        "\n// Generated font consts for google_font!/local_font! declarations.\nmod __proa_fonts_generated { include!(concat!(env!(\"OUT_DIR\"), \"/proa_fonts.rs\")); }\n"
    } else {
        ""
    };
    let fonts_route = if has_fonts {
        "        .nest_service(\"/fonts\", static_assets::fonts_service())\n"
    } else {
        ""
    };
    let mut routes = "        .route(\"/\", get(pages::home::handler))\n        .route(\"/healthz\", get(healthz))\n".to_string();
    if has_docs {
        routes.push_str("        .route(\"/docs\", get(pages::docs::handler))\n");
        routes.push_str("        .route(\"/docs/*path\", get(pages::docs::path_handler))\n");
    }
    if has_blog {
        routes.push_str("        .route(\"/blog\", get(pages::blog::handler))\n");
        routes.push_str("        .route(\"/blog/*path\", get(pages::blog::path_handler))\n");
    }

    fill(
        r#"use axum::{
    http::header::{HeaderValue, REFERRER_POLICY, X_CONTENT_TYPE_OPTIONS, X_FRAME_OPTIONS},
    routing::get,
    Router,
};
use std::time::Duration;
use tower_http::{set_header::SetResponseHeaderLayer, trace::TraceLayer};

mod components;
mod data;
mod layouts;
mod pages;
mod static_assets;
__FONTS_MOD__
const SHUTDOWN_DRAIN_TIMEOUT: Duration = Duration::from_secs(30);

async fn healthz() -> &'static str {
    "ok"
}

async fn robots_txt() -> &'static str {
    "User-agent: *\nAllow: /\n"
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    tracing_subscriber::fmt().init();
    let host = std::env::var("HOST").unwrap_or_else(|_| "127.0.0.1".to_string());
    let port = std::env::var("PORT")
        .ok()
        .and_then(|port| port.parse::<u16>().ok())
        .unwrap_or(3000);

    let app = Router::new()
__ROUTES__        .route("/robots.txt", get(robots_txt))
__FONTS_ROUTE__        .nest_service("/static", static_assets::service())
        .layer(TraceLayer::new_for_http())
        .layer(SetResponseHeaderLayer::if_not_present(
            X_CONTENT_TYPE_OPTIONS,
            HeaderValue::from_static("nosniff"),
        ))
        .layer(SetResponseHeaderLayer::if_not_present(
            X_FRAME_OPTIONS,
            HeaderValue::from_static("DENY"),
        ))
        .layer(SetResponseHeaderLayer::if_not_present(
            REFERRER_POLICY,
            HeaderValue::from_static("strict-origin-when-cross-origin"),
        ));

    let listener = tokio::net::TcpListener::bind((host.as_str(), port)).await?;
    tracing::info!("listening on http://{}", listener.local_addr()?);
    tracing::info!(routes = ?data::routes::ROUTES, "registered page routes");
    proa_framework_axum::serve(listener, app, SHUTDOWN_DRAIN_TIMEOUT).await?;
    Ok(())
}
"#
        .to_string(),
        &[
            ("__FONTS_MOD__", fonts_mod),
            ("__FONTS_ROUTE__", fonts_route),
            ("__ROUTES__", &routes),
        ],
    )
}

fn static_assets_rs(has_fonts: bool) -> String {
    let fonts_service = if has_fonts {
        r#"
/// Vendored font files are content-hashed, so they can be cached forever.
pub fn fonts_service() -> Router {
    serve_dir(
        static_root().join("fonts"),
        "public, max-age=31536000, immutable",
    )
}
"#
    } else {
        ""
    };
    fill(
        r#"use std::{env, path::PathBuf};

use axum::{
    http::header::{HeaderValue, CACHE_CONTROL, X_CONTENT_TYPE_OPTIONS},
    Router,
};
use tower::ServiceBuilder;
use tower_http::{services::ServeDir, set_header::SetResponseHeaderLayer};

fn static_root() -> PathBuf {
    env::var_os("STATIC_DIR")
        .map(PathBuf::from)
        .unwrap_or_else(|| PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("public"))
}

fn serve_dir(dir: PathBuf, cache_control: &'static str) -> Router {
    Router::new().nest_service(
        "/",
        ServiceBuilder::new()
            .layer(SetResponseHeaderLayer::if_not_present(
                CACHE_CONTROL,
                HeaderValue::from_static(cache_control),
            ))
            .layer(SetResponseHeaderLayer::if_not_present(
                X_CONTENT_TYPE_OPTIONS,
                HeaderValue::from_static("nosniff"),
            ))
            .service(ServeDir::new(dir)),
    )
}

pub fn service() -> Router {
    // Debug builds revalidate on every load so `proa dev` asset rebuilds show
    // up on refresh; release builds keep the shared one-hour cache.
    let cache_control = if cfg!(debug_assertions) {
        "no-cache"
    } else {
        "public, max-age=3600"
    };
    serve_dir(static_root(), cache_control)
}
__FONTS_SERVICE__"#
            .to_string(),
        &[("__FONTS_SERVICE__", fonts_service)],
    )
}

fn dockerfile(project_name: &str) -> String {
    format!(
        r#"# syntax=docker/dockerfile:1.7

FROM rust:1.93-bookworm AS build
WORKDIR /app

COPY . .

# Public Proa dependencies resolve from crates.io without credentials.
RUN cargo build --release --bin {project_name}

FROM debian:bookworm-slim AS runtime
WORKDIR /app
RUN apt-get update \
    && apt-get install -y --no-install-recommends ca-certificates \
    && rm -rf /var/lib/apt/lists/*

COPY --from=build /app/target/release/{project_name} /usr/local/bin/{project_name}
COPY --from=build /app/public /app/public

ENV HOST=0.0.0.0
ENV PORT=3000
ENV STATIC_DIR=/app/public
ENV RUST_LOG=info

EXPOSE 3000
CMD ["{project_name}"]
"#
    )
}

fn dockerignore() -> String {
    r#".git
target
.env
.env.*
!.env.example
node_modules
.DS_Store
"#
    .to_string()
}

fn coolify_env_example() -> String {
    r#"# Runtime variables for Coolify.
HOST=0.0.0.0
PORT=3000
STATIC_DIR=/app/public
RUST_LOG=info

"#
    .to_string()
}

fn coolify_readme(project_name: &str) -> String {
    format!(
        r#"# Coolify deployment

This Proa app is ready for Coolify's Dockerfile deployment path.

## Local image check

```bash
docker build -t {project_name} .

docker run --rm -p 3000:3000 -e PORT=3000 {project_name}
curl -fsS http://127.0.0.1:3000/healthz
```

## Coolify settings

- Deployment type: Dockerfile
- Exposed port: `3000`
- Health check path: `/healthz`
- Runtime variables: copy from `coolify.env.example`

Proa dependencies resolve from crates.io at build time. No registry or GitHub
credentials are required.

## Pull request previews

Coolify can create pull request preview deployments with unique URLs. Use the
GitHub App integration, add a wildcard DNS record such as
`*.preview.example.com`, then enable Preview Deployments with a URL template
such as `{{{{pr_id}}}}.preview.example.com`.
"#
    )
}

fn build_rs(has_fonts: bool, has_docs: bool, has_blog: bool) -> String {
    let has_docs_build = has_docs || has_blog;
    let mut out = String::new();
    if has_docs_build {
        out.push_str("use std::{env, path::PathBuf};\n\n");
    }
    out.push_str("fn main() {\n");
    if has_fonts {
        out.push_str(
            r#"    println!("cargo:rerun-if-changed=src");
    println!("cargo:rerun-if-changed=proa-fonts.lock");
    let fonts = proa_fonts_build::collect::CollectOptions::from_build_env()
        .expect("cargo build env");
    if let Err(error) = proa_fonts_build::collect::run(&fonts) {
        panic!("proa fonts: {error}");
    }
"#,
        );
    }
    if has_docs_build {
        if has_fonts {
            out.push('\n');
        }
        out.push_str(
            r#"    let manifest_dir = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").expect("manifest dir"));
    println!("cargo:rerun-if-changed=build.rs");
    println!("cargo:rerun-if-changed={}", manifest_dir.join("src/docs").display());
    println!("cargo:rerun-if-changed={}", manifest_dir.join("src/blog").display());

    let config = proa_docs_build::Config::new()
"#,
        );
        if has_docs {
            out.push_str(
                r#"        .source(
            proa_docs_build::DocsSource::new(manifest_dir.join("src/docs"))
                .out_dir_name("docs")
                .base_path("/docs")
                .site_name("Docs"),
        )
"#,
            );
        }
        if has_blog {
            out.push_str(
                r#"        .source(
            proa_docs_build::DocsSource::new(manifest_dir.join("src/blog"))
                .out_dir_name("blog")
                .base_path("/blog")
                .site_name("Blog"),
        )
"#,
            );
        }
        out.push_str(";\n\n    proa_docs_build::compile(config);\n");
    }
    out.push_str("}\n");
    out
}

fn docs_index() -> String {
    "---\ntitle: Introduction\ndescription: Start here.\nnav_order: 1\n---\n\n# Introduction\n\nWelcome to your Proa docs.\n".to_string()
}

fn docs_getting_started() -> String {
    "---\ntitle: Getting Started\ndescription: Your first Proa page.\nnav_order: 2\n---\n\n# Getting Started\n\nEdit `src/docs/getting-started.mdx`.\n".to_string()
}

fn blog_first_post() -> String {
    "---\ntitle: First Post\ndescription: Your first Proa blog post.\n---\n\n# First Post\n\nEdit `src/blog/first-post.mdx`.\n".to_string()
}

fn tailwind_input_css() -> String {
    // The layer declaration has to precede `@import "tailwindcss"`. Tailwind
    // otherwise declares `@layer theme, base, components, utilities` itself,
    // whichever declaration the browser sees first wins, and `proa-compat`
    // lands last — where a component stylesheet's hand-written colour
    // fallbacks outrank the real generated utilities and buttons come out
    // without their padding or radius.
    //
    // `proa-compat` is the layer Proa UI's runtime CSS wraps those fallbacks
    // in. Declaring it here is inert for a site that never installs a
    // component, and correct in advance for one that does.
    "@layer theme, base, proa-compat, components, utilities;\n\n\
     @import \"tailwindcss\";\n\n@source \"../src/**/*.rs\";\n\nbody {\n  margin: 0;\n}\n"
        .to_string()
}

fn generated_styles_placeholder() -> String {
    "/* Generated CSS placeholder. Run the Tailwind build command from the CLI output. */\n"
        .to_string()
}

fn plain_css() -> String {
    ":root {\n  color: #18181b;\n  background: #ffffff;\n}\n\n* {\n  box-sizing: border-box;\n}\n\nbody {\n  margin: 0;\n  font-family: system-ui, sans-serif;\n}\n".to_string()
}

fn favicon_svg() -> String {
    r##"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64"><path d="M13 36.5C17.6 25.7 25.5 18 36.2 13L34.8 33.5L13 36.5Z" fill="#dd7627"/><path d="M10 39.5C16.2 39.8 23.4 39.2 31.3 37.9C41.2 36.3 49 33.7 54 30.2C50.9 39.8 42.3 46.5 30 47.8C18.7 49 11.5 46 10 39.5Z" fill="#18181b"/></svg>
"##
    .to_string()
}

fn component_source(
    struct_name: &str,
    data_attr: &str,
    children: bool,
    asynchronous: bool,
    markdown: bool,
    recipe: bool,
    test: bool,
) -> String {
    let base = if recipe {
        "use super::recipe::BASE_CLASS;\n"
    } else {
        "const BASE_CLASS: &str = \"rounded-lg border p-4\";\n"
    };
    let template = match (children, asynchronous) {
        (true, false) => {
            r#"use proa_core::{ClassList, DataLoader, WebContext, WebRenderSync, WriteError};
use proa_macros::html_sync;

__BASE__
pub struct __STRUCT__<C = ()> {
    pub class: Option<&'static str>,
    pub children: C,
}

impl<C> __STRUCT__<C> {
    pub const fn new(children: C) -> Self {
        Self { class: None, children }
    }

    pub const fn class(mut self, class: &'static str) -> Self {
        self.class = Some(class);
        self
    }
}

impl<L, C> WebRenderSync<L> for __STRUCT__<C>
where
    L: DataLoader,
    C: WebRenderSync<L>,
{
    fn render(self, cx: &mut WebContext<L>) -> Result<(), WriteError> {
        let classes = ClassList::<2>::new().add(BASE_CLASS).add_opt(self.class);
        html_sync! {
            <div __DATA__ class={classes}>{self.children}</div>
        }
        .render(cx)
    }
}
"#
        }
        (false, false) => {
            r#"use proa_core::{ClassList, DataLoader, WebContext, WebRenderSync, WriteError};
use proa_macros::html_sync;

__BASE__
#[derive(Debug, Clone, Copy, Default)]
pub struct __STRUCT__ {
    pub class: Option<&'static str>,
}

impl __STRUCT__ {
    pub const fn new() -> Self {
        Self { class: None }
    }

    pub const fn class(mut self, class: &'static str) -> Self {
        self.class = Some(class);
        self
    }
}

impl<L: DataLoader> WebRenderSync<L> for __STRUCT__ {
    fn render(self, cx: &mut WebContext<L>) -> Result<(), WriteError> {
        let classes = ClassList::<2>::new().add(BASE_CLASS).add_opt(self.class);
        html_sync! {
            <div __DATA__ class={classes}>"__STRUCT__"</div>
        }
        .render(cx)
    }
}
"#
        }
        (true, true) => {
            r#"use std::future::Future;

use proa_core::{ClassList, DataLoader, RenderOutcome, WebContext, WebRender, WriteError};
use proa_macros::html;

__BASE__
pub struct __STRUCT__<C = ()> {
    pub class: Option<&'static str>,
    pub children: C,
}

impl<C> __STRUCT__<C> {
    pub const fn new(children: C) -> Self {
        Self { class: None, children }
    }

    pub const fn class(mut self, class: &'static str) -> Self {
        self.class = Some(class);
        self
    }
}

impl<L, C> WebRender<L> for __STRUCT__<C>
where
    L: DataLoader,
    C: WebRender<L> + Send + 'static,
{
    fn render(self, cx: &mut WebContext<L>) -> RenderOutcome<impl Future<Output = Result<(), WriteError>>> {
        RenderOutcome::pending(async move {
            let classes = ClassList::<2>::new().add(BASE_CLASS).add_opt(self.class);
            html! {
                <div __DATA__ class={classes}>{self.children}</div>
            }
            .render(cx)
            .resolve()
            .await
        })
    }
}
"#
        }
        (false, true) => {
            r#"use std::future::Future;

use proa_core::{ClassList, DataLoader, RenderOutcome, WebContext, WebRender, WriteError};
use proa_macros::html;

__BASE__
#[derive(Debug, Clone, Copy, Default)]
pub struct __STRUCT__ {
    pub class: Option<&'static str>,
}

impl __STRUCT__ {
    pub const fn new() -> Self {
        Self { class: None }
    }

    pub const fn class(mut self, class: &'static str) -> Self {
        self.class = Some(class);
        self
    }
}

impl<L: DataLoader> WebRender<L> for __STRUCT__ {
    fn render(self, cx: &mut WebContext<L>) -> RenderOutcome<impl Future<Output = Result<(), WriteError>>> {
        RenderOutcome::pending(async move {
            let classes = ClassList::<2>::new().add(BASE_CLASS).add_opt(self.class);
            let content = proa_core::await_component(async { proa_core::text::text("__STRUCT__") });
            html! {
                <div __DATA__ class={classes}>{content}</div>
            }
            .render(cx)
            .resolve()
            .await
        })
    }
}
"#
        }
    };

    let markdown_source = if markdown {
        if children {
            r####"
use proa_core::{MdRenderSync, WriteBuf};
use proa_macros::md_sync;

impl<L: proa_core::DataLoader, B, C> MdRenderSync<L, B> for __STRUCT__<C>
where
    B: WriteBuf,
    C: MdRenderSync<L, B>,
{
    fn render_md(self, cx: &mut WebContext<L, B>) -> Result<(), WriteError> {
        md_sync! { r###"
### __STRUCT__

{self.children}
"###
        }
        .render_md(cx)
    }
}
"####
        } else {
            r####"
use proa_core::{MdRenderSync, WriteBuf};
use proa_macros::md_sync;

impl<L: proa_core::DataLoader, B: WriteBuf> MdRenderSync<L, B> for __STRUCT__ {
    fn render_md(self, cx: &mut WebContext<L, B>) -> Result<(), WriteError> {
        md_sync! { r###"__STRUCT__"### }.render_md(cx)
    }
}
"####
        }
    } else {
        ""
    };

    let test_source = if test {
        match (children, asynchronous) {
            (true, false) => {
                r#"
#[cfg(test)]
mod tests {
    use super::*;
    use proa_core::{text::text, WebContext, WebRenderSync};

    #[test]
    fn renders_dynamic_children_and_the_component_contract() {
        let mut cx = WebContext::new();
        __STRUCT__::new(text("dynamic child")).render(&mut cx).unwrap();
        let html = cx.into_output().into_string();
        assert!(html.contains("__DATA__"));
        assert!(html.contains("dynamic child"));
    }
}
"#
            }
            (false, false) => {
                r#"
#[cfg(test)]
mod tests {
    use super::*;
    use proa_core::{WebContext, WebRenderSync};

    #[test]
    fn renders_the_component_contract() {
        let mut cx = WebContext::new();
        __STRUCT__::new().render(&mut cx).unwrap();
        let html = cx.into_output().into_string();
        assert!(html.contains("__DATA__"));
    }
}
"#
            }
            (true, true) => {
                r#"
#[cfg(test)]
mod tests {
    use super::*;
    use proa_core::{text::text, WebContext, WebRender};

    #[tokio::test]
    async fn renders_dynamic_children_and_the_component_contract() {
        let mut cx = WebContext::new();
        __STRUCT__::new(proa_core::to_async(text("dynamic child")))
            .render(&mut cx)
            .resolve()
            .await
            .unwrap();
        let html = cx.into_output().into_string();
        assert!(html.contains("__DATA__"));
        assert!(html.contains("dynamic child"));
    }
}
"#
            }
            (false, true) => {
                r#"
#[cfg(test)]
mod tests {
    use super::*;
    use proa_core::{WebContext, WebRender};

    #[tokio::test]
    async fn renders_the_component_contract() {
        let mut cx = WebContext::new();
        __STRUCT__::new()
            .render(&mut cx)
            .resolve()
            .await
            .unwrap();
        let html = cx.into_output().into_string();
        assert!(html.contains("__DATA__"));
    }
}
"#
            }
        }
    } else {
        ""
    };

    fill(
        format!("{template}{markdown_source}{test_source}"),
        &[
            ("__BASE__", base),
            ("__STRUCT__", struct_name),
            ("__DATA__", data_attr),
        ],
    )
}

fn component_recipe_source(struct_name: &str) -> String {
    fill(
        r#"pub const BASE_CLASS: &str = "rounded-lg border p-4";

pub struct __STRUCT__Recipe {
    pub class: &'static str,
}

impl Default for __STRUCT__Recipe {
    fn default() -> Self {
        Self { class: BASE_CLASS }
    }
}
"#
        .to_string(),
        &[("__STRUCT__", struct_name)],
    )
}

fn section_layout_source(struct_name: &str) -> String {
    fill(
        r#"use proa_core::{DataLoader, WebContext, WebRenderSync, WriteError};
use proa_macros::html_sync;

pub struct __STRUCT__<C> {
    pub content: C,
}

impl<C> __STRUCT__<C> {
    pub const fn new(content: C) -> Self {
        Self { content }
    }
}

impl<L, C> WebRenderSync<L> for __STRUCT__<C>
where
    L: DataLoader,
    C: WebRenderSync<L>,
{
    fn render(self, cx: &mut WebContext<L>) -> Result<(), WriteError> {
        html_sync! {
            <section class="mx-auto max-w-5xl px-6 py-12">
                {self.content}
            </section>
        }
        .render(cx)
    }
}
"#
        .to_string(),
        &[("__STRUCT__", struct_name)],
    )
}

fn document_layout_source(struct_name: &str) -> String {
    fill(
        r#"// proa:document-root
use proa_core::{raw_text_ok, DataLoader, WebContext, WebRenderSync, WriteError};
use proa_macros::html_sync;

pub struct __STRUCT__<C> {
    pub title: &'static str,
    pub description: &'static str,
    pub content: C,
}

impl<L, C> WebRenderSync<L> for __STRUCT__<C>
where
    L: DataLoader,
    C: WebRenderSync<L>,
{
    fn render(self, cx: &mut WebContext<L>) -> Result<(), WriteError> {
        html_sync! {
            <!DOCTYPE html>
            <html lang="en">
                <head>
                    <meta charset="utf-8" />
                    <meta name="viewport" content="width=device-width, initial-scale=1" />
                    <title>{raw_text_ok(self.title)}</title>
                    <meta name="description" content={self.description} />
                </head>
                <body>{self.content}</body>
            </html>
        }
        .render(cx)
    }
}
"#
        .to_string(),
        &[("__STRUCT__", struct_name)],
    )
}

fn data_source(name: &str) -> String {
    if name == "routes" {
        "pub const ROUTES: &[&str] = &[\"/\"];\n".to_string()
    } else if name == "nav" {
        nav_data()
    } else {
        format!(
            "pub struct {}Item {{\n    pub label: &'static str,\n}}\n\npub const ITEMS: &[{}Item] = &[];\n",
            pascal_case(name),
            pascal_case(name)
        )
    }
}

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

    #[test]
    fn new_site_dry_run_writes_nothing() {
        let temp = tempfile::tempdir().unwrap();
        let root = temp.path().join("demo");
        new_site(NewSiteOptions {
            name: "demo".to_string(),
            root: root.clone(),
            template: "basic".to_string(),
            empty: false,
            server: ServerIntegration::Axum,
            tailwind: true,
            docs: DocsIntegration::None,
            blog: false,
            deploy: DeployTarget::None,
            skip_install: true,
            no_git: true,
            agents_md: true,
            dry_run: Some(DryRunFormat::Text),
            force: false,
            yes: true,
        })
        .unwrap();

        assert!(!root.exists());
    }

    #[test]
    fn new_site_writes_tailwind_sources_under_styles() {
        let temp = tempfile::tempdir().unwrap();
        let root = temp.path().join("styles-demo");
        new_site(NewSiteOptions {
            name: "styles-demo".to_string(),
            root: root.clone(),
            template: "basic".to_string(),
            empty: false,
            server: ServerIntegration::Axum,
            tailwind: true,
            docs: DocsIntegration::None,
            blog: false,
            deploy: DeployTarget::None,
            skip_install: true,
            no_git: true,
            agents_md: true,
            dry_run: None,
            force: false,
            yes: true,
        })
        .unwrap();

        assert!(root.join("styles/input.css").exists());
        assert!(!root.join("styles/tailwind.config.cjs").exists());
        assert!(root.join("public/styles.css").exists());
        assert!(!root.join("frontend").exists());

        // Layer order is load-bearing and fails silently: get it wrong and the
        // site still builds, but installed components render without their
        // padding or radius. Assert the declaration exists and, critically,
        // that it comes before the Tailwind import.
        let input = fs::read_to_string(root.join("styles/input.css")).unwrap();
        let layers = input
            .find("@layer theme, base, proa-compat, components, utilities;")
            .expect("input.css must declare the cascade layer order");
        let tailwind = input
            .find("@import \"tailwindcss\";")
            .expect("input.css must import Tailwind");
        assert!(
            layers < tailwind,
            "the layer declaration must precede the Tailwind import, or Tailwind's own \
             declaration wins and `proa-compat` is appended last"
        );
    }

    #[test]
    fn apply_plan_rolls_back_files_on_mid_plan_failure() {
        let temp = tempfile::tempdir().unwrap();
        let root = temp.path().to_path_buf();
        let mut plan = ScaffoldPlan::new(root.clone());
        plan.write_file(root.join("ok.txt"), "hello".to_string());
        // Writing over a directory fails, aborting the plan midway.
        let blocked = root.join("blocked");
        fs::create_dir_all(&blocked).unwrap();
        plan.write_file(blocked.clone(), "nope".to_string());

        apply_plan(&plan, true).unwrap_err();
        assert!(
            !root.join("ok.txt").exists(),
            "files from the failed plan should be rolled back"
        );
    }

    #[test]
    fn apply_plan_rollback_restores_overwritten_files() {
        let temp = tempfile::tempdir().unwrap();
        let root = temp.path().to_path_buf();
        fs::write(root.join("keep.txt"), "original").unwrap();
        let mut plan = ScaffoldPlan::new(root.clone());
        plan.write_file(root.join("keep.txt"), "replaced".to_string());
        let blocked = root.join("blocked");
        fs::create_dir_all(&blocked).unwrap();
        plan.write_file(blocked.clone(), "nope".to_string());

        apply_plan(&plan, true).unwrap_err();
        assert_eq!(
            fs::read_to_string(root.join("keep.txt")).unwrap(),
            "original",
            "overwritten files should be restored on rollback"
        );
    }

    #[test]
    fn generate_component_updates_mod_file() {
        let temp = tempfile::tempdir().unwrap();
        fs::create_dir_all(temp.path().join("src/components")).unwrap();
        fs::write(temp.path().join("Cargo.toml"), "[package]\nname=\"demo\"\n").unwrap();
        fs::write(temp.path().join("src/components/mod.rs"), "").unwrap();

        generate_component(GenerateComponentOptions {
            name: "marketing-card".to_string(),
            root: Some(temp.path().to_path_buf()),
            folder: false,
            recipe: false,
            children: false,
            asynchronous: false,
            markdown: false,
            no_test: true,
            dry_run: None,
            force: false,
        })
        .unwrap();

        let module = fs::read_to_string(temp.path().join("src/components/mod.rs")).unwrap();
        assert!(module.contains("pub mod marketing_card;"));
        assert!(temp
            .path()
            .join("src/components/marketing_card.rs")
            .exists());

        let source =
            fs::read_to_string(temp.path().join("src/components/marketing_card.rs")).unwrap();
        assert!(source.contains(
            "use proa_core::{ClassList, DataLoader, WebContext, WebRenderSync, WriteError};"
        ));
        assert!(source.contains("impl<L: DataLoader> WebRenderSync<L> for MarketingCard"));
        assert!(source.contains("fn render(self, cx: &mut WebContext<L>)"));
        syn::parse_file(&source).expect("generated component must be valid Rust syntax");
    }

    #[test]
    fn generated_component_with_children_uses_default_writer() {
        let source = component_source(
            "MarketingCard",
            "data-marketing-card",
            true,
            false,
            false,
            false,
            true,
        );

        assert!(source.contains("impl<L, C> WebRenderSync<L> for MarketingCard<C>"));
        assert!(source.contains("C: WebRenderSync<L>"));
        assert!(source.contains("WebContext::new()"));
        assert!(source.contains("cx.into_output().into_string()"));
        syn::parse_file(&source).expect("generated child component must be valid Rust syntax");
    }

    #[test]
    fn generated_layouts_use_default_writer() {
        for source in [
            section_layout_source("MarketingLayout"),
            document_layout_source("DocumentLayout"),
        ] {
            assert!(source.contains("impl<L, C> WebRenderSync<L>"));
            assert!(source.contains("C: WebRenderSync<L>"));
            assert!(source.contains("DataLoader"));
            assert!(!source.contains("WriteBuf"));
            syn::parse_file(&source).expect("generated layout must be valid Rust syntax");
        }
    }

    #[test]
    fn new_site_writes_project_config_and_lock() {
        let temp = tempfile::tempdir().unwrap();
        let root = temp.path().join("config-demo");
        new_site(NewSiteOptions {
            name: "config-demo".to_string(),
            root: root.clone(),
            template: "basic".to_string(),
            empty: false,
            server: ServerIntegration::Axum,
            tailwind: false,
            docs: DocsIntegration::None,
            blog: false,
            deploy: DeployTarget::None,
            skip_install: true,
            no_git: true,
            agents_md: true,
            dry_run: None,
            force: false,
            yes: true,
        })
        .unwrap();

        assert!(root.join(crate::config::CONFIG_FILE).exists());
        assert!(root.join(crate::config::LOCK_FILE).exists());
    }

    #[test]
    fn new_site_writes_hardened_dev_scripts() {
        let temp = tempfile::tempdir().unwrap();
        let root = temp.path().join("watch-demo");
        new_site(NewSiteOptions {
            name: "watch-demo".to_string(),
            root: root.clone(),
            template: "basic".to_string(),
            empty: false,
            server: ServerIntegration::Axum,
            tailwind: false,
            docs: DocsIntegration::None,
            blog: false,
            deploy: DeployTarget::None,
            skip_install: true,
            no_git: true,
            agents_md: true,
            dry_run: None,
            force: false,
            yes: true,
        })
        .unwrap();

        let dev = fs::read_to_string(root.join("scripts/dev.sh")).unwrap();
        assert_eq!(dev, dev_script());
        assert!(dev.contains("$ROOT/target/watch/dev"));
        assert!(dev.contains("proa_watch_lock_acquire \"dev\" \"$ROOT\""));
        assert!(dev.contains(
            "proa_watch_run proa dev --root \"$ROOT\" ${dev_args[@]+\"${dev_args[@]}\"} \"$@\""
        ));

        // Live-reload settings are shell variables at the top of the script,
        // so they can be edited in place or exported, and each one maps to the
        // `proa dev` flag that implements it.
        for setting in [
            "PROA_DEV_RELOAD=\"${PROA_DEV_RELOAD:-on}\"",
            "PROA_DEV_RELOAD_PORT=\"${PROA_DEV_RELOAD_PORT:-}\"",
            "PROA_DEV_CSS_HOT_SWAP=\"${PROA_DEV_CSS_HOT_SWAP:-on}\"",
            "PROA_DEV_DEBOUNCE_MS=\"${PROA_DEV_DEBOUNCE_MS:-300}\"",
            "dev_args+=(--no-reload)",
            "dev_args+=(--no-css-hot-swap)",
            "dev_args+=(--reload-port \"$PROA_DEV_RELOAD_PORT\")",
            "dev_args+=(--debounce-ms \"$PROA_DEV_DEBOUNCE_MS\")",
            // Turning a setting off must work for the spellings people
            // actually type, not only the literal `0`.
            "0 | false | no | off) return 0 ;;",
        ] {
            assert!(dev.contains(setting), "dev.sh is missing `{setting}`");
        }

        let watch_lock = fs::read_to_string(root.join("scripts/watch-lock.sh")).unwrap();
        assert_eq!(watch_lock, watch_lock_script());
        assert!(watch_lock.contains("if mkdir \"$lock_dir\""));
        assert!(watch_lock.contains("kill -KILL -- \"-$child_pgid\""));

        let build = fs::read_to_string(root.join("scripts/build.sh")).unwrap();
        assert_eq!(build, build_script());
        assert!(build.contains("proa build --root \"$ROOT\" \"$@\""));

        let readme = fs::read_to_string(root.join("README.md")).unwrap();
        assert!(readme.contains("bash scripts/dev.sh"));
        assert!(readme.contains("bash scripts/build.sh"));
    }

    #[test]
    fn new_site_generates_production_server_defaults() {
        let temp = tempfile::tempdir().unwrap();
        let root = temp.path().join("prod-demo");
        new_site(NewSiteOptions {
            name: "prod-demo".to_string(),
            root: root.clone(),
            template: "basic".to_string(),
            empty: false,
            server: ServerIntegration::Axum,
            tailwind: false,
            docs: DocsIntegration::None,
            blog: false,
            deploy: DeployTarget::None,
            skip_install: true,
            no_git: true,
            agents_md: true,
            dry_run: None,
            force: false,
            yes: true,
        })
        .unwrap();

        let main = fs::read_to_string(root.join("src/main.rs")).unwrap();
        assert!(main.contains("const SHUTDOWN_DRAIN_TIMEOUT"));
        assert!(main.contains("proa_framework_axum::serve(listener, app, SHUTDOWN_DRAIN_TIMEOUT)"));
        assert!(!main.contains("async fn shutdown_signal"));
        assert!(main.contains("/robots.txt"));
        assert!(main.contains("X_CONTENT_TYPE_OPTIONS"));
        assert!(main.contains("X_FRAME_OPTIONS"));
        assert!(main.contains("REFERRER_POLICY"));
        assert!(main.contains("TraceLayer::new_for_http()"));

        let layout = fs::read_to_string(root.join("src/layouts/root.rs")).unwrap();
        // Written into the macro, not through StaticRaw: `html/staticraw-doctype`
        // rejects the wrapped form, so scaffolding it made every fresh project
        // fail the `proa lint --deny warnings` and `proa build` we tell people to run.
        assert!(layout.contains("<!DOCTYPE html>"));
        assert!(!layout.contains("StaticRaw"));
        assert!(!layout.contains("UnsafeRaw"));
        assert!(layout.contains("NAV_ITEMS"));
        assert!(layout.contains("rel=\"icon\""));
        assert!(layout.contains("impl<L, C> WebRenderSync<L> for RootDocument<C>"));
        assert!(layout.contains("C: WebRenderSync<L>"));
        assert!(layout.contains("impl<L: DataLoader> WebRenderSync<L> for SiteNav"));
        syn::parse_file(&layout).expect("generated root layout must be valid Rust syntax");

        let home = fs::read_to_string(root.join("src/pages/home.rs")).unwrap();
        assert!(home.contains("fn home_page_renders"));
        assert!(home.contains("impl<L: DataLoader> WebRenderSync<L> for HomePage"));
        assert!(home.contains("render_to_string(RootDocument"));
        assert!(!home.contains("WebContext::with_buffer(&mut out)"));
        syn::parse_file(&home).expect("generated home page must be valid Rust syntax");

        let agent_instructions = fs::read_to_string(root.join("AGENTS.md")).unwrap();
        assert!(agent_instructions.contains("WebRenderSync<L>"));
        assert!(agent_instructions.contains("C: WebRenderSync<L>"));
        assert!(agent_instructions.contains("owns the default `RenderBuffer`"));
        assert!(agent_instructions.contains("#[rsjs(component)]"));
        assert!(agent_instructions.contains("#[rsjs(component, client)]"));
        assert!(agent_instructions.contains("Do not use inherent render shorthand"));
        assert!(agent_instructions.contains("component `clients(...)` allowlists"));
        assert!(agent_instructions.contains("direct inbound read such as `self.label`"));
        assert!(agent_instructions.contains("`signal(self.value)` initializer samples"));

        let cargo = fs::read_to_string(root.join("Cargo.toml")).unwrap();
        assert!(cargo.contains("proa_core = \"0.3.0\""));
        assert!(cargo.contains("proa_macros = \"0.3.0\""));
        assert!(cargo.contains("proa_framework_axum = \"0.3.0\""));
        assert!(!cargo.contains("registry = \"proa\""));
        assert!(!cargo.contains("proa_core = { path ="));
        let cargo_config = fs::read_to_string(root.join(".cargo/config.toml")).unwrap();
        assert!(cargo_config.contains("git-fetch-with-cli = true"));
        assert!(cargo_config
            .parse::<toml::Value>()
            .unwrap()
            .get("registries")
            .is_none());
        assert!(!cargo.contains("proa_macros = { path ="));
        assert!(!cargo.contains("proa_framework_axum = { path ="));
        assert!(!cargo.contains("\"signal\""));
        assert!(cargo.contains("\"trace\""));
        assert!(cargo.contains("[profile.release]"));
    }

    #[test]
    fn new_site_docs_option_creates_docs_route() {
        let temp = tempfile::tempdir().unwrap();
        let root = temp.path().join("docs-demo");
        new_site(NewSiteOptions {
            name: "docs-demo".to_string(),
            root: root.clone(),
            template: "basic".to_string(),
            empty: false,
            server: ServerIntegration::Axum,
            tailwind: false,
            docs: DocsIntegration::ProaDocs,
            blog: false,
            deploy: DeployTarget::None,
            skip_install: true,
            no_git: true,
            agents_md: true,
            dry_run: None,
            force: false,
            yes: true,
        })
        .unwrap();

        assert!(root.join("src/docs/index.mdx").exists());
        assert!(root.join("src/pages/docs.rs").exists());
        let main = fs::read_to_string(root.join("src/main.rs")).unwrap();
        assert!(main.contains("/docs/*path"));

        // Docs and fonts share one build.rs: the font collector first, then
        // the docs compilation, with [build-dependencies] emitted only once.
        let build = fs::read_to_string(root.join("build.rs")).unwrap();
        let fonts = build
            .find("proa_fonts_build::collect::run")
            .expect("build.rs must run the font collector");
        let docs = build
            .find("proa_docs_build::compile")
            .expect("build.rs must compile docs");
        assert!(
            fonts < docs,
            "the fonts section must precede docs compilation"
        );
        syn::parse_file(&build).expect("generated build.rs must be valid Rust syntax");

        let cargo = fs::read_to_string(root.join("Cargo.toml")).unwrap();
        assert_eq!(cargo.matches("[build-dependencies]").count(), 1);
        assert!(cargo.contains("proa_fonts_build = \"0.1.0\""));
        assert!(cargo.contains("proa_docs_build = \"0.2.0\""));
    }

    #[test]
    fn new_site_wires_font_collector() {
        let temp = tempfile::tempdir().unwrap();
        let root = temp.path().join("fonts-demo");
        new_site(NewSiteOptions {
            name: "fonts-demo".to_string(),
            root: root.clone(),
            template: "basic".to_string(),
            empty: false,
            server: ServerIntegration::Axum,
            tailwind: false,
            docs: DocsIntegration::None,
            blog: false,
            deploy: DeployTarget::None,
            skip_install: true,
            no_git: true,
            agents_md: false,
            dry_run: None,
            force: false,
            yes: true,
        })
        .unwrap();

        let cargo = fs::read_to_string(root.join("Cargo.toml")).unwrap();
        assert!(cargo.contains("proa_fonts = \"0.1.0\""));
        assert!(cargo.contains("proa_fonts_macros = \"0.1.0\""));
        assert!(cargo.contains("[build-dependencies]\nproa_fonts_build = \"0.1.0\""));
        assert!(!cargo.contains("proa_docs_build"));

        let build = fs::read_to_string(root.join("build.rs")).unwrap();
        assert!(build.contains("proa_fonts_build::collect::run("));
        assert!(build.contains("CollectOptions::from_build_env()"));
        assert!(build.contains("panic!(\"proa fonts: {error}\")"));
        assert!(build.contains("cargo:rerun-if-changed=src"));
        assert!(build.contains("cargo:rerun-if-changed=proa-fonts.lock"));
        assert!(!build.contains("proa_docs_build"));
        syn::parse_file(&build).expect("generated build.rs must be valid Rust syntax");

        let main = fs::read_to_string(root.join("src/main.rs")).unwrap();
        assert!(main.contains(
            "mod __proa_fonts_generated { include!(concat!(env!(\"OUT_DIR\"), \"/proa_fonts.rs\")); }"
        ));
        assert!(
            main.contains("// Generated font consts for google_font!/local_font! declarations.")
        );
        // The generated CSS references /fonts, so the router must serve the
        // vendored files there, not only under /static.
        let fonts_route = main
            .find(".nest_service(\"/fonts\", static_assets::fonts_service())")
            .expect("main.rs must mount the fonts service at /fonts");
        let static_route = main
            .find(".nest_service(\"/static\"")
            .expect("main.rs must mount the static service");
        assert!(fonts_route < static_route);
        syn::parse_file(&main).expect("generated main.rs must be valid Rust syntax");

        let assets = fs::read_to_string(root.join("src/static_assets.rs")).unwrap();
        assert!(assets.contains("pub fn fonts_service() -> Router"));
        assert!(assets.contains("static_root().join(\"fonts\")"));
        assert!(assets.contains("public, max-age=31536000, immutable"));
        syn::parse_file(&assets).expect("generated static_assets.rs must be valid Rust syntax");
    }

    #[test]
    fn new_site_empty_skips_font_wiring() {
        let temp = tempfile::tempdir().unwrap();
        let root = temp.path().join("empty-demo");
        new_site(NewSiteOptions {
            name: "empty-demo".to_string(),
            root: root.clone(),
            template: "basic".to_string(),
            empty: true,
            server: ServerIntegration::Axum,
            tailwind: false,
            docs: DocsIntegration::None,
            blog: false,
            deploy: DeployTarget::None,
            skip_install: true,
            no_git: true,
            agents_md: false,
            dry_run: None,
            force: false,
            yes: true,
        })
        .unwrap();

        let cargo = fs::read_to_string(root.join("Cargo.toml")).unwrap();
        assert!(!cargo.contains("proa_fonts"));
        assert!(!cargo.contains("[build-dependencies]"));
        assert!(!root.join("build.rs").exists());

        let main = fs::read_to_string(root.join("src/main.rs")).unwrap();
        assert!(!main.contains("__proa_fonts_generated"));
        assert!(!main.contains("fonts_service"));

        let assets = fs::read_to_string(root.join("src/static_assets.rs")).unwrap();
        assert!(!assets.contains("fonts_service"));
        syn::parse_file(&assets).expect("generated static_assets.rs must be valid Rust syntax");
    }

    #[test]
    fn new_site_server_none_skips_font_wiring() {
        let temp = tempfile::tempdir().unwrap();
        let root = temp.path().join("headless-demo");
        new_site(NewSiteOptions {
            name: "headless-demo".to_string(),
            root: root.clone(),
            template: "basic".to_string(),
            empty: false,
            server: ServerIntegration::None,
            tailwind: false,
            docs: DocsIntegration::ProaDocs,
            blog: false,
            deploy: DeployTarget::None,
            skip_install: true,
            no_git: true,
            agents_md: false,
            dry_run: None,
            force: false,
            yes: true,
        })
        .unwrap();

        // Without a generated crate root there is nowhere to include the
        // build-generated module, so no font wiring is emitted.
        let cargo = fs::read_to_string(root.join("Cargo.toml")).unwrap();
        assert!(!cargo.contains("proa_fonts"));
        assert_eq!(cargo.matches("[build-dependencies]").count(), 1);
        assert!(cargo.contains("proa_docs_build = \"0.2.0\""));

        let build = fs::read_to_string(root.join("build.rs")).unwrap();
        assert!(!build.contains("proa_fonts_build"));
        assert!(build.contains("proa_docs_build::compile(config);"));
        syn::parse_file(&build).expect("generated build.rs must be valid Rust syntax");

        assert!(!root.join("src/main.rs").exists());
    }

    #[test]
    fn new_site_coolify_deploy_preset_writes_deploy_files() {
        let temp = tempfile::tempdir().unwrap();
        let root = temp.path().join("deploy-demo");
        new_site(NewSiteOptions {
            name: "deploy-demo".to_string(),
            root: root.clone(),
            template: "basic".to_string(),
            empty: false,
            server: ServerIntegration::Axum,
            tailwind: false,
            docs: DocsIntegration::None,
            blog: false,
            deploy: DeployTarget::Coolify,
            skip_install: true,
            no_git: true,
            agents_md: true,
            dry_run: None,
            force: false,
            yes: true,
        })
        .unwrap();

        assert!(root.join("Dockerfile").exists());
        assert!(root.join(".dockerignore").exists());
        assert!(root.join("coolify.env.example").exists());
        assert!(root.join("COOLIFY.md").exists());
        let dockerfile = fs::read_to_string(root.join("Dockerfile")).unwrap();
        assert!(!dockerfile.contains("CARGO_REGISTRIES_PROA_TOKEN"));
        assert!(!dockerfile.contains("GIT_CONFIG"));
        assert!(!dockerfile.contains("github_token"));
        assert!(dockerfile.contains("cargo build --release --bin deploy-demo"));
        assert!(dockerfile.contains("EXPOSE 3000"));
        let main = fs::read_to_string(root.join("src/main.rs")).unwrap();
        assert!(main.contains("/healthz"));
    }
}