nika 0.35.4

Semantic YAML workflow engine for AI tasks - DAG execution, MCP integration, multi-provider LLM support
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
//! Studio View - YAML editor with validation and task DAG
//!
//! Layout:
//! ```text
//! ┌─────────────────────────────────────────────────────┬───────────────────────┐
//! │ EDITOR                                              │ STRUCTURE             │
//! │ YAML with line numbers and syntax highlighting      │ Task DAG mini-view    │
//! ├─────────────────────────────────────────────────────┴───────────────────────┤
//! │ Valid YAML │ Schema OK │ 1 warning                                          │
//! └─────────────────────────────────────────────────────────────────────────────┘
//! ```
//!
//! Note: This implementation uses a simple line-based editor (`TextBuffer`).
//! Full text editor integration is planned via `ratatui-textarea` (the ratatui org's
//! hard fork of tui-textarea with ratatui 0.30 support).
//!
//! See: <https://github.com/ratatui/ratatui-textarea> (replaces rhysd/tui-textarea)

use crate::ast::parse_workflow;
use camino::Utf8Path;
use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::path::PathBuf;
use std::time::{Duration, Instant};

use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use ratatui::{
    layout::{Constraint, Direction, Layout, Rect},
    style::{Color, Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Borders, Paragraph, Widget},
    Frame,
};

use super::view_trait::View;
use super::ViewAction;
use crate::ast::schema_validator::WorkflowSchemaValidator;
use crate::ast::Workflow;
use crate::error::NikaError;
use crate::tui::diagnostics::DiagnosticsEngine;
use crate::tui::edit_history::EditHistory;
use crate::tui::git::{GitStatus, LineChange};
use crate::tui::selection::{Position, Selection, SelectionSet};
use crate::tui::state::TuiState;
use crate::tui::theme::{TaskStatus, Theme, VerbColor};
use crate::tui::views::TuiView;
use crate::tui::widgets::tree::{
    build_git_status_cache, AnimationTicker, FilterConfig, GitStatusCache, TreeAction, TreeColors,
    TreeFilter, TreeNode, TreeState, TreeWidget,
};
use crate::tui::widgets::{
    centered_rect, CommandPalette, CommandPaletteState, DagAscii, MatrixRain, NodeBoxData,
    NodeBoxMode, ScrollIndicator, WhichKey, WhichKeyState,
};
use crate::util::atomic_write;

/// Editor mode (vim-like)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum EditorMode {
    #[default]
    Normal,
    Insert,
}

// ═══════════════════════════════════════════════════════════════════════════════
// StudioView: 3-Panel Layout
// Browser (20%) | Editor (50%) | DAG Structure (30%)
// ═══════════════════════════════════════════════════════════════════════════════

/// Panel focus in 3-panel Studio layout
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum StudioFocus {
    /// Left panel: File browser (TreeWidget)
    #[default]
    Browser,
    /// Center panel: YAML editor
    Editor,
    /// Right panel: DAG structure (read-only)
    Dag,
}

impl StudioFocus {
    /// Cycle to next panel (Browser -> Editor -> Dag -> Browser)
    pub fn next(&self) -> Self {
        match self {
            StudioFocus::Browser => StudioFocus::Editor,
            StudioFocus::Editor => StudioFocus::Dag,
            StudioFocus::Dag => StudioFocus::Browser,
        }
    }

    /// Cycle to previous panel (Browser <- Editor <- Dag <- Browser)
    pub fn prev(&self) -> Self {
        match self {
            StudioFocus::Browser => StudioFocus::Dag,
            StudioFocus::Editor => StudioFocus::Browser,
            StudioFocus::Dag => StudioFocus::Editor,
        }
    }

    /// Display name for status bar
    pub fn title(&self) -> &'static str {
        match self {
            StudioFocus::Browser => "Browser",
            StudioFocus::Editor => "Editor",
            StudioFocus::Dag => "DAG",
        }
    }
}

/// Layout ratios for 3-panel Studio
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum StudioRatio {
    /// Balanced: 20% / 50% / 30%
    #[default]
    Balanced,
    /// Editor focus: 15% / 65% / 20%
    EditorFocus,
    /// Browser focus: 35% / 45% / 20%
    BrowserFocus,
    /// DAG focus: 15% / 35% / 50%
    DagFocus,
}

impl StudioRatio {
    /// Get constraints for Layout::split()
    pub fn constraints(&self) -> [Constraint; 3] {
        match self {
            StudioRatio::Balanced => [
                Constraint::Percentage(20),
                Constraint::Percentage(50),
                Constraint::Percentage(30),
            ],
            StudioRatio::EditorFocus => [
                Constraint::Percentage(15),
                Constraint::Percentage(65),
                Constraint::Percentage(20),
            ],
            StudioRatio::BrowserFocus => [
                Constraint::Percentage(35),
                Constraint::Percentage(45),
                Constraint::Percentage(20),
            ],
            StudioRatio::DagFocus => [
                Constraint::Percentage(15),
                Constraint::Percentage(35),
                Constraint::Percentage(50),
            ],
        }
    }

    /// Cycle to next ratio (Balanced -> EditorFocus -> BrowserFocus -> DagFocus -> Balanced)
    pub fn next(&self) -> Self {
        match self {
            StudioRatio::Balanced => StudioRatio::EditorFocus,
            StudioRatio::EditorFocus => StudioRatio::BrowserFocus,
            StudioRatio::BrowserFocus => StudioRatio::DagFocus,
            StudioRatio::DagFocus => StudioRatio::Balanced,
        }
    }

    /// Display name for status bar
    pub fn title(&self) -> &'static str {
        match self {
            StudioRatio::Balanced => "Balanced",
            StudioRatio::EditorFocus => "Editor+",
            StudioRatio::BrowserFocus => "Browser+",
            StudioRatio::DagFocus => "DAG+",
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// StudioView: Main 3-Panel Struct
// ═══════════════════════════════════════════════════════════════════════════════

/// Studio View - Unified 3-panel layout: Browser | Editor | DAG
///
/// This is the main view for workflow development, combining:
/// - **Browser**: TreeWidget for file navigation (VS Code-like)
/// - **Editor**: YamlEditorPanel for YAML editing with validation
/// - **DAG**: Structure preview showing task dependencies
pub struct StudioView {
    // === Browser Panel (Left) - Real TreeWidget ===
    /// Root directory for file browsing
    pub root_dir: PathBuf,
    /// Tree widget state (selection, expansion)
    pub tree_state: TreeState,
    /// Tree widget colors (from theme)
    pub tree_colors: TreeColors,
    /// Animation ticker for glow effects
    pub animation_ticker: AnimationTicker,
    /// File filter configuration (W/A/E/0 hotkeys)
    pub filter_config: FilterConfig,
    /// Cached git status for files (refreshed periodically)
    git_cache: GitStatusCache,
    /// Last time the git cache was refreshed
    git_cache_time: Instant,
    /// Cached tree root node (avoids rebuilding on every frame)
    cached_tree: Option<TreeNode>,
    /// Quick Access: recently used .nika.yaml files
    quick_access: Vec<PathBuf>,

    // === Editor Panel (Center) ===
    /// YAML editor panel with validation and syntax highlighting
    pub editor: YamlEditorPanel,

    // === Focus & Layout ===
    /// Currently focused panel
    pub focus: StudioFocus,
    /// Current panel ratio
    pub ratio: StudioRatio,

    // === Overlays ===
    /// Command palette state (Ctrl+P)
    pub command_palette: CommandPaletteState,
    /// Which-key popup state (g, z, \[, \], Space prefixes)
    pub which_key: WhichKeyState,
}

impl Default for StudioView {
    fn default() -> Self {
        Self::new()
    }
}

impl StudioView {
    /// Create a new StudioView with current working directory
    pub fn new() -> Self {
        let root_dir = std::env::current_dir().unwrap_or_default();
        Self::with_root(root_dir)
    }

    /// Create a StudioView with a specific root directory
    pub fn with_root(root_dir: PathBuf) -> Self {
        // Build git status cache from root directory
        let root_path = Utf8Path::from_path(&root_dir).unwrap_or(Utf8Path::new("."));
        let git_cache = build_git_status_cache(root_path);

        // Scan for .nika.yaml files for Quick Access
        let quick_access = Self::scan_nika_files(&root_dir);

        Self {
            root_dir,
            tree_state: TreeState::default(),
            tree_colors: TreeColors::default(),
            animation_ticker: AnimationTicker::new(),
            filter_config: FilterConfig::default(),
            editor: YamlEditorPanel::new(),
            focus: StudioFocus::default(),
            ratio: StudioRatio::default(),
            git_cache,
            git_cache_time: Instant::now(),
            cached_tree: None,
            quick_access,
            // Overlay states
            command_palette: CommandPaletteState::new(),
            which_key: WhichKeyState::new(),
        }
    }

    /// Scan for .nika.yaml files in the root directory (max 5, top-level first)
    fn scan_nika_files(root: &PathBuf) -> Vec<PathBuf> {
        let mut files = Vec::new();

        // First pass: root level .nika.yaml files
        if let Ok(entries) = std::fs::read_dir(root) {
            for entry in entries.filter_map(|e| e.ok()) {
                let path = entry.path();
                if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
                    if name.ends_with(".nika.yaml") {
                        files.push(path);
                    }
                }
            }
        }

        // Second pass: workflows/ directory
        let workflows_dir = root.join("workflows");
        if workflows_dir.is_dir() {
            if let Ok(entries) = std::fs::read_dir(&workflows_dir) {
                for entry in entries.filter_map(|e| e.ok()) {
                    let path = entry.path();
                    if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
                        if name.ends_with(".nika.yaml") && files.len() < 5 {
                            files.push(path);
                        }
                    }
                }
            }
        }

        // Limit to 5 files
        files.truncate(5);
        files
    }

    /// Load a file into the editor panel (returns Result for error handling)
    pub fn load_file(&mut self, path: PathBuf) -> Result<(), std::io::Error> {
        let result = self.editor.load_file(path);
        self.focus = StudioFocus::Editor;
        result
    }

    /// Open a file in the editor panel (ignores errors)
    pub fn open_file(&mut self, path: PathBuf) {
        // Ignore errors for now - user will see empty editor
        let _ = self.load_file(path);
    }

    /// Get border style based on focus state
    fn border_style(&self, panel: StudioFocus, theme: &Theme) -> Style {
        use ratatui::style::Modifier;
        if self.focus == panel {
            Style::default()
                .fg(theme.border_focused)
                .add_modifier(Modifier::BOLD)
        } else {
            Style::default().fg(theme.border_normal)
        }
    }

    /// Render the browser panel with TreeWidget
    fn render_browser(&mut self, frame: &mut Frame, area: Rect, theme: &Theme) {
        let style = self.border_style(StudioFocus::Browser, theme);

        // UX: Add focus indicator ● to show which panel is active
        let focus_indicator = if self.focus == StudioFocus::Browser {
            ""
        } else {
            " "
        };

        // Show filter badge in title so users know which filter is active
        let title = if self.filter_config.filter.badge().is_empty() {
            format!(" {} Browser ", focus_indicator)
        } else {
            format!(
                " {} Browser [{}] ",
                focus_indicator,
                self.filter_config.filter.badge()
            )
        };

        let block = Block::default()
            .title(Span::styled(title, style))
            .borders(Borders::ALL)
            .border_style(style);

        let inner = block.inner(area);
        frame.render_widget(block, area);

        // Split into Quick Access (top) + Tree (bottom)
        let quick_access_height = if self.quick_access.is_empty() {
            0
        } else {
            // Header (1) + files (n) + separator (1)
            (self.quick_access.len() + 2) as u16
        };

        let chunks = Layout::default()
            .direction(Direction::Vertical)
            .constraints([Constraint::Length(quick_access_height), Constraint::Min(3)])
            .split(inner);

        // Render Quick Access section
        if !self.quick_access.is_empty() {
            self.render_quick_access(frame, chunks[0], theme);
        }

        let tree_area = chunks[1];

        // Refresh git cache periodically (every 5 seconds)
        const GIT_CACHE_TTL: Duration = Duration::from_secs(5);
        let root_path = Utf8Path::from_path(&self.root_dir).unwrap_or(Utf8Path::new("."));

        if self.git_cache_time.elapsed() > GIT_CACHE_TTL {
            self.git_cache = build_git_status_cache(root_path);
            self.git_cache_time = Instant::now();
            // Invalidate tree cache when git status changes
            self.cached_tree = None;
        }

        // Build tree with caching (rebuild only when cache is empty)
        // Track if tree was just rebuilt to avoid per-frame overhead
        let tree_rebuilt = self.cached_tree.is_none();
        let root_node = if let Some(ref cached) = self.cached_tree {
            cached.clone()
        } else {
            let tree = TreeNode::build_tree(root_path, Some(&self.git_cache), None);
            self.cached_tree = Some(tree.clone());
            tree
        };

        // Only update visible_nodes when tree structure changes
        // This was being called EVERY frame causing severe performance issues
        if tree_rebuilt {
            self.tree_state.update_visible_nodes(&root_node);
            self.tree_state.select_first_if_none();
            // Ensure root is expanded on rebuild
            if !self.tree_state.is_expanded(root_node.id) {
                self.tree_state.expand(root_node.id);
                self.tree_state.update_visible_nodes(&root_node);
            }
        }

        // Use solarized colors (dark by default, matching Theme::default())
        self.tree_colors = TreeColors::solarized_dark();

        // Render TreeWidget with state
        let tree_widget = TreeWidget::new(&root_node)
            .colors(self.tree_colors.clone())
            .ticker(&self.animation_ticker)
            .filter(self.filter_config.clone());

        frame.render_stateful_widget(tree_widget, tree_area, &mut self.tree_state);
    }

