evault-tui 0.1.0

Terminal user interface for evault.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
//! Deterministic state machine driving the dashboard.

use evault_core::model::{Group, VarId, VarKind};
use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
use ratatui::widgets::TableState;
use secrecy::SecretString;

use crate::event::Action;
use crate::filter::FilterState;
use crate::provider::{ProviderError, VarDraft, VarProvider, VarSummary};

/// In-session toast displayed at the bottom of the screen.
///
/// `Toast` is part of the *crate*-internal API. External callers
/// inspect toast state via [`AppState::toast_text`] /
/// [`AppState::toast_is_error`] and cannot construct or pattern-match
/// on the inner kind directly. Keeping these types private lets the
/// toast model evolve (e.g. severity levels, timeouts) without an API
/// break.
#[allow(clippy::redundant_pub_crate)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Toast {
    pub(crate) text: String,
    pub(crate) kind: ToastKind,
}

/// Whether a toast represents a user-recoverable info message or an
/// error worth keeping on-screen until explicitly dismissed.
///
/// See [`Toast`] — crate-internal.
#[allow(clippy::redundant_pub_crate)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ToastKind {
    /// Neutral, informational message. Auto-dismissed on the next
    /// non-`Noop` interaction so it does not pile up in front of the
    /// user.
    Info,
    /// Failure surfaced from the provider or another subsystem.
    /// Sticky: only dismissed by an explicit `Action::Dismiss` or
    /// `Action::Refresh` so the user has time to read the message
    /// even if they keep typing.
    Error,
}

/// Overlay currently on top of the main view.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Overlay {
    None,
    Help,
}

/// Top-level view currently displayed by the runtime.
///
/// Views are mutually exclusive: at any moment the dashboard is
/// either showing the table or the per-variable detail screen, not
/// both. Overlays (help, modals) layer on top of *either* view.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum View {
    /// Variable list — the default screen.
    Dashboard,
    /// Read-only inspection of the row that was selected when the
    /// user pressed Enter.
    Detail,
}

/// Dashboard state.
///
/// `AppState` is a pure value: every public method is a function of
/// `(state, input) -> state'`, so the whole UI can be exercised in
/// unit tests without ever touching a terminal. The runtime
/// ([`crate::run_tui`]) is the only piece that performs I/O.
#[derive(Debug)]
pub struct AppState {
    rows: Vec<VarSummary>,
    table_state: TableState,
    overlay: Overlay,
    toast: Option<Toast>,
    secrets_visible: bool,
    quit: bool,
    filter: Option<FilterState>,
    view: View,
    /// `Some(id)` while `view == Detail`. Tracking the inspected
    /// variable by id (rather than by selection index) keeps the
    /// Detail screen from silently re-pointing at a different row
    /// when an external mutation reshuffles the row buffer. Cleared
    /// on every return to the dashboard.
    detail_target: Option<VarId>,
    /// Modal confirmation request currently focused. When `Some`,
    /// [`Self::dispatch_key`] routes all keys to the confirm-modal
    /// handler instead of the normal Action / filter paths.
    confirm: Option<ConfirmRequest>,
    /// Editor form (modal popup) currently focused. When `Some`,
    /// [`Self::dispatch_key`] routes typed characters and editing
    /// keys to this form and bypasses the Action / filter / modal
    /// paths.
    form: Option<EditorForm>,
    /// Link-to-project form (modal popup) currently focused. Same
    /// focus-stealing semantics as `form` above.
    link_form: Option<LinkForm>,
    /// Run-in-project form (modal popup) currently focused. Captures
    /// the project path, the profile, and the command line to spawn.
    run_form: Option<RunForm>,
    /// Read-only view-value modal currently focused. Shows a
    /// variable's decrypted value; closed by Esc.
    view_value: Option<ViewValueModal>,
    /// Error modal currently focused. Surfaces an action failure
    /// with a contextual hint. Dismissed by Esc / Enter.
    error_modal: Option<ErrorModal>,
}

/// Outcome of [`AppState::dispatch_key`]: signals whether the caller
/// (the runtime) should perform an I/O side effect after the state
/// has been updated.
#[derive(Debug, Clone)]
pub enum DispatchOutcome {
    /// Continue the event loop without further side effects.
    Continue,
    /// Re-fetch rows from the provider; the dispatch translated a
    /// `Refresh` intent. The runtime owns the provider and is
    /// responsible for the actual call.
    RefreshRequested,
    /// The user confirmed deletion of the variable identified by
    /// `id`; the runtime should call
    /// [`VarMutator::delete`](crate::VarMutator::delete) and then
    /// refresh. `name` is carried along for the post-delete success
    /// toast so the runtime does not have to look it up after the row
    /// is gone.
    DeleteRequested {
        /// Variable identifier the user confirmed deleting.
        id: VarId,
        /// Human-readable name, for use in the post-delete toast.
        name: String,
    },
    /// The user submitted the new-var prompt; the runtime should call
    /// [`crate::VarMutator::create`] and refresh on success.
    CreateRequested(VarDraft),
    /// The user submitted the edit-value prompt; the runtime should
    /// call [`crate::VarMutator::update_value`] and refresh on success.
    /// `name` is carried for the post-update toast.
    UpdateValueRequested {
        /// Variable to update.
        id: VarId,
        /// New value.
        value: SecretString,
        /// Human-readable name for the success toast.
        name: String,
    },
    /// The user submitted the link form; the runtime should call
    /// [`crate::VarMutator::link_to_project`] and refresh on success.
    LinkRequested {
        /// Variable being linked.
        id: VarId,
        /// Variable's display name (for the success toast).
        name: String,
        /// Project path the user typed.
        project_path: std::path::PathBuf,
        /// Profile name to use for the binding.
        profile: String,
        /// Whether to also materialize `.env` after linking.
        materialize: bool,
    },
    /// The user asked to view the value of a variable. The runtime
    /// should fetch via [`crate::VarProvider::get_value`] and then
    /// call [`AppState::show_value_modal`] with the result.
    ViewValueRequested {
        /// Variable id whose value to fetch.
        id: VarId,
        /// Display name for the modal title.
        name: String,
    },
    /// The user submitted the run-in-project form. The runtime should
    /// restore the terminal, call
    /// [`crate::VarMutator::run_in_project`], and re-init the TUI
    /// afterwards.
    RunRequested {
        /// Project path to load the manifest from.
        project_path: std::path::PathBuf,
        /// Profile to resolve bindings under.
        profile: String,
        /// Program to spawn.
        program: String,
        /// Arguments forwarded to the program.
        args: Vec<String>,
    },
}

/// Active editor form — modal popup for the `n` (new var) and `e`
/// (edit value) flows. Replaces the bottom-strip prompt with a
/// centered window that has multiple fields (name / group / kind /
/// value).
#[allow(clippy::redundant_pub_crate)]
#[derive(Debug, Clone)]
pub(crate) struct EditorForm {
    /// What the form is collecting.
    pub(crate) mode: EditorMode,
    /// Variable name. Editable only in `NewVar` mode.
    pub(crate) name: String,
    /// Variable value (the secret material). Always editable.
    pub(crate) value: String,
    /// Index into [`GROUP_CYCLE`].
    pub(crate) group_idx: usize,
    /// Index into [`KIND_CYCLE`].
    pub(crate) kind_idx: usize,
    /// Currently-focused field — receives typing / arrow input.
    pub(crate) focus: FormField,
    /// Whether the value field should be rendered verbatim (true)
    /// or as `*` characters (false). Defaults to false for Secret
    /// kind, true for Plain.
    pub(crate) show_value: bool,
}

/// What the editor form is collecting.
#[allow(clippy::redundant_pub_crate)]
#[derive(Debug, Clone)]
pub(crate) enum EditorMode {
    /// Creating a new variable. All four fields are editable.
    NewVar,
    /// Replacing the value of an existing variable. The name /
    /// group / kind are display-only; only the value field accepts
    /// input.
    EditValue {
        /// Target variable id (snapshotted at form-open time).
        id: VarId,
        /// Original variable name, shown read-only.
        original_name: String,
    },
}

/// Field currently focused inside [`EditorForm`].
#[allow(clippy::redundant_pub_crate)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum FormField {
    Name,
    Group,
    Kind,
    Value,
}

/// Groups exposed by the TUI cycler. Custom groups remain available
/// via the CLI's `--group` flag.
#[allow(clippy::redundant_pub_crate)]
pub(crate) const GROUP_CYCLE: &[Group] = &[Group::User, Group::System, Group::Project];

/// Kinds exposed by the TUI cycler.
#[allow(clippy::redundant_pub_crate)]
pub(crate) const KIND_CYCLE: &[VarKind] = &[VarKind::Secret, VarKind::Plain];

/// Link-to-project form — modal popup for the `l` flow.
///
/// Captures the project path, the profile name, and whether to
/// materialise the project's `.env` immediately after linking.
#[allow(clippy::redundant_pub_crate)]
#[derive(Debug, Clone)]
pub(crate) struct LinkForm {
    /// Variable being linked (id snapshotted at form-open time).
    pub(crate) var_id: VarId,
    /// Display name for the form title + success toast.
    pub(crate) var_name: String,
    /// Filesystem path the user has typed.
    pub(crate) path: String,
    /// Profile name (defaults to `default`).
    pub(crate) profile: String,
    /// Whether to materialise `.env` right after linking.
    pub(crate) materialize: bool,
    /// Field currently focused.
    pub(crate) focus: LinkField,
}

/// Field currently focused inside [`LinkForm`].
#[allow(clippy::redundant_pub_crate)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum LinkField {
    Path,
    Profile,
    Materialize,
}

/// Run-in-project form — modal popup for the `R` flow.
///
/// Captures the project path, the profile name, and the command line
/// (program + args) to spawn with the project's resolved environment
/// overlay injected.
#[allow(clippy::redundant_pub_crate)]
#[derive(Debug, Clone)]
pub(crate) struct RunForm {
    /// Filesystem path the user has typed.
    pub(crate) path: String,
    /// Profile name (defaults to `default`).
    pub(crate) profile: String,
    /// Raw command line as typed by the user.
    ///
    /// Tokenised by whitespace at submit time. Quoted arguments are
    /// NOT supported — users with complex shell quoting needs should
    /// fall back to the `evault run` CLI command.
    pub(crate) command: String,
    /// Field currently focused.
    pub(crate) focus: RunField,
}

/// Field currently focused inside [`RunForm`].
#[allow(clippy::redundant_pub_crate)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum RunField {
    Path,
    Profile,
    Command,
}

