twc-rs 0.3.5

Fast single-binary CLI and interactive TUI dashboard for Timeweb Cloud
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
// SPDX-FileCopyrightText: 2026 RAprogramm <andrey.rozanov.vl@gmail.com>
// SPDX-License-Identifier: MIT

//! Application state for the TUI dashboard.

use std::{
    borrow::Cow,
    collections::VecDeque,
    time::{Duration, Instant}
};

use rust_i18n::t;

/// Severity of an event-log entry.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(dead_code)]
pub enum LogLevel {
    /// Neutral informational event.
    Info,
    /// A successful outcome.
    Success,
    /// A non-fatal warning.
    Warn,
    /// A failure.
    Error
}

/// A single entry in the dashboard event log.
#[derive(Debug, Clone)]
pub struct LogEntry {
    /// Severity of the entry.
    pub level: LogLevel,
    /// Human-readable message.
    pub text:  String
}

/// Account information from the API.
#[derive(Debug, Clone, Default)]
pub struct AccountInfo {
    pub login:      String,
    pub account_id: i64,
    pub balance:    String,
    pub status:     String
}

/// Summary of a single server.
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct ServerSummary {
    pub id:       i32,
    pub name:     String,
    pub status:   String,
    pub cpu:      i32,
    pub ram_mb:   i32,
    pub disk_gb:  i32,
    pub ip:       String,
    pub location: String
}

/// Summary of a single database.
#[derive(Debug, Clone)]
pub struct DatabaseSummary {
    pub id:      i32,
    pub name:    String,
    pub status:  String,
    pub engine:  String,
    pub size_mb: i64
}

/// Summary of a single S3 storage.
#[derive(Debug, Clone)]
pub struct S3Summary {
    pub id:           i32,
    pub name:         String,
    pub region:       String,
    pub size_bytes:   i64,
    pub bucket_count: i32
}

/// Summary of a single Kubernetes cluster.
#[derive(Debug, Clone)]
pub struct K8sSummary {
    pub id:         i32,
    pub name:       String,
    pub status:     String,
    pub version:    String,
    pub node_count: i32
}

/// Summary of a single project.
#[derive(Debug, Clone)]
pub struct ProjectSummary {
    pub id:           i32,
    pub name:         String,
    pub server_count: i32
}

/// Summary of a single load balancer.
#[derive(Debug, Clone)]
// JUSTIFY: Public API type for future API integration.
#[allow(dead_code)]
pub struct BalancerSummary {
    pub id:       i32,
    pub name:     String,
    pub status:   String,
    pub ip:       String,
    pub location: String
}

/// Summary of a single container registry.
#[derive(Debug, Clone)]
// JUSTIFY: Public API type for future API integration.
#[allow(dead_code)]
pub struct RegistrySummary {
    pub id:               i32,
    pub name:             String,
    pub region:           String,
    pub repository_count: i32
}

/// Summary of a single domain.
#[derive(Debug, Clone)]
// JUSTIFY: Public API type for future API integration.
#[allow(dead_code)]
pub struct DomainSummary {
    pub id:           i32,
    pub name:         String,
    pub status:       String,
    pub auto_prolong: bool
}

/// Summary of a single firewall.
#[derive(Debug, Clone)]
// JUSTIFY: Public API type for future API integration.
#[allow(dead_code)]
pub struct FirewallSummary {
    pub id:             i32,
    pub name:           String,
    pub rule_count:     i32,
    pub resource_count: i32
}

/// Summary of a single floating IP.
#[derive(Debug, Clone)]
// JUSTIFY: Public API type for future API integration.
#[allow(dead_code)]
pub struct FloatingIpSummary {
    pub id:          i32,
    pub ip:          String,
    pub status:      String,
    pub server_name: String
}

/// Summary of a single image.
#[derive(Debug, Clone)]
// JUSTIFY: Public API type for future API integration.
#[allow(dead_code)]
pub struct ImageSummary {
    pub id:      i32,
    pub name:    String,
    pub size_mb: i64,
    pub status:  String
}

/// Summary of a single network drive.
#[derive(Debug, Clone)]
// JUSTIFY: Public API type for future API integration.
#[allow(dead_code)]
pub struct NetworkDriveSummary {
    pub id:      i32,
    pub name:    String,
    pub size_gb: i64,
    pub status:  String
}

/// Summary of a single VPC.
#[derive(Debug, Clone)]
// JUSTIFY: Public API type for future API integration.
#[allow(dead_code)]
pub struct VpcSummary {
    pub id:           i32,
    pub name:         String,
    pub subnet_count: i32,
    pub status:       String
}

/// Summary of a single dedicated server.
#[derive(Debug, Clone)]
// JUSTIFY: Public API type for future API integration.
#[allow(dead_code)]
pub struct DedicatedServerSummary {
    pub id:      i32,
    pub name:    String,
    pub status:  String,
    pub cpu:     i32,
    pub ram_mb:  i32,
    pub disk_gb: i64
}

/// Summary of a single mail service.
#[derive(Debug, Clone)]
// JUSTIFY: Public API type for future API integration.
#[allow(dead_code)]
pub struct MailSummary {
    pub id:            i32,
    pub name:          String,
    pub mailbox_count: i32,
    pub status:        String
}

/// Summary of a single application.
#[derive(Debug, Clone)]
// JUSTIFY: Public API type for future API integration.
#[allow(dead_code)]
pub struct AppSummary {
    pub id:           i32,
    pub name:         String,
    pub status:       String,
    pub deploy_count: i32
}

/// Summary of a single AI agent.
#[derive(Debug, Clone)]
// JUSTIFY: Public API type for future API integration.
#[allow(dead_code)]
pub struct AiAgentSummary {
    pub id:     i32,
    pub name:   String,
    pub status: String,
    pub model:  String
}

/// Summary of a single knowledge base.
#[derive(Debug, Clone)]
// JUSTIFY: Public API type for future API integration.
#[allow(dead_code)]
pub struct KnowledgeBaseSummary {
    pub id:             i32,
    pub name:           String,
    pub document_count: i32,
    pub status:         String
}

/// A kind of action that can be performed on a resource.
///
/// Each [`ResourceTab`] declares which kinds apply to it via
/// [`ResourceTab::actions`]; the dashboard loop maps a chosen
/// `(tab, kind)` pair to the matching Timeweb API call.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ActionKind {
    /// Power the resource on.
    Start,
    /// Gracefully power the resource off.
    Shutdown,
    /// Reboot the resource.
    Reboot,
    /// Create a clone of the resource.
    Clone,
    /// Permanently delete the resource.
    Delete
}

impl ActionKind {
    /// Returns the label shown in the action menu and confirmation prompt.
    #[must_use]
    pub const fn label(self) -> &'static str {
        match self {
            Self::Start => "Start",
            Self::Shutdown => "Shutdown",
            Self::Reboot => "Reboot",
            Self::Clone => "Clone",
            Self::Delete => "Delete"
        }
    }

    /// Returns the localized label shown in menus and confirmation prompts.
    #[must_use]
    pub fn display_label(self) -> Cow<'static, str> {
        match self {
            Self::Start => t!("app.action_start"),
            Self::Shutdown => t!("app.action_shutdown"),
            Self::Reboot => t!("app.action_reboot"),
            Self::Clone => t!("app.action_clone"),
            Self::Delete => t!("app.action_delete")
        }
    }

    /// Returns true when the action is destructive and irreversible.
    ///
    /// Destructive actions require an extra confirmation step.
    #[must_use]
    pub const fn is_destructive(self) -> bool {
        matches!(self, Self::Delete)
    }
}