    /// Render Quick Access section
    fn render_quick_access(&self, frame: &mut Frame, area: Rect, theme: &Theme) {
        let mut lines: Vec<Line> = Vec::new();

        // Header with emoji
        lines.push(Line::from(vec![
            Span::styled("", Style::default().fg(theme.status_running)),
            Span::styled(
                "QUICK ACCESS",
                Style::default()
                    .fg(theme.text_primary)
                    .add_modifier(Modifier::BOLD),
            ),
        ]));

        // Quick access files
        for path in &self.quick_access {
            let name = path
                .file_name()
                .and_then(|n| n.to_str())
                .unwrap_or("unknown");
            lines.push(Line::from(vec![
                Span::raw("  "),
                Span::styled("🦋 ", Style::default()),
                Span::styled(name, Style::default().fg(theme.border_focused)),
            ]));
        }

        // Separator
        let sep_width = area.width.saturating_sub(2) as usize;
        lines.push(Line::from(Span::styled(
            "".repeat(sep_width),
            Style::default().fg(theme.border_normal),
        )));

        let paragraph = Paragraph::new(lines);
        frame.render_widget(paragraph, area);
    }

    /// Render the DAG panel (delegates to editor's DAG rendering)
    fn render_dag_panel(&self, frame: &mut Frame, area: Rect, theme: &Theme) {
        let style = self.border_style(StudioFocus::Dag, theme);

        // UX: Add focus indicator ● to show which panel is active
        let focus_indicator = if self.focus == StudioFocus::Dag {
            ""
        } else {
            " "
        };

        let block = Block::default()
            .title(Span::styled(
                format!(" {} DAG Preview ", focus_indicator),
                style,
            ))
            .borders(Borders::ALL)
            .border_style(style);

        let inner = block.inner(area);
        frame.render_widget(block, area);

        // Render DAG structure from editor's cached workflow
        let yaml = self.editor.buffer.content();
        self.editor.render_dag_structure(frame, inner, &yaml, theme);
    }

    /// Refresh the tree cache (called when filesystem changes are expected)
    pub fn refresh_tree(&mut self) {
        let root_path = Utf8Path::from_path(&self.root_dir).unwrap_or(Utf8Path::new("."));
        self.git_cache = build_git_status_cache(root_path);
        self.git_cache_time = Instant::now();
        self.cached_tree = None;
    }

    /// Get or build the cached tree node
    fn get_or_build_tree(&mut self) -> TreeNode {
        if let Some(ref cached) = self.cached_tree {
            return cached.clone();
        }
        let root_path = Utf8Path::from_path(&self.root_dir).unwrap_or(Utf8Path::new("."));
        let tree = TreeNode::build_tree(root_path, Some(&self.git_cache), None);
        self.cached_tree = Some(tree.clone());
        tree
    }

    /// Handle keyboard input for the browser panel
    fn handle_browser_key(&mut self, key: KeyEvent) -> ViewAction {
        // Get cached tree for tree operations
        let root_node = self.get_or_build_tree();

        // Try TreeAction navigation first
        if let Some(action) = TreeAction::from_key_event(key) {
            // Only update visible_nodes for structure-changing actions
            let needs_update = matches!(
                action,
                TreeAction::Toggle | TreeAction::Left | TreeAction::Right
            );
            self.tree_state.handle_action(action, &root_node);
            if needs_update {
                self.tree_state.update_visible_nodes(&root_node);
            }
            return ViewAction::None;
        }

        // Handle other keys
        match key.code {
            KeyCode::Enter => {
                // Open selected file in editor
                if let Some(node_id) = self.tree_state.selected() {
                    if let Some(node) = self.tree_state.find_node(&root_node, node_id) {
                        let path = PathBuf::from(node.path.as_str());
                        if path.is_file()
                            && path.extension().is_some_and(|e| e == "yaml" || e == "yml")
                        {
                            self.open_file(path);
                        }
                    }
                }
                ViewAction::None
            }
            // Refresh tree (R key)
            KeyCode::Char('r') | KeyCode::Char('R') => {
                self.refresh_tree();
                ViewAction::None
            }
            // Filter hotkeys (W/A/E/0)
            // Invalidate cached_tree so visible_nodes updates correctly
            KeyCode::Char('w') | KeyCode::Char('W') => {
                self.filter_config.set_filter(TreeFilter::WorkflowsOnly);
                self.cached_tree = None; // Force rebuild with new filter
                ViewAction::None
            }
            KeyCode::Char('a') | KeyCode::Char('A') => {
                self.filter_config.set_filter(TreeFilter::All);
                self.cached_tree = None; // Force rebuild with new filter
                ViewAction::None
            }
            KeyCode::Char('e') | KeyCode::Char('E') => {
                self.filter_config.set_filter(TreeFilter::EcosystemOnly);
                self.cached_tree = None; // Force rebuild with new filter
                ViewAction::None
            }
            KeyCode::Char('0') => {
                self.filter_config.reset();
                self.cached_tree = None; // Force rebuild with new filter
                ViewAction::None
            }
            _ => ViewAction::None,
        }
    }
}

impl View for StudioView {
    fn render(&mut self, frame: &mut Frame, area: Rect, _state: &TuiState, theme: &Theme) {
        // Main layout: 3 horizontal panels + status bar at bottom
        let main_chunks = Layout::default()
            .direction(Direction::Vertical)
            .constraints([Constraint::Min(10), Constraint::Length(3)])
            .split(area);

        // Split main area into 3 horizontal panels
        let panel_chunks = Layout::default()
            .direction(Direction::Horizontal)
            .constraints(self.ratio.constraints())
            .split(main_chunks[0]);

        // Render each panel
        self.render_browser(frame, panel_chunks[0], theme);

        // Editor panel with focused border
        // UX: Add focus indicator ● at start, modified indicator ◆ after name
        let editor_style = self.border_style(StudioFocus::Editor, theme);
        let focus_indicator = if self.focus == StudioFocus::Editor {
            ""
        } else {
            " "
        };
        let modified_indicator = if self.editor.modified { "" } else { "" };
        let editor_block = Block::default()
            .title(Span::styled(
                format!(" {} Editor{} ", focus_indicator, modified_indicator),
                editor_style,
            ))
            .borders(Borders::ALL)
            .border_style(editor_style);

        let editor_inner = editor_block.inner(panel_chunks[1]);
        frame.render_widget(editor_block, panel_chunks[1]);
        self.editor.render_editor(frame, editor_inner, theme);

        // DAG panel
        self.render_dag_panel(frame, panel_chunks[2], theme);

        // Validation bar at bottom
        self.editor.render_validation(frame, main_chunks[1], theme);

        // Render overlays on top
        // Command palette (Ctrl+P)
        if self.command_palette.visible {
            let palette_area = centered_rect(60, 50, area);
            CommandPalette::new(&self.command_palette).render(palette_area, frame.buffer_mut());
        }

        // Which-key popup (vim-style)
        if self.which_key.is_visible() {
            WhichKey::new(&self.which_key).render(area, frame.buffer_mut());
        }
    }

    fn handle_key(&mut self, key: KeyEvent, state: &mut TuiState) -> ViewAction {
        // Handle command palette overlay first
        if self.command_palette.visible {
            return self.handle_palette_key(key);
        }

        // Handle which-key popup
        if self.which_key.is_visible() || self.which_key.is_pending() {
            if let KeyCode::Char(c) = key.code {
                if let Some((prefix, key_char)) = self.which_key.on_key(c) {
                    return self.handle_which_key_action(prefix, key_char);
                }
            }
            if key.code == KeyCode::Esc {
                self.which_key.close();
                return ViewAction::None;
            }
        }

        // Detect which-key prefixes in Normal mode
        // Only trigger for g, z, [, ], Space when not in Insert mode
        if self.focus != StudioFocus::Editor || self.editor.mode != EditorMode::Insert {
            if let KeyCode::Char(c) = key.code {
                if key.modifiers == KeyModifiers::NONE && self.which_key.on_prefix(c) {
                    // Prefix detected - wait for popup or next key
                    return ViewAction::None;
                }
            }
        }

        // Global shortcuts first
        match (key.code, key.modifiers) {
            // Ctrl+P: Open command palette
            (KeyCode::Char('p'), KeyModifiers::CONTROL) => {
                self.command_palette.open();
                return ViewAction::None;
            }
            // Tab: Cycle focus forward (but NOT when editor is in Insert mode)
            // Allow Tab for indentation in Insert mode
            (KeyCode::Tab, KeyModifiers::NONE) => {
                if self.focus == StudioFocus::Editor && self.editor.mode == EditorMode::Insert {
                    // Let editor handle Tab for indentation
                    return self.editor.handle_key(key, state);
                }
                self.focus = self.focus.next();
                return ViewAction::None;
            }
            // Shift+Tab: Cycle focus backward (or dedent in Insert mode)
            (KeyCode::BackTab, _) => {
                if self.focus == StudioFocus::Editor && self.editor.mode == EditorMode::Insert {
                    // Let editor handle Shift+Tab for dedent
                    return self.editor.handle_key(key, state);
                }
                self.focus = self.focus.prev();
                return ViewAction::None;
            }
            // Ctrl+]: Cycle ratio
            (KeyCode::Char(']'), KeyModifiers::CONTROL) => {
                self.ratio = self.ratio.next();
                return ViewAction::None;
            }
            // Ctrl+T or Shift+T: Toggle theme
            (KeyCode::Char('T'), _) | (KeyCode::Char('t'), KeyModifiers::CONTROL) => {
                return ViewAction::ToggleTheme;
            }
            // View switching: number keys 2-3
            (KeyCode::Char('2'), _) => return ViewAction::SwitchView(TuiView::Command),
            (KeyCode::Char('3'), _) => return ViewAction::SwitchView(TuiView::Control),
            _ => {}
        }

        // Dispatch to focused panel
        match self.focus {
            StudioFocus::Browser => self.handle_browser_key(key),
            StudioFocus::Editor => self.editor.handle_key(key, state),
            StudioFocus::Dag => {
                // DAG panel is read-only, but support scroll
                match key.code {
                    KeyCode::Up | KeyCode::Char('k') => {
                        self.editor.dag_scroll = self.editor.dag_scroll.saturating_sub(1);
                        ViewAction::None
                    }
                    KeyCode::Down | KeyCode::Char('j') => {
                        self.editor.dag_scroll = self.editor.dag_scroll.saturating_add(1);
                        ViewAction::None
                    }
                    KeyCode::Char('e') | KeyCode::Char('E') => {
                        self.editor.toggle_dag_mode();
                        ViewAction::None
                    }
                    _ => ViewAction::None,
                }
            }
        }
    }

    fn status_line(&self, _state: &TuiState) -> String {
        let mode = match self.editor.mode {
            EditorMode::Normal => "NORMAL",
            EditorMode::Insert => "INSERT",
        };
        // UX: Highlight focus panel with ● prefix
        format!(
            "Studio | ●{} | {} | {} | [Tab] Panel • [Ctrl+]] Ratio",
            self.focus.title(),
            self.ratio.title(),
            mode
        )
    }

    fn tick(&mut self, _state: &mut TuiState) {
        // Tick animation for tree widget glow effects
        self.animation_ticker.tick();
        // Tick editor (matrix rain, etc.)
        self.editor.tick();
        // Run debounced validation
        self.editor.maybe_validate();
        // Tick which-key popup for delay timing
        self.which_key.tick();
    }

    fn on_enter(&mut self, _state: &mut TuiState) {
        // Root cause: visible_nodes was never populated at startup,
        // causing keyboard navigation to fail.

        let root_path = Utf8Path::from_path(&self.root_dir).unwrap_or(Utf8Path::new("."));

        // 1. Refresh git cache
        self.git_cache = build_git_status_cache(root_path);
        self.git_cache_time = Instant::now();

        // 2. Build tree and cache it
        let root_node = TreeNode::build_tree(root_path, Some(&self.git_cache), None);
        self.cached_tree = Some(root_node.clone());

        // 3. Update visible_nodes for navigation
        self.tree_state.update_visible_nodes(&root_node);

        // 4. Expand root directory so children are visible
        self.tree_state.expand(root_node.id);

        // 5. Re-update visible_nodes after expansion (to include children)
        self.tree_state.update_visible_nodes(&root_node);

        // 6. Select first item for immediate navigation
        self.tree_state.select_first_if_none();
    }

