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

import json
import logging
import os
import re
import time
from pathlib import Path
from typing import Any, Mapping, Sequence

import httpx
from dotenv import load_dotenv

from .file_service import (
    build_preview_file_tree,
    finalize_preview_files,
)


load_dotenv()

DEFAULT_OLLAMA_BASE_URL = "http://localhost:11434"
DEFAULT_OLLAMA_MODEL = "qwen2.5-coder"
FAST_PLANNER_TIMEOUT_SECONDS = 22.0
DEEP_PREVIEW_TIMEOUT_SECONDS = 70.0
MIN_CUSTOM_PASS_SECONDS = 8.0
MAX_CUSTOM_FILES = 8
MAX_CUSTOM_FILE_LINES = 300
FAST_PLANNER_RESPONSE_BYTES_LIMIT = 48 * 1024
REQUIRED_PLANNER_KEYS = {
    "projectName",
    "projectType",
    "selectedStack",
    "modules",
    "packageRequirements",
    "installCommands",
    "runCommands",
    "requiredInputs",
    "envVariables",
    "customFiles",
}

logger = logging.getLogger(__name__)

STACK_FIELDS = (
    "language",
    "frontend",
    "backend",
    "database",
    "aiTools",
    "deployment",
)
STACK_DEFAULTS = {
    "language": "Auto",
    "frontend": "Auto",
    "backend": "Auto",
    "database": "Auto",
    "aiTools": "Auto",
    "deployment": "Auto",
}
NONE_LIKE = {"", "Auto", "None"}
YES_ANSWERS = {"yes", "y", "true", "1", "required", "needed"}
NO_ANSWERS = {"no", "n", "false", "0", "none", "not needed"}
BOOLEAN_QUESTION_IDS = {"authentication", "admin_dashboard", "email_smtp", "payment_system"}
SUPPORTED_SCOPE_VALUES = {"full-stack", "backend-only", "frontend-only"}
SUPPORTED_LANGUAGE_VALUES = {"Python", "JavaScript", "TypeScript", "Java"}
SUPPORTED_FRONTEND_VALUES = {"React", "Next.js", "Vue", "HTML/CSS/JavaScript", "None"}
SUPPORTED_BACKEND_VALUES = {"FastAPI", "Flask", "Express", "NestJS", "Spring Boot"}
SUPPORTED_DATABASE_VALUES = {"SQLite", "PostgreSQL", "MySQL", "MongoDB", "None"}
SUPPORTED_AI_VALUES = {"None", "Ollama", "OpenAI API", "LangChain"}
SUPPORTED_DEPLOYMENT_VALUES = {"Render", "Railway", "Vercel", "Docker", "None"}
SUPPORTED_COMPLEXITY_VALUES = {"Simple", "Standard", "Advanced"}
SUPPORTED_ROLE_VALUES = {
    "Single user / no roles",
    "Basic roles (admin + user)",
    "Custom roles needed",
}
CANONICAL_ANSWER_ALIASES = {
    "project_scope": {
        "full stack": "full-stack",
        "full-stack": "full-stack",
        "fullstack": "full-stack",
        "frontend and backend": "full-stack",
        "backend only": "backend-only",
        "backend-only": "backend-only",
        "api only": "backend-only",
        "service only": "backend-only",
        "frontend only": "frontend-only",
        "frontend-only": "frontend-only",
        "ui only": "frontend-only",
    },
    "language": {
        "python": "Python",
        "py": "Python",
        "javascript": "JavaScript",
        "js": "JavaScript",
        "typescript": "TypeScript",
        "ts": "TypeScript",
        "java": "Java",
    },
    "frontend_framework": {
        "react": "React",
        "next": "Next.js",
        "nextjs": "Next.js",
        "next.js": "Next.js",
        "vue": "Vue",
        "vanilla": "HTML/CSS/JavaScript",
        "html": "HTML/CSS/JavaScript",
        "html/css/javascript": "HTML/CSS/JavaScript",
        "plain html": "HTML/CSS/JavaScript",
        "none": "None",
        "no frontend": "None",
    },
    "backend_framework": {
        "fastapi": "FastAPI",
        "flask": "Flask",
        "express": "Express",
        "node": "Express",
        "node.js": "Express",
        "nestjs": "NestJS",
        "nest": "NestJS",
        "spring": "Spring Boot",
        "spring boot": "Spring Boot",
        "none": "None",
        "no backend": "None",
    },
    "database": {
        "sqlite": "SQLite",
        "postgres": "PostgreSQL",
        "postgresql": "PostgreSQL",
        "postgre sql": "PostgreSQL",
        "mysql": "MySQL",
        "mongo": "MongoDB",
        "mongodb": "MongoDB",
        "none": "None",
        "no database": "None",
    },
    "ai_integration": {
        "none": "None",
        "no": "None",
        "ollama": "Ollama",
        "openai": "OpenAI API",
        "openai api": "OpenAI API",
        "langchain": "LangChain",
    },
    "deployment_target": {
        "render": "Render",
        "railway": "Railway",
        "vercel": "Vercel",
        "docker": "Docker",
        "container": "Docker",
        "none": "None",
    },
    "complexity_level": {
        "simple": "Simple",
        "basic": "Simple",
        "beginner": "Simple",
        "standard": "Standard",
        "normal": "Standard",
        "default": "Standard",
        "advanced": "Advanced",
        "complex": "Advanced",
    },
    "user_roles": {
        "single": "Single user / no roles",
        "single user": "Single user / no roles",
        "no roles": "Single user / no roles",
        "admin and user": "Basic roles (admin + user)",
        "admin + user": "Basic roles (admin + user)",
        "basic roles": "Basic roles (admin + user)",
        "custom roles": "Custom roles needed",
        "custom": "Custom roles needed",
        "manager roles": "Custom roles needed",
    },
}

KEYWORD_MAP = {
    "language": {
        "Python": ("python", "fastapi", "flask", "django"),
        "JavaScript": ("javascript", "node", "express"),
        "TypeScript": ("typescript", "next.js", "nextjs", "nest", "nestjs"),
        "Java": ("java", "spring", "spring boot"),
    },
    "frontend": {
        "React": ("react",),
        "Next.js": ("next.js", "nextjs"),
        "Vue": ("vue",),
        "HTML/CSS/JavaScript": ("html", "css", "javascript", "vanilla js", "frontend"),
        "None": ("api only", "backend only", "cli", "script"),
    },
    "backend": {
        "FastAPI": ("fastapi",),
        "Flask": ("flask",),
        "Express": ("express", "node api"),
        "NestJS": ("nestjs", "nest js"),
        "Spring Boot": ("spring boot", "spring"),
        "None": ("static site", "frontend only"),
    },
    "database": {
        "SQLite": ("sqlite",),
        "PostgreSQL": ("postgres", "postgresql"),
        "MySQL": ("mysql",),
        "MongoDB": ("mongodb", "mongo"),
        "None": ("no database", "without database", "in memory"),
    },
    "aiTools": {
        "Ollama": ("ollama", "local llm", "qwen", "llama"),
        "OpenAI API": ("openai", "gpt"),
        "LangChain": ("langchain",),
        "None": ("no ai", "without ai"),
    },
    "deployment": {
        "Render": ("render",),
        "Railway": ("railway",),
        "Vercel": ("vercel",),
        "Docker": ("docker", "container"),
        "None": ("no deployment",),
    },
}


async def generate_project_preview(
    idea: str,
    selected_stack: dict[str, str] | None = None,
    generation_mode: str = "fast",
    final_requirements: str = "",
) -> dict[str, Any]:
    from .agent_controller import agent_controller

    return await agent_controller.generate_files(
        idea,
        selected_stack,
        generation_mode,
        final_requirements,
    )


def prepare_preview_for_output(preview: dict[str, Any]) -> dict[str, Any]:
    base_idea = (
        str(preview.get("problemStatement") or "").strip()
        or str(preview.get("summary") or "").strip()
        or str(preview.get("projectName") or "").strip()
        or "Generated project"
    )
    requested_stack = normalize_stack_selection(preview.get("selectedStack"))
    return normalize_preview(preview, base_idea, requested_stack, "fast")


async def generate_project_plan(
    idea: str,
    requested_stack: dict[str, str],
    generation_mode: str,
    deadline: float,
) -> dict[str, Any]:
    timeout_seconds = remaining_time(deadline)
    if timeout_seconds <= 0:
        raise TimeoutError("Preview time budget was exhausted before generation started.")

    prompt = build_planning_prompt(idea, requested_stack, generation_mode)
    planner_payload = await call_ollama_json(
        prompt,
        timeout_seconds,
        enforce_compact_output=(generation_mode == "fast"),
        response_kind="planner",
    )
    return validate_planner_payload(planner_payload)


async def generate_deep_custom_files(
    idea: str,
    project_name: str,
    selected_stack: dict[str, str],
    custom_manifest: list[dict[str, str]],
    timeout_seconds: float,
) -> list[dict[str, str]]:
    prompt = build_custom_files_prompt(idea, project_name, selected_stack, custom_manifest)
    payload = await call_ollama_json(
        prompt,
        timeout_seconds,
        enforce_compact_output=False,
        response_kind="custom",
    )
    files = normalize_files(payload.get("files"))

    allowed_paths = {item["path"] for item in custom_manifest}
    trimmed: list[dict[str, str]] = []
    for file_entry in files:
        if file_entry["path"] not in allowed_paths:
            continue
        content = trim_content_lines(file_entry["content"])
        trimmed.append({"path": file_entry["path"], "content": content})
    return trimmed


async def call_ollama_json(
    prompt: str,
    timeout_seconds: float,
    *,
    enforce_compact_output: bool,
    response_kind: str,
) -> dict[str, Any]:
    configured_base_url = str(os.getenv("OLLAMA_BASE_URL") or "").strip()
    if not configured_base_url:
        raise RuntimeError(
            "AI generation is unavailable because OLLAMA_BASE_URL is not configured."
        )

    base_url = configured_base_url.rstrip("/")
    model = os.getenv("OLLAMA_MODEL", DEFAULT_OLLAMA_MODEL).strip() or DEFAULT_OLLAMA_MODEL
    safe_timeout = max(1.0, timeout_seconds)
    request_timeout = httpx.Timeout(
        connect=min(3.5, safe_timeout),
        read=safe_timeout,
        write=min(5.0, safe_timeout),
        pool=min(5.0, safe_timeout),
    )

    try:
        async with httpx.AsyncClient(timeout=request_timeout) as client:
            response = await client.post(
                f"{base_url}/api/generate",
                json={"model": model, "prompt": prompt, "stream": False},
            )
    except httpx.HTTPError as exc:
        raise RuntimeError(
            f"AI generation is unavailable because Ollama could not be reached at {base_url}."
        ) from exc

    if response.status_code >= 400:
        detail = response.text.strip() or f"HTTP {response.status_code}"
        raise RuntimeError(f"Ollama returned an error: {detail}")

    payload = response.json()
    raw_text = str(payload.get("response") or "").strip()
    if not raw_text:
        raise RuntimeError("Ollama returned an empty response.")
    if enforce_compact_output and len(raw_text.encode("utf-8")) > FAST_PLANNER_RESPONSE_BYTES_LIMIT:
        raise ValueError(
            f"{response_kind.capitalize()} response exceeded the compact output limit for Fast Mode."
        )

    return parse_model_json(raw_text)


def build_planning_prompt(
    idea: str,
    selected_stack: dict[str, str],
    generation_mode: str,
) -> str:
    stack_summary = "\n".join(
        f"- {field}: {selected_stack.get(field, 'Auto')}" for field in STACK_FIELDS
    )

    return f"""
You are Project Agent.

Return compact JSON only. No markdown. No commentary.

Goal:
- Plan a 100% runnable starter project.
- Do not generate the entire project.
- The backend will create standard files from templates.

Rules:
1. Preserve user-specified language, frameworks, tools, and deployment choices.
2. Respect any non-Auto selectedStack values exactly.
3. Choose beginner-friendly defaults only for missing stack categories.
4. For full-stack output, plan both frontend and backend.
5. Do not include README, dependency manifests, setup scripts, run scripts, env files, config boilerplate, or installed libraries in customFiles.
6. customFiles must contain only project-specific business logic files, max {MAX_CUSTOM_FILES}.
7. Each custom file should be worth generating because it contains app-specific logic or UI, not generic boilerplate.
8. Detect required external inputs and return them in requiredInputs.
9. If the project uses auth, database, email, payments, AI providers, or OAuth, include the needed keys in requiredInputs.
10. Do not claim this is a production-finished application. It is a 100% runnable starter project.

Return this exact top-level shape:
{{
  "projectName": "string",
  "projectType": "frontend-only | backend-only | full-stack",
  "selectedStack": {{
    "language": "string",
    "frontend": "string",
    "backend": "string",
    "database": "string",
    "aiTools": "string",
    "deployment": "string"
  }},
  "modules": [
    {{
      "name": "string",
      "purpose": "string",
      "keyFiles": ["string"]
    }}
  ],
  "packageRequirements": ["string"],
  "installCommands": ["string"],
  "runCommands": ["string"],
  "requiredInputs": [
    {{
      "name": "string",
      "required": true,
      "example": "string",
      "whereToAdd": ".env",
      "purpose": "string"
    }}
  ],
  "envVariables": [
    {{
      "name": "string",
      "value": "string",
      "description": "string"
    }}
  ],
  "customFiles": [
    {{
      "path": "relative/path",
      "purpose": "why this file is needed"
    }}
  ]
}}

Mode:
- generationMode: {generation_mode}
- Fast Mode should keep customFiles compact and rely on backend templates for standard files.
- Deep Mode may propose richer custom business files, but still max {MAX_CUSTOM_FILES}.

Selected stack:
{stack_summary}

Project idea:
{idea}
""".strip()