/// A resource action awaiting confirmation, or ready to be dispatched.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PendingAction {
    /// The resource category the action targets.
    pub tab:           ResourceTab,
    /// The action to perform.
    pub kind:          ActionKind,
    /// Target resource id.
    pub resource_id:   i32,
    /// Target resource name, for display.
    pub resource_name: String
}

/// A single row inside a drill-in view (a resource contained in a parent).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DrillItem {
    /// Resource kind label (e.g. "Server", "Database").
    pub kind:   String,
    /// Resource name.
    pub name:   String,
    /// Short secondary detail (status, engine, ...).
    pub detail: String
}

/// A drill-in view showing the contents of a selected resource.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DrillView {
    /// Title describing what was drilled into.
    pub title:    String,
    /// Contained resources.
    pub items:    Vec<DrillItem>,
    /// Index of the highlighted row.
    pub selected: usize
}

/// A request to fetch live hardware statistics for a resource.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StatsRequest {
    /// The resource category (only
    /// [`ResourceTab::Servers`]/[`ResourceTab::Apps`] expose live
    /// statistics).
    pub tab:  ResourceTab,
    /// The resource id as a string (servers and apps use different id types).
    pub id:   String,
    /// Resource name, shown as the metrics-panel subject.
    pub name: String
}

/// Live hardware statistics time-series for the selected resource.
#[derive(Debug, Clone, Default)]
pub struct ResourceStats {
    /// The resource id these statistics belong to.
    pub id:      String,
    /// Resource name shown in the metrics-panel title.
    pub subject: String,
    /// CPU load percentage over time.
    pub cpu:     Vec<f64>,
    /// RAM usage percentage over time.
    pub ram:     Vec<f64>,
    /// Incoming network bytes over time.
    pub net_in:  Vec<f64>,
    /// Outgoing network bytes over time.
    pub net_out: Vec<f64>
}

/// A single editable field in a create form.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InputField {
    /// Field label shown to the left of the input.
    pub label:    String,
    /// Current value typed by the user.
    pub value:    String,
    /// Whether the field must be non-empty to submit.
    pub required: bool
}

/// An in-dashboard form for creating a new resource.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CreateForm {
    /// The resource category being created.
    pub tab:    ResourceTab,
    /// Form title.
    pub title:  String,
    /// Editable fields in tab order.
    pub fields: Vec<InputField>,
    /// Index of the focused field.
    pub active: usize
}

/// A context action menu opened over the selected resource.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ActionMenu {
    /// The resource category the menu targets.
    pub tab:           ResourceTab,
    /// Target resource id.
    pub resource_id:   i32,
    /// Target resource name, for display.
    pub resource_name: String,
    /// Available actions, in display order.
    pub actions:       Vec<ActionKind>,
    /// Index of the highlighted action.
    pub selected:      usize
}

/// Resource category in the left panel.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ResourceTab {
    Servers,
    Databases,
    S3,
    Kubernetes,
    Projects,
    Balancers,
    Registry,
    Domains,
    Firewall,
    FloatingIps,
    Images,
    NetworkDrives,
    Vpc,
    DedicatedServers,
    Mail,
    Apps,
    AiAgents,
    KnowledgeBases,
    SshKeys,
    Finances
}

impl ResourceTab {
    /// Returns all tab names.
    // JUSTIFY: Public API method for future UI integration.
    #[allow(dead_code)]
    #[must_use]
    pub const fn names() -> &'static [&'static str] {
        &[
            "Servers",
            "Databases",
            "S3",
            "Kubernetes",
            "Projects",
            "Balancers",
            "Registry",
            "Domains",
            "Firewall",
            "FloatingIps",
            "Images",
            "NetworkDrives",
            "Vpc",
            "DedicatedServers",
            "Mail",
            "Apps",
            "AiAgents",
            "KnowledgeBases",
            "SshKeys",
            "Finances"
        ]
    }

    /// Returns the localized display name for this tab.
    #[must_use]
    pub fn display_name(self) -> Cow<'static, str> {
        match self {
            Self::Servers => t!("tabs.servers"),
            Self::Databases => t!("tabs.databases"),
            Self::S3 => t!("tabs.s3"),
            Self::Kubernetes => t!("tabs.kubernetes"),
            Self::Projects => t!("tabs.projects"),
            Self::Balancers => t!("tabs.balancers"),
            Self::Registry => t!("tabs.registry"),
            Self::Domains => t!("tabs.domains"),
            Self::Firewall => t!("tabs.firewall"),
            Self::FloatingIps => t!("tabs.floating_ips"),
            Self::Images => t!("tabs.images"),
            Self::NetworkDrives => t!("tabs.network_drives"),
            Self::Vpc => t!("tabs.vpc"),
            Self::DedicatedServers => t!("tabs.dedicated_servers"),
            Self::Mail => t!("tabs.mail"),
            Self::Apps => t!("tabs.apps"),
            Self::AiAgents => t!("tabs.ai_agents"),
            Self::KnowledgeBases => t!("tabs.knowledge_bases"),
            Self::SshKeys => t!("tabs.ssh_keys"),
            Self::Finances => t!("tabs.finances")
        }
    }

    /// Returns the index of this tab.
    #[must_use]
    pub const fn index(self) -> usize {
        match self {
            Self::Servers => 0,
            Self::Databases => 1,
            Self::S3 => 2,
            Self::Kubernetes => 3,
            Self::Projects => 4,
            Self::Balancers => 5,
            Self::Registry => 6,
            Self::Domains => 7,
            Self::Firewall => 8,
            Self::FloatingIps => 9,
            Self::Images => 10,
            Self::NetworkDrives => 11,
            Self::Vpc => 12,
            Self::DedicatedServers => 13,
            Self::Mail => 14,
            Self::Apps => 15,
            Self::AiAgents => 16,
            Self::KnowledgeBases => 17,
            Self::SshKeys => 18,
            Self::Finances => 19
        }
    }

    /// Returns the context actions available for this resource type,
    /// in menu display order.
    ///
    /// Tabs without wired actions return an empty slice, so the action
    /// menu does not open for them.
    #[must_use]
    pub const fn actions(self) -> &'static [ActionKind] {
        use ActionKind::{Clone, Delete, Reboot, Shutdown, Start};
        match self {
            Self::Servers => &[Start, Shutdown, Reboot, Clone, Delete],
            Self::Databases
            | Self::S3
            | Self::Kubernetes
            | Self::Balancers
            | Self::Registry
            | Self::Projects
            | Self::DedicatedServers
            | Self::AiAgents
            | Self::KnowledgeBases
            | Self::Apps => &[Delete],
            _ => &[]
        }
    }

    /// All tabs, in display order.
    pub const ALL: [Self; 20] = [
        Self::Servers,
        Self::Databases,
        Self::S3,
        Self::Kubernetes,
        Self::Projects,
        Self::Balancers,
        Self::Registry,
        Self::Domains,
        Self::Firewall,
        Self::FloatingIps,
        Self::Images,
        Self::NetworkDrives,
        Self::Vpc,
        Self::DedicatedServers,
        Self::Mail,
        Self::Apps,
        Self::AiAgents,
        Self::KnowledgeBases,
        Self::SshKeys,
        Self::Finances
    ];
}
/// Which panel is currently highlighted.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[allow(dead_code)]
pub enum Focus {
    /// Resource tabs bar at the top.
    ResourceTabs,
    /// Resource list panel (left side).
    #[default]
    ResourceList,
    /// Details panel (right side).
    Details
}