    fn on_leave(&mut self, _state: &mut TuiState) {
        // Save session state when leaving
        // Could persist open files, cursor positions, etc.
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Command Palette & Which-Key Handling
// ═══════════════════════════════════════════════════════════════════════════════

impl StudioView {
    /// Handle keyboard input when command palette is open
    fn handle_palette_key(&mut self, key: KeyEvent) -> ViewAction {
        match key.code {
            KeyCode::Esc => {
                self.command_palette.close();
                ViewAction::None
            }
            KeyCode::Enter => {
                // Clone the command ID to avoid borrow conflict
                let cmd_id = self
                    .command_palette
                    .selected_command()
                    .map(|cmd| cmd.id.clone());
                if let Some(id) = cmd_id {
                    let action = self.execute_palette_command(&id);
                    self.command_palette.close();
                    return action;
                }
                ViewAction::None
            }
            KeyCode::Up => {
                self.command_palette.select_prev();
                ViewAction::None
            }
            KeyCode::Down => {
                self.command_palette.select_next();
                ViewAction::None
            }
            KeyCode::Char(c) => {
                self.command_palette.query.push(c);
                self.command_palette.update_filter();
                ViewAction::None
            }
            KeyCode::Backspace => {
                self.command_palette.query.pop();
                self.command_palette.update_filter();
                ViewAction::None
            }
            _ => ViewAction::None,
        }
    }

    /// Execute a command from the palette
    fn execute_palette_command(&mut self, cmd_id: &str) -> ViewAction {
        match cmd_id {
            // View switching (3-view: Studio, Command, Control)
            "studio" => ViewAction::SwitchView(TuiView::Studio),
            "command" | "chat" | "home" | "monitor" => ViewAction::SwitchView(TuiView::Command),
            "control" => ViewAction::SwitchView(TuiView::Control),
            // Workflow actions
            "run" => {
                // Run the current workflow
                if let Some(path) = &self.editor.path {
                    ViewAction::RunWorkflow(path.clone())
                } else {
                    ViewAction::None
                }
            }
            "validate" => {
                // Trigger validation
                self.editor.validate();
                ViewAction::None
            }
            // Help - switch to Settings view which has help info
            "help" => ViewAction::SwitchView(TuiView::Control),
            _ => ViewAction::None,
        }
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // Which-Key Popup Handling
    // ═══════════════════════════════════════════════════════════════════════════

    /// Handle a which-key action (prefix + follow-up key)
    fn handle_which_key_action(&mut self, prefix: char, key: char) -> ViewAction {
        match (prefix, key) {
            // g prefix - Go to commands
            ('g', 'g') => {
                // Go to top of file
                self.editor.buffer.cursor_row = 0;
                self.editor.buffer.cursor_col = 0;
                self.editor.buffer.scroll_offset = 0;
                ViewAction::None
            }
            ('g', 'e') => {
                // Go to end of file
                let last_line = self.editor.buffer.lines.len().saturating_sub(1);
                self.editor.buffer.cursor_row = last_line;
                self.editor.buffer.cursor_col = 0;
                ViewAction::None
            }
            // z prefix - View/centering commands
            ('z', 'z') => {
                // Center cursor in viewport
                let visible = self.editor.buffer.visible_height.get();
                let target_scroll = self.editor.buffer.cursor_row.saturating_sub(visible / 2);
                self.editor.buffer.scroll_offset = target_scroll;
                ViewAction::None
            }
            ('z', 't') => {
                // Cursor to top of viewport
                self.editor.buffer.scroll_offset = self.editor.buffer.cursor_row;
                ViewAction::None
            }
            ('z', 'b') => {
                // Cursor to bottom of viewport
                let visible = self.editor.buffer.visible_height.get();
                self.editor.buffer.scroll_offset = self
                    .editor
                    .buffer
                    .cursor_row
                    .saturating_sub(visible.saturating_sub(1));
                ViewAction::None
            }
            // Space (leader) prefix - Common commands
            (' ', 'w') => {
                // Save file
                if let Err(e) = self.editor.save_file() {
                    tracing::error!("Failed to save: {}", e);
                }
                ViewAction::None
            }
            (' ', 'f') => {
                // Find file (open command palette)
                self.command_palette.open();
                ViewAction::None
            }
            (' ', 'e') => {
                // Toggle browser focus
                self.focus = StudioFocus::Browser;
                ViewAction::None
            }
            (' ', 'q') => ViewAction::Quit,
            _ => ViewAction::None,
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// YamlEditorPanel and TextBuffer
// ═══════════════════════════════════════════════════════════════════════════════

/// Validation result
#[derive(Debug, Clone)]
pub struct ValidationResult {
    pub yaml_valid: bool,
    pub schema_valid: bool,
    pub warnings: Vec<String>,
    pub errors: Vec<String>,
}

impl Default for ValidationResult {
    fn default() -> Self {
        Self {
            yaml_valid: true,
            schema_valid: true,
            warnings: vec![],
            errors: vec![],
        }
    }
}

/// Simple line-based text buffer for the editor
#[derive(Debug)]
pub struct TextBuffer {
    /// Lines of text
    lines: Vec<String>,
    /// Cursor row (0-indexed)
    cursor_row: usize,
    /// Cursor column (0-indexed)
    cursor_col: usize,
    /// Scroll offset (first visible line)
    scroll_offset: usize,
    /// Last known viewport height — Cell allows update via &self from render path
    visible_height: Cell<usize>,
    /// Track if buffer has unsaved changes
    modified: bool,
    /// Selection state (anchor/head model)
    /// Upgraded to SelectionSet for multi-cursor support
    selections: SelectionSet,
}

impl Clone for TextBuffer {
    fn clone(&self) -> Self {
        Self {
            lines: self.lines.clone(),
            cursor_row: self.cursor_row,
            cursor_col: self.cursor_col,
            scroll_offset: self.scroll_offset,
            visible_height: Cell::new(self.visible_height.get()),
            modified: self.modified,
            selections: self.selections.clone(),
        }
    }
}

impl Default for TextBuffer {
    fn default() -> Self {
        Self {
            lines: vec![String::new()],
            cursor_row: 0,
            cursor_col: 0,
            scroll_offset: 0,
            visible_height: Cell::new(20),
            modified: false,
            selections: SelectionSet::origin(),
        }
    }
}

impl TextBuffer {
    /// Create a new text buffer from content
    pub fn from_content(content: &str) -> Self {
        let lines: Vec<String> = if content.is_empty() {
            vec![String::new()]
        } else {
            content.lines().map(String::from).collect()
        };
        Self {
            lines,
            cursor_row: 0,
            cursor_col: 0,
            scroll_offset: 0,
            visible_height: Cell::new(20),
            modified: false, // Fresh content is not modified
            selections: SelectionSet::origin(),
        }
    }

    /// Check if buffer has unsaved changes
    pub fn is_modified(&self) -> bool {
        self.modified
    }

    /// Mark buffer as modified (called after any edit)
    pub fn mark_modified(&mut self) {
        self.modified = true;
    }

    /// Clear modified flag (called after save)
    pub fn clear_modified(&mut self) {
        self.modified = false;
    }

    /// Update the known viewport height (called by renderer each frame via &self)
    pub fn set_visible_height(&self, height: usize) {
        if height > 0 {
            self.visible_height.set(height);
        }
    }

    /// Get all lines as a joined string
    pub fn content(&self) -> String {
        self.lines.join("\n")
    }

    /// Get lines slice
    pub fn lines(&self) -> &[String] {
        &self.lines
    }

    /// Get cursor position (row, col) - 0-indexed
    pub fn cursor(&self) -> (usize, usize) {
        (self.cursor_row, self.cursor_col)
    }

    /// Move cursor up
    pub fn cursor_up(&mut self) {
        if self.cursor_row > 0 {
            self.cursor_row -= 1;
            self.clamp_cursor_col();
            self.adjust_scroll();
        }
    }

    /// Move cursor down
    pub fn cursor_down(&mut self) {
        if self.cursor_row < self.lines.len().saturating_sub(1) {
            self.cursor_row += 1;
            self.clamp_cursor_col();
            self.adjust_scroll();
        }
    }

    /// Move cursor left
    pub fn cursor_left(&mut self) {
        if self.cursor_col > 0 {
            self.cursor_col -= 1;
        } else if self.cursor_row > 0 {
            self.cursor_row -= 1;
            self.cursor_col = self.current_line_len();
            self.adjust_scroll();
        }
    }

    /// Move cursor right
    pub fn cursor_right(&mut self) {
        let line_len = self.current_line_len();
        if self.cursor_col < line_len {
            self.cursor_col += 1;
        } else if self.cursor_row < self.lines.len().saturating_sub(1) {
            self.cursor_row += 1;
            self.cursor_col = 0;
            self.adjust_scroll();
        }
    }

    /// Insert a character at cursor
    pub fn insert_char(&mut self, c: char) {
        if let Some(line) = self.lines.get_mut(self.cursor_row) {
            let col = self.cursor_col.min(line.len());
            line.insert(col, c);
            self.cursor_col = col + 1;
            self.modified = true; // track modification
        }
    }

    /// Insert a newline at cursor
    pub fn insert_newline(&mut self) {
        if let Some(line) = self.lines.get_mut(self.cursor_row) {
            let col = self.cursor_col.min(line.len());
            let rest = line[col..].to_string();
            line.truncate(col);
            self.lines.insert(self.cursor_row + 1, rest);
            self.cursor_row += 1;
            self.cursor_col = 0;
            self.modified = true; // track modification
            self.adjust_scroll();
        }
    }

    /// Delete character before cursor (backspace)
    pub fn backspace(&mut self) {
        if self.cursor_col > 0 {
            if let Some(line) = self.lines.get_mut(self.cursor_row) {
                let col = self.cursor_col.min(line.len());
                if col > 0 {
                    line.remove(col - 1);
                    self.cursor_col = col - 1;
                    self.modified = true; // track modification
                }
            }
        } else if self.cursor_row > 0 {
            // Merge with previous line
            let current_line = self.lines.remove(self.cursor_row);
            self.cursor_row -= 1;
            self.cursor_col = self.lines[self.cursor_row].len();
            self.lines[self.cursor_row].push_str(&current_line);
            self.modified = true; // track modification
            self.adjust_scroll();
        }
    }

    /// Delete character at cursor
    pub fn delete(&mut self) {
        if let Some(line) = self.lines.get_mut(self.cursor_row) {
            let col = self.cursor_col.min(line.len());
            if col < line.len() {
                line.remove(col);
                self.modified = true; // track modification
            } else if self.cursor_row < self.lines.len() - 1 {
                // Merge with next line
                let next_line = self.lines.remove(self.cursor_row + 1);
                self.lines[self.cursor_row].push_str(&next_line);
                self.modified = true; // track modification
            }
        }
    }

    /// Get current line length
    fn current_line_len(&self) -> usize {
        self.lines
            .get(self.cursor_row)
            .map(|l| l.len())
            .unwrap_or(0)
    }

    /// Clamp cursor column to line length
    fn clamp_cursor_col(&mut self) {
        let line_len = self.current_line_len();
        self.cursor_col = self.cursor_col.min(line_len);
    }

    /// Adjust scroll to keep cursor visible (both up and down)
    fn adjust_scroll(&mut self) {
        let height = self.visible_height.get();
        // Scroll up: cursor above viewport
        if self.cursor_row < self.scroll_offset {
            self.scroll_offset = self.cursor_row;
        }
        // Scroll down: cursor below viewport
        if height > 0 && self.cursor_row >= self.scroll_offset + height {
            self.scroll_offset = self.cursor_row - height + 1;
        }
    }

    /// Get scroll offset
    pub fn scroll_offset(&self) -> usize {
        self.scroll_offset
    }

    /// Get current selection (primary selection from SelectionSet)
    pub fn selection(&self) -> &Selection {
        self.selections.primary()
    }

    /// Get mutable selection (primary selection from SelectionSet)
    pub fn selection_mut(&mut self) -> &mut Selection {
        self.selections.primary_mut()
    }

    /// Get all selections (for multi-cursor support)
    pub fn selections(&self) -> &SelectionSet {
        &self.selections
    }

    /// Get mutable selections (for multi-cursor support)
    pub fn selections_mut(&mut self) -> &mut SelectionSet {
        &mut self.selections
    }

    /// Check if there is an active selection (any selection is active)
    pub fn has_selection(&self) -> bool {
        self.selections.has_active_selection()
    }

    /// Check if multi-cursor mode is active
    pub fn is_multi_cursor(&self) -> bool {
        self.selections.is_multi_cursor()
    }

    /// Get number of cursors
    pub fn cursor_count(&self) -> usize {
        self.selections.cursor_count()
    }

    /// Clear all selections (collapse to primary cursor position)
    pub fn clear_selection(&mut self) {
        let pos = Position::new(self.cursor_row, self.cursor_col);
        self.selections.move_all_to(pos);
    }

    /// Clear additional cursors, keep primary
    pub fn clear_additional_cursors(&mut self) {
        self.selections.clear_additional();
    }

    /// Start a new selection at current cursor position
    pub fn start_selection(&mut self) {
        // Reset to single cursor mode with fresh selection
        let pos = Position::new(self.cursor_row, self.cursor_col);
        self.selections = SelectionSet::new(pos);
    }

    /// Extend selection to current cursor position
    pub fn extend_selection(&mut self) {
        let pos = Position::new(self.cursor_row, self.cursor_col);
        self.selections.primary_mut().extend_to(pos);
    }

    /// Sync selection head with cursor (call after cursor movement when extending)
    pub fn sync_selection_to_cursor(&mut self) {
        let pos = Position::new(self.cursor_row, self.cursor_col);
        self.selections.primary_mut().extend_to(pos);
    }

    /// Add a cursor at position (multi-cursor support)
    pub fn add_cursor(&mut self, line: usize, col: usize) {
        let pos = Position::new(line, col);
        self.selections.add_selection(pos);
    }

    /// Get the selected text as a string (primary selection)
    pub fn get_selected_text(&self) -> Option<String> {
        if !self.has_selection() {
            return None;
        }

        let selection = self.selections.primary();
        let start = selection.start();
        let end = selection.end();

        if start.line == end.line {
            // Single line selection
            let line = self.lines.get(start.line)?;
            let start_col = start.col.min(line.len());
            let end_col = end.col.min(line.len());
            Some(line[start_col..end_col].to_string())
        } else {
            // Multi-line selection
            let mut result = String::new();

            // First line (from start_col to end)
            if let Some(first_line) = self.lines.get(start.line) {
                let start_col = start.col.min(first_line.len());
                result.push_str(&first_line[start_col..]);
            }

            // Middle lines (full lines)
            for line_idx in (start.line + 1)..end.line {
                result.push('\n');
                if let Some(line) = self.lines.get(line_idx) {
                    result.push_str(line);
                }
            }

            // Last line (from start to end_col)
            if let Some(last_line) = self.lines.get(end.line) {
                result.push('\n');
                let end_col = end.col.min(last_line.len());
                result.push_str(&last_line[..end_col]);
            }

            Some(result)
        }
    }

    /// Delete selected text and collapse selection (primary selection)
    pub fn delete_selection(&mut self) -> bool {
        if !self.has_selection() {
            return false;
        }

        let selection = self.selections.primary();
        let start = selection.start();
        let end = selection.end();

        if start.line == end.line {
            // Single line deletion
            if let Some(line) = self.lines.get_mut(start.line) {
                let start_col = start.col.min(line.len());
                let end_col = end.col.min(line.len());
                line.replace_range(start_col..end_col, "");
            }
        } else {
            // Multi-line deletion
            // Keep start of first line + end of last line
            let first_part = self
                .lines
                .get(start.line)
                .map(|l| l[..start.col.min(l.len())].to_string())
                .unwrap_or_default();

            let last_part = self
                .lines
                .get(end.line)
                .map(|l| l[end.col.min(l.len())..].to_string())
                .unwrap_or_default();

            // Remove lines from start.line to end.line (inclusive)
            self.lines.drain(start.line..=end.line);

            // Insert merged line
            let merged = format!("{}{}", first_part, last_part);
            self.lines.insert(start.line, merged);
        }

        // Move cursor to selection start
        self.cursor_row = start.line;
        self.cursor_col = start.col;
        self.clear_selection();
        self.modified = true;

        true
    }

    /// Select all text
    pub fn select_all(&mut self) {
        let start = Position::new(0, 0);
        let last_line = self.lines.len().saturating_sub(1);
        let last_col = self.lines.get(last_line).map(|l| l.len()).unwrap_or(0);
        let end = Position::new(last_line, last_col);
        self.selections.primary_mut().select_range(start, end);
        self.cursor_row = last_line;
        self.cursor_col = last_col;
    }

    /// Select next occurrence of current selection (Ctrl+D - VS Code style)
    /// Returns true if a new occurrence was found and selected
    pub fn select_next_occurrence(&mut self) -> bool {
        // Get the current selected text
        let selected = match self.get_selected_text() {
            Some(text) if !text.is_empty() => text,
            _ => {
                // No selection - select word under cursor
                return self.select_word_under_cursor();
            }
        };

        // Get current primary selection end position
        let primary = self.selections.primary();
        let search_start_line = primary.end().line;
        let search_start_col = primary.end().col;

        // Search for next occurrence after the current selection
        for line_idx in search_start_line..self.lines.len() {
            if let Some(line) = self.lines.get(line_idx) {
                let start_col = if line_idx == search_start_line {
                    search_start_col
                } else {
                    0
                };

                // Search in this line from start_col
                if start_col < line.len() {
                    if let Some(found_col) = line[start_col..].find(&selected) {
                        let abs_col = start_col + found_col;
                        // Add new cursor at this occurrence
                        let new_anchor = Position::new(line_idx, abs_col);
                        let new_head = Position::new(line_idx, abs_col + selected.len());

                        // Add a new selection spanning the occurrence
                        let mut new_selection = Selection::new(new_anchor);
                        new_selection.extend_to(new_head);
                        self.selections.add_selection_spanning(new_anchor, new_head);

                        // Move cursor to the new selection
                        self.cursor_row = line_idx;
                        self.cursor_col = abs_col + selected.len();

                        return true;
                    }
                }
            }
        }

        // Wrap around and search from beginning
        for line_idx in 0..=search_start_line {
            if let Some(line) = self.lines.get(line_idx) {
                let end_col = if line_idx == search_start_line {
                    search_start_col.saturating_sub(selected.len())
                } else {
                    line.len()
                };

                if let Some(found_col) = line[..end_col].find(&selected) {
                    let new_anchor = Position::new(line_idx, found_col);
                    let new_head = Position::new(line_idx, found_col + selected.len());
                    self.selections.add_selection_spanning(new_anchor, new_head);
                    self.cursor_row = line_idx;
                    self.cursor_col = found_col + selected.len();
                    return true;
                }
            }
        }

        false
    }

    /// Select the word under cursor (helper for select_next_occurrence)
    fn select_word_under_cursor(&mut self) -> bool {
        if let Some(line) = self.lines.get(self.cursor_row) {
            if line.is_empty() || self.cursor_col >= line.len() {
                return false;
            }

            let chars: Vec<char> = line.chars().collect();
            let col = self.cursor_col.min(chars.len().saturating_sub(1));

            // Check if current char is a word char
            if !chars
                .get(col)
                .map(|c| c.is_alphanumeric() || *c == '_')
                .unwrap_or(false)
            {
                return false;
            }

            // Find word start
            let mut word_start = col;
            while word_start > 0 {
                if !chars
                    .get(word_start - 1)
                    .map(|c| c.is_alphanumeric() || *c == '_')
                    .unwrap_or(false)
                {
                    break;
                }
                word_start -= 1;
            }

            // Find word end
            let mut word_end = col;
            while word_end < chars.len() {
                if !chars
                    .get(word_end)
                    .map(|c| c.is_alphanumeric() || *c == '_')
                    .unwrap_or(false)
                {
                    break;
                }
                word_end += 1;
            }

            // Select the word
            let start = Position::new(self.cursor_row, word_start);
            let end = Position::new(self.cursor_row, word_end);
            self.selections.primary_mut().select_range(start, end);
            self.cursor_col = word_end;

            true
        } else {
            false
        }
    }

    /// Get cursor position as linear index (for EditHistory)
    pub fn cursor_position(&self) -> usize {
        let mut pos = 0;
        for (i, line) in self.lines.iter().enumerate() {
            if i == self.cursor_row {
                return pos + self.cursor_col.min(line.len());
            }
            pos += line.len() + 1; // +1 for newline
        }
        pos
    }

    /// Set content from string (clears buffer and reloads)
    pub fn set_content(&mut self, content: &str) {
        *self = Self::from_content(content);
    }

    /// Set cursor to linear position
    pub fn set_cursor_position(&mut self, pos: usize) {
        let mut remaining = pos;
        for (row, line) in self.lines.iter().enumerate() {
            let line_len = line.len() + 1; // +1 for newline
            if remaining < line_len || row == self.lines.len() - 1 {
                self.cursor_row = row;
                self.cursor_col = remaining.min(line.len());
                self.adjust_scroll();
                return;
            }
            remaining -= line_len;
        }
        // Fallback: end of document
        self.cursor_row = self.lines.len().saturating_sub(1);
        self.cursor_col = self.lines.last().map(|l| l.len()).unwrap_or(0);
        self.adjust_scroll();
    }
}

/// Studio view state
/// Debounce delay for validation (ms)
const VALIDATION_DEBOUNCE_MS: u64 = 300;

pub struct YamlEditorPanel {
    /// File path being edited
    pub path: Option<PathBuf>,
    /// Text buffer
    pub buffer: TextBuffer,
    /// Editor mode
    pub mode: EditorMode,
    /// Validation result
    pub validation: ValidationResult,
    /// Whether file has unsaved changes
    pub modified: bool,
    /// DAG expanded mode (toggle with 'E')
    pub dag_expanded: bool,
    /// DAG scroll offset for vertical scrolling
    pub dag_scroll: u16,
    /// Whether validation is pending (debounced)
    validation_pending: bool,
    /// Time of last edit (for debouncing)
    last_edit_time: Option<Instant>,
    /// Cached parsed workflow (RefCell for interior mutability in render)
    cached_workflow: RefCell<Option<crate::ast::Workflow>>,
    /// Content hash when workflow was cached (invalidation)
    cached_content_hash: Cell<u64>,
    /// Animation frame counter (0-255, wraps)
    pub frame: u8,
    /// Matrix rain background opacity (0.0 = invisible, 1.0 = full)
    pub rain_opacity: f32,
    /// Whether rain effect is actively fading out
    pub rain_fading: bool,
    /// Whether matrix effect is enabled
    pub matrix_effect_enabled: bool,
    /// Edit history for undo/redo support (Ctrl+Z/Ctrl+Y)
    edit_history: EditHistory,
    /// Diagnostics engine for real-time error display (gutter + underline)
    diagnostics: DiagnosticsEngine,
    /// System clipboard for copy/paste
    clipboard: Option<arboard::Clipboard>,
    /// Git status for line-level change indicators
    git_status: Option<GitStatus>,
}

impl YamlEditorPanel {
    pub fn new() -> Self {
        Self {
            path: None,
            buffer: TextBuffer::default(),
            mode: EditorMode::Normal,
            validation: ValidationResult::default(),
            modified: false,
            dag_expanded: false,
            dag_scroll: 0,
            validation_pending: false,
            last_edit_time: None,
            cached_workflow: RefCell::new(None),
            cached_content_hash: Cell::new(0),
            // Matrix Rain starts visible and fades
            frame: 0,
            rain_opacity: 1.0,
            rain_fading: true,
            matrix_effect_enabled: true,
            // Edit History (Undo/Redo)
            edit_history: EditHistory::new(100),
            // Real-time Diagnostics
            diagnostics: DiagnosticsEngine::new(),
            // Clipboard (graceful fallback if unavailable)
            clipboard: arboard::Clipboard::new().ok(),
            // Git gutter (initialized on file load)
            git_status: None,
        }
    }

    /// Tick animation frame (called from main loop)
    pub fn tick(&mut self) {
        self.frame = self.frame.wrapping_add(1);
        // Tick matrix rain fade effect
        if self.rain_fading && self.rain_opacity > 0.0 {
            self.rain_opacity = (self.rain_opacity - 0.04).max(0.0); // Smooth fade ~2s
        }
    }

    /// Mark content as edited - validation will run after debounce delay
    fn mark_edited(&mut self) {
        self.modified = true;
        self.validation_pending = true;
        self.last_edit_time = Some(Instant::now());
        // Push state to edit history for undo/redo
        let content = self.buffer.content();
        let cursor = self.buffer.cursor_position();
        self.edit_history.push(&content, cursor);
    }

    /// Simple hash for cache invalidation
    fn content_hash(content: &str) -> u64 {
        use std::hash::{Hash, Hasher};
        let mut hasher = std::collections::hash_map::DefaultHasher::new();
        content.hash(&mut hasher);
        hasher.finish()
    }

    /// Check if debounced validation should run (call in render/tick)
    pub fn maybe_validate(&mut self) {
        if !self.validation_pending {
            return;
        }
        if let Some(last_edit) = self.last_edit_time {
            if last_edit.elapsed() >= Duration::from_millis(VALIDATION_DEBOUNCE_MS) {
                self.validate();
                self.validation_pending = false;
            }
        }
    }

    /// Toggle DAG expanded/minimal mode
    pub fn toggle_dag_mode(&mut self) {
        self.dag_expanded = !self.dag_expanded;
    }

    /// Load a file into the editor
    pub fn load_file(&mut self, path: PathBuf) -> Result<(), std::io::Error> {
        let content = std::fs::read_to_string(&path)?;
        self.buffer = TextBuffer::from_content(&content);
        self.path = Some(path.clone());
        self.modified = false;
        self.validate();
        // Initialize edit history with loaded content
        self.edit_history.init(&content, 0);
        // Initialize git status for gutter display
        self.git_status = GitStatus::open(&path);
        Ok(())
    }

    /// Save the file using atomic write (temp+rename) for data integrity
    pub fn save_file(&mut self) -> Result<(), std::io::Error> {
        if let Some(path) = &self.path {
            let content = self.buffer.content();
            atomic_write(path, content.as_bytes())?;
            self.modified = false;
        }
        Ok(())
    }

    /// Undo the last edit (Ctrl+Z)
    fn undo(&mut self) {
        if let Some((text, cursor)) = self.edit_history.undo() {
            self.buffer.set_content(&text);
            self.buffer.set_cursor_position(cursor);
            self.modified = true;
            self.validation_pending = true;
            self.last_edit_time = Some(Instant::now());
        }
    }

    /// Redo the last undone edit (Ctrl+Y or Ctrl+Shift+Z)
    fn redo(&mut self) {
        if let Some((text, cursor)) = self.edit_history.redo() {
            self.buffer.set_content(&text);
            self.buffer.set_cursor_position(cursor);
            self.modified = true;
            self.validation_pending = true;
            self.last_edit_time = Some(Instant::now());
        }
    }

    /// Validate the YAML content
    pub fn validate(&mut self) {
        let content = self.buffer.content();
        self.validation.errors.clear();
        self.validation.warnings.clear();

        // Phase 1: Check YAML syntax validity
        // Note: Using serde_json::Value as target since serde-saphyr doesn't export Value type
        match crate::serde_yaml::from_str::<serde_json::Value>(&content) {
            Ok(_) => {
                self.validation.yaml_valid = true;
            }
            Err(e) => {
                self.validation.yaml_valid = false;
                self.validation.schema_valid = false;
                self.validation.errors = vec![format!("YAML syntax error: {}", e)];
                return; // Can't validate schema if YAML is invalid
            }
        }

        // Phase 2: Validate against Nika workflow schema
        match WorkflowSchemaValidator::new() {
            Ok(validator) => match validator.validate_yaml(&content) {
                Ok(()) => {
                    self.validation.schema_valid = true;
                }
                Err(NikaError::SchemaValidationFailed { errors }) => {
                    self.validation.schema_valid = false;
                    self.validation.errors = errors
                        .into_iter()
                        .map(|e| format!("{}: {}", e.path, e.message))
                        .collect();
                }
                Err(e) => {
                    // Other validation error (e.g., YAML parse error from validator)
                    self.validation.schema_valid = false;
                    self.validation.errors = vec![e.to_string()];
                }
            },
            Err(e) => {
                // Schema validator creation failed
                self.validation.schema_valid = false;
                self.validation.warnings = vec![format!("Schema validator unavailable: {}", e)];
            }
        }

        // Phase 3: Two-Phase IR diagnostics
        // Provides precise span locations for gutter icons and underlines
        self.diagnostics.analyze(&content);
    }

    /// Get current line number (1-indexed)
    #[allow(dead_code)] // Will be used for status line display
    pub fn current_line(&self) -> usize {
        self.buffer.cursor().0 + 1
    }

    /// Get current column (1-indexed)
    #[allow(dead_code)] // Will be used for status line display
    pub fn current_col(&self) -> usize {
        self.buffer.cursor().1 + 1
    }

    /// Get diagnostics engine reference (for gutter and underline rendering)
    pub fn diagnostics(&self) -> &DiagnosticsEngine {
        &self.diagnostics
    }

    /// Check if line has any diagnostics (for gutter icon)
    pub fn line_has_diagnostic(&self, line: usize) -> bool {
        self.diagnostics.has_diagnostics_on_line(line)
    }

    /// Get error count from diagnostics
    pub fn diagnostic_error_count(&self) -> usize {
        self.diagnostics.error_count()
    }
}

impl Default for YamlEditorPanel {
    fn default() -> Self {
        Self::new()
    }
}

impl View for YamlEditorPanel {
    fn render(&mut self, frame: &mut Frame, area: Rect, _state: &TuiState, theme: &Theme) {
        // Matrix Rain background effect (full screen, renders FIRST)
        if self.matrix_effect_enabled && self.rain_opacity > 0.05 {
            let rain_area = Rect {
                x: area.x + 1,
                y: area.y + 1,
                width: area.width.saturating_sub(2),
                height: area.height.saturating_sub(2),
            };
            MatrixRain::new()
                .frame(self.frame)
                .density(0.08) // Very subtle, smooth
                .opacity(self.rain_opacity)
                .with_mascots(true)
                .render(rain_area, frame.buffer_mut());
        }

        // Layout: Editor (70%) | Structure (30%) above, Validation bar below
        let chunks = Layout::default()
            .direction(Direction::Vertical)
            .constraints([Constraint::Min(10), Constraint::Length(3)])
            .split(area);

        let main_chunks = Layout::default()
            .direction(Direction::Horizontal)
            .constraints([Constraint::Percentage(70), Constraint::Percentage(30)])
            .split(chunks[0]);

        // Editor panel
        self.render_editor(frame, main_chunks[0], theme);

        // Structure panel
        self.render_structure(frame, main_chunks[1], theme);

        // Validation bar
        self.render_validation(frame, chunks[1], theme);
    }

    fn handle_key(&mut self, key: KeyEvent, _state: &mut TuiState) -> ViewAction {
        match self.mode {
            EditorMode::Normal => self.handle_normal_mode(key),
            EditorMode::Insert => self.handle_insert_mode(key),
        }
    }

    fn status_line(&self, _state: &TuiState) -> String {
        let mode = match self.mode {
            EditorMode::Normal => "NORMAL",
            EditorMode::Insert => "INSERT",
        };
        let modified = if self.modified { "" } else { "" };

        // Undo/Redo indicator (⤺N ⤻M format)
        let undo_depth = self.edit_history.undo_depth();
        let redo_depth = self.edit_history.redo_depth();
        let undo_redo = if undo_depth > 0 || redo_depth > 0 {
            format!(" | ⤺{}{}", undo_depth, redo_depth)
        } else {
            String::new()
        };

        format!(
            "{} | Ln {}, Col {}{}{}",
            mode,
            self.current_line(),
            self.current_col(),
            modified,
            undo_redo
        )
    }
}

impl YamlEditorPanel {
    fn handle_normal_mode(&mut self, key: KeyEvent) -> ViewAction {
        match key.code {
            KeyCode::Char('q') => ViewAction::SwitchView(TuiView::Studio),
            // Ctrl+S to save file (must be before plain 's')
            KeyCode::Char('s') if key.modifiers.contains(KeyModifiers::CONTROL) => {
                if let Err(e) = self.save_file() {
                    ViewAction::Error(format!("Save failed: {}", e))
                } else {
                    ViewAction::None
                }
            }
            // Ctrl+Z for undo
            KeyCode::Char('z') if key.modifiers.contains(KeyModifiers::CONTROL) => {
                self.undo();
                ViewAction::None
            }
            // Ctrl+Y or Ctrl+Shift+Z for redo
            KeyCode::Char('y') if key.modifiers.contains(KeyModifiers::CONTROL) => {
                self.redo();
                ViewAction::None
            }
            KeyCode::Char('Z')
                if key
                    .modifiers
                    .contains(KeyModifiers::CONTROL | KeyModifiers::SHIFT) =>
            {
                self.redo();
                ViewAction::None
            }
            // 's' opens Settings view
            KeyCode::Char('s') => ViewAction::OpenControl,
            // Shift+T or Ctrl+t toggles theme
            KeyCode::Char('T') => ViewAction::ToggleTheme,
            KeyCode::Char('t') if key.modifiers.contains(KeyModifiers::CONTROL) => {
                ViewAction::ToggleTheme
            }
            KeyCode::Char('i') => {
                self.mode = EditorMode::Insert;
                ViewAction::None
            }
            KeyCode::Char('c') => ViewAction::SwitchView(TuiView::Command),
            // Toggle DAG expanded/minimal mode
            KeyCode::Char('e') | KeyCode::Char('E') => {
                self.toggle_dag_mode();
                ViewAction::None
            }
            KeyCode::F(5) => {
                if let Some(path) = &self.path {
                    ViewAction::RunWorkflow(path.clone())
                } else {
                    ViewAction::Error("No file loaded".to_string())
                }
            }
            // View switching: number keys
            // 1=Studio (current), 2=Command, 3=Control
            KeyCode::Char('2') => ViewAction::SwitchView(TuiView::Command),
            KeyCode::Char('3') => ViewAction::SwitchView(TuiView::Control),
            KeyCode::Up | KeyCode::Char('k') => {
                self.buffer.cursor_up();
                ViewAction::None
            }
            KeyCode::Down | KeyCode::Char('j') => {
                self.buffer.cursor_down();
                ViewAction::None
            }
            KeyCode::Left => {
                self.buffer.cursor_left();
                ViewAction::None
            }
            KeyCode::Right => {
                self.buffer.cursor_right();
                ViewAction::None
            }
            _ => ViewAction::None,
        }
    }

    fn handle_insert_mode(&mut self, key: KeyEvent) -> ViewAction {
        // Handle Ctrl+Z and Ctrl+Y before other key handlers
        if key.modifiers.contains(KeyModifiers::CONTROL) {
            match key.code {
                KeyCode::Char('z') => {
                    self.undo();
                    return ViewAction::None;
                }
                KeyCode::Char('y') => {
                    self.redo();
                    return ViewAction::None;
                }
                // Ctrl+A for select all
                KeyCode::Char('a') => {
                    self.buffer.select_all();
                    return ViewAction::None;
                }
                // Ctrl+C for copy
                KeyCode::Char('c') => {
                    if let Some(text) = self.buffer.get_selected_text() {
                        if let Some(ref mut clipboard) = self.clipboard {
                            let _ = clipboard.set_text(text);
                        }
                    }
                    return ViewAction::None;
                }
                // Ctrl+X for cut
                KeyCode::Char('x') => {
                    if let Some(text) = self.buffer.get_selected_text() {
                        if let Some(ref mut clipboard) = self.clipboard {
                            let _ = clipboard.set_text(text);
                        }
                        self.buffer.delete_selection();
                        self.mark_edited();
                    }
                    return ViewAction::None;
                }
                // Ctrl+V for paste
                KeyCode::Char('v') => {
                    if let Some(ref mut clipboard) = self.clipboard {
                        if let Ok(text) = clipboard.get_text() {
                            // Delete selection first if active
                            if self.buffer.has_selection() {
                                self.buffer.delete_selection();
                            }
                            // Insert pasted text character by character
                            for c in text.chars() {
                                if c == '\n' {
                                    self.buffer.insert_newline();
                                } else if c != '\r' {
                                    self.buffer.insert_char(c);
                                }
                            }
                            self.mark_edited();
                        }
                    }
                    return ViewAction::None;
                }
                // Ctrl+D for select next occurrence (multi-cursor)
                KeyCode::Char('d') => {
                    self.buffer.select_next_occurrence();
                    return ViewAction::None;
                }
                // Escape clears additional cursors first
                KeyCode::Char('g') if key.modifiers.contains(KeyModifiers::CONTROL) => {
                    // Ctrl+G: Clear additional cursors (back to single cursor mode)
                    if self.buffer.is_multi_cursor() {
                        self.buffer.clear_additional_cursors();
                        return ViewAction::None;
                    }
                }
                _ => {}
            }
        }
        // Ctrl+Shift+Z for redo
        if key
            .modifiers
            .contains(KeyModifiers::CONTROL | KeyModifiers::SHIFT)
            && matches!(key.code, KeyCode::Char('Z'))
        {
            self.redo();
            return ViewAction::None;
        }

        // Shift+Arrow for selection extension
        let extending = key.modifiers.contains(KeyModifiers::SHIFT);

        match key.code {
            KeyCode::Esc => {
                // Clear selection first, then exit insert mode
                if self.buffer.has_selection() {
                    self.buffer.clear_selection();
                } else {
                    self.mode = EditorMode::Normal;
                }
                ViewAction::None
            }
            KeyCode::Up => {
                if extending && !self.buffer.has_selection() {
                    self.buffer.start_selection();
                }
                self.buffer.cursor_up();
                if extending {
                    self.buffer.sync_selection_to_cursor();
                } else {
                    self.buffer.clear_selection();
                }
                ViewAction::None
            }
            KeyCode::Down => {
                if extending && !self.buffer.has_selection() {
                    self.buffer.start_selection();
                }
                self.buffer.cursor_down();
                if extending {
                    self.buffer.sync_selection_to_cursor();
                } else {
                    self.buffer.clear_selection();
                }
                ViewAction::None
            }
            KeyCode::Left => {
                if extending && !self.buffer.has_selection() {
                    self.buffer.start_selection();
                }
                self.buffer.cursor_left();
                if extending {
                    self.buffer.sync_selection_to_cursor();
                } else {
                    self.buffer.clear_selection();
                }
                ViewAction::None
            }
            KeyCode::Right => {
                if extending && !self.buffer.has_selection() {
                    self.buffer.start_selection();
                }
                self.buffer.cursor_right();
                if extending {
                    self.buffer.sync_selection_to_cursor();
                } else {
                    self.buffer.clear_selection();
                }
                ViewAction::None
            }
            KeyCode::Enter => {
                // Delete selection before inserting newline
                if self.buffer.has_selection() {
                    self.buffer.delete_selection();
                }
                self.buffer.insert_newline();
                self.mark_edited();
                ViewAction::None
            }
            KeyCode::Backspace => {
                // Delete selection if active
                if self.buffer.has_selection() {
                    self.buffer.delete_selection();
                    self.mark_edited();
                } else {
                    self.buffer.backspace();
                    self.mark_edited();
                }
                ViewAction::None
            }
            KeyCode::Delete => {
                // Delete selection if active
                if self.buffer.has_selection() {
                    self.buffer.delete_selection();
                    self.mark_edited();
                } else {
                    self.buffer.delete();
                    self.mark_edited();
                }
                ViewAction::None
            }
            KeyCode::Char(c) => {
                // Replace selection with typed character
                if self.buffer.has_selection() {
                    self.buffer.delete_selection();
                }
                self.buffer.insert_char(c);
                self.mark_edited();
                ViewAction::None
            }
            KeyCode::Tab => {
                // Replace selection with tab spaces
                if self.buffer.has_selection() {
                    self.buffer.delete_selection();
                }
                self.buffer.insert_char(' ');
                self.buffer.insert_char(' ');
                self.mark_edited();
                ViewAction::None
            }
            _ => ViewAction::None,
        }
    }

    /// Render just the editor content (for use in StudioView 3-panel layout)
    pub(crate) fn render_editor(&mut self, frame: &mut Frame, area: Rect, theme: &Theme) {
        let mode_indicator = match self.mode {
            EditorMode::Normal => "",
            EditorMode::Insert => " [INSERT]",
        };

        // Breadcrumb with file path (VS Code-like)
        let title = if let Some(path) = &self.path {
            let filename = path
                .file_name()
                .and_then(|n| n.to_str())
                .unwrap_or("unknown");
            let icon = if filename.ends_with(".nika.yaml") {
                "🦋"
            } else {
                "📄"
            };
            let modified = if self.buffer.is_modified() {
                ""
            } else {
                ""
            };
            format!(" {} {}{}{} ", icon, filename, modified, mode_indicator)
        } else {
            format!(" EDITOR{} ", mode_indicator)
        };

        let block = Block::default()
            .borders(Borders::ALL)
            .title(title)
            .border_style(Style::default().fg(theme.border_normal));

        let inner = block.inner(area);
        frame.render_widget(block, area);

        // Split inner into content + status bar
        let content_area = Rect {
            x: inner.x,
            y: inner.y,
            width: inner.width,
            height: inner.height.saturating_sub(1), // Leave 1 row for status bar
        };
        let status_area = Rect {
            x: inner.x,
            y: inner.y + inner.height.saturating_sub(1),
            width: inner.width,
            height: 1,
        };

        // Show placeholder when no file is loaded
        if self.path.is_none() {
            let placeholder = vec![
                Line::from(""),
                Line::from(""),
                Line::from(vec![Span::styled(
                    "  No file selected",
                    Style::default().fg(theme.text_muted),
                )]),
                Line::from(""),
                Line::from(vec![Span::styled(
                    "  Press [Tab] to focus Browser",
                    Style::default().fg(theme.text_muted),
                )]),
                Line::from(vec![Span::styled(
                    "  Select a .nika.yaml file with [Enter]",
                    Style::default().fg(theme.text_muted),
                )]),
            ];
            let paragraph = Paragraph::new(placeholder);
            frame.render_widget(paragraph, content_area);
            return;
        }

        // Calculate visible height (excluding borders and status bar) and sync to buffer
        let visible_height = content_area.height as usize;
        self.buffer.set_visible_height(visible_height);

        // Build lines with line numbers, syntax highlighting, and selection
        let selection = self.buffer.selection();

        // Get git line changes for gutter display
        // We clone the changes to avoid borrow issues in the closure
        let git_line_changes: Option<HashMap<usize, LineChange>> =
            self.path.as_ref().and_then(|p| {
                self.git_status
                    .as_mut()
                    .map(|gs| gs.line_changes(p).changes_map())
            });

        let lines: Vec<Line> = self
            .buffer
            .lines()
            .iter()
            .enumerate()
            .skip(self.buffer.scroll_offset())
            .take(visible_height)
            .map(|(i, line)| {
                let line_num = i + 1;
                let is_cursor_line = i == self.buffer.cursor().0;

                let base_style = if is_cursor_line {
                    Style::default().bg(theme.highlight)
                } else {
                    Style::default()
                };

                // Git gutter indicator
                let git_gutter = git_line_changes
                    .as_ref()
                    .and_then(|changes| changes.get(&i))
                    .map(|change| {
                        let (symbol, color) = match change {
                            LineChange::Added => ("+", theme.git_added),
                            LineChange::Modified => ("~", theme.git_modified),
                            LineChange::Deleted => ("-", theme.git_deleted),
                        };
                        Span::styled(symbol, Style::default().fg(color))
                    })
                    .unwrap_or_else(|| Span::raw(" "));

                // Line number with git gutter prefix
                let mut spans = vec![
                    git_gutter,
                    Span::styled(
                        format!("{:4} ", line_num),
                        Style::default().fg(theme.text_muted),
                    ),
                ];

                // Check for selection on this line
                let selection_range = selection.line_range(i);

                if let Some((sel_start, sel_end)) = selection_range {
                    // Line has selection - split into before/selected/after
                    let sel_end_col = sel_end.unwrap_or(line.len());
                    let sel_start = sel_start.min(line.len());
                    let sel_end_col = sel_end_col.min(line.len());

                    // Before selection
                    if sel_start > 0 {
                        let before = &line[..sel_start];
                        spans.extend(YamlHighlight::highlight_line(before, base_style));
                    }

                    // Selected portion
                    if sel_start < sel_end_col {
                        let selected = &line[sel_start..sel_end_col];
                        let selection_style = Style::default().bg(theme.selection).fg(theme.text);
                        spans.push(Span::styled(selected.to_string(), selection_style));
                    }

                    // After selection (only if selection ends on this line)
                    if sel_end.is_some() && sel_end_col < line.len() {
                        let after = &line[sel_end_col..];
                        spans.extend(YamlHighlight::highlight_line(after, base_style));
                    } else if sel_end.is_none() {
                        // Selection continues to next line - no after portion
                    }
                } else {
                    // No selection on this line - normal highlighting
                    spans.extend(YamlHighlight::highlight_line(line, base_style));
                }

                Line::from(spans)
            })
            .collect();

        let paragraph = Paragraph::new(lines);
        frame.render_widget(paragraph, content_area);

        // Render ScrollIndicator with dynamic arrows
        let total_lines = self.buffer.lines().len();
        if total_lines > visible_height {
            let scrollbar_area = Rect {
                x: content_area.x + content_area.width.saturating_sub(1),
                y: content_area.y,
                width: 1,
                height: content_area.height,
            };

            let scroll_indicator = ScrollIndicator::new()
                .position(self.buffer.scroll_offset(), total_lines, visible_height)
                .thumb_style(Style::default().fg(theme.scrollbar_thumb))
                .track_style(Style::default().fg(theme.scrollbar_track))
                .show_arrows(true);

            frame.render_widget(scroll_indicator, scrollbar_area);
        }

        // Render status bar (Ln:Col | Mode | Schema)
        // Show cursor count when multi-cursor active
        let (cursor_row, cursor_col) = self.buffer.cursor();
        let mode_str = match self.mode {
            EditorMode::Normal => "NORMAL",
            EditorMode::Insert => "INSERT",
        };
        let cursor_count = self.buffer.cursor_count();
        let status_left = if cursor_count > 1 {
            format!(
                " Ln {}, Col {} ({} cursors) ",
                cursor_row + 1,
                cursor_col + 1,
                cursor_count
            )
        } else {
            format!(" Ln {}, Col {} ", cursor_row + 1, cursor_col + 1)
        };
        let status_right = format!(" {} | nika/workflow ", mode_str);

        // Calculate padding for right-alignment
        let padding = status_area.width as usize - status_left.len() - status_right.len();
        let padding_str = " ".repeat(padding.max(0));

        let status_line = Line::from(vec![
            Span::styled(status_left, Style::default().fg(theme.text_muted)),
            Span::raw(padding_str),
            Span::styled(status_right, Style::default().fg(theme.text_muted)),
        ]);

        let status_paragraph = Paragraph::new(status_line)
            .style(Style::default().bg(theme.highlight).fg(theme.text_primary));
        frame.render_widget(status_paragraph, status_area);
    }

    /// Render just the DAG structure (for use in StudioView 3-panel layout)
    pub(crate) fn render_structure(&self, frame: &mut Frame, area: Rect, theme: &Theme) {
        // Title shows mode state
        let title = if self.dag_expanded {
            " STRUCTURE [E]xpanded "
        } else {
            " STRUCTURE [E]->expand "
        };

        let block = Block::default()
            .borders(Borders::ALL)
            .title(title)
            .border_style(Style::default().fg(theme.border_normal));

        let inner = block.inner(area);
        frame.render_widget(block, area);

        // Parse YAML and render with DagAscii
        let content = self.buffer.content();
        self.render_dag_structure(frame, inner, &content, theme);
    }

    /// Render DAG structure using DagAscii widget (for use in StudioView 3-panel layout)
    ///
    /// PERF: Uses cached_workflow to avoid re-parsing YAML every frame (60 FPS).
    /// Only parses when content hash changes.
    pub(crate) fn render_dag_structure(
        &self,
        frame: &mut Frame,
        area: Rect,
        yaml: &str,
        theme: &Theme,
    ) {
        // PERF: Compute content hash to check if we need to re-parse
        let current_hash = Self::content_hash(yaml);
        let cached_hash = self.cached_content_hash.get();

        // Update cache if content changed
        if current_hash != cached_hash {
            *self.cached_workflow.borrow_mut() = parse_workflow(yaml).ok();
            self.cached_content_hash.set(current_hash);
        }

        // Use cached workflow (avoids parsing every frame)
        let cached = self.cached_workflow.borrow();
        match cached.as_ref() {
            Some(wf) => {
                if wf.tasks.is_empty() {
                    let paragraph =
                        Paragraph::new("(no tasks)").style(Style::default().fg(theme.text_muted));
                    frame.render_widget(paragraph, area);
                    return;
                }

                // Build dependency map from Flow objects
                let deps = self.extract_flow_dependencies(wf);

                // Convert tasks to NodeBoxData
                let nodes: Vec<NodeBoxData> = wf
                    .tasks
                    .iter()
                    .map(|task| {
                        let verb = self.task_verb_color(task.as_ref());
                        NodeBoxData::new(&task.id, verb).with_status(TaskStatus::Pending)
                    })
                    .collect();

                let mode = if self.dag_expanded {
                    NodeBoxMode::Expanded
                } else {
                    NodeBoxMode::Minimal
                };

                // Create and render DagAscii widget
                let widget = DagAscii::new(&nodes)
                    .with_dependencies(deps)
                    .mode(mode)
                    .scroll(0, self.dag_scroll);

                // Render to buffer (DagAscii implements Widget)
                let buf = frame.buffer_mut();
                widget.render(area, buf);
            }
            None => {
                // YAML doesn't parse as workflow - show parse error state
                let paragraph =
                    Paragraph::new("⚠ Invalid workflow\n\nFix YAML errors to\nsee task structure")
                        .style(Style::default().fg(theme.status_failed));
                frame.render_widget(paragraph, area);
            }
        }
    }

    /// Extract dependencies from depends_on fields
    ///
    /// Returns a map: target_task_id -> [source_task_ids]
    fn extract_flow_dependencies(&self, wf: &Workflow) -> HashMap<String, Vec<String>> {
        let mut deps: HashMap<String, Vec<String>> = HashMap::new();

        for task in &wf.tasks {
            if let Some(ref task_deps) = task.depends_on {
                let entry = deps.entry(task.id.clone()).or_default();
                for source in task_deps {
                    if !entry.contains(source) {
                        entry.push(source.clone());
                    }
                }
            }
        }

        deps
    }

    /// Get VerbColor from task action
    fn task_verb_color(&self, task: &crate::ast::Task) -> VerbColor {
        use crate::ast::TaskAction;
        match &task.action {
            TaskAction::Infer { .. } => VerbColor::Infer,
            TaskAction::Exec { .. } => VerbColor::Exec,
            TaskAction::Fetch { .. } => VerbColor::Fetch,
            TaskAction::Invoke { .. } => VerbColor::Invoke,
            TaskAction::Agent { .. } => VerbColor::Agent,
        }
    }

    /// Render the validation status bar
    pub(crate) fn render_validation(&self, frame: &mut Frame, area: Rect, theme: &Theme) {
        let yaml_status = if self.validation.yaml_valid {
            Span::styled("Valid YAML", Style::default().fg(theme.status_success))
        } else {
            Span::styled("Invalid YAML", Style::default().fg(theme.status_failed))
        };

        let schema_status = if self.validation.schema_valid {
            Span::styled("Schema OK", Style::default().fg(theme.status_success))
        } else {
            Span::styled("Schema Error", Style::default().fg(theme.status_failed))
        };

        let warning_count = self.validation.warnings.len();
        let warning_status = if warning_count > 0 {
            Span::styled(
                format!("{} warning(s)", warning_count),
                Style::default().fg(theme.status_running), // Amber for warnings
            )
        } else {
            Span::styled("No warnings", Style::default().fg(theme.status_success))
        };

        // DAG Complexity Meter
        let complexity_meter = self.render_complexity_meter(theme);

        let line = Line::from(vec![
            Span::raw(" "),
            yaml_status,
            Span::raw("  |  "),
            schema_status,
            Span::raw("  |  "),
            warning_status,
            Span::raw("  |  "),
            complexity_meter,
        ]);

        let paragraph = Paragraph::new(line).block(
            Block::default()
                .borders(Borders::ALL)
                .border_style(Style::default().fg(theme.border_normal)),
        );

        frame.render_widget(paragraph, area);
    }

    /// Render DAG complexity meter
    /// Shows visual indicator of workflow complexity (tasks × flows)
    fn render_complexity_meter(&self, theme: &Theme) -> Span<'static> {
        let cached = self.cached_workflow.borrow();
        match cached.as_ref() {
            Some(wf) => {
                let task_count = wf.tasks.len();
                let flow_count = wf.flow_count();
                // Complexity score: tasks + edges (simple heuristic)
                let score = task_count + flow_count;

                // Visual meter: ▁▂▃▄▅▆▇█ (8 levels)
                let (meter, color) = match score {
                    0 => ("", theme.text_muted),
                    1..=2 => ("", theme.status_success),
                    3..=5 => ("", theme.status_success),
                    6..=8 => ("", theme.highlight),
                    9..=12 => ("", theme.highlight),
                    13..=16 => ("", theme.status_running),
                    17..=20 => ("", theme.status_running),
                    _ => ("", theme.status_failed),
                };

                Span::styled(
                    format!("DAG {} {}t {}e", meter, task_count, flow_count),
                    Style::default().fg(color),
                )
            }
            None => Span::styled("DAG ▁ --", Style::default().fg(theme.text_muted)),
        }
    }
}

/// YAML syntax highlighting colors
struct YamlHighlight;

impl YamlHighlight {
    /// Key color (Solarized blue)
    const KEY: Color = Color::Rgb(38, 139, 210);
    /// String value color (Solarized green)
    const STRING: Color = Color::Rgb(133, 153, 0);
    /// Number value color (Solarized orange)
    const NUMBER: Color = Color::Rgb(203, 75, 22);
    /// Boolean value color (Solarized violet)
    const BOOL: Color = Color::Rgb(108, 113, 196);
    /// Comment color (Solarized base01 - muted)
    const COMMENT: Color = Color::Rgb(88, 110, 117);
    /// Default text color (reserved for future use)
    #[allow(dead_code)]
    const DEFAULT: Color = Color::Reset;
    /// Nika verbs (Solarized MAGENTA) - infer, exec, fetch, invoke, agent 🦋
    const VERB: Color = Color::Rgb(211, 54, 130);
    /// Nika structure keywords (Solarized yellow)
    const NIKA_KEYWORD: Color = Color::Rgb(181, 137, 0);

    /// Highlight a single YAML line into styled spans
    fn highlight_line(line: &str, base_style: Style) -> Vec<Span<'static>> {
        let line_owned = line.to_string();
        let trimmed = line_owned.trim();

        // Empty line
        if trimmed.is_empty() {
            return vec![Span::styled(line_owned, base_style)];
        }

        // Full-line comment
        if trimmed.starts_with('#') {
            return vec![Span::styled(line_owned, base_style.fg(Self::COMMENT))];
        }

        // Try to parse as key: value
        if let Some(colon_pos) = line.find(':') {
            let (key_part, rest) = line.split_at(colon_pos);
            let key_trimmed = key_part.trim();

            // Check if it's a Nika verb (magenta) or Nika keyword (yellow)
            let key_color = if matches!(
                key_trimmed,
                "infer" | "exec" | "fetch" | "invoke" | "agent" | "decompose"
            ) {
                Self::VERB // 🦋 Nika verbs in magenta
            } else if matches!(
                key_trimmed,
                "schema"
                    | "workflow"
                    | "tasks"
                    | "flows"
                    | "mcp"
                    | "servers"
                    | "with"
                    | "params"
                    | "context"
                    | "include"
                    | "skills"
                    | "for_each"
                    | "id"
                    | "model"
                    | "provider"
            ) {
                Self::NIKA_KEYWORD // Structure keywords in yellow
            } else {
                Self::KEY
            };

            let mut spans = vec![Span::styled(
                format!("{}:", key_part),
                base_style.fg(key_color),
            )];

            // Handle the value part (skip the colon we already included)
            let value = &rest[1..]; // Skip ':'
            if !value.is_empty() {
                spans.push(Self::highlight_value(value, base_style));
            }

            return spans;
        }

        // List item (- value)
        if trimmed.starts_with('-') {
            // SAFETY: starts_with('-') guarantees find('-') will succeed
            if let Some(dash_pos) = line.find('-') {
                let before_dash = &line[..dash_pos];
                let after_dash = &line[dash_pos + 1..];

                let mut spans = vec![
                    Span::styled(before_dash.to_string(), base_style),
                    Span::styled("-".to_string(), base_style.fg(Self::KEY)),
                ];

                if !after_dash.is_empty() {
                    spans.push(Self::highlight_value(after_dash, base_style));
                }

                return spans;
            }
        }

        // Default: return as-is
        vec![Span::styled(line_owned, base_style)]
    }

    /// Highlight a YAML value
    fn highlight_value(value: &str, base_style: Style) -> Span<'static> {
        let trimmed = value.trim();

        // String in quotes
        if (trimmed.starts_with('"') && trimmed.ends_with('"'))
            || (trimmed.starts_with('\'') && trimmed.ends_with('\''))
        {
            return Span::styled(value.to_string(), base_style.fg(Self::STRING));
        }

        // Boolean
        if matches!(trimmed, "true" | "false" | "yes" | "no" | "True" | "False") {
            return Span::styled(value.to_string(), base_style.fg(Self::BOOL));
        }

        // Number (integer or float)
        if trimmed.parse::<f64>().is_ok() || trimmed.parse::<i64>().is_ok() {
            return Span::styled(value.to_string(), base_style.fg(Self::NUMBER));
        }

        // Inline comment - for now, return as-is (would need multiple spans)
        if value.contains(" #") {
            return Span::styled(value.to_string(), base_style);
        }

        // Default
        Span::styled(value.to_string(), base_style)
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Helper Functions
// ═══════════════════════════════════════════════════════════════════════════════

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

    // ═══════════════════════════════════════════════════════════════════════════
    // Schema validation tests (TDD)
    // ═══════════════════════════════════════════════════════════════════════════

    #[test]
    fn test_yaml_editor_panel_schema_validation_valid_workflow() {
        let mut view = YamlEditorPanel::new();

        // Valid Nika workflow YAML
        let valid_yaml = r#"schema: "nika/workflow@0.12"
tasks:
  - id: step1
    infer: "Hello world""#;

        view.buffer = TextBuffer::from_content(valid_yaml);
        view.validate();

        assert!(view.validation.yaml_valid, "YAML should be valid");
        assert!(view.validation.schema_valid, "Schema should be valid");
        assert!(view.validation.errors.is_empty(), "No errors expected");
    }

    #[test]
    fn test_yaml_editor_panel_schema_validation_invalid_schema() {
        let mut view = YamlEditorPanel::new();

        // Invalid Nika workflow - missing required 'tasks' field
        let invalid_yaml = r#"schema: "nika/workflow@0.12"
unknown_field: "should fail""#;

        view.buffer = TextBuffer::from_content(invalid_yaml);
        view.validate();

        assert!(view.validation.yaml_valid, "YAML syntax is valid");
        assert!(!view.validation.schema_valid, "Schema should be invalid");
        assert!(!view.validation.errors.is_empty(), "Should have errors");
    }

    #[test]
    fn test_yaml_editor_panel_schema_validation_missing_schema_field() {
        let mut view = YamlEditorPanel::new();

        // Missing required 'schema' field
        let yaml = r#"tasks:
  - id: step1
    infer: "Hello""#;

        view.buffer = TextBuffer::from_content(yaml);
        view.validate();

        assert!(view.validation.yaml_valid, "YAML syntax is valid");
        assert!(!view.validation.schema_valid, "Schema should be invalid");
        assert!(
            view.validation.errors.iter().any(|e| e.contains("schema")),
            "Error should mention 'schema'"
        );
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // TextBuffer tests
    // ═══════════════════════════════════════════════════════════════════════════

    #[test]
    fn test_text_buffer_default() {
        let buffer = TextBuffer::default();
        assert_eq!(buffer.lines().len(), 1);
        assert_eq!(buffer.lines()[0], "");
        assert_eq!(buffer.cursor(), (0, 0));
    }

    #[test]
    fn test_text_buffer_from_content() {
        let buffer = TextBuffer::from_content("line1\nline2\nline3");
        assert_eq!(buffer.lines().len(), 3);
        assert_eq!(buffer.lines()[0], "line1");
        assert_eq!(buffer.lines()[1], "line2");
        assert_eq!(buffer.lines()[2], "line3");
    }

    #[test]
    fn test_text_buffer_from_empty_content() {
        let buffer = TextBuffer::from_content("");
        assert_eq!(buffer.lines().len(), 1);
        assert_eq!(buffer.lines()[0], "");
    }

    #[test]
    fn test_text_buffer_content() {
        let buffer = TextBuffer::from_content("a\nb\nc");
        assert_eq!(buffer.content(), "a\nb\nc");
    }

    #[test]
    fn test_text_buffer_cursor_movement() {
        let mut buffer = TextBuffer::from_content("abc\ndef\nghi");

        // Move down
        buffer.cursor_down();
        assert_eq!(buffer.cursor(), (1, 0));

        // Move right
        buffer.cursor_right();
        buffer.cursor_right();
        assert_eq!(buffer.cursor(), (1, 2));

        // Move up (cursor col should clamp)
        buffer.cursor_up();
        assert_eq!(buffer.cursor(), (0, 2));

        // Move left
        buffer.cursor_left();
        assert_eq!(buffer.cursor(), (0, 1));
    }

    #[test]
    fn test_text_buffer_cursor_boundary() {
        let mut buffer = TextBuffer::from_content("ab\ncd");

        // Can't go up from first line
        buffer.cursor_up();
        assert_eq!(buffer.cursor(), (0, 0));

        // Go to last line
        buffer.cursor_down();
        buffer.cursor_down(); // Should stay at last line
        assert_eq!(buffer.cursor(), (1, 0));
    }

    #[test]
    fn test_text_buffer_insert_char() {
        let mut buffer = TextBuffer::default();
        buffer.insert_char('a');
        buffer.insert_char('b');
        buffer.insert_char('c');
        assert_eq!(buffer.lines()[0], "abc");
        assert_eq!(buffer.cursor(), (0, 3));
    }

    #[test]
    fn test_text_buffer_insert_newline() {
        let mut buffer = TextBuffer::from_content("abc");
        buffer.cursor_right();
        buffer.cursor_right(); // cursor at position 2
        buffer.insert_newline();
        assert_eq!(buffer.lines().len(), 2);
        assert_eq!(buffer.lines()[0], "ab");
        assert_eq!(buffer.lines()[1], "c");
        assert_eq!(buffer.cursor(), (1, 0));
    }

    #[test]
    fn test_text_buffer_backspace() {
        let mut buffer = TextBuffer::from_content("abc");
        buffer.cursor_right();
        buffer.cursor_right();
        buffer.cursor_right();
        buffer.backspace();
        assert_eq!(buffer.lines()[0], "ab");
        assert_eq!(buffer.cursor(), (0, 2));
    }

    #[test]
    fn test_text_buffer_backspace_merge_lines() {
        let mut buffer = TextBuffer::from_content("ab\ncd");
        buffer.cursor_down();
        buffer.backspace(); // Should merge lines
        assert_eq!(buffer.lines().len(), 1);
        assert_eq!(buffer.lines()[0], "abcd");
        assert_eq!(buffer.cursor(), (0, 2));
    }

    #[test]
    fn test_text_buffer_delete() {
        let mut buffer = TextBuffer::from_content("abc");
        buffer.cursor_right();
        buffer.delete();
        assert_eq!(buffer.lines()[0], "ac");
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // YamlEditorPanel tests
    // ═══════════════════════════════════════════════════════════════════════════

    #[test]
    fn test_yaml_editor_panel_new() {
        let view = YamlEditorPanel::new();
        assert_eq!(view.mode, EditorMode::Normal);
        assert!(!view.modified);
        assert!(view.path.is_none());
    }

    #[test]
    fn test_yaml_editor_panel_mode_switch() {
        let mut view = YamlEditorPanel::new();
        assert_eq!(view.mode, EditorMode::Normal);

        view.mode = EditorMode::Insert;
        assert_eq!(view.mode, EditorMode::Insert);
    }

    #[test]
    fn test_yaml_editor_panel_validation_valid_yaml_syntax() {
        let mut view = YamlEditorPanel::new();

        // Valid YAML syntax (but not a valid Nika workflow schema)
        view.buffer = TextBuffer::from_content("key: value");
        view.validate();
        assert!(view.validation.yaml_valid, "YAML syntax should be valid");
        // Note: schema validation will fail because this isn't a valid workflow
        // but yaml_valid should be true because the syntax is correct
        assert!(
            !view.validation.schema_valid,
            "Schema should be invalid for non-workflow YAML"
        );
    }

    #[test]
    fn test_yaml_editor_panel_validation_invalid_yaml() {
        let mut view = YamlEditorPanel::new();

        // Invalid YAML
        view.buffer = TextBuffer::from_content("key: [unclosed");
        view.validate();
        assert!(!view.validation.yaml_valid);
        assert!(!view.validation.errors.is_empty());
    }

    #[test]
    fn test_yaml_editor_panel_cursor_position() {
        let view = YamlEditorPanel::new();
        assert_eq!(view.current_line(), 1);
        assert_eq!(view.current_col(), 1);
    }

    #[test]
    fn test_yaml_editor_panel_default_validation_result() {
        let result = ValidationResult::default();
        assert!(result.yaml_valid);
        assert!(result.schema_valid);
        assert!(result.warnings.is_empty());
        assert!(result.errors.is_empty());
    }

    #[test]
    fn test_yaml_editor_panel_status_line_normal_mode() {
        let view = YamlEditorPanel::new();
        let state = TuiState::new("test.nika.yaml");
        let status = view.status_line(&state);
        assert!(status.contains("NORMAL"));
        assert!(status.contains("Ln 1"));
        assert!(status.contains("Col 1"));
    }

    #[test]
    fn test_yaml_editor_panel_status_line_insert_mode() {
        let mut view = YamlEditorPanel::new();
        view.mode = EditorMode::Insert;
        let state = TuiState::new("test.nika.yaml");
        let status = view.status_line(&state);
        assert!(status.contains("INSERT"));
    }

    #[test]
    fn test_yaml_editor_panel_status_line_modified() {
        let mut view = YamlEditorPanel::new();
        view.modified = true;
        let state = TuiState::new("test.nika.yaml");
        let status = view.status_line(&state);
        assert!(status.contains(""));
    }

    #[test]
    fn test_editor_mode_default() {
        let mode = EditorMode::default();
        assert_eq!(mode, EditorMode::Normal);
    }

    #[test]
    fn test_yaml_editor_panel_handle_normal_mode_quit() {
        let mut view = YamlEditorPanel::new();
        let mut state = TuiState::new("test.nika.yaml");
        let key = KeyEvent::from(KeyCode::Char('q'));
        let action = view.handle_key(key, &mut state);
        match action {
            ViewAction::SwitchView(TuiView::Studio) => {}
            _ => panic!("Expected SwitchView(Studio)"),
        }
    }

    #[test]
    fn test_yaml_editor_panel_handle_normal_mode_insert() {
        let mut view = YamlEditorPanel::new();
        let mut state = TuiState::new("test.nika.yaml");
        let key = KeyEvent::from(KeyCode::Char('i'));
        let _ = view.handle_key(key, &mut state);
        assert_eq!(view.mode, EditorMode::Insert);
    }

    #[test]
    fn test_yaml_editor_panel_handle_insert_mode_escape() {
        let mut view = YamlEditorPanel::new();
        view.mode = EditorMode::Insert;
        let mut state = TuiState::new("test.nika.yaml");
        let key = KeyEvent::from(KeyCode::Esc);
        let _ = view.handle_key(key, &mut state);
        assert_eq!(view.mode, EditorMode::Normal);
    }

    #[test]
    fn test_yaml_editor_panel_handle_insert_mode_typing() {
        let mut view = YamlEditorPanel::new();
        view.mode = EditorMode::Insert;
        let mut state = TuiState::new("test.nika.yaml");

        // Type some characters
        view.handle_key(KeyEvent::from(KeyCode::Char('a')), &mut state);
        view.handle_key(KeyEvent::from(KeyCode::Char('b')), &mut state);
        view.handle_key(KeyEvent::from(KeyCode::Char('c')), &mut state);

        assert_eq!(view.buffer.lines()[0], "abc");
        assert!(view.modified);
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // YAML syntax highlighting tests
    // ═══════════════════════════════════════════════════════════════════════════

    #[test]
    fn test_yaml_highlight_comment() {
        let base = Style::default();
        let spans = YamlHighlight::highlight_line("# This is a comment", base);
        assert_eq!(spans.len(), 1);
        // Should have comment color (gray)
        assert_eq!(spans[0].style.fg, Some(YamlHighlight::COMMENT));
    }

    #[test]
    fn test_yaml_highlight_key_value() {
        let base = Style::default();
        let spans = YamlHighlight::highlight_line("name: my-workflow", base);
        assert!(spans.len() >= 2, "Should have key and value spans");
        // First span should be the key with colon
        assert!(spans[0].content.contains("name:"));
    }

    #[test]
    fn test_yaml_highlight_verb() {
        let base = Style::default();
        let spans = YamlHighlight::highlight_line("    infer: \"prompt\"", base);
        // Should highlight 'infer' as a Nika verb (cyan)
        assert!(spans[0].content.contains("infer:"));
        assert_eq!(spans[0].style.fg, Some(YamlHighlight::VERB));
    }

    #[test]
    fn test_yaml_highlight_boolean() {
        let base = Style::default();
        let spans = YamlHighlight::highlight_line("enabled: true", base);
        assert!(spans.len() >= 2);
        // Value should have boolean color (purple)
        let value_span = &spans[1];
        assert_eq!(value_span.style.fg, Some(YamlHighlight::BOOL));
    }

    #[test]
    fn test_yaml_highlight_number() {
        let base = Style::default();
        let spans = YamlHighlight::highlight_line("port: 8080", base);
        assert!(spans.len() >= 2);
        // Value should have number color (orange)
        let value_span = &spans[1];
        assert_eq!(value_span.style.fg, Some(YamlHighlight::NUMBER));
    }

    #[test]
    fn test_yaml_highlight_string() {
        let base = Style::default();
        let spans = YamlHighlight::highlight_line("prompt: \"Hello world\"", base);
        assert!(spans.len() >= 2);
        // Value should have string color (green)
        let value_span = &spans[1];
        assert_eq!(value_span.style.fg, Some(YamlHighlight::STRING));
    }

    #[test]
    fn test_yaml_highlight_list_item() {
        let base = Style::default();
        let spans = YamlHighlight::highlight_line("  - item", base);
        assert!(
            spans.len() >= 2,
            "Should have indent, dash, and value spans"
        );
        // Should contain dash
        assert!(spans.iter().any(|s| s.content.contains("-")));
    }

    #[test]
    fn test_yaml_highlight_empty_line() {
        let base = Style::default();
        let spans = YamlHighlight::highlight_line("", base);
        assert_eq!(spans.len(), 1);
        assert!(spans[0].content.is_empty());
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // StudioView lifecycle tests
    // ═══════════════════════════════════════════════════════════════════════════

    #[test]
    fn test_studio_view_on_enter_initializes_tree_state() {
        use crate::tui::state::TuiState;
        use crate::tui::views::View;

        // Create a StudioView with the current directory (which should have files)
        let mut studio = StudioView::new();
        let mut state = TuiState::new("test.yaml");

        // Before on_enter: tree_state should be empty
        assert!(
            studio.tree_state.visible_nodes().is_empty(),
            "visible_nodes should be empty before on_enter"
        );
        assert!(
            studio.tree_state.selected().is_none(),
            "selection should be None before on_enter"
        );

        // Call on_enter (the lifecycle hook)
        studio.on_enter(&mut state);

        // After on_enter: tree_state should be initialized
        assert!(
            !studio.tree_state.visible_nodes().is_empty(),
            "visible_nodes should NOT be empty after on_enter"
        );
        assert!(
            studio.tree_state.selected().is_some(),
            "selection should be Some after on_enter"
        );
        assert!(
            studio.cached_tree.is_some(),
            "cached_tree should be Some after on_enter"
        );
    }

    #[test]
    fn test_studio_view_on_enter_expands_root() {
        use crate::tui::state::TuiState;
        use crate::tui::views::View;

        let mut studio = StudioView::new();
        let mut state = TuiState::new("test.yaml");

        studio.on_enter(&mut state);

        // Root directory should be expanded
        if let Some(ref tree) = studio.cached_tree {
            assert!(
                studio.tree_state.is_expanded(tree.id),
                "Root directory should be expanded after on_enter"
            );
        } else {
            panic!("cached_tree should be Some after on_enter");
        }
    }

    #[test]
    fn test_studio_view_tab_cycles_panels_in_normal_mode() {
        use crate::tui::state::TuiState;
        use crate::tui::views::View;

        let mut studio = StudioView::new();
        let mut state = TuiState::new("test.yaml");

        // Start in Browser focus, editor in Normal mode
        studio.focus = StudioFocus::Browser;
        studio.editor.mode = EditorMode::Normal;

        // Press Tab - should cycle to Editor
        let key = KeyEvent::new(KeyCode::Tab, KeyModifiers::NONE);
        studio.handle_key(key, &mut state);

        assert_eq!(
            studio.focus,
            StudioFocus::Editor,
            "Tab should cycle from Browser to Editor"
        );

        // Press Tab again - should cycle to Dag (not insert indent)
        studio.handle_key(key, &mut state);
        assert_eq!(
            studio.focus,
            StudioFocus::Dag,
            "Tab should cycle from Editor to Dag when in Normal mode"
        );
    }

    #[test]
    fn test_studio_view_tab_indents_in_insert_mode() {
        use crate::tui::state::TuiState;
        use crate::tui::views::View;

        let mut studio = StudioView::new();
        let mut state = TuiState::new("test.yaml");

        // Focus editor in Insert mode
        studio.focus = StudioFocus::Editor;
        studio.editor.mode = EditorMode::Insert;

        // Press Tab - should NOT cycle panels (handled by editor)
        let key = KeyEvent::new(KeyCode::Tab, KeyModifiers::NONE);
        studio.handle_key(key, &mut state);

        // Focus should still be Editor (Tab was sent to editor, not used for cycling)
        assert_eq!(
            studio.focus,
            StudioFocus::Editor,
            "Tab should NOT cycle panels when editor is in Insert mode"
        );
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // Selection tests
    // ═══════════════════════════════════════════════════════════════════════════

    #[test]
    fn test_text_buffer_selection_single_line() {
        let mut buffer = TextBuffer::from_content("hello world");

        // Select "world"
        buffer.start_selection();
        buffer.cursor_right();
        buffer.cursor_right();
        buffer.cursor_right();
        buffer.cursor_right();
        buffer.cursor_right();
        buffer.cursor_right(); // Now at position 6
        buffer.sync_selection_to_cursor();

        assert!(buffer.has_selection());
        assert_eq!(buffer.get_selected_text(), Some("hello ".to_string()));
    }

    #[test]
    fn test_text_buffer_selection_clear() {
        let mut buffer = TextBuffer::from_content("hello");
        buffer.start_selection();
        buffer.cursor_right();
        buffer.cursor_right();
        buffer.sync_selection_to_cursor();

        assert!(buffer.has_selection());
        buffer.clear_selection();
        assert!(!buffer.has_selection());
    }

    #[test]
    fn test_text_buffer_delete_selection() {
        let mut buffer = TextBuffer::from_content("hello world");

        // Select "hello "
        buffer.start_selection();
        for _ in 0..6 {
            buffer.cursor_right();
        }
        buffer.sync_selection_to_cursor();

        assert!(buffer.delete_selection());
        assert_eq!(buffer.content(), "world");
        assert!(!buffer.has_selection());
    }

    #[test]
    fn test_text_buffer_select_all() {
        let mut buffer = TextBuffer::from_content("line1\nline2\nline3");
        buffer.select_all();

        assert!(buffer.has_selection());
        assert_eq!(
            buffer.get_selected_text(),
            Some("line1\nline2\nline3".to_string())
        );
    }

    #[test]
    fn test_text_buffer_delete_multiline_selection() {
        let mut buffer = TextBuffer::from_content("aaa\nbbb\nccc");

        // Move to line 0, col 1 and start selection
        buffer.cursor_right();
        buffer.start_selection();

        // Move to line 2, col 1
        buffer.cursor_down();
        buffer.cursor_down();
        buffer.sync_selection_to_cursor();

        assert!(buffer.has_selection());
        buffer.delete_selection();

        // Should have "a" + "cc" = "acc"
        assert_eq!(buffer.content(), "acc");
    }

    #[test]
    fn test_multi_cursor_add_cursor() {
        let mut buffer = TextBuffer::from_content("line1\nline2\nline3");

        assert_eq!(buffer.cursor_count(), 1);
        assert!(!buffer.is_multi_cursor());

        buffer.add_cursor(1, 0);
        assert_eq!(buffer.cursor_count(), 2);
        assert!(buffer.is_multi_cursor());

        buffer.add_cursor(2, 0);
        assert_eq!(buffer.cursor_count(), 3);
    }

    #[test]
    fn test_multi_cursor_clear_additional() {
        let mut buffer = TextBuffer::from_content("hello\nworld");

        buffer.add_cursor(1, 0);
        buffer.add_cursor(1, 2);
        assert_eq!(buffer.cursor_count(), 3);

        buffer.clear_additional_cursors();
        assert_eq!(buffer.cursor_count(), 1);
        assert!(!buffer.is_multi_cursor());
    }

    #[test]
    fn test_select_word_under_cursor() {
        let mut buffer = TextBuffer::from_content("hello world test");

        // Position cursor in "world"
        for _ in 0..7 {
            buffer.cursor_right();
        }

        // First Ctrl+D should select "world"
        let selected = buffer.select_next_occurrence();
        assert!(selected);
        assert!(buffer.has_selection());
        assert_eq!(buffer.get_selected_text(), Some("world".to_string()));
    }

    #[test]
    fn test_select_next_occurrence() {
        let mut buffer = TextBuffer::from_content("foo bar foo baz foo");

        // Select first "foo" manually
        buffer.start_selection();
        for _ in 0..3 {
            buffer.cursor_right();
        }
        buffer.sync_selection_to_cursor();

        assert_eq!(buffer.get_selected_text(), Some("foo".to_string()));
        assert_eq!(buffer.cursor_count(), 1);

        // Ctrl+D should find next "foo" and add cursor
        let found = buffer.select_next_occurrence();
        assert!(found);
        assert_eq!(buffer.cursor_count(), 2);
    }

    #[test]
    fn test_select_next_occurrence_wraps() {
        let mut buffer = TextBuffer::from_content("ab cd ab");

        // Select last "ab" (position 6-8)
        for _ in 0..6 {
            buffer.cursor_right();
        }
        buffer.start_selection();
        buffer.cursor_right();
        buffer.cursor_right();
        buffer.sync_selection_to_cursor();

        assert_eq!(buffer.get_selected_text(), Some("ab".to_string()));

        // Should wrap around and find "ab" at position 0
        let found = buffer.select_next_occurrence();
        assert!(found);
        assert_eq!(buffer.cursor_count(), 2);
    }

    #[test]
    fn test_select_word_boundary() {
        let mut buffer = TextBuffer::from_content("hello_world test_case");

        // cursor at 'h'
        let selected = buffer.select_next_occurrence();
        assert!(selected);
        // Should select "hello_world" (underscore is part of word)
        assert_eq!(buffer.get_selected_text(), Some("hello_world".to_string()));
    }
}