def build_custom_files_prompt(
    idea: str,
    project_name: str,
    selected_stack: dict[str, str],
    custom_manifest: list[dict[str, str]],
) -> str:
    manifest_json = json.dumps(custom_manifest, indent=2)
    stack_json = json.dumps(selected_stack, indent=2)
    return f"""
You are Project Agent.

Generate only the custom business-logic files listed below.
Return JSON only in this shape:
{{
  "files": [
    {{
      "path": "relative/path",
      "content": "file content"
    }}
  ]
}}

Rules:
1. Generate only the listed files.
2. Do not generate standard boilerplate, dependency manifests, docs, setup scripts, or env files.
3. Keep each file under {MAX_CUSTOM_FILE_LINES} lines.
4. Use the selected stack exactly.
5. Produce runnable starter logic, not empty placeholders.

Project name: {project_name}
Project idea: {idea}
Selected stack:
{stack_json}

Requested custom files:
{manifest_json}
""".strip()


def parse_model_json(raw_text: str) -> dict[str, Any]:
    text = strip_markdown_fences(raw_text)
    candidate = extract_json_object(text)
    if not candidate:
        raise ValueError("Could not recover a JSON object from the AI response.")

    parsed = json.loads(candidate)
    if not isinstance(parsed, dict):
        raise ValueError("AI response JSON was not an object.")
    return parsed


def strip_markdown_fences(text: str) -> str:
    stripped = text.strip()
    if stripped.startswith("```"):
        stripped = re.sub(r"^```[a-zA-Z0-9_-]*\s*", "", stripped)
        stripped = re.sub(r"\s*```$", "", stripped)
    return stripped.strip()


def extract_json_object(text: str) -> str:
    start = text.find("{")
    if start == -1:
        return ""

    depth = 0
    in_string = False
    escape = False
    for index in range(start, len(text)):
        char = text[index]
        if in_string:
            if escape:
                escape = False
            elif char == "\\":
                escape = True
            elif char == '"':
                in_string = False
            continue

        if char == '"':
            in_string = True
        elif char == "{":
            depth += 1
        elif char == "}":
            depth -= 1
            if depth == 0:
                return text[start : index + 1]

    return ""


def build_fallback_preview(
    idea: str,
    requested_stack: dict[str, str],
    generation_mode: str,
    reason: str,
    generation_context: str | None = None,
) -> dict[str, Any]:
    preview = normalize_preview(
        {},
        idea,
        requested_stack,
        generation_mode,
        requirements_context=generation_context or idea,
    )
    fallback_note = (
        "Fast Mode returned a complete template-based preview because AI generation was unavailable."
        if generation_mode == "fast"
        else "Deep Mode AI enrichment was unavailable, so a complete template-based preview was returned."
    )
    preview["assumptions"] = dedupe_list(
        [
            fallback_note,
            f"Template fallback preview was generated because the AI planner could not complete in time or returned invalid output: {reason}",
            *preview.get("assumptions", []),
        ]
    )
    return preview


def build_generation_context(
    idea: str,
    final_requirements: str = "",
    generation_mode: str = "fast",
) -> str:
    cleaned_idea = idea.strip()
    cleaned_requirements = final_requirements.strip()
    if not cleaned_requirements:
        return cleaned_idea
    if generation_mode == "fast":
        return (
            f"Idea: {cleaned_idea}\n"
            "Final requirements:\n"
            f"{cleaned_requirements}"
        ).strip()
    return (
        f"Original idea:\n{cleaned_idea}\n\n"
        "Finalized requirements from the agent conversation:\n"
        f"{cleaned_requirements}"
    ).strip()


def analyze_project_idea(idea: str) -> dict[str, Any]:
    detected_choices = detect_user_choices(idea)
    suggested_stack = resolve_selected_stack(
        idea,
        normalize_stack_selection({}),
        None,
        detected_choices,
    )
    project_kind = determine_project_kind(suggested_stack, infer_declared_project_type(idea))
    questions = build_agent_questions(idea, suggested_stack, project_kind)
    return {
        "understanding": build_agent_understanding(idea, suggested_stack, project_kind),
        "assumptions": build_agent_analysis_assumptions(suggested_stack, project_kind, questions),
        "suggestedStack": suggested_stack,
        "stackReasons": build_stack_reasons(suggested_stack, project_kind),
        "questions": questions,
        "detectedProjectType": project_kind["label"],
        "confidence": compute_agent_confidence(idea, detected_choices, questions, project_kind),
    }


def finalize_agent_requirements(
    idea: str,
    answers: Mapping[str, Any] | None,
    suggested_stack: Mapping[str, Any] | None,
) -> dict[str, Any]:
    normalized_answers = normalize_agent_answers(answers)
    selected_stack = normalize_stack_selection(suggested_stack)
    selected_stack = apply_agent_answers_to_stack(idea, selected_stack, normalized_answers)
    project_kind = determine_project_kind(selected_stack, normalized_answers.get("project_scope"))
    final_requirements = build_final_requirements_summary(
        idea,
        normalized_answers,
        selected_stack,
        project_kind,
    )
    assumptions = build_agent_finalize_assumptions(
        normalized_answers,
        selected_stack,
        project_kind,
    )
    return {
        "finalRequirements": final_requirements,
        "selectedStack": selected_stack,
        "assumptions": assumptions,
    }


def infer_declared_project_type(idea: str) -> str:
    lowered = idea.lower()
    if any(token in lowered for token in ("full stack", "full-stack", "frontend and backend")):
        return "full-stack"
    if any(token in lowered for token in ("backend only", "api only", "service only")):
        return "backend-only"
    if any(token in lowered for token in ("frontend only", "landing page only", "static site")):
        return "frontend-only"
    return ""


def build_agent_understanding(
    idea: str,
    suggested_stack: dict[str, str],
    project_kind: dict[str, Any],
) -> str:
    description = idea.strip().rstrip(".")
    if len(description) > 180:
        description = description[:177].rstrip() + "..."
    stack_bits: list[str] = []
    if project_kind["hasFrontend"]:
        stack_bits.append(suggested_stack["frontend"])
    if project_kind["hasBackend"]:
        stack_bits.append(suggested_stack["backend"])
    stack_text = " + ".join(bit for bit in stack_bits if bit not in NONE_LIKE) or suggested_stack["language"]
    return (
        f"I understood your idea as a {project_kind['label']} project focused on {description}. "
        f"My starting recommendation is {stack_text} with template-generated runtime structure so you can get to a runnable starter quickly."
    )


def build_agent_analysis_assumptions(
    suggested_stack: dict[str, str],
    project_kind: dict[str, Any],
    questions: list[dict[str, Any]],
) -> list[str]:
    assumptions = [
        "Questions are limited to decisions that change architecture, dependencies, or required files.",
        "If you skip the questions, the suggested stack and defaults will be used to generate the project.",
    ]
    if project_kind["isFullStack"]:
        assumptions.append("The app currently looks like a full-stack build, so both frontend and backend are planned by default.")
    elif project_kind["hasBackend"]:
        assumptions.append("The current recommendation leans backend-first and will keep the frontend optional unless you add it.")
    else:
        assumptions.append("The current recommendation treats this as a frontend-focused starter unless you add backend needs.")
    if questions:
        assumptions.append("Each question starts with your own input first. If you leave it blank, the agent will suggest a default and explain the benefit.")
    if suggested_stack.get("deployment") == "Render":
        assumptions.append("Render is used as a deployment default when no target is mentioned explicitly.")
    return dedupe_list(assumptions)


def build_stack_reasons(
    suggested_stack: dict[str, str],
    project_kind: dict[str, Any],
) -> list[str]:
    reasons: list[str] = []
    backend = suggested_stack.get("backend", "Auto")
    frontend = suggested_stack.get("frontend", "Auto")
    database = suggested_stack.get("database", "Auto")
    deployment = suggested_stack.get("deployment", "Auto")
    ai_tools = suggested_stack.get("aiTools", "Auto")

    if backend == "FastAPI":
        reasons.append("FastAPI is a beginner-friendly Python API framework that pairs well with the current runnable starter templates.")
    elif backend == "Express":
        reasons.append("Express keeps a Node-based backend lightweight and easy to extend for API-first projects.")
    elif backend == "Spring Boot":
        reasons.append("Spring Boot was chosen because the idea strongly suggests a Java-based backend stack.")

    if frontend == "React":
        reasons.append("React with Vite gives a fast local frontend starter and aligns with the existing component templates.")
    elif frontend == "HTML/CSS/JavaScript":
        reasons.append("A lightweight HTML/CSS/JavaScript frontend keeps the starter simple when a framework was not clearly required.")
    elif frontend == "None":
        reasons.append("No frontend was selected because the current idea reads like an API, worker, or backend-only service.")

    if database == "SQLite":
        reasons.append("SQLite keeps local setup simple for a runnable starter when the app does not need heavier infrastructure yet.")
    elif database == "PostgreSQL":
        reasons.append("PostgreSQL is a strong default when the app looks multi-user, relational, or production-oriented.")
    elif database == "None":
        reasons.append("No database was selected because the current idea can start without persistent storage.")

    if ai_tools == "Ollama":
        reasons.append("Ollama stays the local-first AI default when the idea explicitly suggests AI features.")
    elif ai_tools == "None":
        reasons.append("No AI tool was added because the current idea does not require model integration.")

    if deployment == "Render":
        reasons.append("Render is the default deployment target because it is simple for web apps and matches the current starter flow.")
    elif deployment == "Docker":
        reasons.append("Docker is selected because the idea or deployment preference points toward containerized delivery.")

    if project_kind["isFullStack"]:
        reasons.append("Both frontend and backend are included because the idea appears to need a user interface and a service layer.")
    return dedupe_list(reasons)


