qb 0.3.8

EMACS for Kubernetes — a powerful, extensible terminal UI for managing Kubernetes clusters
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
use {
    anyhow::{
        Context,
        Result,
    },
    jiff::Timestamp,
    k8s_openapi::{
        api::{
            apps::v1::{
                DaemonSet,
                Deployment,
                ReplicaSet,
                StatefulSet,
            },
            autoscaling::v1::HorizontalPodAutoscaler,
            batch::v1::{
                CronJob,
                Job,
            },
            core::v1::{
                ConfigMap,
                Endpoints,
                Namespace,
                Node,
                PersistentVolume,
                PersistentVolumeClaim,
                Pod,
                Secret,
                Service,
                ServiceAccount,
            },
            events::v1::Event,
            networking::v1::{
                Ingress,
                NetworkPolicy,
            },
            rbac::v1::{
                ClusterRole,
                ClusterRoleBinding,
                Role,
                RoleBinding,
            },
            storage::v1::StorageClass,
        },
        apiextensions_apiserver::pkg::apis::apiextensions::v1::CustomResourceDefinition,
        apimachinery::pkg::apis::meta::v1::Time,
    },
    kube::{
        api::{
            Api,
            ListParams,
        },
        config::{
            KubeConfigOptions,
            Kubeconfig,
        },
        Client,
    },
    serde_json::Value,
    std::time::Duration,
};

const REQUEST_TIMEOUT: Duration = Duration::from_secs(5);

fn shell_quote(value: &str) -> String {
    if value.is_empty()
        || value
            .chars()
            .any(|c| !(c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '.' | '/' | ':' | '=' | '@' | '+')))
    {
        format!("'{}'", value.replace('\'', "'\\''"))
    } else {
        value.to_string()
    }
}

fn sanitize_terminal_text(message: &str, max_chars: usize) -> String {
    let mut out = String::new();
    let mut chars = message.chars().peekable();
    while let Some(ch) = chars.next() {
        if ch == '\u{1b}' {
            if chars.peek() == Some(&'[') {
                chars.next();
                for next in chars.by_ref() {
                    if next.is_ascii_alphabetic() {
                        break;
                    }
                }
            }
            continue;
        }
        if ch.is_control() {
            out.push(' ');
        } else {
            out.push(ch);
        }
        if out.chars().count() >= max_chars {
            break;
        }
    }
    let collapsed = out.split_whitespace().collect::<Vec<_>>().join(" ");
    if collapsed.chars().count() > max_chars {
        collapsed.chars().take(max_chars).collect()
    } else {
        collapsed
    }
}

/// Why a cluster call failed. Auth is a typed kube failure, not a text search.
#[derive(Clone, Debug)]
pub enum ClusterFailure {
    /// Exec plugin failed, or the apiserver rejected the token (401 /
    /// Unauthorized).
    AuthExpired {
        detail: String,
    },
    Other {
        detail: String,
    },
}

impl ClusterFailure {
    pub fn other(detail: impl Into<String>) -> Self {
        Self::Other {
            detail: sanitize_terminal_text(&detail.into(), 240),
        }
    }

    pub fn from_anyhow(err: anyhow::Error) -> Self {
        Self::classify(auth_in_chain(&err), sanitize_terminal_text(&format!("{err:#}"), 240))
    }

    pub fn from_kube(err: &kube::Error) -> Self {
        Self::classify(kube_is_auth(err), sanitize_terminal_text(&err.to_string(), 240))
    }

    pub fn is_auth(&self) -> bool {
        matches!(self, Self::AuthExpired { .. })
    }

    pub fn detail(&self) -> &str {
        match self {
            | Self::AuthExpired { detail } | Self::Other { detail } => detail,
        }
    }

    pub fn context(self, prefix: &str) -> Self {
        let detail = sanitize_terminal_text(&format!("{prefix}: {}", self.detail()), 240);
        match self {
            | Self::AuthExpired { .. } => Self::AuthExpired { detail },
            | Self::Other { .. } => Self::Other { detail },
        }
    }

    fn classify(auth: bool, detail: String) -> Self {
        if auth {
            Self::AuthExpired { detail }
        } else {
            Self::Other { detail }
        }
    }
}

fn auth_in_chain(err: &anyhow::Error) -> bool {
    err.chain().any(|cause| {
        cause.downcast_ref::<kube::Error>().is_some_and(kube_is_auth)
            || cause.downcast_ref::<kube::client::AuthError>().is_some()
    })
}

fn kube_is_auth(err: &kube::Error) -> bool {
    match err {
        | kube::Error::Auth(_) => true,
        | kube::Error::Api(status) => is_unauthorized(status),
        | _ => false,
    }
}

fn is_unauthorized(status: &kube::core::Status) -> bool {
    status.code == 401 || status.reason == kube::core::response::reason::UNAUTHORIZED
}

fn apply_timeouts(config: &mut kube::Config) {
    config.connect_timeout = Some(REQUEST_TIMEOUT);
    config.read_timeout = Some(REQUEST_TIMEOUT);
    config.write_timeout = Some(REQUEST_TIMEOUT);
}

// ---------------------------------------------------------------------------
// Custom Resource Definition info (for dynamic CR support)
// ---------------------------------------------------------------------------

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct CrdInfo {
    pub group: String,
    pub version: String,
    pub kind: String,
    pub plural: String,
    pub scope: CrdScope,
    pub display_name: String,
    pub columns: Vec<CrdColumn>,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum CrdScope {
    Namespaced,
    Cluster,
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct CrdColumn {
    pub name: String,
    pub json_path: String,
    pub type_: String,
}

impl CrdInfo {
    pub fn api_resource(&self) -> kube::api::ApiResource {
        kube::api::ApiResource {
            group: self.group.clone(),
            version: self.version.clone(),
            api_version: if self.group.is_empty() {
                self.version.clone()
            } else {
                format!("{}/{}", self.group, self.version)
            },
            kind: self.kind.clone(),
            plural: self.plural.clone(),
        }
    }

    pub fn is_cluster_scoped(&self) -> bool {
        self.scope == CrdScope::Cluster
    }

    pub fn column_headers(&self) -> Vec<String> {
        let mut headers = vec!["NAME".to_string()];
        for col in &self.columns {
            headers.push(col.name.to_uppercase());
        }
        headers.push("AGE".to_string());
        headers
    }

    pub fn matches_gvk(&self, api_version: &str, kind: &str) -> bool {
        self.kind == kind && self.api_resource().api_version == api_version
    }
}

// ---------------------------------------------------------------------------
// Resource type definitions
// ---------------------------------------------------------------------------

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ResourceType {
    // Workloads
    Deployment,
    StatefulSet,
    DaemonSet,
    ReplicaSet,
    Pod,
    CronJob,
    Job,
    HorizontalPodAutoscaler,
    // Config
    ConfigMap,
    Secret,
    // Network
    Service,
    Ingress,
    Endpoints,
    NetworkPolicy,
    // Storage
    PersistentVolumeClaim,
    PersistentVolume,
    StorageClass,
    // RBAC
    ServiceAccount,
    Role,
    RoleBinding,
    ClusterRole,
    ClusterRoleBinding,
    // Cluster
    Node,
    Namespace,
    Event,
    CustomResourceDefinition,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Category {
    Workloads,
    Config,
    Network,
    Storage,
    Rbac,
    Cluster,
}

impl ResourceType {
    pub fn display_name(&self) -> &'static str {
        match self {
            | Self::Deployment => "Deployments",
            | Self::StatefulSet => "StatefulSets",
            | Self::DaemonSet => "DaemonSets",
            | Self::ReplicaSet => "ReplicaSets",
            | Self::Pod => "Pods",
            | Self::CronJob => "CronJobs",
            | Self::Job => "Jobs",
            | Self::HorizontalPodAutoscaler => "HPAs",
            | Self::ConfigMap => "ConfigMaps",
            | Self::Secret => "Secrets",
            | Self::Service => "Services",
            | Self::Ingress => "Ingresses",
            | Self::Endpoints => "Endpoints",
            | Self::NetworkPolicy => "NetworkPolicies",
            | Self::PersistentVolumeClaim => "PVCs",
            | Self::PersistentVolume => "PVs",
            | Self::StorageClass => "StorageClasses",
            | Self::ServiceAccount => "ServiceAccounts",
            | Self::Role => "Roles",
            | Self::RoleBinding => "RoleBindings",
            | Self::ClusterRole => "ClusterRoles",
            | Self::ClusterRoleBinding => "ClusterRoleBindings",
            | Self::Node => "Nodes",
            | Self::Namespace => "Namespaces",
            | Self::Event => "Events",
            | Self::CustomResourceDefinition => "CRDs",
        }
    }

    pub fn singular_name(&self) -> &'static str {
        match self {
            | Self::Deployment => "deployment",
            | Self::StatefulSet => "statefulset",
            | Self::DaemonSet => "daemonset",
            | Self::ReplicaSet => "replicaset",
            | Self::Pod => "pod",
            | Self::CronJob => "cronjob",
            | Self::Job => "job",
            | Self::HorizontalPodAutoscaler => "hpa",
            | Self::ConfigMap => "configmap",
            | Self::Secret => "secret",
            | Self::Service => "service",
            | Self::Ingress => "ingress",
            | Self::Endpoints => "endpoints",
            | Self::NetworkPolicy => "networkpolicy",
            | Self::PersistentVolumeClaim => "pvc",
            | Self::PersistentVolume => "pv",
            | Self::StorageClass => "storageclass",
            | Self::ServiceAccount => "serviceaccount",
            | Self::Role => "role",
            | Self::RoleBinding => "rolebinding",
            | Self::ClusterRole => "clusterrole",
            | Self::ClusterRoleBinding => "clusterrolebinding",
            | Self::Node => "node",
            | Self::Namespace => "namespace",
            | Self::Event => "event",
            | Self::CustomResourceDefinition => "crd",
        }
    }

    pub fn from_singular_name(name: &str) -> Option<Self> {
        match name {
            | "deployment" => Some(Self::Deployment),
            | "statefulset" => Some(Self::StatefulSet),
            | "daemonset" => Some(Self::DaemonSet),
            | "replicaset" => Some(Self::ReplicaSet),
            | "pod" => Some(Self::Pod),
            | "cronjob" => Some(Self::CronJob),
            | "job" => Some(Self::Job),
            | "hpa" => Some(Self::HorizontalPodAutoscaler),
            | "configmap" => Some(Self::ConfigMap),
            | "secret" => Some(Self::Secret),
            | "service" => Some(Self::Service),
            | "ingress" => Some(Self::Ingress),
            | "endpoints" => Some(Self::Endpoints),
            | "networkpolicy" => Some(Self::NetworkPolicy),
            | "pvc" => Some(Self::PersistentVolumeClaim),
            | "pv" => Some(Self::PersistentVolume),
            | "storageclass" => Some(Self::StorageClass),
            | "serviceaccount" => Some(Self::ServiceAccount),
            | "role" => Some(Self::Role),
            | "rolebinding" => Some(Self::RoleBinding),
            | "clusterrole" => Some(Self::ClusterRole),
            | "clusterrolebinding" => Some(Self::ClusterRoleBinding),
            | "node" => Some(Self::Node),
            | "namespace" => Some(Self::Namespace),
            | "event" => Some(Self::Event),
            | "crd" | "customresourcedefinition" => Some(Self::CustomResourceDefinition),
            | _ => None,
        }
    }

    pub fn column_headers(&self) -> Vec<&'static str> {
        match self {
            | Self::Deployment => vec!["NAME", "READY", "UP-TO-DATE", "AVAILABLE", "AGE"],
            | Self::StatefulSet => vec!["NAME", "READY", "AGE"],
            | Self::DaemonSet => vec!["NAME", "DESIRED", "CURRENT", "READY", "UP-TO-DATE", "AGE"],
            | Self::ReplicaSet => vec!["NAME", "DESIRED", "CURRENT", "READY", "AGE"],
            | Self::Pod => vec!["NAME", "READY", "STATUS", "RESTARTS", "AGE"],
            | Self::CronJob => vec!["NAME", "SCHEDULE", "SUSPEND", "ACTIVE", "AGE"],
            | Self::Job => vec!["NAME", "COMPLETIONS", "DURATION", "AGE"],
            | Self::HorizontalPodAutoscaler => vec!["NAME", "REFERENCE", "MIN", "MAX", "REPLICAS", "AGE"],
            | Self::ConfigMap => vec!["NAME", "DATA", "AGE"],
            | Self::Secret => vec!["NAME", "TYPE", "DATA", "AGE"],
            | Self::Service => vec!["NAME", "TYPE", "CLUSTER-IP", "PORT(S)", "AGE"],
            | Self::Ingress => vec!["NAME", "CLASS", "HOSTS", "ADDRESS", "AGE"],
            | Self::Endpoints => vec!["NAME", "ENDPOINTS", "AGE"],
            | Self::NetworkPolicy => vec!["NAME", "POD-SELECTOR", "AGE"],
            | Self::PersistentVolumeClaim => vec!["NAME", "STATUS", "VOLUME", "CAPACITY", "AGE"],
            | Self::PersistentVolume => vec!["NAME", "CAPACITY", "RECLAIM POLICY", "STATUS", "CLAIM", "AGE"],
            | Self::StorageClass => vec!["NAME", "PROVISIONER", "RECLAIM POLICY", "AGE"],
            | Self::ServiceAccount => vec!["NAME", "SECRETS", "AGE"],
            | Self::Role => vec!["NAME", "AGE"],
            | Self::RoleBinding => vec!["NAME", "ROLE", "AGE"],
            | Self::ClusterRole => vec!["NAME", "AGE"],
            | Self::ClusterRoleBinding => vec!["NAME", "ROLE", "AGE"],
            | Self::Node => vec!["NAME", "STATUS", "ROLES", "VERSION", "AGE"],
            | Self::Namespace => vec!["NAME", "STATUS", "AGE"],
            | Self::Event => vec!["NAME", "TYPE", "REASON", "OBJECT", "AGE"],
            | Self::CustomResourceDefinition => vec!["NAME", "GROUP", "VERSIONS", "SCOPE", "AGE"],
        }
    }

    pub fn supports_logs(&self) -> bool {
        matches!(
            self,
            Self::Deployment
                | Self::StatefulSet
                | Self::DaemonSet
                | Self::ReplicaSet
                | Self::Pod
                | Self::CronJob
                | Self::Job
        )
    }

    pub fn supports_scale(&self) -> bool {
        matches!(self, Self::Deployment | Self::StatefulSet | Self::ReplicaSet)
    }

    pub fn supports_exec(&self) -> bool {
        matches!(self, Self::Pod)
    }

    pub fn supports_restart(&self) -> bool {
        matches!(self, Self::Deployment | Self::StatefulSet | Self::DaemonSet)
    }

    pub fn supports_port_forward(&self) -> bool {
        matches!(
            self,
            Self::Service
                | Self::Deployment
                | Self::StatefulSet
                | Self::DaemonSet
                | Self::ReplicaSet
                | Self::Pod
                | Self::Job
                | Self::CronJob
        )
    }

    pub fn is_cluster_scoped(&self) -> bool {
        matches!(
            self,
            Self::Node
                | Self::Namespace
                | Self::PersistentVolume
                | Self::StorageClass
                | Self::ClusterRole
                | Self::ClusterRoleBinding
                | Self::CustomResourceDefinition
        )
    }

    /// Returns the kube `ApiResource` descriptor for dynamic API operations.
    pub fn api_resource(&self) -> kube::api::ApiResource {
        use kube::api::ApiResource;
        match self {
            | Self::Deployment => ApiResource::erase::<Deployment>(&()),
            | Self::StatefulSet => ApiResource::erase::<StatefulSet>(&()),
            | Self::DaemonSet => ApiResource::erase::<DaemonSet>(&()),
            | Self::ReplicaSet => ApiResource::erase::<ReplicaSet>(&()),
            | Self::Pod => ApiResource::erase::<Pod>(&()),
            | Self::CronJob => ApiResource::erase::<CronJob>(&()),
            | Self::Job => ApiResource::erase::<Job>(&()),
            | Self::HorizontalPodAutoscaler => ApiResource::erase::<HorizontalPodAutoscaler>(&()),
            | Self::ConfigMap => ApiResource::erase::<ConfigMap>(&()),
            | Self::Secret => ApiResource::erase::<Secret>(&()),
            | Self::Service => ApiResource::erase::<Service>(&()),
            | Self::Ingress => ApiResource::erase::<Ingress>(&()),
            | Self::Endpoints => ApiResource::erase::<Endpoints>(&()),
            | Self::NetworkPolicy => ApiResource::erase::<NetworkPolicy>(&()),
            | Self::PersistentVolumeClaim => ApiResource::erase::<PersistentVolumeClaim>(&()),
            | Self::PersistentVolume => ApiResource::erase::<PersistentVolume>(&()),
            | Self::StorageClass => ApiResource::erase::<StorageClass>(&()),
            | Self::ServiceAccount => ApiResource::erase::<ServiceAccount>(&()),
            | Self::Role => ApiResource::erase::<Role>(&()),
            | Self::RoleBinding => ApiResource::erase::<RoleBinding>(&()),
            | Self::ClusterRole => ApiResource::erase::<ClusterRole>(&()),
            | Self::ClusterRoleBinding => ApiResource::erase::<ClusterRoleBinding>(&()),
            | Self::Node => ApiResource::erase::<Node>(&()),
            | Self::Namespace => ApiResource::erase::<Namespace>(&()),
            | Self::Event => ApiResource::erase::<Event>(&()),
            | Self::CustomResourceDefinition => ApiResource::erase::<CustomResourceDefinition>(&()),
        }
    }

    pub fn all_by_category() -> Vec<(Category, Vec<ResourceType>)> {
        vec![
            (Category::Workloads, vec![
                Self::Deployment,
                Self::StatefulSet,
                Self::DaemonSet,
                Self::ReplicaSet,
                Self::Pod,
                Self::CronJob,
                Self::Job,
                Self::HorizontalPodAutoscaler,
            ]),
            (Category::Network, vec![
                Self::Service,
                Self::Ingress,
                Self::Endpoints,
                Self::NetworkPolicy,
            ]),
            (Category::Config, vec![Self::ConfigMap, Self::Secret]),
            (Category::Storage, vec![
                Self::PersistentVolumeClaim,
                Self::PersistentVolume,
                Self::StorageClass,
            ]),
            (Category::Rbac, vec![
                Self::ServiceAccount,
                Self::Role,
                Self::RoleBinding,
                Self::ClusterRole,
                Self::ClusterRoleBinding,
            ]),
            (Category::Cluster, vec![
                Self::Node,
                Self::Namespace,
                Self::Event,
                Self::CustomResourceDefinition,
            ]),
        ]
    }

    pub fn category(self) -> Category {
        match self {
            | Self::Deployment
            | Self::StatefulSet
            | Self::DaemonSet
            | Self::ReplicaSet
            | Self::Pod
            | Self::CronJob
            | Self::Job
            | Self::HorizontalPodAutoscaler => Category::Workloads,
            | Self::ConfigMap | Self::Secret => Category::Config,
            | Self::Service | Self::Ingress | Self::Endpoints | Self::NetworkPolicy => Category::Network,
            | Self::PersistentVolumeClaim | Self::PersistentVolume | Self::StorageClass => Category::Storage,
            | Self::ServiceAccount | Self::Role | Self::RoleBinding | Self::ClusterRole | Self::ClusterRoleBinding => {
                Category::Rbac
            },
            | Self::Node | Self::Namespace | Self::Event | Self::CustomResourceDefinition => Category::Cluster,
        }
    }

    /// Abbreviated label for compact tables (favorites, port-forward type
    /// column).
    pub fn short_label(self) -> &'static str {
        match self {
            | Self::Deployment => "Deploy",
            | Self::StatefulSet => "StatefulSet",
            | Self::DaemonSet => "DaemonSet",
            | Self::ReplicaSet => "ReplicaSet",
            | Self::Pod => "Pod",
            | Self::CronJob => "CronJob",
            | Self::Job => "Job",
            | Self::HorizontalPodAutoscaler => "HPA",
            | Self::ConfigMap => "ConfigMap",
            | Self::Secret => "Secret",
            | Self::Service => "Service",
            | Self::Ingress => "Ingress",
            | Self::Endpoints => "Endpoints",
            | Self::NetworkPolicy => "NetPolicy",
            | Self::PersistentVolumeClaim => "PVC",
            | Self::PersistentVolume => "PV",
            | Self::StorageClass => "StorageClass",
            | Self::ServiceAccount => "SA",
            | Self::Role => "Role",
            | Self::RoleBinding => "RoleBinding",
            | Self::ClusterRole => "ClusterRole",
            | Self::ClusterRoleBinding => "CRBinding",
            | Self::Node => "Node",
            | Self::Namespace => "Namespace",
            | Self::Event => "Event",
            | Self::CustomResourceDefinition => "CRD",
        }
    }

    /// Match a builtin type by the `apiVersion` and `kind` written in YAML.
    pub fn from_gvk(api_version: &str, kind: &str) -> Option<Self> {
        Self::all_by_category()
            .into_iter()
            .flat_map(|(_, types)| types)
            .find(|rt| {
                let ar = rt.api_resource();
                ar.kind == kind && ar.api_version == api_version
            })
    }
}

