oxo-call 0.11.0

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

// ─── Workflow definition (parsed from TOML) ───────────────────────────────────

/// `[workflow]` block — human-readable metadata.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct WorkflowMeta {
    pub name: String,
    #[serde(default)]
    pub description: String,
    #[serde(default)]
    pub version: String,
}

/// A single `[[step]]` entry.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StepDef {
    /// Unique step identifier (used in `depends_on` lists).
    pub name: String,
    /// Shell command.  Supports `{wildcard}` and `{params.key}` substitution.
    pub cmd: String,
    /// Names of steps that must complete before this step starts.
    #[serde(default)]
    pub depends_on: Vec<String>,
    /// Input file patterns for freshness checking.
    #[serde(default)]
    pub inputs: Vec<String>,
    /// Output file patterns for freshness checking and skip-if-fresh logic.
    #[serde(default)]
    pub outputs: Vec<String>,
    /// When true, runs once after ALL wildcard expansions of dependency steps
    /// complete (like a gather/aggregate step, e.g. MultiQC).
    #[serde(default)]
    pub gather: bool,
    /// Optional shell preamble executed before the main command.
    ///
    /// Use this to activate conda environments, set interpreter paths, or
    /// configure environment variables for tools that need a specific runtime
    /// (e.g., a step requiring Python 2 vs. Python 3).
    ///
    /// Example values:
    /// - `"conda activate py2_env &&"`
    /// - `"source /opt/py2/bin/activate &&"`
    /// - `"export PATH=/opt/star-2.7.11b/bin:$PATH &&"`
    #[serde(default)]
    pub env: Option<String>,
}

/// Top-level workflow definition (the parsed `.oxo.toml`).
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct WorkflowDef {
    pub workflow: WorkflowMeta,
    /// Named wildcard lists.  Each key can appear as `{key}` in step fields.
    #[serde(default)]
    pub wildcards: HashMap<String, Vec<String>>,
    /// String parameters accessible as `{params.key}` in step commands.
    #[serde(default)]
    pub params: HashMap<String, String>,
    /// Pipeline steps (ordered; later steps may depend on earlier ones).
    #[serde(rename = "step", default)]
    pub steps: Vec<StepDef>,
}

impl WorkflowDef {
    /// Load and parse a workflow from a TOML file on disk.
    pub fn from_file(path: &Path) -> Result<Self> {
        let text = std::fs::read_to_string(path)?;
        Self::from_str_content(&text)
    }

    /// Parse a workflow from a TOML string.
    pub fn from_str_content(s: &str) -> Result<Self> {
        Ok(toml::from_str(s)?)
    }
}

// ─── Concrete task (wildcard-expanded) ───────────────────────────────────────

/// A fully expanded, runnable unit of work.
#[derive(Debug, Clone)]
pub struct ConcreteTask {
    /// Unique task identifier: `"<step>"` or `"<step>[key=val,…]"`.
    pub id: String,
    pub step_name: String,
    /// Fully substituted shell command (ready to pass to `sh -c`).
    pub cmd: String,
    /// Fully substituted input file paths.
    pub inputs: Vec<String>,
    /// Fully substituted output file paths.
    pub outputs: Vec<String>,
    /// IDs of tasks that must complete before this task can start.
    pub deps: Vec<String>,
    pub gather: bool,
    /// Optional shell preamble (e.g., conda activate, PATH override).
    pub env: Option<String>,
}

// ─── Wildcard expansion helpers ────────────────────────────────────────────────

/// Enumerate every combination of wildcard bindings.
fn wildcard_combinations(wildcards: &HashMap<String, Vec<String>>) -> Vec<HashMap<String, String>> {
    if wildcards.is_empty() {
        return vec![HashMap::new()];
    }
    let mut result: Vec<HashMap<String, String>> = vec![HashMap::new()];
    // Iterate in a deterministic order (sorted keys).
    let mut keys: Vec<&String> = wildcards.keys().collect();
    keys.sort();
    for key in keys {
        let values = &wildcards[key];
        let mut next = Vec::new();
        for val in values {
            for existing in &result {
                let mut m = existing.clone();
                m.insert(key.clone(), val.clone());
                next.push(m);
            }
        }
        result = next;
    }
    result
}

/// Substitute `{key}` (wildcard) and `{params.key}` placeholders.
fn substitute(
    template: &str,
    bindings: &HashMap<String, String>,
    params: &HashMap<String, String>,
) -> String {
    let mut s = template.to_string();
    // Wildcard substitution first (higher precedence).
    for (k, v) in bindings {
        s = s.replace(&format!("{{{k}}}"), v);
    }
    // Param substitution ({params.key} and bare {key} if not shadowed).
    for (k, v) in params {
        s = s.replace(&format!("{{params.{k}}}", k = k), v);
        // Bare {key} only if not already a wildcard key.
        if !bindings.contains_key(k.as_str()) {
            s = s.replace(&format!("{{{k}}}"), v);
        }
    }
    s
}

/// Build a canonical task ID from step name + wildcard bindings.
fn task_id(step_name: &str, bindings: &HashMap<String, String>) -> String {
    if bindings.is_empty() {
        return step_name.to_string();
    }
    let mut parts: Vec<String> = bindings.iter().map(|(k, v)| format!("{k}={v}")).collect();
    parts.sort();
    format!("{step_name}[{}]", parts.join(","))
}

/// Returns true if the step uses any wildcard key in any of its fields.
fn uses_wildcards(step: &StepDef, wildcards: &HashMap<String, Vec<String>>) -> bool {
    wildcards.keys().any(|k| {
        let pat = format!("{{{k}}}");
        step.cmd.contains(&pat)
            || step.inputs.iter().any(|i| i.contains(&pat))
            || step.outputs.iter().any(|o| o.contains(&pat))
    })
}

// ─── DAG construction ─────────────────────────────────────────────────────────

/// Expand a `WorkflowDef` into a flat list of fully resolved `ConcreteTask`s
/// with explicit inter-task dependency edges.
pub fn expand(def: &WorkflowDef) -> Result<Vec<ConcreteTask>> {
    let combos = wildcard_combinations(&def.wildcards);
    let mut tasks: Vec<ConcreteTask> = Vec::new();

    // Map: step_name → list of task IDs produced by that step.
    let mut step_tasks: HashMap<String, Vec<String>> = HashMap::new();

    for step in &def.steps {
        let wc = uses_wildcards(step, &def.wildcards);

        if step.gather || !wc || combos.len() <= 1 {
            // ── Single task (gather, no-wildcard, or only one combo) ────────
            let bindings: HashMap<String, String> = if !step.gather && wc && combos.len() == 1 {
                combos.first().expect("combos.len() == 1").clone()
            } else {
                HashMap::new()
            };

            let id = if step.gather {
                step.name.clone() // gather tasks always use the bare step name
            } else {
                task_id(&step.name, &bindings)
            };

            let deps = if step.gather {
                // Gather: depends on ALL tasks of each listed dep step.
                step.depends_on
                    .iter()
                    .flat_map(|dep| step_tasks.get(dep).cloned().unwrap_or_default())
                    .collect()
            } else {
                // Single combo: depends on same-combo task of each dep step.
                step.depends_on
                    .iter()
                    .flat_map(|dep| {
                        step_tasks
                            .get(dep)
                            .cloned()
                            .unwrap_or_default()
                            .into_iter()
                            .filter(|t| bindings.is_empty() || *t == task_id(dep, &bindings))
                    })
                    .collect()
            };

            let t = ConcreteTask {
                id: id.clone(),
                step_name: step.name.clone(),
                cmd: substitute(&step.cmd, &bindings, &def.params),
                inputs: step
                    .inputs
                    .iter()
                    .map(|i| substitute(i, &bindings, &def.params))
                    .collect(),
                outputs: step
                    .outputs
                    .iter()
                    .map(|o| substitute(o, &bindings, &def.params))
                    .collect(),
                deps,
                gather: step.gather,
                env: step.env.clone(),
            };
            step_tasks.entry(step.name.clone()).or_default().push(id);
            tasks.push(t);
        } else {
            // ── Per-combo expansion ─────────────────────────────────────────
            for bindings in &combos {
                let id = task_id(&step.name, bindings);

                let deps: Vec<String> = step
                    .depends_on
                    .iter()
                    .flat_map(|dep| {
                        step_tasks
                            .get(dep)
                            .cloned()
                            .unwrap_or_default()
                            .into_iter()
                            .filter(|t| *t == task_id(dep, bindings))
                    })
                    .collect();

                let t = ConcreteTask {
                    id: id.clone(),
                    step_name: step.name.clone(),
                    cmd: substitute(&step.cmd, bindings, &def.params),
                    inputs: step
                        .inputs
                        .iter()
                        .map(|i| substitute(i, bindings, &def.params))
                        .collect(),
                    outputs: step
                        .outputs
                        .iter()
                        .map(|o| substitute(o, bindings, &def.params))
                        .collect(),
                    deps,
                    gather: false,
                    env: step.env.clone(),
                };
                step_tasks.entry(step.name.clone()).or_default().push(id);
                tasks.push(t);
            }
        }
    }

    // Detect dependency cycles via a simple forward-reachability check.
    let id_to_idx: HashMap<&str, usize> = tasks
        .iter()
        .enumerate()
        .map(|(i, t)| (t.id.as_str(), i))
        .collect();
    for task in &tasks {
        for dep in &task.deps {
            if !id_to_idx.contains_key(dep.as_str()) {
                return Err(OxoError::ExecutionError(format!(
                    "Step '{}' depends on unknown step/task '{dep}'",
                    task.step_name
                )));
            }
        }
    }

    Ok(tasks)
}

// ─── Output-freshness caching ─────────────────────────────────────────────────

fn mtime(path: &str) -> Option<SystemTime> {
    std::fs::metadata(path).ok()?.modified().ok()
}

/// Returns `true` if all outputs exist and are at least as new as all inputs.
fn is_up_to_date(task: &ConcreteTask) -> bool {
    if task.outputs.is_empty() {
        return false; // No declared outputs — always run.
    }
    let Some(oldest_output) = task
        .outputs
        .iter()
        .map(|o| mtime(o))
        .collect::<Option<Vec<_>>>()
        .and_then(|v| v.into_iter().min())
    else {
        return false; // At least one output is missing.
    };
    // Every input must be older than (or equal to) the oldest output.
    task.inputs
        .iter()
        .all(|i| mtime(i).is_none_or(|t| t <= oldest_output))
}

// ─── DAG phase computation ────────────────────────────────────────────────────

/// Compute execution phases: groups of tasks that can run in parallel.
///
/// Each phase contains tasks whose dependencies are all satisfied by
/// earlier phases.  Within a phase, all tasks are independent and can
/// execute concurrently.
pub fn compute_phases(tasks: &[ConcreteTask]) -> Vec<Vec<&ConcreteTask>> {
    let mut phases: Vec<Vec<&ConcreteTask>> = Vec::new();
    let mut assigned: HashSet<&str> = HashSet::new();
    let mut remaining: Vec<&ConcreteTask> = tasks.iter().collect();

    while !remaining.is_empty() {
        let (ready, rest): (Vec<&ConcreteTask>, Vec<&ConcreteTask>) = remaining
            .into_iter()
            .partition(|t| t.deps.iter().all(|d| assigned.contains(d.as_str())));

        if ready.is_empty() {
            // All remaining tasks have unsatisfied deps — cycle or error.
            break;
        }

        for t in &ready {
            assigned.insert(&t.id);
        }
        phases.push(ready);
        remaining = rest;
    }

    phases
}

/// Format elapsed time as human-readable string.
fn format_elapsed(elapsed: std::time::Duration) -> String {
    let secs = elapsed.as_secs();
    if secs < 60 {
        format!("{:.1}s", elapsed.as_secs_f64())
    } else if secs < 3600 {
        format!("{}m {:02}s", secs / 60, secs % 60)
    } else {
        format!(
            "{}h {:02}m {:02}s",
            secs / 3600,
            (secs % 3600) / 60,
            secs % 60
        )
    }
}

/// Print the DAG phase diagram for a set of tasks.
fn print_dag_phases(tasks: &[ConcreteTask]) {
    let phases = compute_phases(tasks);
    if phases.is_empty() {
        return;
    }

    println!();
    println!(
        "  {} {}",
        "Pipeline DAG".bold(),
        format!("({} phases, {} tasks)", phases.len(), tasks.len()).dimmed()
    );
    println!();

    for (i, phase) in phases.iter().enumerate() {
        let phase_num = format!("Phase {}", i + 1);

        // Group by step_name to show compact per-sample expansion.
        let mut groups: Vec<(String, Vec<String>)> = Vec::new();
        for t in phase.iter() {
            let label = if t.gather {
                format!("{} [gather]", t.id)
            } else {
                t.id.clone()
            };
            if let Some(g) = groups.last_mut().filter(|(name, _)| name == &t.step_name) {
                g.1.push(label);
            } else {
                groups.push((t.step_name.clone(), vec![label]));
            }
        }

        let mut group_strs: Vec<String> = Vec::new();
        for (_, items) in &groups {
            if items.len() <= 3 {
                group_strs.extend(items.iter().cloned());
            } else {
                // Show first two and a count.
                group_strs.push(items[0].clone());
                group_strs.push(format!("… +{} more", items.len() - 1));
            }
        }

        println!("  {:>9}  {}", phase_num.cyan(), group_strs.join(""));

        if i + 1 < phases.len() {
            println!("  {:>9}  {}", "", "".dimmed());
        }
    }

    println!();
}