def build_agent_questions(
    idea: str,
    suggested_stack: dict[str, str],
    project_kind: dict[str, Any],
) -> list[dict[str, Any]]:
    lowered = idea.lower()
    questions: list[dict[str, Any]] = []

    def add_question(
        question_id: str,
        question: str,
        question_type: str,
        default: str,
        reason: str,
        options: Sequence[str] | None = None,
    ) -> None:
        questions.append(
            {
                "id": question_id,
                "question": question,
                "type": question_type,
                "options": list(options or []),
                "default": default,
                "reason": reason,
            }
        )

    has_explicit_frontend = bool(match_keyword("frontend", lowered))
    has_explicit_backend = bool(match_keyword("backend", lowered))

    if not infer_declared_project_type(idea) and not (has_explicit_frontend and has_explicit_backend):
        add_question(
            "project_scope",
            "Do you need both a frontend and a backend for this project?",
            "choice",
            project_kind["label"],
            "If you leave this blank, I will suggest the current project scope because it keeps the runnable starter aligned with the folders and runtime scripts your idea appears to need.",
            ["full-stack", "backend-only", "frontend-only"],
        )

    if not match_keyword("backend", lowered) and project_kind["hasBackend"]:
        add_question(
            "backend_framework",
            "Which backend framework do you prefer?",
            "choice",
            suggested_stack["backend"],
            f"If you leave this blank, I will suggest {suggested_stack['backend']} because it matches the current starter templates and keeps backend setup straightforward.",
            ["FastAPI", "Flask", "Express", "NestJS", "Spring Boot"],
        )

    if not match_keyword("frontend", lowered) and project_kind["hasFrontend"]:
        add_question(
            "frontend_framework",
            "Which frontend framework should the starter use?",
            "choice",
            suggested_stack["frontend"],
            f"If you leave this blank, I will suggest {suggested_stack['frontend']} because it gives the cleanest starter structure for the current UI needs.",
            ["React", "Next.js", "Vue", "HTML/CSS/JavaScript", "None"],
        )

    if not match_keyword("language", lowered) and not match_keyword("backend", lowered):
        add_question(
            "language",
            "Do you want to lock in a preferred programming language?",
            "choice",
            suggested_stack["language"],
            f"If you leave this blank, I will suggest {suggested_stack['language']} because it fits the recommended stack and keeps the starter consistent.",
            ["Python", "JavaScript", "TypeScript", "Java"],
        )

    if project_kind["hasBackend"] and not match_keyword("database", lowered):
        add_question(
            "database",
            "Which database should the starter prepare for?",
            "choice",
            suggested_stack["database"],
            f"If you leave this blank, I will suggest {suggested_stack['database']} because it gives the best balance of simple setup and starter reliability for this project.",
            ["SQLite", "PostgreSQL", "MySQL", "MongoDB", "None"],
        )

    if project_kind["hasBackend"] and not _context_mentions_any(lowered, ("auth", "authentication", "login", "signup", "jwt")):
        add_question(
            "authentication",
            "Do you need authentication in the starter?",
            "boolean",
            "No",
            "If you leave this blank, I will suggest No so the starter stays simpler unless your app clearly needs protected user flows.",
            ["Yes", "No"],
        )

    if project_kind["hasBackend"] and not _context_mentions_any(lowered, ("role", "roles", "permissions", "admin", "staff", "manager")):
        add_question(
            "user_roles",
            "Do you need multiple user roles?",
            "choice",
            "Single user / no roles",
            "If you leave this blank, I will suggest a single-user or no-roles setup so the starter stays easy to run before you add permissions logic.",
            ["Single user / no roles", "Basic roles (admin + user)", "Custom roles needed"],
        )

    if project_kind["hasFrontend"] and not _context_mentions_any(lowered, ("admin dashboard", "admin panel", "back office")):
        add_question(
            "admin_dashboard",
            "Should the starter include an admin dashboard path?",
            "boolean",
            "No",
            "If you leave this blank, I will suggest No so the starter focuses on the main product flow before adding admin-specific pages.",
            ["Yes", "No"],
        )

    if project_kind["hasBackend"] and not _context_mentions_any(lowered, ("email", "smtp", "mail", "newsletter", "verification email", "contact form")):
        add_question(
            "email_smtp",
            "Will the project need email or SMTP support?",
            "boolean",
            "No",
            "If you leave this blank, I will suggest No so the starter avoids extra SMTP setup unless email is part of the core workflow.",
            ["Yes", "No"],
        )

    if project_kind["hasBackend"] and not _context_mentions_any(lowered, ("payment", "payments", "stripe", "checkout", "subscription", "billing")):
        add_question(
            "payment_system",
            "Will the project need payment processing?",
            "boolean",
            "No",
            "If you leave this blank, I will suggest No so the starter avoids payment keys and billing setup unless your app clearly needs them.",
            ["Yes", "No"],
        )

    if project_kind["hasBackend"] and not _context_mentions_any(lowered, ("api", "integration", "webhook", "third-party", "external service")):
        add_question(
            "external_apis",
            "Do you already know any external APIs or third-party services this project must connect to?",
            "text",
            "None",
            "If you leave this blank, I will assume no required external APIs so the starter stays self-contained and quicker to run locally.",
        )

    if (
        not match_keyword("aiTools", lowered)
        and _context_mentions_any(
            lowered,
            ("ai", "assistant", "agent", "chatbot", "llm", "openai", "ollama", "summar", "recommendation"),
        )
    ):
        add_question(
            "ai_integration",
            "Do you want AI integration in the starter?",
            "choice",
            "None",
            "If you leave this blank, I will suggest no AI integration unless the idea clearly needs model tooling and provider keys.",
            ["None", "Ollama", "OpenAI API", "LangChain"],
        )

    if not match_keyword("deployment", lowered):
        add_question(
            "deployment_target",
            "Where do you expect to deploy this project first?",
            "choice",
            suggested_stack["deployment"],
            f"If you leave this blank, I will suggest {suggested_stack['deployment']} because it matches the current deployment defaults and generated starter files.",
            ["Render", "Railway", "Vercel", "Docker", "None"],
        )

    if len(re.findall(r"[A-Za-z0-9]+", idea)) < 18:
        add_question(
            "complexity_level",
            "How ambitious should the first starter be?",
            "choice",
            "Standard",
            "If you leave this blank, I will suggest Standard so the starter includes useful structure without becoming heavy too early.",
            ["Simple", "Standard", "Advanced"],
        )

    return questions


def compute_agent_confidence(
    idea: str,
    detected_choices: list[str],
    questions: list[dict[str, Any]],
    project_kind: dict[str, Any],
) -> int:
    score = 35
    word_count = len(re.findall(r"[A-Za-z0-9]+", idea))
    if word_count >= 12:
        score += 15
    elif word_count >= 6:
        score += 8

    score += min(len(detected_choices) * 8, 32)
    if infer_declared_project_type(idea):
        score += 8
    if project_kind["isFullStack"]:
        score += 4
    score -= min(len(questions) * 3, 24)
    return max(20, min(95, score))


def normalize_agent_answers(answers: Mapping[str, Any] | None) -> dict[str, str]:
    if not isinstance(answers, Mapping):
        return {}
    normalized: dict[str, str] = {}
    for key, value in answers.items():
        question_id = str(key).strip()
        normalized[question_id] = normalize_agent_answer_value(question_id, value)
    return normalized


def apply_agent_answers_to_stack(
    idea: str,
    selected_stack: dict[str, str],
    answers: Mapping[str, str],
) -> dict[str, str]:
    resolved = normalize_stack_selection(selected_stack)
    scope = normalize_project_scope(
        answers.get("project_scope", infer_declared_project_type(idea) or determine_project_kind(resolved)["label"])
    )
    scope_value = scope.strip().lower()

    frontend_choice = supported_answer(answers.get("frontend_framework"), SUPPORTED_FRONTEND_VALUES)
    backend_choice = supported_answer(answers.get("backend_framework"), SUPPORTED_BACKEND_VALUES)
    language_choice = supported_answer(answers.get("language"), SUPPORTED_LANGUAGE_VALUES)
    database_choice = supported_answer(answers.get("database"), SUPPORTED_DATABASE_VALUES)
    deployment_choice = supported_answer(answers.get("deployment_target"), SUPPORTED_DEPLOYMENT_VALUES)
    ai_choice = supported_answer(answers.get("ai_integration"), SUPPORTED_AI_VALUES)

    if frontend_choice:
        resolved["frontend"] = frontend_choice
    if backend_choice:
        resolved["backend"] = backend_choice
    if language_choice:
        resolved["language"] = language_choice
    if database_choice:
        resolved["database"] = database_choice
    if deployment_choice:
        resolved["deployment"] = deployment_choice
    if ai_choice:
        resolved["aiTools"] = ai_choice

    if scope_value == "backend-only":
        resolved["frontend"] = "None"
        if resolved["backend"] in NONE_LIKE:
            resolved["backend"] = infer_backend(idea.lower())
    elif scope_value == "frontend-only":
        resolved["backend"] = "None"
        resolved["database"] = "None"
        if resolved["frontend"] in NONE_LIKE:
            resolved["frontend"] = infer_frontend(idea.lower(), "None")
    else:
        if resolved["frontend"] in NONE_LIKE:
            resolved["frontend"] = "React"
        if resolved["backend"] in NONE_LIKE:
            resolved["backend"] = "FastAPI"
        if resolved["database"] in {"Auto", ""}:
            resolved["database"] = infer_database(idea.lower(), resolved["backend"])

    if resolved["language"] in {"Auto", ""}:
        resolved["language"] = infer_language(idea.lower(), resolved["frontend"], resolved["backend"])
    return resolved


def build_final_requirements_summary(
    idea: str,
    answers: Mapping[str, str],
    selected_stack: dict[str, str],
    project_kind: dict[str, Any],
) -> str:
    lines = [
        f"Original idea: {idea.strip()}",
        f"Project scope: {project_kind['label']}",
        "Selected stack:",
        f"- Language: {selected_stack['language']}",
        f"- Frontend: {selected_stack['frontend']}",
        f"- Backend: {selected_stack['backend']}",
        f"- Database: {selected_stack['database']}",
        f"- AI / Tools: {selected_stack['aiTools']}",
        f"- Deployment: {selected_stack['deployment']}",
    ]

    details: list[str] = []
    if _is_yes_answer(answers.get("authentication")):
        details.append("Include authentication-ready structure and required secrets.")
    if _is_yes_answer(answers.get("admin_dashboard")):
        details.append("Include admin-oriented navigation or management surface in the starter.")
    if _is_yes_answer(answers.get("email_smtp")):
        details.append("Prepare SMTP/email configuration and service wiring.")
    if _is_yes_answer(answers.get("payment_system")):
        details.append("Prepare payment provider integration points and required keys.")
    if answers.get("user_roles") and answers.get("user_roles") != "Single user / no roles":
        details.append(f"Support user access model: {answers['user_roles']}.")
    if answers.get("external_apis") and answers.get("external_apis").lower() not in {"", "none", "no"}:
        details.append(f"Prepare integration boundaries for: {answers['external_apis']}.")
    if answers.get("complexity_level"):
        details.append(f"Target complexity level: {answers['complexity_level']}.")
    if answers.get("ai_integration") and answers.get("ai_integration") not in {"", "None"}:
        details.append(f"Prepare AI integration around: {answers['ai_integration']}.")
    details.extend(build_unmapped_preference_notes(answers, selected_stack))

    if details:
        lines.append("Architectural requirements:")
        lines.extend(f"- {item}" for item in details)
    else:
        lines.append("Architectural requirements:")
        lines.append("- Use the suggested defaults for unspecified decisions and keep the starter runnable-first.")

    return "\n".join(lines).strip()


def build_agent_finalize_assumptions(
    answers: Mapping[str, str],
    selected_stack: dict[str, str],
    project_kind: dict[str, Any],
) -> list[str]:
    assumptions = [
        "Final requirements were synthesized from the original idea, your answers, and the selected stack.",
        "Suggested defaults were used only where you left a question blank, so generation can continue without extra back-and-forth.",
    ]
    if project_kind["isFullStack"]:
        assumptions.append("Frontend and backend will both be generated in separate folders.")
    if _is_yes_answer(answers.get("authentication")):
        assumptions.append("Authentication-related secrets and starter environment variables will be included where needed.")
    if selected_stack.get("deployment") == "Docker":
        assumptions.append("Docker-oriented deployment files will be included where the starter templates support them.")
    return dedupe_list(assumptions)


def _is_yes_answer(value: str | None) -> bool:
    return str(value or "").strip().lower() in YES_ANSWERS


def normalize_agent_answer_value(question_id: str, value: Any) -> str:
    text = str(value or "").strip()
    if not text:
        return ""

    lowered = text.lower()
    if question_id in BOOLEAN_QUESTION_IDS:
        if lowered in YES_ANSWERS:
            return "Yes"
        if lowered in NO_ANSWERS:
            return "No"

    if question_id == "project_scope":
        return normalize_project_scope(text)

    alias_map = CANONICAL_ANSWER_ALIASES.get(question_id, {})
    if lowered in alias_map:
        return alias_map[lowered]

    if question_id == "user_roles":
        if "admin" in lowered and "user" in lowered:
            return "Basic roles (admin + user)"
        if "custom" in lowered or "manager" in lowered or "permission" in lowered:
            return "Custom roles needed"
        if "single" in lowered or "no role" in lowered:
            return "Single user / no roles"

    if question_id == "external_apis" and lowered in NO_ANSWERS:
        return "None"

    return text


def normalize_project_scope(value: Any) -> str:
    text = str(value or "").strip()
    if not text:
        return ""
    lowered = text.lower()
    return CANONICAL_ANSWER_ALIASES["project_scope"].get(lowered, text)


def supported_answer(value: str | None, supported_values: set[str]) -> str:
    text = str(value or "").strip()
    return text if text in supported_values else ""


def build_unmapped_preference_notes(
    answers: Mapping[str, str],
    selected_stack: Mapping[str, str],
) -> list[str]:
    notes: list[str] = []
    mappings = [
        ("language", "Language", SUPPORTED_LANGUAGE_VALUES, "language"),
        ("frontend_framework", "Frontend framework", SUPPORTED_FRONTEND_VALUES, "frontend"),
        ("backend_framework", "Backend framework", SUPPORTED_BACKEND_VALUES, "backend"),
        ("database", "Database", SUPPORTED_DATABASE_VALUES, "database"),
        ("ai_integration", "AI integration", SUPPORTED_AI_VALUES, "aiTools"),
        ("deployment_target", "Deployment target", SUPPORTED_DEPLOYMENT_VALUES, "deployment"),
    ]

    for answer_key, label, supported_values, stack_key in mappings:
        raw_value = str(answers.get(answer_key, "")).strip()
        if not raw_value or raw_value in supported_values:
            continue
        notes.append(
            f"{label} preference noted: {raw_value}. The runnable starter keeps {selected_stack.get(stack_key, 'the suggested default')} as the safe template-backed default for now."
        )
    return notes


