ym 0.3.58

Yummy - A modern Java build tool
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
use anyhow::{bail, Result};
use console::style;
use std::collections::{BTreeMap, HashSet};
use std::path::Path;

use crate::config;
use crate::config::schema::{DependencySpec, DependencyValue, YmConfig};

/// Standalone `ym migrate` command
pub fn execute(verify: bool) -> Result<()> {
    let cwd = std::env::current_dir()?;
    let config_path = cwd.join(config::CONFIG_FILE);

    if config_path.exists() {
        bail!("ym.json already exists. Remove it first to re-migrate.");
    }

    let pom = cwd.join("pom.xml");
    let gradle = cwd.join("build.gradle");
    let gradle_kts = cwd.join("build.gradle.kts");
    let settings_gradle = cwd.join("settings.gradle");
    let settings_gradle_kts = cwd.join("settings.gradle.kts");

    // Check for multi-module Gradle project (settings.gradle with include statements)
    if settings_gradle.exists() || settings_gradle_kts.exists() {
        let settings_path = if settings_gradle_kts.exists() {
            &settings_gradle_kts
        } else {
            &settings_gradle
        };
        let modules = parse_settings_gradle(settings_path)?;
        if !modules.is_empty() {
            println!(
                "  {} migrating multi-module Gradle project ({} modules)...",
                style("➜").green(),
                modules.len()
            );
            migrate_gradle_multimodule(&cwd, settings_path, &modules)?;
            if verify {
                run_post_migration_verify()?;
            }
            return Ok(());
        }
    }

    // Check for multi-module Maven project
    if pom.exists() {
        let content = std::fs::read_to_string(&pom)?;
        let doc = roxmltree::Document::parse(&content)?;
        let modules = find_modules(&doc.root_element());
        if !modules.is_empty() {
            println!(
                "  {} migrating multi-module Maven project ({} modules)...",
                style("➜").green(),
                modules.len()
            );
            migrate_maven_multimodule(&cwd, &pom, &modules)?;
            if verify {
                run_post_migration_verify()?;
            }
            return Ok(());
        }
    }

    // Single-project migration
    let cfg = if pom.exists() {
        println!("  {} migrating from pom.xml...", style("➜").green());
        migrate_from_pom(&pom)?
    } else if gradle.exists() {
        println!("  {} migrating from build.gradle...", style("➜").green());
        migrate_from_gradle(&gradle)?
    } else if gradle_kts.exists() {
        println!(
            "  {} migrating from build.gradle.kts...",
            style("➜").green()
        );
        migrate_from_gradle(&gradle_kts)?
    } else {
        bail!("No pom.xml or build.gradle found in current directory");
    };

    config::save_config(&config_path, &cfg)?;
    print_migration_summary(&cfg);

    if verify {
        run_post_migration_verify()?;
    }

    Ok(())
}

/// Run post-migration verification: resolve deps + build
fn run_post_migration_verify() -> Result<()> {
    println!();
    println!(
        "  {} Verifying migration...",
        style("➜").green()
    );

    match super::build::compile_only(None) {
        Ok(()) => {
            println!(
                "  {} Migration verified — build succeeded",
                style("✓").green()
            );
            Ok(())
        }
        Err(e) => {
            eprintln!(
                "  {} Migration verification failed: {}",
                style("✗").red(),
                e
            );
            eprintln!(
                "  {} ym.json was generated but may need manual adjustments",
                style("!").yellow()
            );
            Ok(()) // Don't fail the convert command itself
        }
    }
}

fn print_migration_summary(cfg: &YmConfig) {
    println!("  {} Created ym.json", style("✓").green());

    let dep_count = cfg.dependencies.as_ref().map(|d| d.len()).unwrap_or(0);

    if dep_count > 0 {
        println!(
            "  {} Migrated {} dependencies",
            style("✓").green(),
            dep_count
        );
    }

    if let Some(ref java) = cfg.target {
        println!(
            "  {} Java version: {}",
            style("✓").green(),
            style(java).cyan()
        );
    }

    println!();
    println!("  Run {} to start developing", style("ym dev").cyan());
}

// --- Gradle ext variable parsing ---

/// Parse `ext { key = 'value' }` block from a Gradle build file.
/// Returns a map of variable name → resolved value.
fn parse_ext_variables(content: &str) -> BTreeMap<String, String> {
    let mut variables = BTreeMap::new();
    let mut in_ext = false;
    let mut brace_depth = 0;

    for line in content.lines() {
        let trimmed = line.trim();

        if !in_ext {
            if trimmed.starts_with("ext {") || trimmed == "ext {" || trimmed.starts_with("ext{") {
                in_ext = true;
                brace_depth = 1;
                continue;
            }
            continue;
        }

        brace_depth += trimmed.chars().filter(|&c| c == '{').count() as i32;
        brace_depth -= trimmed.chars().filter(|&c| c == '}').count() as i32;
        if brace_depth <= 0 {
            in_ext = false;
            continue;
        }

        if trimmed.starts_with("//") {
            continue;
        }

        // Match: key = 'value' or key = "value"
        if let Some(eq_pos) = trimmed.find('=') {
            let key = trimmed[..eq_pos].trim();
            let val_part = trimmed[eq_pos + 1..].trim();

            // Extract quoted value
            if let Some(val) = extract_quoted_value(val_part) {
                if key.chars().all(|c| c.is_alphanumeric() || c == '_') {
                    variables.insert(key.to_string(), val);
                }
            }
            // Handle: applicationVersion = version (bare identifier referencing project version)
            else if val_part == "version" && key == "applicationVersion" {
                variables.insert(key.to_string(), "PROJECT_VERSION".to_string());
            }
        }
    }

    variables
}

/// Extract a quoted string value from a Gradle expression.
fn extract_quoted_value(s: &str) -> Option<String> {
    let s = s.trim();
    if (s.starts_with('\'') && s.ends_with('\'')) || (s.starts_with('"') && s.ends_with('"')) {
        Some(s[1..s.len() - 1].to_string())
    } else {
        None
    }
}

/// Resolve `${variableName}` references in a string using the ext variables map.
fn resolve_ext_refs(s: &str, ext_vars: &BTreeMap<String, String>, project_version: &str) -> String {
    let mut result = s.to_string();
    // Resolve all ${...} references
    while let Some(start) = result.find("${") {
        if let Some(end) = result[start..].find('}') {
            let var_name = &result[start + 2..start + end];
            let replacement = if var_name == "applicationVersion"
                || var_name == "rootProject.version"
                || var_name == "project.version"
            {
                project_version.to_string()
            } else if let Some(val) = ext_vars.get(var_name) {
                if val == "PROJECT_VERSION" {
                    project_version.to_string()
                } else {
                    val.clone()
                }
            } else {
                // Unresolved variable
                return result;
            };
            result = format!("{}{}{}", &result[..start], replacement, &result[start + end + 1..]);
        } else {
            break;
        }
    }
    result
}

/// Parse `allprojects { group "xxx" }` or `allprojects { group = "xxx" }` from root build.gradle.
/// Returns (group_id, version, compiler_args).
fn parse_allprojects(content: &str) -> (Option<String>, Option<String>, Vec<String>) {
    let mut group_id = None;
    let version = None;
    let mut compiler_args = Vec::new();
    let mut in_allprojects = false;
    let mut brace_depth = 0;

    for line in content.lines() {
        let trimmed = line.trim();

        if !in_allprojects {
            if trimmed.starts_with("allprojects") && trimmed.contains('{') {
                in_allprojects = true;
                brace_depth = 1;
                continue;
            }
            // Also check subprojects block
            if trimmed.starts_with("subprojects") && trimmed.contains('{') {
                in_allprojects = true;
                brace_depth = 1;
                continue;
            }
            continue;
        }

        brace_depth += trimmed.chars().filter(|&c| c == '{').count() as i32;
        brace_depth -= trimmed.chars().filter(|&c| c == '}').count() as i32;
        if brace_depth <= 0 {
            in_allprojects = false;
            continue;
        }

        // group "xxx" or group = "xxx" or group 'xxx'
        if trimmed.starts_with("group") && !trimmed.starts_with("groupId") {
            if let Some(val) = extract_string_value(trimmed) {
                group_id = Some(val);
            } else if let Some(val) = extract_string_arg(trimmed) {
                group_id = Some(val);
            }
        }

        // compilerArgs.add('-parameters') or compilerArgs << '-parameters'
        if trimmed.contains("compilerArgs") && trimmed.contains("-parameters") {
            compiler_args.push("-parameters".to_string());
        }
    }

    (group_id, version, compiler_args)
}

/// Scan filesystem for build.gradle files when settings.gradle uses dynamic fileTree discovery.
fn scan_gradle_modules(root: &Path) -> Vec<String> {
    let mut modules = Vec::new();
    scan_gradle_modules_recursive(root, root, &mut modules);
    modules
}

fn scan_gradle_modules_recursive(root: &Path, dir: &Path, modules: &mut Vec<String>) {
    let entries = match std::fs::read_dir(dir) {
        Ok(e) => e,
        Err(_) => return,
    };

    for entry in entries.flatten() {
        let path = entry.path();
        if !path.is_dir() {
            continue;
        }

        let name = entry.file_name().to_string_lossy().to_string();
        // Skip common non-module directories
        if matches!(
            name.as_str(),
            "build" | ".gradle" | ".idea" | "buildSrc" | "gradle" | ".git" | "node_modules" | "specs"
        ) {
            continue;
        }

        let gradle_file = path.join("build.gradle");
        let gradle_kts_file = path.join("build.gradle.kts");
        if gradle_file.exists() || gradle_kts_file.exists() {
            if let Ok(rel) = path.strip_prefix(root) {
                let rel_str = rel.to_string_lossy().replace('\\', "/");
                modules.push(rel_str);
            }
        }

        // Recurse into subdirectories
        scan_gradle_modules_recursive(root, &path, modules);
    }
}

// --- Gradle multi-module migration ---