impl Category {
    pub fn display_name(&self) -> &'static str {
        match self {
            | Self::Workloads => "Workloads",
            | Self::Config => "Config",
            | Self::Network => "Network",
            | Self::Storage => "Storage",
            | Self::Rbac => "RBAC",
            | Self::Cluster => "Cluster",
        }
    }
}

// ---------------------------------------------------------------------------
// Resource kind — builtin type or a discovered custom resource.
// One value, so callers cannot pair a dummy ResourceType with a CrdInfo.
// ---------------------------------------------------------------------------

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum ResourceKind {
    Builtin(ResourceType),
    Custom(CrdInfo),
}

impl ResourceKind {
    pub fn builtin(&self) -> Option<ResourceType> {
        match self {
            | Self::Builtin(rt) => Some(*rt),
            | Self::Custom(_) => None,
        }
    }

    pub fn crd(&self) -> Option<&CrdInfo> {
        match self {
            | Self::Custom(crd) => Some(crd),
            | Self::Builtin(_) => None,
        }
    }

    pub fn display_name(&self) -> &str {
        match self {
            | Self::Builtin(rt) => rt.display_name(),
            | Self::Custom(crd) => crd.display_name.as_str(),
        }
    }

    pub fn column_headers(&self) -> Vec<String> {
        match self {
            | Self::Builtin(rt) => rt.column_headers().into_iter().map(str::to_string).collect(),
            | Self::Custom(crd) => crd.column_headers(),
        }
    }

    pub fn is_event(&self) -> bool {
        matches!(self.builtin(), Some(ResourceType::Event))
    }

    pub fn is_cluster_scoped(&self) -> bool {
        match self {
            | Self::Builtin(rt) => rt.is_cluster_scoped(),
            | Self::Custom(crd) => crd.is_cluster_scoped(),
        }
    }

    pub fn api_resource(&self) -> kube::api::ApiResource {
        match self {
            | Self::Builtin(rt) => rt.api_resource(),
            | Self::Custom(crd) => crd.api_resource(),
        }
    }

    pub fn favorite_key(&self) -> FavoriteType {
        match self {
            | Self::Builtin(rt) => FavoriteType::builtin(*rt),
            | Self::Custom(crd) => FavoriteType::custom(&crd.group, &crd.plural),
        }
    }
}

/// Resource identity as stored in config.
///
/// Builtins use `ResourceType::singular_name` (`deployment`). Custom resources
/// use `cr:{group}/{plural}` so a favorite survives without embedding the full
/// CRD. Unknown strings are preserved so a newer config is not dropped on save.
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
#[serde(transparent)]
pub struct FavoriteType(String);

impl FavoriteType {
    pub fn builtin(rt: ResourceType) -> Self {
        Self(rt.singular_name().to_string())
    }

    pub fn custom(group: &str, plural: &str) -> Self {
        Self(format!("cr:{group}/{plural}"))
    }

    pub fn as_str(&self) -> &str {
        &self.0
    }

    pub fn builtin_type(&self) -> Option<ResourceType> {
        ResourceType::from_singular_name(&self.0)
    }

    pub fn custom_ref(&self) -> Option<(&str, &str)> {
        self.0.strip_prefix("cr:")?.split_once('/')
    }

    pub fn short_label(&self) -> std::borrow::Cow<'_, str> {
        if let Some(rt) = self.builtin_type() {
            return std::borrow::Cow::Borrowed(rt.short_label());
        }
        if let Some((_, plural)) = self.custom_ref() {
            return std::borrow::Cow::Borrowed(plural);
        }
        std::borrow::Cow::Borrowed("Resource")
    }
}

impl std::fmt::Display for FavoriteType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.0)
    }
}

// ---------------------------------------------------------------------------
// Resource entry (one row in the table)
// ---------------------------------------------------------------------------

pub struct ResourceEntry {
    pub name: String,
    pub namespace: String,
    pub columns: Vec<String>,
    /// Optional sort key (e.g. ISO 8601 timestamp for Events). Not displayed.
    pub sort_key: Option<String>,
}

// ---------------------------------------------------------------------------
// Pod info (for log view pod/container selection)
// ---------------------------------------------------------------------------

pub struct PodInfo {
    pub name: String,
    pub containers: Vec<String>,
}

pub struct RelatedResource {
    pub resource_type: ResourceType,
    pub name: String,
    pub namespace: String,
    pub info: String,
    pub category: &'static str,
    pub sort_key: String,
}

pub struct RelatedEvent {
    pub type_: String,
    pub reason: String,
    pub message: String,
    pub count: i32,
    pub last_seen: String,
}

// ---------------------------------------------------------------------------
// Cluster stats
// ---------------------------------------------------------------------------

pub struct ResourceCapacity {
    pub capacity: String,
    pub allocatable: String,
}

pub struct ResourceUsage {
    pub cpu_cores: f64,
    pub memory_bytes: f64,
}

pub struct NodeStats {
    pub name: String,
    pub status: String,
    pub unschedulable: bool,
    pub roles: String,
    pub version: String,
    pub os_arch: String,
    pub cpu: ResourceCapacity,
    pub mem: ResourceCapacity,
    pub pods: ResourceCapacity,
    pub age: String,
    pub usage: Option<ResourceUsage>,
}

pub struct NodeCounts {
    pub total: usize,
    pub ready: usize,
    pub not_ready: usize,
    pub cordoned: usize,
}

pub struct PodCounts {
    pub total: usize,
    pub running: usize,
    pub pending: usize,
    pub succeeded: usize,
    pub failed: usize,
    pub crash_loop: usize,
    pub error: usize,
}

pub struct WorkloadHealth {
    pub deployments_ready: usize,
    pub deployments_total: usize,
    pub statefulsets_ready: usize,
    pub statefulsets_total: usize,
    pub daemonsets_ready: usize,
    pub daemonsets_desired: usize,
}

pub struct ContainerCounts {
    pub total: usize,
    pub ready: usize,
    pub total_restarts: i64,
}

pub struct ClusterResources {
    pub cpu_capacity: f64,
    pub cpu_allocatable: f64,
    pub mem_capacity_bytes: f64,
    pub mem_allocatable_bytes: f64,
    pub pod_capacity: usize,
}

pub struct TopRestartingPod {
    pub name: String,
    pub namespace: String,
    pub restart_count: i64,
}

pub struct WarningEventDetail {
    pub reason: String,
    pub message: String,
    pub namespace: String,
    pub count: i32,
}

pub struct PressureDetail {
    pub disk_pressure: usize,
    pub memory_pressure: usize,
    pub pid_pressure: usize,
}

// ---------------------------------------------------------------------------
// Metrics-server data (metrics.k8s.io/v1beta1)
// ---------------------------------------------------------------------------

#[derive(Clone)]
pub struct NodeMetricsEntry {
    pub name: String,
    pub cpu_cores: f64,
    pub memory_bytes: f64,
}

#[derive(Clone, Default)]
pub struct PodResourceSpec {
    pub cpu_request: f64,
    pub cpu_limit: f64,
    pub mem_request: f64,
    pub mem_limit: f64,
}

#[derive(Clone)]
pub struct PodMetricsEntry {
    pub namespace: String,
    pub cpu_cores: f64,
    pub memory_bytes: f64,
    /// Aggregated from pod spec containers[].resources
    pub spec: PodResourceSpec,
    /// Top-level owner (Deployment, StatefulSet, DaemonSet, Job, etc.)
    pub owner: String,
    pub owner_kind: String,
}

#[derive(Clone)]
pub struct WorkloadMetrics {
    pub name: String,
    pub namespace: String,
    pub kind: String,
    pub pod_count: usize,
    pub cpu_usage: f64,
    pub mem_usage: f64,
    pub cpu_request: f64,
    pub cpu_limit: f64,
    pub mem_request: f64,
    pub mem_limit: f64,
}

#[derive(Clone)]
pub struct MetricsData {
    pub available: bool,
    pub node_metrics: Vec<NodeMetricsEntry>,
    pub pod_metrics: Vec<PodMetricsEntry>,
    pub workload_metrics: Vec<WorkloadMetrics>,
    /// Aggregate cluster usage (sum of all nodes)
    pub total_cpu_usage: f64,
    pub total_memory_usage: f64,
}

pub struct ClusterStatsData {
    pub server_version: String,
    pub nodes: NodeCounts,
    pub node_list: Vec<NodeStats>,
    pub namespace_count: usize,
    pub pods: PodCounts,
    pub deployment_count: usize,
    pub service_count: usize,
    pub recent_warnings: usize,
    pub statefulset_count: usize,
    pub daemonset_count: usize,
    pub job_count: usize,
    pub cronjob_count: usize,
    pub configmap_count: usize,
    pub secret_count: usize,
    pub pvc_count: usize,
    pub ingress_count: usize,
    pub hpa_count: usize,
    pub workload_health: WorkloadHealth,
    pub containers: ContainerCounts,
    pub cluster_resources: ClusterResources,
    pub top_restarting: Vec<TopRestartingPod>,
    pub warning_details: Vec<WarningEventDetail>,
    pub pressure: PressureDetail,
}

// ---------------------------------------------------------------------------
// Kubernetes client
// ---------------------------------------------------------------------------

#[derive(Clone)]
pub struct KubeClients {
    /// Standard client with connect/read/write timeouts.
    pub default: Client,
    /// Client with only connect timeout — no read timeout so long-lived
    /// log follow streams aren't killed.
    pub streaming: Client,
}

#[derive(Clone)]
pub struct KubeconfigState {
    pub config: Kubeconfig,
    pub path: Option<String>,
}

#[derive(Clone)]
pub struct KubeContext {
    pub name: String,
    pub namespace: Option<String>,
}

#[derive(Clone)]
pub struct KubeClient {
    pub client: KubeClients,
    pub kubeconfig: KubeconfigState,
    pub context: KubeContext,
}

impl KubeClient {
    pub async fn new(
        kubeconfig_path: Option<String>,
        context_name: Option<String>,
        namespace: Option<String>,
    ) -> Result<Self> {
        // Always resolve the kubeconfig path so it can be persisted per profile
        let path = kubeconfig_path.or_else(|| {
            std::env::var("KUBECONFIG")
                .ok()
                .or_else(|| dirs::home_dir().map(|h| h.join(".kube/config").to_string_lossy().into_owned()))
        });
        let config = match &path {
            | Some(p) => Kubeconfig::read_from(p).context("Failed to read kubeconfig")?,
            | None => Kubeconfig::read().context("Failed to read default kubeconfig")?,
        };

        let name = context_name
            .or_else(|| config.current_context.clone())
            .unwrap_or_default();

        let options = KubeConfigOptions {
            context: Some(name.clone()),
            ..Default::default()
        };
        let client = Self::build_clients(&config, &options).await?;

        Ok(Self {
            client,
            kubeconfig: KubeconfigState { config, path },
            context: KubeContext { name, namespace },
        })
    }

    pub fn contexts(&self) -> Vec<String> {
        self.kubeconfig.config.contexts.iter().map(|c| c.name.clone()).collect()
    }

    async fn build_clients(kubeconfig: &Kubeconfig, options: &KubeConfigOptions) -> Result<KubeClients> {
        let mut silenced = kubeconfig.clone();
        Self::silence_exec_plugins(&mut silenced);
        let mut config = kube::Config::from_custom_kubeconfig(silenced.clone(), options)
            .await
            .context("Failed to build kube config from kubeconfig")?;
        apply_timeouts(&mut config);
        let default = Client::try_from(config)?;

        let mut stream_config = kube::Config::from_custom_kubeconfig(silenced, options)
            .await
            .context("Failed to build kube config from kubeconfig")?;
        stream_config.connect_timeout = Some(REQUEST_TIMEOUT);
        stream_config.read_timeout = None;
        stream_config.write_timeout = Some(REQUEST_TIMEOUT);
        let streaming = Client::try_from(stream_config)?;

        Ok(KubeClients { default, streaming })
    }

    /// Credential plugins (gcloud, gke-gcloud-auth-plugin) inherit this process
    /// when kube marks them interactive. Their prompts and ANSI errors are what
    /// tear down the alternate screen. Force non-interactive and drop stderr.
    /// The user's kubeconfig file is not modified.
    pub fn silence_exec_plugins(config: &mut Kubeconfig) {
        // Also covers the legacy gcp auth-provider command, which inherits this
        // process.
        std::env::set_var("CLOUDSDK_CORE_DISABLE_PROMPTS", "1");
        for named in &mut config.auth_infos {
            let Some(exec) = named.auth_info.as_mut().and_then(|info| info.exec.as_mut()) else {
                continue;
            };
            exec.interactive_mode = Some(kube::config::ExecInteractiveMode::Never);
            let Some(command) = exec.command.clone().filter(|cmd| !cmd.is_empty()) else {
                continue;
            };
            if command == "sh" {
                continue;
            }
            let mut parts = vec![shell_quote(&command)];
            if let Some(args) = &exec.args {
                parts.extend(args.iter().map(|arg| shell_quote(arg)));
            }
            // ponytail: sh redirect. kube still inherits stderr unless the child closes it.
            let script = format!("CLOUDSDK_CORE_DISABLE_PROMPTS=1 exec {} 2>/dev/null", parts.join(" "));
            exec.command = Some("sh".into());
            exec.args = Some(vec!["-c".into(), script]);
        }
    }

    /// One line, no ANSI, no controls. Safe to draw in the TUI.
    pub fn display_error(message: &str) -> String {
        sanitize_terminal_text(message, 240)
    }

    pub async fn switch_context(&mut self, ctx: &str) -> Result<()> {
        let options = KubeConfigOptions {
            context: Some(ctx.to_string()),
            ..Default::default()
        };
        self.client = Self::build_clients(&self.kubeconfig.config, &options).await?;
        self.context.namespace = None;
        self.context.name = ctx.to_string();
        Ok(())
    }

    pub async fn list_namespaces(&self) -> Result<Vec<String>> {
        let api: Api<Namespace> = Api::all(self.client.default.clone());
        let list = api.list(&ListParams::default()).await?;
        let mut names: Vec<String> = list.items.iter().filter_map(|ns| ns.metadata.name.clone()).collect();
        names.sort();
        Ok(names)
    }