def preview_budget_seconds(generation_mode: str) -> float:
    return FAST_PLANNER_TIMEOUT_SECONDS if generation_mode == "fast" else DEEP_PREVIEW_TIMEOUT_SECONDS


def validate_planner_payload(payload: Any) -> dict[str, Any]:
    if not isinstance(payload, Mapping):
        raise ValueError("Planner response was not a JSON object.")

    missing_keys = sorted(REQUIRED_PLANNER_KEYS.difference(payload.keys()))
    if missing_keys:
        raise ValueError(f"Planner response was partial and missed required keys: {', '.join(missing_keys)}")

    if not isinstance(payload.get("selectedStack"), Mapping):
        raise ValueError("Planner response did not include a valid selectedStack object.")
    if not isinstance(payload.get("modules"), Sequence) or isinstance(payload.get("modules"), (str, bytes, bytearray)):
        raise ValueError("Planner response did not include a valid modules list.")
    if not isinstance(payload.get("packageRequirements"), Sequence) or isinstance(payload.get("packageRequirements"), (str, bytes, bytearray)):
        raise ValueError("Planner response did not include a valid packageRequirements list.")
    if not isinstance(payload.get("installCommands"), Sequence) or isinstance(payload.get("installCommands"), (str, bytes, bytearray)):
        raise ValueError("Planner response did not include a valid installCommands list.")
    if not isinstance(payload.get("runCommands"), Sequence) or isinstance(payload.get("runCommands"), (str, bytes, bytearray)):
        raise ValueError("Planner response did not include a valid runCommands list.")
    if not isinstance(payload.get("requiredInputs"), Sequence) or isinstance(payload.get("requiredInputs"), (str, bytes, bytearray)):
        raise ValueError("Planner response did not include a valid requiredInputs list.")
    if not isinstance(payload.get("envVariables"), Sequence) or isinstance(payload.get("envVariables"), (str, bytes, bytearray)):
        raise ValueError("Planner response did not include a valid envVariables list.")
    if not isinstance(payload.get("customFiles"), Sequence) or isinstance(payload.get("customFiles"), (str, bytes, bytearray)):
        raise ValueError("Planner response did not include a valid customFiles list.")

    normalized_payload = dict(payload)
    normalized_payload["customFiles"] = list(payload.get("customFiles", []))[:MAX_CUSTOM_FILES]
    return normalized_payload


def normalize_preview(
    raw_preview: Mapping[str, Any] | None,
    idea: str,
    requested_stack: dict[str, str] | None = None,
    generation_mode: str = "fast",
    requirements_context: str | None = None,
) -> dict[str, Any]:
    raw = dict(raw_preview or {})
    mode = normalize_generation_mode(generation_mode)
    requested = normalize_stack_selection(requested_stack)
    planning_context = requirements_context or idea
    detected_choices = dedupe_list(
        normalize_string_list(raw.get("detectedUserChoices")) or detect_user_choices(idea)
    )
    selected_stack = resolve_selected_stack(
        idea=idea,
        requested_stack=requested,
        model_stack=raw.get("selectedStack"),
        detected_choices=detected_choices,
    )
    project_kind = determine_project_kind(selected_stack, raw.get("projectType"))
    project_name = clean_project_name(raw.get("projectName"), idea)

    modules = merge_modules(
        normalize_modules(raw.get("modules")),
        build_default_modules(selected_stack, project_kind),
    )
    required_inputs = merge_required_inputs(
        normalize_required_inputs(raw.get("requiredInputs")),
        build_required_inputs(planning_context, selected_stack, project_kind, modules),
    )
    env_variables = merge_env_variables(
        normalize_env_variables(raw.get("envVariables")),
        required_inputs_to_env_variables(required_inputs),
    )
    package_requirements = dedupe_list(
        normalize_string_list(raw.get("packageRequirements"))
        + build_package_requirements(selected_stack, project_kind)
    )
    install_commands = dedupe_list(
        normalize_string_list(raw.get("installCommands"))
        + build_install_commands(selected_stack, project_kind)
    )
    run_commands = dedupe_list(
        normalize_string_list(raw.get("runCommands"))
        + build_run_commands(selected_stack, project_kind)
    )

    custom_manifest = normalize_custom_manifest(raw.get("customFiles"), selected_stack, project_kind)
    validated_files = finalize_preview_files(
        project_name=project_name,
        selected_stack=selected_stack,
        project_kind=project_kind,
        custom_manifest=custom_manifest,
        raw_files=raw.get("files"),
    )

    summary = (
        str(raw.get("summary") or "").strip()
        or build_summary(project_name, project_kind, selected_stack, mode)
    )
    problem_statement = (
        str(raw.get("problemStatement") or "").strip()
        or idea.strip()
        or f"Build a starter project for {project_name}."
    )
    assumptions = dedupe_list(
        normalize_string_list(raw.get("assumptions"))
        + build_assumptions(selected_stack, project_kind, requested, mode, bool(custom_manifest))
    )
    architecture = dedupe_list(
        normalize_string_list(raw.get("architecture"))
        + build_architecture(selected_stack, project_kind)
    )
    chosen_stack = build_chosen_stack(selected_stack)

    file_tree = build_preview_file_tree(validated_files, include_env_example=bool(env_variables))

    return {
        "projectName": project_name,
        "detectedUserChoices": detected_choices,
        "selectedStack": selected_stack,
        "chosenStack": chosen_stack,
        "assumptions": assumptions,
        "summary": summary,
        "problemStatement": problem_statement,
        "architecture": architecture,
        "modules": modules,
        "packageRequirements": package_requirements,
        "installCommands": install_commands,
        "runCommands": run_commands,
        "requiredInputs": required_inputs,
        "envVariables": env_variables,
        "fileTree": file_tree,
        "files": validated_files,
    }


def apply_custom_file_overrides(
    preview: dict[str, Any], custom_files: list[dict[str, str]]
) -> dict[str, Any]:
    merged_files = merge_file_entries(preview.get("files", []), custom_files)
    selected_stack = normalize_stack_selection(preview.get("selectedStack"))
    project_kind = determine_project_kind(selected_stack)
    validated_files = finalize_preview_files(
        project_name=str(preview.get("projectName") or "Generated Project"),
        selected_stack=selected_stack,
        project_kind=project_kind,
        raw_files=merged_files,
    )
    env_variables = normalize_env_variables(preview.get("envVariables"))

    preview["files"] = validated_files
    preview["fileTree"] = build_preview_file_tree(
        validated_files,
        include_env_example=bool(env_variables),
    )
    return preview


def normalize_generation_mode(value: Any) -> str:
    return "deep" if str(value or "").strip().lower() == "deep" else "fast"


def remaining_time(deadline: float) -> float:
    return max(0.0, deadline - time.monotonic())


def normalize_stack_selection(selection: Any) -> dict[str, str]:
    data = selection if isinstance(selection, Mapping) else {}
    normalized: dict[str, str] = {}
    for field in STACK_FIELDS:
        value = str(data.get(field) or STACK_DEFAULTS[field]).strip()
        normalized[field] = value or STACK_DEFAULTS[field]
    return normalized


def resolve_selected_stack(
    idea: str,
    requested_stack: dict[str, str],
    model_stack: Any,
    detected_choices: list[str],
) -> dict[str, str]:
    model_selection = normalize_stack_selection(model_stack)
    resolved = normalize_stack_selection(requested_stack)
    lowered_idea = idea.lower()

    backend = pick_stack_value(
        requested_stack.get("backend"),
        model_selection.get("backend"),
        infer_backend(lowered_idea),
    )
    frontend = pick_stack_value(
        requested_stack.get("frontend"),
        model_selection.get("frontend"),
        infer_frontend(lowered_idea, backend),
    )
    language = pick_stack_value(
        requested_stack.get("language"),
        model_selection.get("language"),
        infer_language(lowered_idea, frontend, backend),
    )
    database = pick_stack_value(
        requested_stack.get("database"),
        model_selection.get("database"),
        infer_database(lowered_idea, backend),
    )
    ai_tools = pick_stack_value(
        requested_stack.get("aiTools"),
        model_selection.get("aiTools"),
        infer_ai_tools(lowered_idea),
    )
    deployment = pick_stack_value(
        requested_stack.get("deployment"),
        model_selection.get("deployment"),
        infer_deployment(lowered_idea),
    )

    resolved.update(
        {
            "language": language,
            "frontend": frontend,
            "backend": backend,
            "database": database,
            "aiTools": ai_tools,
            "deployment": deployment,
        }
    )

    if not detected_choices and all(value == "Auto" for value in requested_stack.values()):
        resolved["backend"] = resolved["backend"] if resolved["backend"] != "Auto" else "FastAPI"
        resolved["frontend"] = resolved["frontend"] if resolved["frontend"] != "Auto" else "React"
        resolved["language"] = resolved["language"] if resolved["language"] != "Auto" else "Python"
        resolved["database"] = resolved["database"] if resolved["database"] != "Auto" else "SQLite"
        resolved["aiTools"] = resolved["aiTools"] if resolved["aiTools"] != "Auto" else "None"
        resolved["deployment"] = resolved["deployment"] if resolved["deployment"] != "Auto" else "Render"

    return resolved


def pick_stack_value(requested: str | None, suggested: str | None, inferred: str) -> str:
    for candidate in (requested, suggested):
        if candidate is not None and str(candidate).strip() not in {"", "Auto"}:
            return str(candidate).strip()
    return inferred


def infer_language(idea: str, frontend: str, backend: str) -> str:
    detected = match_keyword("language", idea)
    if detected:
        return detected
    if backend in {"FastAPI", "Flask"}:
        return "Python"
    if backend == "Express":
        return "JavaScript"
    if backend == "NestJS":
        return "TypeScript"
    if backend == "Spring Boot":
        return "Java"
    if frontend in {"React", "Next.js", "Vue", "HTML/CSS/JavaScript"}:
        return "JavaScript"
    return "Python"


def infer_frontend(idea: str, backend: str) -> str:
    detected = match_keyword("frontend", idea)
    if detected:
        return detected
    if any(token in idea for token in ("api only", "backend only", "worker", "cli", "automation")):
        return "None"
    if any(token in idea for token in ("dashboard", "web app", "portal", "browser", "frontend", "website")):
        return "React"
    if backend in {"FastAPI", "Express", "NestJS", "Spring Boot"}:
        return "React"
    return "None"


def infer_backend(idea: str) -> str:
    detected = match_keyword("backend", idea)
    if detected:
        return detected
    if any(token in idea for token in ("frontend only", "static site", "landing page only")):
        return "None"
    if "spring" in idea or "java" in idea:
        return "Spring Boot"
    if "node" in idea or "express" in idea:
        return "Express"
    return "FastAPI"


def infer_database(idea: str, backend: str) -> str:
    detected = match_keyword("database", idea)
    if detected:
        return detected
    if any(token in idea for token in ("no database", "without database", "in memory")):
        return "None"
    if backend in {"FastAPI", "Flask"}:
        return "SQLite"
    if backend in {"Express", "NestJS", "Spring Boot"}:
        return "PostgreSQL"
    return "None"


def infer_ai_tools(idea: str) -> str:
    detected = match_keyword("aiTools", idea)
    if detected:
        return detected
    if any(token in idea for token in ("ai", "assistant", "agent", "generator", "chatbot", "llm")):
        return "Ollama"
    return "None"


def infer_deployment(idea: str) -> str:
    detected = match_keyword("deployment", idea)
    return detected or "Render"


def match_keyword(category: str, idea: str) -> str:
    for value, keywords in KEYWORD_MAP[category].items():
        if any(keyword in idea for keyword in keywords):
            return value
    return ""


def detect_user_choices(idea: str) -> list[str]:
    lowered = idea.lower()
    detected: list[str] = []
    labels = {
        "language": "Language",
        "frontend": "Frontend",
        "backend": "Backend",
        "database": "Database",
        "aiTools": "AI / Tools",
        "deployment": "Deployment",
    }
    for category, mapping in KEYWORD_MAP.items():
        for value, keywords in mapping.items():
            if any(keyword in lowered for keyword in keywords):
                detected.append(f"{labels[category]}: {value}")
                break
    return dedupe_list(detected)