/// Holds all runtime state for the TUI dashboard.
#[allow(clippy::struct_excessive_bools)]
pub struct App {
    pub account:           AccountInfo,
    pub servers:           Vec<ServerSummary>,
    pub databases:         Vec<DatabaseSummary>,
    pub s3_storages:       Vec<S3Summary>,
    pub k8s_clusters:      Vec<K8sSummary>,
    pub projects:          Vec<ProjectSummary>,
    pub balancers:         Vec<BalancerSummary>,
    pub registries:        Vec<RegistrySummary>,
    pub domains:           Vec<DomainSummary>,
    pub firewalls:         Vec<FirewallSummary>,
    pub floating_ips:      Vec<FloatingIpSummary>,
    pub images:            Vec<ImageSummary>,
    pub network_drives:    Vec<NetworkDriveSummary>,
    pub vpcs:              Vec<VpcSummary>,
    pub dedicated_servers: Vec<DedicatedServerSummary>,
    pub mails:             Vec<MailSummary>,
    pub apps:              Vec<AppSummary>,
    pub ai_agents:         Vec<AiAgentSummary>,
    pub knowledge_bases:   Vec<KnowledgeBaseSummary>,
    pub ssh_keys:          Vec<String>,
    pub finances:          Vec<String>,
    pub selected:          usize,
    pub active_tab:        ResourceTab,
    pub theme:             super::themes::Theme,
    pub token:             Option<String>,
    pub cpu_history:       VecDeque<f64>,
    pub ram_history:       VecDeque<f64>,
    pub net_in_history:    VecDeque<f64>,
    pub net_out_history:   VecDeque<f64>,
    pub last_refresh:      Instant,
    pub refresh_interval:  Duration,
    pub running:           bool,
    pub show_help:         bool,
    pub status_message:    Option<String>,
    pub error_message:     Option<String>,
    pub is_loading:        bool,
    pub widgets:           super::widgets::WidgetRegistry,
    pub focus:             Focus,
    pub action_menu:       Option<ActionMenu>,
    pub confirm:           Option<PendingAction>,
    pub dispatch:          Option<PendingAction>,
    pub palette:           Option<super::command_palette::CommandPalette>,
    pub list_width_pct:    u16,
    pub anim_tick:         u64,
    pub prefs_dirty:       bool,
    pub logs:              VecDeque<LogEntry>,
    pub last_load_errors:  Vec<String>,
    pub refresh_requested: bool,
    pub drill:             Option<DrillView>,
    pub drill_request:     Option<(ResourceTab, i32, String)>,
    pub filter:            String,
    pub filter_editing:    bool,
    pub hide_empty_tabs:   bool,
    pub language:          crate::config::Language,
    pub stats_subject:     Option<String>,
    pub stats_loaded_for:  Option<String>,
    pub create_form:       Option<CreateForm>,
    pub create_request:    Option<CreateForm>,
    pub profiles:          Vec<String>,
    pub active_profile:    String,
    pub switch_profile:    Option<String>
}

impl App {
    /// Creates a new `App` with default state.
    // JUSTIFY: Used in tests and as a convenience constructor.
    #[allow(dead_code)]
    #[must_use]
    pub fn new(refresh_secs: u64) -> Self {
        Self::new_with_theme(refresh_secs, super::themes::Theme::default(), None)
    }

    /// Creates a new `App` with a specific theme and optional token.
    #[must_use]
    pub fn new_with_theme(
        refresh_secs: u64,
        theme: super::themes::Theme,
        token: Option<String>
    ) -> Self {
        Self {
            account: AccountInfo::default(),
            servers: Vec::new(),
            databases: Vec::new(),
            s3_storages: Vec::new(),
            k8s_clusters: Vec::new(),
            projects: Vec::new(),
            balancers: Vec::new(),
            registries: Vec::new(),
            domains: Vec::new(),
            firewalls: Vec::new(),
            floating_ips: Vec::new(),
            images: Vec::new(),
            network_drives: Vec::new(),
            vpcs: Vec::new(),
            dedicated_servers: Vec::new(),
            mails: Vec::new(),
            apps: Vec::new(),
            ai_agents: Vec::new(),
            knowledge_bases: Vec::new(),
            ssh_keys: Vec::new(),
            finances: Vec::new(),
            selected: 0,
            active_tab: ResourceTab::Servers,
            theme,
            token,
            cpu_history: VecDeque::with_capacity(60),
            ram_history: VecDeque::with_capacity(60),
            net_in_history: VecDeque::with_capacity(60),
            net_out_history: VecDeque::with_capacity(60),
            last_refresh: Instant::now(),
            refresh_interval: Duration::from_secs(refresh_secs),
            running: true,
            show_help: false,
            status_message: None,
            error_message: None,
            is_loading: false,
            widgets: super::widgets::WidgetRegistry::new(),
            focus: Focus::ResourceList,
            action_menu: None,
            confirm: None,
            dispatch: None,
            palette: None,
            list_width_pct: 40,
            anim_tick: 0,
            prefs_dirty: false,
            logs: VecDeque::with_capacity(200),
            last_load_errors: Vec::new(),
            refresh_requested: false,
            drill: None,
            drill_request: None,
            filter: String::new(),
            filter_editing: false,
            hide_empty_tabs: false,
            language: crate::config::Language::default(),
            stats_subject: None,
            stats_loaded_for: None,
            create_form: None,
            create_request: None,
            profiles: Vec::new(),
            active_profile: "default".to_string(),
            switch_profile: None
        }
    }

    /// Takes a pending profile-switch request (the dashboard loop re-auths).
    pub fn take_switch_profile(&mut self) -> Option<String> {
        self.switch_profile.take()
    }

    /// Returns true when the create form overlay is open.
    #[must_use]
    pub const fn create_form_open(&self) -> bool {
        self.create_form.is_some()
    }

    /// Opens a create form for the active tab, when that resource supports
    /// in-dashboard creation. Resources whose creation needs many or
    /// structured fields stay CLI-only.
    pub fn open_create_form(&mut self) {
        let form = match self.active_tab {
            ResourceTab::Projects => Some(CreateForm {
                tab:    ResourceTab::Projects,
                title:  "Create project".to_string(),
                fields: vec![
                    InputField {
                        label:    "Name".to_string(),
                        value:    String::new(),
                        required: true
                    },
                    InputField {
                        label:    "Description".to_string(),
                        value:    String::new(),
                        required: false
                    },
                ],
                active: 0
            }),
            _ => None
        };
        if let Some(form) = form {
            self.create_form = Some(form);
        }
    }

    /// Closes the create form without submitting.
    pub fn close_create_form(&mut self) {
        self.create_form = None;
    }

    /// Moves focus to the next form field.
    pub const fn form_next_field(&mut self) {
        if let Some(form) = &mut self.create_form
            && !form.fields.is_empty()
        {
            form.active = (form.active + 1) % form.fields.len();
        }
    }

    /// Moves focus to the previous form field.
    pub const fn form_prev_field(&mut self) {
        if let Some(form) = &mut self.create_form
            && !form.fields.is_empty()
        {
            let len = form.fields.len();
            form.active = (form.active + len - 1) % len;
        }
    }