// ─── Async executor ────────────────────────────────────────────────────────────

/// Execute a task graph with maximum parallelism.
///
/// Independent tasks (no mutual dependencies) run concurrently via
/// `tokio::task::JoinSet`.  Dependent tasks wait until all their prerequisites
/// have succeeded or been skipped.  If any task fails the whole run is aborted.
#[cfg(not(target_arch = "wasm32"))]
pub async fn execute(tasks: Vec<ConcreteTask>, dry_run: bool) -> Result<()> {
    use tokio::task::JoinSet;

    if tasks.is_empty() {
        println!("{}", "Workflow has no steps to execute.".yellow());
        return Ok(());
    }

    let total = tasks.len();
    let start_time = Instant::now();

    println!();
    println!(
        "{} {}{} task(s){}",
        "".cyan().bold(),
        "oxo workflow".bold(),
        total,
        if dry_run { " (dry-run)" } else { "" }
    );
    println!("{}", "".repeat(60).dimmed());

    // Print DAG phase diagram.
    print_dag_phases(&tasks);

    println!("{}", "".repeat(60).dimmed());

    // Set of completed task IDs (includes both run and skipped).
    let mut done: HashSet<String> = HashSet::new();
    // Tasks that have been dispatched (to avoid double-dispatch).
    let mut started: HashSet<String> = HashSet::new();
    // Separately track which tasks were skipped (up-to-date).
    let mut skipped_count: usize = 0;
    let mut completed_count: usize = 0;
    let mut join_set: JoinSet<Result<(String, bool /*skipped*/)>> = JoinSet::new();

    let mut iterations_without_progress = 0usize;

    loop {
        // Find all tasks whose dependencies are fully satisfied.
        let newly_ready: Vec<&ConcreteTask> = tasks
            .iter()
            .filter(|t| !started.contains(&t.id) && t.deps.iter().all(|d| done.contains(d)))
            .collect();

        for task in newly_ready {
            started.insert(task.id.clone());
            if dry_run {
                print_task_dry_run(task, completed_count + 1, total);
                done.insert(task.id.clone());
                completed_count += 1;
            } else {
                let t = task.clone();
                join_set.spawn(async move { run_single_task(t).await });
            }
        }

        if dry_run {
            // In dry-run we process synchronously; no JoinSet to wait on.
            if done.len() == total {
                break;
            }
            iterations_without_progress += 1;
            if iterations_without_progress > total {
                return Err(OxoError::ExecutionError(
                    "Workflow dry-run stalled: possible dependency cycle".to_string(),
                ));
            }
            continue;
        }

        if done.len() == total {
            break;
        }

        match join_set.join_next().await {
            None => {
                // JoinSet is empty but we haven't finished — cycle or missing dep.
                if done.len() < total {
                    return Err(OxoError::ExecutionError(
                        "Workflow stalled: possible dependency cycle or missing step".to_string(),
                    ));
                }
                break;
            }
            Some(result) => {
                let (id, skipped) = result
                    .map_err(|e| OxoError::ExecutionError(format!("Task join error: {e}")))??;
                completed_count += 1;
                if skipped {
                    skipped_count += 1;
                    println!(
                        "  {} [{}/{}] {} {}",
                        "".dimmed(),
                        completed_count,
                        total,
                        id.dimmed(),
                        "(up to date)".dimmed()
                    );
                } else {
                    println!(
                        "  {} [{}/{}] {}",
                        "".green().bold(),
                        completed_count,
                        total,
                        id.green()
                    );
                }
                done.insert(id);
            }
        }
    }

    let elapsed = start_time.elapsed();
    println!("{}", "".repeat(60).dimmed());
    let run_count = started.len();
    if dry_run {
        println!(
            "\n{} {} task(s) would execute across {} phase(s)",
            "".cyan().bold(),
            run_count,
            compute_phases(&tasks).len()
        );
    } else {
        println!(
            "\n{} Workflow complete — {} task(s) run, {} up to date  ({})",
            "".green().bold(),
            run_count.saturating_sub(skipped_count),
            skipped_count,
            format_elapsed(elapsed)
        );
    }

    Ok(())
}

/// Run a single concrete task via `sh -c`.
#[cfg(not(target_arch = "wasm32"))]
async fn run_single_task(task: ConcreteTask) -> Result<(String, bool)> {
    use tokio::process::Command;

    // Skip if all outputs are newer than all inputs.
    if is_up_to_date(&task) {
        return Ok((task.id, true));
    }

    println!("  {} {}", "".cyan().bold(), task.id.cyan().bold());
    println!("    {}", task.cmd.dimmed());

    // Ensure output parent directories exist.
    for out in &task.outputs {
        if let Some(parent) = Path::new(out).parent() {
            std::fs::create_dir_all(parent)?;
        }
    }

    // Build the full shell command, prepending env preamble if set.
    let full_cmd = match &task.env {
        Some(env) => format!("{env} {}", task.cmd),
        None => task.cmd.clone(),
    };

    let status = Command::new("sh")
        .arg("-c")
        .arg(&full_cmd)
        .status()
        .await
        .map_err(|e| {
            OxoError::ExecutionError(format!("Failed to start task '{}': {e}", task.id))
        })?;

    if !status.success() {
        return Err(OxoError::ExecutionError(format!(
            "Task '{}' failed with exit code {}",
            task.id,
            status.code().unwrap_or(-1)
        )));
    }

    Ok((task.id, false))
}

/// Print a dry-run preview for a single task.
fn print_task_dry_run(task: &ConcreteTask, current: usize, total: usize) {
    println!(
        "  {} [{}/{}] {}",
        "".cyan(),
        current,
        total,
        if task.gather {
            format!("{} [gather]", task.id).cyan().bold().to_string()
        } else {
            task.id.cyan().bold().to_string()
        }
    );
    println!("    $ {}", task.cmd.dimmed());
    if let Some(env) = &task.env {
        println!("    env:     {}", env.dimmed());
    }
    if !task.deps.is_empty() {
        println!("    after:   {}", task.deps.join(", ").dimmed());
    }
    if !task.inputs.is_empty() {
        println!("    inputs:  {}", task.inputs.join("  ").dimmed());
    }
    if !task.outputs.is_empty() {
        println!("    outputs: {}", task.outputs.join("  ").dimmed());
    }
    println!();
}

// ─── Snakemake export ─────────────────────────────────────────────────────────

/// Convert a native `WorkflowDef` into a Snakemake `Snakefile`.
pub fn to_snakemake(def: &WorkflowDef) -> String {
    let mut out = String::new();

    out.push_str(&format!(
        "# Generated by oxo-call — workflow '{}'\n",
        def.workflow.name
    ));
    if !def.workflow.description.is_empty() {
        out.push_str(&format!("# {}\n", def.workflow.description));
    }
    out.push_str("# Edit this file or re-run 'oxo-call workflow export' to regenerate.\n\n");

    out.push_str("configfile: \"config.yaml\"\n\n");

    // Wildcard lists as Python variables.
    if !def.wildcards.is_empty() {
        out.push_str("# ── Wildcard sample lists ─────────────────────────────────────────────\n");
        let mut keys: Vec<&String> = def.wildcards.keys().collect();
        keys.sort();
        for k in &keys {
            let vals = &def.wildcards[*k];
            let quoted: Vec<String> = vals.iter().map(|v| format!("\"{v}\"")).collect();
            out.push_str(&format!("{} = [{}]\n", k.to_uppercase(), quoted.join(", ")));
        }
        out.push('\n');
    }

    // rule all — collect the "leaf" outputs (steps no other step depends on).
    let depended_on: HashSet<&str> = def
        .steps
        .iter()
        .flat_map(|s| s.depends_on.iter().map(|d| d.as_str()))
        .collect();
    let leaf_steps: Vec<&StepDef> = def
        .steps
        .iter()
        .filter(|s| !depended_on.contains(s.name.as_str()))
        .collect();

    out.push_str("rule all:\n    input:\n");
    for step in &leaf_steps {
        for out_pat in &step.outputs {
            if step.gather || !uses_wildcards(step, &def.wildcards) || def.wildcards.is_empty() {
                out.push_str(&format!("        \"{out_pat}\",\n"));
            } else {
                // Use Snakemake's expand() for wildcard outputs.
                let mut keys: Vec<&String> = def.wildcards.keys().collect();
                keys.sort();
                let expand_args: Vec<String> = keys
                    .iter()
                    .map(|k| format!("{k}={ku}", ku = k.to_uppercase()))
                    .collect();
                out.push_str(&format!(
                    "        expand(\"{out_pat}\", {}),\n",
                    expand_args.join(", ")
                ));
            }
        }
    }
    out.push('\n');

    // Individual rules.
    for step in &def.steps {
        out.push_str(&format!("\nrule {}:\n", step.name));
        if !step.inputs.is_empty() {
            out.push_str("    input:\n");
            for inp in &step.inputs {
                out.push_str(&format!("        \"{inp}\",\n"));
            }
        }
        if !step.outputs.is_empty() {
            out.push_str("    output:\n");
            for outp in &step.outputs {
                out.push_str(&format!("        \"{outp}\",\n"));
            }
        }
        out.push_str(&format!("    log: \"logs/{}.log\"\n", step.name));
        out.push_str("    shell:\n");
        // Rewrite wildcards and params for Snakemake.
        let mut cmd = step.cmd.clone();
        let mut keys: Vec<&String> = def.wildcards.keys().collect();
        keys.sort();
        for k in &keys {
            cmd = cmd.replace(&format!("{{{k}}}"), &format!("{{wildcards.{k}}}"));
        }
        let mut pkeys: Vec<&String> = def.params.keys().collect();
        pkeys.sort();
        for k in &pkeys {
            cmd = cmd.replace(&format!("{{params.{k}}}"), &format!("{{config[\"{k}\"]}}"));
        }
        // Escape for Snakemake shell string (double-quote safe).
        let cmd_escaped = cmd.replace('"', "\\\"");
        out.push_str(&format!("        \"{cmd_escaped}\"\n"));
    }

    // config.yaml comment block.
    if !def.params.is_empty() {
        out.push_str(
            "\n# ─── config.yaml (create this file next to your Snakefile) ───────────────\n",
        );
        out.push_str("# ");
        let mut pkeys: Vec<&String> = def.params.keys().collect();
        pkeys.sort();
        for k in pkeys {
            out.push_str(&format!("{k}: \"{}\"\n# ", def.params[k]));
        }
        out.push('\n');
    }

    out
}

// ─── Nextflow export ──────────────────────────────────────────────────────────

/// Convert a native `WorkflowDef` into a Nextflow DSL2 `.nf` file.
pub fn to_nextflow(def: &WorkflowDef) -> String {
    let mut out = String::new();

    out.push_str("#!/usr/bin/env nextflow\n");
    out.push_str(&format!(
        "// Generated by oxo-call — workflow '{}'\n",
        def.workflow.name
    ));
    if !def.workflow.description.is_empty() {
        out.push_str(&format!("// {}\n", def.workflow.description));
    }
    out.push_str("// Edit this file or re-run 'oxo-call workflow export' to regenerate.\n\n");
    out.push_str("nextflow.enable.dsl = 2\n\n");

    // params block.
    let mut pkeys: Vec<&String> = def.params.keys().collect();
    pkeys.sort();
    for k in &pkeys {
        let v = def.params.get(k.as_str()).map(String::as_str).unwrap_or("");
        out.push_str(&format!("params.{k} = \"{v}\"\n"));
    }
    if !def.wildcards.is_empty() {
        out.push_str("params.samplesheet = \"samplesheet.csv\"\n");
    }
    out.push('\n');

    // Samplesheet channel.
    if !def.wildcards.is_empty() {
        let mut keys: Vec<&String> = def.wildcards.keys().collect();
        keys.sort();
        out.push_str(
            "// ── Sample channel ──────────────────────────────────────────────────────\n",
        );
        out.push_str("Channel\n");
        out.push_str("    .fromPath(params.samplesheet)\n");
        out.push_str("    .splitCsv(header: true)\n");
        let fields: Vec<String> = keys.iter().map(|k| format!("row.{k}")).collect();
        out.push_str(&format!(
            "    .map {{ row -> tuple({}) }}\n",
            fields.join(", ")
        ));
        out.push_str("    .set { samples_ch }\n\n");
    }

    // process blocks.
    for step in &def.steps {
        out.push_str(&format!("process {} {{\n", step.name.to_uppercase()));
        if !step.inputs.is_empty() {
            out.push_str("    input:\n");
            for inp in &step.inputs {
                out.push_str(&format!("    path '{}'\n", inp));
            }
        }
        if !step.outputs.is_empty() {
            out.push_str("    output:\n");
            for outp in &step.outputs {
                out.push_str(&format!("    path '{}'\n", outp));
            }
        }
        out.push_str("\n    script:\n    \"\"\"\n");

        // Rewrite {wildcard} → ${wildcard_val} and {params.key} → ${params.key}.
        let mut cmd = step.cmd.clone();
        let mut wc_keys: Vec<&String> = def.wildcards.keys().collect();
        wc_keys.sort();
        for k in &wc_keys {
            cmd = cmd.replace(&format!("{{{k}}}"), &format!("${{{k}}}"));
        }
        let mut p_keys: Vec<&String> = def.params.keys().collect();
        p_keys.sort();
        for k in &p_keys {
            cmd = cmd.replace(&format!("{{params.{k}}}"), &format!("${{params.{k}}}"));
        }

        out.push_str(&format!("    {cmd}\n"));
        out.push_str("    \"\"\"\n}\n\n");
    }

    // workflow block (linear chain, simplified).
    out.push_str("workflow {\n");
    if !def.wildcards.is_empty() {
        out.push_str("    ch = samples_ch\n");
    }
    for step in &def.steps {
        let process = step.name.to_uppercase();
        if def.wildcards.is_empty() {
            out.push_str(&format!("    {process}()\n"));
        } else if step.gather {
            out.push_str(&format!("    {process}(ch.collect())\n"));
        } else {
            out.push_str(&format!("    ch = {process}(ch)\n"));
        }
    }
    out.push_str("}\n");

    out
}