/// Parse settings.gradle(.kts) to extract included module paths.
fn parse_settings_gradle(path: &Path) -> Result<Vec<String>> {
    let content = std::fs::read_to_string(path)?;
    let mut modules = Vec::new();

    // Track brace depth to skip fileTree/pluginManagement blocks
    let mut skip_depth: i32 = 0;
    let mut in_skippable_block = false;

    for line in content.lines() {
        let trimmed = line.trim();

        // Detect blocks we should skip (fileTree, pluginManagement, etc.)
        if !in_skippable_block && (trimmed.starts_with("fileTree") || trimmed.starts_with("pluginManagement")) {
            in_skippable_block = true;
            skip_depth = 0;
        }

        if in_skippable_block {
            skip_depth += trimmed.chars().filter(|&c| c == '{').count() as i32;
            skip_depth -= trimmed.chars().filter(|&c| c == '}').count() as i32;
            if skip_depth <= 0 && trimmed.contains('}') {
                in_skippable_block = false;
            }
            continue;
        }

        // Match: include 'module-a', ':module-b', ':parent:child'
        // Match: include("module-a", ":module-b")
        if !trimmed.starts_with("include") {
            continue;
        }

        // Extract all quoted strings from the line
        let mut i = 0;
        let chars: Vec<char> = trimmed.chars().collect();
        while i < chars.len() {
            if chars[i] == '\'' || chars[i] == '"' {
                let quote = chars[i];
                i += 1;
                let start = i;
                while i < chars.len() && chars[i] != quote {
                    i += 1;
                }
                if i < chars.len() {
                    let module = &trimmed[start..i];
                    // Strip leading colon and convert : to /
                    let module_path = module.trim_start_matches(':').replace(':', "/");
                    // Skip glob patterns (these are file includes, not module includes)
                    if !module_path.is_empty() && !module_path.contains('*') {
                        modules.push(module_path);
                    }
                }
            }
            i += 1;
        }
    }

    // Fallback: if no module include statements found, check for dynamic fileTree scanning
    if modules.is_empty() && content.contains("fileTree") {
        if let Some(root_dir) = path.parent() {
            let scanned = scan_gradle_modules(root_dir);
            if !scanned.is_empty() {
                println!(
                    "  {} Detected dynamic fileTree module discovery, scanning filesystem ({} modules found)",
                    style("➜").green(),
                    scanned.len()
                );
                return Ok(scanned);
            }
        }
    }

    Ok(modules)
}

/// Migrate a Gradle multi-module project.
fn migrate_gradle_multimodule(root: &Path, _settings_path: &Path, modules: &[String]) -> Result<()> {
    let root_gradle = root.join("build.gradle.kts");
    let root_gradle_alt = root.join("build.gradle");
    let root_gradle_path = if root_gradle.exists() {
        Some(root_gradle.as_path())
    } else if root_gradle_alt.exists() {
        Some(root_gradle_alt.as_path())
    } else {
        None
    };

    // Parse root build.gradle for ext variables, allprojects, and shared settings
    let root_content = root_gradle_path
        .and_then(|p| std::fs::read_to_string(p).ok())
        .unwrap_or_default();

    let ext_vars = parse_ext_variables(&root_content);
    let (all_group, _all_version, compiler_args) = parse_allprojects(&root_content);

    // Extract project version from root build.gradle
    let mut project_version = String::new();
    for line in root_content.lines() {
        let trimmed = line.trim();
        // Match: version "4.0.9" or version = "4.0.9" or version '4.0.9'
        if trimmed.starts_with("version") && !trimmed.starts_with("version(") {
            if let Some(val) = extract_string_value(trimmed) {
                project_version = val;
                break;
            }
            if let Some(val) = extract_string_arg(trimmed) {
                project_version = val;
                break;
            }
        }
    }

    if !ext_vars.is_empty() {
        println!(
            "  {} Parsed {} ext variables from root build.gradle",
            style("✓").green(),
            ext_vars.len()
        );
    }

    // Parse root build.gradle for basic config (name, etc.)
    let mut root_cfg = if let Some(path) = root_gradle_path {
        migrate_from_gradle(path)?
    } else {
        YmConfig::default()
    };

    if root_cfg.name.is_empty() {
        root_cfg.name = root
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or("my-project")
            .to_string();
    }

    // Apply allprojects group
    if let Some(ref group) = all_group {
        if root_cfg.group_id == "com.example" || root_cfg.group_id.is_empty() {
            root_cfg.group_id.clone_from(group);
        }
    }

    // Apply compiler args
    if !compiler_args.is_empty() {
        let compiler = root_cfg.compiler.get_or_insert_with(Default::default);
        compiler.args = Some(compiler_args);
    }

    // Set version
    if !project_version.is_empty() {
        root_cfg.version = Some(project_version.clone());
    }

    // Determine workspace patterns from module paths (use smart glob patterns)
    let workspace_patterns = compute_workspace_patterns(modules);
    root_cfg.workspaces = Some(workspace_patterns);

    // All module names for inter-module dependency detection
    let all_module_names: HashSet<String> = modules
        .iter()
        .map(|m| {
            Path::new(m)
                .file_name()
                .unwrap_or_default()
                .to_string_lossy()
                .to_string()
        })
        .collect();
    let all_module_names_vec: Vec<String> = all_module_names.iter().cloned().collect();

    // First pass: collect all external dependencies with their versions for root catalog
    let mut all_ext_deps: BTreeMap<String, String> = BTreeMap::new();
    let mut bom_managed_deps: HashSet<String> = HashSet::new(); // deps with no version (BOM managed)
    for module_path in modules {
        let module_dir = root.join(module_path);
        let gradle_path = find_gradle_file(&module_dir);
        let gradle_path = match gradle_path {
            Some(p) => p,
            None => continue,
        };
        let content = match std::fs::read_to_string(&gradle_path) {
            Ok(c) => c,
            Err(_) => continue,
        };
        collect_ext_deps_from_gradle(&content, &ext_vars, &project_version, &all_module_names, &mut all_ext_deps, &mut bom_managed_deps);
    }

    // Build root dependencies from ext variables + collected deps
    let mut root_deps: BTreeMap<String, DependencyValue> = BTreeMap::new();
    for (coord, version) in &all_ext_deps {
        if coord.starts_with("com.summer.jarvis:") {
            continue; // External jarvis deps stay per-module
        }
        root_deps.insert(
            coord.clone(),
            DependencyValue::Simple(version.clone()),
        );
    }

    // Pre-scan for Spring Boot plugin version from ext vars or submodules
    let mut spring_boot_version: Option<String> = ext_vars.get("springBootVersion").cloned();
    if spring_boot_version.is_none() {
        // Scan root and submodule build.gradle files for Spring Boot plugin version
        let all_gradle_contents: Vec<String> = std::iter::once(root_content.clone())
            .chain(modules.iter().filter_map(|m| {
                let gp = find_gradle_file(&root.join(m));
                gp.and_then(|p| std::fs::read_to_string(p).ok())
            }))
            .collect();
        for content in &all_gradle_contents {
            for line in content.lines() {
                let line = line.trim();
                if line.contains("org.springframework.boot") && line.contains("version") {
                    let resolved = resolve_ext_refs(line, &ext_vars, &project_version);
                    if let Some(start) = resolved.rfind('\'').or_else(|| resolved.rfind('"')) {
                        let before = &resolved[..start];
                        if let Some(vstart) = before.rfind('\'').or_else(|| before.rfind('"')) {
                            let ver = &resolved[vstart + 1..start];
                            if !ver.is_empty() && ver.chars().next().is_some_and(|c| c.is_ascii_digit()) {
                                spring_boot_version = Some(ver.to_string());
                            }
                        }
                    }
                }
            }
            if spring_boot_version.is_some() { break; }
        }
    }

    // Add BOM-managed deps to root with resolved versions
    if spring_boot_version.is_some() {
        for coord in &bom_managed_deps {
            if !root_deps.contains_key(coord) {
                let version = resolve_bom_version(coord, "", &spring_boot_version);
                root_deps.insert(coord.clone(), DependencyValue::Simple(version));
            }
        }
    } else {
        let sb_version = ext_vars.get("springBootVersion").cloned().unwrap_or_default();
        if !sb_version.is_empty() {
            for coord in &bom_managed_deps {
                if !root_deps.contains_key(coord) {
                    root_deps.insert(coord.clone(), DependencyValue::Simple(sb_version.clone()));
                }
            }
        }
    }

    // Lombok → provided scope
    // Check ext vars first, then scan for io.freefair.lombok plugin
    let lombok_ver = ext_vars.get("lombokVersion").cloned();
    let has_freefair_lombok = root_content.contains("io.freefair.lombok")
        || modules.iter().any(|m| {
            find_gradle_file(&root.join(m))
                .and_then(|p| std::fs::read_to_string(p).ok())
                .is_some_and(|c| c.contains("io.freefair.lombok"))
        });
    if lombok_ver.is_some() || has_freefair_lombok {
        let ver = lombok_ver.unwrap_or_else(|| "1.18.36".to_string()); // latest stable default
        root_deps.insert(
            "org.projectlombok:lombok".to_string(),
            DependencyValue::Detailed(DependencySpec {
                version: Some(ver),
                scope: Some("provided".to_string()),
                ..Default::default()
            }),
        );
        if has_freefair_lombok {
            println!(
                "  {} io.freefair.lombok plugin detected → added Lombok as provided dependency",
                style("✓").green()
            );
        }
    }

    if !root_deps.is_empty() {
        root_cfg.dependencies = Some(root_deps.clone());
        println!(
            "  {} Root dependencies: {} shared entries",
            style("✓").green(),
            root_deps.len()
        );
    }

    let root_dep_coords: HashSet<String> = root_deps.keys().cloned().collect();

    let root_config_path = root.join(config::CONFIG_FILE);
    config::save_config(&root_config_path, &root_cfg)?;
    println!("  {} Created root ym.json", style("✓").green());

    // Second pass: migrate each submodule
    let mut migrated = 0;
    for module_path in modules {
        let module_dir = root.join(module_path);
        if !module_dir.exists() {
            continue;
        }

        let module_config_path = module_dir.join(config::CONFIG_FILE);
        if module_config_path.exists() {
            continue; // Already migrated
        }

        let gradle_path = match find_gradle_file(&module_dir) {
            Some(p) => p,
            None => continue,
        };

        let mut module_cfg = migrate_from_gradle_ext(
            &gradle_path,
            &all_module_names_vec,
            &ext_vars,
            &project_version,
            &root_dep_coords,
            true, // quiet: suppress per-module hints in multi-module migration
        )?;

        // Use directory name as module name if not set
        if module_cfg.name.is_empty() {
            module_cfg.name = module_dir
                .file_name()
                .and_then(|n| n.to_str())
                .unwrap_or("module")
                .to_string();
        }

        // Inherit group from allprojects
        if let Some(ref group) = all_group {
            if module_cfg.group_id == "com.example" || module_cfg.group_id.is_empty() {
                module_cfg.group_id.clone_from(group);
            }
        }

        // Save with workspace inheritance for version and target
        save_module_config(&module_config_path, &module_cfg)?;
        migrated += 1;
    }

    println!(
        "  {} Migrated {} submodules",
        style("✓").green(),
        migrated
    );
    println!();
    println!(
        "  Run {} to resolve dependencies",
        style("ym install").cyan()
    );

    Ok(())
}