    pub async fn fetch_cluster_stats_with_client(c: &Client) -> Result<ClusterStatsData> {
        let lp = ListParams::default();

        // Server version
        let server_version = match c.apiserver_version().await {
            | Ok(info) => format!("{}.{}", info.major, info.minor),
            | Err(_) => "unknown".into(),
        };

        // Parallel API calls
        let node_api: Api<Node> = Api::all(c.clone());
        let ns_api: Api<Namespace> = Api::all(c.clone());
        let pod_api: Api<Pod> = Api::all(c.clone());
        let dep_api: Api<Deployment> = Api::all(c.clone());
        let svc_api: Api<Service> = Api::all(c.clone());
        let sts_api: Api<StatefulSet> = Api::all(c.clone());
        let ds_api: Api<DaemonSet> = Api::all(c.clone());
        let job_api: Api<Job> = Api::all(c.clone());
        let cj_api: Api<CronJob> = Api::all(c.clone());
        let cm_api: Api<ConfigMap> = Api::all(c.clone());
        let secret_api: Api<Secret> = Api::all(c.clone());
        let pvc_api: Api<PersistentVolumeClaim> = Api::all(c.clone());
        let ing_api: Api<Ingress> = Api::all(c.clone());
        let hpa_api: Api<HorizontalPodAutoscaler> = Api::all(c.clone());
        let event_api: Api<Event> = Api::all(c.clone());

        let (
            node_res,
            ns_res,
            pod_res,
            dep_res,
            svc_res,
            sts_res,
            ds_res,
            job_res,
            cj_res,
            cm_res,
            secret_res,
            pvc_res,
            ing_res,
            hpa_res,
            event_res,
        ) = tokio::join!(
            node_api.list(&lp),
            ns_api.list(&lp),
            pod_api.list(&lp),
            dep_api.list(&lp),
            svc_api.list(&lp),
            sts_api.list(&lp),
            ds_api.list(&lp),
            job_api.list(&lp),
            cj_api.list(&lp),
            cm_api.list(&lp),
            secret_api.list(&lp),
            pvc_api.list(&lp),
            ing_api.list(&lp),
            hpa_api.list(&lp),
            event_api.list(&lp),
        );

        let node_list = node_res?;
        let pod_list = pod_res?;
        let dep_list = dep_res?;
        let sts_list = sts_res?;
        let ds_list = ds_res?;

        let ns_count = ns_res.map(|l| l.items.len()).unwrap_or(0);
        let service_count = svc_res.map(|l| l.items.len()).unwrap_or(0);
        let job_count = job_res.map(|l| l.items.len()).unwrap_or(0);
        let cronjob_count = cj_res.map(|l| l.items.len()).unwrap_or(0);
        let configmap_count = cm_res.map(|l| l.items.len()).unwrap_or(0);
        let secret_count = secret_res.map(|l| l.items.len()).unwrap_or(0);
        let pvc_count = pvc_res.map(|l| l.items.len()).unwrap_or(0);
        let ingress_count = ing_res.map(|l| l.items.len()).unwrap_or(0);
        let hpa_count = hpa_res.map(|l| l.items.len()).unwrap_or(0);

        // ── Nodes ────────────────────────────────────────────────
        let mut nodes_ready = 0usize;
        let mut nodes_not_ready = 0usize;
        let mut node_stats = Vec::new();
        let mut total_cpu_cap = 0.0f64;
        let mut total_cpu_alloc = 0.0f64;
        let mut total_mem_cap = 0.0f64;
        let mut total_mem_alloc = 0.0f64;
        let mut total_pod_cap = 0usize;
        let mut pressure = PressureDetail {
            disk_pressure: 0,
            memory_pressure: 0,
            pid_pressure: 0,
        };

        for n in &node_list.items {
            let status = n.status.as_ref();
            let conditions = status.and_then(|s| s.conditions.as_ref());
            let is_ready = conditions
                .and_then(|c| c.iter().find(|c| c.type_ == "Ready"))
                .map(|c| c.status == "True")
                .unwrap_or(false);
            if is_ready {
                nodes_ready += 1;
            } else {
                nodes_not_ready += 1;
            }

            // Pressure per type
            if let Some(conds) = conditions {
                for cond in conds {
                    if cond.status == "True" {
                        match cond.type_.as_str() {
                            | "DiskPressure" => pressure.disk_pressure += 1,
                            | "MemoryPressure" => pressure.memory_pressure += 1,
                            | "PIDPressure" => pressure.pid_pressure += 1,
                            | _ => {},
                        }
                    }
                }
            }

            let roles = n
                .metadata
                .labels
                .as_ref()
                .map(|labels| {
                    labels
                        .keys()
                        .filter_map(|k| {
                            k.strip_prefix("node-role.kubernetes.io/")
                                .map(|r| if r.is_empty() { "worker" } else { r })
                        })
                        .collect::<Vec<_>>()
                        .join(",")
                })
                .unwrap_or_default();

            let info = status.and_then(|s| s.node_info.as_ref());
            let capacity = status.and_then(|s| s.capacity.as_ref());
            let allocatable = status.and_then(|s| s.allocatable.as_ref());

            let get_res = |map: Option<
                &std::collections::BTreeMap<String, k8s_openapi::apimachinery::pkg::api::resource::Quantity>,
            >,
                           key: &str|
             -> String {
                map.and_then(|m| m.get(key))
                    .map(|q| q.0.clone())
                    .unwrap_or_else(|| "-".into())
            };

            // Aggregate cluster resources
            let cpu_cap_raw = get_res(capacity, "cpu");
            let cpu_alloc_raw = get_res(allocatable, "cpu");
            let mem_cap_raw = get_res(capacity, "memory");
            let mem_alloc_raw = get_res(allocatable, "memory");
            let pod_cap_raw = get_res(capacity, "pods");
            let pod_alloc_raw = get_res(allocatable, "pods");

            total_cpu_cap += parse_cpu_cores(&cpu_cap_raw);
            total_cpu_alloc += parse_cpu_cores(&cpu_alloc_raw);
            total_mem_cap += parse_memory_bytes(&mem_cap_raw);
            total_mem_alloc += parse_memory_bytes(&mem_alloc_raw);
            total_pod_cap += pod_cap_raw.parse::<usize>().unwrap_or(0);

            let unschedulable = n.spec.as_ref().and_then(|s| s.unschedulable).unwrap_or(false);
            let node_status = match (is_ready, unschedulable) {
                | (true, false) => "Ready".to_string(),
                | (true, true) => "Ready,SchedulingDisabled".to_string(),
                | (false, false) => "NotReady".to_string(),
                | (false, true) => "NotReady,SchedulingDisabled".to_string(),
            };

            node_stats.push(NodeStats {
                name: meta_name(&n.metadata),
                status: node_status,
                unschedulable,
                roles: if roles.is_empty() { "<none>".into() } else { roles },
                version: info.map(|i| i.kubelet_version.clone()).unwrap_or_default(),
                os_arch: info
                    .map(|i| format!("{}/{}", i.operating_system, i.architecture))
                    .unwrap_or_default(),
                cpu: ResourceCapacity {
                    capacity: cpu_cap_raw,
                    allocatable: cpu_alloc_raw,
                },
                mem: ResourceCapacity {
                    capacity: format_memory_gb(&mem_cap_raw),
                    allocatable: format_memory_gb(&mem_alloc_raw),
                },
                pods: ResourceCapacity {
                    capacity: pod_cap_raw,
                    allocatable: pod_alloc_raw,
                },
                age: format_age(n.metadata.creation_timestamp.as_ref()),
                usage: None, // Filled in by metrics fetch
            });
        }

        let nodes_cordoned = node_stats.iter().filter(|n| n.unschedulable).count();

        // ── Pods ─────────────────────────────────────────────────
        let pod_count = pod_list.items.len();
        let mut pods_running = 0usize;
        let mut pods_pending = 0usize;
        let mut pods_succeeded = 0usize;
        let mut pods_failed = 0usize;
        let mut pods_crash_loop = 0usize;
        let mut pods_error = 0usize;
        let mut total_containers = 0usize;
        let mut ready_containers = 0usize;
        let mut total_restarts = 0i64;
        let mut restart_pods: Vec<TopRestartingPod> = Vec::new();

        for p in &pod_list.items {
            match p.status.as_ref().and_then(|s| s.phase.as_deref()) {
                | Some("Running") => pods_running += 1,
                | Some("Pending") => pods_pending += 1,
                | Some("Succeeded") => pods_succeeded += 1,
                | Some("Failed") => pods_failed += 1,
                | _ => {},
            }
            let container_statuses = p.status.as_ref().and_then(|s| s.container_statuses.as_ref());
            if let Some(statuses) = container_statuses {
                let mut pod_restarts = 0i64;
                for cs in statuses {
                    total_containers += 1;
                    if cs.ready {
                        ready_containers += 1;
                    }
                    let r = cs.restart_count as i64;
                    total_restarts += r;
                    pod_restarts += r;
                    if let Some(waiting) = cs.state.as_ref().and_then(|s| s.waiting.as_ref()) {
                        match waiting.reason.as_deref() {
                            | Some("CrashLoopBackOff") => pods_crash_loop += 1,
                            | Some("ImagePullBackOff") | Some("ErrImagePull") | Some("CreateContainerConfigError") => {
                                pods_error += 1
                            },
                            | _ => {},
                        }
                    }
                }
                if pod_restarts > 0 {
                    restart_pods.push(TopRestartingPod {
                        name: meta_name(&p.metadata),
                        namespace: meta_ns(&p.metadata),
                        restart_count: pod_restarts,
                    });
                }
            }
            // Also count init containers
            if let Some(init_statuses) = p.status.as_ref().and_then(|s| s.init_container_statuses.as_ref()) {
                for cs in init_statuses {
                    total_containers += 1;
                    if cs.ready {
                        ready_containers += 1;
                    }
                }
            }
        }

        // Top 5 restarting pods
        restart_pods.sort_by(|a, b| b.restart_count.cmp(&a.restart_count));
        restart_pods.truncate(5);

        // ── Workload health ──────────────────────────────────────
        let deployment_count = dep_list.items.len();
        let deployments_ready = dep_list
            .items
            .iter()
            .filter(|d| {
                let desired = d.spec.as_ref().and_then(|s| s.replicas).unwrap_or(1);
                let available = d.status.as_ref().and_then(|s| s.available_replicas).unwrap_or(0);
                available >= desired
            })
            .count();

        let statefulset_count = sts_list.items.len();
        let statefulsets_ready = sts_list
            .items
            .iter()
            .filter(|s| {
                let desired = s.spec.as_ref().and_then(|sp| sp.replicas).unwrap_or(1);
                let ready = s.status.as_ref().and_then(|st| st.ready_replicas).unwrap_or(0);
                ready >= desired
            })
            .count();

        let daemonset_count = ds_list.items.len();
        let daemonsets_ready = ds_list
            .items
            .iter()
            .filter(|d| {
                let desired = d.status.as_ref().map(|s| s.desired_number_scheduled).unwrap_or(0);
                let ready = d.status.as_ref().map(|s| s.number_ready).unwrap_or(0);
                ready >= desired
            })
            .count();

        // ── Events ───────────────────────────────────────────────
        let (recent_warnings, warning_details) = if let Ok(events) = event_res {
            let cutoff = Timestamp::now() - jiff::SignedDuration::from_hours(1);
            let recent: Vec<_> = events
                .items
                .iter()
                .filter(|e| {
                    e.type_.as_deref() == Some("Warning")
                        && e.event_time
                            .as_ref()
                            .map(|t| t.0)
                            .or(e.deprecated_last_timestamp.as_ref().map(|t| t.0))
                            .map(|t| t >= cutoff)
                            .unwrap_or(false)
                })
                .collect();
            let count = recent.len();

            // Deduplicate by (reason, message), aggregate counts
            let mut event_map: std::collections::HashMap<(String, String), WarningEventDetail> =
                std::collections::HashMap::new();
            for e in &recent {
                let reason = e.reason.clone().unwrap_or_default();
                let msg = e.note.clone().unwrap_or_default();
                let ns = e.metadata.namespace.clone().unwrap_or_default();
                let key = (reason.clone(), msg.clone());
                let entry = event_map.entry(key).or_insert(WarningEventDetail {
                    reason,
                    message: msg,
                    namespace: ns,
                    count: 0,
                });
                entry.count += 1;
            }
            let mut details: Vec<_> = event_map.into_values().collect();
            details.sort_by(|a, b| b.count.cmp(&a.count));
            details.truncate(8);

            (count, details)
        } else {
            (0, Vec::new())
        };

        Ok(ClusterStatsData {
            server_version,
            nodes: NodeCounts {
                total: node_list.items.len(),
                ready: nodes_ready,
                not_ready: nodes_not_ready,
                cordoned: nodes_cordoned,
            },
            node_list: node_stats,
            namespace_count: ns_count,
            pods: PodCounts {
                total: pod_count,
                running: pods_running,
                pending: pods_pending,
                succeeded: pods_succeeded,
                failed: pods_failed,
                crash_loop: pods_crash_loop,
                error: pods_error,
            },
            deployment_count,
            service_count,
            recent_warnings,
            statefulset_count,
            daemonset_count,
            job_count,
            cronjob_count,
            configmap_count,
            secret_count,
            pvc_count,
            ingress_count,
            hpa_count,
            workload_health: WorkloadHealth {
                deployments_ready,
                deployments_total: deployment_count,
                statefulsets_ready,
                statefulsets_total: statefulset_count,
                daemonsets_ready,
                daemonsets_desired: daemonset_count,
            },
            containers: ContainerCounts {
                total: total_containers,
                ready: ready_containers,
                total_restarts,
            },
            cluster_resources: ClusterResources {
                cpu_capacity: total_cpu_cap,
                cpu_allocatable: total_cpu_alloc,
                mem_capacity_bytes: total_mem_cap,
                mem_allocatable_bytes: total_mem_alloc,
                pod_capacity: total_pod_cap,
            },
            top_restarting: restart_pods,
            warning_details,
            pressure,
        })
    }