/// Error modal — focused popup that surfaces an action failure
/// (failed create / edit / delete / link) with an explanatory hint.
///
/// Replaces a sticky error toast for cases where the user needs to
/// actively acknowledge the failure (the toast is too easy to miss
/// when an action they just initiated fails).
#[allow(clippy::redundant_pub_crate)]
#[derive(Debug, Clone)]
pub(crate) struct ErrorModal {
    /// Short title, e.g. `"create failed"` or `"link failed"`.
    pub(crate) title: String,
    /// The raw error message from the backend.
    pub(crate) message: String,
    /// Optional contextual hint explaining the failure and how to
    /// fix it (e.g. naming rules when the create rejected the name).
    pub(crate) hint: Option<String>,
}

/// View-value modal — popup showing a variable's decrypted value
/// after the user pressed `v`. The runtime fetches the value and
/// calls [`AppState::show_value_modal`] which inserts this struct.
#[allow(clippy::redundant_pub_crate)]
#[derive(Debug)]
pub(crate) struct ViewValueModal {
    /// Variable name for the modal title.
    pub(crate) name: String,
    /// The decrypted value. Held in `SecretString` so it gets
    /// zeroized on drop.
    pub(crate) value: SecretString,
    /// Whether the value is currently rendered verbatim (`true`)
    /// or as `*` characters. Toggle with `Ctrl+S` while open.
    pub(crate) show: bool,
}

/// Modal confirmation request — internal state for the y/n overlay.
///
/// Crate-private: external callers don't construct these; they are
/// raised by [`AppState`] in response to user input and rendered by
/// the views layer.
#[allow(clippy::redundant_pub_crate)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ConfirmRequest {
    pub(crate) title: String,
    pub(crate) body: String,
    pub(crate) action: PendingAction,
}

/// Action to perform when a [`ConfirmRequest`] is accepted.
#[allow(clippy::redundant_pub_crate)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum PendingAction {
    /// Delete the named variable; the runtime resolves via
    /// [`VarMutator::delete`](crate::VarMutator::delete).
    DeleteVar { id: VarId, name: String },
}

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

impl AppState {
    /// Construct an empty state with no rows loaded yet.
    ///
    /// Call [`Self::refresh`] before rendering for the first time to
    /// populate the dashboard. Until then [`Self::selected_index`]
    /// returns `None` (rather than `Some(0)` pointing at non-existent
    /// row 0) so callers cannot accidentally index into an empty
    /// buffer.
    #[must_use]
    pub fn new() -> Self {
        // Selection begins at `None`. `clamp_selection` (run from
        // `refresh`) re-anchors to row 0 once rows are loaded.
        Self {
            rows: Vec::new(),
            table_state: TableState::default(),
            overlay: Overlay::None,
            toast: None,
            secrets_visible: false,
            quit: false,
            filter: None,
            view: View::Dashboard,
            detail_target: None,
            confirm: None,
            form: None,
            link_form: None,
            run_form: None,
            view_value: None,
            error_modal: None,
        }
    }

    /// Re-read rows from `provider` and re-anchor the selection.
    ///
    /// On success the dashboard's row buffer is replaced. The cursor
    /// is clamped to `[0, rows.len())`: if the previously-selected
    /// index is still in range it survives; if the new row count is
    /// smaller the cursor pins to the last surviving row; if the
    /// dashboard is now empty the cursor is cleared to `None`.
    ///
    /// On failure the previous rows are preserved and the error is
    /// returned unchanged — the runtime decides whether to display it
    /// as a toast.
    ///
    /// Note: re-anchoring is *by index*, not by [`evault_core::model::VarId`].
    /// In phase 1 the dashboard is read-only, so external mutation
    /// during a refresh can silently shift the user's selection by one
    /// row. Phase 2 will track the selected `VarId` and re-anchor by
    /// identity once CRUD is wired.
    ///
    /// # Errors
    /// Propagates whatever [`ProviderError`] the provider returns.
    pub fn refresh<P: VarProvider + ?Sized>(&mut self, provider: &P) -> Result<(), ProviderError> {
        let rows = provider.list()?;
        self.rows = rows;
        self.rebuild_filter();
        self.clamp_selection();
        // Detail-target validation. If the user is inspecting a
        // variable that just disappeared (external delete, profile
        // switch, etc.) auto-return to the dashboard with a loud
        // error toast. Without this check the Detail pane would
        // silently re-anchor by index and start showing a
        // *different* variable under the same screen header — the
        // exact silent-failure mode the audit charter forbids.
        if matches!(self.view, View::Detail) && !self.detail_target_is_present() {
            self.view = View::Dashboard;
            self.detail_target = None;
            self.set_error_toast("variable removed elsewhere \u{2014} returned to dashboard");
        }
        Ok(())
    }

    fn detail_target_is_present(&self) -> bool {
        let Some(target) = self.detail_target else {
            return false;
        };
        self.rows.iter().any(|v| v.id == target)
    }

    /// Dispatch a raw key event.
    ///
    /// When the filter input is active, characters and Backspace edit
    /// the needle, Enter accepts (filter stays applied but the input
    /// is closed), and Esc cancels the filter entirely. Navigation
    /// keys (Up / Down / `PageUp` / `PageDown`) and Ctrl-C remain bound
    /// so the user can scroll through results and quit even while
    /// typing.
    ///
    /// When the filter input is **not** active, the key is translated
    /// via [`Action::from_key`] and dispatched to [`Self::apply`].
    ///
    /// Returns [`DispatchOutcome::RefreshRequested`] when the user
    /// pressed `r` (or otherwise triggered `Action::Refresh`); the
    /// runtime is responsible for the actual provider call.
    pub fn dispatch_key(&mut self, key: KeyEvent) -> DispatchOutcome {
        if key.kind != KeyEventKind::Press {
            return DispatchOutcome::Continue;
        }
        // Error modal: takes priority over everything else so the
        // user has to acknowledge an action failure before continuing.
        if self.error_modal.is_some() {
            return self.dispatch_error_modal_key(key);
        }
        // Modal confirm steals focus from everything else: when the
        // user is being asked "are you sure?", any other action would
        // be ambiguous.
        if self.confirm.is_some() {
            return self.dispatch_confirm_key(key);
        }
        // View-value modal: read-only popup, only Esc / Ctrl+S / Ctrl+C
        // make sense while it's focused.
        if self.view_value.is_some() {
            return self.dispatch_view_value_key(key);
        }
        // Link form: modal popup capturing path + profile + materialize.
        if self.link_form.is_some() {
            return self.dispatch_link_form_key(key);
        }
        // Run form: modal popup capturing path + profile + command line.
        if self.run_form.is_some() {
            return self.dispatch_run_form_key(key);
        }
        // Editor form: typed characters go to its fields.
        if self.form.is_some() {
            return self.dispatch_form_key(key);
        }
        if self.is_filter_input_active() {
            return self.dispatch_filter_input_key(key);
        }
        let action = Action::from_key(key);
        // `ViewValue` is special: it needs to emit a runtime request
        // carrying the selected row's id, which `apply` cannot
        // express. We intercept it here.
        if matches!(action, Action::ViewValue) {
            if let Some((id, name)) = self.request_view_value() {
                return DispatchOutcome::ViewValueRequested { id, name };
            }
            return DispatchOutcome::Continue;
        }
        self.apply(action);
        if matches!(action, Action::Refresh) {
            DispatchOutcome::RefreshRequested
        } else {
            DispatchOutcome::Continue
        }
    }

    /// Handle a key while the editor form modal is focused.
    ///
    /// - `Tab` / `Shift+Tab` — cycle focus across the four fields.
    /// - `Enter` — submit (validates; on failure leaves the form
    ///   open and surfaces a sticky error toast).
    /// - `Esc` — cancel.
    /// - `Ctrl+C` — quit (escape hatch).
    /// - On `Name` / `Value` focus: typed characters append, Backspace
    ///   pops.
    /// - On `Group` / `Kind` focus: Left / Right arrows + Space cycle
    ///   the option.
    /// - `s` on `Value` focus toggles secret-value masking (kept as
    ///   typed but rendered with `*`).
    fn dispatch_form_key(&mut self, key: KeyEvent) -> DispatchOutcome {
        let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
        if matches!(key.code, KeyCode::Char('c')) && ctrl {
            self.quit = true;
            return DispatchOutcome::Continue;
        }
        match key.code {
            KeyCode::Esc => {
                self.form = None;
                DispatchOutcome::Continue
            }
            KeyCode::Enter => self.submit_form(),
            KeyCode::Tab => {
                if let Some(form) = self.form.as_mut() {
                    form.focus = next_focus(form.focus, &form.mode);
                }
                DispatchOutcome::Continue
            }
            KeyCode::BackTab => {
                if let Some(form) = self.form.as_mut() {
                    form.focus = prev_focus(form.focus, &form.mode);
                }
                DispatchOutcome::Continue
            }
            _ => {
                if let Some(form) = self.form.as_mut() {
                    handle_field_key(form, key);
                }
                DispatchOutcome::Continue
            }
        }
    }

    /// Validate the editor form's current contents and emit the
    /// matching outcome. On validation failure, surfaces a sticky
    /// error toast and leaves the form open with the entered data
    /// preserved.
    fn submit_form(&mut self) -> DispatchOutcome {
        let Some(form) = self.form.take() else {
            return DispatchOutcome::Continue;
        };
        // Reach into the cycle slices to recover the typed options.
        // Indices are bounded at construction + key handling, so
        // out-of-range here is unreachable; we clamp defensively.
        let group = GROUP_CYCLE
            .get(form.group_idx.min(GROUP_CYCLE.len() - 1))
            .cloned()
            .unwrap_or(Group::User);
        let kind = *KIND_CYCLE
            .get(form.kind_idx.min(KIND_CYCLE.len() - 1))
            .unwrap_or(&VarKind::Secret);

        match form.mode.clone() {
            EditorMode::NewVar => {
                if form.name.trim().is_empty() {
                    self.set_error_toast("name must be non-empty (Esc to cancel)");
                    self.form = Some(EditorForm {
                        focus: FormField::Name,
                        ..form
                    });
                    return DispatchOutcome::Continue;
                }
                if form.value.is_empty() {
                    self.set_error_toast("value must be non-empty (Esc to cancel)");
                    self.form = Some(EditorForm {
                        focus: FormField::Value,
                        ..form
                    });
                    return DispatchOutcome::Continue;
                }
                DispatchOutcome::CreateRequested(VarDraft {
                    name: form.name.trim().to_owned(),
                    group,
                    kind,
                    value: SecretString::new(form.value.into()),
                })
            }
            EditorMode::EditValue { id, original_name } => {
                if form.value.is_empty() {
                    self.set_error_toast("value must be non-empty (Esc to cancel)");
                    self.form = Some(EditorForm {
                        focus: FormField::Value,
                        ..form
                    });
                    return DispatchOutcome::Continue;
                }
                DispatchOutcome::UpdateValueRequested {
                    id,
                    value: SecretString::new(form.value.into()),
                    name: original_name,
                }
            }
        }
    }