/// Save a sub-module config with `version = { workspace = true }` and `target = { workspace = true }`.
fn save_module_config(path: &Path, cfg: &YmConfig) -> Result<()> {
    // Separate workspace deps from non-workspace deps
    let mut workspace_deps: BTreeMap<String, ()> = BTreeMap::new();
    let mut temp_cfg = cfg.clone();
    temp_cfg.version = None;
    temp_cfg.target = None;

    if let Some(ref mut deps) = temp_cfg.dependencies {
        let mut non_ws = BTreeMap::new();
        for (k, v) in deps.iter() {
            if v.is_workspace() {
                workspace_deps.insert(k.clone(), ());
            } else {
                non_ws.insert(k.clone(), v.clone());
            }
        }
        if non_ws.is_empty() {
            temp_cfg.dependencies = None;
        } else {
            *deps = non_ws;
        }
    }

    // Save non-workspace deps via serde
    config::save_config(path, &temp_cfg)?;

    // Patch in workspace inheritance for version and target
    let content = std::fs::read_to_string(path)?;
    let mut lines: Vec<String> = content.lines().map(|l| l.to_string()).collect();

    // Find the name line and insert version/target after it
    if let Some(name_idx) = lines.iter().position(|l| l.starts_with("name")) {
        let insert_at = name_idx + 1;
        let insert_at = if insert_at < lines.len() && lines[insert_at].starts_with("group_id") {
            insert_at + 1
        } else {
            insert_at
        };
        lines.insert(insert_at, "version = { workspace = true }".to_string());
        lines.insert(insert_at + 1, "target = { workspace = true }".to_string());
    }

    // Append workspace deps as inline format under [dependencies]
    if !workspace_deps.is_empty() {
        // Ensure [dependencies] section exists
        if !lines.iter().any(|l| l.trim() == "[dependencies]") {
            lines.push(String::new());
            lines.push("[dependencies]".to_string());
        }
        // Find the [dependencies] line and insert after it
        if let Some(dep_idx) = lines.iter().position(|l| l.trim() == "[dependencies]") {
            let mut insert_at = dep_idx + 1;
            // Skip existing key = value lines
            while insert_at < lines.len() {
                let l = lines[insert_at].trim();
                if l.is_empty() || l.starts_with('[') {
                    break;
                }
                insert_at += 1;
            }
            for key in workspace_deps.keys() {
                let needs_quote = key.contains(':');
                let entry = if needs_quote {
                    format!("\"{}\" = {{ workspace = true }}", key)
                } else {
                    format!("{} = {{ workspace = true }}", key)
                };
                lines.insert(insert_at, entry);
                insert_at += 1;
            }
        }
    }

    std::fs::write(path, lines.join("\n"))?;
    Ok(())
}

/// Find build.gradle or build.gradle.kts in a directory.
fn find_gradle_file(dir: &Path) -> Option<std::path::PathBuf> {
    let kts = dir.join("build.gradle.kts");
    if kts.exists() {
        return Some(kts);
    }
    let groovy = dir.join("build.gradle");
    if groovy.exists() {
        return Some(groovy);
    }
    None
}

/// Compute smart workspace patterns from module paths.
/// Groups modules under common parent directories to minimize pattern count.
fn compute_workspace_patterns(modules: &[String]) -> Vec<String> {
    // Group by top-level directories and find optimal glob depth
    let mut patterns: Vec<String> = Vec::new();
    let mut seen_prefixes: HashSet<String> = HashSet::new();

    // Sort modules by path for grouping
    let mut sorted: Vec<&String> = modules.iter().collect();
    sorted.sort();

    for module in &sorted {
        let parts: Vec<&str> = module.split('/').collect();
        if parts.len() <= 1 {
            // Top-level module
            let pattern = format!("{}/*", parts[0]);
            if seen_prefixes.insert(pattern.clone()) {
                // Check if this single dir actually contains the module or is the module
                // For top-level, use direct pattern
                patterns.push(format!("./*"));
            }
            continue;
        }

        // For deeper paths, try to find the shallowest common prefix that uses **/*
        // e.g., project/jarvis-utils/utils-xxx → project/jarvis-utils/*
        // e.g., project/jarvis-infra/account/xxx/yyy → project/jarvis-infra/**/*
        let mut found = false;
        for depth in (1..parts.len()).rev() {
            let prefix: String = parts[..depth].join("/");
            let pattern = if depth == parts.len() - 1 {
                format!("{}/*", prefix)
            } else {
                format!("{}/**/*", prefix)
            };
            if seen_prefixes.contains(&pattern) {
                found = true;
                break;
            }
        }

        if !found {
            // Find the best prefix depth: check if all siblings share same parent depth
            let prefix: String = parts[..parts.len() - 1].join("/");
            let pattern = format!("{}/*", prefix);

            // Check if any other module has more nesting under the same top-2 prefix
            let top_prefix = if parts.len() >= 2 {
                parts[..2].join("/")
            } else {
                parts[0].to_string()
            };

            let has_deeper = sorted.iter().any(|m| {
                m.starts_with(&top_prefix) && m.split('/').count() > parts.len()
            });

            if has_deeper {
                let deep_pattern = format!("{}/**/*", top_prefix);
                if seen_prefixes.insert(deep_pattern.clone()) {
                    patterns.push(deep_pattern);
                }
            } else {
                if seen_prefixes.insert(pattern.clone()) {
                    patterns.push(pattern);
                }
            }
        }
    }

    // Deduplicate: remove patterns that are subsumed by **/* patterns
    let glob_patterns: Vec<String> = patterns.iter().filter(|p| p.contains("**")).cloned().collect();
    patterns.retain(|p| {
        if p.contains("**") {
            return true;
        }
        // Check if any **/* pattern already covers this
        !glob_patterns.iter().any(|gp| {
            let gp_prefix = gp.trim_end_matches("/**/*");
            p.starts_with(gp_prefix)
        })
    });

    if patterns.is_empty() {
        patterns.push("./*".to_string());
    }

    patterns
}

/// Collect all external dependency coordinates+versions from a build.gradle
/// (first pass, for building root dependency catalog).
fn collect_ext_deps_from_gradle(
    content: &str,
    ext_vars: &BTreeMap<String, String>,
    project_version: &str,
    all_module_names: &HashSet<String>,
    deps: &mut BTreeMap<String, String>,
    bom_managed: &mut HashSet<String>,
) {
    for line in content.lines() {
        let trimmed = line.trim();
        if trimmed.starts_with("//") {
            continue;
        }

        // Skip project() dependencies
        if trimmed.contains("project(") {
            continue;
        }

        // Skip platform() dependencies
        if trimmed.contains("platform(") {
            continue;
        }

        // Parse dependency line
        if let Some((coord, version)) = parse_gradle_dep_raw(trimmed) {
            // Resolve ext variable references
            let resolved_coord = resolve_ext_refs(&coord, ext_vars, project_version);
            let resolved_version = resolve_ext_refs(&version, ext_vars, project_version);

            if resolved_coord.contains("${") {
                continue; // Unresolvable coordinate
            }

            let parts: Vec<&str> = resolved_coord.split(':').collect();
            if parts.len() < 2 {
                continue;
            }

            let full_coord = if parts.len() >= 3 {
                format!("{}:{}", parts[0], parts[1])
            } else {
                resolved_coord.clone()
            };
            let ver = if parts.len() >= 3 {
                parts[2].to_string()
            } else if !resolved_version.is_empty() && !resolved_version.contains("${") {
                resolved_version
            } else {
                String::new()
            };

            // Skip com.summer.jarvis internal modules
            if full_coord.starts_with("com.summer.jarvis:") {
                let artifact = full_coord.split(':').nth(1).unwrap_or("");
                if all_module_names.contains(artifact) {
                    continue;
                }
                // External jarvis deps: don't add to root catalog
                continue;
            }

            if ver.is_empty() {
                // BOM-managed dependency (no version)
                bom_managed.insert(full_coord);
            } else {
                deps.entry(full_coord).or_insert(ver);
            }
        }
    }
}

/// Raw parse a Gradle dependency line, returning the full dependency string and version.
fn parse_gradle_dep_raw(line: &str) -> Option<(String, String)> {
    let scope_prefixes = [
        "implementation", "api", "compile", "testImplementation",
        "testCompile", "compileOnly", "runtimeOnly", "annotationProcessor",
    ];

    let trimmed = line.trim();
    if !scope_prefixes.iter().any(|p| trimmed.starts_with(p)) {
        return None;
    }

    // Skip project() references
    if trimmed.contains("project(") || trimmed.contains("platform(") {
        return None;
    }

    // Extract quoted dependency string
    let start = trimmed.find('\'').or_else(|| trimmed.find('"'))?;
    let quote_char = trimmed.chars().nth(start)?;
    let rest = &trimmed[start + 1..];
    let end = rest.rfind(quote_char)?;
    let dep_str = &rest[..end];

    // Split into coordinate parts
    let parts: Vec<&str> = dep_str.split(':').collect();
    match parts.len() {
        3 => Some((dep_str.to_string(), parts[2].to_string())),
        2 => Some((dep_str.to_string(), String::new())),
        _ => None,
    }
}