    /// Appends a character to the focused field.
    pub fn form_input(&mut self, c: char) {
        if let Some(form) = &mut self.create_form
            && let Some(field) = form.fields.get_mut(form.active)
        {
            field.value.push(c);
        }
    }

    /// Removes the last character from the focused field.
    pub fn form_backspace(&mut self) {
        if let Some(form) = &mut self.create_form
            && let Some(field) = form.fields.get_mut(form.active)
        {
            field.value.pop();
        }
    }

    /// Submits the form when all required fields are filled, queuing it for the
    /// dashboard loop to perform the create. Returns false when a required
    /// field is empty (the form stays open).
    pub fn form_submit(&mut self) -> bool {
        let Some(form) = &self.create_form else {
            return false;
        };
        if form
            .fields
            .iter()
            .any(|f| f.required && f.value.trim().is_empty())
        {
            return false;
        }
        self.create_request = self.create_form.take();
        true
    }

    /// Takes a submitted create request for the dashboard loop to perform.
    pub fn take_create_request(&mut self) -> Option<CreateForm> {
        self.create_request.take()
    }

    /// Polls the selected resource for live statistics.
    ///
    /// Returns a [`StatsRequest`] when the selected resource changed and
    /// exposes live statistics (servers and apps). Returns `None` when
    /// nothing changed; clears the metrics panel when the selection moved
    /// to a resource without live statistics.
    pub fn poll_stats_request(&mut self) -> Option<StatsRequest> {
        let idx = self.selected_real_index();
        let target = match self.active_tab {
            ResourceTab::Servers => self
                .servers
                .get(idx)
                .map(|s| (s.id.to_string(), s.name.clone())),
            ResourceTab::Apps => self
                .apps
                .get(idx)
                .map(|a| (a.id.to_string(), a.name.clone())),
            _ => None
        };

        let target_id = target.as_ref().map(|(id, _)| id.clone());
        if target_id == self.stats_loaded_for {
            return None;
        }
        self.stats_loaded_for = target_id;

        if let Some((id, name)) = target {
            Some(StatsRequest {
                tab: self.active_tab,
                id,
                name
            })
        } else {
            self.clear_stats();
            None
        }
    }

    /// Clears the metrics panel.
    fn clear_stats(&mut self) {
        self.stats_subject = None;
        self.cpu_history.clear();
        self.ram_history.clear();
        self.net_in_history.clear();
        self.net_out_history.clear();
    }

    /// Applies fetched statistics, ignoring stale results whose resource is no
    /// longer selected.
    pub fn apply_stats(&mut self, stats: ResourceStats) {
        if self.stats_loaded_for.as_deref() != Some(stats.id.as_str()) {
            return;
        }
        self.stats_subject = Some(stats.subject);
        self.cpu_history = stats.cpu.into_iter().collect();
        self.ram_history = stats.ram.into_iter().collect();
        self.net_in_history = stats.net_in.into_iter().collect();
        self.net_out_history = stats.net_out.into_iter().collect();
    }

    /// Sets the UI language, applies it live, and marks preferences dirty.
    pub fn set_language(&mut self, language: crate::config::Language) {
        self.language = language;
        rust_i18n::set_locale(language.locale());
        self.prefs_dirty = true;
    }

    /// Returns the number of items currently loaded for the given tab.
    #[must_use]
    pub const fn tab_count(&self, tab: ResourceTab) -> usize {
        match tab {
            ResourceTab::Servers => self.servers.len(),
            ResourceTab::Databases => self.databases.len(),
            ResourceTab::S3 => self.s3_storages.len(),
            ResourceTab::Kubernetes => self.k8s_clusters.len(),
            ResourceTab::Projects => self.projects.len(),
            ResourceTab::Balancers => self.balancers.len(),
            ResourceTab::Registry => self.registries.len(),
            ResourceTab::Domains => self.domains.len(),
            ResourceTab::Firewall => self.firewalls.len(),
            ResourceTab::FloatingIps => self.floating_ips.len(),
            ResourceTab::Images => self.images.len(),
            ResourceTab::NetworkDrives => self.network_drives.len(),
            ResourceTab::Vpc => self.vpcs.len(),
            ResourceTab::DedicatedServers => self.dedicated_servers.len(),
            ResourceTab::Mail => self.mails.len(),
            ResourceTab::Apps => self.apps.len(),
            ResourceTab::AiAgents => self.ai_agents.len(),
            ResourceTab::KnowledgeBases => self.knowledge_bases.len(),
            ResourceTab::SshKeys => self.ssh_keys.len(),
            ResourceTab::Finances => self.finances.len()
        }
    }

    /// Returns the tabs to display: all tabs, or only non-empty ones (plus the
    /// active tab) when empty tabs are hidden.
    #[must_use]
    pub fn visible_tabs(&self) -> Vec<ResourceTab> {
        if !self.hide_empty_tabs {
            return ResourceTab::ALL.to_vec();
        }
        let mut tabs: Vec<ResourceTab> = ResourceTab::ALL
            .into_iter()
            .filter(|t| self.tab_count(*t) > 0 || *t == self.active_tab)
            .collect();
        if tabs.is_empty() {
            tabs.push(self.active_tab);
        }
        tabs
    }

    /// Toggles hiding of empty tabs and marks preferences dirty.
    pub const fn toggle_hide_empty_tabs(&mut self) {
        self.hide_empty_tabs = !self.hide_empty_tabs;
        self.prefs_dirty = true;
    }

    /// Returns the display names of the current tab's items, in list order.
    #[must_use]
    pub fn current_item_names(&self) -> Vec<String> {
        match self.active_tab {
            ResourceTab::Servers => self.servers.iter().map(|s| s.name.clone()).collect(),
            ResourceTab::Databases => self.databases.iter().map(|d| d.name.clone()).collect(),
            ResourceTab::S3 => self.s3_storages.iter().map(|s| s.name.clone()).collect(),
            ResourceTab::Kubernetes => self.k8s_clusters.iter().map(|c| c.name.clone()).collect(),
            ResourceTab::Projects => self.projects.iter().map(|p| p.name.clone()).collect(),
            ResourceTab::Balancers => self.balancers.iter().map(|b| b.name.clone()).collect(),
            ResourceTab::Registry => self.registries.iter().map(|r| r.name.clone()).collect(),
            ResourceTab::Domains => self.domains.iter().map(|d| d.name.clone()).collect(),
            ResourceTab::Firewall => self.firewalls.iter().map(|f| f.name.clone()).collect(),
            ResourceTab::FloatingIps => self.floating_ips.iter().map(|f| f.ip.clone()).collect(),
            ResourceTab::Images => self.images.iter().map(|i| i.name.clone()).collect(),
            ResourceTab::NetworkDrives => {
                self.network_drives.iter().map(|n| n.name.clone()).collect()
            }
            ResourceTab::Vpc => self.vpcs.iter().map(|v| v.name.clone()).collect(),
            ResourceTab::DedicatedServers => self
                .dedicated_servers
                .iter()
                .map(|d| d.name.clone())
                .collect(),
            ResourceTab::Mail => self.mails.iter().map(|m| m.name.clone()).collect(),
            ResourceTab::Apps => self.apps.iter().map(|a| a.name.clone()).collect(),
            ResourceTab::AiAgents => self.ai_agents.iter().map(|a| a.name.clone()).collect(),
            ResourceTab::KnowledgeBases => self
                .knowledge_bases
                .iter()
                .map(|k| k.name.clone())
                .collect(),
            ResourceTab::SshKeys => self.ssh_keys.clone(),
            ResourceTab::Finances => self.finances.clone()
        }
    }