def determine_project_kind(
    selected_stack: dict[str, str],
    declared_type: Any = None,
) -> dict[str, Any]:
    declared = str(declared_type or "").strip().lower()
    frontend = selected_stack.get("frontend", "None")
    backend = selected_stack.get("backend", "None")
    has_frontend = frontend not in NONE_LIKE
    has_backend = backend not in NONE_LIKE

    if declared == "frontend-only":
        has_backend = False
    elif declared == "backend-only":
        has_frontend = False
    elif declared == "full-stack":
        has_frontend = True
        has_backend = True

    is_full_stack = has_frontend and has_backend
    if is_full_stack:
        label = "full-stack"
        minimum_files = 15
    elif has_backend:
        label = "backend-only"
        minimum_files = 8
    else:
        label = "frontend-only"
        minimum_files = 6

    return {
        "hasFrontend": has_frontend,
        "hasBackend": has_backend,
        "isFullStack": is_full_stack,
        "label": label,
        "minimumFiles": minimum_files,
    }


def clean_project_name(raw_name: Any, idea: str) -> str:
    if isinstance(raw_name, str) and raw_name.strip():
        return raw_name.strip()[:80]

    words = re.findall(r"[A-Za-z0-9]+", idea)
    if not words:
        return "Project Agent Output"
    return " ".join(word.capitalize() for word in words[:4])


def build_summary(
    project_name: str,
    project_kind: dict[str, Any],
    selected_stack: dict[str, str],
    generation_mode: str,
) -> str:
    stack_bits = []
    if project_kind["hasFrontend"]:
        stack_bits.append(selected_stack["frontend"])
    if project_kind["hasBackend"]:
        stack_bits.append(selected_stack["backend"])
    stack_summary = " + ".join(bit for bit in stack_bits if bit not in NONE_LIKE) or selected_stack["language"]
    mode_label = "Fast Mode" if generation_mode == "fast" else "Deep Mode"
    return (
        f"{project_name} is a 100% runnable starter project built around {stack_summary}. "
        f"{mode_label} uses backend templates for all standard project structure so output quality stays complete while generation remains fast."
    )


def build_assumptions(
    selected_stack: dict[str, str],
    project_kind: dict[str, Any],
    requested_stack: dict[str, str],
    generation_mode: str,
    has_custom_manifest: bool,
) -> list[str]:
    assumptions: list[str] = []
    if all(value == "Auto" for value in requested_stack.values()):
        assumptions.append(
            "Some stack choices were inferred automatically to provide a complete beginner-friendly starter."
        )
    if generation_mode == "fast":
        assumptions.append(
            "Fast Mode used backend templates for standard files so project completeness was preserved without waiting for the model to write boilerplate."
        )
    else:
        assumptions.append(
            "Deep Mode keeps the same complete standard structure as Fast Mode and uses extra model time only for project-specific custom files."
        )
    if project_kind["isFullStack"]:
        assumptions.append("The project is split into frontend and backend folders to keep the full-stack boundary explicit.")
    assumptions.append(
        "This output is a 100% runnable starter project after `.env` is filled, setup is run, and the run script is started. Business logic can still be customized afterward."
    )
    if selected_stack.get("database") == "SQLite":
        assumptions.append("SQLite was chosen as a lightweight local-first database default.")
    if has_custom_manifest:
        assumptions.append("Project-specific custom files were layered on top of the standard stack templates.")
    return assumptions


def build_architecture(
    selected_stack: dict[str, str],
    project_kind: dict[str, Any],
) -> list[str]:
    architecture: list[str] = []
    if project_kind["hasFrontend"]:
        architecture.append(
            f"{selected_stack['frontend']} handles the user-facing workflows, starter pages, and client-side integration points."
        )
    if project_kind["hasBackend"]:
        architecture.append(
            f"{selected_stack['backend']} provides the API surface, routing, services, and configuration layer."
        )
    if selected_stack.get("database") not in NONE_LIKE:
        architecture.append(
            f"{selected_stack['database']} is configured as the primary persistence layer through environment-driven settings."
        )
    if selected_stack.get("aiTools") not in NONE_LIKE:
        architecture.append(
            f"{selected_stack['aiTools']} integration is isolated behind service boundaries so model/provider settings can evolve independently."
        )
    architecture.append("Setup, run scripts, and dependency manifests are generated server-side for a consistent starter layout.")
    return architecture


def build_default_modules(
    selected_stack: dict[str, str],
    project_kind: dict[str, Any],
) -> list[dict[str, Any]]:
    modules: list[dict[str, Any]] = []
    if project_kind["hasFrontend"]:
        base = "frontend/" if project_kind["isFullStack"] else ""
        modules.append(
            {
                "name": "Frontend Experience",
                "purpose": "Provides the main user interface, starter pages, and client-side state or API hooks.",
                "keyFiles": [
                    f"{base}src/App.jsx" if selected_stack["frontend"] in {"React", "Next.js", "Vue"} else f"{base}index.html",
                    f"{base}src/pages/HomePage.jsx" if selected_stack["frontend"] in {"React", "Next.js", "Vue"} else f"{base}src/views/home.js",
                    f"{base}src/services/api.js",
                ],
            }
        )
    if project_kind["hasBackend"]:
        base = "backend/" if project_kind["isFullStack"] else ""
        if selected_stack["backend"] in {"FastAPI", "Flask"}:
            key_files = [f"{base}app/main.py", f"{base}app/routers/items.py", f"{base}app/services/item_service.py"]
        elif selected_stack["backend"] in {"Express", "NestJS"}:
            key_files = [f"{base}server.js", f"{base}src/routes/items.js", f"{base}src/services/itemService.js"]
        else:
            key_files = [f"{base}pom.xml", f"{base}src/main/java/com/example/demo/service/AppService.java"]
        modules.append(
            {
                "name": "Backend API",
                "purpose": "Provides the routes, services, and integration-ready backend surface.",
                "keyFiles": key_files,
            }
        )
    if selected_stack.get("database") not in NONE_LIKE:
        base = "backend/" if project_kind["isFullStack"] else ""
        modules.append(
            {
                "name": "Persistence Layer",
                "purpose": "Supplies data models, configuration, and starter persistence wiring.",
                "keyFiles": [
                    f"{base}app/database.py" if selected_stack["backend"] in {"FastAPI", "Flask"} else f"{base}src/models/itemModel.js"
                ],
            }
        )
    return modules


def build_package_requirements(
    selected_stack: dict[str, str],
    project_kind: dict[str, Any],
) -> list[str]:
    packages: list[str] = []
    if project_kind["hasBackend"]:
        backend = selected_stack["backend"]
        database = selected_stack["database"]
        if backend == "FastAPI":
            packages.extend(
                ["fastapi", "uvicorn[standard]", "pydantic", "pydantic-settings", "sqlalchemy", "python-dotenv"]
            )
            if database == "SQLite":
                packages.append("aiosqlite")
            elif database == "PostgreSQL":
                packages.append("psycopg[binary]")
        elif backend == "Flask":
            packages.extend(["flask", "python-dotenv", "sqlalchemy"])
        elif backend in {"Express", "NestJS"}:
            packages.extend(["express", "cors", "dotenv"])
            if database == "PostgreSQL":
                packages.append("pg")
            elif database == "MongoDB":
                packages.append("mongoose")
        elif backend == "Spring Boot":
            packages.extend(
                [
                    "org.springframework.boot:spring-boot-starter-web",
                    "org.springframework.boot:spring-boot-starter-data-jpa",
                ]
            )
    if project_kind["hasFrontend"]:
        frontend = selected_stack["frontend"]
        if frontend in {"React", "Next.js", "Vue"}:
            packages.extend(["react", "react-dom", "vite"])
        elif frontend == "HTML/CSS/JavaScript":
            packages.append("vite")
    ai_tools = selected_stack.get("aiTools", "None")
    if ai_tools == "Ollama":
        packages.append("ollama")
    elif ai_tools == "LangChain":
        packages.append("langchain")
    return dedupe_list(packages)


def build_install_commands(
    selected_stack: dict[str, str],
    project_kind: dict[str, Any],
) -> list[str]:
    if project_kind["isFullStack"]:
        return ["setup.bat", "./setup.sh"]
    if project_kind["hasBackend"] and selected_stack["backend"] == "Spring Boot":
        return ["setup.bat", "./setup.sh", "mvn install"]
    if project_kind["hasFrontend"] and not project_kind["hasBackend"]:
        return ["setup.bat", "./setup.sh", "npm install"]
    return ["setup.bat", "./setup.sh"]


def build_run_commands(
    selected_stack: dict[str, str],
    project_kind: dict[str, Any],
) -> list[str]:
    commands = ["run.bat", "./run.sh"]
    if project_kind["isFullStack"]:
        return commands
    if project_kind["hasBackend"]:
        backend = selected_stack["backend"]
        if backend in {"FastAPI", "Flask"}:
            commands.append("python -m uvicorn app.main:app --reload")
        elif backend in {"Express", "NestJS"}:
            commands.append("npm start")
        elif backend == "Spring Boot":
            commands.append("mvn spring-boot:run")
    elif project_kind["hasFrontend"]:
        commands.append("npm run dev")
    return commands


def build_required_inputs(
    idea: str,
    selected_stack: dict[str, str],
    project_kind: dict[str, Any],
    modules: list[dict[str, Any]],
) -> list[dict[str, Any]]:
    context_parts = [idea]
    for module in modules:
        context_parts.append(str(module.get("name") or ""))
        context_parts.append(str(module.get("purpose") or ""))
        context_parts.extend(normalize_string_list(module.get("keyFiles")))
    lowered = " ".join(part.lower() for part in context_parts if part)

    required_inputs: list[dict[str, Any]] = []
    if project_kind["hasBackend"]:
        required_inputs.append(
            {
                "name": "APP_ENV",
                "required": False,
                "example": "development",
                "whereToAdd": ".env",
                "purpose": "Application environment name.",
            }
        )

    database = selected_stack.get("database")
    if database == "SQLite":
        required_inputs.append(
            {
                "name": "DATABASE_URL",
                "required": True,
                "example": "sqlite:///./app.db",
                "whereToAdd": ".env",
                "purpose": "SQLite connection string for local development.",
            }
        )
    elif database in {"PostgreSQL", "MySQL"}:
        example = (
            "postgresql://postgres:postgres@localhost:5432/app_db"
            if database == "PostgreSQL"
            else "mysql://root:password@localhost:3306/app_db"
        )
        required_inputs.append(
            {
                "name": "DATABASE_URL",
                "required": True,
                "example": example,
                "whereToAdd": ".env",
                "purpose": f"{database} connection string for the backend database.",
            }
        )
    elif database == "MongoDB":
        required_inputs.append(
            {
                "name": "MONGODB_URI",
                "required": True,
                "example": "mongodb://localhost:27017/app_db",
                "whereToAdd": ".env",
                "purpose": "MongoDB connection string for the backend database.",
            }
        )

    if _context_mentions_any(lowered, ("auth", "authentication", "login", "signup", "jwt", "token", "session")):
        required_inputs.append(
            {
                "name": "JWT_SECRET",
                "required": True,
                "example": "change-me-super-secret",
                "whereToAdd": ".env",
                "purpose": "Secret used to sign authentication tokens.",
            }
        )

    if _context_mentions_any(lowered, ("email", "smtp", "mail", "newsletter", "verification email", "contact form")):
        required_inputs.extend(
            [
                {
                    "name": "SMTP_HOST",
                    "required": True,
                    "example": "smtp.gmail.com",
                    "whereToAdd": ".env",
                    "purpose": "SMTP email server host.",
                },
                {
                    "name": "SMTP_PORT",
                    "required": True,
                    "example": "587",
                    "whereToAdd": ".env",
                    "purpose": "SMTP email server port.",
                },
                {
                    "name": "SMTP_USER",
                    "required": True,
                    "example": "notifications@example.com",
                    "whereToAdd": ".env",
                    "purpose": "SMTP account username or sender login.",
                },
                {
                    "name": "SMTP_PASSWORD",
                    "required": True,
                    "example": "app-password",
                    "whereToAdd": ".env",
                    "purpose": "SMTP account password or app password.",
                },
            ]
        )

    if _context_mentions_any(lowered, ("payment", "payments", "stripe", "checkout", "billing", "subscription")):
        key_name = "STRIPE_SECRET_KEY" if "stripe" in lowered else "PAYMENT_SECRET_KEY"
        required_inputs.append(
            {
                "name": key_name,
                "required": True,
                "example": "sk_test_1234567890",
                "whereToAdd": ".env",
                "purpose": "Secret key for payment provider API requests.",
            }
        )

    ai_tools = selected_stack.get("aiTools")
    if ai_tools == "Ollama" or _context_mentions_any(lowered, ("ollama", "local llm", "qwen", "llama")):
        required_inputs.extend(
            [
                {
                    "name": "OLLAMA_BASE_URL",
                    "required": True,
                    "example": "http://localhost:11434",
                    "whereToAdd": ".env",
                    "purpose": "Base URL for the local Ollama server.",
                },
                {
                    "name": "OLLAMA_MODEL",
                    "required": True,
                    "example": "qwen2.5-coder:latest",
                    "whereToAdd": ".env",
                    "purpose": "Ollama model name to use for local inference.",
                },
            ]
        )
    if ai_tools == "OpenAI API" or _context_mentions_any(lowered, ("openai", "gpt-4", "gpt", "chatgpt")):
        required_inputs.append(
            {
                "name": "OPENAI_API_KEY",
                "required": True,
                "example": "sk-proj-1234567890",
                "whereToAdd": ".env",
                "purpose": "API key for hosted OpenAI requests.",
            }
        )

    if _context_mentions_any(lowered, ("oauth", "google login", "github login", "social login", "openid")):
        required_inputs.extend(
            [
                {
                    "name": "OAUTH_CLIENT_ID",
                    "required": True,
                    "example": "your-client-id",
                    "whereToAdd": ".env",
                    "purpose": "OAuth client identifier for external sign-in.",
                },
                {
                    "name": "OAUTH_CLIENT_SECRET",
                    "required": True,
                    "example": "your-client-secret",
                    "whereToAdd": ".env",
                    "purpose": "OAuth client secret for external sign-in.",
                },
            ]
        )

    if project_kind["isFullStack"]:
        required_inputs.append(
            {
                "name": "VITE_API_BASE_URL",
                "required": False,
                "example": "http://localhost:8000",
                "whereToAdd": ".env",
                "purpose": "Frontend base URL for backend API calls.",
            }
        )

    return dedupe_required_inputs(required_inputs)