/// Migrate a Gradle build file with ext variable resolution and workspace awareness.
fn migrate_from_gradle_ext(
    gradle_path: &Path,
    all_module_names: &[String],
    ext_vars: &BTreeMap<String, String>,
    project_version: &str,
    root_dep_coords: &HashSet<String>,
    quiet: bool,
) -> Result<YmConfig> {
    let content = std::fs::read_to_string(gradle_path)?;

    let project_dir = gradle_path.parent().unwrap_or(Path::new("."));
    let catalog = parse_version_catalog(project_dir);

    let mut config = YmConfig::default();
    let mut dependencies: BTreeMap<String, DependencyValue> = BTreeMap::new();

    // Pre-scan for Spring Boot plugin version
    let mut spring_boot_version: Option<String> = None;
    for line in content.lines() {
        let line = line.trim();
        if line.contains("org.springframework.boot") && line.contains("version") {
            let resolved = resolve_ext_refs(line, ext_vars, project_version);
            if let Some(start) = resolved.rfind('\'').or_else(|| resolved.rfind('"')) {
                let before = &resolved[..start];
                if let Some(vstart) = before.rfind('\'').or_else(|| before.rfind('"')) {
                    let ver = &resolved[vstart + 1..start];
                    if !ver.is_empty() && ver.chars().next().is_some_and(|c| c.is_ascii_digit()) {
                        spring_boot_version = Some(ver.to_string());
                    }
                }
            }
        }
    }

    // Pre-scan settings.gradle for rootProject.name
    let settings_path = project_dir.join("settings.gradle");
    let settings_kts_path = project_dir.join("settings.gradle.kts");
    let settings = if settings_kts_path.exists() {
        std::fs::read_to_string(&settings_kts_path).ok()
    } else if settings_path.exists() {
        std::fs::read_to_string(&settings_path).ok()
    } else {
        None
    };
    if let Some(ref settings_content) = settings {
        for line in settings_content.lines() {
            let line = line.trim();
            if line.starts_with("rootProject.name") {
                if let Some(val) = extract_string_value(line) {
                    config.name = val;
                }
            }
        }
    }

    let all_module_set: HashSet<&str> = all_module_names.iter().map(|s| s.as_str()).collect();

    for line in content.lines() {
        let line = line.trim();

        // Skip comments
        if line.starts_with("//") {
            continue;
        }

        // Extract group/version
        if line.starts_with("group") && !line.starts_with("groupId") {
            if let Some(val) = extract_string_value(line) {
                config.group_id = val.clone();
                if config.package.is_none() {
                    config.package = Some(val);
                }
            }
        }
        if line.starts_with("version") && line.contains('=') && !line.contains("${") {
            if let Some(val) = extract_string_value(line) {
                config.version = Some(val);
            }
        }

        // sourceCompatibility
        if line.starts_with("sourceCompatibility") {
            if let Some(val) = extract_string_value(line) {
                config.target = Some(val.trim_start_matches("JavaVersion.VERSION_").to_string());
            }
        }

        // java toolchain: languageVersion = JavaLanguageVersion.of(21)
        if line.contains("languageVersion") && line.contains("JavaLanguageVersion.of") {
            if let Some(start) = line.find("JavaLanguageVersion.of(") {
                let rest = &line[start + "JavaLanguageVersion.of(".len()..];
                if let Some(end) = rest.find(')') {
                    let ver = rest[..end].trim();
                    if config.target.is_none() {
                        config.target = Some(ver.to_string());
                    }
                }
            }
        }

        // Detect inter-module project dependencies
        if let Some(proj_dep) = parse_gradle_project_dependency(line) {
            // Take the last segment of the project path as module name
            let dep_name = proj_dep.split(':').last().unwrap_or(&proj_dep).to_string();
            if all_module_set.contains(dep_name.as_str()) {
                let scope = detect_scope(line);
                let dep_val = if let Some(s) = scope {
                    DependencyValue::Detailed(DependencySpec {
                        workspace: Some(true),
                        scope: Some(s),
                        ..Default::default()
                    })
                } else {
                    DependencyValue::Detailed(DependencySpec {
                        workspace: Some(true),
                        ..Default::default()
                    })
                };
                dependencies.entry(dep_name).or_insert(dep_val);
                continue;
            }
        }

        // Skip platform() BOM imports (just log)
        if line.contains("platform(") {
            // Detected but not converted — user should handle manually
            continue;
        }

        // External dependencies (with ext variable resolution)
        if let Some((raw_coord, _raw_ver)) = parse_gradle_dep_raw(line) {
            let resolved = resolve_ext_refs(&raw_coord, ext_vars, project_version);
            let parts: Vec<&str> = resolved.split(':').collect();
            if parts.len() < 2 {
                continue;
            }

            let coord = if parts.len() >= 3 {
                format!("{}:{}", parts[0], parts[1])
            } else {
                resolved.clone()
            };
            let version = if parts.len() >= 3 {
                parts[2].to_string()
            } else {
                // BOM-managed: no version
                String::new()
            };

            let scope = detect_scope(line);

            // Check if it's a com.summer.jarvis reference
            if coord.starts_with("com.summer.jarvis:") {
                let artifact = coord.split(':').nth(1).unwrap_or("");
                if all_module_set.contains(artifact) {
                    // Internal module → workspace reference
                    let dep_val = if let Some(s) = scope {
                        DependencyValue::Detailed(DependencySpec {
                            workspace: Some(true),
                            scope: Some(s),
                            ..Default::default()
                        })
                    } else {
                        DependencyValue::Detailed(DependencySpec {
                            workspace: Some(true),
                            ..Default::default()
                        })
                    };
                    dependencies.entry(artifact.to_string()).or_insert(dep_val);
                    continue;
                }
                // External jarvis dep → keep as external with version
                let ver = if version.is_empty() { project_version.to_string() } else { version };
                let dep_val = if let Some(s) = scope {
                    DependencyValue::Detailed(DependencySpec {
                        version: Some(ver),
                        scope: Some(s),
                        ..Default::default()
                    })
                } else {
                    DependencyValue::Simple(ver)
                };
                dependencies.entry(coord).or_insert(dep_val);
                continue;
            }

            // Regular external dep: use workspace = true if in root catalog
            if root_dep_coords.contains(&coord) {
                let dep_val = if let Some(s) = scope {
                    DependencyValue::Detailed(DependencySpec {
                        workspace: Some(true),
                        scope: Some(s),
                        ..Default::default()
                    })
                } else {
                    DependencyValue::Detailed(DependencySpec {
                        workspace: Some(true),
                        ..Default::default()
                    })
                };
                dependencies.entry(coord).or_insert(dep_val);
            } else {
                // Not in root → include version directly
                let ver = if version.is_empty() {
                    resolve_bom_version(&coord, "", &spring_boot_version)
                } else {
                    version
                };
                let dep_val = if let Some(s) = scope {
                    DependencyValue::Detailed(DependencySpec {
                        version: Some(ver),
                        scope: Some(s),
                        ..Default::default()
                    })
                } else {
                    DependencyValue::Simple(ver)
                };
                dependencies.entry(coord).or_insert(dep_val);
            }

            continue;
        }

        // Version Catalog references
        if !catalog.is_empty() {
            if let Some((scope_str, alias)) = parse_catalog_reference(line) {
                let normalized = alias.replace('.', "-");
                if let Some((module, version)) = catalog.get(&normalized) {
                    let dep_val = match scope_str {
                        "compile" => DependencyValue::Simple(version.clone()),
                        _ => DependencyValue::Detailed(DependencySpec {
                            version: Some(version.clone()),
                            scope: Some(scope_str.to_string()),
                            ..Default::default()
                        }),
                    };
                    dependencies.insert(module.clone(), dep_val);
                }
            }
        }
    }

    // Detect common Gradle plugins
    detect_gradle_plugins(&content, &mut config, &mut dependencies, quiet);

    // Post-process: convert deps that exist in root catalog to { workspace = true }
    for (coord, dep_val) in dependencies.iter_mut() {
        if root_dep_coords.contains(coord) {
            // Preserve scope if any, but switch to workspace reference
            let scope = match dep_val {
                DependencyValue::Detailed(spec) => spec.scope.clone(),
                _ => None,
            };
            if let Some(s) = scope {
                *dep_val = DependencyValue::Detailed(DependencySpec {
                    workspace: Some(true),
                    scope: Some(s),
                    ..Default::default()
                });
            } else {
                *dep_val = DependencyValue::Detailed(DependencySpec {
                    workspace: Some(true),
                    ..Default::default()
                });
            }
        }
    }

    if config.name.is_empty() {
        config.name = gradle_path
            .parent()
            .and_then(|p| p.file_name())
            .and_then(|n| n.to_str())
            .unwrap_or("my-app")
            .to_string();
    }

    if !dependencies.is_empty() {
        config.dependencies = Some(dependencies);
    }

    Ok(config)
}

/// Detect Gradle scope from a dependency line.
fn detect_scope(line: &str) -> Option<String> {
    let trimmed = line.trim();
    if trimmed.starts_with("testImplementation") || trimmed.starts_with("testCompile") {
        Some("test".to_string())
    } else if trimmed.starts_with("compileOnly") {
        Some("provided".to_string())
    } else if trimmed.starts_with("runtimeOnly") {
        Some("runtime".to_string())
    } else {
        None
    }
}

/// Parse a Gradle build file, detecting inter-module (project) dependencies.
/// Parse Gradle Version Catalog (gradle/libs.versions.toml).
/// Returns a map of alias → (groupId:artifactId, version).
fn parse_version_catalog(project_dir: &Path) -> BTreeMap<String, (String, String)> {
    let catalog_path = project_dir.join("gradle").join("libs.versions.toml");
    let mut result = BTreeMap::new();
    let content = match std::fs::read_to_string(&catalog_path) {
        Ok(c) => c,
        Err(_) => return result,
    };
    let doc: toml::Value = match content.parse() {
        Ok(v) => v,
        Err(_) => return result,
    };

    // Collect [versions]
    let mut versions: BTreeMap<String, String> = BTreeMap::new();
    if let Some(vers) = doc.get("versions").and_then(|v| v.as_table()) {
        for (k, v) in vers {
            if let Some(s) = v.as_str() {
                versions.insert(k.clone(), s.to_string());
            }
        }
    }

    // Collect [libraries]
    if let Some(libs) = doc.get("libraries").and_then(|v| v.as_table()) {
        for (alias, val) in libs {
            let (module, version) = match val {
                toml::Value::String(s) => {
                    // "group:artifact:version" shorthand
                    let parts: Vec<&str> = s.splitn(3, ':').collect();
                    if parts.len() == 3 {
                        (format!("{}:{}", parts[0], parts[1]), parts[2].to_string())
                    } else {
                        continue;
                    }
                }
                toml::Value::Table(t) => {
                    let module = t.get("module").and_then(|m| m.as_str()).unwrap_or("");
                    if module.is_empty() {
                        // Try group + name format
                        let g = t.get("group").and_then(|v| v.as_str()).unwrap_or("");
                        let n = t.get("name").and_then(|v| v.as_str()).unwrap_or("");
                        if g.is_empty() || n.is_empty() { continue; }
                        let module = format!("{}:{}", g, n);
                        let ver = resolve_catalog_version(t, &versions);
                        if ver.is_empty() { continue; }
                        (module, ver)
                    } else {
                        let ver = resolve_catalog_version(t, &versions);
                        if ver.is_empty() { continue; }
                        (module.to_string(), ver)
                    }
                }
                _ => continue,
            };
            // Normalize alias: spring-boot-starter-web → spring.boot.starter.web (for Gradle accessor matching)
            result.insert(alias.clone(), (module, version));
        }
    }

    result
}

/// Resolve version from a catalog library entry.
fn resolve_catalog_version(table: &toml::value::Table, versions: &BTreeMap<String, String>) -> String {
    // Direct version string
    if let Some(v) = table.get("version") {
        if let Some(s) = v.as_str() {
            return s.to_string();
        }
        // version.ref = "key"
        if let Some(t) = v.as_table() {
            if let Some(ref_key) = t.get("ref").and_then(|r| r.as_str()) {
                return versions.get(ref_key).cloned().unwrap_or_default();
            }
        }
    }
    // version.ref at top level (TOML flattened: "version.ref" = "key")
    if let Some(vr) = table.get("version.ref").and_then(|v| v.as_str()) {
        return versions.get(vr).cloned().unwrap_or_default();
    }
    String::new()
}