    /// Handle a key while the view-value modal is focused.
    /// `Esc` closes; `Ctrl+S` toggles masking; `Ctrl+C` quits.
    fn dispatch_view_value_key(&mut self, key: KeyEvent) -> DispatchOutcome {
        let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
        if matches!(key.code, KeyCode::Char('c')) && ctrl {
            self.quit = true;
            return DispatchOutcome::Continue;
        }
        match key.code {
            KeyCode::Esc | KeyCode::Enter => {
                self.view_value = None;
            }
            KeyCode::Char('s') if ctrl => {
                if let Some(modal) = self.view_value.as_mut() {
                    modal.show = !modal.show;
                }
            }
            _ => {}
        }
        DispatchOutcome::Continue
    }

    /// Handle a key while the link form modal is focused.
    fn dispatch_link_form_key(&mut self, key: KeyEvent) -> DispatchOutcome {
        let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
        if matches!(key.code, KeyCode::Char('c')) && ctrl {
            self.quit = true;
            return DispatchOutcome::Continue;
        }
        match key.code {
            KeyCode::Esc => {
                self.link_form = None;
                DispatchOutcome::Continue
            }
            KeyCode::Enter => self.submit_link_form(),
            KeyCode::Tab => {
                if let Some(form) = self.link_form.as_mut() {
                    form.focus = match form.focus {
                        LinkField::Path => LinkField::Profile,
                        LinkField::Profile => LinkField::Materialize,
                        LinkField::Materialize => LinkField::Path,
                    };
                }
                DispatchOutcome::Continue
            }
            KeyCode::BackTab => {
                if let Some(form) = self.link_form.as_mut() {
                    form.focus = match form.focus {
                        LinkField::Path => LinkField::Materialize,
                        LinkField::Profile => LinkField::Path,
                        LinkField::Materialize => LinkField::Profile,
                    };
                }
                DispatchOutcome::Continue
            }
            _ => {
                if let Some(form) = self.link_form.as_mut() {
                    handle_link_field_key(form, key);
                }
                DispatchOutcome::Continue
            }
        }
    }

    fn submit_link_form(&mut self) -> DispatchOutcome {
        let Some(form) = self.link_form.take() else {
            return DispatchOutcome::Continue;
        };
        let path = form.path.trim();
        if path.is_empty() {
            self.set_error_toast("project path must be non-empty (Esc to cancel)");
            self.link_form = Some(LinkForm {
                focus: LinkField::Path,
                ..form
            });
            return DispatchOutcome::Continue;
        }
        let profile = if form.profile.trim().is_empty() {
            "default".to_owned()
        } else {
            form.profile.trim().to_owned()
        };
        DispatchOutcome::LinkRequested {
            id: form.var_id,
            name: form.var_name,
            project_path: std::path::PathBuf::from(path),
            profile,
            materialize: form.materialize,
        }
    }

    /// Handle a key while the run-in-project form modal is focused.
    fn dispatch_run_form_key(&mut self, key: KeyEvent) -> DispatchOutcome {
        let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
        if matches!(key.code, KeyCode::Char('c')) && ctrl {
            self.quit = true;
            return DispatchOutcome::Continue;
        }
        match key.code {
            KeyCode::Esc => {
                self.run_form = None;
                DispatchOutcome::Continue
            }
            KeyCode::Enter => self.submit_run_form(),
            KeyCode::Tab => {
                if let Some(form) = self.run_form.as_mut() {
                    form.focus = match form.focus {
                        RunField::Path => RunField::Profile,
                        RunField::Profile => RunField::Command,
                        RunField::Command => RunField::Path,
                    };
                }
                DispatchOutcome::Continue
            }
            KeyCode::BackTab => {
                if let Some(form) = self.run_form.as_mut() {
                    form.focus = match form.focus {
                        RunField::Path => RunField::Command,
                        RunField::Profile => RunField::Path,
                        RunField::Command => RunField::Profile,
                    };
                }
                DispatchOutcome::Continue
            }
            _ => {
                if let Some(form) = self.run_form.as_mut() {
                    handle_run_field_key(form, key);
                }
                DispatchOutcome::Continue
            }
        }
    }

    /// Validate the form's contents and emit a [`DispatchOutcome::RunRequested`]
    /// or — if validation fails — re-open the form with an info toast.
    fn submit_run_form(&mut self) -> DispatchOutcome {
        let Some(form) = self.run_form.take() else {
            return DispatchOutcome::Continue;
        };
        let path = form.path.trim();
        if path.is_empty() {
            self.set_error_toast("project path must be non-empty (Esc to cancel)");
            self.run_form = Some(RunForm {
                focus: RunField::Path,
                ..form
            });
            return DispatchOutcome::Continue;
        }
        let command_trim = form.command.trim();
        if command_trim.is_empty() {
            self.set_error_toast("command line must be non-empty (Esc to cancel)");
            self.run_form = Some(RunForm {
                focus: RunField::Command,
                ..form
            });
            return DispatchOutcome::Continue;
        }
        let mut tokens = command_trim.split_whitespace();
        // `command_trim` is non-empty, so the first token exists.
        let program = tokens.next().unwrap_or("").to_owned();
        let args: Vec<String> = tokens.map(str::to_owned).collect();
        let profile = if form.profile.trim().is_empty() {
            "default".to_owned()
        } else {
            form.profile.trim().to_owned()
        };
        DispatchOutcome::RunRequested {
            project_path: std::path::PathBuf::from(path),
            profile,
            program,
            args,
        }
    }

    /// Open the run-in-project form modal. Unlike the link form, the
    /// run form is per-project rather than per-var, so no row needs to
    /// be selected — the user types the project path explicitly.
    fn open_run_form(&mut self) {
        self.run_form = Some(RunForm {
            path: String::new(),
            profile: "default".to_owned(),
            command: String::new(),
            focus: RunField::Path,
        });
    }

    /// Whether the run-in-project form modal is currently focused.
    #[must_use]
    pub const fn is_run_form_visible(&self) -> bool {
        self.run_form.is_some()
    }

    /// Read-only access to the focused run-form (for the views layer).
    pub(crate) const fn current_run_form(&self) -> Option<&RunForm> {
        self.run_form.as_ref()
    }

    /// Open the link-form modal for the currently-targeted variable.
    /// No-op (with info toast) if there is no row selected.
    fn open_link_form(&mut self) {
        let target = match self.view {
            View::Dashboard => self.selected_row(),
            View::Detail => self.detail_row(),
        };
        let Some(var) = target else {
            if !self.toast_is_error() {
                self.set_info_toast("no row selected");
            }
            return;
        };
        self.link_form = Some(LinkForm {
            var_id: var.id,
            var_name: var.name.clone(),
            path: String::new(),
            profile: "default".to_owned(),
            materialize: false,
            focus: LinkField::Path,
        });
    }

    /// Trigger a value-view request for the currently-targeted row.
    /// Returns the outcome the runtime should act on; called from
    /// `apply(Action::ViewValue)` via the special path.
    fn request_view_value(&mut self) -> Option<(VarId, String)> {
        let target = match self.view {
            View::Dashboard => self.selected_row(),
            View::Detail => self.detail_row(),
        };
        let Some(var) = target else {
            if !self.toast_is_error() {
                self.set_info_toast("no row selected");
            }
            return None;
        };
        Some((var.id, var.name.clone()))
    }

    /// Show the value modal. Runtime calls this after fetching the
    /// secret material via [`crate::VarProvider::get_value`].
    pub fn show_value_modal(&mut self, name: String, value: SecretString) {
        self.view_value = Some(ViewValueModal {
            name,
            value,
            show: false,
        });
    }

    /// Raise a focused error modal so the user has to acknowledge
    /// an action failure before continuing. Preferred over
    /// [`Self::set_error_toast`] when the user just initiated the
    /// failing action (create / edit / delete / link) — the toast
    /// is too easy to miss.
    pub fn show_error_modal(
        &mut self,
        title: impl Into<String>,
        message: impl Into<String>,
        hint: Option<String>,
    ) {
        self.error_modal = Some(ErrorModal {
            title: title.into(),
            message: message.into(),
            hint,
        });
    }

    /// Whether the error modal is currently focused.
    #[must_use]
    pub const fn is_error_modal_visible(&self) -> bool {
        self.error_modal.is_some()
    }

    /// Read-only access to the focused error modal (for the views layer).
    pub(crate) const fn current_error_modal(&self) -> Option<&ErrorModal> {
        self.error_modal.as_ref()
    }

    /// Handle a key while the error modal is focused. Only `Esc`,
    /// `Enter`, and `Ctrl+C` are recognised; the modal swallows
    /// everything else so the user can't accidentally trigger a new
    /// action while still reading the error.
    fn dispatch_error_modal_key(&mut self, key: KeyEvent) -> DispatchOutcome {
        let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
        if matches!(key.code, KeyCode::Char('c')) && ctrl {
            self.quit = true;
            return DispatchOutcome::Continue;
        }
        if matches!(key.code, KeyCode::Esc | KeyCode::Enter | KeyCode::Char(' ')) {
            self.error_modal = None;
        }
        DispatchOutcome::Continue
    }

    /// Whether the link form modal is currently focused.
    #[must_use]
    pub const fn is_link_form_visible(&self) -> bool {
        self.link_form.is_some()
    }

    /// Read-only access to the link form for the views layer.
    pub(crate) const fn current_link_form(&self) -> Option<&LinkForm> {
        self.link_form.as_ref()
    }

    /// Whether the view-value modal is currently focused.
    #[must_use]
    pub const fn is_view_value_visible(&self) -> bool {
        self.view_value.is_some()
    }

    /// Read-only access to the view-value modal for the views layer.
    pub(crate) const fn current_view_value(&self) -> Option<&ViewValueModal> {
        self.view_value.as_ref()
    }

    /// Handle a key while a confirmation modal is focused.
    ///
    /// Recognised keys:
    /// - `y` / `Y` / `Enter` — accept; consume the pending action
    ///   and surface it as a [`DispatchOutcome`] for the runtime.
    /// - `n` / `N` / `Esc` — cancel; clear the modal.
    /// - `Ctrl+C` — quit (overrides the modal so the user can always
    ///   escape).
    /// - Any other key is ignored and the modal stays focused.
    fn dispatch_confirm_key(&mut self, key: KeyEvent) -> DispatchOutcome {
        let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
        if matches!(key.code, KeyCode::Char('c')) && ctrl {
            self.quit = true;
            return DispatchOutcome::Continue;
        }
        let accept = matches!(key.code, KeyCode::Char('y' | 'Y') | KeyCode::Enter);
        let reject = matches!(key.code, KeyCode::Char('n' | 'N') | KeyCode::Esc);
        if !accept && !reject {
            return DispatchOutcome::Continue;
        }
        let Some(req) = self.confirm.take() else {
            return DispatchOutcome::Continue;
        };
        if reject {
            // Cascade the dismissal: if the user opened help and then
            // raised a modal, an Esc press should back them out of
            // *both* layers in one go rather than leaving help still
            // visible. The user opened help BEFORE the modal (modals
            // steal focus so `?` cannot reach the Action path), so
            // closing it here matches the "Esc means back to the
            // dashboard root" invariant.
            if matches!(self.overlay, Overlay::Help) {
                self.overlay = Overlay::None;
            }
            return DispatchOutcome::Continue;
        }
        match req.action {
            PendingAction::DeleteVar { id, name } => DispatchOutcome::DeleteRequested { id, name },
        }
    }