    /// Returns the indices of the current tab's items that match the filter,
    /// in list order. With no filter, returns every index.
    #[must_use]
    pub fn filtered_indices(&self) -> Vec<usize> {
        let names = self.current_item_names();
        if self.filter.is_empty() {
            return (0..names.len()).collect();
        }
        let needle = self.filter.to_lowercase();
        names
            .iter()
            .enumerate()
            .filter(|(_, name)| name.to_lowercase().contains(&needle))
            .map(|(i, _)| i)
            .collect()
    }

    /// Begins filter input for the current list.
    pub const fn start_filter(&mut self) {
        self.filter_editing = true;
        self.selected = 0;
    }

    /// Appends a character to the filter query.
    pub fn filter_push(&mut self, c: char) {
        self.filter.push(c);
        self.selected = 0;
    }

    /// Removes the last filter character; clears the filter when empty.
    pub fn filter_backspace(&mut self) {
        self.filter.pop();
        self.selected = 0;
    }

    /// Applies the filter and leaves input mode (keeps it active for nav).
    pub const fn filter_apply(&mut self) {
        self.filter_editing = false;
    }

    /// Clears the filter entirely and leaves input mode.
    pub fn filter_clear(&mut self) {
        self.filter.clear();
        self.filter_editing = false;
        self.selected = 0;
    }

    /// Returns true when the filter is being typed or is applied.
    #[must_use]
    pub const fn filter_active(&self) -> bool {
        self.filter_editing || !self.filter.is_empty()
    }

    /// Maps the visible selection to the real index into the unfiltered list.
    #[must_use]
    pub fn selected_real_index(&self) -> usize {
        self.filtered_indices()
            .get(self.selected)
            .copied()
            .unwrap_or(0)
    }

    /// Appends an entry to the event log, trimming to the last 200 entries.
    pub fn log(&mut self, level: LogLevel, text: impl Into<String>) {
        if self.logs.len() >= 200 {
            self.logs.pop_front();
        }
        self.logs.push_back(LogEntry {
            level,
            text: text.into()
        });
    }

    /// Returns the currently selected resource list length.
    #[must_use]
    pub fn current_list_len(&self) -> usize {
        self.filtered_indices().len()
    }

    /// Moves selection up.
    pub const fn select_previous(&mut self) {
        if self.selected > 0 {
            self.selected -= 1;
        }
    }

    /// Moves selection down.
    pub fn select_next(&mut self) {
        if self.selected + 1 < self.current_list_len() {
            self.selected += 1;
        }
    }

    /// Cycles to the next visible resource tab, resetting any filter.
    pub fn next_tab(&mut self) {
        let tabs = self.visible_tabs();
        let pos = tabs.iter().position(|t| *t == self.active_tab).unwrap_or(0);
        self.active_tab = tabs[(pos + 1) % tabs.len()];
        self.reset_after_tab_change();
    }

    /// Cycles to the previous visible resource tab, resetting any filter.
    pub fn previous_tab(&mut self) {
        let tabs = self.visible_tabs();
        let pos = tabs.iter().position(|t| *t == self.active_tab).unwrap_or(0);
        self.active_tab = tabs[(pos + tabs.len() - 1) % tabs.len()];
        self.reset_after_tab_change();
    }

    fn reset_after_tab_change(&mut self) {
        self.selected = 0;
        self.filter.clear();
        self.filter_editing = false;
    }

    /// Toggles the help overlay.
    pub const fn toggle_help(&mut self) {
        self.show_help = !self.show_help;
    }

    /// Marks that a refresh is needed immediately.
    pub fn force_refresh(&mut self) {
        self.refresh_requested = true;
        self.log(LogLevel::Info, t!("app.log_manual_refresh").to_string());
    }

    /// Returns true when the refresh interval has elapsed.
    #[must_use]
    #[allow(dead_code)]
    pub fn needs_refresh(&self) -> bool {
        self.last_refresh.elapsed() >= self.refresh_interval
    }

    /// Updates account info.
    pub fn update_account(&mut self, info: AccountInfo) {
        self.account = info;
    }

    /// Updates server data.
    pub fn update_servers(&mut self, servers: Vec<ServerSummary>) {
        self.servers = servers;
        self.clamp_selection();
        self.last_refresh = Instant::now();
    }

    /// Updates database data.
    pub fn update_databases(&mut self, databases: Vec<DatabaseSummary>) {
        self.databases = databases;
        self.clamp_selection();
    }

    /// Updates S3 data.
    pub fn update_s3(&mut self, storages: Vec<S3Summary>) {
        self.s3_storages = storages;
        self.clamp_selection();
    }

    /// Updates Kubernetes data.
    pub fn update_k8s(&mut self, clusters: Vec<K8sSummary>) {
        self.k8s_clusters = clusters;
        self.clamp_selection();
    }

    /// Updates project data.
    pub fn update_projects(&mut self, projects: Vec<ProjectSummary>) {
        self.projects = projects;
        self.clamp_selection();
    }

    /// Updates balancer data.
    // JUSTIFY: Public API method for future API integration.
    #[allow(dead_code)]
    pub fn update_balancers(&mut self, balancers: Vec<BalancerSummary>) {
        self.balancers = balancers;
        self.clamp_selection();
    }

    /// Updates registry data.
    // JUSTIFY: Public API method for future API integration.
    #[allow(dead_code)]
    pub fn update_registries(&mut self, registries: Vec<RegistrySummary>) {
        self.registries = registries;
        self.clamp_selection();
    }

    /// Updates domain data.
    // JUSTIFY: Public API method for future API integration.
    #[allow(dead_code)]
    pub fn update_domains(&mut self, domains: Vec<DomainSummary>) {
        self.domains = domains;
        self.clamp_selection();
    }

    /// Updates firewall data.
    // JUSTIFY: Public API method for future API integration.
    #[allow(dead_code)]
    pub fn update_firewalls(&mut self, firewalls: Vec<FirewallSummary>) {
        self.firewalls = firewalls;
        self.clamp_selection();
    }

    /// Updates floating IP data.
    // JUSTIFY: Public API method for future API integration.
    #[allow(dead_code)]
    pub fn update_floating_ips(&mut self, ips: Vec<FloatingIpSummary>) {
        self.floating_ips = ips;
        self.clamp_selection();
    }

    /// Updates image data.
    // JUSTIFY: Public API method for future API integration.
    #[allow(dead_code)]
    pub fn update_images(&mut self, images: Vec<ImageSummary>) {
        self.images = images;
        self.clamp_selection();
    }

    /// Updates network drive data.
    // JUSTIFY: Public API method for future API integration.
    #[allow(dead_code)]
    pub fn update_network_drives(&mut self, drives: Vec<NetworkDriveSummary>) {
        self.network_drives = drives;
        self.clamp_selection();
    }

    /// Updates VPC data.
    // JUSTIFY: Public API method for future API integration.
    #[allow(dead_code)]
    pub fn update_vpcs(&mut self, vpcs: Vec<VpcSummary>) {
        self.vpcs = vpcs;
        self.clamp_selection();
    }

    /// Updates dedicated server data.
    // JUSTIFY: Public API method for future API integration.
    #[allow(dead_code)]
    pub fn update_dedicated_servers(&mut self, servers: Vec<DedicatedServerSummary>) {
        self.dedicated_servers = servers;
        self.clamp_selection();
    }