    /// Fetch metrics from the metrics-server API (metrics.k8s.io/v1beta1).
    /// Returns `MetricsData { available: false, .. }` if metrics-server is not
    /// installed — never fails hard so the overview keeps working without it.
    pub async fn fetch_metrics_with_client(c: &Client) -> MetricsData {
        let empty = MetricsData {
            available: false,
            node_metrics: Vec::new(),
            pod_metrics: Vec::new(),
            workload_metrics: Vec::new(),
            total_cpu_usage: 0.0,
            total_memory_usage: 0.0,
        };

        let node_ar = kube::api::ApiResource {
            group: "metrics.k8s.io".into(),
            version: "v1beta1".into(),
            api_version: "metrics.k8s.io/v1beta1".into(),
            kind: "NodeMetrics".into(),
            plural: "nodes".into(),
        };
        let pod_ar = kube::api::ApiResource {
            group: "metrics.k8s.io".into(),
            version: "v1beta1".into(),
            api_version: "metrics.k8s.io/v1beta1".into(),
            kind: "PodMetrics".into(),
            plural: "pods".into(),
        };

        let node_api: Api<kube::api::DynamicObject> = Api::all_with(c.clone(), &node_ar);
        let pod_metrics_api: Api<kube::api::DynamicObject> = Api::all_with(c.clone(), &pod_ar);
        let pod_spec_api: Api<Pod> = Api::all(c.clone());

        let lp = ListParams::default();
        let (node_res, metrics_res, spec_res) =
            tokio::join!(node_api.list(&lp), pod_metrics_api.list(&lp), pod_spec_api.list(&lp),);

        let node_list = match node_res {
            | Ok(list) => list,
            | Err(_) => return empty,
        };
        let metrics_list = match metrics_res {
            | Ok(list) => list,
            | Err(_) => return empty,
        };
        let spec_list = spec_res.ok();

        // Build pod spec lookup: name+ns → (requests, limits, owner)
        let mut spec_map: std::collections::HashMap<(String, String), (PodResourceSpec, String, String)> =
            std::collections::HashMap::new();
        if let Some(pods) = &spec_list {
            for p in &pods.items {
                let name = meta_name(&p.metadata);
                let ns = meta_ns(&p.metadata);
                let mut spec = PodResourceSpec::default();

                if let Some(pod_spec) = &p.spec {
                    for container in &pod_spec.containers {
                        if let Some(res) = &container.resources {
                            if let Some(req) = &res.requests {
                                if let Some(cpu) = req.get("cpu") {
                                    spec.cpu_request += parse_cpu_cores(&cpu.0);
                                }
                                if let Some(mem) = req.get("memory") {
                                    spec.mem_request += parse_memory_bytes(&mem.0);
                                }
                            }
                            if let Some(lim) = &res.limits {
                                if let Some(cpu) = lim.get("cpu") {
                                    spec.cpu_limit += parse_cpu_cores(&cpu.0);
                                }
                                if let Some(mem) = lim.get("memory") {
                                    spec.mem_limit += parse_memory_bytes(&mem.0);
                                }
                            }
                        }
                    }
                }

                // Resolve owner — walk up: Pod → ReplicaSet → Deployment
                let (owner_name, owner_kind) = resolve_top_owner(p);

                spec_map.insert((name, ns), (spec, owner_name, owner_kind));
            }
        }

        // Node metrics
        let mut total_cpu = 0.0f64;
        let mut total_mem = 0.0f64;

        let node_metrics: Vec<NodeMetricsEntry> = node_list
            .items
            .iter()
            .map(|obj| {
                let name = obj.metadata.name.clone().unwrap_or_default();
                let usage = obj.data.get("usage");
                let cpu = usage
                    .and_then(|u| u.get("cpu"))
                    .and_then(|v| v.as_str())
                    .map(parse_cpu_cores)
                    .unwrap_or(0.0);
                let mem = usage
                    .and_then(|u| u.get("memory"))
                    .and_then(|v| v.as_str())
                    .map(parse_memory_bytes)
                    .unwrap_or(0.0);
                total_cpu += cpu;
                total_mem += mem;
                NodeMetricsEntry {
                    name,
                    cpu_cores: cpu,
                    memory_bytes: mem,
                }
            })
            .collect();

        // Pod metrics + spec enrichment
        let pod_metrics: Vec<PodMetricsEntry> = metrics_list
            .items
            .iter()
            .map(|obj| {
                let name = obj.metadata.name.clone().unwrap_or_default();
                let namespace = obj.metadata.namespace.clone().unwrap_or_default();
                let containers = obj.data.get("containers").and_then(|c| c.as_array());
                let (mut cpu, mut mem) = (0.0f64, 0.0f64);
                if let Some(cs) = containers {
                    for c in cs {
                        let usage = c.get("usage");
                        cpu += usage
                            .and_then(|u| u.get("cpu"))
                            .and_then(|v| v.as_str())
                            .map(parse_cpu_cores)
                            .unwrap_or(0.0);
                        mem += usage
                            .and_then(|u| u.get("memory"))
                            .and_then(|v| v.as_str())
                            .map(parse_memory_bytes)
                            .unwrap_or(0.0);
                    }
                }

                let key = (name.clone(), namespace.clone());
                let (spec, owner, owner_kind) = spec_map
                    .remove(&key)
                    .unwrap_or_else(|| (PodResourceSpec::default(), name.clone(), "Pod".into()));

                PodMetricsEntry {
                    namespace,
                    cpu_cores: cpu,
                    memory_bytes: mem,
                    spec,
                    owner,
                    owner_kind,
                }
            })
            .collect();

        // Workload-level aggregation
        let mut wl_map: std::collections::HashMap<(String, String, String), WorkloadMetrics> =
            std::collections::HashMap::new();
        for pm in &pod_metrics {
            let key = (pm.owner.clone(), pm.namespace.clone(), pm.owner_kind.clone());
            let wl = wl_map.entry(key).or_insert(WorkloadMetrics {
                name: pm.owner.clone(),
                namespace: pm.namespace.clone(),
                kind: pm.owner_kind.clone(),
                pod_count: 0,
                cpu_usage: 0.0,
                mem_usage: 0.0,
                cpu_request: 0.0,
                cpu_limit: 0.0,
                mem_request: 0.0,
                mem_limit: 0.0,
            });
            wl.pod_count += 1;
            wl.cpu_usage += pm.cpu_cores;
            wl.mem_usage += pm.memory_bytes;
            wl.cpu_request += pm.spec.cpu_request;
            wl.cpu_limit += pm.spec.cpu_limit;
            wl.mem_request += pm.spec.mem_request;
            wl.mem_limit += pm.spec.mem_limit;
        }
        let mut workload_metrics: Vec<WorkloadMetrics> = wl_map.into_values().collect();
        workload_metrics.sort_by(|a, b| {
            b.cpu_usage
                .partial_cmp(&a.cpu_usage)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        MetricsData {
            available: true,
            node_metrics,
            pod_metrics,
            workload_metrics,
            total_cpu_usage: total_cpu,
            total_memory_usage: total_mem,
        }
    }

    pub async fn list_resources(&self, rt: ResourceType) -> Result<Vec<ResourceEntry>> {
        match rt {
            | ResourceType::Deployment => self.list_typed::<Deployment>(Self::map_deployment).await,
            | ResourceType::StatefulSet => self.list_typed::<StatefulSet>(Self::map_statefulset).await,
            | ResourceType::DaemonSet => self.list_typed::<DaemonSet>(Self::map_daemonset).await,
            | ResourceType::ReplicaSet => self.list_typed::<ReplicaSet>(Self::map_replicaset).await,
            | ResourceType::Pod => self.list_typed::<Pod>(Self::map_pod).await,
            | ResourceType::CronJob => self.list_typed::<CronJob>(Self::map_cronjob).await,
            | ResourceType::Job => self.list_typed::<Job>(Self::map_job).await,
            | ResourceType::HorizontalPodAutoscaler => self.list_typed::<HorizontalPodAutoscaler>(Self::map_hpa).await,
            | ResourceType::ConfigMap => self.list_typed::<ConfigMap>(Self::map_configmap).await,
            | ResourceType::Secret => self.list_typed::<Secret>(Self::map_secret).await,
            | ResourceType::Service => self.list_typed::<Service>(Self::map_service).await,
            | ResourceType::Ingress => self.list_typed::<Ingress>(Self::map_ingress).await,
            | ResourceType::Endpoints => self.list_typed::<Endpoints>(Self::map_endpoints).await,
            | ResourceType::NetworkPolicy => self.list_typed::<NetworkPolicy>(Self::map_network_policy).await,
            | ResourceType::PersistentVolumeClaim => self.list_typed::<PersistentVolumeClaim>(Self::map_pvc).await,
            | ResourceType::PersistentVolume => self.list_cluster::<PersistentVolume>(Self::map_pv).await,
            | ResourceType::StorageClass => self.list_cluster::<StorageClass>(Self::map_storage_class).await,
            | ResourceType::ServiceAccount => self.list_typed::<ServiceAccount>(Self::map_service_account).await,
            | ResourceType::Role => self.list_typed::<Role>(Self::map_role).await,
            | ResourceType::RoleBinding => self.list_typed::<RoleBinding>(Self::map_role_binding).await,
            | ResourceType::ClusterRole => self.list_cluster::<ClusterRole>(Self::map_cluster_role).await,
            | ResourceType::ClusterRoleBinding => {
                self.list_cluster::<ClusterRoleBinding>(Self::map_cluster_role_binding)
                    .await
            },
            | ResourceType::Node => self.list_cluster::<Node>(Self::map_node).await,
            | ResourceType::Namespace => self.list_cluster::<Namespace>(Self::map_namespace).await,
            | ResourceType::Event => self.list_typed::<Event>(Self::map_event).await,
            | ResourceType::CustomResourceDefinition => {
                self.list_cluster::<CustomResourceDefinition>(Self::map_crd).await
            },
        }
    }

    pub async fn get_resource(&self, rt: ResourceType, ns: &str, name: &str) -> Result<Value> {
        match rt {
            // Cluster-scoped
            | ResourceType::PersistentVolume => self.get_value_cluster::<PersistentVolume>(name).await,
            | ResourceType::StorageClass => self.get_value_cluster::<StorageClass>(name).await,
            | ResourceType::ClusterRole => self.get_value_cluster::<ClusterRole>(name).await,
            | ResourceType::ClusterRoleBinding => self.get_value_cluster::<ClusterRoleBinding>(name).await,
            | ResourceType::Node => self.get_value_cluster::<Node>(name).await,
            | ResourceType::Namespace => self.get_value_cluster::<Namespace>(name).await,
            // Namespaced
            | ResourceType::Deployment => self.get_value::<Deployment>(ns, name).await,
            | ResourceType::StatefulSet => self.get_value::<StatefulSet>(ns, name).await,
            | ResourceType::DaemonSet => self.get_value::<DaemonSet>(ns, name).await,
            | ResourceType::ReplicaSet => self.get_value::<ReplicaSet>(ns, name).await,
            | ResourceType::Pod => self.get_value::<Pod>(ns, name).await,
            | ResourceType::CronJob => self.get_value::<CronJob>(ns, name).await,
            | ResourceType::Job => self.get_value::<Job>(ns, name).await,
            | ResourceType::HorizontalPodAutoscaler => self.get_value::<HorizontalPodAutoscaler>(ns, name).await,
            | ResourceType::ConfigMap => self.get_value::<ConfigMap>(ns, name).await,
            | ResourceType::Secret => self.get_value::<Secret>(ns, name).await,
            | ResourceType::Service => self.get_value::<Service>(ns, name).await,
            | ResourceType::Ingress => self.get_value::<Ingress>(ns, name).await,
            | ResourceType::Endpoints => self.get_value::<Endpoints>(ns, name).await,
            | ResourceType::NetworkPolicy => self.get_value::<NetworkPolicy>(ns, name).await,
            | ResourceType::PersistentVolumeClaim => self.get_value::<PersistentVolumeClaim>(ns, name).await,
            | ResourceType::ServiceAccount => self.get_value::<ServiceAccount>(ns, name).await,
            | ResourceType::Role => self.get_value::<Role>(ns, name).await,
            | ResourceType::RoleBinding => self.get_value::<RoleBinding>(ns, name).await,
            | ResourceType::Event => self.get_value::<Event>(ns, name).await,
            | ResourceType::CustomResourceDefinition => self.get_value_cluster::<CustomResourceDefinition>(name).await,
        }
    }

    // -- Edit / replace resource -----------------------------------------------

    fn dynamic_api(
        &self,
        ar: &kube::api::ApiResource,
        cluster_scoped: bool,
        ns: &str,
    ) -> Api<kube::api::DynamicObject> {
        if cluster_scoped {
            Api::all_with(self.client.default.clone(), ar)
        } else {
            Api::namespaced_with(self.client.default.clone(), ns, ar)
        }
    }

    /// Replace a resource with edited YAML. Uses the kube dynamic API so it
    /// works for any resource type without per-type match arms.
    pub async fn replace_kind_yaml(&self, kind: &ResourceKind, ns: &str, name: &str, yaml: &str) -> Result<Value> {
        let obj: kube::api::DynamicObject = serde_yaml::from_str(yaml).context("Invalid YAML")?;
        let ar = kind.api_resource();
        let api = self.dynamic_api(&ar, kind.is_cluster_scoped(), ns);
        let pp = kube::api::PostParams::default();
        let result = api.replace(name, &pp, &obj).await.context("Failed to apply resource")?;
        Ok(serde_json::to_value(&result)?)
    }

    pub async fn patch_kind_metadata(
        &self,
        kind: &ResourceKind,
        ns: &str,
        name: &str,
        labels: Option<&serde_json::Map<String, Value>>,
        annotations: Option<&serde_json::Map<String, Value>>,
    ) -> Result<Value> {
        let ar = kind.api_resource();
        let api = self.dynamic_api(&ar, kind.is_cluster_scoped(), ns);
        let mut metadata = serde_json::Map::new();
        if let Some(l) = labels {
            metadata.insert("labels".into(), Value::Object(l.clone()));
        }
        if let Some(a) = annotations {
            metadata.insert("annotations".into(), Value::Object(a.clone()));
        }
        let patch = serde_json::json!({ "metadata": metadata });
        let pp = kube::api::PatchParams::default();
        let result = api
            .patch(name, &pp, &kube::api::Patch::Merge(&patch))
            .await
            .context("Failed to patch metadata")?;
        let val = serde_json::to_value(&result)?;
        Ok(val)
    }

    pub async fn restart_workload(&self, rt: ResourceType, ns: &str, name: &str) -> Result<()> {
        let ar = rt.api_resource();
        let api: Api<kube::api::DynamicObject> = Api::namespaced_with(self.client.default.clone(), ns, &ar);
        let now = Timestamp::now().to_string();
        let patch = serde_json::json!({
            "spec": {
                "template": {
                    "metadata": {
                        "annotations": {
                            "kubectl.kubernetes.io/restartedAt": now
                        }
                    }
                }
            }
        });
        let pp = kube::api::PatchParams::default();
        api.patch(name, &pp, &kube::api::Patch::Merge(&patch))
            .await
            .context("Failed to restart workload")?;
        Ok(())
    }

    /// Create a resource from YAML.
    ///
    /// Builtin types and already-discovered CRDs supply the plural and scope.
    /// Anything else is resolved with a single discovery call — guessing
    /// `kind + "s"` sends Ingress, StorageClass, and most CRDs to the wrong
    /// URL, and always using the namespaced client rejects cluster-scoped
    /// creates.
    pub async fn create_resource_yaml(&self, yaml: &str, crds: &[CrdInfo]) -> Result<Value> {
        let value: Value = serde_yaml::from_str(yaml).context("Invalid YAML")?;

        let api_version = value.get("apiVersion").and_then(|v| v.as_str()).unwrap_or("");
        let kind = value.get("kind").and_then(|v| v.as_str()).unwrap_or("");
        let namespace = value
            .get("metadata")
            .and_then(|m| m.get("namespace"))
            .and_then(|n| n.as_str())
            .unwrap_or("");

        if kind.is_empty() || api_version.is_empty() {
            anyhow::bail!("YAML must contain apiVersion and kind");
        }

        let (ar, cluster_scoped) = if let Some(rt) = ResourceType::from_gvk(api_version, kind) {
            (rt.api_resource(), rt.is_cluster_scoped())
        } else if let Some(crd) = crds.iter().find(|c| c.matches_gvk(api_version, kind)) {
            (crd.api_resource(), crd.is_cluster_scoped())
        } else {
            let (group, version) = api_version.split_once('/').unwrap_or(("", api_version));
            let gvk = kube::api::GroupVersionKind::gvk(group, version, kind);
            let (ar, caps) = kube::discovery::pinned_kind(&self.client.default, &gvk)
                .await
                .with_context(|| format!("Failed to discover {kind} ({api_version})"))?;
            (ar, caps.scope == kube::discovery::Scope::Cluster)
        };

        let obj: kube::api::DynamicObject = serde_yaml::from_str(yaml).context("Invalid YAML for DynamicObject")?;
        let api = if cluster_scoped {
            Api::all_with(self.client.default.clone(), &ar)
        } else {
            let effective_ns = if namespace.is_empty() {
                self.context.namespace.as_deref().unwrap_or("default")
            } else {
                namespace
            };
            Api::namespaced_with(self.client.default.clone(), effective_ns, &ar)
        };

        let pp = kube::api::PostParams::default();
        let result = api.create(&pp, &obj).await.context("Failed to create resource")?;
        Ok(serde_json::to_value(&result)?)
    }

    pub async fn delete_kind(&self, kind: &ResourceKind, ns: &str, name: &str) -> Result<()> {
        let ar = kind.api_resource();
        let api = self.dynamic_api(&ar, kind.is_cluster_scoped(), ns);
        let dp = kube::api::DeleteParams::default();
        api.delete(name, &dp).await.context("Failed to delete resource")?;
        Ok(())
    }

    pub async fn list_kind(&self, kind: &ResourceKind) -> Result<Vec<ResourceEntry>> {
        match kind {
            | ResourceKind::Builtin(rt) => self.list_resources(*rt).await,
            | ResourceKind::Custom(crd) => self.list_custom_resources(crd).await,
        }
    }

    pub async fn get_kind(&self, kind: &ResourceKind, ns: &str, name: &str) -> Result<Value> {
        match kind {
            | ResourceKind::Builtin(rt) => self.get_resource(*rt, ns, name).await,
            | ResourceKind::Custom(crd) => self.get_custom_resource(crd, ns, name).await,
        }
    }

    pub async fn scale_resource(&self, rt: ResourceType, ns: &str, name: &str, replicas: u32) -> Result<()> {
        let ar = rt.api_resource();
        let api: Api<kube::api::DynamicObject> = Api::namespaced_with(self.client.default.clone(), ns, &ar);
        let patch = serde_json::json!({ "spec": { "replicas": replicas } });
        let pp = kube::api::PatchParams::default();
        api.patch(name, &pp, &kube::api::Patch::Merge(&patch))
            .await
            .context("Failed to scale resource")?;
        Ok(())
    }

    /// Generate a default job name for a manually triggered CronJob.
    pub fn default_trigger_job_name(cronjob_name: &str) -> String {
        let ts = Timestamp::now().strftime("%Y%m%d%H%M%S").to_string();
        format!("{}-manual-{}", cronjob_name, ts)
    }

    /// Create a Job from a CronJob's jobTemplate (manual trigger).
    pub async fn trigger_cronjob(&self, ns: &str, name: &str, job_name: &str) -> Result<String> {
        let cj_api: Api<CronJob> = Api::namespaced(self.client.default.clone(), ns);
        let cj = cj_api.get(name).await.context("Failed to get CronJob")?;

        let job_template = cj
            .spec
            .as_ref()
            .map(|s| &s.job_template)
            .ok_or_else(|| anyhow::anyhow!("CronJob has no spec"))?;

        let mut job_meta = job_template.metadata.clone().unwrap_or_default();
        job_meta.name = Some(job_name.to_string());
        job_meta.namespace = Some(ns.to_string());
        // Add owner reference so the job shows up as related
        job_meta.owner_references = Some(vec![k8s_openapi::apimachinery::pkg::apis::meta::v1::OwnerReference {
            api_version: "batch/v1".to_string(),
            kind: "CronJob".to_string(),
            name: name.to_string(),
            uid: cj.metadata.uid.clone().unwrap_or_default(),
            controller: Some(true),
            block_owner_deletion: Some(true),
        }]);
        // Annotate as manually triggered
        let annotations = job_meta.annotations.get_or_insert_with(Default::default);
        annotations.insert("cronjob.kubernetes.io/instantiate".to_string(), "manual".to_string());

        let job = Job {
            metadata: job_meta,
            spec: job_template.spec.clone(),
            ..Default::default()
        };

        let job_api: Api<Job> = Api::namespaced(self.client.default.clone(), ns);
        let pp = kube::api::PostParams::default();
        job_api
            .create(&pp, &job)
            .await
            .context("Failed to create Job from CronJob")?;

        Ok(job_name.to_string())
    }

    /// Cordon a node (mark as unschedulable).
    pub async fn cordon_node(&self, name: &str) -> Result<()> {
        let api: Api<Node> = Api::all(self.client.default.clone());
        let patch = serde_json::json!({ "spec": { "unschedulable": true } });
        let pp = kube::api::PatchParams::default();
        api.patch(name, &pp, &kube::api::Patch::Merge(&patch))
            .await
            .context("Failed to cordon node")?;
        Ok(())
    }

    /// Uncordon a node (mark as schedulable).
    pub async fn uncordon_node(&self, name: &str) -> Result<()> {
        let api: Api<Node> = Api::all(self.client.default.clone());
        let patch = serde_json::json!({ "spec": { "unschedulable": false } });
        let pp = kube::api::PatchParams::default();
        api.patch(name, &pp, &kube::api::Patch::Merge(&patch))
            .await
            .context("Failed to uncordon node")?;
        Ok(())
    }

    /// Drain a node: cordon it, then evict all non-DaemonSet, non-mirror pods.
    pub async fn drain_node(&self, name: &str) -> Result<usize> {
        // Step 1: cordon
        self.cordon_node(name).await?;

        // Step 2: list pods on this node
        let pod_api: Api<Pod> = Api::all(self.client.default.clone());
        let lp = ListParams::default().fields(&format!("spec.nodeName={}", name));
        let pods = pod_api.list(&lp).await.context("Failed to list pods on node")?;

        let mut evicted = 0;
        for pod in &pods.items {
            let meta = &pod.metadata;
            let pod_name = meta.name.as_deref().unwrap_or("");
            let ns = meta.namespace.as_deref().unwrap_or("default");

            // Skip DaemonSet-owned pods
            if let Some(owners) = &meta.owner_references {
                if owners.iter().any(|o| o.kind == "DaemonSet") {
                    continue;
                }
            }

            // Skip mirror pods (created by kubelet)
            if let Some(annotations) = &meta.annotations {
                if annotations.contains_key("kubernetes.io/config.mirror") {
                    continue;
                }
            }

            // Evict the pod
            let ns_api: Api<Pod> = Api::namespaced(self.client.default.clone(), ns);
            let ep = kube::api::EvictParams::default();
            match ns_api.evict(pod_name, &ep).await {
                | Ok(_) => evicted += 1,
                | Err(_) => {
                    // Continue — some pods may have PDBs blocking eviction
                },
            }
        }

        Ok(evicted)
    }

    pub async fn fetch_related_events(&self, ns: &str, resource_name: &str) -> Result<Vec<RelatedEvent>> {
        let api: Api<Event> = if ns.is_empty() {
            Api::all(self.client.default.clone())
        } else {
            Api::namespaced(self.client.default.clone(), ns)
        };
        let lp = ListParams::default().fields(&format!("regarding.name={}", resource_name));
        let list = api.list(&lp).await.context("Failed to fetch events")?;
        let mut events: Vec<RelatedEvent> = list
            .items
            .iter()
            .map(|e| {
                let last_seen = e
                    .event_time
                    .as_ref()
                    .map(|t| {
                        let dur = jiff::Timestamp::now().duration_since(t.0);
                        format_duration(dur)
                    })
                    .or_else(|| e.deprecated_last_timestamp.as_ref().map(|t| format_age(Some(t))))
                    .unwrap_or_else(|| "-".into());
                let count = e.series.as_ref().map(|s| s.count).or(e.deprecated_count).unwrap_or(1);
                RelatedEvent {
                    type_: e.type_.clone().unwrap_or_default(),
                    reason: e.reason.clone().unwrap_or_default(),
                    message: e.note.clone().unwrap_or_default(),
                    count,
                    last_seen,
                }
            })
            .collect();
        events.reverse();
        Ok(events)
    }

    pub async fn fetch_related_resources(
        &self,
        rt: ResourceType,
        ns: &str,
        name: &str,
        value: &Value,
    ) -> Vec<RelatedResource> {
        let mut related = Vec::new();

        // Owner references (universal — all resource types)
        Self::add_owner_refs(&mut related, value, ns);

        match rt {
            | ResourceType::Deployment => {
                self.add_replicasets_for_deployment(&mut related, ns, value).await;
                self.add_pods_by_selector(&mut related, ns, value).await;
                self.add_hpa_for_workload(&mut related, ns, name).await;
                Self::add_service_account(&mut related, value, ns, true);
            },
            | ResourceType::StatefulSet => {
                self.add_pods_by_selector(&mut related, ns, value).await;
                self.add_hpa_for_workload(&mut related, ns, name).await;
                Self::add_service_account(&mut related, value, ns, true);
                if let Some(templates) = value
                    .get("spec")
                    .and_then(|s| s.get("volumeClaimTemplates"))
                    .and_then(|v| v.as_array())
                {
                    for tpl in templates {
                        if let Some(pvc_name) = tpl.get("metadata").and_then(|m| m.get("name")).and_then(|n| n.as_str())
                        {
                            Self::push_unique(
                                &mut related,
                                ResourceType::PersistentVolumeClaim,
                                pvc_name,
                                ns,
                                "template",
                                "Storage",
                            );
                        }
                    }
                }
            },
            | ResourceType::DaemonSet => {
                self.add_pods_by_selector(&mut related, ns, value).await;
                Self::add_service_account(&mut related, value, ns, true);
            },
            | ResourceType::ReplicaSet => {
                self.add_pods_by_selector(&mut related, ns, value).await;
                Self::add_service_account(&mut related, value, ns, true);
            },
            | ResourceType::Pod => {
                Self::add_service_account(&mut related, value, ns, false);
                if let Some(node) = value
                    .get("spec")
                    .and_then(|s| s.get("nodeName"))
                    .and_then(|n| n.as_str())
                {
                    Self::push_unique(&mut related, ResourceType::Node, node, "", "scheduled", "Cluster");
                }
            },
            | ResourceType::Service => {
                Self::push_unique(&mut related, ResourceType::Endpoints, name, ns, "endpoints", "Network");
                self.add_pods_by_service_selector(&mut related, ns, value).await;
            },
            | ResourceType::Ingress => {
                if let Some(rules) = value
                    .get("spec")
                    .and_then(|s| s.get("rules"))
                    .and_then(|r| r.as_array())
                {
                    for rule in rules {
                        if let Some(paths) = rule.get("http").and_then(|h| h.get("paths")).and_then(|p| p.as_array()) {
                            for path in paths {
                                if let Some(svc) = path
                                    .get("backend")
                                    .and_then(|b| b.get("service"))
                                    .and_then(|s| s.get("name"))
                                    .and_then(|n| n.as_str())
                                {
                                    Self::push_unique(
                                        &mut related,
                                        ResourceType::Service,
                                        svc,
                                        ns,
                                        "backend",
                                        "Network",
                                    );
                                }
                            }
                        }
                    }
                }
                if let Some(tls) = value.get("spec").and_then(|s| s.get("tls")).and_then(|t| t.as_array()) {
                    for entry in tls {
                        if let Some(secret) = entry.get("secretName").and_then(|s| s.as_str()) {
                            Self::push_unique(&mut related, ResourceType::Secret, secret, ns, "tls", "Config");
                        }
                    }
                }
            },
            | ResourceType::Job => {
                self.add_pods_by_label(&mut related, ns, &format!("job-name={}", name))
                    .await;
                Self::add_service_account(&mut related, value, ns, true);
            },
            | ResourceType::CronJob => {
                let job_api: Api<Job> = Api::namespaced(self.client.default.clone(), ns);
                if let Ok(jobs) = job_api.list(&ListParams::default()).await {
                    for job in &jobs.items {
                        let is_owned = job
                            .metadata
                            .owner_references
                            .as_ref()
                            .map(|refs| refs.iter().any(|r| r.kind == "CronJob" && r.name == name))
                            .unwrap_or(false);
                        if is_owned {
                            let ts = job
                                .metadata
                                .creation_timestamp
                                .as_ref()
                                .map(|t| t.0.to_string())
                                .unwrap_or_default();
                            let status = if job.status.as_ref().and_then(|s| s.succeeded).unwrap_or(0) > 0 {
                                "Succeeded"
                            } else if job.status.as_ref().and_then(|s| s.active).unwrap_or(0) > 0 {
                                "Active"
                            } else {
                                "Unknown"
                            };
                            related.push(RelatedResource {
                                resource_type: ResourceType::Job,
                                name: meta_name(&job.metadata),
                                namespace: ns.to_string(),
                                info: status.to_string(),
                                category: ResourceType::Job.display_name(),
                                sort_key: ts,
                            });
                        }
                    }
                }
                // SA from jobTemplate
                if let Some(sa) = value
                    .get("spec")
                    .and_then(|s| s.get("jobTemplate"))
                    .and_then(|j| j.get("spec"))
                    .and_then(|s| s.get("template"))
                    .and_then(|t| t.get("spec"))
                    .and_then(|s| s.get("serviceAccountName"))
                    .and_then(|n| n.as_str())
                {
                    Self::push_unique(
                        &mut related,
                        ResourceType::ServiceAccount,
                        sa,
                        ns,
                        "serviceAccount",
                        "RBAC",
                    );
                }
            },
            | ResourceType::HorizontalPodAutoscaler => {
                if let Some(target_ref) = value.get("spec").and_then(|s| s.get("scaleTargetRef")) {
                    let kind = target_ref.get("kind").and_then(|k| k.as_str()).unwrap_or("");
                    let tname = target_ref.get("name").and_then(|n| n.as_str()).unwrap_or("");
                    let trt = match kind {
                        | "Deployment" => Some(ResourceType::Deployment),
                        | "StatefulSet" => Some(ResourceType::StatefulSet),
                        | "ReplicaSet" => Some(ResourceType::ReplicaSet),
                        | _ => None,
                    };
                    if let Some(trt) = trt {
                        Self::push_unique(&mut related, trt, tname, ns, "scale target", "Workloads");
                    }
                }
            },
            | ResourceType::PersistentVolumeClaim => {
                if let Some(vol) = value
                    .get("spec")
                    .and_then(|s| s.get("volumeName"))
                    .and_then(|v| v.as_str())
                {
                    Self::push_unique(
                        &mut related,
                        ResourceType::PersistentVolume,
                        vol,
                        "",
                        "bound",
                        "Storage",
                    );
                }
                if let Some(sc) = value
                    .get("spec")
                    .and_then(|s| s.get("storageClassName"))
                    .and_then(|v| v.as_str())
                {
                    Self::push_unique(
                        &mut related,
                        ResourceType::StorageClass,
                        sc,
                        "",
                        "storageClass",
                        "Storage",
                    );
                }
            },
            | ResourceType::PersistentVolume => {
                if let Some(claim) = value.get("spec").and_then(|s| s.get("claimRef")) {
                    let cn = claim.get("name").and_then(|n| n.as_str()).unwrap_or("");
                    let cns = claim.get("namespace").and_then(|n| n.as_str()).unwrap_or("");
                    if !cn.is_empty() {
                        Self::push_unique(
                            &mut related,
                            ResourceType::PersistentVolumeClaim,
                            cn,
                            cns,
                            "claim",
                            "Storage",
                        );
                    }
                }
                if let Some(sc) = value
                    .get("spec")
                    .and_then(|s| s.get("storageClassName"))
                    .and_then(|v| v.as_str())
                {
                    Self::push_unique(
                        &mut related,
                        ResourceType::StorageClass,
                        sc,
                        "",
                        "storageClass",
                        "Storage",
                    );
                }
            },
            | ResourceType::ServiceAccount => {
                // Token secrets
                if let Some(secrets) = value.get("secrets").and_then(|s| s.as_array()) {
                    for secret in secrets {
                        if let Some(sname) = secret.get("name").and_then(|n| n.as_str()) {
                            Self::push_unique(&mut related, ResourceType::Secret, sname, ns, "token", "x");
                        }
                    }
                }
                // RoleBindings referencing this SA
                let rb_api: Api<RoleBinding> = Api::namespaced(self.client.default.clone(), ns);
                if let Ok(list) = rb_api.list(&ListParams::default()).await {
                    for rb in &list.items {
                        let refs_sa = rb
                            .subjects
                            .as_ref()
                            .map(|subjects| subjects.iter().any(|s| s.kind == "ServiceAccount" && s.name == name))
                            .unwrap_or(false);
                        if refs_sa {
                            Self::push_unique(
                                &mut related,
                                ResourceType::RoleBinding,
                                &meta_name(&rb.metadata),
                                ns,
                                "binds SA",
                                "x",
                            );
                        }
                    }
                }
                // ClusterRoleBindings referencing this SA
                let crb_api: Api<ClusterRoleBinding> = Api::all(self.client.default.clone());
                if let Ok(list) = crb_api.list(&ListParams::default()).await {
                    for crb in &list.items {
                        let refs_sa = crb
                            .subjects
                            .as_ref()
                            .map(|subjects| {
                                subjects.iter().any(|s| {
                                    s.kind == "ServiceAccount" && s.name == name && s.namespace.as_deref() == Some(ns)
                                })
                            })
                            .unwrap_or(false);
                        if refs_sa {
                            Self::push_unique(
                                &mut related,
                                ResourceType::ClusterRoleBinding,
                                &meta_name(&crb.metadata),
                                "",
                                "binds SA",
                                "x",
                            );
                        }
                    }
                }
                // Workloads using this SA
                self.add_workloads_using_sa(&mut related, ns, name).await;
            },
            | ResourceType::RoleBinding | ResourceType::ClusterRoleBinding => {
                if let Some(role_ref) = value.get("roleRef") {
                    let kind = role_ref.get("kind").and_then(|k| k.as_str()).unwrap_or("");
                    let rname = role_ref.get("name").and_then(|n| n.as_str()).unwrap_or("");
                    let rrt = match kind {
                        | "Role" => Some(ResourceType::Role),
                        | "ClusterRole" => Some(ResourceType::ClusterRole),
                        | _ => None,
                    };
                    if let Some(rrt) = rrt {
                        Self::push_unique(&mut related, rrt, rname, ns, "bound role", "RBAC");
                    }
                }
                if let Some(subjects) = value.get("subjects").and_then(|s| s.as_array()) {
                    for subj in subjects {
                        if subj.get("kind").and_then(|k| k.as_str()) == Some("ServiceAccount") {
                            let sa_name = subj.get("name").and_then(|n| n.as_str()).unwrap_or("");
                            let sa_ns = subj.get("namespace").and_then(|n| n.as_str()).unwrap_or(ns);
                            Self::push_unique(
                                &mut related,
                                ResourceType::ServiceAccount,
                                sa_name,
                                sa_ns,
                                "subject",
                                "RBAC",
                            );
                        }
                    }
                }
            },
            | ResourceType::NetworkPolicy => {
                self.add_pods_by_network_policy_selector(&mut related, ns, value).await;
            },
            | ResourceType::Secret | ResourceType::ConfigMap => {
                self.add_workloads_referencing(&mut related, rt, ns, name).await;
            },
            | _ => {},
        }

        // Extract mounted volumes + env refs from pod templates
        let template_spec = match rt {
            | ResourceType::CronJob => {
                value
                    .get("spec")
                    .and_then(|s| s.get("jobTemplate"))
                    .and_then(|j| j.get("spec"))
                    .and_then(|s| s.get("template"))
                    .and_then(|t| t.get("spec"))
            },
            | ResourceType::Deployment
            | ResourceType::StatefulSet
            | ResourceType::DaemonSet
            | ResourceType::Job
            | ResourceType::ReplicaSet => {
                value
                    .get("spec")
                    .and_then(|s| s.get("template"))
                    .and_then(|t| t.get("spec"))
            },
            | ResourceType::Pod => value.get("spec"),
            | _ => None,
        };
        if let Some(spec) = template_spec {
            if let Some(volumes) = spec.get("volumes").and_then(|v| v.as_array()) {
                Self::extract_volume_refs(&mut related, volumes, ns);
            }
            if let Some(containers) = spec.get("containers").and_then(|c| c.as_array()) {
                Self::extract_env_refs(&mut related, containers, ns);
            }
            if let Some(init_containers) = spec.get("initContainers").and_then(|c| c.as_array()) {
                Self::extract_env_refs(&mut related, init_containers, ns);
            }
        }

        // Sort: by resource type category order, then by sort_key (timestamp)
        let type_order = |c: &str| -> u8 {
            match c {
                // Workloads
                | "Deployments" => 0,
                | "ReplicaSets" => 1,
                | "StatefulSets" => 2,
                | "DaemonSets" => 3,
                | "Pods" => 4,
                | "Jobs" => 5,
                | "CronJobs" => 6,
                | "HPAs" => 7,
                // Network
                | "Services" => 10,
                | "Ingresses" => 11,
                | "Endpoints" => 12,
                | "NetworkPolicies" => 13,
                // Config
                | "ConfigMaps" => 20,
                | "Secrets" => 21,
                // Storage
                | "PVCs" => 30,
                | "PVs" => 31,
                | "StorageClasses" => 32,
                // RBAC
                | "ServiceAccounts" => 40,
                | "Roles" => 41,
                | "RoleBindings" => 42,
                | "ClusterRoles" => 43,
                | "ClusterRoleBindings" => 44,
                // Cluster
                | "Nodes" => 50,
                | "Namespaces" => 51,
                | _ => 99,
            }
        };
        related.sort_by(|a, b| {
            type_order(a.category).cmp(&type_order(b.category)).then_with(|| {
                if a.sort_key.is_empty() && b.sort_key.is_empty() {
                    a.name.cmp(&b.name)
                } else {
                    b.sort_key.cmp(&a.sort_key)
                }
            })
        });

        related
    }

    fn add_owner_refs(related: &mut Vec<RelatedResource>, value: &Value, ns: &str) {
        if let Some(owners) = value
            .get("metadata")
            .and_then(|m| m.get("ownerReferences"))
            .and_then(|o| o.as_array())
        {
            for owner in owners {
                let kind = owner.get("kind").and_then(|k| k.as_str()).unwrap_or("");
                let oname = owner.get("name").and_then(|n| n.as_str()).unwrap_or("");
                let owner_rt = match kind {
                    | "Deployment" => Some(ResourceType::Deployment),
                    | "ReplicaSet" => Some(ResourceType::ReplicaSet),
                    | "StatefulSet" => Some(ResourceType::StatefulSet),
                    | "DaemonSet" => Some(ResourceType::DaemonSet),
                    | "Job" => Some(ResourceType::Job),
                    | "CronJob" => Some(ResourceType::CronJob),
                    | "Node" => Some(ResourceType::Node),
                    | _ => None,
                };
                if let Some(ort) = owner_rt {
                    related.push(RelatedResource {
                        resource_type: ort,
                        name: oname.to_string(),
                        namespace: ns.to_string(),
                        info: "owner".to_string(),
                        category: ort.display_name(),
                        sort_key: String::new(),
                    });
                }
            }
        }
    }

    async fn add_replicasets_for_deployment(&self, related: &mut Vec<RelatedResource>, ns: &str, value: &Value) {
        let selector = value
            .get("spec")
            .and_then(|s| s.get("selector"))
            .and_then(|s| s.get("matchLabels"))
            .and_then(|m| m.as_object());
        if let Some(labels) = selector {
            let sel = labels
                .iter()
                .map(|(k, v)| format!("{}={}", k, v.as_str().unwrap_or("")))
                .collect::<Vec<_>>()
                .join(",");
            let api: Api<ReplicaSet> = Api::namespaced(self.client.default.clone(), ns);
            if let Ok(list) = api.list(&ListParams::default().labels(&sel)).await {
                for rs in &list.items {
                    let rs_name = meta_name(&rs.metadata);
                    let replicas = rs.status.as_ref().map(|s| s.replicas).unwrap_or(0);
                    let ready = rs.status.as_ref().and_then(|s| s.ready_replicas).unwrap_or(0);
                    let revision = rs
                        .metadata
                        .annotations
                        .as_ref()
                        .and_then(|a| a.get("deployment.kubernetes.io/revision"))
                        .map(|s| s.as_str())
                        .unwrap_or("0");
                    let ts = rs
                        .metadata
                        .creation_timestamp
                        .as_ref()
                        .map(|t| t.0.to_string())
                        .unwrap_or_default();
                    related.push(RelatedResource {
                        resource_type: ResourceType::ReplicaSet,
                        name: rs_name,
                        namespace: ns.to_string(),
                        info: format!("rev:{} ready:{}/{}", revision, ready, replicas),
                        category: ResourceType::ReplicaSet.display_name(),
                        sort_key: ts,
                    });
                }
            }
        }
    }

    async fn add_pods_by_selector(&self, related: &mut Vec<RelatedResource>, ns: &str, value: &Value) {
        let selector = value
            .get("spec")
            .and_then(|s| s.get("selector"))
            .and_then(|s| s.get("matchLabels"))
            .and_then(|m| m.as_object());
        if let Some(labels) = selector {
            let sel = labels
                .iter()
                .map(|(k, v)| format!("{}={}", k, v.as_str().unwrap_or("")))
                .collect::<Vec<_>>()
                .join(",");
            self.add_pods_by_label(related, ns, &sel).await;
        }
    }

    async fn add_pods_by_service_selector(&self, related: &mut Vec<RelatedResource>, ns: &str, value: &Value) {
        if let Some(selector) = value
            .get("spec")
            .and_then(|s| s.get("selector"))
            .and_then(|s| s.as_object())
        {
            let sel = selector
                .iter()
                .map(|(k, v)| format!("{}={}", k, v.as_str().unwrap_or("")))
                .collect::<Vec<_>>()
                .join(",");
            self.add_pods_by_label(related, ns, &sel).await;
        }
    }

    async fn add_pods_by_network_policy_selector(&self, related: &mut Vec<RelatedResource>, ns: &str, value: &Value) {
        if let Some(selector) = value
            .get("spec")
            .and_then(|s| s.get("podSelector"))
            .and_then(|s| s.get("matchLabels"))
            .and_then(|m| m.as_object())
        {
            let sel = selector
                .iter()
                .map(|(k, v)| format!("{}={}", k, v.as_str().unwrap_or("")))
                .collect::<Vec<_>>()
                .join(",");
            self.add_pods_by_label(related, ns, &sel).await;
        }
    }

    async fn add_pods_by_label(&self, related: &mut Vec<RelatedResource>, ns: &str, label_sel: &str) {
        let api: Api<Pod> = Api::namespaced(self.client.default.clone(), ns);
        if let Ok(list) = api.list(&ListParams::default().labels(label_sel)).await {
            for pod in &list.items {
                let phase = pod
                    .status
                    .as_ref()
                    .and_then(|s| s.phase.as_deref())
                    .unwrap_or("Unknown");
                let ts = pod
                    .metadata
                    .creation_timestamp
                    .as_ref()
                    .map(|t| t.0.to_string())
                    .unwrap_or_default();
                related.push(RelatedResource {
                    resource_type: ResourceType::Pod,
                    name: meta_name(&pod.metadata),
                    namespace: ns.to_string(),
                    info: phase.to_string(),
                    category: ResourceType::Pod.display_name(),
                    sort_key: ts,
                });
            }
        }
    }

    async fn add_hpa_for_workload(&self, related: &mut Vec<RelatedResource>, ns: &str, name: &str) {
        let api: Api<HorizontalPodAutoscaler> = Api::namespaced(self.client.default.clone(), ns);
        if let Ok(list) = api.list(&ListParams::default()).await {
            for hpa in &list.items {
                let target = hpa
                    .spec
                    .as_ref()
                    .map(|s| s.scale_target_ref.name.as_str())
                    .unwrap_or("");
                if target == name {
                    let min = hpa.spec.as_ref().and_then(|s| s.min_replicas).unwrap_or(0);
                    let max = hpa.spec.as_ref().map(|s| s.max_replicas).unwrap_or(0);
                    related.push(RelatedResource {
                        resource_type: ResourceType::HorizontalPodAutoscaler,
                        name: meta_name(&hpa.metadata),
                        namespace: ns.to_string(),
                        info: format!("min:{} max:{}", min, max),
                        category: ResourceType::HorizontalPodAutoscaler.display_name(),
                        sort_key: String::new(),
                    });
                }
            }
        }
    }

    fn add_service_account(related: &mut Vec<RelatedResource>, value: &Value, ns: &str, from_template: bool) {
        let spec = if from_template {
            value
                .get("spec")
                .and_then(|s| s.get("template"))
                .and_then(|t| t.get("spec"))
        } else {
            value.get("spec")
        };
        if let Some(sa) = spec.and_then(|s| s.get("serviceAccountName")).and_then(|n| n.as_str()) {
            Self::push_unique(related, ResourceType::ServiceAccount, sa, ns, "serviceAccount", "RBAC");
        }
        // Also check serviceAccount field (deprecated but still used)
        if let Some(sa) = spec.and_then(|s| s.get("serviceAccount")).and_then(|n| n.as_str()) {
            Self::push_unique(related, ResourceType::ServiceAccount, sa, ns, "serviceAccount", "RBAC");
        }
    }

    fn push_unique(
        related: &mut Vec<RelatedResource>,
        rt: ResourceType,
        name: &str,
        ns: &str,
        info: &str,
        _category: &'static str,
    ) {
        if !related.iter().any(|r| r.resource_type == rt && r.name == name) {
            related.push(RelatedResource {
                resource_type: rt,
                name: name.to_string(),
                namespace: ns.to_string(),
                info: info.to_string(),
                category: rt.display_name(),
                sort_key: String::new(),
            });
        }
    }

    /// Find workloads (Deployments, StatefulSets, DaemonSets, Jobs, CronJobs)
    /// that reference a given Secret or ConfigMap by name.
    async fn add_workloads_referencing(
        &self,
        related: &mut Vec<RelatedResource>,
        ref_type: ResourceType,
        ns: &str,
        ref_name: &str,
    ) {
        let is_secret = ref_type == ResourceType::Secret;

        // Helper: check if a pod spec references the target name
        let spec_references = |spec: &Value| -> bool {
            // Check volumes
            if let Some(volumes) = spec.get("volumes").and_then(|v| v.as_array()) {
                for vol in volumes {
                    if is_secret {
                        if vol
                            .get("secret")
                            .and_then(|s| s.get("secretName"))
                            .and_then(|n| n.as_str())
                            == Some(ref_name)
                        {
                            return true;
                        }
                        if let Some(sources) = vol
                            .get("projected")
                            .and_then(|p| p.get("sources"))
                            .and_then(|s| s.as_array())
                        {
                            for src in sources {
                                if src.get("secret").and_then(|s| s.get("name")).and_then(|n| n.as_str())
                                    == Some(ref_name)
                                {
                                    return true;
                                }
                            }
                        }
                    } else {
                        if vol
                            .get("configMap")
                            .and_then(|c| c.get("name"))
                            .and_then(|n| n.as_str())
                            == Some(ref_name)
                        {
                            return true;
                        }
                        if let Some(sources) = vol
                            .get("projected")
                            .and_then(|p| p.get("sources"))
                            .and_then(|s| s.as_array())
                        {
                            for src in sources {
                                if src
                                    .get("configMap")
                                    .and_then(|c| c.get("name"))
                                    .and_then(|n| n.as_str())
                                    == Some(ref_name)
                                {
                                    return true;
                                }
                            }
                        }
                    }
                }
            }
            // Check containers env refs
            for container_key in &["containers", "initContainers"] {
                if let Some(containers) = spec.get(container_key).and_then(|c| c.as_array()) {
                    for container in containers {
                        if let Some(env_from) = container.get("envFrom").and_then(|e| e.as_array()) {
                            for src in env_from {
                                let ref_field = if is_secret { "secretRef" } else { "configMapRef" };
                                if src.get(ref_field).and_then(|r| r.get("name")).and_then(|n| n.as_str())
                                    == Some(ref_name)
                                {
                                    return true;
                                }
                            }
                        }
                        if let Some(env) = container.get("env").and_then(|e| e.as_array()) {
                            for var in env {
                                if let Some(vf) = var.get("valueFrom") {
                                    let ref_field = if is_secret { "secretKeyRef" } else { "configMapKeyRef" };
                                    if vf.get(ref_field).and_then(|r| r.get("name")).and_then(|n| n.as_str())
                                        == Some(ref_name)
                                    {
                                        return true;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            false
        };

        // Search Deployments
        let dep_api: Api<Deployment> = Api::namespaced(self.client.default.clone(), ns);
        if let Ok(list) = dep_api.list(&ListParams::default()).await {
            for dep in &list.items {
                let val = serde_json::to_value(dep).unwrap_or_default();
                if let Some(spec) = val
                    .get("spec")
                    .and_then(|s| s.get("template"))
                    .and_then(|t| t.get("spec"))
                {
                    if spec_references(spec) {
                        Self::push_unique(
                            related,
                            ResourceType::Deployment,
                            &meta_name(&dep.metadata),
                            ns,
                            "references",
                            "Workloads",
                        );
                    }
                }
            }
        }
        // Search StatefulSets
        let ss_api: Api<StatefulSet> = Api::namespaced(self.client.default.clone(), ns);
        if let Ok(list) = ss_api.list(&ListParams::default()).await {
            for ss in &list.items {
                let val = serde_json::to_value(ss).unwrap_or_default();
                if let Some(spec) = val
                    .get("spec")
                    .and_then(|s| s.get("template"))
                    .and_then(|t| t.get("spec"))
                {
                    if spec_references(spec) {
                        Self::push_unique(
                            related,
                            ResourceType::StatefulSet,
                            &meta_name(&ss.metadata),
                            ns,
                            "references",
                            "Workloads",
                        );
                    }
                }
            }
        }
        // Search DaemonSets
        let ds_api: Api<DaemonSet> = Api::namespaced(self.client.default.clone(), ns);
        if let Ok(list) = ds_api.list(&ListParams::default()).await {
            for ds in &list.items {
                let val = serde_json::to_value(ds).unwrap_or_default();
                if let Some(spec) = val
                    .get("spec")
                    .and_then(|s| s.get("template"))
                    .and_then(|t| t.get("spec"))
                {
                    if spec_references(spec) {
                        Self::push_unique(
                            related,
                            ResourceType::DaemonSet,
                            &meta_name(&ds.metadata),
                            ns,
                            "references",
                            "Workloads",
                        );
                    }
                }
            }
        }
        // Search Jobs
        let job_api: Api<Job> = Api::namespaced(self.client.default.clone(), ns);
        if let Ok(list) = job_api.list(&ListParams::default()).await {
            for job in &list.items {
                let val = serde_json::to_value(job).unwrap_or_default();
                if let Some(spec) = val
                    .get("spec")
                    .and_then(|s| s.get("template"))
                    .and_then(|t| t.get("spec"))
                {
                    if spec_references(spec) {
                        Self::push_unique(
                            related,
                            ResourceType::Job,
                            &meta_name(&job.metadata),
                            ns,
                            "references",
                            "Workloads",
                        );
                    }
                }
            }
        }
        // Search CronJobs
        let cj_api: Api<CronJob> = Api::namespaced(self.client.default.clone(), ns);
        if let Ok(list) = cj_api.list(&ListParams::default()).await {
            for cj in &list.items {
                let val = serde_json::to_value(cj).unwrap_or_default();
                if let Some(spec) = val
                    .get("spec")
                    .and_then(|s| s.get("jobTemplate"))
                    .and_then(|j| j.get("spec"))
                    .and_then(|s| s.get("template"))
                    .and_then(|t| t.get("spec"))
                {
                    if spec_references(spec) {
                        Self::push_unique(
                            related,
                            ResourceType::CronJob,
                            &meta_name(&cj.metadata),
                            ns,
                            "references",
                            "Workloads",
                        );
                    }
                }
            }
        }
    }

    async fn add_workloads_using_sa(&self, related: &mut Vec<RelatedResource>, ns: &str, sa_name: &str) {
        let check_sa = |val: &Value| -> bool {
            val.get("spec")
                .and_then(|s| s.get("template"))
                .and_then(|t| t.get("spec"))
                .and_then(|s| s.get("serviceAccountName").or_else(|| s.get("serviceAccount")))
                .and_then(|n| n.as_str())
                == Some(sa_name)
        };
        let dep_api: Api<Deployment> = Api::namespaced(self.client.default.clone(), ns);
        if let Ok(list) = dep_api.list(&ListParams::default()).await {
            for dep in &list.items {
                let val = serde_json::to_value(dep).unwrap_or_default();
                if check_sa(&val) {
                    Self::push_unique(
                        related,
                        ResourceType::Deployment,
                        &meta_name(&dep.metadata),
                        ns,
                        "uses SA",
                        "x",
                    );
                }
            }
        }
        let ss_api: Api<StatefulSet> = Api::namespaced(self.client.default.clone(), ns);
        if let Ok(list) = ss_api.list(&ListParams::default()).await {
            for ss in &list.items {
                let val = serde_json::to_value(ss).unwrap_or_default();
                if check_sa(&val) {
                    Self::push_unique(
                        related,
                        ResourceType::StatefulSet,
                        &meta_name(&ss.metadata),
                        ns,
                        "uses SA",
                        "x",
                    );
                }
            }
        }
        let ds_api: Api<DaemonSet> = Api::namespaced(self.client.default.clone(), ns);
        if let Ok(list) = ds_api.list(&ListParams::default()).await {
            for ds in &list.items {
                let val = serde_json::to_value(ds).unwrap_or_default();
                if check_sa(&val) {
                    Self::push_unique(
                        related,
                        ResourceType::DaemonSet,
                        &meta_name(&ds.metadata),
                        ns,
                        "uses SA",
                        "x",
                    );
                }
            }
        }
        let job_api: Api<Job> = Api::namespaced(self.client.default.clone(), ns);
        if let Ok(list) = job_api.list(&ListParams::default()).await {
            for job in &list.items {
                let val = serde_json::to_value(job).unwrap_or_default();
                if check_sa(&val) {
                    Self::push_unique(
                        related,
                        ResourceType::Job,
                        &meta_name(&job.metadata),
                        ns,
                        "uses SA",
                        "x",
                    );
                }
            }
        }
    }

    fn extract_volume_refs(related: &mut Vec<RelatedResource>, volumes: &[Value], ns: &str) {
        for vol in volumes {
            if let Some(name) = vol
                .get("secret")
                .and_then(|s| s.get("secretName"))
                .and_then(|n| n.as_str())
            {
                Self::push_unique(related, ResourceType::Secret, name, ns, "mounted", "Config");
            }
            if let Some(name) = vol
                .get("configMap")
                .and_then(|c| c.get("name"))
                .and_then(|n| n.as_str())
            {
                Self::push_unique(related, ResourceType::ConfigMap, name, ns, "mounted", "Config");
            }
            if let Some(name) = vol
                .get("persistentVolumeClaim")
                .and_then(|p| p.get("claimName"))
                .and_then(|n| n.as_str())
            {
                Self::push_unique(
                    related,
                    ResourceType::PersistentVolumeClaim,
                    name,
                    ns,
                    "mounted",
                    "Storage",
                );
            }
            if let Some(name) = vol
                .get("projected")
                .and_then(|p| p.get("sources"))
                .and_then(|s| s.as_array())
            {
                for src in name {
                    if let Some(sname) = src.get("secret").and_then(|s| s.get("name")).and_then(|n| n.as_str()) {
                        Self::push_unique(related, ResourceType::Secret, sname, ns, "projected", "Config");
                    }
                    if let Some(cname) = src
                        .get("configMap")
                        .and_then(|c| c.get("name"))
                        .and_then(|n| n.as_str())
                    {
                        Self::push_unique(related, ResourceType::ConfigMap, cname, ns, "projected", "Config");
                    }
                }
            }
        }
    }

    fn extract_env_refs(related: &mut Vec<RelatedResource>, containers: &[Value], ns: &str) {
        for container in containers {
            if let Some(env_from) = container.get("envFrom").and_then(|e| e.as_array()) {
                for src in env_from {
                    if let Some(name) = src
                        .get("secretRef")
                        .and_then(|s| s.get("name"))
                        .and_then(|n| n.as_str())
                    {
                        Self::push_unique(related, ResourceType::Secret, name, ns, "envFrom", "Config");
                    }
                    if let Some(name) = src
                        .get("configMapRef")
                        .and_then(|c| c.get("name"))
                        .and_then(|n| n.as_str())
                    {
                        Self::push_unique(related, ResourceType::ConfigMap, name, ns, "envFrom", "Config");
                    }
                }
            }
            if let Some(env) = container.get("env").and_then(|e| e.as_array()) {
                for var in env {
                    if let Some(vf) = var.get("valueFrom") {
                        if let Some(name) = vf
                            .get("secretKeyRef")
                            .and_then(|s| s.get("name"))
                            .and_then(|n| n.as_str())
                        {
                            Self::push_unique(related, ResourceType::Secret, name, ns, "env", "Config");
                        }
                        if let Some(name) = vf
                            .get("configMapKeyRef")
                            .and_then(|c| c.get("name"))
                            .and_then(|n| n.as_str())
                        {
                            Self::push_unique(related, ResourceType::ConfigMap, name, ns, "env", "Config");
                        }
                    }
                }
            }
        }
    }

    // -- Log support -----------------------------------------------------------

    pub async fn find_pods(&self, rt: ResourceType, ns: &str, name: &str) -> Result<Vec<PodInfo>> {
        let api: Api<Pod> = Api::namespaced(self.client.default.clone(), ns);
        match rt {
            | ResourceType::Pod => {
                let pod = api.get(name).await?;
                let containers = pod
                    .spec
                    .as_ref()
                    .map(|s| s.containers.iter().map(|c| c.name.clone()).collect())
                    .unwrap_or_default();
                Ok(vec![PodInfo {
                    name: name.to_string(),
                    containers,
                }])
            },
            | ResourceType::Job => {
                let lp = ListParams::default().labels(&format!("job-name={}", name));
                self.pods_from_list(&api, &lp).await
            },
            | ResourceType::CronJob => {
                // Find latest Job owned by this CronJob, then find its pods
                let job_api: Api<Job> = Api::namespaced(self.client.default.clone(), ns);
                let jobs = job_api.list(&ListParams::default()).await?;
                let latest = jobs
                    .items
                    .iter()
                    .filter(|j| {
                        j.metadata
                            .owner_references
                            .as_ref()
                            .map(|refs| refs.iter().any(|r| r.kind == "CronJob" && r.name == name))
                            .unwrap_or(false)
                    })
                    .max_by_key(|j| j.metadata.creation_timestamp.as_ref().map(|t| t.0));
                match latest {
                    | Some(j) => {
                        let job_name = j.metadata.name.as_deref().unwrap_or("");
                        let lp = ListParams::default().labels(&format!("job-name={}", job_name));
                        self.pods_from_list(&api, &lp).await
                    },
                    | None => Ok(vec![]),
                }
            },
            | ResourceType::Deployment
            | ResourceType::StatefulSet
            | ResourceType::DaemonSet
            | ResourceType::ReplicaSet => {
                // Use the resource's spec.selector.matchLabels
                let val = self.get_resource(rt, ns, name).await?;
                let labels = val
                    .get("spec")
                    .and_then(|s| s.get("selector"))
                    .and_then(|s| s.get("matchLabels"))
                    .and_then(|m| m.as_object());
                match labels {
                    | Some(map) => {
                        let selector = map
                            .iter()
                            .map(|(k, v)| format!("{}={}", k, v.as_str().unwrap_or("")))
                            .collect::<Vec<_>>()
                            .join(",");
                        let lp = ListParams::default().labels(&selector);
                        self.pods_from_list(&api, &lp).await
                    },
                    | None => Ok(vec![]),
                }
            },
            | _ => Ok(vec![]),
        }
    }

    async fn pods_from_list(&self, api: &Api<Pod>, lp: &ListParams) -> Result<Vec<PodInfo>> {
        let list = api.list(lp).await?;
        Ok(list
            .items
            .iter()
            .map(|p| {
                PodInfo {
                    name: meta_name(&p.metadata),
                    containers: p
                        .spec
                        .as_ref()
                        .map(|s| s.containers.iter().map(|c| c.name.clone()).collect())
                        .unwrap_or_default(),
                }
            })
            .collect())
    }

    /// Fetch logs for a single pod/container.
    pub async fn fetch_logs(
        &self,
        ns: &str,
        pod: &str,
        container: &str,
        tail: i64,
        since_seconds: Option<i64>,
    ) -> Result<String> {
        let api: Api<Pod> = Api::namespaced(self.client.default.clone(), ns);
        let lp = kube::api::LogParams {
            container: Some(container.to_string()),
            tail_lines: Some(tail),
            timestamps: true,
            since_seconds,
            ..Default::default()
        };
        Ok(api.logs(pod, &lp).await?)
    }

    /// Fetch logs for multiple pod/container pairs and merge them with
    /// prefixes.  Returns structured `LogLine`s sorted by timestamp.
    pub async fn fetch_logs_multi(
        &self,
        ns: &str,
        pairs: &[(String, String)],
        tail: i64,
        since_seconds: Option<i64>,
    ) -> Result<Vec<crate::tui::logs::LogLine>> {
        use crate::tui::logs::LogLine;
        let mut all_lines = Vec::new();
        for (pod, container) in pairs {
            let prefix = format!("[{}/{}] ", pod, container);
            match self.fetch_logs(ns, pod, container, tail, since_seconds).await {
                | Ok(logs) => {
                    for line in logs.lines() {
                        all_lines.push(LogLine::parse(&format!("{}{}", prefix, line)));
                    }
                },
                | Err(_) => {
                    all_lines.push(LogLine::parse(&format!("{}(failed to fetch logs)", prefix)));
                },
            }
        }
        // Sort by timestamp — RFC3339 timestamps are lexicographically sortable.
        all_lines.sort_by(|a, b| a.timestamp.cmp(&b.timestamp));
        Ok(all_lines)
    }

    // -- Generic list helper -------------------------------------------------

    async fn list_typed<K>(&self, mapper: fn(&K) -> ResourceEntry) -> Result<Vec<ResourceEntry>>
    where
        K: kube::Resource<DynamicType=()>+serde::de::DeserializeOwned+Clone+std::fmt::Debug,
        K: kube::Resource<Scope=k8s_openapi::NamespaceResourceScope>,
    {
        let lp = ListParams::default();
        let list = match &self.context.namespace {
            | Some(ns) => Api::<K>::namespaced(self.client.default.clone(), ns).list(&lp).await?,
            | None => Api::<K>::all(self.client.default.clone()).list(&lp).await?,
        };
        Ok(list.items.iter().map(mapper).collect())
    }

    // -- Generic value fetch for any namespaced resource --------------------

    async fn get_value<K>(&self, ns: &str, name: &str) -> Result<Value>
    where
        K: kube::Resource<DynamicType=()>+serde::de::DeserializeOwned+serde::Serialize+Clone+std::fmt::Debug,
        K: kube::Resource<Scope=k8s_openapi::NamespaceResourceScope>,
    {
        let api: Api<K> = Api::namespaced(self.client.default.clone(), ns);
        let obj = api.get(name).await?;
        strip_managed_fields(serde_json::to_value(&obj)?)
    }

    // -- Cluster-scoped list helper ------------------------------------------

    async fn list_cluster<K>(&self, mapper: fn(&K) -> ResourceEntry) -> Result<Vec<ResourceEntry>>
    where
        K: kube::Resource<DynamicType=()>+serde::de::DeserializeOwned+Clone+std::fmt::Debug,
        K: kube::Resource<Scope=k8s_openapi::ClusterResourceScope>,
    {
        let api: Api<K> = Api::all(self.client.default.clone());
        let list = api.list(&ListParams::default()).await?;
        Ok(list.items.iter().map(mapper).collect())
    }

    // -- Cluster-scoped value fetch ------------------------------------------

    async fn get_value_cluster<K>(&self, name: &str) -> Result<Value>
    where
        K: kube::Resource<DynamicType=()>+serde::de::DeserializeOwned+serde::Serialize+Clone+std::fmt::Debug,
        K: kube::Resource<Scope=k8s_openapi::ClusterResourceScope>,
    {
        let api: Api<K> = Api::all(self.client.default.clone());
        let obj = api.get(name).await?;
        strip_managed_fields(serde_json::to_value(&obj)?)
    }

    // -- Per-resource mappers ------------------------------------------------

    fn map_deployment(d: &Deployment) -> ResourceEntry {
        let status = d.status.as_ref();
        let replicas = d.spec.as_ref().and_then(|s| s.replicas).unwrap_or(0);
        let ready = status.and_then(|s| s.ready_replicas).unwrap_or(0);
        let updated = status.and_then(|s| s.updated_replicas).unwrap_or(0);
        let available = status.and_then(|s| s.available_replicas).unwrap_or(0);
        ResourceEntry {
            name: meta_name(&d.metadata),
            namespace: meta_ns(&d.metadata),
            columns: vec![
                format!("{}/{}", ready, replicas),
                updated.to_string(),
                available.to_string(),
                format_age(d.metadata.creation_timestamp.as_ref()),
            ],
            sort_key: None,
        }
    }

    fn map_statefulset(s: &StatefulSet) -> ResourceEntry {
        let status = s.status.as_ref();
        let replicas = s.spec.as_ref().and_then(|sp| sp.replicas).unwrap_or(0);
        let ready = status.and_then(|st| st.ready_replicas).unwrap_or(0);
        ResourceEntry {
            name: meta_name(&s.metadata),
            namespace: meta_ns(&s.metadata),
            columns: vec![
                format!("{}/{}", ready, replicas),
                format_age(s.metadata.creation_timestamp.as_ref()),
            ],
            sort_key: None,
        }
    }

    fn map_replicaset(r: &ReplicaSet) -> ResourceEntry {
        let status = r.status.as_ref();
        let desired = r.spec.as_ref().and_then(|s| s.replicas).unwrap_or(0);
        let current = status.map(|s| s.replicas).unwrap_or(0);
        let ready = status.and_then(|s| s.ready_replicas).unwrap_or(0);
        ResourceEntry {
            name: meta_name(&r.metadata),
            namespace: meta_ns(&r.metadata),
            columns: vec![
                desired.to_string(),
                current.to_string(),
                ready.to_string(),
                format_age(r.metadata.creation_timestamp.as_ref()),
            ],
            sort_key: None,
        }
    }

    fn map_pod(p: &Pod) -> ResourceEntry {
        let status = p.status.as_ref();
        let phase = status.and_then(|s| s.phase.clone()).unwrap_or_else(|| "Unknown".into());
        let containers = status
            .and_then(|s| s.container_statuses.as_ref())
            .cloned()
            .unwrap_or_default();
        let total = containers.len();
        let ready_count = containers.iter().filter(|c| c.ready).count();
        let restarts: i32 = containers.iter().map(|c| c.restart_count).sum();
        ResourceEntry {
            name: meta_name(&p.metadata),
            namespace: meta_ns(&p.metadata),
            columns: vec![
                format!("{}/{}", ready_count, total),
                phase,
                restarts.to_string(),
                format_age(p.metadata.creation_timestamp.as_ref()),
            ],
            sort_key: None,
        }
    }

    fn map_cronjob(c: &CronJob) -> ResourceEntry {
        let spec = c.spec.as_ref();
        let schedule = spec.map(|s| s.schedule.clone()).unwrap_or_default();
        let suspend = spec.and_then(|s| s.suspend).unwrap_or(false);
        let active = c
            .status
            .as_ref()
            .and_then(|s| s.active.as_ref())
            .map(|a| a.len())
            .unwrap_or(0);
        ResourceEntry {
            name: meta_name(&c.metadata),
            namespace: meta_ns(&c.metadata),
            columns: vec![
                schedule,
                suspend.to_string(),
                active.to_string(),
                format_age(c.metadata.creation_timestamp.as_ref()),
            ],
            sort_key: None,
        }
    }

    fn map_job(j: &Job) -> ResourceEntry {
        let status = j.status.as_ref();
        let succeeded = status.and_then(|s| s.succeeded).unwrap_or(0);
        let completions = j.spec.as_ref().and_then(|s| s.completions).unwrap_or(1);
        let duration = status
            .and_then(|s| {
                let start = s.start_time.as_ref()?;
                let end = s.completion_time.as_ref().map(|t| t.0).unwrap_or_else(Timestamp::now);
                let dur = end.duration_since(start.0);
                Some(format_duration(dur))
            })
            .unwrap_or_default();
        ResourceEntry {
            name: meta_name(&j.metadata),
            namespace: meta_ns(&j.metadata),
            columns: vec![
                format!("{}/{}", succeeded, completions),
                duration,
                format_age(j.metadata.creation_timestamp.as_ref()),
            ],
            sort_key: None,
        }
    }

    fn map_configmap(c: &ConfigMap) -> ResourceEntry {
        let data_count =
            c.data.as_ref().map(|d| d.len()).unwrap_or(0) + c.binary_data.as_ref().map(|d| d.len()).unwrap_or(0);
        ResourceEntry {
            name: meta_name(&c.metadata),
            namespace: meta_ns(&c.metadata),
            columns: vec![
                data_count.to_string(),
                format_age(c.metadata.creation_timestamp.as_ref()),
            ],
            sort_key: None,
        }
    }

    fn map_secret(s: &Secret) -> ResourceEntry {
        let secret_type = s.type_.clone().unwrap_or_default();
        let data_count = s.data.as_ref().map(|d| d.len()).unwrap_or(0);
        ResourceEntry {
            name: meta_name(&s.metadata),
            namespace: meta_ns(&s.metadata),
            columns: vec![
                secret_type,
                data_count.to_string(),
                format_age(s.metadata.creation_timestamp.as_ref()),
            ],
            sort_key: None,
        }
    }

    fn map_service(s: &Service) -> ResourceEntry {
        let spec = s.spec.as_ref();
        let svc_type = spec.and_then(|s| s.type_.clone()).unwrap_or_default();
        let cluster_ip = spec
            .and_then(|s| s.cluster_ip.clone())
            .unwrap_or_else(|| "<none>".into());
        let ports = spec
            .and_then(|s| s.ports.as_ref())
            .map(|ports| {
                ports
                    .iter()
                    .map(|p| {
                        let proto = p.protocol.as_deref().unwrap_or("TCP");
                        match p.node_port {
                            | Some(np) => format!("{}:{}/{}", p.port, np, proto),
                            | None => format!("{}/{}", p.port, proto),
                        }
                    })
                    .collect::<Vec<_>>()
                    .join(",")
            })
            .unwrap_or_default();
        ResourceEntry {
            name: meta_name(&s.metadata),
            namespace: meta_ns(&s.metadata),
            columns: vec![
                svc_type,
                cluster_ip,
                ports,
                format_age(s.metadata.creation_timestamp.as_ref()),
            ],
            sort_key: None,
        }
    }

    fn map_daemonset(d: &DaemonSet) -> ResourceEntry {
        let status = d.status.as_ref();
        let desired = status.map(|s| s.desired_number_scheduled).unwrap_or(0);
        let current = status.map(|s| s.current_number_scheduled).unwrap_or(0);
        let ready = status.map(|s| s.number_ready).unwrap_or(0);
        let updated = status.and_then(|s| s.updated_number_scheduled).unwrap_or(0);
        ResourceEntry {
            name: meta_name(&d.metadata),
            namespace: meta_ns(&d.metadata),
            columns: vec![
                desired.to_string(),
                current.to_string(),
                ready.to_string(),
                updated.to_string(),
                format_age(d.metadata.creation_timestamp.as_ref()),
            ],
            sort_key: None,
        }
    }

    fn map_hpa(h: &HorizontalPodAutoscaler) -> ResourceEntry {
        let spec = h.spec.as_ref();
        let reference = spec
            .map(|s| {
                let kind = s.scale_target_ref.kind.as_str();
                let name = s.scale_target_ref.name.as_str();
                format!("{}/{}", kind, name)
            })
            .unwrap_or_default();
        let min_replicas = spec.and_then(|s| s.min_replicas).unwrap_or(0);
        let max_replicas = spec.map(|s| s.max_replicas).unwrap_or(0);
        let current = h.status.as_ref().map(|s| s.current_replicas).unwrap_or(0);
        ResourceEntry {
            name: meta_name(&h.metadata),
            namespace: meta_ns(&h.metadata),
            columns: vec![
                reference,
                min_replicas.to_string(),
                max_replicas.to_string(),
                current.to_string(),
                format_age(h.metadata.creation_timestamp.as_ref()),
            ],
            sort_key: None,
        }
    }

    fn map_ingress(i: &Ingress) -> ResourceEntry {
        let spec = i.spec.as_ref();
        let class = spec
            .and_then(|s| s.ingress_class_name.clone())
            .unwrap_or_else(|| "<none>".into());
        let hosts = spec
            .and_then(|s| s.rules.as_ref())
            .map(|rules| {
                rules
                    .iter()
                    .filter_map(|r| r.host.clone())
                    .collect::<Vec<_>>()
                    .join(",")
            })
            .unwrap_or_default();
        let address = i
            .status
            .as_ref()
            .and_then(|s| s.load_balancer.as_ref())
            .and_then(|lb| lb.ingress.as_ref())
            .map(|ingress| {
                ingress
                    .iter()
                    .filter_map(|i| i.ip.as_ref().or(i.hostname.as_ref()).cloned())
                    .collect::<Vec<_>>()
                    .join(",")
            })
            .unwrap_or_default();
        ResourceEntry {
            name: meta_name(&i.metadata),
            namespace: meta_ns(&i.metadata),
            columns: vec![
                class,
                hosts,
                address,
                format_age(i.metadata.creation_timestamp.as_ref()),
            ],
            sort_key: None,
        }
    }

    fn map_endpoints(e: &Endpoints) -> ResourceEntry {
        let endpoints_str = e
            .subsets
            .as_ref()
            .map(|subsets| {
                let mut addrs = Vec::new();
                for subset in subsets {
                    let ports: Vec<i32> = subset
                        .ports
                        .as_ref()
                        .map(|p| p.iter().map(|p| p.port).collect())
                        .unwrap_or_default();
                    if let Some(addresses) = &subset.addresses {
                        for addr in addresses {
                            for port in &ports {
                                addrs.push(format!("{}:{}", addr.ip, port));
                            }
                        }
                    }
                }
                if addrs.len() > 3 {
                    let total = addrs.len();
                    addrs.truncate(3);
                    format!("{} + {} more", addrs.join(","), total - 3)
                } else {
                    addrs.join(",")
                }
            })
            .unwrap_or_else(|| "<none>".into());
        ResourceEntry {
            name: meta_name(&e.metadata),
            namespace: meta_ns(&e.metadata),
            columns: vec![endpoints_str, format_age(e.metadata.creation_timestamp.as_ref())],
            sort_key: None,
        }
    }

    fn map_network_policy(n: &NetworkPolicy) -> ResourceEntry {
        let selector = n
            .spec
            .as_ref()
            .and_then(|s| s.pod_selector.as_ref())
            .and_then(|ps| ps.match_labels.as_ref())
            .map(|m| {
                m.iter()
                    .map(|(k, v)| format!("{}={}", k, v))
                    .collect::<Vec<_>>()
                    .join(",")
            })
            .unwrap_or_else(|| "<none>".into());
        ResourceEntry {
            name: meta_name(&n.metadata),
            namespace: meta_ns(&n.metadata),
            columns: vec![selector, format_age(n.metadata.creation_timestamp.as_ref())],
            sort_key: None,
        }
    }

    fn map_pvc(p: &PersistentVolumeClaim) -> ResourceEntry {
        let status = p.status.as_ref();
        let phase = status.and_then(|s| s.phase.clone()).unwrap_or_else(|| "Pending".into());
        let volume = p.spec.as_ref().and_then(|s| s.volume_name.clone()).unwrap_or_default();
        let capacity = status
            .and_then(|s| s.capacity.as_ref())
            .and_then(|c| c.get("storage"))
            .map(|q| q.0.clone())
            .unwrap_or_default();
        ResourceEntry {
            name: meta_name(&p.metadata),
            namespace: meta_ns(&p.metadata),
            columns: vec![
                phase,
                volume,
                capacity,
                format_age(p.metadata.creation_timestamp.as_ref()),
            ],
            sort_key: None,
        }
    }

    fn map_pv(p: &PersistentVolume) -> ResourceEntry {
        let spec = p.spec.as_ref();
        let capacity = spec
            .and_then(|s| s.capacity.as_ref())
            .and_then(|c| c.get("storage"))
            .map(|q| q.0.clone())
            .unwrap_or_default();
        let reclaim = spec
            .and_then(|s| s.persistent_volume_reclaim_policy.clone())
            .unwrap_or_default();
        let phase = p.status.as_ref().and_then(|s| s.phase.clone()).unwrap_or_default();
        let claim = spec
            .and_then(|s| s.claim_ref.as_ref())
            .map(|c| {
                format!(
                    "{}/{}",
                    c.namespace.as_deref().unwrap_or(""),
                    c.name.as_deref().unwrap_or("")
                )
            })
            .unwrap_or_default();
        ResourceEntry {
            name: meta_name(&p.metadata),
            namespace: String::new(),
            columns: vec![
                capacity,
                reclaim,
                phase,
                claim,
                format_age(p.metadata.creation_timestamp.as_ref()),
            ],
            sort_key: None,
        }
    }

    fn map_storage_class(s: &StorageClass) -> ResourceEntry {
        let provisioner = s.provisioner.clone();
        let reclaim = s.reclaim_policy.clone().unwrap_or_default();
        ResourceEntry {
            name: meta_name(&s.metadata),
            namespace: String::new(),
            columns: vec![provisioner, reclaim, format_age(s.metadata.creation_timestamp.as_ref())],
            sort_key: None,
        }
    }

    fn map_service_account(s: &ServiceAccount) -> ResourceEntry {
        let secrets_count = s.secrets.as_ref().map(|s| s.len()).unwrap_or(0);
        ResourceEntry {
            name: meta_name(&s.metadata),
            namespace: meta_ns(&s.metadata),
            columns: vec![
                secrets_count.to_string(),
                format_age(s.metadata.creation_timestamp.as_ref()),
            ],
            sort_key: None,
        }
    }

    fn map_role(r: &Role) -> ResourceEntry {
        ResourceEntry {
            name: meta_name(&r.metadata),
            namespace: meta_ns(&r.metadata),
            columns: vec![format_age(r.metadata.creation_timestamp.as_ref())],
            sort_key: None,
        }
    }

    fn map_role_binding(r: &RoleBinding) -> ResourceEntry {
        let role = format!("{}/{}", r.role_ref.kind, r.role_ref.name);
        ResourceEntry {
            name: meta_name(&r.metadata),
            namespace: meta_ns(&r.metadata),
            columns: vec![role, format_age(r.metadata.creation_timestamp.as_ref())],
            sort_key: None,
        }
    }

    fn map_cluster_role(r: &ClusterRole) -> ResourceEntry {
        ResourceEntry {
            name: meta_name(&r.metadata),
            namespace: String::new(),
            columns: vec![format_age(r.metadata.creation_timestamp.as_ref())],
            sort_key: None,
        }
    }

    fn map_cluster_role_binding(r: &ClusterRoleBinding) -> ResourceEntry {
        let role = format!("{}/{}", r.role_ref.kind, r.role_ref.name);
        ResourceEntry {
            name: meta_name(&r.metadata),
            namespace: String::new(),
            columns: vec![role, format_age(r.metadata.creation_timestamp.as_ref())],
            sort_key: None,
        }
    }

    fn map_node(n: &Node) -> ResourceEntry {
        let status = n.status.as_ref();
        let conditions = status.and_then(|s| s.conditions.as_ref());
        let ready = conditions
            .and_then(|conds| conds.iter().find(|c| c.type_ == "Ready").map(|c| c.status == "True"))
            .unwrap_or(false);
        let unschedulable = n.spec.as_ref().and_then(|s| s.unschedulable).unwrap_or(false);

        let status_str = match (ready, unschedulable) {
            | (true, false) => "Ready".to_string(),
            | (true, true) => "Ready,SchedulingDisabled".to_string(),
            | (false, false) => "NotReady".to_string(),
            | (false, true) => "NotReady,SchedulingDisabled".to_string(),
        };

        let roles = n
            .metadata
            .labels
            .as_ref()
            .map(|labels| {
                labels
                    .keys()
                    .filter_map(|k| {
                        k.strip_prefix("node-role.kubernetes.io/")
                            .map(|r| if r.is_empty() { "worker" } else { r })
                    })
                    .collect::<Vec<_>>()
                    .join(",")
            })
            .unwrap_or_default();
        let version = status
            .and_then(|s| s.node_info.as_ref())
            .map(|i| i.kubelet_version.clone())
            .unwrap_or_default();
        ResourceEntry {
            name: meta_name(&n.metadata),
            namespace: String::new(),
            columns: vec![
                status_str,
                if roles.is_empty() { "<none>".into() } else { roles },
                version,
                format_age(n.metadata.creation_timestamp.as_ref()),
            ],
            sort_key: None,
        }
    }

    fn map_namespace(n: &Namespace) -> ResourceEntry {
        let phase = n
            .status
            .as_ref()
            .and_then(|s| s.phase.clone())
            .unwrap_or_else(|| "Active".into());
        ResourceEntry {
            name: meta_name(&n.metadata),
            namespace: String::new(),
            columns: vec![phase, format_age(n.metadata.creation_timestamp.as_ref())],
            sort_key: None,
        }
    }

    fn map_event(e: &Event) -> ResourceEntry {
        let event_type = e.type_.clone().unwrap_or_default();
        let reason = e.reason.clone().unwrap_or_default();
        let object = e
            .regarding
            .as_ref()
            .and_then(|r| {
                r.name
                    .as_ref()
                    .map(|n| format!("{}/{}", r.kind.as_deref().unwrap_or(""), n))
            })
            .unwrap_or_default();
        let message = e.note.clone().unwrap_or_default();
        let count = e.series.as_ref().map(|s| s.count).or(e.deprecated_count).unwrap_or(1);
        // Prefer eventTime, fall back to deprecated lastTimestamp, then
        // creationTimestamp
        let best_time: Option<Timestamp> = e
            .event_time
            .as_ref()
            .map(|t| t.0)
            .or(e.deprecated_last_timestamp.as_ref().map(|t| t.0))
            .or(e.metadata.creation_timestamp.as_ref().map(|t| t.0));
        let sort_ts = best_time.map(|t| t.to_string()).unwrap_or_default();
        let age = best_time
            .map(|t| {
                let dur = Timestamp::now().duration_since(t);
                format_duration(dur)
            })
            .unwrap_or_else(|| "N/A".into());
        ResourceEntry {
            name: meta_name(&e.metadata),
            namespace: meta_ns(&e.metadata),
            // [0]=TYPE  [1]=REASON  [2]=OBJECT  [3]=AGE  [4]=MESSAGE  [5]=COUNT
            columns: vec![event_type, reason, object, age, message, count.to_string()],
            sort_key: Some(sort_ts),
        }
    }

    fn map_crd(c: &CustomResourceDefinition) -> ResourceEntry {
        let group = c.spec.group.clone();
        let versions = c
            .spec
            .versions
            .iter()
            .filter(|v| v.served)
            .map(|v| v.name.as_str())
            .collect::<Vec<_>>()
            .join(",");
        let scope = c.spec.scope.clone();
        ResourceEntry {
            name: meta_name(&c.metadata),
            namespace: String::new(),
            columns: vec![
                group,
                versions,
                scope,
                format_age(c.metadata.creation_timestamp.as_ref()),
            ],
            sort_key: None,
        }
    }

    // -- CRD discovery and dynamic CR operations --------------------------------

    /// Discover all CRDs in the cluster and return metadata for each.
    pub async fn discover_crds(&self) -> Result<Vec<CrdInfo>> {
        let api: Api<CustomResourceDefinition> = Api::all(self.client.default.clone());
        let list = api.list(&ListParams::default()).await?;
        let mut crds = Vec::new();
        for crd in &list.items {
            let spec = &crd.spec;
            // Find the served+storage version, or fall back to first served
            let version = spec
                .versions
                .iter()
                .find(|v| v.served && v.storage)
                .or_else(|| spec.versions.iter().find(|v| v.served));
            let version = match version {
                | Some(v) => v,
                | None => continue,
            };
            let columns: Vec<CrdColumn> = version
                .additional_printer_columns
                .as_ref()
                .map(|cols| {
                    cols.iter()
                        .filter(|c| c.json_path != ".metadata.creationTimestamp")
                        .map(|c| {
                            CrdColumn {
                                name: c.name.clone(),
                                json_path: c.json_path.clone(),
                                type_: c.type_.clone(),
                            }
                        })
                        .collect()
                })
                .unwrap_or_default();

            let scope = if spec.scope == "Namespaced" {
                CrdScope::Namespaced
            } else {
                CrdScope::Cluster
            };

            crds.push(CrdInfo {
                group: spec.group.clone(),
                version: version.name.clone(),
                kind: spec.names.kind.clone(),
                plural: spec.names.plural.clone(),
                scope,
                display_name: spec.names.kind.clone(),
                columns,
            });
        }
        crds.sort_by(|a, b| {
            let fqdn_a = format!("{}.{}", a.plural, a.group);
            let fqdn_b = format!("{}.{}", b.plural, b.group);
            fqdn_a.cmp(&fqdn_b)
        });
        Ok(crds)
    }

    /// List custom resource instances for a given CRD.
    pub async fn list_custom_resources(&self, crd: &CrdInfo) -> Result<Vec<ResourceEntry>> {
        let ar = crd.api_resource();
        let lp = ListParams::default();
        let api: Api<kube::api::DynamicObject> = if crd.is_cluster_scoped() {
            Api::all_with(self.client.default.clone(), &ar)
        } else {
            match &self.context.namespace {
                | Some(ns) => Api::namespaced_with(self.client.default.clone(), ns, &ar),
                | None => Api::all_with(self.client.default.clone(), &ar),
            }
        };
        let list = api.list(&lp).await?;
        Ok(list
            .items
            .iter()
            .map(|obj| {
                let data = serde_json::to_value(obj).unwrap_or_default();
                let name = obj.metadata.name.clone().unwrap_or_default();
                let namespace = obj.metadata.namespace.clone().unwrap_or_default();
                let mut columns: Vec<String> = crd
                    .columns
                    .iter()
                    .map(|col| resolve_json_path(&data, &col.json_path))
                    .collect();
                let age = obj
                    .metadata
                    .creation_timestamp
                    .as_ref()
                    .map(|t| format_age(Some(&Time(t.0))))
                    .unwrap_or_default();
                columns.push(age);
                ResourceEntry {
                    name,
                    namespace,
                    columns,
                    sort_key: None,
                }
            })
            .collect())
    }

    /// Get a single custom resource as a JSON Value.
    pub async fn get_custom_resource(&self, crd: &CrdInfo, ns: &str, name: &str) -> Result<Value> {
        let ar = crd.api_resource();
        let api: Api<kube::api::DynamicObject> = if crd.is_cluster_scoped() {
            Api::all_with(self.client.default.clone(), &ar)
        } else {
            Api::namespaced_with(self.client.default.clone(), ns, &ar)
        };
        let obj = api.get(name).await?;
        let val = serde_json::to_value(&obj)?;
        strip_managed_fields(val)
    }
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/// Resolve a simple JSON path (dot-separated, starting with ".") against a
/// serde_json::Value. Supports basic dot notation like ".spec.replicas" and
/// simple array indexing like ".status.conditions[0].type".
fn resolve_json_path(data: &Value, path: &str) -> String {
    let path = path.strip_prefix('.').unwrap_or(path);
    let mut current = data;
    for segment in path.split('.') {
        // Handle array index: e.g., "conditions[0]"
        if let Some(idx_start) = segment.find('[') {
            let key = &segment[..idx_start];
            let idx_str = &segment[idx_start + 1..segment.len().saturating_sub(1)];
            if !key.is_empty() {
                current = match current.get(key) {
                    | Some(v) => v,
                    | None => return String::new(),
                };
            }
            if let Ok(idx) = idx_str.parse::<usize>() {
                current = match current.get(idx) {
                    | Some(v) => v,
                    | None => return String::new(),
                };
            }
        } else {
            current = match current.get(segment) {
                | Some(v) => v,
                | None => return String::new(),
            };
        }
    }
    match current {
        | Value::String(s) => s.clone(),
        | Value::Number(n) => n.to_string(),
        | Value::Bool(b) => b.to_string(),
        | Value::Null => String::new(),
        | other => other.to_string(),
    }
}

fn meta_name(meta: &k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta) -> String {
    meta.name.clone().unwrap_or_default()
}

fn meta_ns(meta: &k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta) -> String {
    meta.namespace.clone().unwrap_or_default()
}

/// Resolve the top-level owner of a pod (e.g. Deployment, not ReplicaSet).
/// For ReplicaSet-owned pods, strips the hash suffix to infer the Deployment
/// name.
fn resolve_top_owner(pod: &Pod) -> (String, String) {
    let owners = pod.metadata.owner_references.as_ref();
    let owner = owners.and_then(|refs| refs.first());
    match owner {
        | Some(oref) => {
            let kind = oref.kind.as_str();
            let name = &oref.name;
            match kind {
                // ReplicaSets are owned by Deployments — infer Deployment name
                | "ReplicaSet" => {
                    // Deployment-created RS names follow: <deploy>-<hash>
                    let deploy_name = name.rsplit_once('-').map(|(prefix, _)| prefix).unwrap_or(name);
                    (deploy_name.to_string(), "Deployment".to_string())
                },
                | _ => (name.clone(), kind.to_string()),
            }
        },
        | None => (meta_name(&pod.metadata), "Pod".to_string()),
    }
}

fn format_age(timestamp: Option<&Time>) -> String {
    match timestamp {
        | Some(time) => {
            let dur = Timestamp::now().duration_since(time.0);
            format_duration(dur)
        },
        | None => "N/A".into(),
    }
}

fn format_duration(dur: jiff::SignedDuration) -> String {
    let secs = dur.as_secs().max(0);
    let days = secs / 86400;
    let hours = (secs % 86400) / 3600;
    let mins = (secs % 3600) / 60;
    if days > 0 {
        format!("{}d", days)
    } else if hours > 0 {
        format!("{}h", hours)
    } else if mins > 0 {
        format!("{}m", mins)
    } else {
        format!("{}s", secs)
    }
}

pub fn parse_memory_bytes(raw: &str) -> f64 {
    if raw == "-" || raw.is_empty() {
        return 0.0;
    }
    if let Some(val) = raw.strip_suffix("Ki") {
        val.parse::<f64>().unwrap_or(0.0) * 1024.0
    } else if let Some(val) = raw.strip_suffix("Mi") {
        val.parse::<f64>().unwrap_or(0.0) * 1024.0 * 1024.0
    } else if let Some(val) = raw.strip_suffix("Gi") {
        val.parse::<f64>().unwrap_or(0.0) * 1024.0 * 1024.0 * 1024.0
    } else if let Some(val) = raw.strip_suffix("Ti") {
        val.parse::<f64>().unwrap_or(0.0) * 1024.0 * 1024.0 * 1024.0 * 1024.0
    } else if let Some(val) = raw.strip_suffix('K') {
        val.parse::<f64>().unwrap_or(0.0) * 1000.0
    } else if let Some(val) = raw.strip_suffix('M') {
        val.parse::<f64>().unwrap_or(0.0) * 1_000_000.0
    } else if let Some(val) = raw.strip_suffix('G') {
        val.parse::<f64>().unwrap_or(0.0) * 1_000_000_000.0
    } else if let Some(val) = raw.strip_suffix('T') {
        val.parse::<f64>().unwrap_or(0.0) * 1_000_000_000_000.0
    } else {
        raw.parse::<f64>().unwrap_or(0.0)
    }
}

pub fn parse_cpu_cores(raw: &str) -> f64 {
    if raw == "-" || raw.is_empty() {
        return 0.0;
    }
    if let Some(val) = raw.strip_suffix('m') {
        val.parse::<f64>().unwrap_or(0.0) / 1000.0
    } else if let Some(val) = raw.strip_suffix('n') {
        val.parse::<f64>().unwrap_or(0.0) / 1_000_000_000.0
    } else {
        raw.parse::<f64>().unwrap_or(0.0)
    }
}

fn format_memory_gb(raw: &str) -> String {
    if raw == "-" || raw.is_empty() {
        return raw.to_string();
    }
    let gb = parse_memory_bytes(raw) / (1024.0 * 1024.0 * 1024.0);
    if gb >= 10.0 {
        format!("{:.0}Gi", gb)
    } else {
        format!("{:.1}Gi", gb)
    }
}

pub fn format_memory_gb_from_bytes(bytes: f64) -> String {
    let gb = bytes / (1024.0 * 1024.0 * 1024.0);
    if gb >= 10.0 {
        format!("{:.0}Gi", gb)
    } else {
        format!("{:.1}Gi", gb)
    }
}

pub fn format_cpu_cores(cores: f64) -> String {
    if cores >= 10.0 {
        format!("{:.0}", cores)
    } else {
        format!("{:.1}", cores)
    }
}

fn strip_managed_fields(mut val: Value) -> Result<Value> {
    if let Some(metadata) = val.get_mut("metadata") {
        if let Some(map) = metadata.as_object_mut() {
            map.remove("managedFields");
        }
    }
    Ok(val)
}

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

    #[test]
    fn gvk_uses_api_plural_not_a_suffix_guess() {
        let ingress = ResourceType::Ingress.api_resource();
        assert_eq!(ingress.plural, "ingresses");
        assert_eq!(ingress.api_version, "networking.k8s.io/v1");
        assert_eq!(
            ResourceType::from_gvk("networking.k8s.io/v1", "Ingress"),
            Some(ResourceType::Ingress)
        );

        let sc = ResourceType::StorageClass.api_resource();
        assert_eq!(sc.plural, "storageclasses");
        assert!(sc.kind != "storageclasss");
        assert!(ResourceType::StorageClass.is_cluster_scoped());
        assert!(!ResourceType::Ingress.is_cluster_scoped());
    }

    #[test]
    fn favorite_type_round_trips_builtin_and_custom_keys() {
        let deploy = FavoriteType::builtin(ResourceType::Deployment);
        assert_eq!(deploy.as_str(), "deployment");
        assert_eq!(deploy.builtin_type(), Some(ResourceType::Deployment));
        assert_eq!(deploy.short_label().as_ref(), "Deploy");

        let cert = FavoriteType::custom("cert-manager.io", "certificates");
        assert_eq!(cert.as_str(), "cr:cert-manager.io/certificates");
        assert_eq!(cert.custom_ref(), Some(("cert-manager.io", "certificates")));
        assert_eq!(cert.short_label().as_ref(), "certificates");
        assert!(cert.builtin_type().is_none());

        let yaml = serde_yaml::to_string(&deploy).unwrap();
        assert!(yaml.contains("deployment"));
        let back: FavoriteType = serde_yaml::from_str(&yaml).unwrap();
        assert_eq!(back, deploy);
    }

    #[test]
    fn quantity_parsers_accept_k8s_suffixes() {
        assert_eq!(parse_cpu_cores("250m"), 0.25);
        assert_eq!(parse_cpu_cores("2"), 2.0);
        assert_eq!(parse_memory_bytes("1Ki"), 1024.0);
        assert_eq!(parse_memory_bytes("1Mi"), 1024.0 * 1024.0);
        assert_eq!(parse_memory_bytes("-"), 0.0);
    }

    #[test]
    fn auth_expiry_is_the_kube_auth_error_or_http_401() {
        let plugin = kube::Error::Auth(kube::client::AuthError::ExecPluginFailed);
        assert!(ClusterFailure::from_kube(&plugin).is_auth());

        let unauthorized = kube::Error::Api(
            kube::core::Status::failure("token rejected", kube::core::response::reason::UNAUTHORIZED)
                .with_code(401)
                .boxed(),
        );
        assert!(ClusterFailure::from_kube(&unauthorized).is_auth());

        let forbidden = kube::Error::Api(
            kube::core::Status::failure("pods is forbidden", kube::core::response::reason::FORBIDDEN)
                .with_code(403)
                .boxed(),
        );
        assert!(!ClusterFailure::from_kube(&forbidden).is_auth());

        let prose = anyhow::anyhow!("Error from server (Unauthorized): Unauthorized");
        assert!(!ClusterFailure::from_anyhow(prose).is_auth());
    }

    #[test]
    fn display_error_strips_ansi_and_does_not_split_a_char() {
        let raw = "fail \u{1b}[31mnow\u{1b}[0m\nline two — tail";
        let shown = KubeClient::display_error(raw);
        assert!(!shown.contains('\u{1b}'));
        assert!(!shown.contains('\n'));
        assert!(shown.contains("fail now line two"));
        let sliced = sanitize_terminal_text("éééé", 2);
        assert_eq!(sliced, "éé");
    }

    #[test]
    fn exec_plugin_stderr_is_redirected_and_not_saved_as_interactive() {
        let mut config: Kubeconfig = serde_yaml::from_str(
            r#"
users:
- name: gke
  user:
    exec:
      apiVersion: client.authentication.k8s.io/v1beta1
      command: gke-gcloud-auth-plugin
      args: ["--flag", "bar baz"]
      interactiveMode: IfAvailable
"#,
        )
        .unwrap();
        KubeClient::silence_exec_plugins(&mut config);
        let exec = config.auth_infos[0].auth_info.as_ref().unwrap().exec.as_ref().unwrap();
        assert_eq!(exec.command.as_deref(), Some("sh"));
        assert_eq!(exec.interactive_mode, Some(kube::config::ExecInteractiveMode::Never));
        let script = exec.args.as_ref().unwrap()[1].as_str();
        assert!(script.contains("gke-gcloud-auth-plugin"));
        assert!(script.contains("'bar baz'"));
        assert!(script.contains("2>/dev/null"));
        assert!(script.contains("CLOUDSDK_CORE_DISABLE_PROMPTS=1"));
    }
}