    fn dispatch_filter_input_key(&mut self, key: KeyEvent) -> DispatchOutcome {
        // Mirror `apply()`'s contract: any active interaction in the
        // filter input clears a stale info toast so the user never
        // sees a "refreshed (5 vars)" sitting on screen while they
        // type a needle that contradicts it. Error toasts remain
        // sticky and survive — same policy as the Action path.
        if !self.toast_is_error() {
            self.toast = None;
        }
        let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
        match key.code {
            // Universal exit gesture remains bound.
            KeyCode::Char('c') if ctrl => {
                self.quit = true;
            }
            // Cancel — clear the filter, restore full row set.
            KeyCode::Esc => self.close_filter(),
            // Accept — keep filter applied, hide the input box.
            KeyCode::Enter => {
                if let Some(filter) = self.filter.as_mut() {
                    filter.commit();
                }
            }
            // Edit the needle.
            KeyCode::Backspace => self.filter_pop(),
            KeyCode::Char(c) if !ctrl => self.filter_push(c),
            // Navigation through filtered results — arrow / page keys
            // only; j/k are valid needle characters and must not steal
            // the keystroke while the input is active.
            KeyCode::Up => self.select_prev(),
            KeyCode::Down => self.select_next(),
            KeyCode::PageUp => self.page(false),
            KeyCode::PageDown => self.page(true),
            _ => {}
        }
        DispatchOutcome::Continue
    }

    /// Apply one [`Action`] to the state.
    ///
    /// Side-effect-free: only mutates `self`. The runtime drives this
    /// in a tight loop with each translated key event.
    ///
    /// Toast lifecycle: *info* toasts auto-dismiss on any non-`Noop`
    /// interaction so they do not pile up; *error* toasts are sticky
    /// and only cleared by [`Action::Dismiss`] or [`Action::Refresh`]
    /// so a user typing fast cannot lose a failure notice they never
    /// had a chance to read.
    pub fn apply(&mut self, action: Action) {
        // Auto-dismiss INFO toasts on any non-Noop action *before*
        // dispatch so the current action can set its own toast which
        // will survive. Error toasts persist until explicitly handled.
        if !matches!(action, Action::Noop) && !self.toast_is_error() {
            self.toast = None;
        }
        match action {
            Action::Quit => self.quit = true,
            Action::Dismiss => self.dismiss(),
            Action::MoveDown => self.select_next(),
            Action::MoveUp => self.select_prev(),
            Action::MoveTop => self.select_first(),
            Action::MoveBottom => self.select_last(),
            Action::PageDown => self.page(true),
            Action::PageUp => self.page(false),
            Action::ToggleHelp => self.toggle_help(),
            Action::ToggleSecretVisibility => {
                self.secrets_visible = !self.secrets_visible;
            }
            Action::Refresh => {
                // The runtime owns the refresh side-effect (it owns
                // the provider). Here we just clear any toast so the
                // runtime's post-refresh toast — whether the success
                // confirmation or an error from the provider — is the
                // only message visible afterwards.
                self.toast = None;
            }
            Action::StartFuzzy => self.open_filter(),
            Action::OpenDetail => self.open_detail(),
            Action::DeleteVar => self.request_delete_confirmation(),
            Action::NewVar => self.open_new_var_prompt(),
            Action::EditVar => self.open_edit_value_prompt(),
            Action::LinkVar => self.open_link_form(),
            Action::RunInProject => self.open_run_form(),
            // `ViewValue` is handled in `dispatch_key` directly
            // because its outcome needs to leave the apply path. We
            // accept it here as a no-op for tests that bypass
            // `dispatch_key`. Same for `Noop`.
            Action::ViewValue | Action::Noop => {}
            // Remaining stubs — copy / profile / next-view.
            Action::CopyValue | Action::SwitchProfile | Action::NextView => {
                self.set_info_toast("not implemented yet (use CLI for now)");
            }
        }
    }

    /// Open the fuzzy filter overlay. If a filter is already active
    /// the input box is re-opened so the user can keep typing without
    /// losing the existing needle.
    /// Open the fuzzy filter overlay.
    ///
    /// If a filter is already applied, the input box is re-opened so
    /// the user can continue editing the existing needle. The cursor
    /// position is **preserved** in both first-open and re-open paths
    /// so the user's mental "what is selected" pointer survives a
    /// roundtrip through the input.
    pub fn open_filter(&mut self) {
        if let Some(filter) = self.filter.as_mut() {
            filter.reopen_input();
            return;
        }
        self.filter = Some(FilterState::new(self.rows.len()));
        // The fresh `FilterState` already matches all rows in original
        // order, and the visible row count is unchanged, so the
        // existing selection index remains valid. Clamp defensively
        // in case the dashboard was empty.
        self.clamp_selection();
    }

    /// Clear the filter and restore the full row set.
    pub fn close_filter(&mut self) {
        self.filter = None;
        self.clamp_selection();
    }

    /// Switch to [`View::Detail`] for the currently-selected row.
    ///
    /// The selected row's `VarId` is snapshotted into
    /// [`detail_target`](Self::detail_row) so the Detail screen looks
    /// up its data by identity rather than index — this prevents the
    /// pane from silently re-pointing at a different row when a
    /// concurrent refresh reshuffles the row buffer.
    ///
    /// If no row is selected, the call is a no-op aside from a brief
    /// `"no row selected"` info toast. A pre-existing error toast is
    /// **never** clobbered (errors are sticky for a reason; an
    /// accidental Enter must not erase a refresh-failure notice the
    /// user has not yet read).
    pub fn open_detail(&mut self) {
        if let Some(var) = self.selected_row() {
            self.detail_target = Some(var.id);
            self.view = View::Detail;
            return;
        }
        // Preserve sticky error toasts; only show the info hint when
        // the toast slot is empty or already an info toast (which
        // `apply` already cleared before dispatch in the normal path).
        if !self.toast_is_error() {
            self.set_info_toast("no row selected");
        }
    }

    /// Return to the dashboard from any other view. Clears the
    /// Detail target so a subsequent re-entry re-snapshots fresh.
    pub const fn return_to_dashboard(&mut self) {
        self.view = View::Dashboard;
        self.detail_target = None;
    }

    /// Splice the given variable id out of the row buffer locally
    /// without going through the provider.
    ///
    /// Used by the runtime when a `delete` succeeded but the
    /// subsequent `refresh` failed: keeping the deleted row visible
    /// would let the user press `d` a second time on a ghost entry,
    /// producing a confusing `NotFound` error or a "deleted twice"
    /// success. Splicing locally restores the user's mental model.
    ///
    /// No-op if the id is not in the buffer. Re-ranks any active
    /// filter and clamps the selection cursor.
    pub fn splice_out_row(&mut self, id: VarId) {
        let before = self.rows.len();
        self.rows.retain(|v| v.id != id);
        if self.rows.len() == before {
            return;
        }
        self.rebuild_filter();
        self.clamp_selection();
        // If the user was inspecting the just-spliced variable,
        // return to the dashboard so the detail target does not
        // dangle.
        if self.detail_target == Some(id) {
            self.return_to_dashboard();
        }
    }

    /// Open the editor form modal for creating a new variable.
    /// Fields default to: empty name + empty value, group=user,
    /// kind=secret (matches the most common case).
    fn open_new_var_prompt(&mut self) {
        self.form = Some(EditorForm {
            mode: EditorMode::NewVar,
            name: String::new(),
            value: String::new(),
            group_idx: 0,
            kind_idx: 0,
            focus: FormField::Name,
            show_value: false,
        });
    }

    /// Open the editor form modal for editing the value of the
    /// currently-targeted variable. The name / group / kind fields
    /// are displayed but read-only; only the value is editable.
    fn open_edit_value_prompt(&mut self) {
        let target = match self.view {
            View::Dashboard => self.selected_row(),
            View::Detail => self.detail_row(),
        };
        let Some(var) = target else {
            if !self.toast_is_error() {
                self.set_info_toast("no row selected");
            }
            return;
        };
        let group_idx = GROUP_CYCLE
            .iter()
            .position(|g| g == &var.group)
            .unwrap_or(0);
        let kind_idx = KIND_CYCLE.iter().position(|k| *k == var.kind).unwrap_or(0);
        self.form = Some(EditorForm {
            mode: EditorMode::EditValue {
                id: var.id,
                original_name: var.name.clone(),
            },
            name: var.name.clone(),
            value: String::new(),
            group_idx,
            kind_idx,
            focus: FormField::Value,
            show_value: !matches!(var.kind, VarKind::Secret),
        });
    }

    /// Whether the editor form modal is currently focused.
    #[must_use]
    pub const fn is_form_visible(&self) -> bool {
        self.form.is_some()
    }

    /// Read-only access to the focused editor form (for the views layer).
    pub(crate) const fn current_form(&self) -> Option<&EditorForm> {
        self.form.as_ref()
    }

    /// Raise a confirmation modal for deleting the currently-targeted
    /// variable (the selected row on the Dashboard, or
    /// [`Self::detail_row`] when on Detail). Surfaces an info toast
    /// instead if there is no target — without clobbering a sticky
    /// error toast.
    fn request_delete_confirmation(&mut self) {
        debug_assert!(
            self.confirm.is_none(),
            "request_delete_confirmation called with a focused modal — \
             dispatch_key routes confirm-mode keys to dispatch_confirm_key \
             before any Action::DeleteVar can reach here"
        );
        let target = match self.view {
            View::Dashboard => self.selected_row(),
            View::Detail => self.detail_row(),
        };
        let Some(var) = target else {
            if !self.toast_is_error() {
                self.set_info_toast("no row selected");
            }
            return;
        };
        let kind = match var.kind {
            evault_core::model::VarKind::Secret => "secret",
            evault_core::model::VarKind::Plain => "plain",
        };
        self.confirm = Some(ConfirmRequest {
            title: "delete variable".to_owned(),
            body: format!("Delete `{}` ({kind})?\nThis cannot be undone.", var.name),
            action: PendingAction::DeleteVar {
                id: var.id,
                name: var.name.clone(),
            },
        });
    }

    /// Whether a confirmation modal is currently focused.
    #[must_use]
    pub const fn is_confirm_visible(&self) -> bool {
        self.confirm.is_some()
    }