    /// Updates mail data.
    // JUSTIFY: Public API method for future API integration.
    #[allow(dead_code)]
    pub fn update_mails(&mut self, mails: Vec<MailSummary>) {
        self.mails = mails;
        self.clamp_selection();
    }

    /// Updates application data.
    // JUSTIFY: Public API method for future API integration.
    #[allow(dead_code)]
    pub fn update_apps(&mut self, apps: Vec<AppSummary>) {
        self.apps = apps;
        self.clamp_selection();
    }

    /// Updates AI agent data.
    // JUSTIFY: Public API method for future API integration.
    #[allow(dead_code)]
    pub fn update_ai_agents(&mut self, agents: Vec<AiAgentSummary>) {
        self.ai_agents = agents;
        self.clamp_selection();
    }

    /// Updates knowledge base data.
    // JUSTIFY: Public API method for future API integration.
    #[allow(dead_code)]
    pub fn update_knowledge_bases(&mut self, bases: Vec<KnowledgeBaseSummary>) {
        self.knowledge_bases = bases;
        self.clamp_selection();
    }

    /// Updates SSH key data.
    // JUSTIFY: Public API method for future API integration.
    #[allow(dead_code)]
    pub fn update_ssh_keys(&mut self, keys: Vec<String>) {
        self.ssh_keys = keys;
        self.clamp_selection();
    }

    /// Updates finances data.
    // JUSTIFY: Public API method for future API integration.
    #[allow(dead_code)]
    pub fn update_finances(&mut self, data: Vec<String>) {
        self.finances = data;
        self.clamp_selection();
    }

    fn clamp_selection(&mut self) {
        let len = self.current_list_len();
        if len == 0 {
            self.selected = 0;
        } else if self.selected >= len {
            self.selected = len - 1;
        }
    }

    /// Appends a CPU sample (rolling 60-point window).
    // JUSTIFY: Part of the public API for future dashboard charts.
    #[allow(dead_code)]
    pub fn push_cpu(&mut self, value: f64) {
        if self.cpu_history.len() >= 60 {
            self.cpu_history.pop_front();
        }
        self.cpu_history.push_back(value);
    }

    /// Appends a RAM sample (rolling 60-point window).
    // JUSTIFY: Part of the public API for future dashboard charts.
    #[allow(dead_code)]
    pub fn push_ram(&mut self, value: f64) {
        if self.ram_history.len() >= 60 {
            self.ram_history.pop_front();
        }
        self.ram_history.push_back(value);
    }

    /// Appends a network-in sample.
    // JUSTIFY: Part of the public API for future dashboard charts.
    #[allow(dead_code)]
    pub fn push_net_in(&mut self, value: f64) {
        if self.net_in_history.len() >= 60 {
            self.net_in_history.pop_front();
        }
        self.net_in_history.push_back(value);
    }

    /// Appends a network-out sample.
    // JUSTIFY: Part of the public API for future dashboard charts.
    #[allow(dead_code)]
    pub fn push_net_out(&mut self, value: f64) {
        if self.net_out_history.len() >= 60 {
            self.net_out_history.pop_front();
        }
        self.net_out_history.push_back(value);
    }

    /// Returns the `(id, name)` of the selected item on the active tab,
    /// for tabs whose resources are addressable by a numeric id.
    #[must_use]
    pub fn selected_resource(&self) -> Option<(i32, String)> {
        let real = *self.filtered_indices().get(self.selected)?;
        match self.active_tab {
            ResourceTab::Servers => self.servers.get(real).map(|s| (s.id, s.name.clone())),
            ResourceTab::Databases => self.databases.get(real).map(|d| (d.id, d.name.clone())),
            ResourceTab::S3 => self.s3_storages.get(real).map(|s| (s.id, s.name.clone())),
            ResourceTab::Kubernetes => self.k8s_clusters.get(real).map(|c| (c.id, c.name.clone())),
            ResourceTab::Balancers => self.balancers.get(real).map(|b| (b.id, b.name.clone())),
            ResourceTab::Registry => self.registries.get(real).map(|r| (r.id, r.name.clone())),
            ResourceTab::Projects => self.projects.get(real).map(|p| (p.id, p.name.clone())),
            ResourceTab::DedicatedServers => self
                .dedicated_servers
                .get(real)
                .map(|d| (d.id, d.name.clone())),
            ResourceTab::AiAgents => self.ai_agents.get(real).map(|a| (a.id, a.name.clone())),
            ResourceTab::Apps => self.apps.get(real).map(|a| (a.id, a.name.clone())),
            ResourceTab::KnowledgeBases => self
                .knowledge_bases
                .get(real)
                .map(|k| (k.id, k.name.clone())),
            _ => None
        }
    }

    /// Opens the context action menu for the selected resource.
    ///
    /// No-op when the active tab declares no actions or nothing is selected.
    pub fn open_action_menu(&mut self) {
        let actions = self.active_tab.actions();
        if actions.is_empty() {
            return;
        }
        if let Some((id, name)) = self.selected_resource() {
            self.action_menu = Some(ActionMenu {
                tab:           self.active_tab,
                resource_id:   id,
                resource_name: name,
                actions:       actions.to_vec(),
                selected:      0
            });
        }
    }

    /// Closes the action menu without choosing anything.
    pub fn close_action_menu(&mut self) {
        self.action_menu = None;
    }

    /// Returns true when the active tab's selected resource can be entered
    /// to reveal contained resources (currently only projects).
    #[must_use]
    pub fn can_drill(&self) -> bool {
        matches!(self.active_tab, ResourceTab::Projects) && self.selected_resource().is_some()
    }

    /// Requests a drill-in into the selected resource; the loop fetches it.
    pub fn request_drill(&mut self) {
        if self.can_drill()
            && let Some((id, name)) = self.selected_resource()
        {
            self.drill_request = Some((self.active_tab, id, name));
        }
    }

    /// Takes the pending drill request for the loop to fetch.
    #[must_use]
    pub const fn take_drill_request(&mut self) -> Option<(ResourceTab, i32, String)> {
        self.drill_request.take()
    }

    /// Opens the drill-in view with fetched contents.
    pub fn open_drill(&mut self, view: DrillView) {
        self.drill = Some(view);
    }

    /// Closes the drill-in view, returning to the resource list.
    pub fn close_drill(&mut self) {
        self.drill = None;
    }

    /// Returns true while a drill-in view is open.
    #[must_use]
    pub const fn drill_open(&self) -> bool {
        self.drill.is_some()
    }

    /// Returns the open drill-in view, for rendering.
    #[must_use]
    pub const fn drill_view(&self) -> Option<&DrillView> {
        self.drill.as_ref()
    }

    /// Moves the drill selection down.
    pub const fn drill_next(&mut self) {
        if let Some(view) = self.drill.as_mut()
            && view.selected + 1 < view.items.len()
        {
            view.selected += 1;
        }
    }

    /// Moves the drill selection up.
    pub const fn drill_previous(&mut self) {
        if let Some(view) = self.drill.as_mut() {
            view.selected = view.selected.saturating_sub(1);
        }
    }

    /// Returns true while the action menu is open.
    #[must_use]
    pub const fn action_menu_open(&self) -> bool {
        self.action_menu.is_some()
    }

    /// Returns the open action menu, for rendering.
    #[must_use]
    pub const fn action_menu(&self) -> Option<&ActionMenu> {
        self.action_menu.as_ref()
    }