fn migrate_from_gradle_with_projects(
    gradle_path: &Path,
    all_module_names: &[String],
) -> Result<YmConfig> {
    let content = std::fs::read_to_string(gradle_path)?;

    // Load Version Catalog if available
    let project_dir = gradle_path.parent().unwrap_or(Path::new("."));
    let catalog = parse_version_catalog(project_dir);

    let mut config = YmConfig::default();
    let mut dependencies: BTreeMap<String, DependencyValue> = BTreeMap::new();

    // Pre-scan for Spring Boot plugin version (used for BOM-managed dependencies)
    let mut spring_boot_version: Option<String> = None;
    for line in content.lines() {
        let line = line.trim();
        // id 'org.springframework.boot' version '3.4.0'
        // id("org.springframework.boot") version "3.4.0"
        if line.contains("org.springframework.boot") && line.contains("version") {
            if let Some(start) = line.rfind('\'').or_else(|| line.rfind('"')) {
                let before = &line[..start];
                if let Some(vstart) = before.rfind('\'').or_else(|| before.rfind('"')) {
                    let ver = &line[vstart + 1..start];
                    if !ver.is_empty() && ver.chars().next().is_some_and(|c| c.is_ascii_digit()) {
                        spring_boot_version = Some(ver.to_string());
                    }
                }
            }
        }
    }

    // Pre-scan settings.gradle for rootProject.name
    let settings_path = project_dir.join("settings.gradle");
    let settings_kts_path = project_dir.join("settings.gradle.kts");
    let settings = if settings_kts_path.exists() {
        std::fs::read_to_string(&settings_kts_path).ok()
    } else if settings_path.exists() {
        std::fs::read_to_string(&settings_path).ok()
    } else {
        None
    };
    if let Some(ref settings_content) = settings {
        for line in settings_content.lines() {
            let line = line.trim();
            if line.starts_with("rootProject.name") {
                if let Some(val) = extract_string_value(line) {
                    config.name = val;
                }
            }
        }
    }

    for line in content.lines() {
        let line = line.trim();

        // Extract group/version
        if line.starts_with("group") && !line.starts_with("groupId") && line.contains('=') {
            if let Some(val) = extract_string_value(line) {
                config.group_id = val.clone();
                // Also set package if not already set
                if config.package.is_none() {
                    config.package = Some(val);
                }
            }
        }
        if line.starts_with("version") && line.contains('=') {
            if let Some(val) = extract_string_value(line) {
                config.version = Some(val);
            }
        }

        // sourceCompatibility
        if line.starts_with("sourceCompatibility") {
            if let Some(val) = extract_string_value(line) {
                config.target = Some(val.trim_start_matches("JavaVersion.VERSION_").to_string());
            }
        }

        // java toolchain: languageVersion = JavaLanguageVersion.of(21)
        if line.contains("languageVersion") && line.contains("JavaLanguageVersion.of") {
            if let Some(start) = line.find("JavaLanguageVersion.of(") {
                let rest = &line[start + "JavaLanguageVersion.of(".len()..];
                if let Some(end) = rest.find(')') {
                    let ver = rest[..end].trim();
                    if config.target.is_none() {
                        config.target = Some(ver.to_string());
                    }
                }
            }
        }

        // Detect inter-module project dependencies
        if let Some(proj_dep) = parse_gradle_project_dependency(line) {
            let dep_name = proj_dep.trim_start_matches(':').replace(':', "-");
            if all_module_names.contains(&dep_name) {
                dependencies.entry(dep_name).or_insert(DependencyValue::Detailed(DependencySpec {
                    workspace: Some(true),
                    ..Default::default()
                }));
                continue;
            }
        }

        // External dependencies
        if let Some(dep) = parse_gradle_dependency(line, "implementation")
            .or_else(|| parse_gradle_dependency(line, "api"))
            .or_else(|| parse_gradle_dependency(line, "compile"))
        {
            let version = resolve_bom_version(&dep.0, &dep.1, &spring_boot_version);
            dependencies.insert(dep.0, DependencyValue::Simple(version));
        }
        if let Some(dep) = parse_gradle_dependency(line, "testImplementation")
            .or_else(|| parse_gradle_dependency(line, "testCompile"))
        {
            let version = resolve_bom_version(&dep.0, &dep.1, &spring_boot_version);
            dependencies.insert(dep.0, DependencyValue::Detailed(DependencySpec {
                version: Some(version),
                scope: Some("test".to_string()),
                ..Default::default()
            }));
        }
        if let Some(dep) = parse_gradle_dependency(line, "compileOnly") {
            let version = resolve_bom_version(&dep.0, &dep.1, &spring_boot_version);
            dependencies.insert(dep.0, DependencyValue::Detailed(DependencySpec {
                version: Some(version),
                scope: Some("provided".to_string()),
                ..Default::default()
            }));
        }
        if let Some(dep) = parse_gradle_dependency(line, "runtimeOnly") {
            let version = resolve_bom_version(&dep.0, &dep.1, &spring_boot_version);
            dependencies.insert(dep.0, DependencyValue::Detailed(DependencySpec {
                version: Some(version),
                scope: Some("runtime".to_string()),
                ..Default::default()
            }));
        }

        // Version Catalog references: implementation(libs.spring.boot.starter.web)
        if !catalog.is_empty() {
            if let Some((scope, alias)) = parse_catalog_reference(line) {
                // Normalize: libs.spring.boot.starter.web → spring-boot-starter-web
                let normalized = alias.replace('.', "-");
                if let Some((module, version)) = catalog.get(&normalized) {
                    let dep_val = match scope {
                        "compile" => DependencyValue::Simple(version.clone()),
                        _ => DependencyValue::Detailed(DependencySpec {
                            version: Some(version.clone()),
                            scope: Some(scope.to_string()),
                            ..Default::default()
                        }),
                    };
                    dependencies.insert(module.clone(), dep_val);
                }
            }
        }
    }

    // Detect common Gradle plugins and map to ym config
    detect_gradle_plugins(&content, &mut config, &mut dependencies, false);

    if config.name.is_empty() {
        config.name = gradle_path
            .parent()
            .and_then(|p| p.file_name())
            .and_then(|n| n.to_str())
            .unwrap_or("my-app")
            .to_string();
    }

    if !dependencies.is_empty() {
        config.dependencies = Some(dependencies);
    }

    Ok(config)
}

/// Detect common Gradle plugins and map to ym compiler/config settings.
fn detect_gradle_plugins(
    content: &str,
    config: &mut YmConfig,
    deps: &mut BTreeMap<String, DependencyValue>,
    quiet: bool,
) {
    let mut hints: Vec<String> = Vec::new();

    // Spring Boot plugin
    if content.contains("org.springframework.boot") || content.contains("spring-boot") {
        // Detect main class from SpringBootApplication
        if config.main.is_none() {
            if let Some(pkg) = config.package.as_ref() {
                config.main = Some(format!("{}.Application", pkg));
            }
        }
        hints.push("Spring Boot plugin detected".to_string());
    }

    // Annotation processing (Lombok, MapStruct, etc.)
    let has_lombok_dep = deps.keys().any(|k| k.contains("lombok"));
    // io.freefair.lombok plugin implicitly adds Lombok as compileOnly + annotationProcessor
    let has_lombok_plugin = content.contains("io.freefair.lombok");
    let has_lombok = has_lombok_dep || has_lombok_plugin;
    let has_mapstruct = deps.keys().any(|k| k.contains("mapstruct"));
    if has_lombok || has_mapstruct {
        let mut ap = Vec::new();
        if has_lombok {
            ap.push("org.projectlombok:lombok".to_string());
            // If Lombok came from plugin (not explicitly in deps), add it as provided dependency
            if has_lombok_plugin && !has_lombok_dep {
                // freefair plugin version != Lombok library version (8.x → 1.18.x)
                // Use latest stable Lombok version as default
                let lombok_ver = "1.18.36".to_string();
                deps.insert(
                    "org.projectlombok:lombok".to_string(),
                    DependencyValue::Detailed(DependencySpec {
                        version: Some(lombok_ver),
                        scope: Some("provided".to_string()),
                        ..Default::default()
                    }),
                );
                hints.push("io.freefair.lombok plugin → added Lombok as provided dependency".to_string());
            }
        }
        if has_mapstruct {
            ap.push("org.mapstruct:mapstruct-processor".to_string());
        }
        let compiler = config.compiler.get_or_insert_with(Default::default);
        compiler.annotation_processors = Some(ap.clone());
        hints.push(format!("Annotation processors: {}", ap.join(", ")));
    }

    // Java compilation target from plugins { java { targetCompatibility } }
    for line in content.lines() {
        let line = line.trim();
        if line.starts_with("targetCompatibility") {
            if let Some(val) = extract_string_value(line) {
                let ver = val.trim_start_matches("JavaVersion.VERSION_").to_string();
                if config.target.is_none() {
                    config.target = Some(ver);
                }
            }
        }
    }

    // Print detected plugin hints (skip in quiet mode for sub-modules)
    if !quiet {
        for hint in &hints {
            println!("  {} {}", style("➜").green(), style(hint).dim());
        }
    }
}

/// Parse Version Catalog reference from a Gradle dependency line.
/// Returns (scope, alias) where alias is the dotted accessor after "libs."
/// Examples:
///   implementation(libs.spring.boot.starter.web) → ("compile", "spring.boot.starter.web")
///   testImplementation libs.junit.jupiter → ("test", "junit.jupiter")
fn parse_catalog_reference(line: &str) -> Option<(&str, String)> {
    let line = line.trim();
    let scope_prefixes: &[(&str, &str)] = &[
        ("implementation", "compile"),
        ("api", "compile"),
        ("compile", "compile"),
        ("testImplementation", "test"),
        ("testCompile", "test"),
        ("compileOnly", "provided"),
        ("runtimeOnly", "runtime"),
    ];
    for &(prefix, scope) in scope_prefixes {
        if !line.starts_with(prefix) { continue; }
        let rest = line[prefix.len()..].trim();
        // Match: (libs.xxx) or libs.xxx
        let inner = rest.trim_start_matches('(').trim_end_matches(')').trim();
        if let Some(alias) = inner.strip_prefix("libs.") {
            let alias = alias.trim_end_matches(')').trim();
            if !alias.is_empty() && !alias.contains('\'') && !alias.contains('"') {
                return Some((scope, alias.to_string()));
            }
        }
    }
    None
}

/// Parse project(':module-name') from a Gradle dependency line.
fn parse_gradle_project_dependency(line: &str) -> Option<String> {
    // Match patterns like:
    //   implementation project(':module-name')
    //   implementation(project(":module-name"))
    //   api project(':module-name')
    let prefixes = [
        "implementation", "api", "compile",
        "testImplementation", "testCompile",
    ];
    let trimmed = line.trim_start();
    let is_dep_line = prefixes.iter().any(|p| trimmed.starts_with(p));
    if !is_dep_line {
        return None;
    }
    let line = trimmed;

    // Look for project( or project (
    let proj_idx = line.find("project(")?;
    let after_proj = &line[proj_idx + 8..]; // skip "project("
    // Find the quoted module name
    let quote_start = after_proj.find('\'').or_else(|| after_proj.find('"'))?;
    let quote_char = after_proj.chars().nth(quote_start)?;
    let rest = &after_proj[quote_start + 1..];
    let quote_end = rest.find(quote_char)?;
    Some(rest[..quote_end].to_string())
}