    /// Read-only access to the focused confirm request (for the
    /// views layer). Crate-private so external callers stay on the
    /// observation-only API ([`Self::is_confirm_visible`] et al.).
    pub(crate) const fn current_confirm(&self) -> Option<&ConfirmRequest> {
        self.confirm.as_ref()
    }

    /// Programmatic dismissal of a focused modal. Returns `true` if a
    /// modal was actually cleared. Used by the runtime to flush state
    /// after the user-initiated delete it triggered has completed.
    pub fn dismiss_confirm(&mut self) -> bool {
        let was_set = self.confirm.is_some();
        self.confirm = None;
        was_set
    }

    /// The variable currently displayed by the Detail view, looked
    /// up by identity rather than by selection index.
    ///
    /// Returns `None` when no Detail view is active, or when the
    /// inspected variable has been removed from the row buffer
    /// between Detail entry and the current frame.
    #[must_use]
    pub fn detail_row(&self) -> Option<&VarSummary> {
        let id = self.detail_target?;
        self.rows.iter().find(|v| v.id == id)
    }

    fn filter_push(&mut self, c: char) {
        let haystacks: Vec<&str> = self.rows.iter().map(|v| v.name.as_str()).collect();
        if let Some(filter) = self.filter.as_mut() {
            filter.push(c, &haystacks);
        }
        self.clamp_selection();
    }

    fn filter_pop(&mut self) {
        let haystacks: Vec<&str> = self.rows.iter().map(|v| v.name.as_str()).collect();
        if let Some(filter) = self.filter.as_mut() {
            filter.pop(&haystacks);
        }
        self.clamp_selection();
    }

    /// Re-rank the existing filter against the (possibly changed) row
    /// buffer. Called from [`Self::refresh`]; no-op when no filter is
    /// active.
    ///
    /// `FilterState::rerank` is pure in `(needle, haystacks)`, so
    /// replaying the needle char-by-char against the new haystacks
    /// produces the same ranking that live typing would. We rebuild
    /// rather than expose a public rerank to keep `FilterState`'s
    /// surface minimal.
    fn rebuild_filter(&mut self) {
        let Some(filter) = self.filter.as_mut() else {
            return;
        };
        let needle = filter.needle().to_owned();
        let input_active = filter.input_active();
        let haystacks: Vec<&str> = self.rows.iter().map(|v| v.name.as_str()).collect();
        let mut fresh = FilterState::new(self.rows.len());
        for c in needle.chars() {
            fresh.push(c, &haystacks);
        }
        if !input_active {
            fresh.commit();
        }
        *filter = fresh;
    }

    /// Set an informational toast (auto-dismissed on the next action).
    pub fn set_info_toast(&mut self, msg: impl Into<String>) {
        self.toast = Some(Toast {
            text: msg.into(),
            kind: ToastKind::Info,
        });
    }

    /// Set an error toast (rendered in the `error` palette).
    pub fn set_error_toast(&mut self, msg: impl Into<String>) {
        self.toast = Some(Toast {
            text: msg.into(),
            kind: ToastKind::Error,
        });
    }

    /// `true` if the runtime should exit after the current frame.
    #[must_use]
    pub const fn quit_requested(&self) -> bool {
        self.quit
    }

    /// Read-only access to the full row buffer (filter-independent).
    /// Use [`Self::visible_row_indices`] / [`Self::visible_rows`] when
    /// you need the rows currently rendered by the dashboard.
    #[must_use]
    pub fn rows(&self) -> &[VarSummary] {
        &self.rows
    }

    /// Indices into [`Self::rows`] of the rows currently rendered.
    ///
    /// When a filter is applied the indices are in match-score order
    /// (best score first). Without a filter they are simply
    /// `0..rows.len()`.
    #[must_use]
    pub fn visible_row_indices(&self) -> Vec<usize> {
        self.filter.as_ref().map_or_else(
            || (0..self.rows.len()).collect(),
            |f| f.visible_indices().to_vec(),
        )
    }

    /// Iterator over the rows currently rendered by the dashboard.
    pub fn visible_rows(&self) -> impl Iterator<Item = &VarSummary> {
        self.visible_row_indices()
            .into_iter()
            .filter_map(move |i| self.rows.get(i))
    }

    /// Whether the fuzzy-filter input box is currently capturing
    /// keystrokes. While `true` characters edit the needle instead of
    /// firing actions.
    #[must_use]
    pub fn is_filter_input_active(&self) -> bool {
        self.filter.as_ref().is_some_and(FilterState::input_active)
    }

    /// Whether a filter is currently applied (regardless of whether
    /// the input box is still open).
    #[must_use]
    pub const fn is_filter_active(&self) -> bool {
        self.filter.is_some()
    }

    /// The current filter needle, if any. Empty string when the user
    /// has opened the filter but not typed anything yet.
    #[must_use]
    pub fn filter_needle(&self) -> Option<&str> {
        self.filter.as_ref().map(FilterState::needle)
    }

    /// Visible-row index of the currently-selected row, if any. This
    /// is the index inside [`Self::visible_rows`], not into
    /// [`Self::rows`]. Use [`Self::selected_row`] to dereference to
    /// the underlying [`VarSummary`].
    #[must_use]
    pub const fn selected_index(&self) -> Option<usize> {
        self.table_state.selected()
    }

    /// The currently selected [`VarSummary`], if any, resolved through
    /// the active filter.
    #[must_use]
    pub fn selected_row(&self) -> Option<&VarSummary> {
        let visible_idx = self.table_state.selected()?;
        let absolute_idx = match self.filter.as_ref() {
            Some(f) => *f.visible_indices().get(visible_idx)?,
            None => visible_idx,
        };
        self.rows.get(absolute_idx)
    }

    /// Read-only access to the [`TableState`]. Useful for tests that
    /// want to inspect the cursor without rendering.
    #[must_use]
    pub const fn table_state(&self) -> &TableState {
        &self.table_state
    }

    /// Mutable access to the [`TableState`]. The dashboard view
    /// uses this when calling `render_stateful_widget`.
    pub const fn table_state_mut(&mut self) -> &mut TableState {
        &mut self.table_state
    }

    /// Whether the help overlay is currently visible.
    #[must_use]
    pub const fn help_visible(&self) -> bool {
        matches!(self.overlay, Overlay::Help)
    }

    /// Whether secret values should be rendered (otherwise masked).
    #[must_use]
    pub const fn secrets_visible(&self) -> bool {
        self.secrets_visible
    }

    /// Top-level view currently displayed.
    #[must_use]
    pub const fn current_view(&self) -> View {
        self.view
    }

    /// The currently-displayed toast text, if any.
    #[must_use]
    pub fn toast_text(&self) -> Option<&str> {
        self.toast.as_ref().map(|t| t.text.as_str())
    }

    /// Whether the current toast is an error (vs informational).
    #[must_use]
    pub fn toast_is_error(&self) -> bool {
        matches!(self.toast.as_ref().map(|t| t.kind), Some(ToastKind::Error))
    }

    pub(crate) const fn current_toast(&self) -> Option<&Toast> {
        self.toast.as_ref()
    }

    fn dismiss(&mut self) {
        // Cascade: toast → filter → secondary view → overlay → quit.
        // Each level is dismissed in turn so the user has a
        // predictable Esc path back to the dashboard root before the
        // app exits. Modals and similar focus-stealing overlays will
        // be inserted ahead of `toast` in subsequent phases.
        if self.toast.is_some() {
            self.toast = None;
            return;
        }
        if self.is_filter_active() {
            self.close_filter();
            return;
        }
        if !matches!(self.view, View::Dashboard) {
            self.view = View::Dashboard;
            self.detail_target = None;
            return;
        }
        if matches!(self.overlay, Overlay::Help) {
            self.overlay = Overlay::None;
            return;
        }
        self.quit = true;
    }

    const fn toggle_help(&mut self) {
        self.overlay = match self.overlay {
            Overlay::Help => Overlay::None,
            Overlay::None => Overlay::Help,
        };
    }

    /// Visible-row count: the rendered length of the dashboard.
    /// When a filter is applied this is the count of *matching* rows;
    /// otherwise it equals `rows.len()`. Navigation operates in this
    /// space so the cursor never lands on a hidden row.
    fn visible_len(&self) -> usize {
        self.filter
            .as_ref()
            .map_or_else(|| self.rows.len(), |f| f.visible_indices().len())
    }

    fn clamp_selection(&mut self) {
        let len = self.visible_len();
        if len == 0 {
            self.table_state.select(None);
            return;
        }
        let max = len - 1;
        let cur = self.table_state.selected().unwrap_or(0).min(max);
        self.table_state.select(Some(cur));
    }

    fn select_next(&mut self) {
        let len = self.visible_len();
        if len == 0 {
            return;
        }
        let next = self.table_state.selected().map_or(0, |i| (i + 1) % len);
        self.table_state.select(Some(next));
    }

    fn select_prev(&mut self) {
        let len = self.visible_len();
        if len == 0 {
            return;
        }
        let prev = self
            .table_state
            .selected()
            .map_or(0, |i| if i == 0 { len - 1 } else { i - 1 });
        self.table_state.select(Some(prev));
    }

    #[allow(clippy::missing_const_for_fn)]
    fn select_first(&mut self) {
        if self.visible_len() > 0 {
            self.table_state.select(Some(0));
        }
    }

    #[allow(clippy::missing_const_for_fn)]
    fn select_last(&mut self) {
        if let Some(last) = self.visible_len().checked_sub(1) {
            self.table_state.select(Some(last));
        }
    }

    fn page(&mut self, down: bool) {
        // A "page" is intentionally a fixed stride; the runtime does
        // not know the viewport size at action-translation time. Ten
        // rows is a sensible compromise that works on small and large
        // terminals alike.
        const STRIDE: usize = 10;
        let len = self.visible_len();
        if len == 0 {
            return;
        }
        let cur = self.table_state.selected().unwrap_or(0);
        let new = if down {
            cur.saturating_add(STRIDE).min(len - 1)
        } else {
            cur.saturating_sub(STRIDE)
        };
        self.table_state.select(Some(new));
    }
}

/// Return the next field in the editor form's focus cycle. In
/// `EditValue` mode the Name / Group / Kind fields are display-only
/// so focus stays on `Value`.
const fn next_focus(current: FormField, mode: &EditorMode) -> FormField {
    if matches!(mode, EditorMode::EditValue { .. }) {
        return FormField::Value;
    }
    match current {
        FormField::Name => FormField::Group,
        FormField::Group => FormField::Kind,
        FormField::Kind => FormField::Value,
        FormField::Value => FormField::Name,
    }
}

/// Return the previous field in the editor form's focus cycle.
const fn prev_focus(current: FormField, mode: &EditorMode) -> FormField {
    if matches!(mode, EditorMode::EditValue { .. }) {
        return FormField::Value;
    }
    match current {
        FormField::Name => FormField::Value,
        FormField::Group => FormField::Name,
        FormField::Kind => FormField::Group,
        FormField::Value => FormField::Kind,
    }
}