def required_inputs_to_env_variables(
    required_inputs: list[dict[str, Any]],
) -> list[dict[str, str]]:
    env_vars: list[dict[str, str]] = []
    for item in required_inputs:
        name = str(item.get("name") or "").strip()
        if not name:
            continue
        env_vars.append(
            {
                "name": name,
                "value": str(item.get("example") or "").strip(),
                "description": str(item.get("purpose") or "").strip(),
            }
        )
    return env_vars


def normalize_custom_manifest(
    value: Any,
    selected_stack: dict[str, str],
    project_kind: dict[str, Any],
) -> list[dict[str, str]]:
    manifest: list[dict[str, str]] = []
    if isinstance(value, Sequence) and not isinstance(value, (str, bytes, bytearray)):
        for item in value:
            if len(manifest) >= MAX_CUSTOM_FILES:
                break
            if isinstance(item, str):
                path = clean_relative_path(item)
                purpose = "Project-specific custom logic."
            elif isinstance(item, Mapping):
                path = clean_relative_path(item.get("path"))
                purpose = str(item.get("purpose") or item.get("description") or "Project-specific custom logic.").strip()
            else:
                continue
            if not path or is_standard_file_path(path):
                continue
            manifest.append({"path": path, "purpose": purpose})

    if manifest:
        return dedupe_manifest(manifest)

    defaults: list[dict[str, str]] = []
    if project_kind["isFullStack"]:
        if selected_stack["frontend"] not in NONE_LIKE:
            defaults.append(
                {
                    "path": "frontend/src/pages/DashboardPage.jsx",
                    "purpose": "Primary project-specific frontend page for the main user workflow.",
                }
            )
        if selected_stack["backend"] in {"FastAPI", "Flask"}:
            defaults.append(
                {
                    "path": "backend/app/services/domain_service.py",
                    "purpose": "Project-specific backend domain service with starter business rules.",
                }
            )
        elif selected_stack["backend"] in {"Express", "NestJS"}:
            defaults.append(
                {
                    "path": "backend/src/services/domainService.js",
                    "purpose": "Project-specific backend domain service with starter business rules.",
                }
            )
    elif project_kind["hasBackend"]:
        if selected_stack["backend"] in {"FastAPI", "Flask"}:
            defaults.append(
                {
                    "path": "app/services/domain_service.py",
                    "purpose": "Project-specific backend domain service with starter business rules.",
                }
            )
        elif selected_stack["backend"] in {"Express", "NestJS"}:
            defaults.append(
                {
                    "path": "src/services/domainService.js",
                    "purpose": "Project-specific backend domain service with starter business rules.",
                }
            )
        elif selected_stack["backend"] == "Spring Boot":
            defaults.append(
                {
                    "path": "src/main/java/com/example/demo/service/DomainService.java",
                    "purpose": "Project-specific backend service with starter business rules.",
                }
            )
    elif project_kind["hasFrontend"]:
        if selected_stack["frontend"] in {"React", "Next.js", "Vue"}:
            defaults.append(
                {
                    "path": "src/pages/DashboardPage.jsx",
                    "purpose": "Primary project-specific frontend page for the main workflow.",
                }
            )
        else:
            defaults.append(
                {
                    "path": "src/views/dashboard.js",
                    "purpose": "Primary project-specific frontend view for the main workflow.",
                }
            )
    return defaults[:MAX_CUSTOM_FILES]


def build_standard_files(
    project_name: str,
    selected_stack: dict[str, str],
    project_kind: dict[str, Any],
) -> list[dict[str, str]]:
    files: dict[str, str] = {}
    if project_kind["isFullStack"]:
        if project_kind["hasFrontend"]:
            files.update(build_frontend_files(selected_stack["frontend"], project_name, "frontend"))
        if project_kind["hasBackend"]:
            files.update(build_backend_files(selected_stack, project_name, "backend"))
        files.update(build_root_scripts(selected_stack, project_kind))
    elif project_kind["hasBackend"]:
        files.update(build_backend_files(selected_stack, project_name, ""))
        files.update(build_root_scripts(selected_stack, project_kind))
    else:
        files.update(build_frontend_files(selected_stack["frontend"], project_name, ""))
        files.update(build_root_scripts(selected_stack, project_kind))
    return [{"path": path, "content": content} for path, content in files.items()]


def build_custom_template_files(
    manifest: list[dict[str, str]],
    project_name: str,
    selected_stack: dict[str, str],
    project_kind: dict[str, Any],
) -> list[dict[str, str]]:
    files: list[dict[str, str]] = []
    for item in manifest[:MAX_CUSTOM_FILES]:
        path = item["path"]
        purpose = item["purpose"]
        content = build_custom_template_content(path, purpose, project_name, selected_stack, project_kind)
        files.append({"path": path, "content": trim_content_lines(content)})
    return files


def build_custom_template_content(
    path: str,
    purpose: str,
    project_name: str,
    selected_stack: dict[str, str],
    project_kind: dict[str, Any],
) -> str:
    filename = Path(path).name
    stem = Path(path).stem
    pretty_name = stem.replace("_", " ").replace("-", " ").title()
    extension = Path(path).suffix.lower()

    if extension in {".jsx", ".tsx"}:
        if "page" in stem.lower() or "/pages/" in path:
            return f"""const cards = [
  {{
    title: "{pretty_name} Overview",
    detail: "{purpose}"
  }},
  {{
    title: "Starter Workflow",
    detail: "Use this page to connect forms, API requests, and user-facing business actions."
  }}
];

export default function {safe_component_name(stem)}() {{
  return (
    <section className="card">
      <h2>{pretty_name}</h2>
      <p>{purpose}</p>
      <ul>
        {{cards.map((card) => (
          <li key={{card.title}}>
            <strong>{{card.title}}</strong>: {{card.detail}}
          </li>
        ))}}
      </ul>
    </section>
  );
}}
"""
        return f"""export default function {safe_component_name(stem)}() {{
  return (
    <section className="card">
      <h3>{pretty_name}</h3>
      <p>{purpose}</p>
    </section>
  );
}}
"""

    if extension == ".py":
        if "router" in path or "/routers/" in path:
            return f"""from fastapi import APIRouter

router = APIRouter()


@router.get("/")
def read_{safe_python_name(stem)}() -> dict[str, str]:
    return {{
        "message": "{purpose}",
        "project": "{project_name}",
    }}
"""
        if "schema" in path or "/schemas/" in path:
            return f"""from pydantic import BaseModel


class {safe_component_name(stem)}(BaseModel):
    name: str
    description: str = "{purpose}"
"""
        if "model" in path or "/models/" in path:
            return f"""from dataclasses import dataclass


@dataclass
class {safe_component_name(stem)}:
    name: str
    status: str = "ready"
"""
        return f"""def {safe_python_name(stem)}_summary() -> dict[str, str]:
    return {{
        "name": "{pretty_name}",
        "purpose": "{purpose}",
        "project": "{project_name}",
    }}
"""

    if extension in {".js", ".mjs"}:
        if "service" in path.lower():
            return f"""export function get{safe_component_name(stem)}Summary() {{
  return {{
    project: "{project_name}",
    purpose: "{purpose}"
  }};
}}
"""
        if "controller" in path.lower():
            return f"""export function {safe_js_name(stem)}(_req, res) {{
  res.json({{
    project: "{project_name}",
    purpose: "{purpose}"
  }});
}}
"""
        return f"""export const {safe_js_name(stem)} = {{
  project: "{project_name}",
  purpose: "{purpose}"
}};
"""

    if extension == ".java":
        class_name = safe_component_name(stem)
        return f"""package com.example.demo.service;

import org.springframework.stereotype.Service;

@Service
public class {class_name} {{
    public String summary() {{
        return "{purpose}";
    }}
}}
"""

    return f"# {pretty_name}\n\n{purpose}\n"


def ensure_minimum_project_files(
    files: list[dict[str, str]],
    selected_stack: dict[str, str],
    project_kind: dict[str, Any],
) -> list[dict[str, str]]:
    merged = {entry["path"]: entry["content"] for entry in files}
    for path, content in build_root_scripts(selected_stack, project_kind).items():
        merged.setdefault(path, content)

    if project_kind["isFullStack"]:
        for path, content in build_frontend_files(selected_stack["frontend"], "App", "frontend").items():
            merged.setdefault(path, content)
        for path, content in build_backend_files(selected_stack, "App", "backend").items():
            merged.setdefault(path, content)
    elif project_kind["hasBackend"]:
        for path, content in build_backend_files(selected_stack, "App", "").items():
            merged.setdefault(path, content)
    else:
        for path, content in build_frontend_files(selected_stack["frontend"], "App", "").items():
            merged.setdefault(path, content)

    filler_index = 1
    while len(merged) < project_kind["minimumFiles"]:
        filler_path = f"notes/starter-note-{filler_index}.md"
        merged.setdefault(
            filler_path,
            f"# Starter Note {filler_index}\n\nThis file preserves the complete minimum project structure while you continue iterating.\n",
        )
        filler_index += 1

    return [{"path": path, "content": content} for path, content in merged.items()]


def build_backend_files(
    selected_stack: dict[str, str],
    project_name: str,
    prefix: str,
) -> dict[str, str]:
    backend = selected_stack.get("backend", "FastAPI")
    if backend in {"FastAPI", "Flask"}:
        return build_fastapi_backend_files(project_name, prefix)
    if backend in {"Express", "NestJS"}:
        return build_express_backend_files(project_name, prefix)
    if backend == "Spring Boot":
        return build_spring_backend_files(project_name, prefix)
    return build_fastapi_backend_files(project_name, prefix)


def build_frontend_files(frontend: str, project_name: str, prefix: str) -> dict[str, str]:
    if frontend in {"React", "Next.js", "Vue"}:
        return build_react_frontend_files(project_name, prefix)
    return build_vanilla_frontend_files(project_name, prefix)