    /// Moves the action-menu highlight to the next item (wraps).
    pub const fn menu_next(&mut self) {
        if let Some(menu) = self.action_menu.as_mut()
            && !menu.actions.is_empty()
        {
            menu.selected = (menu.selected + 1) % menu.actions.len();
        }
    }

    /// Moves the action-menu highlight to the previous item (wraps).
    pub const fn menu_previous(&mut self) {
        if let Some(menu) = self.action_menu.as_mut() {
            let len = menu.actions.len();
            if len > 0 {
                menu.selected = (menu.selected + len - 1) % len;
            }
        }
    }

    /// Chooses the highlighted action: destructive actions open a
    /// confirmation prompt, others are queued for dispatch immediately.
    pub fn menu_select(&mut self) {
        let Some(menu) = self.action_menu.take() else {
            return;
        };
        let Some(&action) = menu.actions.get(menu.selected) else {
            return;
        };
        let pending = PendingAction {
            tab:           menu.tab,
            kind:          action,
            resource_id:   menu.resource_id,
            resource_name: menu.resource_name
        };
        if action.is_destructive() {
            self.confirm = Some(pending);
        } else {
            self.dispatch = Some(pending);
        }
    }

    /// Confirms the pending destructive action, queueing it for dispatch.
    pub fn confirm_action(&mut self) {
        self.dispatch = self.confirm.take();
    }

    /// Dismisses the pending action without dispatching it.
    pub fn cancel_action(&mut self) {
        self.confirm = None;
    }

    /// Returns the action awaiting confirmation, for rendering the modal.
    #[must_use]
    pub const fn pending_action(&self) -> Option<&PendingAction> {
        self.confirm.as_ref()
    }

    /// Returns true while a confirmation modal is open.
    #[must_use]
    pub const fn awaiting_confirm(&self) -> bool {
        self.confirm.is_some()
    }

    /// Takes the action queued for dispatch, if the user confirmed one.
    #[must_use]
    pub const fn take_dispatch(&mut self) -> Option<PendingAction> {
        self.dispatch.take()
    }

    /// Widgets the user can show/hide from the layout: `(id, label)`.
    pub const TOGGLEABLE_WIDGETS: [(&'static str, &'static str); 4] = [
        ("account", "Account header"),
        ("stats", "Stats panel"),
        ("token_info", "Token info panel"),
        ("events", "Events log")
    ];

    /// Applies persisted dashboard preferences: hides the given widgets and
    /// sets the resource-list width.
    pub fn apply_prefs(&mut self, hidden: &[String], list_width_pct: u16, hide_empty_tabs: bool) {
        for id in hidden {
            if self.is_widget_enabled(id) {
                self.widgets.toggle(id);
            }
        }
        if (10..=90).contains(&list_width_pct) {
            self.list_width_pct = list_width_pct;
        }
        self.hide_empty_tabs = hide_empty_tabs;
    }

    /// Returns true when the widget with `id` is registered and enabled.
    #[must_use]
    pub fn is_widget_enabled(&self, id: &str) -> bool {
        self.widgets
            .get(id)
            .is_some_and(super::widgets::Widget::enabled)
    }

    /// Toggles a widget's visibility and marks preferences dirty.
    pub fn toggle_widget(&mut self, id: &str) {
        self.widgets.toggle(id);
        self.prefs_dirty = true;
    }