/// Apply a non-Tab / non-Esc / non-Enter / non-Ctrl-C key to the
/// currently-focused field of the editor form.
fn handle_field_key(form: &mut EditorForm, key: KeyEvent) {
    let read_only_metadata = matches!(form.mode, EditorMode::EditValue { .. });
    match form.focus {
        FormField::Name => {
            if read_only_metadata {
                return;
            }
            match key.code {
                KeyCode::Backspace => {
                    form.name.pop();
                }
                KeyCode::Char(c) if is_text_input(key) => form.name.push(c),
                _ => {}
            }
        }
        FormField::Group => {
            if read_only_metadata {
                return;
            }
            match key.code {
                KeyCode::Left => {
                    form.group_idx = (form.group_idx + GROUP_CYCLE.len() - 1) % GROUP_CYCLE.len();
                }
                KeyCode::Right | KeyCode::Char(' ') => {
                    form.group_idx = (form.group_idx + 1) % GROUP_CYCLE.len();
                }
                _ => {}
            }
        }
        FormField::Kind => {
            if read_only_metadata {
                return;
            }
            match key.code {
                KeyCode::Left => {
                    form.kind_idx = (form.kind_idx + KIND_CYCLE.len() - 1) % KIND_CYCLE.len();
                }
                KeyCode::Right | KeyCode::Char(' ') => {
                    form.kind_idx = (form.kind_idx + 1) % KIND_CYCLE.len();
                }
                _ => {}
            }
        }
        FormField::Value => match key.code {
            KeyCode::Backspace => {
                form.value.pop();
            }
            // `Ctrl+S` toggles "show value" so the user can verify what
            // they typed.
            KeyCode::Char('s') if key.modifiers.contains(KeyModifiers::CONTROL) => {
                form.show_value = !form.show_value;
            }
            KeyCode::Char(c) if is_text_input(key) => form.value.push(c),
            _ => {}
        },
    }
}

/// Apply a key to the currently-focused field of the link form.
fn handle_link_field_key(form: &mut LinkForm, key: KeyEvent) {
    match form.focus {
        LinkField::Path => match key.code {
            KeyCode::Backspace => {
                form.path.pop();
            }
            KeyCode::Char(c) if is_text_input(key) => form.path.push(c),
            _ => {}
        },
        LinkField::Profile => match key.code {
            KeyCode::Backspace => {
                form.profile.pop();
            }
            KeyCode::Char(c) if is_text_input(key) => form.profile.push(c),
            _ => {}
        },
        LinkField::Materialize => match key.code {
            KeyCode::Left | KeyCode::Right | KeyCode::Char(' ' | 'y' | 'Y' | 'n' | 'N') => {
                form.materialize = !form.materialize;
            }
            _ => {}
        },
    }
}

/// Apply a key to the currently-focused field of the run form.
fn handle_run_field_key(form: &mut RunForm, key: KeyEvent) {
    let target = match form.focus {
        RunField::Path => &mut form.path,
        RunField::Profile => &mut form.profile,
        RunField::Command => &mut form.command,
    };
    match key.code {
        KeyCode::Backspace => {
            target.pop();
        }
        KeyCode::Char(c) if is_text_input(key) => target.push(c),
        _ => {}
    }
}

/// Whether a key event represents a typeable character (no Ctrl /
/// Alt modifier; Shift is OK).
const fn is_text_input(key: KeyEvent) -> bool {
    !key.modifiers.contains(KeyModifiers::CONTROL) && !key.modifiers.contains(KeyModifiers::ALT)
}

#[cfg(test)]
mod tests {
    use super::*;
    use evault_core::model::{Group, VarId, VarKind};
    use time::OffsetDateTime;

    struct StaticProvider(Vec<VarSummary>);
    impl VarProvider for StaticProvider {
        fn list(&self) -> Result<Vec<VarSummary>, ProviderError> {
            Ok(self.0.clone())
        }
        fn get_value(&self, _: VarId) -> Result<Option<SecretString>, ProviderError> {
            Ok(None)
        }
    }

    struct FailingProvider;
    impl VarProvider for FailingProvider {
        fn list(&self) -> Result<Vec<VarSummary>, ProviderError> {
            Err(ProviderError::Backend("synthetic".into()))
        }
        fn get_value(&self, _: VarId) -> Result<Option<SecretString>, ProviderError> {
            Err(ProviderError::Backend("synthetic".into()))
        }
    }

    fn summary(name: &str) -> VarSummary {
        VarSummary {
            id: VarId::new_v4(),
            name: name.into(),
            group: Group::User,
            kind: VarKind::Plain,
            value_len: name.len(),
            linked_projects: 0,
            updated_at: OffsetDateTime::now_utc(),
        }
    }

    fn three_rows() -> StaticProvider {
        StaticProvider(vec![summary("ALPHA"), summary("BETA"), summary("GAMMA")])
    }

    #[test]
    fn refresh_populates_rows() {
        let mut app = AppState::new();
        app.refresh(&three_rows()).unwrap();
        assert_eq!(app.rows().len(), 3);
        assert_eq!(app.selected_index(), Some(0));
    }

    #[test]
    fn selection_is_none_before_first_refresh() {
        // Invariant: an `AppState` that has never been refreshed must
        // not advertise a selection (would point at non-existent row 0).
        let app = AppState::new();
        assert!(app.rows().is_empty());
        assert_eq!(app.selected_index(), None);
    }

    #[test]
    fn refresh_with_empty_provider_clears_selection() {
        let mut app = AppState::new();
        app.refresh(&three_rows()).unwrap();
        app.refresh(&StaticProvider(Vec::new())).unwrap();
        assert!(app.rows().is_empty());
        assert_eq!(app.selected_index(), None);
    }

    #[test]
    fn refresh_propagates_provider_error() {
        let mut app = AppState::new();
        let err = app.refresh(&FailingProvider).unwrap_err();
        assert!(matches!(err, ProviderError::Backend(_)));
    }

    #[test]
    fn selection_wraps_around() {
        let mut app = AppState::new();
        app.refresh(&three_rows()).unwrap();
        app.apply(Action::MoveUp); // wrap from 0 to last
        assert_eq!(app.selected_index(), Some(2));
        app.apply(Action::MoveDown); // wrap from last back to 0
        assert_eq!(app.selected_index(), Some(0));
        app.apply(Action::MoveBottom);
        assert_eq!(app.selected_index(), Some(2));
        app.apply(Action::MoveTop);
        assert_eq!(app.selected_index(), Some(0));
    }

    #[test]
    fn navigation_on_empty_rows_does_nothing() {
        let mut app = AppState::new();
        app.refresh(&StaticProvider(Vec::new())).unwrap();
        app.apply(Action::MoveDown);
        app.apply(Action::MoveUp);
        app.apply(Action::MoveTop);
        app.apply(Action::MoveBottom);
        assert_eq!(app.selected_index(), None);
    }

    #[test]
    fn quit_action_sets_quit_flag() {
        let mut app = AppState::new();
        app.apply(Action::Quit);
        assert!(app.quit_requested());
    }

    #[test]
    fn dismiss_closes_help_overlay_first_then_quits() {
        let mut app = AppState::new();
        app.apply(Action::ToggleHelp);
        assert!(app.help_visible());
        app.apply(Action::Dismiss);
        assert!(!app.help_visible());
        assert!(!app.quit_requested());
        app.apply(Action::Dismiss);
        assert!(app.quit_requested());
    }

    #[test]
    fn toggle_secret_visibility_round_trips() {
        let mut app = AppState::new();
        assert!(!app.secrets_visible());
        app.apply(Action::ToggleSecretVisibility);
        assert!(app.secrets_visible());
        app.apply(Action::ToggleSecretVisibility);
        assert!(!app.secrets_visible());
    }

    #[test]
    fn toasts_distinguish_info_and_error() {
        let mut app = AppState::new();
        app.set_info_toast("hello");
        assert_eq!(app.toast_text(), Some("hello"));
        assert!(!app.toast_is_error());
        app.set_error_toast("boom");
        assert_eq!(app.toast_text(), Some("boom"));
        assert!(app.toast_is_error());
    }

    #[test]
    fn info_toast_dismissed_on_next_interaction() {
        let mut app = AppState::new();
        app.refresh(&three_rows()).unwrap();
        app.set_info_toast("hi");
        app.apply(Action::MoveDown);
        assert!(app.toast_text().is_none());
    }

    /// Error toasts must survive navigation so a user typing fast
    /// (e.g. holding `j` to scroll) cannot lose a failure notice
    /// before reading it. Only explicit `Dismiss` / `Refresh` clears
    /// an error.
    #[test]
    fn error_toast_survives_navigation_and_help_toggle() {
        let mut app = AppState::new();
        app.refresh(&three_rows()).unwrap();
        app.set_error_toast("backend exploded");
        app.apply(Action::MoveDown);
        assert_eq!(app.toast_text(), Some("backend exploded"));
        app.apply(Action::ToggleHelp);
        assert_eq!(app.toast_text(), Some("backend exploded"));
        // Explicit dismiss clears it. Because the toast is present,
        // `Dismiss` consumes it instead of closing the help overlay,
        // so help stays visible.
        app.apply(Action::Dismiss);
        assert!(app.toast_text().is_none());
        assert!(app.help_visible());
    }

    /// `Action::Refresh` is *not* a stub: it MUST clear any toast so
    /// the runtime's post-refresh success/failure message is the only
    /// thing visible afterwards. Previously this action set a
    /// "not implemented" info toast that lingered on every successful
    /// runtime refresh.
    #[test]
    fn refresh_action_clears_pre_existing_toast() {
        let mut app = AppState::new();
        app.set_info_toast("stale info");
        app.apply(Action::Refresh);
        assert!(app.toast_text().is_none());

        app.set_error_toast("stale error");
        app.apply(Action::Refresh);
        assert!(
            app.toast_text().is_none(),
            "Refresh must also clear sticky error toasts"
        );
    }

    #[test]
    fn noop_action_preserves_toast() {
        let mut app = AppState::new();
        app.set_info_toast("hi");
        app.apply(Action::Noop);
        assert_eq!(app.toast_text(), Some("hi"));
    }

    #[test]
    fn page_navigation_is_bounded() {
        let mut app = AppState::new();
        app.refresh(&three_rows()).unwrap();
        app.apply(Action::PageDown);
        // Only 3 rows; PageDown should pin at the last.
        assert_eq!(app.selected_index(), Some(2));
        app.apply(Action::PageUp);
        assert_eq!(app.selected_index(), Some(0));
    }

    // ─── Phase 2a: fuzzy filter ───────────────────────────────────