def build_fastapi_backend_files(project_name: str, prefix: str) -> dict[str, str]:
    app_prefix = prefixed(prefix, "app")
    return {
        prefixed(prefix, "requirements.txt"): "\n".join(
            [
                "fastapi",
                "uvicorn[standard]",
                "pydantic",
                "pydantic-settings",
                "sqlalchemy",
                "python-dotenv",
                "aiosqlite",
                "",
            ]
        ),
        prefixed(app_prefix, "__init__.py"): "",
        prefixed(app_prefix, "main.py"): f"""from fastapi import FastAPI

from app.routers import health, items


app = FastAPI(title="{project_name} API")
app.include_router(health.router)
app.include_router(items.router, prefix="/api/items", tags=["items"])


@app.get("/")
def read_root() -> dict[str, str]:
    return {{"message": "{project_name} backend is running."}}
""",
        prefixed(app_prefix, "routers/__init__.py"): "",
        prefixed(app_prefix, "routers/health.py"): """from fastapi import APIRouter

from app.schemas.health import HealthResponse

router = APIRouter(tags=["health"])


@router.get("/health", response_model=HealthResponse)
def healthcheck() -> HealthResponse:
    return HealthResponse(status="ok")
""",
        prefixed(app_prefix, "routers/items.py"): """from fastapi import APIRouter

from app.schemas.item import Item
from app.services.item_service import list_items

router = APIRouter()


@router.get("/", response_model=list[Item])
def get_items() -> list[Item]:
    return list_items()
""",
        prefixed(app_prefix, "services/__init__.py"): "",
        prefixed(app_prefix, "services/app_service.py"): f"""def get_app_summary() -> str:
    return "{project_name} includes routes, services, schemas, and configuration for quick iteration."
""",
        prefixed(app_prefix, "services/item_service.py"): """from app.schemas.item import Item


def list_items() -> list[Item]:
    return [
        Item(id=1, name="Starter task", status="ready"),
        Item(id=2, name="Next iteration", status="planned"),
    ]
""",
        prefixed(app_prefix, "models/__init__.py"): "",
        prefixed(app_prefix, "models/base.py"): """from sqlalchemy.orm import DeclarativeBase


class Base(DeclarativeBase):
    pass
""",
        prefixed(app_prefix, "models/item.py"): """from sqlalchemy import String
from sqlalchemy.orm import Mapped, mapped_column

from app.models.base import Base


class ItemModel(Base):
    __tablename__ = "items"

    id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
    name: Mapped[str] = mapped_column(String(120))
    status: Mapped[str] = mapped_column(String(40), default="ready")
""",
        prefixed(app_prefix, "schemas/__init__.py"): "",
        prefixed(app_prefix, "schemas/health.py"): """from pydantic import BaseModel


class HealthResponse(BaseModel):
    status: str
""",
        prefixed(app_prefix, "schemas/item.py"): """from pydantic import BaseModel


class Item(BaseModel):
    id: int
    name: str
    status: str
""",
        prefixed(app_prefix, "database.py"): """from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from app.config import settings

engine = create_engine(settings.database_url, future=True)
SessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False)
""",
        prefixed(app_prefix, "config.py"): """from pydantic_settings import BaseSettings, SettingsConfigDict


class Settings(BaseSettings):
    model_config = SettingsConfigDict(env_file=".env")
    app_env: str = "development"
    database_url: str = "sqlite:///./app.db"


settings = Settings()
""",
    }


def build_express_backend_files(project_name: str, prefix: str) -> dict[str, str]:
    return {
        prefixed(prefix, "package.json"): json.dumps(
            {
                "name": project_name.lower().replace(" ", "-"),
                "version": "0.1.0",
                "private": True,
                "type": "module",
                "scripts": {"dev": "node --watch server.js", "start": "node server.js"},
                "dependencies": {"cors": "^2.8.5", "dotenv": "^16.4.5", "express": "^4.19.2"},
            },
            indent=2,
        )
        + "\n",
        prefixed(prefix, "server.js"): f"""import cors from "cors";
import dotenv from "dotenv";
import express from "express";
import indexRouter from "./src/routes/index.js";
import itemsRouter from "./src/routes/items.js";

dotenv.config();

const app = express();
app.use(cors());
app.use(express.json());
app.use("/", indexRouter);
app.use("/api/items", itemsRouter);

const port = process.env.PORT || 8000;
app.listen(port, () => {{
  console.log("{project_name} API listening on port", port);
}});
""",
        prefixed(prefix, "src/routes/index.js"): """import { Router } from "express";
import { getStatus } from "../controllers/appController.js";

const router = Router();
router.get("/", getStatus);

export default router;
""",
        prefixed(prefix, "src/routes/items.js"): """import { Router } from "express";
import { listItems } from "../controllers/itemController.js";

const router = Router();
router.get("/", listItems);

export default router;
""",
        prefixed(prefix, "src/controllers/appController.js"): """export function getStatus(_req, res) {
  res.json({ status: "ok" });
}
""",
        prefixed(prefix, "src/controllers/itemController.js"): """import { getItems } from "../services/itemService.js";

export function listItems(_req, res) {
  res.json(getItems());
}
""",
        prefixed(prefix, "src/services/appService.js"): f"""export function getAppSummary() {{
  return "{project_name} includes routes, controllers, services, and starter configuration.";
}}
""",
        prefixed(prefix, "src/services/itemService.js"): """export function getItems() {
  return [
    { id: 1, name: "Starter task", status: "ready" },
    { id: 2, name: "Next iteration", status: "planned" }
  ];
}
""",
        prefixed(prefix, "src/models/itemModel.js"): """export const itemShape = {
  id: "number",
  name: "string",
  status: "string"
};
""",
    }


def build_react_frontend_files(project_name: str, prefix: str) -> dict[str, str]:
    return {
        prefixed(prefix, "package.json"): json.dumps(
            {
                "name": project_name.lower().replace(" ", "-") + "-frontend",
                "private": True,
                "version": "0.1.0",
                "type": "module",
                "scripts": {"dev": "vite", "build": "vite build", "preview": "vite preview"},
                "dependencies": {"react": "^18.3.1", "react-dom": "^18.3.1"},
                "devDependencies": {"vite": "^5.4.0", "@vitejs/plugin-react": "^4.3.1"},
            },
            indent=2,
        )
        + "\n",
        prefixed(prefix, "index.html"): """<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Project Starter</title>
    <script type="module" src="/src/main.jsx"></script>
  </head>
  <body>
    <div id="root"></div>
  </body>
</html>
""",
        prefixed(prefix, "vite.config.js"): """import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";

export default defineConfig({
  plugins: [react()],
  server: {
    port: 5173
  }
});
""",
        prefixed(prefix, "src/main.jsx"): """import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
import "./styles.css";

ReactDOM.createRoot(document.getElementById("root")).render(
  <React.StrictMode>
    <App />
  </React.StrictMode>
);
""",
        prefixed(prefix, "src/App.jsx"): f"""import AppShell from "./components/AppShell";
import HomePage from "./pages/HomePage";

export default function App() {{
  return (
    <AppShell title="{project_name}">
      <HomePage />
    </AppShell>
  );
}}
""",
        prefixed(prefix, "src/components/AppShell.jsx"): """export default function AppShell({ title, children }) {
  return (
    <div className="app-shell">
      <header className="hero">
        <p className="eyebrow">Generated by Project Agent</p>
        <h1>{title}</h1>
      </header>
      <main>{children}</main>
    </div>
  );
}
""",
        prefixed(prefix, "src/pages/HomePage.jsx"): """import { getProjectHealth } from "../services/api";

export default function HomePage() {
  const projectHealth = getProjectHealth();

  return (
    <section className="card">
      <h2>Starter Overview</h2>
      <p>This frontend is ready for your first feature slice.</p>
      <p>API health source: {projectHealth}</p>
    </section>
  );
}
""",
        prefixed(prefix, "src/services/api.js"): """export function getProjectHealth() {
  return import.meta.env.VITE_API_BASE_URL || "http://localhost:8000";
}
""",
        prefixed(prefix, "src/styles.css"): """:root {
  color-scheme: light;
  font-family: "Segoe UI", Arial, sans-serif;
  background: #f5f7fb;
  color: #132238;
}

body {
  margin: 0;
}

.app-shell {
  max-width: 960px;
  margin: 0 auto;
  padding: 32px 20px 56px;
}

.hero {
  margin-bottom: 24px;
}

.eyebrow {
  font-size: 0.75rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: #57657d;
}

.card {
  background: white;
  border-radius: 16px;
  padding: 24px;
  box-shadow: 0 18px 40px rgba(19, 34, 56, 0.08);
}
""",
    }


def build_vanilla_frontend_files(project_name: str, prefix: str) -> dict[str, str]:
    return {
        prefixed(prefix, "package.json"): json.dumps(
            {
                "name": project_name.lower().replace(" ", "-") + "-frontend",
                "private": True,
                "version": "0.1.0",
                "type": "module",
                "scripts": {"dev": "vite", "build": "vite build", "preview": "vite preview"},
                "devDependencies": {"vite": "^5.4.0"},
            },
            indent=2,
        )
        + "\n",
        prefixed(prefix, "index.html"): """<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Project Starter</title>
    <script type="module" src="/src/main.js"></script>
  </head>
  <body>
    <div id="app"></div>
  </body>
</html>
""",
        prefixed(prefix, "vite.config.js"): """import { defineConfig } from "vite";

export default defineConfig({
  server: {
    port: 5173
  }
});
""",
        prefixed(prefix, "src/main.js"): f"""import {{ renderHomePage }} from "./views/home.js";
import "./styles.css";

document.querySelector("#app").innerHTML = renderHomePage("{project_name}");
""",
        prefixed(prefix, "src/views/home.js"): """export function renderHomePage(title) {
  return `
    <main class="app-shell">
      <section class="card">
        <p class="eyebrow">Generated by Project Agent</p>
        <h1>${title}</h1>
        <p>This starter is ready for your first feature slice.</p>
      </section>
    </main>
  `;
}
""",
        prefixed(prefix, "src/services/api.js"): """export function getApiBaseUrl() {
  return import.meta.env.VITE_API_BASE_URL || "http://localhost:8000";
}
""",
        prefixed(prefix, "src/styles.css"): """body {
  margin: 0;
  font-family: "Segoe UI", Arial, sans-serif;
  background: #f5f7fb;
  color: #132238;
}

.app-shell {
  max-width: 960px;
  margin: 0 auto;
  padding: 32px 20px 56px;
}

.card {
  background: white;
  border-radius: 16px;
  padding: 24px;
  box-shadow: 0 18px 40px rgba(19, 34, 56, 0.08);
}

.eyebrow {
  font-size: 0.75rem;
  text-transform: uppercase;
  color: #57657d;
}
""",
    }


def build_spring_backend_files(project_name: str, prefix: str) -> dict[str, str]:
    java_base = prefixed(prefix, "src/main/java/com/example/demo")
    resources_base = prefixed(prefix, "src/main/resources")
    return {
        prefixed(prefix, "pom.xml"): """<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.3.2</version>
  </parent>
  <properties>
    <java.version>17</java.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>
""",
        prefixed(java_base, "Application.java"): """package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
""",
        prefixed(java_base, "controller/AppController.java"): """package com.example.demo.controller;

import com.example.demo.service.AppService;
import java.util.Map;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AppController {
    private final AppService appService;

    public AppController(AppService appService) {
        this.appService = appService;
    }

    @GetMapping("/")
    public Map<String, String> status() {
        return Map.of("message", appService.status());
    }
}
""",
        prefixed(java_base, "service/AppService.java"): f"""package com.example.demo.service;

import org.springframework.stereotype.Service;

@Service
public class AppService {{
    public String status() {{
        return "{project_name} backend is running.";
    }}
}}
""",
        prefixed(java_base, "model/AppModel.java"): """package com.example.demo.model;

public class AppModel {
    private Long id;
    private String name;

    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
}
""",
        prefixed(java_base, "repository/AppRepository.java"): """package com.example.demo.repository;

import com.example.demo.model.AppModel;
import java.util.List;
import org.springframework.stereotype.Repository;

@Repository
public class AppRepository {
    public List<AppModel> findAll() {
        return List.of();
    }
}
""",
        prefixed(resources_base, "application.properties"): """spring.application.name=demo
server.port=8080
""",
    }


def build_root_scripts(
    selected_stack: dict[str, str],
    project_kind: dict[str, Any],
) -> dict[str, str]:
    if project_kind["isFullStack"]:
        return build_fullstack_scripts(selected_stack)
    if project_kind["hasBackend"]:
        backend = selected_stack["backend"]
        if backend in {"FastAPI", "Flask"}:
            return build_python_scripts(".")
        if backend in {"Express", "NestJS"}:
            return build_node_scripts(".")
        if backend == "Spring Boot":
            return build_java_scripts(".")
    return build_node_scripts(".")