// ─── Verify ───────────────────────────────────────────────────────────────────

/// A single diagnostic produced by [`verify`].
#[derive(Debug, Clone)]
pub struct Diagnostic {
    pub level: DiagLevel,
    pub message: String,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DiagLevel {
    Error,
    Warning,
}

impl std::fmt::Display for DiagLevel {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            DiagLevel::Error => write!(f, "error"),
            DiagLevel::Warning => write!(f, "warning"),
        }
    }
}

/// Verify a parsed [`WorkflowDef`] for semantic correctness and return
/// a list of diagnostics (errors + warnings).
///
/// Returns `Ok(diags)` even if there are errors — the caller decides whether
/// to abort.  The vector is empty when the workflow is fully valid.
pub fn verify(def: &WorkflowDef) -> Vec<Diagnostic> {
    let mut diags: Vec<Diagnostic> = Vec::new();

    // ── Metadata checks ──────────────────────────────────────────────────────
    if def.workflow.name.is_empty() {
        diags.push(Diagnostic {
            level: DiagLevel::Warning,
            message: "[workflow] name is empty".to_string(),
        });
    }

    // ── Step name uniqueness ─────────────────────────────────────────────────
    let mut seen_names: HashSet<&str> = HashSet::new();
    for step in &def.steps {
        if step.name.is_empty() {
            diags.push(Diagnostic {
                level: DiagLevel::Error,
                message: "A step has an empty name".to_string(),
            });
        }
        if !seen_names.insert(step.name.as_str()) {
            diags.push(Diagnostic {
                level: DiagLevel::Error,
                message: format!("Duplicate step name: '{}'", step.name),
            });
        }
        if step.cmd.trim().is_empty() {
            diags.push(Diagnostic {
                level: DiagLevel::Error,
                message: format!("Step '{}' has an empty cmd", step.name),
            });
        }
    }

    // ── Unknown depends_on references ────────────────────────────────────────
    let all_names: HashSet<&str> = def.steps.iter().map(|s| s.name.as_str()).collect();
    for step in &def.steps {
        for dep in &step.depends_on {
            if !all_names.contains(dep.as_str()) {
                diags.push(Diagnostic {
                    level: DiagLevel::Error,
                    message: format!("Step '{}' depends on unknown step '{dep}'", step.name),
                });
            }
        }
    }

    // ── Forward-dependency ordering (informational) ──────────────────────────
    let name_to_pos: HashMap<&str, usize> = def
        .steps
        .iter()
        .enumerate()
        .map(|(i, s)| (s.name.as_str(), i))
        .collect();
    for step in &def.steps {
        let step_pos = name_to_pos[step.name.as_str()];
        for dep in &step.depends_on {
            if let Some(&dep_pos) = name_to_pos.get(dep.as_str())
                && dep_pos > step_pos
            {
                diags.push(Diagnostic {
                    level: DiagLevel::Warning,
                    message: format!(
                        "Step '{}' references '{}' which appears later in the file — \
                         dependency will be silently ignored at runtime",
                        step.name, dep
                    ),
                });
            }
        }
    }

    // ── Wildcard references in cmds ──────────────────────────────────────────
    let wc_keys: HashSet<String> = def.wildcards.keys().cloned().collect();
    let param_keys: HashSet<String> = def.params.keys().cloned().collect();
    for step in &def.steps {
        // Find {placeholder} references in cmd, inputs, outputs.
        let all_text = std::iter::once(step.cmd.as_str())
            .chain(step.inputs.iter().map(|s| s.as_str()))
            .chain(step.outputs.iter().map(|s| s.as_str()));
        for text in all_text {
            // Scan for {placeholder} patterns, skipping shell-style ${var} expansions.
            let bytes = text.as_bytes();
            let mut i = 0;
            while i < bytes.len() {
                if bytes[i] == b'{'
                    // Skip ${...} — shell variable expansion, not a workflow placeholder.
                    && (i == 0 || bytes[i - 1] != b'$')
                {
                    let rest = &text[i + 1..];
                    if let Some(end) = rest.find('}') {
                        let placeholder = &rest[..end];
                        if let Some(key) = placeholder.strip_prefix("params.") {
                            if !param_keys.contains(key) {
                                diags.push(Diagnostic {
                                    level: DiagLevel::Warning,
                                    message: format!(
                                        "Step '{}' references {{params.{key}}} but it is not defined in [params]",
                                        step.name
                                    ),
                                });
                            }
                        } else if !placeholder.is_empty()
                            && !wc_keys.contains(placeholder)
                            && !param_keys.contains(placeholder)
                        {
                            diags.push(Diagnostic {
                                level: DiagLevel::Warning,
                                message: format!(
                                    "Step '{}' references {{{placeholder}}} which is not a defined wildcard or param",
                                    step.name
                                ),
                            });
                        }
                        i += 1 + end + 1; // skip past the closing '}'
                        continue;
                    }
                }
                i += 1;
            }
        }
    }

    // ── Cycle detection via DAG expand ───────────────────────────────────────
    if diags.iter().all(|d| d.level != DiagLevel::Error)
        && let Err(e) = expand(def)
    {
        diags.push(Diagnostic {
            level: DiagLevel::Error,
            message: format!("DAG expansion failed: {e}"),
        });
    }

    diags
}

/// Print verify diagnostics to stdout in a human-friendly format.
/// Returns `true` if there are any errors.
pub fn print_verify_report(def: &WorkflowDef, diags: &[Diagnostic]) -> bool {
    let errors = diags.iter().filter(|d| d.level == DiagLevel::Error).count();
    let warnings = diags
        .iter()
        .filter(|d| d.level == DiagLevel::Warning)
        .count();

    println!(
        "{} workflow '{}' — {} step(s), {} wildcard(s)",
        "".cyan().bold(),
        def.workflow.name.bold(),
        def.steps.len(),
        def.wildcards.len()
    );

    if diags.is_empty() {
        println!("{} No issues found — workflow is valid", "".green().bold());
        return false;
    }

    println!("{}", "".repeat(60).dimmed());
    for d in diags {
        let (icon, colored_level) = match d.level {
            DiagLevel::Error => (
                "".red().bold().to_string(),
                "error".red().bold().to_string(),
            ),
            DiagLevel::Warning => (
                "".yellow().bold().to_string(),
                "warning".yellow().bold().to_string(),
            ),
        };
        println!("  {} [{}] {}", icon, colored_level, d.message);
    }
    println!("{}", "".repeat(60).dimmed());
    println!(
        "  {} error(s), {} warning(s)",
        if errors > 0 {
            errors.to_string().red().bold().to_string()
        } else {
            errors.to_string()
        },
        if warnings > 0 {
            warnings.to_string().yellow().bold().to_string()
        } else {
            warnings.to_string()
        },
    );

    errors > 0
}

// ─── Format ───────────────────────────────────────────────────────────────────

/// Serialize a [`WorkflowDef`] back to canonical `.oxo.toml` TOML text.
///
/// The output is deterministically ordered:
/// `[workflow]` → `[wildcards]` → `[params]` → `[[step]]` blocks.
pub fn format_toml(def: &WorkflowDef) -> String {
    let mut out = String::new();

    // ── [workflow] ────────────────────────────────────────────────────────────
    out.push_str("[workflow]\n");
    out.push_str(&format!("name        = {:?}\n", def.workflow.name));
    if !def.workflow.description.is_empty() {
        out.push_str(&format!("description = {:?}\n", def.workflow.description));
    }
    if !def.workflow.version.is_empty() {
        out.push_str(&format!("version     = {:?}\n", def.workflow.version));
    }
    out.push('\n');

    // ── [wildcards] ────────────────────────────────────────────────────────────
    if !def.wildcards.is_empty() {
        out.push_str("[wildcards]\n");
        let mut keys: Vec<&String> = def.wildcards.keys().collect();
        keys.sort();
        for k in &keys {
            let vals = &def.wildcards[*k];
            let quoted: Vec<String> = vals.iter().map(|v| format!("{v:?}")).collect();
            out.push_str(&format!("{k} = [{}]\n", quoted.join(", ")));
        }
        out.push('\n');
    }

    // ── [params] ──────────────────────────────────────────────────────────────
    if !def.params.is_empty() {
        out.push_str("[params]\n");
        let mut pkeys: Vec<&String> = def.params.keys().collect();
        pkeys.sort();
        for k in &pkeys {
            out.push_str(&format!("{k:<12} = {:?}\n", def.params[*k]));
        }
        out.push('\n');
    }

    // ── [[step]] blocks ────────────────────────────────────────────────────────
    for step in &def.steps {
        out.push_str("[[step]]\n");
        out.push_str(&format!("name       = {:?}\n", step.name));
        if step.gather {
            out.push_str("gather     = true\n");
        }
        if let Some(env) = &step.env {
            out.push_str(&format!("env        = {:?}\n", env));
        }
        if !step.depends_on.is_empty() {
            let deps: Vec<String> = step.depends_on.iter().map(|d| format!("{d:?}")).collect();
            out.push_str(&format!("depends_on = [{}]\n", deps.join(", ")));
        }
        // cmd: use multi-line literal for long commands
        let cmd = &step.cmd;
        if cmd.contains('\n') || cmd.len() > MAX_INLINE_CMD_LENGTH {
            // Render as triple-quoted string with backslash-continuation lines.
            out.push_str("cmd        = \"\"\"\\\n");
            for line in cmd.lines() {
                out.push_str(&format!("{line}\n"));
            }
            out.push_str("\"\"\"\n");
        } else {
            out.push_str(&format!("cmd        = {:?}\n", cmd));
        }
        if !step.inputs.is_empty() {
            let ins: Vec<String> = step.inputs.iter().map(|i| format!("{i:?}")).collect();
            out.push_str(&format!("inputs     = [{}]\n", ins.join(", ")));
        }
        if !step.outputs.is_empty() {
            let outs: Vec<String> = step.outputs.iter().map(|o| format!("{o:?}")).collect();
            out.push_str(&format!("outputs    = [{}]\n", outs.join(", ")));
        }
        out.push('\n');
    }

    // Remove trailing newline.
    out.pop();
    out
}

// Maximum command length for single-line formatting; longer commands use multi-line syntax.
const MAX_INLINE_CMD_LENGTH: usize = 80;

/// Print a rich DAG visualisation for a workflow to stdout.
///
/// Shows the phase diagram, step dependency table, and a summary.
pub fn visualize_workflow(def: &WorkflowDef) -> Result<()> {
    let tasks = expand(def)?;
    let phases = compute_phases(&tasks);

    println!();
    println!(
        "{} {} {}",
        "".cyan().bold(),
        format!("Workflow: {}", def.workflow.name).bold(),
        format!(
            "({} steps, {} tasks, {} phases)",
            def.steps.len(),
            tasks.len(),
            phases.len()
        )
        .dimmed()
    );
    if !def.workflow.description.is_empty() {
        println!("  {}", def.workflow.description.dimmed());
    }

    // ── Wildcard summary ──────────────────────────────────────────────────────
    if !def.wildcards.is_empty() {
        println!();
        println!("  {}", "Wildcards:".bold());
        let mut keys: Vec<&String> = def.wildcards.keys().collect();
        keys.sort();
        for k in &keys {
            let vals = &def.wildcards[k.as_str()];
            let preview = if vals.len() <= 4 {
                vals.join(", ")
            } else {
                format!("{}, … ({} total)", vals[..3].join(", "), vals.len())
            };
            println!("    {:<12} = [{}]", k.cyan(), preview);
        }
    }

    // ── Phase diagram (reuse existing helper) ─────────────────────────────────
    println!();
    println!("{}", "".repeat(60).dimmed());
    print_dag_phases(&tasks);
    println!("{}", "".repeat(60).dimmed());

    // ── Per-step dependency table ─────────────────────────────────────────────
    println!();
    println!("  {}", "Step details:".bold());
    println!(
        "  {:<18} {:<8} {:<8} {}",
        "Step".bold(),
        "Gather".bold(),
        "Tasks".bold(),
        "Depends on".bold()
    );
    println!("  {}", "".repeat(56).dimmed());
    for step in &def.steps {
        let task_count = tasks.iter().filter(|t| t.step_name == step.name).count();
        let gather_str = if step.gather { "yes" } else { "" };
        let deps_str = if step.depends_on.is_empty() {
            "(none)".dimmed().to_string()
        } else {
            step.depends_on.join(", ").cyan().to_string()
        };
        println!(
            "  {:<18} {:<8} {:<8} {}",
            step.name.cyan(),
            gather_str,
            task_count,
            deps_str
        );
    }

    println!();
    Ok(())
}