    fn press(code: KeyCode) -> KeyEvent {
        KeyEvent::new(code, KeyModifiers::NONE)
    }

    fn five_rows() -> StaticProvider {
        StaticProvider(vec![
            summary("DATABASE_URL"),
            summary("API_KEY"),
            summary("DB_HOST"),
            summary("NODE_ENV"),
            summary("PORT"),
        ])
    }

    #[test]
    fn start_fuzzy_opens_filter_with_empty_needle() {
        let mut app = AppState::new();
        app.refresh(&five_rows()).unwrap();
        app.apply(Action::StartFuzzy);
        assert!(app.is_filter_active());
        assert!(app.is_filter_input_active());
        assert_eq!(app.filter_needle(), Some(""));
        // Empty needle shows every row.
        assert_eq!(app.visible_rows().count(), 5);
    }

    #[test]
    fn typing_filter_chars_narrows_visible_rows() {
        let mut app = AppState::new();
        app.refresh(&five_rows()).unwrap();
        app.apply(Action::StartFuzzy);
        // Dispatch each char through dispatch_key so the filter-input
        // routing path is exercised.
        app.dispatch_key(press(KeyCode::Char('d')));
        app.dispatch_key(press(KeyCode::Char('b')));
        let visible: Vec<_> = app.visible_rows().map(|v| v.name.clone()).collect();
        assert!(visible.contains(&"DATABASE_URL".to_string()));
        assert!(visible.contains(&"DB_HOST".to_string()));
        assert!(!visible.contains(&"API_KEY".to_string()));
        assert!(!visible.contains(&"PORT".to_string()));
        assert_eq!(app.filter_needle(), Some("db"));
    }

    #[test]
    fn backspace_pops_needle_and_widens_visible_set() {
        let mut app = AppState::new();
        app.refresh(&five_rows()).unwrap();
        app.apply(Action::StartFuzzy);
        app.dispatch_key(press(KeyCode::Char('x')));
        assert_eq!(app.visible_rows().count(), 0);
        app.dispatch_key(press(KeyCode::Backspace));
        assert_eq!(app.filter_needle(), Some(""));
        assert_eq!(app.visible_rows().count(), 5);
    }

    #[test]
    fn enter_commits_filter_input_but_keeps_filter_applied() {
        let mut app = AppState::new();
        app.refresh(&five_rows()).unwrap();
        app.apply(Action::StartFuzzy);
        app.dispatch_key(press(KeyCode::Char('p')));
        app.dispatch_key(press(KeyCode::Enter));
        assert!(app.is_filter_active());
        assert!(!app.is_filter_input_active());
        // The filter is still narrowing the view.
        assert!(app.visible_rows().count() < 5);
        // Char keys now go through the Action path again.
        app.dispatch_key(press(KeyCode::Char('s')));
        assert!(app.secrets_visible());
    }

    #[test]
    fn esc_clears_the_filter_entirely() {
        let mut app = AppState::new();
        app.refresh(&five_rows()).unwrap();
        app.apply(Action::StartFuzzy);
        app.dispatch_key(press(KeyCode::Char('p')));
        app.dispatch_key(press(KeyCode::Esc));
        assert!(!app.is_filter_active());
        assert_eq!(app.visible_rows().count(), 5);
    }

    #[test]
    fn selection_clamps_to_visible_count_on_narrow() {
        let mut app = AppState::new();
        app.refresh(&five_rows()).unwrap();
        app.apply(Action::MoveBottom); // select row 4 (PORT)
        assert_eq!(app.selected_index(), Some(4));
        app.apply(Action::StartFuzzy);
        // Type something that filters down to only a handful of rows.
        app.dispatch_key(press(KeyCode::Char('d')));
        app.dispatch_key(press(KeyCode::Char('b')));
        // Visible count is 2; selection must clamp.
        let visible = app.visible_rows().count();
        assert!(visible <= 2);
        assert!(app.selected_index().is_some_and(|i| i < visible));
    }

    #[test]
    fn selected_row_resolves_through_filter() {
        let mut app = AppState::new();
        app.refresh(&five_rows()).unwrap();
        app.apply(Action::StartFuzzy);
        // Filter narrows to rows containing "API".
        app.dispatch_key(press(KeyCode::Char('a')));
        app.dispatch_key(press(KeyCode::Char('p')));
        // The selected row should now be API_KEY (the best match for
        // "ap") rather than whatever absolute index 0 points to.
        let selected = app.selected_row().expect("a row should be selected");
        assert_eq!(selected.name, "API_KEY");
    }