def build_fullstack_scripts(selected_stack: dict[str, str]) -> dict[str, str]:
    backend_setup = ""
    backend_run_windows = "echo No backend runtime configured.\n"
    backend_run_unix = 'echo "No backend runtime configured."\n'

    if selected_stack["backend"] in {"FastAPI", "Flask"}:
        backend_setup = (
            "if exist backend\\requirements.txt (\n"
            "  python -m pip install -r backend\\requirements.txt\n"
            ")\n"
        )
        backend_run_windows = (
            'start "Backend" cmd /k "cd backend && python -m uvicorn app.main:app --reload"\n'
        )
        backend_run_unix = (
            '(cd backend && python -m uvicorn app.main:app --reload) &\n'
        )
    elif selected_stack["backend"] in {"Express", "NestJS"}:
        backend_setup = 'if exist backend\\package.json (\n  pushd backend\n  call npm install\n  popd\n)\n'
        backend_run_windows = 'start "Backend" cmd /k "cd backend && npm run dev"\n'
        backend_run_unix = '(cd backend && npm run dev) &\n'
    elif selected_stack["backend"] == "Spring Boot":
        backend_setup = (
            'where mvn >nul 2>nul && (\n'
            '  pushd backend\n'
            '  call mvn install\n'
            '  popd\n'
            ') || echo Maven not found. Skipping backend install.\n'
        )
        backend_run_windows = 'start "Backend" cmd /k "cd backend && mvn spring-boot:run"\n'
        backend_run_unix = '(cd backend && mvn spring-boot:run) &\n'

    return {
        "setup.bat": f"""@echo off
setlocal
{backend_setup}if exist frontend\\package.json (
  pushd frontend
  call npm install
  popd
)
echo Setup complete.
""",
        "setup.sh": """#!/usr/bin/env bash
set -e
if [ -f backend/requirements.txt ]; then
  python -m pip install -r backend/requirements.txt
fi
if [ -f backend/package.json ]; then
  (cd backend && npm install)
fi
if [ -f backend/pom.xml ]; then
  if command -v mvn >/dev/null 2>&1; then
    (cd backend && mvn install)
  else
    echo "Maven not found. Skipping backend install."
  fi
fi
if [ -f frontend/package.json ]; then
  (cd frontend && npm install)
fi
echo "Setup complete."
""",
        "run.bat": f"""@echo off
setlocal
{backend_run_windows}start "Frontend" cmd /k "cd frontend && npm run dev"
""",
        "run.sh": f"""#!/usr/bin/env bash
set -e
{backend_run_unix}(cd frontend && npm run dev) &
wait
""",
    }


def build_python_scripts(target_dir: str) -> dict[str, str]:
    directory_prefix = "" if target_dir in {"", "."} else f"{target_dir}/"
    windows_prefix = "" if target_dir in {"", "."} else f"{target_dir}\\"
    return {
        "setup.bat": f"""@echo off
setlocal
python -m pip install -r {windows_prefix}requirements.txt
echo Setup complete.
""",
        "setup.sh": f"""#!/usr/bin/env bash
set -e
python -m pip install -r {directory_prefix}requirements.txt
echo "Setup complete."
""",
        "run.bat": f"""@echo off
setlocal
python -m uvicorn app.main:app --reload
""",
        "run.sh": f"""#!/usr/bin/env bash
set -e
python -m uvicorn app.main:app --reload
""",
    }


def build_node_scripts(target_dir: str) -> dict[str, str]:
    directory_prefix = "" if target_dir in {"", "."} else f"{target_dir}/"
    windows_prefix = "" if target_dir in {"", "."} else f"{target_dir}\\"
    return {
        "setup.bat": f"""@echo off
setlocal
pushd {windows_prefix or "."}
call npm install
popd
echo Setup complete.
""",
        "setup.sh": f"""#!/usr/bin/env bash
set -e
(cd {directory_prefix or "."} && npm install)
echo "Setup complete."
""",
        "run.bat": f"""@echo off
setlocal
pushd {windows_prefix or "."}
call npm run dev
popd
""",
        "run.sh": f"""#!/usr/bin/env bash
set -e
(cd {directory_prefix or "."} && npm run dev)
""",
    }


def build_java_scripts(target_dir: str) -> dict[str, str]:
    directory_prefix = "" if target_dir in {"", "."} else f"{target_dir}/"
    windows_prefix = "" if target_dir in {"", "."} else f"{target_dir}\\"
    return {
        "setup.bat": f"""@echo off
setlocal
where mvn >nul 2>nul && (
  pushd {windows_prefix or "."}
  call mvn install
  popd
) || (
  echo Maven not found. Skipping install.
)
""",
        "setup.sh": f"""#!/usr/bin/env bash
set -e
if command -v mvn >/dev/null 2>&1; then
  (cd {directory_prefix or "."} && mvn install)
else
  echo "Maven not found. Skipping install."
fi
""",
        "run.bat": f"""@echo off
setlocal
pushd {windows_prefix or "."}
call mvn spring-boot:run
popd
""",
        "run.sh": f"""#!/usr/bin/env bash
set -e
(cd {directory_prefix or "."} && mvn spring-boot:run)
""",
    }


def normalize_string_list(value: Any) -> list[str]:
    if value is None:
        return []
    if isinstance(value, str):
        lines = [item.strip(" -*\t") for item in value.splitlines()]
        return [line for line in lines if line]
    if isinstance(value, Mapping):
        return [f"{key}: {str(item).strip()}" for key, item in value.items() if str(item).strip()]
    if isinstance(value, Sequence) and not isinstance(value, (str, bytes, bytearray)):
        return [str(item).strip() for item in value if str(item).strip()]
    text = str(value).strip()
    return [text] if text else []


def normalize_modules(value: Any) -> list[dict[str, Any]]:
    if not isinstance(value, Sequence) or isinstance(value, (str, bytes, bytearray)):
        return []
    modules: list[dict[str, Any]] = []
    for item in value:
        if not isinstance(item, Mapping):
            continue
        name = str(item.get("name") or "").strip()
        if not name:
            continue
        modules.append(
            {
                "name": name,
                "purpose": str(item.get("purpose") or "").strip(),
                "keyFiles": normalize_string_list(item.get("keyFiles")),
            }
        )
    return modules


def merge_modules(
    primary: list[dict[str, Any]],
    secondary: list[dict[str, Any]],
) -> list[dict[str, Any]]:
    merged: dict[str, dict[str, Any]] = {}
    for collection in (secondary, primary):
        for module in collection:
            key = module["name"].strip().lower()
            current = merged.setdefault(
                key,
                {"name": module["name"], "purpose": "", "keyFiles": []},
            )
            if module.get("purpose"):
                current["purpose"] = module["purpose"]
            current["keyFiles"] = dedupe_list(current["keyFiles"] + normalize_string_list(module.get("keyFiles")))
    return list(merged.values())


def normalize_env_variables(value: Any) -> list[dict[str, str]]:
    if isinstance(value, Mapping):
        value = [{"name": key, "value": str(item), "description": ""} for key, item in value.items()]
    if not isinstance(value, Sequence) or isinstance(value, (str, bytes, bytearray)):
        return []
    env_vars: list[dict[str, str]] = []
    for item in value:
        if not isinstance(item, Mapping):
            continue
        name = str(item.get("name") or "").strip()
        if not name:
            continue
        env_vars.append(
            {
                "name": name,
                "value": str(item.get("value") or "").strip(),
                "description": str(item.get("description") or "").strip(),
            }
        )
    return env_vars


def normalize_required_inputs(value: Any) -> list[dict[str, Any]]:
    if isinstance(value, Mapping):
        value = [value]
    if not isinstance(value, Sequence) or isinstance(value, (str, bytes, bytearray)):
        return []

    required_inputs: list[dict[str, Any]] = []
    for item in value:
        if not isinstance(item, Mapping):
            continue
        name = str(item.get("name") or "").strip()
        if not name:
            continue
        required_flag = item.get("required")
        required_inputs.append(
            {
                "name": name,
                "required": _coerce_required_flag(required_flag),
                "example": str(item.get("example") or item.get("value") or "").strip(),
                "whereToAdd": str(item.get("whereToAdd") or ".env").strip() or ".env",
                "purpose": str(item.get("purpose") or item.get("description") or "").strip(),
            }
        )
    return dedupe_required_inputs(required_inputs)


def merge_required_inputs(
    primary: list[dict[str, Any]],
    secondary: list[dict[str, Any]],
) -> list[dict[str, Any]]:
    merged: dict[str, dict[str, Any]] = {}
    for collection in (secondary, primary):
        for item in collection:
            name = str(item.get("name") or "").strip()
            if not name:
                continue
            merged[name] = {
                "name": name,
                "required": _coerce_required_flag(item.get("required", True)),
                "example": str(item.get("example") or "").strip(),
                "whereToAdd": str(item.get("whereToAdd") or ".env").strip() or ".env",
                "purpose": str(item.get("purpose") or "").strip(),
            }
    return list(merged.values())


def merge_env_variables(
    primary: list[dict[str, str]],
    secondary: list[dict[str, str]],
) -> list[dict[str, str]]:
    merged: dict[str, dict[str, str]] = {}
    for collection in (secondary, primary):
        for item in collection:
            merged[item["name"]] = {
                "name": item["name"],
                "value": item.get("value", ""),
                "description": item.get("description", ""),
            }
    return list(merged.values())


def normalize_files(value: Any) -> list[dict[str, str]]:
    if not isinstance(value, Sequence) or isinstance(value, (str, bytes, bytearray)):
        return []
    files: list[dict[str, str]] = []
    for item in value:
        if not isinstance(item, Mapping):
            continue
        path = clean_relative_path(item.get("path"))
        if not path:
            continue
        files.append({"path": path, "content": trim_content_lines(str(item.get("content") or ""), allow_long=True)})
    return files


def merge_file_entries(
    primary: list[dict[str, str]],
    secondary: list[dict[str, str]],
) -> list[dict[str, str]]:
    merged: dict[str, dict[str, str]] = {}
    for file_entry in primary:
        merged[file_entry["path"]] = file_entry
    for file_entry in secondary:
        merged[file_entry["path"]] = file_entry
    return list(merged.values())


def build_chosen_stack(selected_stack: dict[str, str]) -> list[str]:
    labels = {
        "language": "Language",
        "frontend": "Frontend",
        "backend": "Backend",
        "database": "Database",
        "aiTools": "AI / Tools",
        "deployment": "Deployment",
    }
    return [
        f"{labels[field]}: {selected_stack[field]}"
        for field in STACK_FIELDS
        if selected_stack.get(field) not in {"", "Auto"}
    ]


def clean_relative_path(value: Any) -> str:
    path = str(value or "").replace("\\", "/").strip().strip("/")
    if not path or path.startswith(".") or ".." in path.split("/"):
        return ""
    return path


def is_standard_file_path(path: str) -> bool:
    standard_names = {
        "readme.md",
        "requirements.txt",
        "package.json",
        "pom.xml",
        ".env.example",
        "setup.bat",
        "setup.sh",
        "run.bat",
        "run.sh",
        "vite.config.js",
        "server.js",
        "index.html",
        "main.py",
        "main.jsx",
        "main.js",
        "app.jsx",
        "application.java",
    }
    return Path(path).name.lower() in standard_names


def dedupe_manifest(items: list[dict[str, str]]) -> list[dict[str, str]]:
    seen: set[str] = set()
    result: list[dict[str, str]] = []
    for item in items:
        path = item["path"]
        if path in seen:
            continue
        seen.add(path)
        result.append(item)
    return result


def dedupe_required_inputs(items: Sequence[Mapping[str, Any]]) -> list[dict[str, Any]]:
    seen: set[str] = set()
    result: list[dict[str, Any]] = []
    for item in items:
        name = str(item.get("name") or "").strip()
        if not name:
            continue
        key = name.lower()
        if key in seen:
            continue
        seen.add(key)
        result.append(
            {
                "name": name,
                "required": _coerce_required_flag(item.get("required", True)),
                "example": str(item.get("example") or "").strip(),
                "whereToAdd": str(item.get("whereToAdd") or ".env").strip() or ".env",
                "purpose": str(item.get("purpose") or "").strip(),
            }
        )
    return result


def trim_content_lines(content: str, allow_long: bool = False) -> str:
    if allow_long:
        return content
    lines = content.splitlines()
    if len(lines) <= MAX_CUSTOM_FILE_LINES:
        return content
    return "\n".join(lines[:MAX_CUSTOM_FILE_LINES]).rstrip() + "\n"


def safe_component_name(value: str) -> str:
    parts = re.findall(r"[A-Za-z0-9]+", value)
    return "".join(part.capitalize() for part in parts) or "GeneratedComponent"


def safe_python_name(value: str) -> str:
    cleaned = re.sub(r"[^a-zA-Z0-9_]+", "_", value).strip("_").lower()
    return cleaned or "generated_item"


def safe_js_name(value: str) -> str:
    parts = re.findall(r"[A-Za-z0-9]+", value)
    if not parts:
        return "generatedItem"
    head = parts[0].lower()
    tail = "".join(part.capitalize() for part in parts[1:])
    return head + tail


def prefixed(prefix: str, path: str) -> str:
    base = Path(prefix) if prefix else Path()
    return (base / path).as_posix()


def dedupe_list(items: Sequence[str]) -> list[str]:
    seen: set[str] = set()
    result: list[str] = []
    for item in items:
        cleaned = str(item).strip()
        if not cleaned:
            continue
        key = cleaned.lower()
        if key in seen:
            continue
        seen.add(key)
        result.append(cleaned)
    return result


def _context_mentions_any(lowered_text: str, keywords: Sequence[str]) -> bool:
    return any(keyword in lowered_text for keyword in keywords)


def _coerce_required_flag(value: Any) -> bool:
    if isinstance(value, bool):
        return value
    if value is None:
        return True
    if isinstance(value, str):
        return value.strip().lower() not in {"false", "0", "no", "optional"}
    return bool(value)