    /// Returns the localized display label for a toggleable widget id.
    #[must_use]
    pub fn widget_display_label(id: &str) -> Cow<'static, str> {
        match id {
            "account" => t!("app.widget_account"),
            "stats" => t!("app.widget_stats"),
            "token_info" => t!("app.widget_token_info"),
            "events" => t!("app.widget_events"),
            _ => Cow::Borrowed("")
        }
    }

    /// Returns the ids of currently hidden toggleable widgets, for persisting.
    #[must_use]
    pub fn hidden_widget_ids(&self) -> Vec<String> {
        Self::TOGGLEABLE_WIDGETS
            .iter()
            .filter(|(id, _)| !self.is_widget_enabled(id))
            .map(|(id, _)| (*id).to_string())
            .collect()
    }

    /// Switches the active theme and marks preferences dirty.
    pub const fn set_theme(&mut self, theme: super::themes::Theme) {
        self.theme = theme;
        self.prefs_dirty = true;
    }

    /// Advances the animation tick (drives skeleton shimmer).
    pub const fn tick(&mut self) {
        self.anim_tick = self.anim_tick.wrapping_add(1);
    }

    /// Returns true while the command palette is open.
    #[must_use]
    pub const fn palette_open(&self) -> bool {
        self.palette.is_some()
    }

    /// Opens the command palette, populated for the current context.
    pub fn open_palette(&mut self) {
        let commands = self.build_palette_commands();
        self.palette = Some(super::command_palette::CommandPalette::new(commands));
    }

    /// Closes the command palette.
    pub fn close_palette(&mut self) {
        self.palette = None;
    }

    /// Feeds a character to the open palette query.
    pub fn palette_input(&mut self, c: char) {
        if let Some(p) = self.palette.as_mut() {
            p.push_char(c);
        }
    }

    /// Deletes the last palette query character.
    pub fn palette_backspace(&mut self) {
        if let Some(p) = self.palette.as_mut() {
            p.backspace();
        }
    }

    /// Moves the palette selection down.
    pub const fn palette_next(&mut self) {
        if let Some(p) = self.palette.as_mut() {
            p.next();
        }
    }

    /// Moves the palette selection up.
    pub const fn palette_previous(&mut self) {
        if let Some(p) = self.palette.as_mut() {
            p.previous();
        }
    }

    /// Runs the highlighted palette command, then closes the palette.
    pub fn palette_run_selected(&mut self) {
        let id = self
            .palette
            .as_ref()
            .and_then(|p| p.selected_command())
            .map(|c| c.id.clone());
        if let Some(id) = id {
            self.run_command(&id);
        }
        self.close_palette();
    }

    fn build_palette_commands(&self) -> Vec<super::command_palette::Command> {
        use super::command_palette::Command;
        let mut commands = Vec::new();

        let general = t!("app.hint_general").to_string();
        for (id, title) in [
            ("app:create", t!("palette.cmd_create")),
            ("app:filter", t!("palette.cmd_filter")),
            ("app:refresh", t!("palette.cmd_refresh")),
            ("app:help", t!("palette.cmd_help")),
            ("app:quit", t!("palette.cmd_quit"))
        ] {
            commands.push(Command {
                id:    id.to_string(),
                title: title.to_string(),
                hint:  general.clone()
            });
        }

        if let Some((_, name)) = self.selected_resource() {
            for action in self.active_tab.actions() {
                commands.push(Command {
                    id:    format!("action:{}", action.label().to_lowercase()),
                    title: format!("{} {name}", action.display_label()),
                    hint:  t!("app.hint_action").to_string()
                });
            }
        }

        for (id, _) in Self::TOGGLEABLE_WIDGETS {
            let verb = if self.is_widget_enabled(id) {
                t!("app.palette_hide")
            } else {
                t!("app.palette_show")
            };
            commands.push(Command {
                id:    format!("widget:{id}"),
                title: format!("{verb} {}", Self::widget_display_label(id)),
                hint:  t!("app.hint_layout").to_string()
            });
        }

        for theme in super::themes::Theme::ALL {
            commands.push(Command {
                id:    format!("theme:{}", theme.id()),
                title: format!("Theme: {}", theme.label()),
                hint:  t!("app.hint_theme").to_string()
            });
        }

        commands.push(Command {
            id:    "tabs:toggle_empty".to_string(),
            title: if self.hide_empty_tabs {
                t!("app.palette_show_empty_tabs").to_string()
            } else {
                t!("app.palette_hide_empty_tabs").to_string()
            },
            hint:  t!("app.hint_layout").to_string()
        });

        commands.push(Command {
            id:    "lang:en".to_string(),
            title: rust_i18n::t!("palette.language_english").to_string(),
            hint:  t!("app.hint_language").to_string()
        });
        commands.push(Command {
            id:    "lang:ru".to_string(),
            title: rust_i18n::t!("palette.language_russian").to_string(),
            hint:  t!("app.hint_language").to_string()
        });

        for profile in &self.profiles {
            if *profile == self.active_profile {
                continue;
            }
            commands.push(Command {
                id:    format!("profile:{profile}"),
                title: t!("palette.switch_profile", name => profile).to_string(),
                hint:  t!("app.hint_account").to_string()
            });
        }

        commands
    }

    fn run_command(&mut self, id: &str) {
        if id == "app:help" {
            self.toggle_help();
        } else if id == "app:refresh" {
            self.force_refresh();
        } else if id == "app:filter" {
            self.start_filter();
        } else if id == "app:create" {
            self.open_create_form();
        } else if id == "app:quit" {
            self.quit();
        } else if id == "tabs:toggle_empty" {
            self.toggle_hide_empty_tabs();
        } else if id == "lang:en" {
            self.set_language(crate::config::Language::En);
        } else if id == "lang:ru" {
            self.set_language(crate::config::Language::Ru);
        } else if let Some(profile) = id.strip_prefix("profile:") {
            self.switch_profile = Some(profile.to_string());
        } else if let Some(rest) = id.strip_prefix("theme:") {
            if let Some(theme) = super::themes::Theme::ALL
                .into_iter()
                .find(|t| t.id() == rest)
            {
                self.set_theme(theme);
            }
        } else if let Some(widget_id) = id.strip_prefix("widget:") {
            self.toggle_widget(widget_id);
        } else if let Some(action_label) = id.strip_prefix("action:")
            && let Some((resource_id, resource_name)) = self.selected_resource()
            && let Some(&kind) = self
                .active_tab
                .actions()
                .iter()
                .find(|a| a.label().eq_ignore_ascii_case(action_label))
        {
            let pending = PendingAction {
                tab: self.active_tab,
                kind,
                resource_id,
                resource_name
            };
            if kind.is_destructive() {
                self.confirm = Some(pending);
            } else {
                self.dispatch = Some(pending);
            }
        }
    }

    /// Quits the application.
    pub const fn quit(&mut self) {
        self.running = false;
    }

    /// Applies a freshly fetched data snapshot, replacing all resource
    /// lists in one shot and clearing the loading state.
    pub fn apply_data(&mut self, data: DashboardData) {
        self.account = data.account;
        self.servers = data.servers;
        self.databases = data.databases;
        self.s3_storages = data.s3_storages;
        self.k8s_clusters = data.k8s_clusters;
        self.projects = data.projects;
        self.balancers = data.balancers;
        self.registries = data.registries;
        self.domains = data.domains;
        self.firewalls = data.firewalls;
        self.floating_ips = data.floating_ips;
        self.images = data.images;
        self.network_drives = data.network_drives;
        self.vpcs = data.vpcs;
        self.dedicated_servers = data.dedicated_servers;
        self.mails = data.mails;
        self.apps = data.apps;
        self.ai_agents = data.ai_agents;
        self.knowledge_bases = data.knowledge_bases;
        self.ssh_keys = data.ssh_keys;
        self.finances = data.finances;
        if data.error_message.is_some() {
            self.error_message = data.error_message;
        } else {
            self.error_message = None;
            self.status_message = data.status_message;
        }
        if data.load_errors != self.last_load_errors {
            let recovered: Vec<String> = self
                .last_load_errors
                .iter()
                .filter(|r| !data.load_errors.contains(r))
                .cloned()
                .collect();
            for entry in &data.load_errors {
                self.log(
                    LogLevel::Error,
                    t!("app.log_load_failed", entry => entry).to_string()
                );
            }
            for entry in recovered {
                let name = entry.split(':').next().unwrap_or(&entry);
                self.log(
                    LogLevel::Success,
                    t!("app.log_recovered", name => name).to_string()
                );
            }
            self.last_load_errors = data.load_errors;
        }
        self.clamp_selection();
        self.is_loading = false;
        self.last_refresh = Instant::now();
    }
}

/// An owned snapshot of all dashboard data, fetched off the UI thread and
/// applied via [`App::apply_data`]. Cloned out of a throwaway [`App`] by the
/// background refresh task.
#[derive(Debug, Clone)]
pub struct DashboardData {
    pub account:           AccountInfo,
    pub servers:           Vec<ServerSummary>,
    pub databases:         Vec<DatabaseSummary>,
    pub s3_storages:       Vec<S3Summary>,
    pub k8s_clusters:      Vec<K8sSummary>,
    pub projects:          Vec<ProjectSummary>,
    pub balancers:         Vec<BalancerSummary>,
    pub registries:        Vec<RegistrySummary>,
    pub domains:           Vec<DomainSummary>,
    pub firewalls:         Vec<FirewallSummary>,
    pub floating_ips:      Vec<FloatingIpSummary>,
    pub images:            Vec<ImageSummary>,
    pub network_drives:    Vec<NetworkDriveSummary>,
    pub vpcs:              Vec<VpcSummary>,
    pub dedicated_servers: Vec<DedicatedServerSummary>,
    pub mails:             Vec<MailSummary>,
    pub apps:              Vec<AppSummary>,
    pub ai_agents:         Vec<AiAgentSummary>,
    pub knowledge_bases:   Vec<KnowledgeBaseSummary>,
    pub ssh_keys:          Vec<String>,
    pub finances:          Vec<String>,
    pub error_message:     Option<String>,
    pub status_message:    Option<String>,
    pub load_errors:       Vec<String>
}

impl DashboardData {
    /// Clones the resource data out of an [`App`] populated by `refresh_all`.
    #[must_use]
    pub fn from_app(app: &App) -> Self {
        Self {
            account:           app.account.clone(),
            servers:           app.servers.clone(),
            databases:         app.databases.clone(),
            s3_storages:       app.s3_storages.clone(),
            k8s_clusters:      app.k8s_clusters.clone(),
            projects:          app.projects.clone(),
            balancers:         app.balancers.clone(),
            registries:        app.registries.clone(),
            domains:           app.domains.clone(),
            firewalls:         app.firewalls.clone(),
            floating_ips:      app.floating_ips.clone(),
            images:            app.images.clone(),
            network_drives:    app.network_drives.clone(),
            vpcs:              app.vpcs.clone(),
            dedicated_servers: app.dedicated_servers.clone(),
            mails:             app.mails.clone(),
            apps:              app.apps.clone(),
            ai_agents:         app.ai_agents.clone(),
            knowledge_bases:   app.knowledge_bases.clone(),
            ssh_keys:          app.ssh_keys.clone(),
            finances:          app.finances.clone(),
            error_message:     app.error_message.clone(),
            status_message:    app.status_message.clone(),
            load_errors:       app.last_load_errors.clone()
        }
    }
}

#[cfg(test)]
mod tests;