    #[test]
    fn ctrl_c_still_quits_while_filter_input_active() {
        let mut app = AppState::new();
        app.refresh(&five_rows()).unwrap();
        app.apply(Action::StartFuzzy);
        let ctrl_c = KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL);
        app.dispatch_key(ctrl_c);
        assert!(app.quit_requested());
    }

    #[test]
    fn refresh_request_is_signalled_when_filter_is_off() {
        let mut app = AppState::new();
        app.refresh(&five_rows()).unwrap();
        let outcome = app.dispatch_key(press(KeyCode::Char('r')));
        assert!(matches!(outcome, DispatchOutcome::RefreshRequested));
    }

    #[test]
    fn dismiss_closes_an_active_filter_before_overlay_or_quit() {
        let mut app = AppState::new();
        app.refresh(&five_rows()).unwrap();
        // Open + commit the filter so input is no longer active.
        app.apply(Action::StartFuzzy);
        app.dispatch_key(press(KeyCode::Char('d')));
        app.dispatch_key(press(KeyCode::Enter));
        assert!(app.is_filter_active());
        // Esc with a committed filter must close the filter, NOT quit.
        app.apply(Action::Dismiss);
        assert!(!app.is_filter_active());
        assert!(!app.quit_requested());
        // A second Esc with no overlay/filter quits.
        app.apply(Action::Dismiss);
        assert!(app.quit_requested());
    }

    #[test]
    fn typing_in_filter_input_clears_pre_existing_info_toast() {
        // Regression: previously the filter-input path never touched
        // the toast, so a stale "refreshed (5 vars)" sat on screen
        // while the user typed a needle that contradicted it.
        let mut app = AppState::new();
        app.refresh(&five_rows()).unwrap();
        app.set_info_toast("refreshed (5 vars)");
        app.apply(Action::StartFuzzy);
        app.dispatch_key(press(KeyCode::Char('d')));
        assert!(app.toast_text().is_none());
    }

    #[test]
    fn typing_in_filter_input_preserves_error_toasts() {
        let mut app = AppState::new();
        app.refresh(&five_rows()).unwrap();
        app.set_error_toast("backend failure");
        app.apply(Action::StartFuzzy);
        app.dispatch_key(press(KeyCode::Char('d')));
        assert_eq!(app.toast_text(), Some("backend failure"));
    }

    // ─── Phase 2b1: detail view ───────────────────────────────────

    #[test]
    fn open_detail_switches_view_when_a_row_is_selected() {
        let mut app = AppState::new();
        app.refresh(&five_rows()).unwrap();
        assert_eq!(app.current_view(), View::Dashboard);
        app.apply(Action::OpenDetail);
        assert_eq!(app.current_view(), View::Detail);
    }

    #[test]
    fn open_detail_on_empty_dashboard_keeps_view_and_toasts() {
        let mut app = AppState::new();
        app.refresh(&StaticProvider(Vec::new())).unwrap();
        app.apply(Action::OpenDetail);
        assert_eq!(app.current_view(), View::Dashboard);
        assert_eq!(app.toast_text(), Some("no row selected"));
    }

    #[test]
    fn dismiss_returns_from_detail_to_dashboard() {
        let mut app = AppState::new();
        app.refresh(&five_rows()).unwrap();
        app.apply(Action::OpenDetail);
        assert_eq!(app.current_view(), View::Detail);
        app.apply(Action::Dismiss);
        assert_eq!(app.current_view(), View::Dashboard);
        assert!(!app.quit_requested());
    }

    #[test]
    fn detail_view_survives_secret_toggle_and_help_open() {
        let mut app = AppState::new();
        app.refresh(&five_rows()).unwrap();
        app.apply(Action::OpenDetail);
        // Action applied while on Detail view should not auto-return.
        app.apply(Action::ToggleSecretVisibility);
        assert_eq!(app.current_view(), View::Detail);
        assert!(app.secrets_visible());
        app.apply(Action::ToggleHelp);
        assert!(app.help_visible());
        assert_eq!(app.current_view(), View::Detail);
    }

    #[test]
    fn detail_row_resolves_by_identity_after_row_reorder() {
        let mut app = AppState::new();
        app.refresh(&five_rows()).unwrap();
        // Select API_KEY (index 1) and open detail.
        app.apply(Action::MoveDown);
        let target_id = app.selected_row().expect("selection").id;
        app.apply(Action::OpenDetail);
        assert_eq!(app.current_view(), View::Detail);
        assert_eq!(
            app.detail_row().map(|v| v.id),
            Some(target_id),
            "Detail must resolve to the originally inspected var"
        );

        // Now refresh with the SAME rows but in reverse order. By
        // index alone the Detail pane would silently jump to a
        // different variable.
        let reversed_rows: Vec<VarSummary> = {
            let mut tmp = app.rows().to_vec();
            tmp.reverse();
            tmp
        };
        app.refresh(&StaticProvider(reversed_rows)).unwrap();
        assert_eq!(app.current_view(), View::Detail);
        assert_eq!(
            app.detail_row().map(|v| v.id),
            Some(target_id),
            "Detail target must follow identity through a row reorder"
        );
    }

    #[test]
    fn refresh_returns_from_detail_when_inspected_var_is_gone() {
        let mut app = AppState::new();
        app.refresh(&five_rows()).unwrap();
        app.apply(Action::MoveDown); // select API_KEY
        let target_id = app.selected_row().expect("selection").id;
        app.apply(Action::OpenDetail);
        assert_eq!(app.current_view(), View::Detail);

        // External delete: rebuild rows without the inspected target.
        let surviving: Vec<VarSummary> = app
            .rows()
            .iter()
            .filter(|v| v.id != target_id)
            .cloned()
            .collect();
        app.refresh(&StaticProvider(surviving)).unwrap();

        assert_eq!(
            app.current_view(),
            View::Dashboard,
            "must auto-return to dashboard when the inspected var disappears"
        );
        assert!(
            app.toast_text()
                .is_some_and(|t| t.contains("removed elsewhere")),
            "must surface a loud error toast"
        );
        assert!(app.toast_is_error());
    }

    #[test]
    fn open_detail_does_not_clobber_sticky_error_toast() {
        let mut app = AppState::new();
        app.refresh(&StaticProvider(Vec::new())).unwrap();
        app.set_error_toast("backend failure");
        // Apply via `apply` so the pre-dispatch info-clear runs:
        // error toasts must survive that step AND the open_detail
        // empty-selection branch.
        app.apply(Action::OpenDetail);
        assert_eq!(app.toast_text(), Some("backend failure"));
        assert!(app.toast_is_error());
        assert_eq!(app.current_view(), View::Dashboard);
    }

    #[test]
    fn return_to_dashboard_clears_detail_target() {
        let mut app = AppState::new();
        app.refresh(&five_rows()).unwrap();
        app.apply(Action::OpenDetail);
        assert!(app.detail_row().is_some());
        app.apply(Action::Dismiss);
        assert_eq!(app.current_view(), View::Dashboard);
        // Internal invariant: re-opening Detail must re-snapshot
        // against the current selection, not retain the prior target.
        app.apply(Action::MoveDown);
        let new_target = app.selected_row().expect("selection").id;
        app.apply(Action::OpenDetail);
        assert_eq!(app.detail_row().map(|v| v.id), Some(new_target));
    }

    #[test]
    fn dismiss_cascade_priority_is_toast_filter_view_overlay() {
        let mut app = AppState::new();
        app.refresh(&five_rows()).unwrap();
        // Build a stacked context: filter committed, view = Detail,
        // overlay = Help, plus an ERROR toast on top (info toasts
        // would auto-clear before the dismiss cascade and merge two
        // steps into one — error toasts are sticky and exercise the
        // explicit-toast-dismiss step on its own).
        app.apply(Action::ToggleHelp);
        app.apply(Action::StartFuzzy);
        app.dispatch_key(press(KeyCode::Char('a')));
        app.dispatch_key(press(KeyCode::Enter));
        app.apply(Action::OpenDetail);
        app.set_error_toast("scratch");
        // 1) toast first
        app.apply(Action::Dismiss);
        assert!(app.toast_text().is_none());
        assert!(app.is_filter_active());
        // 2) filter
        app.apply(Action::Dismiss);
        assert!(!app.is_filter_active());
        assert_eq!(app.current_view(), View::Detail);
        // 3) view (back to dashboard)
        app.apply(Action::Dismiss);
        assert_eq!(app.current_view(), View::Dashboard);
        assert!(app.help_visible());
        // 4) overlay (help)
        app.apply(Action::Dismiss);
        assert!(!app.help_visible());
        // 5) finally quit
        app.apply(Action::Dismiss);
        assert!(app.quit_requested());
    }

    // ─── Phase 2b2: confirm modal + delete flow ───────────────────

    #[test]
    fn delete_action_opens_confirm_modal_for_selected_row() {
        let mut app = AppState::new();
        app.refresh(&five_rows()).unwrap();
        assert!(!app.is_confirm_visible());
        app.apply(Action::MoveDown); // select index 1 (API_KEY)
        let target_id = app.selected_row().expect("selection").id;
        app.apply(Action::DeleteVar);
        assert!(app.is_confirm_visible());
        let req = app.current_confirm().expect("confirm set");
        assert!(req.body.contains("API_KEY"));
        match &req.action {
            PendingAction::DeleteVar { id, name } => {
                assert_eq!(*id, target_id);
                assert_eq!(name, "API_KEY");
            }
        }
    }

    #[test]
    fn delete_action_on_empty_dashboard_does_not_open_modal() {
        let mut app = AppState::new();
        app.refresh(&StaticProvider(Vec::new())).unwrap();
        app.apply(Action::DeleteVar);
        assert!(!app.is_confirm_visible());
        assert_eq!(app.toast_text(), Some("no row selected"));
    }

    #[test]
    fn delete_action_on_detail_view_targets_inspected_var() {
        let mut app = AppState::new();
        app.refresh(&five_rows()).unwrap();
        app.apply(Action::MoveDown);
        let target_id = app.selected_row().expect("selection").id;
        app.apply(Action::OpenDetail);
        app.apply(Action::DeleteVar);
        assert!(app.is_confirm_visible());
        let req = app.current_confirm().expect("confirm set");
        match &req.action {
            PendingAction::DeleteVar { id, .. } => assert_eq!(*id, target_id),
        }
    }

    #[test]
    fn confirm_modal_steals_focus_from_filter_and_actions() {
        let mut app = AppState::new();
        app.refresh(&five_rows()).unwrap();
        app.apply(Action::DeleteVar);
        assert!(app.is_confirm_visible());

        // Char keys like 's' that normally fire ToggleSecretVisibility
        // must NOT take effect while a confirm is focused.
        let s = press(KeyCode::Char('s'));
        let outcome = app.dispatch_key(s);
        assert!(matches!(outcome, DispatchOutcome::Continue));
        assert!(!app.secrets_visible(), "modal must steal focus from `s`");
        assert!(app.is_confirm_visible());

        // Arrow keys must not navigate either.
        let down = press(KeyCode::Down);
        app.dispatch_key(down);
        assert!(app.is_confirm_visible());
    }

    #[test]
    fn modal_n_or_esc_cancels_without_side_effects() {
        let mut app = AppState::new();
        app.refresh(&five_rows()).unwrap();
        app.apply(Action::DeleteVar);
        let outcome = app.dispatch_key(press(KeyCode::Char('n')));
        assert!(matches!(outcome, DispatchOutcome::Continue));
        assert!(!app.is_confirm_visible());

        app.apply(Action::DeleteVar);
        let outcome = app.dispatch_key(press(KeyCode::Esc));
        assert!(matches!(outcome, DispatchOutcome::Continue));
        assert!(!app.is_confirm_visible());
    }

    #[test]
    fn modal_y_emits_delete_requested_with_id_and_name() {
        let mut app = AppState::new();
        app.refresh(&five_rows()).unwrap();
        app.apply(Action::MoveDown); // API_KEY
        let target_id = app.selected_row().expect("selection").id;
        app.apply(Action::DeleteVar);
        let outcome = app.dispatch_key(press(KeyCode::Char('y')));
        assert!(!app.is_confirm_visible(), "modal must clear after accept");
        match outcome {
            DispatchOutcome::DeleteRequested { id, name } => {
                assert_eq!(id, target_id);
                assert_eq!(name, "API_KEY");
            }
            other => panic!("expected DeleteRequested, got {other:?}"),
        }
    }

    #[test]
    fn modal_enter_also_accepts() {
        let mut app = AppState::new();
        app.refresh(&five_rows()).unwrap();
        app.apply(Action::DeleteVar);
        let outcome = app.dispatch_key(press(KeyCode::Enter));
        assert!(!app.is_confirm_visible());
        assert!(matches!(outcome, DispatchOutcome::DeleteRequested { .. }));
    }

    #[test]
    fn ctrl_c_quits_even_with_modal_focused() {
        let mut app = AppState::new();
        app.refresh(&five_rows()).unwrap();
        app.apply(Action::DeleteVar);
        let ctrl_c = KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL);
        app.dispatch_key(ctrl_c);
        assert!(app.quit_requested());
    }

    #[test]
    fn modal_plain_c_does_not_quit_or_dismiss() {
        // Regression: only Ctrl-C exits the modal; a plain `c`
        // keystroke is an unrecognised input that must leave the
        // modal focused.
        let mut app = AppState::new();
        app.refresh(&five_rows()).unwrap();
        app.apply(Action::DeleteVar);
        app.dispatch_key(press(KeyCode::Char('c')));
        assert!(app.is_confirm_visible());
        assert!(!app.quit_requested());
    }

    #[test]
    fn modal_reject_also_closes_help_overlay() {
        // The modal steals focus from `?`, so help can only be open
        // BEFORE the modal is raised. Rejecting the modal with Esc
        // should cascade and close help too — otherwise the user
        // sees nothing visible change when they hit Esc the first
        // time (modal disappears but help still covers everything).
        let mut app = AppState::new();
        app.refresh(&five_rows()).unwrap();
        app.apply(Action::ToggleHelp);
        assert!(app.help_visible());
        app.apply(Action::DeleteVar);
        assert!(app.is_confirm_visible());
        app.dispatch_key(press(KeyCode::Esc));
        assert!(!app.is_confirm_visible());
        assert!(!app.help_visible(), "Esc cascade must close help too");
    }

    #[test]
    fn splice_out_row_removes_local_entry_and_rebuilds_filter() {
        let mut app = AppState::new();
        app.refresh(&five_rows()).unwrap();
        // Snapshot the API_KEY id.
        app.apply(Action::MoveDown);
        let target_id = app.selected_row().expect("selection").id;

        // Apply a filter that matches `target_id` so we can confirm
        // the filter buffer is also re-ranked after the splice.
        app.apply(Action::StartFuzzy);
        app.dispatch_key(press(KeyCode::Char('a')));
        let before = app.visible_rows().count();
        assert!(before >= 1);

        app.splice_out_row(target_id);
        assert!(app.rows().iter().all(|v| v.id != target_id));
        assert!(app.visible_rows().count() < before);
    }

    #[test]
    fn splice_out_row_on_inspected_var_returns_to_dashboard() {
        let mut app = AppState::new();
        app.refresh(&five_rows()).unwrap();
        app.apply(Action::MoveDown);
        let target_id = app.selected_row().expect("selection").id;
        app.apply(Action::OpenDetail);
        assert_eq!(app.current_view(), View::Detail);

        app.splice_out_row(target_id);
        assert_eq!(
            app.current_view(),
            View::Dashboard,
            "splice of the inspected var must return to dashboard"
        );
        assert!(app.detail_row().is_none());
    }

    #[test]
    fn splice_out_row_on_unknown_id_is_a_noop() {
        let mut app = AppState::new();
        app.refresh(&five_rows()).unwrap();
        let before = app.rows().len();
        // Random VarId — must not be one of the five we just loaded.
        let bogus = VarId::new_v4();
        app.splice_out_row(bogus);
        assert_eq!(app.rows().len(), before);
    }

    #[test]
    fn unknown_keys_keep_modal_focused() {
        let mut app = AppState::new();
        app.refresh(&five_rows()).unwrap();
        app.apply(Action::DeleteVar);
        // Letter neither y/Y/n/N nor Enter/Esc — must not dismiss.
        app.dispatch_key(press(KeyCode::Char('q')));
        assert!(app.is_confirm_visible());
        assert!(!app.quit_requested());
    }

    #[test]
    fn refresh_rebuilds_filter_against_new_rows() {
        let mut app = AppState::new();
        app.refresh(&five_rows()).unwrap();
        app.apply(Action::StartFuzzy);
        app.dispatch_key(press(KeyCode::Char('d')));
        let before = app.visible_rows().count();
        // Shrink the underlying data and refresh.
        let shrunk = StaticProvider(vec![summary("DATABASE_URL")]);
        app.refresh(&shrunk).unwrap();
        // Filter must still be applied and re-ranked against the new rows.
        assert!(app.is_filter_active());
        assert_eq!(app.filter_needle(), Some("d"));
        let after = app.visible_rows().count();
        assert!(after <= before);
    }
}