// --- Maven multi-module migration ---

/// Migrate a Maven multi-module project.
fn migrate_maven_multimodule(root: &Path, root_pom: &Path, modules: &[String]) -> Result<()> {
    // Parse root POM for shared settings
    let mut root_cfg = migrate_from_pom(root_pom)?;

    // workspaces is already set by migrate_from_pom via find_modules
    // Make sure it uses direct module names
    root_cfg.private = Some(true);
    root_cfg.workspaces = Some(modules.iter().map(|m| format!("{}/*", m)).collect());

    // Deduplicate workspace patterns
    let mut seen = std::collections::HashSet::new();
    if let Some(ref mut ws) = root_cfg.workspaces {
        ws.retain(|p| seen.insert(p.clone()));
    }

    let root_config_path = root.join(config::CONFIG_FILE);
    config::save_config(&root_config_path, &root_cfg)?;
    println!("  {} Created root ym.json", style("✓").green());

    // Collect all module artifact IDs for inter-module dep detection
    let mut module_artifacts: BTreeMap<String, String> = BTreeMap::new(); // artifactId -> module_path
    for module_path in modules {
        let module_pom = root.join(module_path).join("pom.xml");
        if module_pom.exists() {
            if let Ok(content) = std::fs::read_to_string(&module_pom) {
                if let Ok(doc) = roxmltree::Document::parse(&content) {
                    if let Some(aid) = find_child_text(&doc.root_element(), "artifactId") {
                        module_artifacts.insert(aid, module_path.clone());
                    }
                }
            }
        }
    }

    // Migrate each submodule
    let mut migrated = 0;
    for module_path in modules {
        let module_dir = root.join(module_path);
        let module_pom = module_dir.join("pom.xml");
        if !module_pom.exists() {
            continue;
        }

        let module_config_path = module_dir.join(config::CONFIG_FILE);
        if module_config_path.exists() {
            continue;
        }

        let mut module_cfg = migrate_from_pom(&module_pom)?;

        // Detect inter-module dependencies: if a dependency's artifactId matches
        // a sibling module, convert it to a workspace module ref
        if let Some(ref mut deps) = module_cfg.dependencies {
            let keys_to_remove: Vec<String> = deps
                .keys()
                .filter(|coord| {
                    let parts: Vec<&str> = coord.split(':').collect();
                    parts.len() == 2 && module_artifacts.contains_key(parts[1])
                })
                .cloned()
                .collect();
            for key in keys_to_remove {
                deps.remove(&key);
                let parts: Vec<&str> = key.split(':').collect();
                if parts.len() == 2 {
                    deps.insert(parts[1].to_string(), DependencyValue::Detailed(DependencySpec {
                        workspace: Some(true),
                        ..Default::default()
                    }));
                }
            }
        }

        // Clear workspaces on submodules (only root has workspaces)
        module_cfg.workspaces = None;
        module_cfg.private = None;

        config::save_config(&module_config_path, &module_cfg)?;
        migrated += 1;
    }

    println!(
        "  {} Migrated {} submodules",
        style("✓").green(),
        migrated
    );
    println!();
    println!(
        "  Run {} to resolve dependencies",
        style("ym install").cyan()
    );

    Ok(())
}

/// Parse a Maven pom.xml and convert to YmConfig
pub fn migrate_from_pom(pom_path: &Path) -> Result<YmConfig> {
    let content = std::fs::read_to_string(pom_path)?;
    let doc = roxmltree::Document::parse(&content)?;

    let root = doc.root_element();

    let name = find_child_text(&root, "artifactId").unwrap_or_else(|| "my-app".to_string());
    let version = find_child_text(&root, "version");
    let _group_id = find_child_text(&root, "groupId");

    // Detect Java version from maven-compiler-plugin or properties
    let java_version = detect_java_version(&root);

    // Extract dependencies
    let mut dependencies: BTreeMap<String, DependencyValue> = BTreeMap::new();

    for node in root.descendants() {
        if node.tag_name().name() != "dependencies" {
            continue;
        }
        // Skip dependencyManagement
        if let Some(parent) = node.parent() {
            if parent.tag_name().name() == "dependencyManagement" {
                continue;
            }
        }

        for dep in node.children() {
            if dep.tag_name().name() != "dependency" {
                continue;
            }

            let dep_group = find_child_text(&dep, "groupId");
            let dep_artifact = find_child_text(&dep, "artifactId");
            let dep_version = find_child_text(&dep, "version");
            let dep_scope = find_child_text(&dep, "scope");
            let dep_optional = find_child_text(&dep, "optional");

            if let (Some(g), Some(a)) = (dep_group, dep_artifact) {
                let coord = format!("{}:{}", g, a);
                let ver = dep_version.unwrap_or_else(|| "LATEST".to_string());

                // Skip property references
                if ver.contains("${") {
                    continue;
                }

                // Skip optional dependencies
                if dep_optional.as_deref() == Some("true") {
                    eprintln!(
                        "  {} Skipped optional dependency: {} (add manually if needed)",
                        console::style("!").yellow(), coord
                    );
                    continue;
                }

                match dep_scope.as_deref() {
                    Some("test") => {
                        dependencies.insert(coord, DependencyValue::Detailed(DependencySpec {
                            version: Some(ver),
                            scope: Some("test".to_string()),
                            ..Default::default()
                        }));
                    }
                    Some("provided") => {
                        dependencies.insert(coord, DependencyValue::Detailed(DependencySpec {
                            version: Some(ver),
                            scope: Some("provided".to_string()),
                            ..Default::default()
                        }));
                    }
                    Some("runtime") => {
                        dependencies.insert(coord, DependencyValue::Detailed(DependencySpec {
                            version: Some(ver),
                            scope: Some("runtime".to_string()),
                            ..Default::default()
                        }));
                    }
                    Some("system") => {
                        eprintln!(
                            "  {} Skipped system-scoped dependency: {} (requires local JAR path, handle manually)",
                            console::style("!").yellow(), coord
                        );
                    }
                    _ => {
                        dependencies.insert(coord, DependencyValue::Simple(ver));
                    }
                }
            }
        }
    }

    // Detect main class from exec-maven-plugin
    let main_class = detect_main_class(&root);

    // Build config
    let mut config = YmConfig {
        name,
        version,
        target: java_version,
        main: main_class,
        ..Default::default()
    };

    if !dependencies.is_empty() {
        // Detect Maven plugins and map to ym config
        detect_maven_plugins(&root, &mut config, &dependencies);
        config.dependencies = Some(dependencies);
    }

    // Detect if this is a multi-module project
    let modules = find_modules(&root);
    if !modules.is_empty() {
        config.private = Some(true);
        config.workspaces = Some(modules.iter().map(|m| format!("{}/*", m)).collect());
    }

    Ok(config)
}

/// Parse a build.gradle and extract basic info
pub fn migrate_from_gradle(gradle_path: &Path) -> Result<YmConfig> {
    migrate_from_gradle_with_projects(gradle_path, &[])
}

/// Detect Maven plugins (spring-boot, annotation-processing, shade) and map to ym config.
fn detect_maven_plugins(
    root: &roxmltree::Node,
    config: &mut YmConfig,
    deps: &BTreeMap<String, DependencyValue>,
) {
    let mut hints: Vec<String> = Vec::new();

    // Scan <build><plugins> for known plugins
    for node in root.descendants() {
        if node.tag_name().name() != "plugin" {
            continue;
        }
        let artifact = find_child_text(&node, "artifactId").unwrap_or_default();

        match artifact.as_str() {
            "spring-boot-maven-plugin" => {
                if config.main.is_none() {
                    if let Some(pkg) = config.package.as_ref() {
                        config.main = Some(format!("{}.Application", pkg));
                    }
                }
                hints.push("Spring Boot Maven plugin detected".to_string());
            }
            "maven-compiler-plugin" => {
                // Check for annotation processor configuration
                for cfg_node in node.descendants() {
                    if cfg_node.tag_name().name() == "annotationProcessorPaths" {
                        let mut ap = Vec::new();
                        for path_node in cfg_node.children() {
                            if path_node.tag_name().name() == "path" || path_node.tag_name().name() == "annotationProcessorPath" {
                                let g = find_child_text(&path_node, "groupId");
                                let a = find_child_text(&path_node, "artifactId");
                                if let (Some(g), Some(a)) = (g, a) {
                                    ap.push(format!("{}:{}", g, a));
                                }
                            }
                        }
                        if !ap.is_empty() {
                            let compiler = config.compiler.get_or_insert_with(Default::default);
                            compiler.annotation_processors = Some(ap.clone());
                            hints.push(format!("Annotation processors: {}", ap.join(", ")));
                        }
                    }
                }
            }
            _ => {}
        }
    }

    // Auto-detect Lombok from dependencies
    let has_lombok = deps.keys().any(|k| k.contains("lombok"));
    if has_lombok && config.compiler.as_ref().and_then(|c| c.annotation_processors.as_ref()).is_none() {
        let compiler = config.compiler.get_or_insert_with(Default::default);
        compiler.annotation_processors = Some(vec!["org.projectlombok:lombok".to_string()]);
        hints.push("Lombok auto-detected as annotation processor".to_string());
    }

    for hint in &hints {
        println!("  {} {}", style("➜").green(), style(hint).dim());
    }
}

fn find_child_text(node: &roxmltree::Node, name: &str) -> Option<String> {
    node.children()
        .find(|n| n.tag_name().name() == name)
        .and_then(|n| n.text())
        .map(|s| s.trim().to_string())
}

fn detect_java_version(root: &roxmltree::Node) -> Option<String> {
    // Check <properties><maven.compiler.source>
    for node in root.descendants() {
        if node.tag_name().name() == "properties" {
            for prop in node.children() {
                let name = prop.tag_name().name();
                if name == "maven.compiler.source"
                    || name == "maven.compiler.target"
                    || name == "maven.compiler.release"
                    || name == "java.version"
                {
                    if let Some(text) = prop.text() {
                        let v = text.trim().to_string();
                        if !v.contains("${") {
                            return Some(v);
                        }
                    }
                }
            }
        }
    }
    None
}

fn detect_main_class(root: &roxmltree::Node) -> Option<String> {
    for node in root.descendants() {
        if node.tag_name().name() == "mainClass" {
            if let Some(text) = node.text() {
                let v = text.trim().to_string();
                if !v.contains("${") {
                    return Some(v);
                }
            }
        }
    }
    None
}