/// Post-execution LLM verification for a completed workflow.
///
/// Inspects each step's expected output files, then asks the LLM to summarise
/// the overall state of the workflow outputs and surface any issues.
///
/// This is a best-effort check: failures from the LLM are printed as warnings
/// rather than propagated as errors, so the exit code of `workflow run` is not
/// affected.
#[cfg(not(target_arch = "wasm32"))]
pub async fn verify_workflow_results(def: &WorkflowDef, config: &crate::config::Config) {
    use crate::llm::LlmClient;
    use crate::runner::make_spinner;
    use colored::Colorize;

    // Collect output file metadata for every step.
    let mut step_lines: Vec<String> = Vec::new();
    for step in &def.steps {
        step_lines.push(format!(
            "### Step: `{}`\nCommand: `{}`",
            step.name, step.cmd
        ));
        if step.outputs.is_empty() {
            step_lines.push("Outputs: (none declared)".to_string());
        } else {
            for path in &step.outputs {
                let size_info = match std::fs::metadata(path).ok().map(|m| m.len()) {
                    Some(bytes) => format!("{bytes} bytes"),
                    None => "**MISSING**".to_string(),
                };
                step_lines.push(format!("- `{path}` — {size_info}"));
            }
        }
    }

    // Collect all declared output files with sizes for the LLM call.
    let all_outputs: Vec<(String, Option<u64>)> = def
        .steps
        .iter()
        .flat_map(|s| s.outputs.iter())
        .map(|p| {
            let size = std::fs::metadata(p).ok().map(|m| m.len());
            (p.clone(), size)
        })
        .collect();

    let step_summary = step_lines.join("\n");
    let workflow_task = format!(
        "Workflow '{}': {}\n\n## Step Output Status\n{}",
        def.workflow.name, def.workflow.description, step_summary
    );
    let workflow_cmd = format!("oxo-call workflow run ({})", def.workflow.name);

    let spinner = make_spinner("Verifying workflow results with LLM...");
    let llm = LlmClient::new(config.clone());
    let verification = match llm
        .verify_run_result(
            &def.workflow.name,
            &workflow_task,
            &workflow_cmd,
            0,
            "",
            &all_outputs,
        )
        .await
    {
        Ok(v) => {
            spinner.finish_and_clear();
            v
        }
        Err(e) => {
            spinner.finish_and_clear();
            eprintln!(
                "{} LLM workflow verification failed: {}",
                "warning:".yellow().bold(),
                e
            );
            return;
        }
    };

    println!();
    println!("{}", "".repeat(60).dimmed());
    let label = if verification.success {
        "LLM Workflow Verification: OK".bold().green().to_string()
    } else {
        "LLM Workflow Verification: Issues detected"
            .bold()
            .red()
            .to_string()
    };
    println!("  {}", label);
    if !verification.summary.is_empty() {
        println!("  {}", verification.summary);
    }
    if !verification.issues.is_empty() {
        println!();
        println!("  {}", "Issues:".bold().yellow());
        for issue in &verification.issues {
            println!("    {} {}", "".yellow(), issue);
        }
    }
    if !verification.suggestions.is_empty() {
        println!();
        println!("  {}", "Suggestions:".bold().cyan());
        for sug in &verification.suggestions {
            println!("    {} {}", "".cyan(), sug);
        }
    }
    println!("{}", "".repeat(60).dimmed());
}

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

    /// Helper to build a minimal ConcreteTask for testing.
    fn task(id: &str, deps: &[&str]) -> ConcreteTask {
        ConcreteTask {
            id: id.to_string(),
            step_name: id.split('[').next().unwrap_or(id).to_string(),
            cmd: format!("echo {id}"),
            inputs: vec![],
            outputs: vec![],
            deps: deps.iter().map(|d| d.to_string()).collect(),
            gather: false,
            env: None,
        }
    }

    fn gather_task(id: &str, deps: &[&str]) -> ConcreteTask {
        let mut t = task(id, deps);
        t.gather = true;
        t
    }

    #[test]
    fn test_compute_phases_linear_chain() {
        let tasks = vec![task("a", &[]), task("b", &["a"]), task("c", &["b"])];
        let phases = compute_phases(&tasks);
        assert_eq!(phases.len(), 3);
        assert_eq!(phases[0].len(), 1);
        assert_eq!(phases[0][0].id, "a");
        assert_eq!(phases[1][0].id, "b");
        assert_eq!(phases[2][0].id, "c");
    }

    #[test]
    fn test_compute_phases_parallel_tasks() {
        let tasks = vec![task("a", &[]), task("b", &[]), task("c", &["a", "b"])];
        let phases = compute_phases(&tasks);
        assert_eq!(phases.len(), 2);
        // Phase 1: a and b in parallel.
        assert_eq!(phases[0].len(), 2);
        // Phase 2: c after both.
        assert_eq!(phases[1].len(), 1);
        assert_eq!(phases[1][0].id, "c");
    }

    #[test]
    fn test_compute_phases_diamond_dag() {
        // a → b, a → c, b+c → d
        let tasks = vec![
            task("a", &[]),
            task("b", &["a"]),
            task("c", &["a"]),
            task("d", &["b", "c"]),
        ];
        let phases = compute_phases(&tasks);
        assert_eq!(phases.len(), 3);
        assert_eq!(phases[0].len(), 1); // a
        assert_eq!(phases[1].len(), 2); // b, c
        assert_eq!(phases[2].len(), 1); // d
    }

    #[test]
    fn test_compute_phases_with_gather() {
        // Mimics rnaseq: fastp[s1], fastp[s2] → multiqc (gather)
        let tasks = vec![
            task("fastp[sample=s1]", &[]),
            task("fastp[sample=s2]", &[]),
            gather_task("multiqc", &["fastp[sample=s1]", "fastp[sample=s2]"]),
        ];
        let phases = compute_phases(&tasks);
        assert_eq!(phases.len(), 2);
        assert_eq!(phases[0].len(), 2); // both fastp tasks
        assert_eq!(phases[1].len(), 1); // multiqc
        assert!(phases[1][0].gather);
    }

    #[test]
    fn test_compute_phases_empty() {
        let tasks: Vec<ConcreteTask> = vec![];
        let phases = compute_phases(&tasks);
        assert_eq!(phases.len(), 0);
    }

    #[test]
    fn test_compute_phases_single_task() {
        let tasks = vec![task("only", &[])];
        let phases = compute_phases(&tasks);
        assert_eq!(phases.len(), 1);
        assert_eq!(phases[0].len(), 1);
    }

    #[test]
    fn test_compute_phases_complex_pipeline() {
        // Mimics the real RNA-seq pipeline with upstream QC aggregation:
        // fastp[s1,s2] → { multiqc (gather), star[s1,s2] } → samtools_index[s1,s2] → featurecounts[s1,s2]
        // MultiQC is an upstream QC aggregation step that depends on fastp only
        // and runs in parallel with the STAR alignment branch.
        let tasks = vec![
            task("fastp[sample=s1]", &[]),
            task("fastp[sample=s2]", &[]),
            gather_task("multiqc", &["fastp[sample=s1]", "fastp[sample=s2]"]),
            task("star[sample=s1]", &["fastp[sample=s1]"]),
            task("star[sample=s2]", &["fastp[sample=s2]"]),
            task("samtools_index[sample=s1]", &["star[sample=s1]"]),
            task("samtools_index[sample=s2]", &["star[sample=s2]"]),
            task("featurecounts[sample=s1]", &["samtools_index[sample=s1]"]),
            task("featurecounts[sample=s2]", &["samtools_index[sample=s2]"]),
        ];
        let phases = compute_phases(&tasks);
        assert_eq!(phases.len(), 4);
        assert_eq!(phases[0].len(), 2); // fastp[s1], fastp[s2]
        // Phase 2: multiqc + star[s1,s2] run in parallel (all depend only on fastp)
        assert_eq!(phases[1].len(), 3); // multiqc, star[s1], star[s2]
        assert!(
            phases[1].iter().any(|t| t.id == "multiqc"),
            "multiqc should be in the same phase as star"
        );
        assert_eq!(phases[2].len(), 2); // samtools_index[s1], samtools_index[s2]
        assert_eq!(phases[3].len(), 2); // featurecounts[s1], featurecounts[s2]
    }

    #[test]
    fn test_format_elapsed_seconds() {
        let d = std::time::Duration::from_secs_f64(5.3);
        assert_eq!(format_elapsed(d), "5.3s");
    }

    #[test]
    fn test_format_elapsed_minutes() {
        let d = std::time::Duration::from_secs(125);
        assert_eq!(format_elapsed(d), "2m 05s");
    }

    #[test]
    fn test_format_elapsed_hours() {
        let d = std::time::Duration::from_secs(3723);
        assert_eq!(format_elapsed(d), "1h 02m 03s");
    }

    #[test]
    fn test_expand_rnaseq_template() {
        // Verify that the built-in RNA-seq template parses and expands correctly.
        let toml = include_str!("../workflows/native/rnaseq.toml");
        let def = WorkflowDef::from_str_content(toml).expect("parse rnaseq template");
        let tasks = expand(&def).expect("expand rnaseq DAG");

        // rnaseq has 3 samples and 5 steps:
        // Per-sample: fastp, star, samtools_index, featurecounts (4 × 3 = 12)
        // Gather: multiqc (1)
        assert_eq!(tasks.len(), 13);

        // multiqc is an upstream QC aggregation step that depends on fastp
        let multiqc = tasks.iter().find(|t| t.step_name == "multiqc").unwrap();
        assert!(multiqc.gather);
        // multiqc depends on all fastp instances
        assert_eq!(multiqc.deps.len(), 3);
        for dep in &multiqc.deps {
            assert!(
                dep.starts_with("fastp["),
                "multiqc dep should be fastp: {dep}"
            );
        }

        // Verify phases: multiqc runs in parallel with star (both depend on fastp)
        let phases = compute_phases(&tasks);
        assert_eq!(phases[0].len(), 3); // 3 fastp tasks
        // multiqc and star should be in the same phase (phase 1)
        let multiqc_phase = phases
            .iter()
            .position(|p| p.iter().any(|t| t.step_name == "multiqc"))
            .unwrap();
        let star_phase = phases
            .iter()
            .position(|p| p.iter().any(|t| t.step_name == "star"))
            .unwrap();
        assert_eq!(
            multiqc_phase, star_phase,
            "multiqc and star should execute in the same phase"
        );
    }

    #[test]
    fn test_expand_chipseq_template() {
        // ChIP-seq has parallel macs3 + bigwig branches.
        let toml = include_str!("../workflows/native/chipseq.toml");
        let def = WorkflowDef::from_str_content(toml).expect("parse chipseq template");
        let tasks = expand(&def).expect("expand chipseq DAG");

        // multiqc is upstream QC and depends only on fastp
        let multiqc = tasks.iter().find(|t| t.step_name == "multiqc").unwrap();
        assert!(multiqc.gather);
        assert_eq!(multiqc.deps.len(), 3); // 3 samples × fastp
        for dep in &multiqc.deps {
            assert!(
                dep.starts_with("fastp["),
                "multiqc dep should be fastp: {dep}"
            );
        }

        // multiqc and bowtie2 should be in the same phase (both depend on fastp)
        let phases = compute_phases(&tasks);
        let multiqc_phase = phases
            .iter()
            .position(|p| p.iter().any(|t| t.step_name == "multiqc"))
            .unwrap();
        let bowtie2_phase = phases
            .iter()
            .position(|p| p.iter().any(|t| t.step_name == "bowtie2"))
            .unwrap();
        assert_eq!(
            multiqc_phase, bowtie2_phase,
            "multiqc and bowtie2 should execute in the same phase"
        );

        // Verify parallel phases: macs3 and bigwig should be in the same phase
        let macs3_phase = phases
            .iter()
            .position(|p| p.iter().any(|t| t.step_name == "macs3"))
            .unwrap();
        let bigwig_phase = phases
            .iter()
            .position(|p| p.iter().any(|t| t.step_name == "bigwig"))
            .unwrap();
        assert_eq!(
            macs3_phase, bigwig_phase,
            "macs3 and bigwig should execute in the same phase"
        );
    }

    #[test]
    fn test_expand_longreads_template() {
        // Long-reads has parallel nanostat + flye branches.
        let toml = include_str!("../workflows/native/longreads.toml");
        let def = WorkflowDef::from_str_content(toml).expect("parse longreads template");
        let tasks = expand(&def).expect("expand longreads DAG");

        // Verify nanostat and flye are in the same phase (both depend on nanoq only)
        let phases = compute_phases(&tasks);
        let nanostat_phase = phases
            .iter()
            .position(|p| p.iter().any(|t| t.step_name == "nanostat"))
            .unwrap();
        let flye_phase = phases
            .iter()
            .position(|p| p.iter().any(|t| t.step_name == "flye"))
            .unwrap();
        assert_eq!(
            nanostat_phase, flye_phase,
            "nanostat and flye should execute in parallel"
        );
    }

    #[test]
    fn test_all_builtin_templates_parse_and_expand() {
        // Ensure every built-in template can be parsed and expanded without errors.
        let templates = [
            ("rnaseq", include_str!("../workflows/native/rnaseq.toml")),
            ("wgs", include_str!("../workflows/native/wgs.toml")),
            ("atacseq", include_str!("../workflows/native/atacseq.toml")),
            ("chipseq", include_str!("../workflows/native/chipseq.toml")),
            (
                "metagenomics",
                include_str!("../workflows/native/metagenomics.toml"),
            ),
            (
                "amplicon16s",
                include_str!("../workflows/native/amplicon16s.toml"),
            ),
            (
                "scrnaseq",
                include_str!("../workflows/native/scrnaseq.toml"),
            ),
            (
                "longreads",
                include_str!("../workflows/native/longreads.toml"),
            ),
            (
                "methylseq",
                include_str!("../workflows/native/methylseq.toml"),
            ),
        ];
        for (name, toml) in &templates {
            let def = WorkflowDef::from_str_content(toml)
                .unwrap_or_else(|e| panic!("Failed to parse {name}: {e}"));
            let tasks = expand(&def).unwrap_or_else(|e| panic!("Failed to expand {name}: {e}"));
            assert!(!tasks.is_empty(), "{name} should have at least one task");

            // Verify multiqc is an upstream QC aggregation step (not the final phase)
            let multiqc = tasks.iter().find(|t| t.step_name == "multiqc");
            assert!(multiqc.is_some(), "{name}: should have a multiqc task");
            assert!(
                multiqc.unwrap().gather,
                "{name}: multiqc should be a gather step"
            );
        }
    }

    #[test]
    fn test_env_field_parsing_and_expansion() {
        // Verify that the optional `env` field is parsed and propagated to tasks.
        let toml = r#"
[workflow]
name = "env-test"

[wildcards]
sample = ["s1"]

[[step]]
name = "py2_step"
env  = "conda activate py2_env &&"
cmd  = "python2 script.py {sample}"

[[step]]
name       = "py3_step"
depends_on = ["py2_step"]
cmd        = "python3 analysis.py {sample}"
"#;
        let def = WorkflowDef::from_str_content(toml).expect("parse env-test");
        assert_eq!(
            def.steps[0].env.as_deref(),
            Some("conda activate py2_env &&")
        );
        assert!(def.steps[1].env.is_none());

        let tasks = expand(&def).expect("expand env-test");
        assert_eq!(tasks.len(), 2);
        assert_eq!(tasks[0].env.as_deref(), Some("conda activate py2_env &&"));
        assert!(tasks[1].env.is_none());
    }

    #[test]
    fn test_format_toml_includes_env() {
        let toml = r#"
[workflow]
name = "env-fmt"

[[step]]
name = "step1"
env  = "source /opt/venv/bin/activate &&"
cmd  = "run_tool"
"#;
        let def = WorkflowDef::from_str_content(toml).expect("parse");
        let formatted = format_toml(&def);
        assert!(
            formatted.contains("env"),
            "formatted TOML should include env field"
        );
        assert!(
            formatted.contains("source /opt/venv/bin/activate"),
            "env value should be preserved"
        );
    }

    // ─── wildcard_combinations ────────────────────────────────────────────────

    #[test]
    fn test_wildcard_combinations_empty() {
        let wc: HashMap<String, Vec<String>> = HashMap::new();
        let combos = wildcard_combinations(&wc);
        assert_eq!(combos.len(), 1);
        assert!(combos[0].is_empty());
    }

    #[test]
    fn test_wildcard_combinations_single() {
        let mut wc = HashMap::new();
        wc.insert(
            "sample".to_string(),
            vec!["s1".to_string(), "s2".to_string()],
        );
        let combos = wildcard_combinations(&wc);
        assert_eq!(combos.len(), 2);
        let samples: Vec<&str> = combos.iter().map(|m| m["sample"].as_str()).collect();
        assert!(samples.contains(&"s1"));
        assert!(samples.contains(&"s2"));
    }

    #[test]
    fn test_wildcard_combinations_two_keys() {
        let mut wc = HashMap::new();
        wc.insert(
            "sample".to_string(),
            vec!["s1".to_string(), "s2".to_string()],
        );
        wc.insert(
            "condition".to_string(),
            vec!["ctrl".to_string(), "treat".to_string()],
        );
        let combos = wildcard_combinations(&wc);
        assert_eq!(combos.len(), 4); // 2 × 2
    }

    // ─── substitute ───────────────────────────────────────────────────────────

    #[test]
    fn test_substitute_wildcard() {
        let mut bindings = HashMap::new();
        bindings.insert("sample".to_string(), "s1".to_string());
        let params: HashMap<String, String> = HashMap::new();
        let result = substitute("echo {sample}", &bindings, &params);
        assert_eq!(result, "echo s1");
    }

    #[test]
    fn test_substitute_param() {
        let bindings: HashMap<String, String> = HashMap::new();
        let mut params = HashMap::new();
        params.insert("threads".to_string(), "8".to_string());
        let result = substitute("cmd -t {params.threads}", &bindings, &params);
        assert_eq!(result, "cmd -t 8");
    }

    #[test]
    fn test_substitute_bare_param_key() {
        let bindings: HashMap<String, String> = HashMap::new();
        let mut params = HashMap::new();
        params.insert("ref".to_string(), "/data/hg38.fa".to_string());
        let result = substitute("bwa mem {ref}", &bindings, &params);
        assert_eq!(result, "bwa mem /data/hg38.fa");
    }

    #[test]
    fn test_substitute_wildcard_takes_precedence_over_param() {
        let mut bindings = HashMap::new();
        bindings.insert("key".to_string(), "from_wildcard".to_string());
        let mut params = HashMap::new();
        params.insert("key".to_string(), "from_param".to_string());
        let result = substitute("{key}", &bindings, &params);
        assert_eq!(result, "from_wildcard");
    }

    #[test]
    fn test_substitute_no_placeholders() {
        let bindings: HashMap<String, String> = HashMap::new();
        let params: HashMap<String, String> = HashMap::new();
        let result = substitute("echo hello world", &bindings, &params);
        assert_eq!(result, "echo hello world");
    }

    // ─── task_id ──────────────────────────────────────────────────────────────

    #[test]
    fn test_task_id_no_bindings() {
        let bindings: HashMap<String, String> = HashMap::new();
        assert_eq!(task_id("fastp", &bindings), "fastp");
    }

    #[test]
    fn test_task_id_single_binding() {
        let mut bindings = HashMap::new();
        bindings.insert("sample".to_string(), "s1".to_string());
        assert_eq!(task_id("fastp", &bindings), "fastp[sample=s1]");
    }

    #[test]
    fn test_task_id_multiple_bindings_sorted() {
        let mut bindings = HashMap::new();
        bindings.insert("cond".to_string(), "ctrl".to_string());
        bindings.insert("sample".to_string(), "s1".to_string());
        let id = task_id("step", &bindings);
        // Keys are sorted, so cond before sample
        assert_eq!(id, "step[cond=ctrl,sample=s1]");
    }

    // ─── uses_wildcards ───────────────────────────────────────────────────────

    #[test]
    fn test_uses_wildcards_true_in_cmd() {
        let step = StepDef {
            name: "s".to_string(),
            cmd: "echo {sample}".to_string(),
            depends_on: vec![],
            inputs: vec![],
            outputs: vec![],
            gather: false,
            env: None,
        };
        let mut wc = HashMap::new();
        wc.insert("sample".to_string(), vec!["s1".to_string()]);
        assert!(uses_wildcards(&step, &wc));
    }

    #[test]
    fn test_uses_wildcards_false_no_match() {
        let step = StepDef {
            name: "s".to_string(),
            cmd: "echo hello".to_string(),
            depends_on: vec![],
            inputs: vec![],
            outputs: vec![],
            gather: false,
            env: None,
        };
        let mut wc = HashMap::new();
        wc.insert("sample".to_string(), vec!["s1".to_string()]);
        assert!(!uses_wildcards(&step, &wc));
    }

    #[test]
    fn test_uses_wildcards_in_inputs() {
        let step = StepDef {
            name: "s".to_string(),
            cmd: "echo hello".to_string(),
            depends_on: vec![],
            inputs: vec!["data/{sample}.fq.gz".to_string()],
            outputs: vec![],
            gather: false,
            env: None,
        };
        let mut wc = HashMap::new();
        wc.insert("sample".to_string(), vec!["s1".to_string()]);
        assert!(uses_wildcards(&step, &wc));
    }

    #[test]
    fn test_uses_wildcards_in_outputs() {
        let step = StepDef {
            name: "s".to_string(),
            cmd: "echo hello".to_string(),
            depends_on: vec![],
            inputs: vec![],
            outputs: vec!["out/{sample}.bam".to_string()],
            gather: false,
            env: None,
        };
        let mut wc = HashMap::new();
        wc.insert("sample".to_string(), vec!["s1".to_string()]);
        assert!(uses_wildcards(&step, &wc));
    }

    // ─── expand ───────────────────────────────────────────────────────────────

    #[test]
    fn test_expand_simple_no_wildcards() {
        let toml = r#"
[workflow]
name = "simple"

[[step]]
name = "qc"
cmd  = "fastqc input.fq"
"#;
        let def = WorkflowDef::from_str_content(toml).expect("parse");
        let tasks = expand(&def).expect("expand");
        assert_eq!(tasks.len(), 1);
        assert_eq!(tasks[0].id, "qc");
        assert_eq!(tasks[0].cmd, "fastqc input.fq");
    }

    #[test]
    fn test_expand_unknown_dep_silently_drops_deps() {
        // Unknown depends_on entries are silently dropped from the deps list
        // (verify() catches them as errors, but expand() is more lenient).
        let toml = r#"
[workflow]
name = "bad"

[[step]]
name = "step_b"
cmd  = "echo b"
depends_on = ["step_a"]
"#;
        let def = WorkflowDef::from_str_content(toml).expect("parse");
        let tasks = expand(&def).expect("expand does not error on unknown deps");
        // step_b should exist but with empty deps since step_a was never declared
        assert_eq!(tasks.len(), 1);
        assert!(
            tasks[0].deps.is_empty(),
            "unknown dep should be silently dropped"
        );
    }

    #[test]
    fn test_expand_wildcard_steps() {
        let toml = r#"
[workflow]
name = "wildcard-test"

[wildcards]
sample = ["s1", "s2"]

[[step]]
name = "qc"
cmd  = "fastqc {sample}.fq"
"#;
        let def = WorkflowDef::from_str_content(toml).expect("parse");
        let tasks = expand(&def).expect("expand");
        assert_eq!(tasks.len(), 2);
        let ids: Vec<&str> = tasks.iter().map(|t| t.id.as_str()).collect();
        assert!(ids.iter().any(|id| id.contains("s1")));
        assert!(ids.iter().any(|id| id.contains("s2")));
    }

    #[test]
    fn test_expand_gather_depends_on_all_instances() {
        let toml = r#"
[workflow]
name = "gather-test"

[wildcards]
sample = ["s1", "s2"]

[[step]]
name = "qc"
cmd  = "fastqc {sample}.fq"

[[step]]
name       = "report"
gather     = true
depends_on = ["qc"]
cmd        = "multiqc qc/"
"#;
        let def = WorkflowDef::from_str_content(toml).expect("parse");
        let tasks = expand(&def).expect("expand");
        // 2 qc + 1 report
        assert_eq!(tasks.len(), 3);
        let report = tasks.iter().find(|t| t.id == "report").unwrap();
        assert!(report.gather);
        assert_eq!(report.deps.len(), 2);
    }

    #[test]
    fn test_expand_params_substitution() {
        let toml = r#"
[workflow]
name = "params-test"

[params]
threads = "8"

[[step]]
name = "align"
cmd  = "bwa mem -t {params.threads} ref.fa reads.fq"
"#;
        let def = WorkflowDef::from_str_content(toml).expect("parse");
        let tasks = expand(&def).expect("expand");
        assert_eq!(tasks.len(), 1);
        assert!(tasks[0].cmd.contains("8"), "threads should be substituted");
    }

    // ─── is_up_to_date ────────────────────────────────────────────────────────

    #[test]
    fn test_is_up_to_date_no_outputs_returns_false() {
        let task = ConcreteTask {
            id: "t".to_string(),
            step_name: "t".to_string(),
            cmd: "echo hello".to_string(),
            inputs: vec![],
            outputs: vec![],
            deps: vec![],
            gather: false,
            env: None,
        };
        assert!(!is_up_to_date(&task));
    }

    #[test]
    fn test_is_up_to_date_missing_output_returns_false() {
        let task = ConcreteTask {
            id: "t".to_string(),
            step_name: "t".to_string(),
            cmd: "echo hello".to_string(),
            inputs: vec![],
            outputs: vec!["/nonexistent/path/output.bam".to_string()],
            deps: vec![],
            gather: false,
            env: None,
        };
        assert!(!is_up_to_date(&task));
    }

    #[test]
    fn test_is_up_to_date_output_exists_no_inputs() {
        let tmp = tempfile::tempdir().unwrap();
        let out = tmp.path().join("output.txt");
        std::fs::write(&out, "result").unwrap();

        let task = ConcreteTask {
            id: "t".to_string(),
            step_name: "t".to_string(),
            cmd: "echo hello".to_string(),
            inputs: vec![],
            outputs: vec![out.to_string_lossy().to_string()],
            deps: vec![],
            gather: false,
            env: None,
        };
        assert!(
            is_up_to_date(&task),
            "output exists and there are no inputs"
        );
    }

    // ─── verify ───────────────────────────────────────────────────────────────

    #[test]
    fn test_verify_valid_workflow() {
        let toml = r#"
[workflow]
name = "valid"

[wildcards]
sample = ["s1", "s2"]

[params]
threads = "8"

[[step]]
name    = "fastp"
cmd     = "fastp -i {sample}.fq -o out/{sample}.fq"
outputs = ["out/{sample}.fq"]

[[step]]
name       = "star"
depends_on = ["fastp"]
cmd        = "STAR --runThreadN {params.threads} --readFilesIn out/{sample}.fq"
"#;
        let def = WorkflowDef::from_str_content(toml).expect("parse");
        let diags = verify(&def);
        assert!(
            diags.is_empty(),
            "valid workflow should have no diagnostics: {:?}",
            diags
        );
    }

    #[test]
    fn test_verify_empty_workflow_name_produces_warning() {
        let toml = r#"
[workflow]
name = ""

[[step]]
name = "s1"
cmd  = "echo hello"
"#;
        let def = WorkflowDef::from_str_content(toml).expect("parse");
        let diags = verify(&def);
        assert!(
            diags
                .iter()
                .any(|d| d.level == DiagLevel::Warning && d.message.contains("name")),
            "should warn about empty workflow name"
        );
    }

    #[test]
    fn test_verify_duplicate_step_name_produces_error() {
        let toml = r#"
[workflow]
name = "dup-test"

[[step]]
name = "qc"
cmd  = "echo a"

[[step]]
name = "qc"
cmd  = "echo b"
"#;
        let def = WorkflowDef::from_str_content(toml).expect("parse");
        let diags = verify(&def);
        assert!(
            diags
                .iter()
                .any(|d| d.level == DiagLevel::Error && d.message.contains("Duplicate")),
            "should error on duplicate step name"
        );
    }

    #[test]
    fn test_verify_unknown_depends_on_produces_error() {
        let toml = r#"
[workflow]
name = "bad-dep"

[[step]]
name       = "step_b"
depends_on = ["nonexistent"]
cmd        = "echo b"
"#;
        let def = WorkflowDef::from_str_content(toml).expect("parse");
        let diags = verify(&def);
        assert!(
            diags
                .iter()
                .any(|d| d.level == DiagLevel::Error && d.message.contains("unknown step")),
            "should error on unknown depends_on"
        );
    }

    #[test]
    fn test_verify_empty_step_cmd_produces_error() {
        let toml = r#"
[workflow]
name = "empty-cmd"

[[step]]
name = "s1"
cmd  = ""
"#;
        let def = WorkflowDef::from_str_content(toml).expect("parse");
        let diags = verify(&def);
        assert!(
            diags
                .iter()
                .any(|d| d.level == DiagLevel::Error && d.message.contains("empty cmd")),
            "should error on empty cmd"
        );
    }

    #[test]
    fn test_verify_undefined_placeholder_produces_warning() {
        let toml = r#"
[workflow]
name = "undef-placeholder"

[[step]]
name = "s1"
cmd  = "echo {undefined_var}"
"#;
        let def = WorkflowDef::from_str_content(toml).expect("parse");
        let diags = verify(&def);
        assert!(
            diags.iter().any(|d| d.level == DiagLevel::Warning),
            "should warn about undefined placeholder"
        );
    }

    #[test]
    fn test_verify_undefined_params_placeholder_produces_warning() {
        let toml = r#"
[workflow]
name = "undef-params"

[[step]]
name = "s1"
cmd  = "cmd -t {params.missing_param}"
"#;
        let def = WorkflowDef::from_str_content(toml).expect("parse");
        let diags = verify(&def);
        assert!(
            diags.iter().any(
                |d| d.level == DiagLevel::Warning && d.message.contains("params.missing_param")
            ),
            "should warn about undefined params placeholder"
        );
    }

    #[test]
    fn test_verify_forward_dep_ordering_warning() {
        let toml = r#"
[workflow]
name = "forward-dep"

[[step]]
name       = "step_b"
depends_on = ["step_a"]
cmd        = "echo b"

[[step]]
name = "step_a"
cmd  = "echo a"
"#;
        let def = WorkflowDef::from_str_content(toml).expect("parse");
        let diags = verify(&def);
        // step_b appears before step_a in the file — this should trigger a warning
        assert!(
            diags
                .iter()
                .any(|d| d.level == DiagLevel::Warning && d.message.contains("step_b")),
            "should warn when dep appears later in file"
        );
    }

    #[test]
    fn test_verify_diag_level_display() {
        assert_eq!(format!("{}", DiagLevel::Error), "error");
        assert_eq!(format!("{}", DiagLevel::Warning), "warning");
    }

    // ─── format_toml ──────────────────────────────────────────────────────────

    #[test]
    fn test_format_toml_simple() {
        let toml_in = r#"
[workflow]
name = "test-wf"
description = "A test workflow"

[params]
threads = "8"

[[step]]
name    = "qc"
cmd     = "fastqc input.fq"
outputs = ["qc/"]
"#;
        let def = WorkflowDef::from_str_content(toml_in).expect("parse");
        let formatted = format_toml(&def);
        assert!(formatted.contains("test-wf"));
        assert!(formatted.contains("A test workflow"));
        assert!(formatted.contains("threads"));
        assert!(formatted.contains("fastqc input.fq"));
    }

    #[test]
    fn test_format_toml_with_wildcards() {
        let toml_in = r#"
[workflow]
name = "wc-test"

[wildcards]
sample = ["s1", "s2"]

[[step]]
name = "qc"
cmd  = "fastqc {sample}.fq"
"#;
        let def = WorkflowDef::from_str_content(toml_in).expect("parse");
        let formatted = format_toml(&def);
        assert!(formatted.contains("[wildcards]"));
        assert!(formatted.contains("sample"));
    }

    #[test]
    fn test_format_toml_with_depends_on() {
        let toml_in = r#"
[workflow]
name = "dep-test"

[[step]]
name = "step_a"
cmd  = "echo a"

[[step]]
name       = "step_b"
depends_on = ["step_a"]
cmd        = "echo b"
"#;
        let def = WorkflowDef::from_str_content(toml_in).expect("parse");
        let formatted = format_toml(&def);
        assert!(formatted.contains("depends_on"));
        assert!(formatted.contains("step_a"));
    }

    #[test]
    fn test_format_toml_long_cmd_multiline() {
        let long_cmd = "a".repeat(MAX_INLINE_CMD_LENGTH + 10);
        let toml_in = format!(
            "[workflow]\nname = \"long-cmd\"\n\n[[step]]\nname = \"s\"\ncmd  = \"{long_cmd}\"\n"
        );
        let def = WorkflowDef::from_str_content(&toml_in).expect("parse");
        let formatted = format_toml(&def);
        assert!(
            formatted.contains("\"\"\""),
            "long cmd should use multiline syntax"
        );
    }

    #[test]
    fn test_format_toml_gather_step() {
        let toml_in = r#"
[workflow]
name = "gather-fmt"

[wildcards]
sample = ["s1"]

[[step]]
name   = "qc"
cmd    = "fastqc {sample}.fq"

[[step]]
name       = "report"
gather     = true
depends_on = ["qc"]
cmd        = "multiqc qc/"
"#;
        let def = WorkflowDef::from_str_content(toml_in).expect("parse");
        let formatted = format_toml(&def);
        assert!(formatted.contains("gather     = true"));
    }

    // ─── to_snakemake ─────────────────────────────────────────────────────────

    #[test]
    fn test_to_snakemake_basic() {
        let toml = r#"
[workflow]
name = "snakemake-test"
description = "Test Snakemake export"

[[step]]
name    = "qc"
cmd     = "fastqc input.fq"
inputs  = ["input.fq"]
outputs = ["qc/input_fastqc.html"]
"#;
        let def = WorkflowDef::from_str_content(toml).expect("parse");
        let snakemake = to_snakemake(&def);
        assert!(snakemake.contains("rule qc:"));
        assert!(snakemake.contains("fastqc input.fq"));
        assert!(snakemake.contains("rule all:"));
        assert!(snakemake.contains("configfile:"));
    }

    #[test]
    fn test_to_snakemake_with_wildcards() {
        let toml = r#"
[workflow]
name = "wc-snakemake"

[wildcards]
sample = ["s1", "s2"]

[[step]]
name    = "qc"
cmd     = "fastqc {sample}.fq"
outputs = ["qc/{sample}_fastqc.html"]
"#;
        let def = WorkflowDef::from_str_content(toml).expect("parse");
        let snakemake = to_snakemake(&def);
        assert!(
            snakemake.contains("SAMPLE"),
            "wildcard should be uppercased"
        );
        assert!(
            snakemake.contains("expand("),
            "should use expand() for wildcard outputs"
        );
        assert!(
            snakemake.contains("wildcards.sample"),
            "cmd should use wildcards.sample"
        );
    }

    #[test]
    fn test_to_snakemake_with_params() {
        let toml = r#"
[workflow]
name = "params-snakemake"

[params]
threads = "8"

[[step]]
name = "align"
cmd  = "bwa mem -t {params.threads} ref.fa reads.fq"
"#;
        let def = WorkflowDef::from_str_content(toml).expect("parse");
        let snakemake = to_snakemake(&def);
        // {params.threads} becomes {config["threads"]} which is then shell-escaped to {config[\"threads\"]}
        assert!(
            snakemake.contains("config[") || snakemake.contains("config"),
            "params should use config reference"
        );
        assert!(
            snakemake.contains("config.yaml"),
            "should have config.yaml comment"
        );
        assert!(
            snakemake.contains("threads"),
            "threads param should appear in output"
        );
    }

    #[test]
    fn test_to_snakemake_generated_from_builtin_templates() {
        // All built-in native templates should produce valid-looking Snakemake
        for template in BUILTIN_TEMPLATES {
            let def = WorkflowDef::from_str_content(template.native).expect("parse");
            let snakemake = to_snakemake(&def);
            assert!(
                snakemake.contains("rule all:"),
                "{}: missing rule all",
                template.name
            );
            assert!(
                snakemake.contains("configfile:"),
                "{}: missing configfile",
                template.name
            );
        }
    }

    // ─── to_nextflow ──────────────────────────────────────────────────────────

    #[test]
    fn test_to_nextflow_basic() {
        let toml = r#"
[workflow]
name = "nextflow-test"

[[step]]
name    = "qc"
cmd     = "fastqc input.fq"
inputs  = ["input.fq"]
outputs = ["qc/input_fastqc.html"]
"#;
        let def = WorkflowDef::from_str_content(toml).expect("parse");
        let nf = to_nextflow(&def);
        assert!(
            nf.contains("process QC {"),
            "process names should be uppercase"
        );
        assert!(nf.contains("nextflow.enable.dsl = 2"));
        assert!(nf.contains("workflow {"));
        assert!(nf.contains("fastqc input.fq"));
    }

    #[test]
    fn test_to_nextflow_with_wildcards() {
        let toml = r#"
[workflow]
name = "wc-nextflow"

[wildcards]
sample = ["s1", "s2"]

[[step]]
name    = "qc"
cmd     = "fastqc {sample}.fq"
outputs = ["qc/{sample}_fastqc.html"]
"#;
        let def = WorkflowDef::from_str_content(toml).expect("parse");
        let nf = to_nextflow(&def);
        assert!(
            nf.contains("samplesheet"),
            "should have samplesheet channel"
        );
        assert!(
            nf.contains("${sample}"),
            "wildcards should be expanded as Nextflow vars"
        );
    }

    #[test]
    fn test_to_nextflow_with_params() {
        let toml = r#"
[workflow]
name = "params-nextflow"

[params]
threads = "8"
ref_genome = "/data/hg38.fa"

[[step]]
name = "align"
cmd  = "bwa mem -t {params.threads} {params.ref_genome} reads.fq"
"#;
        let def = WorkflowDef::from_str_content(toml).expect("parse");
        let nf = to_nextflow(&def);
        assert!(nf.contains("params.threads"), "should have params.threads");
        assert!(
            nf.contains("params.ref_genome"),
            "should have params.ref_genome"
        );
    }

    #[test]
    fn test_to_nextflow_gather_step() {
        let toml = r#"
[workflow]
name = "gather-nextflow"

[wildcards]
sample = ["s1"]

[[step]]
name    = "qc"
cmd     = "fastqc {sample}.fq"
outputs = ["qc/{sample}_fastqc.html"]

[[step]]
name       = "report"
gather     = true
depends_on = ["qc"]
cmd        = "multiqc qc/"
"#;
        let def = WorkflowDef::from_str_content(toml).expect("parse");
        let nf = to_nextflow(&def);
        assert!(
            nf.contains("ch.collect()"),
            "gather step should use .collect()"
        );
    }

    #[test]
    fn test_to_nextflow_generated_from_builtin_templates() {
        for template in BUILTIN_TEMPLATES {
            let def = WorkflowDef::from_str_content(template.native).expect("parse");
            let nf = to_nextflow(&def);
            assert!(
                nf.contains("nextflow.enable.dsl = 2"),
                "{}: missing dsl=2",
                template.name
            );
            assert!(
                nf.contains("workflow {"),
                "{}: missing workflow block",
                template.name
            );
        }
    }

    // ─── WorkflowDef::from_file ───────────────────────────────────────────────

    #[test]
    fn test_workflow_def_from_file() {
        let tmp = tempfile::tempdir().unwrap();
        let path = tmp.path().join("test.toml");
        std::fs::write(
            &path,
            "[workflow]\nname = \"file-test\"\n\n[[step]]\nname = \"s1\"\ncmd = \"echo hello\"\n",
        )
        .unwrap();
        let def = WorkflowDef::from_file(&path).expect("from_file");
        assert_eq!(def.workflow.name, "file-test");
    }

    #[test]
    fn test_workflow_def_from_file_not_found() {
        let result = WorkflowDef::from_file(std::path::Path::new("/nonexistent/path.toml"));
        assert!(result.is_err());
    }

    // ─── print_verify_report ──────────────────────────────────────────────────

    #[test]
    fn test_print_verify_report_no_issues() {
        let def = WorkflowDef {
            workflow: WorkflowMeta {
                name: "clean".to_string(),
                ..Default::default()
            },
            ..Default::default()
        };
        let diags: Vec<Diagnostic> = vec![];
        let has_errors = print_verify_report(&def, &diags);
        assert!(!has_errors);
    }

    #[test]
    fn test_print_verify_report_with_errors() {
        let def = WorkflowDef {
            workflow: WorkflowMeta {
                name: "broken".to_string(),
                ..Default::default()
            },
            ..Default::default()
        };
        let diags = vec![
            Diagnostic {
                level: DiagLevel::Error,
                message: "something is broken".to_string(),
            },
            Diagnostic {
                level: DiagLevel::Warning,
                message: "something looks odd".to_string(),
            },
        ];
        let has_errors = print_verify_report(&def, &diags);
        assert!(has_errors);
    }

    // ─── compute_phases edge cases ────────────────────────────────────────────

    #[test]
    fn test_compute_phases_broken_cycle_stops() {
        // Tasks with unsatisfied deps (simulated cycle) — compute_phases should
        // return partial result rather than hanging forever.
        let tasks = vec![
            ConcreteTask {
                id: "a".to_string(),
                step_name: "a".to_string(),
                cmd: "echo a".to_string(),
                inputs: vec![],
                outputs: vec![],
                deps: vec!["b".to_string()], // unsatisfied
                gather: false,
                env: None,
            },
            ConcreteTask {
                id: "b".to_string(),
                step_name: "b".to_string(),
                cmd: "echo b".to_string(),
                inputs: vec![],
                outputs: vec![],
                deps: vec!["a".to_string()], // unsatisfied
                gather: false,
                env: None,
            },
        ];
        let phases = compute_phases(&tasks);
        // Cycle means no tasks ever become ready — result is empty
        assert_eq!(phases.len(), 0);
    }

    // ─── verify() ─────────────────────────────────────────────────────────────

    #[test]
    fn test_verify_valid_workflow_no_diags() {
        let toml = r#"
[workflow]
name = "valid-workflow"

[wildcards]
sample = ["s1", "s2"]

[params]
ref = "hg38.fa"

[[step]]
name = "align"
cmd  = "bwa mem {params.ref} {sample}.fq > {sample}.sam"
inputs  = ["{sample}.fq"]
outputs = ["{sample}.sam"]

[[step]]
name       = "sort"
cmd        = "samtools sort {sample}.sam -o {sample}.bam"
depends_on = ["align"]
inputs     = ["{sample}.sam"]
outputs    = ["{sample}.bam"]
"#;
        let def = WorkflowDef::from_str_content(toml).expect("parse");
        let diags = verify(&def);
        // Should have no errors or warnings
        let errors: Vec<_> = diags
            .iter()
            .filter(|d| d.level == DiagLevel::Error)
            .collect();
        assert!(errors.is_empty(), "expected no errors: {errors:?}");
    }

    #[test]
    fn test_verify_empty_workflow_name_warning() {
        let toml = r#"
[workflow]
name = ""

[[step]]
name = "step1"
cmd  = "echo hello"
"#;
        let def = WorkflowDef::from_str_content(toml).expect("parse");
        let diags = verify(&def);
        assert!(
            diags.iter().any(|d| d.message.contains("name is empty")),
            "should warn about empty workflow name"
        );
    }

    #[test]
    fn test_verify_duplicate_step_name_error() {
        let toml = r#"
[workflow]
name = "dup-test"

[[step]]
name = "step1"
cmd  = "echo first"

[[step]]
name = "step1"
cmd  = "echo duplicate"
"#;
        let def = WorkflowDef::from_str_content(toml).expect("parse");
        let diags = verify(&def);
        assert!(
            diags
                .iter()
                .any(|d| d.level == DiagLevel::Error && d.message.contains("Duplicate")),
            "should report duplicate step name"
        );
    }

    #[test]
    fn test_verify_unknown_dependency_error() {
        let toml = r#"
[workflow]
name = "dep-test"

[[step]]
name       = "step1"
cmd        = "echo hello"
depends_on = ["nonexistent-step"]
"#;
        let def = WorkflowDef::from_str_content(toml).expect("parse");
        let diags = verify(&def);
        assert!(
            diags
                .iter()
                .any(|d| d.level == DiagLevel::Error && d.message.contains("unknown step")),
            "should error on unknown dependency"
        );
    }

    #[test]
    fn test_verify_undefined_wildcard_warning() {
        let toml = r#"
[workflow]
name = "wc-test"

[[step]]
name = "step1"
cmd  = "bwa mem {undefined_wildcard}.fq"
"#;
        let def = WorkflowDef::from_str_content(toml).expect("parse");
        let diags = verify(&def);
        assert!(
            diags
                .iter()
                .any(|d| d.message.contains("undefined_wildcard")),
            "should warn about undefined wildcard: {diags:?}"
        );
    }

    #[test]
    fn test_verify_undefined_params_warning() {
        let toml = r#"
[workflow]
name = "params-test"

[[step]]
name = "step1"
cmd  = "bwa mem {params.reference} reads.fq"
"#;
        let def = WorkflowDef::from_str_content(toml).expect("parse");
        let diags = verify(&def);
        assert!(
            diags.iter().any(|d| d.message.contains("reference")),
            "should warn about undefined param: {diags:?}"
        );
    }

    #[test]
    fn test_verify_empty_step_cmd_error() {
        let toml = r#"
[workflow]
name = "empty-cmd-test"

[[step]]
name = "step1"
cmd  = "  "
"#;
        let def = WorkflowDef::from_str_content(toml).expect("parse");
        let diags = verify(&def);
        assert!(
            diags
                .iter()
                .any(|d| d.level == DiagLevel::Error && d.message.contains("empty cmd")),
            "should report empty cmd"
        );
    }

    #[test]
    fn test_verify_forward_dependency_warning() {
        let toml = r#"
[workflow]
name = "fwd-dep"

[[step]]
name       = "step2"
cmd        = "echo step2"
depends_on = ["step1"]

[[step]]
name = "step1"
cmd  = "echo step1"
"#;
        // Note: step2 appears before step1, creating a forward reference
        // This is the scenario where dep_pos > step_pos.
        // HOWEVER, depends_on for step2 includes "step1" which is the later step.
        // Actually this should work fine since step1 appears AFTER step2.
        // Let me check: step2 is at pos 0, step1 is at pos 1.
        // dep_pos(step1)=1 > step_pos(step2)=0 → forward dependency warning
        let def = WorkflowDef::from_str_content(toml).expect("parse");
        let diags = verify(&def);
        // Should have a forward-dependency warning
        assert!(
            diags.iter().any(|d| d.message.contains("appears later")),
            "should warn about forward dependency: {diags:?}"
        );
    }

    #[test]
    fn test_verify_shell_var_expansion_not_flagged() {
        // ${sample} should NOT trigger an "undefined wildcard" warning
        let toml = r#"
[workflow]
name = "shell-var-test"

[[step]]
name = "step1"
cmd  = "echo ${HOME}/output.txt"
"#;
        let def = WorkflowDef::from_str_content(toml).expect("parse");
        let diags = verify(&def);
        // shell variables (${...}) should be ignored — no spurious warnings
        let shell_var_warnings: Vec<_> = diags
            .iter()
            .filter(|d| d.message.contains("HOME"))
            .collect();
        assert!(
            shell_var_warnings.is_empty(),
            "shell ${{HOME}} should not trigger undefined-wildcard warning"
        );
    }

    #[test]
    fn test_verify_all_builtin_templates_clean() {
        for template in BUILTIN_TEMPLATES {
            let def = WorkflowDef::from_str_content(template.native)
                .unwrap_or_else(|e| panic!("{}: parse failed: {e}", template.name));
            let diags = verify(&def);
            let errors: Vec<_> = diags
                .iter()
                .filter(|d| d.level == DiagLevel::Error)
                .collect();
            assert!(
                errors.is_empty(),
                "{}: verify errors: {errors:?}",
                template.name
            );
        }
    }

    // ─── execute() dry-run ────────────────────────────────────────────────────

    #[tokio::test]
    async fn test_execute_dry_run_no_tasks_ok() {
        let result = execute(vec![], true).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_execute_dry_run_single_task_ok() {
        let tasks = vec![ConcreteTask {
            id: "echo-task".to_string(),
            step_name: "echo-task".to_string(),
            cmd: "echo hello".to_string(),
            inputs: vec![],
            outputs: vec![],
            deps: vec![],
            gather: false,
            env: None,
        }];
        let result = execute(tasks, true).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_execute_dry_run_linear_chain_ok() {
        let tasks = vec![
            ConcreteTask {
                id: "step_a".to_string(),
                step_name: "step_a".to_string(),
                cmd: "echo a".to_string(),
                inputs: vec![],
                outputs: vec![],
                deps: vec![],
                gather: false,
                env: None,
            },
            ConcreteTask {
                id: "step_b".to_string(),
                step_name: "step_b".to_string(),
                cmd: "echo b".to_string(),
                inputs: vec![],
                outputs: vec![],
                deps: vec!["step_a".to_string()],
                gather: false,
                env: None,
            },
            ConcreteTask {
                id: "step_c".to_string(),
                step_name: "step_c".to_string(),
                cmd: "echo c".to_string(),
                inputs: vec![],
                outputs: vec![],
                deps: vec!["step_b".to_string()],
                gather: false,
                env: None,
            },
        ];
        let result = execute(tasks, true).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_execute_dry_run_gather_step_ok() {
        let tasks = vec![
            ConcreteTask {
                id: "fastp[sample=s1]".to_string(),
                step_name: "fastp".to_string(),
                cmd: "fastp -i s1_R1.fq -o s1.fq".to_string(),
                inputs: vec!["s1_R1.fq".to_string()],
                outputs: vec!["s1.fq".to_string()],
                deps: vec![],
                gather: false,
                env: None,
            },
            ConcreteTask {
                id: "fastp[sample=s2]".to_string(),
                step_name: "fastp".to_string(),
                cmd: "fastp -i s2_R1.fq -o s2.fq".to_string(),
                inputs: vec!["s2_R1.fq".to_string()],
                outputs: vec!["s2.fq".to_string()],
                deps: vec![],
                gather: false,
                env: None,
            },
            ConcreteTask {
                id: "multiqc".to_string(),
                step_name: "multiqc".to_string(),
                cmd: "multiqc qc/".to_string(),
                inputs: vec![],
                outputs: vec!["multiqc_report.html".to_string()],
                deps: vec![
                    "fastp[sample=s1]".to_string(),
                    "fastp[sample=s2]".to_string(),
                ],
                gather: true,
                env: None,
            },
        ];
        let result = execute(tasks, true).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_execute_dry_run_with_env_field() {
        let tasks = vec![ConcreteTask {
            id: "py2-step".to_string(),
            step_name: "py2-step".to_string(),
            cmd: "python2 script.py".to_string(),
            inputs: vec![],
            outputs: vec![],
            deps: vec![],
            gather: false,
            env: Some("conda activate py2_env &&".to_string()),
        }];
        let result = execute(tasks, true).await;
        assert!(result.is_ok());
    }

    // ─── format_elapsed ───────────────────────────────────────────────────────

    #[test]
    fn test_format_elapsed_sub_second() {
        let d = std::time::Duration::from_millis(500);
        let s = format_elapsed(d);
        assert!(s.ends_with("s"), "sub-second should end with 's': {s}");
        assert!(s.contains("0.5"), "should show 0.5s: {s}");
    }

    #[test]
    fn test_format_elapsed_exactly_one_minute() {
        let d = std::time::Duration::from_secs(60);
        let s = format_elapsed(d);
        assert!(s.contains("m"), "should contain 'm': {s}");
    }

    #[test]
    fn test_format_elapsed_exactly_one_hour() {
        let d = std::time::Duration::from_secs(3600);
        let s = format_elapsed(d);
        assert!(s.contains("h"), "should contain 'h': {s}");
    }

    // ─── print_dag_phases ────────────────────────────────────────────────────

    #[test]
    fn test_print_dag_phases_no_panic_empty() {
        print_dag_phases(&[]);
        // Should not panic
    }

    #[test]
    fn test_print_dag_phases_no_panic_linear() {
        let tasks = vec![
            ConcreteTask {
                id: "a".to_string(),
                step_name: "a".to_string(),
                cmd: "echo a".to_string(),
                inputs: vec![],
                outputs: vec![],
                deps: vec![],
                gather: false,
                env: None,
            },
            ConcreteTask {
                id: "b".to_string(),
                step_name: "b".to_string(),
                cmd: "echo b".to_string(),
                inputs: vec![],
                outputs: vec![],
                deps: vec!["a".to_string()],
                gather: false,
                env: None,
            },
        ];
        print_dag_phases(&tasks);
    }

    #[test]
    fn test_print_dag_phases_many_tasks_per_group() {
        // Test the "… +N more" display path (> 3 tasks per group)
        let tasks: Vec<ConcreteTask> = (0..5)
            .map(|i| ConcreteTask {
                id: format!("step[sample=s{i}]"),
                step_name: "step".to_string(),
                cmd: format!("echo s{i}"),
                inputs: vec![],
                outputs: vec![],
                deps: vec![],
                gather: false,
                env: None,
            })
            .collect();
        print_dag_phases(&tasks);
    }

    // ─── print_task_dry_run ───────────────────────────────────────────────────

    #[test]
    fn test_print_task_dry_run_no_panic() {
        let t = ConcreteTask {
            id: "align[sample=s1]".to_string(),
            step_name: "align".to_string(),
            cmd: "bwa mem ref.fa reads.fq > out.sam".to_string(),
            inputs: vec!["reads.fq".to_string()],
            outputs: vec!["out.sam".to_string()],
            deps: vec!["qc[sample=s1]".to_string()],
            gather: false,
            env: Some("module load bwa &&".to_string()),
        };
        print_task_dry_run(&t, 1, 3);
    }

    #[test]
    fn test_print_task_dry_run_gather_no_panic() {
        let t = ConcreteTask {
            id: "multiqc".to_string(),
            step_name: "multiqc".to_string(),
            cmd: "multiqc qc/".to_string(),
            inputs: vec![],
            outputs: vec!["multiqc_report.html".to_string()],
            deps: vec!["fastp[sample=s1]".to_string()],
            gather: true,
            env: None,
        };
        print_task_dry_run(&t, 5, 5);
    }

    // ─── WorkflowDef deserialization: inputs/outputs ─────────────────────────

    #[test]
    fn test_workflow_def_step_with_inputs_outputs() {
        let toml = r#"
[workflow]
name = "io-test"

[[step]]
name    = "align"
cmd     = "bwa mem ref.fa reads.fq"
inputs  = ["ref.fa", "reads.fq"]
outputs = ["out.sam"]
"#;
        let def = WorkflowDef::from_str_content(toml).expect("parse");
        assert_eq!(def.steps[0].inputs, vec!["ref.fa", "reads.fq"]);
        assert_eq!(def.steps[0].outputs, vec!["out.sam"]);
    }

    // ─── is_up_to_date: output exists but input is newer ────────────────────

    #[test]
    fn test_is_up_to_date_output_older_than_input() {
        let tmp = tempfile::tempdir().unwrap();
        let input = tmp.path().join("input.fq");
        let output = tmp.path().join("output.bam");

        // Create output first, then input (so output is older)
        std::fs::write(&output, "result").unwrap();
        std::thread::sleep(std::time::Duration::from_millis(10));
        std::fs::write(&input, "reads").unwrap();

        let task = ConcreteTask {
            id: "t".to_string(),
            step_name: "t".to_string(),
            cmd: "bwa mem input.fq > output.bam".to_string(),
            inputs: vec![input.to_string_lossy().to_string()],
            outputs: vec![output.to_string_lossy().to_string()],
            deps: vec![],
            gather: false,
            env: None,
        };
        // Output is older than input — should NOT be up-to-date
        assert!(
            !is_up_to_date(&task),
            "output older than input should not be up-to-date"
        );
    }

    // ─── format_elapsed ───────────────────────────────────────────────────────

    #[test]
    fn test_format_elapsed_zero() {
        assert_eq!(format_elapsed(std::time::Duration::from_secs(0)), "0.0s");
    }

    #[test]
    fn test_format_elapsed_sub_second_millis() {
        assert_eq!(
            format_elapsed(std::time::Duration::from_millis(500)),
            "0.5s"
        );
    }

    // ─── mtime + is_up_to_date ────────────────────────────────────────────────

    #[test]
    fn test_mtime_nonexistent_file() {
        assert!(mtime("/nonexistent/file/xyz").is_none());
    }

    #[test]
    fn test_mtime_existing_file() {
        let dir = tempfile::tempdir().unwrap();
        let file_path = dir.path().join("test.txt");
        std::fs::write(&file_path, "hello").unwrap();
        assert!(mtime(file_path.to_str().unwrap()).is_some());
    }

    // ─── print_dag_phases ─────────────────────────────────────────────────────

    #[test]
    fn test_print_dag_phases_no_panic_empty_vec() {
        print_dag_phases(&[]);
    }

    #[test]
    fn test_print_dag_phases_no_panic_with_tasks() {
        let tasks = vec![
            ConcreteTask {
                step_name: "align".to_string(),
                id: "align__s1".to_string(),
                cmd: "bwa mem ref.fa s1.fq".to_string(),
                inputs: vec!["s1.fq".to_string()],
                outputs: vec!["s1.bam".to_string()],
                deps: vec![],
                gather: false,
                env: None,
            },
            ConcreteTask {
                step_name: "sort".to_string(),
                id: "sort__s1".to_string(),
                cmd: "samtools sort s1.bam".to_string(),
                inputs: vec!["s1.bam".to_string()],
                outputs: vec!["s1.sorted.bam".to_string()],
                deps: vec!["align__s1".to_string()],
                gather: false,
                env: None,
            },
        ];
        print_dag_phases(&tasks);
    }

    // ─── print_task_dry_run ───────────────────────────────────────────────────

    #[test]
    fn test_print_task_dry_run_no_panic_with_io() {
        let task = ConcreteTask {
            step_name: "test".to_string(),
            id: "test__1".to_string(),
            cmd: "echo hello".to_string(),
            inputs: vec!["in.txt".to_string()],
            outputs: vec!["out.txt".to_string()],
            deps: vec![],
            gather: false,
            env: None,
        };
        print_task_dry_run(&task, 1, 5);
    }

    #[test]
    fn test_print_task_dry_run_with_env() {
        let task = ConcreteTask {
            step_name: "test".to_string(),
            id: "test__1".to_string(),
            cmd: "echo hello".to_string(),
            inputs: vec![],
            outputs: vec![],
            deps: vec![],
            gather: false,
            env: Some("THREADS=4".to_string()),
        };
        print_task_dry_run(&task, 0, 1);
    }

    // ─── print_verify_report ──────────────────────────────────────────────────

    #[test]
    fn test_print_verify_report_no_diags() {
        let def = WorkflowDef {
            workflow: WorkflowMeta {
                name: "test".to_string(),
                description: String::new(),
                version: String::new(),
            },
            wildcards: HashMap::new(),
            params: HashMap::new(),
            steps: vec![],
        };
        let result = print_verify_report(&def, &[]);
        assert!(!result, "no errors should return false");
    }

    #[test]
    fn test_print_verify_report_with_single_error() {
        let def = WorkflowDef {
            workflow: WorkflowMeta {
                name: "test".to_string(),
                description: String::new(),
                version: String::new(),
            },
            wildcards: HashMap::new(),
            params: HashMap::new(),
            steps: vec![],
        };
        let diags = vec![Diagnostic {
            level: DiagLevel::Error,
            message: "test error".to_string(),
        }];
        let result = print_verify_report(&def, &diags);
        assert!(result, "errors should return true");
    }

    #[test]
    fn test_print_verify_report_warnings_only() {
        let def = WorkflowDef {
            workflow: WorkflowMeta {
                name: "test".to_string(),
                description: String::new(),
                version: String::new(),
            },
            wildcards: HashMap::new(),
            params: HashMap::new(),
            steps: vec![],
        };
        let diags = vec![Diagnostic {
            level: DiagLevel::Warning,
            message: "test warning".to_string(),
        }];
        let result = print_verify_report(&def, &diags);
        assert!(!result, "warnings only should return false");
    }

    #[test]
    fn test_print_verify_report_mixed_diags() {
        let def = WorkflowDef {
            workflow: WorkflowMeta {
                name: "test".to_string(),
                description: String::new(),
                version: String::new(),
            },
            wildcards: HashMap::new(),
            params: HashMap::new(),
            steps: vec![],
        };
        let diags = vec![
            Diagnostic {
                level: DiagLevel::Error,
                message: "error 1".to_string(),
            },
            Diagnostic {
                level: DiagLevel::Warning,
                message: "warning 1".to_string(),
            },
            Diagnostic {
                level: DiagLevel::Error,
                message: "error 2".to_string(),
            },
        ];
        let result = print_verify_report(&def, &diags);
        assert!(result, "should return true when errors present");
    }

    // ─── visualize_workflow ───────────────────────────────────────────────────

    #[test]
    fn test_visualize_workflow_simple() {
        let def = WorkflowDef {
            workflow: WorkflowMeta {
                name: "test-vis".to_string(),
                description: "A test workflow".to_string(),
                version: String::new(),
            },
            wildcards: HashMap::new(),
            params: HashMap::new(),
            steps: vec![StepDef {
                name: "step1".to_string(),
                cmd: "echo hello".to_string(),
                inputs: vec![],
                outputs: vec![],
                depends_on: vec![],
                gather: false,
                env: None,
            }],
        };
        let result = visualize_workflow(&def);
        assert!(result.is_ok(), "simple visualization should succeed");
    }

    #[test]
    fn test_visualize_workflow_with_wildcards() {
        let mut wildcards = HashMap::new();
        wildcards.insert(
            "sample".to_string(),
            vec![
                "s1".to_string(),
                "s2".to_string(),
                "s3".to_string(),
                "s4".to_string(),
                "s5".to_string(),
            ],
        );
        let def = WorkflowDef {
            workflow: WorkflowMeta {
                name: "wildcard-vis".to_string(),
                description: String::new(),
                version: String::new(),
            },
            wildcards,
            params: HashMap::new(),
            steps: vec![StepDef {
                name: "align".to_string(),
                cmd: "bwa mem ref.fa {sample}.fq".to_string(),
                inputs: vec!["{sample}.fq".to_string()],
                outputs: vec!["{sample}.bam".to_string()],
                depends_on: vec![],
                gather: false,
                env: None,
            }],
        };
        let result = visualize_workflow(&def);
        assert!(result.is_ok());
    }

    // ─── WorkflowDef::from_str_content ────────────────────────────────────────

    #[test]
    fn test_from_str_content_valid() {
        let toml = r#"
[workflow]
name = "test"
description = "A test"

[[step]]
name = "s1"
cmd = "echo hello"
"#;
        let def = WorkflowDef::from_str_content(toml);
        assert!(def.is_ok());
        let def = def.unwrap();
        assert_eq!(def.workflow.name, "test");
        assert_eq!(def.steps.len(), 1);
    }

    #[test]
    fn test_from_str_content_invalid_toml() {
        let bad = "this is not valid toml [[[";
        let result = WorkflowDef::from_str_content(bad);
        assert!(result.is_err());
    }

    #[test]
    fn test_from_str_content_empty() {
        // Empty string is not valid TOML for the expected schema
        let result = WorkflowDef::from_str_content("");
        assert!(result.is_err());
    }

    // ─── is_up_to_date with real files ────────────────────────────────────────

    #[test]
    fn test_is_up_to_date_output_newer_than_input() {
        let dir = tempfile::tempdir().unwrap();
        let input_path = dir.path().join("input.txt");
        std::fs::write(&input_path, "input data").unwrap();
        // Small sleep to ensure different timestamps
        std::thread::sleep(std::time::Duration::from_millis(50));
        let output_path = dir.path().join("output.txt");
        std::fs::write(&output_path, "output data").unwrap();

        let task = ConcreteTask {
            step_name: "test".to_string(),
            id: "test__1".to_string(),
            cmd: "echo".to_string(),
            inputs: vec![input_path.to_str().unwrap().to_string()],
            outputs: vec![output_path.to_str().unwrap().to_string()],
            deps: vec![],
            gather: false,
            env: None,
        };
        assert!(is_up_to_date(&task), "output is newer than input");
    }

    // ─── format_toml additional ───────────────────────────────────────────────

    #[test]
    fn test_format_toml_empty_workflow() {
        let def = WorkflowDef {
            workflow: WorkflowMeta {
                name: String::new(),
                description: String::new(),
                version: String::new(),
            },
            wildcards: HashMap::new(),
            params: HashMap::new(),
            steps: vec![],
        };
        let toml = format_toml(&def);
        assert!(toml.contains("[workflow]"));
    }

    #[test]
    fn test_format_toml_with_params() {
        let mut params = HashMap::new();
        params.insert("threads".to_string(), "8".to_string());
        params.insert("memory".to_string(), "16G".to_string());
        let def = WorkflowDef {
            workflow: WorkflowMeta {
                name: "param-test".to_string(),
                description: String::new(),
                version: String::new(),
            },
            wildcards: HashMap::new(),
            params,
            steps: vec![],
        };
        let toml = format_toml(&def);
        assert!(toml.contains("[params]"));
        assert!(toml.contains("threads"));
        assert!(toml.contains("memory"));
    }
}