fn find_modules(root: &roxmltree::Node) -> Vec<String> {
    let mut modules = Vec::new();
    for node in root.descendants() {
        if node.tag_name().name() == "modules" {
            for child in node.children() {
                if child.tag_name().name() == "module" {
                    if let Some(text) = child.text() {
                        modules.push(text.trim().to_string());
                    }
                }
            }
        }
    }
    modules
}

/// Resolve version for BOM-managed dependencies.
/// Downloads and parses Spring Boot BOM to get exact managed versions.
fn resolve_bom_version(coord: &str, version: &str, spring_boot_version: &Option<String>) -> String {
    if !version.is_empty() {
        return version.to_string();
    }
    if let Some(sb_ver) = spring_boot_version {
        // Spring Boot BOM-managed: same version as Spring Boot itself
        if coord.starts_with("org.springframework.boot:") {
            return sb_ver.clone();
        }
        // Try to resolve from Spring Boot BOM (cached)
        let bom_versions = get_spring_boot_bom_versions(sb_ver);
        if let Some(resolved) = bom_versions.get(coord) {
            return resolved.clone();
        }
        // Spring Framework core: derive from Spring Boot version
        if coord.starts_with("org.springframework:") {
            if let Some(v) = spring_boot_to_framework_version(sb_ver) {
                return v;
            }
        }
    }
    eprintln!(
        "  {} No version for {} — add version manually",
        console::style("!").yellow(),
        coord
    );
    "FIXME".to_string()
}

/// Download and parse Spring Boot Dependencies BOM to extract managed versions.
/// Results are cached in a thread-local to avoid repeated downloads.
fn get_spring_boot_bom_versions(boot_version: &str) -> std::collections::HashMap<String, String> {
    use std::sync::Mutex;
    use std::collections::HashMap;

    // Simple static cache
    static BOM_CACHE: std::sync::LazyLock<Mutex<HashMap<String, HashMap<String, String>>>> =
        std::sync::LazyLock::new(|| Mutex::new(HashMap::new()));

    let mut cache = BOM_CACHE.lock().unwrap();
    if let Some(versions) = cache.get(boot_version) {
        return versions.clone();
    }

    let versions = fetch_spring_boot_bom_versions(boot_version);
    cache.insert(boot_version.to_string(), versions.clone());
    versions
}

fn fetch_spring_boot_bom_versions(boot_version: &str) -> std::collections::HashMap<String, String> {
    use std::collections::HashMap;

    let url = format!(
        "https://repo1.maven.org/maven2/org/springframework/boot/spring-boot-dependencies/{}/spring-boot-dependencies-{}.pom",
        boot_version, boot_version
    );

    eprintln!(
        "  {} Fetching Spring Boot BOM {} for version resolution...",
        console::style("↓").blue(),
        boot_version
    );

    let client = match reqwest::blocking::Client::builder()
        .connect_timeout(std::time::Duration::from_secs(10))
        .build()
    {
        Ok(c) => c,
        Err(_) => return HashMap::new(),
    };

    let mut versions = HashMap::new();
    let mut bom_imports: Vec<(String, String, String)> = Vec::new(); // (groupId, artifactId, version)

    // Parse the main BOM
    if let Some((props, _)) = fetch_and_parse_bom_pom(&client, &url) {
        extract_managed_versions_from_url(&client, &url, &props, &mut versions, &mut bom_imports);
    }

    // Recursively resolve BOM imports (max 2 levels deep for Spring ecosystem)
    for (group_id, artifact_id, version) in &bom_imports {
        let nested_url = format!(
            "https://repo1.maven.org/maven2/{}/{}/{}/{}-{}.pom",
            group_id.replace('.', "/"),
            artifact_id,
            version,
            artifact_id,
            version
        );
        let mut nested_bom_imports = Vec::new();
        if let Some((nested_props, _)) = fetch_and_parse_bom_pom(&client, &nested_url) {
            extract_managed_versions_from_url(
                &client, &nested_url, &nested_props, &mut versions, &mut nested_bom_imports,
            );
        }
    }

    if !versions.is_empty() {
        eprintln!(
            "  {} Spring Boot BOM: {} managed versions extracted (including nested BOMs)",
            console::style("✓").green(),
            versions.len()
        );
    }

    versions
}

/// Fetch and parse a POM file, returning (properties, document_body).
fn fetch_and_parse_bom_pom(
    client: &reqwest::blocking::Client,
    url: &str,
) -> Option<(std::collections::HashMap<String, String>, String)> {
    use std::collections::HashMap;

    let response = client.get(url).send().ok()?;
    if !response.status().is_success() {
        return None;
    }
    let body = response.text().ok()?;
    let doc = roxmltree::Document::parse(&body).ok()?;

    let root = doc.root_element();
    let mut props: HashMap<String, String> = HashMap::new();
    for node in root.children() {
        if node.tag_name().name() == "properties" {
            for child in node.children() {
                if child.is_element() {
                    if let Some(val) = child.text() {
                        props.insert(child.tag_name().name().to_string(), val.trim().to_string());
                    }
                }
            }
        }
    }

    Some((props, body))
}

/// Extract managed versions from a BOM POM, collecting direct versions and BOM imports.
fn extract_managed_versions_from_url(
    _client: &reqwest::blocking::Client,
    url: &str,
    parent_props: &std::collections::HashMap<String, String>,
    versions: &mut std::collections::HashMap<String, String>,
    bom_imports: &mut Vec<(String, String, String)>,
) {
    use std::collections::HashMap;

    let client2 = match reqwest::blocking::Client::builder()
        .connect_timeout(std::time::Duration::from_secs(10))
        .build()
    {
        Ok(c) => c,
        Err(_) => return,
    };

    let response = match client2.get(url).send() {
        Ok(r) if r.status().is_success() => r,
        _ => return,
    };

    let body = match response.text() {
        Ok(b) => b,
        Err(_) => return,
    };

    let doc = match roxmltree::Document::parse(&body) {
        Ok(d) => d,
        Err(_) => return,
    };

    let root = doc.root_element();

    // Collect this POM's own properties, merging with parent
    let mut props: HashMap<String, String> = parent_props.clone();
    for node in root.children() {
        if node.tag_name().name() == "properties" {
            for child in node.children() {
                if child.is_element() {
                    if let Some(val) = child.text() {
                        props.insert(child.tag_name().name().to_string(), val.trim().to_string());
                    }
                }
            }
        }
    }

    for node in root.descendants() {
        if node.tag_name().name() == "dependencyManagement" {
            for deps_node in node.descendants() {
                if deps_node.tag_name().name() == "dependency" {
                    let mut group_id = String::new();
                    let mut artifact_id = String::new();
                    let mut version = String::new();
                    let mut scope = String::new();
                    let mut dep_type = String::new();

                    for child in deps_node.children() {
                        match child.tag_name().name() {
                            "groupId" => group_id = child.text().unwrap_or("").trim().to_string(),
                            "artifactId" => artifact_id = child.text().unwrap_or("").trim().to_string(),
                            "version" => version = child.text().unwrap_or("").trim().to_string(),
                            "scope" => scope = child.text().unwrap_or("").trim().to_string(),
                            "type" => dep_type = child.text().unwrap_or("").trim().to_string(),
                            _ => {}
                        }
                    }

                    if group_id.is_empty() || artifact_id.is_empty() || version.is_empty() {
                        continue;
                    }

                    let resolved_version = resolve_pom_properties(&version, &props);

                    if scope == "import" && dep_type == "pom" {
                        // BOM import — collect for recursive resolution
                        bom_imports.push((
                            resolve_pom_properties(&group_id, &props),
                            resolve_pom_properties(&artifact_id, &props),
                            resolved_version,
                        ));
                    } else {
                        let coord = format!("{}:{}", group_id, artifact_id);
                        versions.entry(coord).or_insert(resolved_version);
                    }
                }
            }
        }
    }
}

/// Resolve ${property} references in a POM version string.
fn resolve_pom_properties(value: &str, props: &std::collections::HashMap<String, String>) -> String {
    let mut result = value.to_string();
    for _ in 0..10 {
        if !result.contains("${") {
            break;
        }
        let mut new_result = result.clone();
        while let Some(start) = new_result.find("${") {
            if let Some(end) = new_result[start..].find('}') {
                let key = &new_result[start + 2..start + end];
                if let Some(val) = props.get(key) {
                    new_result = format!("{}{}{}", &new_result[..start], val, &new_result[start + end + 1..]);
                } else {
                    break;
                }
            } else {
                break;
            }
        }
        if new_result == result {
            break;
        }
        result = new_result;
    }
    result
}

/// Derive Spring Framework version from Spring Boot version.
/// Spring Boot 4.0.x → Spring Framework 7.0.x
/// Spring Boot 3.4.x → Spring Framework 6.2.x
/// Returns None if version pattern is unrecognized.
fn spring_boot_to_framework_version(boot_ver: &str) -> Option<String> {
    let parts: Vec<&str> = boot_ver.split('.').collect();
    if parts.len() < 2 { return None; }
    let major: u32 = parts[0].parse().ok()?;
    let minor: u32 = parts[1].parse().ok()?;
    // Mapping: Boot 4.0 → Framework 7.0, Boot 3.4 → Framework 6.2, Boot 3.3 → Framework 6.1
    let (fw_major, fw_minor) = match (major, minor) {
        (4, m) => (7, m),          // Boot 4.x → Framework 7.x
        (3, 4) => (6, 2),          // Boot 3.4 → Framework 6.2
        (3, 3) => (6, 1),          // Boot 3.3 → Framework 6.1
        (3, m) => (6, m.saturating_sub(1)), // approximate
        _ => return None,
    };
    let patch = if parts.len() >= 3 { parts[2] } else { "0" };
    Some(format!("{}.{}.{}", fw_major, fw_minor, patch))
}

fn extract_string_value(line: &str) -> Option<String> {
    // Handle: key = 'value' or key = "value" or key = value
    let parts: Vec<&str> = line.splitn(2, '=').collect();
    if parts.len() != 2 {
        return None;
    }
    let val = parts[1].trim().trim_matches('\'').trim_matches('"').trim();
    if val.is_empty() || val.contains("${") {
        None
    } else {
        Some(val.to_string())
    }
}

/// Extract string argument from a Gradle method call: `group "com.example"` or `group 'com.example'`
fn extract_string_arg(line: &str) -> Option<String> {
    // Find first quoted string after a space (method-call style: `group "value"`)
    let trimmed = line.trim();
    for (i, c) in trimmed.char_indices() {
        if c == '\'' || c == '"' {
            let rest = &trimmed[i + 1..];
            if let Some(end) = rest.find(c) {
                let val = &rest[..end];
                if !val.is_empty() && !val.contains("${") {
                    return Some(val.to_string());
                }
            }
            break;
        }
    }
    None
}

fn parse_gradle_dependency(line: &str, prefix: &str) -> Option<(String, String)> {
    let trimmed = line.trim_start();
    if !trimmed.starts_with(prefix) {
        return None;
    }
    let line = trimmed;
    // Skip project() dependencies
    if line.contains("project(") {
        return None;
    }
    // implementation 'group:artifact:version'
    // implementation "group:artifact:version"
    let start = line.find('\'').or_else(|| line.find('"'))?;
    let end = line.rfind('\'').or_else(|| line.rfind('"'))?;
    if start >= end {
        return None;
    }
    let dep = &line[start + 1..end];
    let parts: Vec<&str> = dep.split(':').collect();
    if parts.len() >= 3 {
        let coord = format!("{}:{}", parts[0], parts[1]);
        let version = parts[2].to_string();
        Some((coord, version))
    } else if parts.len() == 2 {
        // No version specified (e.g., Spring Boot BOM-managed dependency)
        let coord = format!("{}:{}", parts[0], parts[1]);
        Some((coord, String::new()))
    } else {
        None
    }
}

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

    #[test]
    fn test_parse_settings_gradle_basic() {
        let tmpdir = std::env::temp_dir().join("ym-settings-test");
        let _ = std::fs::remove_dir_all(&tmpdir);
        std::fs::create_dir_all(&tmpdir).unwrap();

        std::fs::write(
            tmpdir.join("settings.gradle"),
            "include ':module-a', ':module-b'\ninclude ':parent:child'\n",
        )
        .unwrap();

        let modules = parse_settings_gradle(&tmpdir.join("settings.gradle")).unwrap();
        assert!(modules.contains(&"module-a".to_string()));
        assert!(modules.contains(&"module-b".to_string()));
        assert!(modules.contains(&"parent/child".to_string()));

        let _ = std::fs::remove_dir_all(&tmpdir);
    }

    #[test]
    fn test_parse_settings_gradle_kts() {
        let tmpdir = std::env::temp_dir().join("ym-settings-kts-test");
        let _ = std::fs::remove_dir_all(&tmpdir);
        std::fs::create_dir_all(&tmpdir).unwrap();

        std::fs::write(
            tmpdir.join("settings.gradle.kts"),
            r#"include(":core", ":web", ":api")"#,
        )
        .unwrap();

        let modules = parse_settings_gradle(&tmpdir.join("settings.gradle.kts")).unwrap();
        assert_eq!(modules.len(), 3);
        assert!(modules.contains(&"core".to_string()));
        assert!(modules.contains(&"web".to_string()));
        assert!(modules.contains(&"api".to_string()));

        let _ = std::fs::remove_dir_all(&tmpdir);
    }

    #[test]
    fn test_parse_gradle_project_dependency() {
        assert_eq!(
            parse_gradle_project_dependency("implementation project(':module-a')"),
            Some(":module-a".to_string())
        );
        assert_eq!(
            parse_gradle_project_dependency(r#"implementation(project(":core"))"#),
            Some(":core".to_string())
        );
        assert_eq!(
            parse_gradle_project_dependency("implementation 'com.example:lib:1.0'"),
            None
        );
    }

    #[test]
    fn test_parse_gradle_dependency_skips_project() {
        assert!(parse_gradle_dependency("implementation project(':core')", "implementation").is_none());
    }

    #[test]
    fn test_migrate_from_pom_basic() {
        let tmpdir = std::env::temp_dir().join("ym-pom-migrate-test");
        let _ = std::fs::remove_dir_all(&tmpdir);
        std::fs::create_dir_all(&tmpdir).unwrap();

        let pom = r#"<?xml version="1.0"?>
<project>
    <groupId>com.example</groupId>
    <artifactId>my-app</artifactId>
    <version>1.0.0</version>
    <properties>
        <maven.compiler.source>17</maven.compiler.source>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>33.0.0-jre</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>"#;
        let pom_path = tmpdir.join("pom.xml");
        std::fs::write(&pom_path, pom).unwrap();

        let cfg = migrate_from_pom(&pom_path).unwrap();
        assert_eq!(cfg.name, "my-app");
        assert_eq!(cfg.version.as_deref(), Some("1.0.0"));
        assert_eq!(cfg.target.as_deref(), Some("17"));
        let deps = cfg.dependencies.unwrap();
        assert!(deps.contains_key("com.google.guava:guava"));
        assert!(deps.contains_key("junit:junit"));
        // junit should have test scope
        match deps.get("junit:junit").unwrap() {
            DependencyValue::Detailed(spec) => assert_eq!(spec.scope.as_deref(), Some("test")),
            _ => panic!("Expected detailed dep for test scope"),
        }

        let _ = std::fs::remove_dir_all(&tmpdir);
    }

    #[test]
    fn test_migrate_from_gradle_basic() {
        let tmpdir = std::env::temp_dir().join("ym-gradle-migrate-test");
        let _ = std::fs::remove_dir_all(&tmpdir);
        std::fs::create_dir_all(&tmpdir).unwrap();

        let gradle = r#"
group = 'com.example'
version = '2.0.0'
sourceCompatibility = '21'

dependencies {
    implementation 'org.springframework:spring-core:5.3.0'
    testImplementation 'junit:junit:4.13'
}
"#;
        let gradle_path = tmpdir.join("build.gradle");
        std::fs::write(&gradle_path, gradle).unwrap();

        let cfg = migrate_from_gradle(&gradle_path).unwrap();
        assert_eq!(cfg.name, "ym-gradle-migrate-test"); // name from dir (no settings.gradle rootProject.name)
        assert_eq!(cfg.group_id, "com.example");
        assert_eq!(cfg.version.as_deref(), Some("2.0.0"));
        assert_eq!(cfg.target.as_deref(), Some("21"));
        let deps = cfg.dependencies.unwrap();
        assert!(deps.contains_key("org.springframework:spring-core"));

        let _ = std::fs::remove_dir_all(&tmpdir);
    }

    #[test]
    fn test_find_modules_maven() {
        let pom = r#"<?xml version="1.0"?>
<project>
    <modules>
        <module>core</module>
        <module>web</module>
        <module>api</module>
    </modules>
</project>"#;
        let doc = roxmltree::Document::parse(pom).unwrap();
        let modules = find_modules(&doc.root_element());
        assert_eq!(modules, vec!["core", "web", "api"]);
    }

    #[test]
    fn test_gradle_kotlin_dsl_dependency() {
        // Kotlin DSL uses implementation("group:artifact:version")
        let dep = parse_gradle_dependency(
            r#"    implementation("org.springframework.boot:spring-boot-starter-web:3.2.0")"#,
            "implementation",
        );
        assert!(dep.is_some());
        let (coord, ver) = dep.unwrap();
        assert_eq!(coord, "org.springframework.boot:spring-boot-starter-web");
        assert_eq!(ver, "3.2.0");
    }

    #[test]
    fn test_gradle_kotlin_dsl_test_dep() {
        let dep = parse_gradle_dependency(
            r#"    testImplementation("org.junit.jupiter:junit-jupiter:5.10.0")"#,
            "testImplementation",
        );
        assert!(dep.is_some());
        let (coord, ver) = dep.unwrap();
        assert_eq!(coord, "org.junit.jupiter:junit-jupiter");
        assert_eq!(ver, "5.10.0");
    }

    #[test]
    fn test_migrate_pom_multimodule_detection() {
        let tmpdir = std::env::temp_dir().join("ym-pom-multimod-test");
        let _ = std::fs::remove_dir_all(&tmpdir);
        std::fs::create_dir_all(&tmpdir).unwrap();

        let pom = r#"<?xml version="1.0"?>
<project>
    <groupId>com.example</groupId>
    <artifactId>parent</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>
    <modules>
        <module>core</module>
        <module>web</module>
    </modules>
</project>"#;
        std::fs::write(tmpdir.join("pom.xml"), pom).unwrap();

        let cfg = migrate_from_pom(&tmpdir.join("pom.xml")).unwrap();
        assert_eq!(cfg.private, Some(true));
        assert!(cfg.workspaces.is_some());
        let ws = cfg.workspaces.unwrap();
        assert!(ws.contains(&"core/*".to_string()));
        assert!(ws.contains(&"web/*".to_string()));

        let _ = std::fs::remove_dir_all(&tmpdir);
    }

    #[test]
    fn test_gradle_project_dep_kotlin_dsl() {
        // Kotlin DSL project dependency
        assert_eq!(
            parse_gradle_project_dependency(r#"    implementation(project(":common"))"#),
            Some(":common".to_string())
        );
    }

    #[test]
    fn test_gradle_project_dep_api_scope() {
        assert_eq!(
            parse_gradle_project_dependency("api project(':shared-lib')"),
            Some(":shared-lib".to_string())
        );
    }

    #[test]
    fn test_settings_gradle_separate_includes() {
        let tmpdir = std::env::temp_dir().join("ym-settings-sep-test");
        let _ = std::fs::remove_dir_all(&tmpdir);
        std::fs::create_dir_all(&tmpdir).unwrap();

        // Each include on separate line
        std::fs::write(
            tmpdir.join("settings.gradle"),
            "include ':core'\ninclude ':web'\ninclude ':api'\n",
        )
        .unwrap();

        let modules = parse_settings_gradle(&tmpdir.join("settings.gradle")).unwrap();
        assert_eq!(modules.len(), 3);
        assert!(modules.contains(&"core".to_string()));
        assert!(modules.contains(&"web".to_string()));
        assert!(modules.contains(&"api".to_string()));

        let _ = std::fs::remove_dir_all(&tmpdir);
    }

    #[test]
    fn test_extract_string_value_variants() {
        assert_eq!(extract_string_value("key = 'value'"), Some("value".to_string()));
        assert_eq!(extract_string_value("key = \"value\""), Some("value".to_string()));
        assert_eq!(extract_string_value("key = plain"), Some("plain".to_string()));
        assert_eq!(extract_string_value("no_equals"), None);
        assert_eq!(extract_string_value("key = "), None);
    }

    #[test]
    fn test_detect_java_version_from_properties() {
        let pom = r#"<?xml version="1.0"?>
<project>
    <properties>
        <java.version>21</java.version>
    </properties>
</project>"#;
        let doc = roxmltree::Document::parse(pom).unwrap();
        assert_eq!(detect_java_version(&doc.root_element()), Some("21".to_string()));
    }

    #[test]
    fn test_detect_main_class() {
        let pom = r#"<?xml version="1.0"?>
<project>
    <build>
        <plugins>
            <plugin>
                <configuration>
                    <mainClass>com.example.App</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>"#;
        let doc = roxmltree::Document::parse(pom).unwrap();
        assert_eq!(detect_main_class(&doc.root_element()), Some("com.example.App".to_string()));
    }
}