nightporter 0.4.6

Handles your PRs while you're away: an agentic night shift for GitHub and GitLab
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
//! Terminal UI. Keyboard-only, text labels for every state (colour is decoration, never the only
//! signal), honours `NO_COLOR`. Reads/writes the same SQLite state as the daemon.

use crate::actions::{self, Scope};
use crate::config::{AgentKind, Config};
use crate::forge::Event;
use crate::paths::Paths;
use crate::state::{Db, Disposition, Job, JobStatus};
use anyhow::Result;
use crossterm::event::{self, Event as CEvent, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
use ratatui::prelude::*;
use ratatui::widgets::{Block, Borders, List, ListItem, ListState, Paragraph, Tabs, Wrap};
use std::time::{Duration, Instant};

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Tab {
    Queue,
    Inbox,
    History,
    Repos,
    Settings,
    Worktrees,
    Help,
}
const TABS: [(Tab, &str); 7] = [
    (Tab::Queue, "1 Queue"),
    (Tab::Inbox, "2 Inbox"),
    (Tab::History, "3 History"),
    (Tab::Repos, "4 Repos"),
    (Tab::Settings, "5 Settings"),
    (Tab::Worktrees, "6 Worktrees"),
    (Tab::Help, "? Help"),
];

/// Work that needs the real terminal: the main loop leaves the alternate screen for it.
enum Suspend {
    Setup,
    Editor(std::path::PathBuf),
    /// Edit a job's replies in $EDITOR.
    Replies(i64),
}

/// A pending yes/no question and what to do on "y".
type Confirm = Box<dyn FnOnce(&mut App) -> Result<String>>;

#[derive(Clone)]
struct RepoRow {
    id: crate::forge::RepoId,
    subscribed: bool,
    level: String,
    agent: String,
    /// What an open schedule window changes right now, e.g. `now auto, from window`. `l` and `a`
    /// cycle the repo's own level and agent (shown), not the window's.
    window: String,
}

#[allow(clippy::struct_excessive_bools, reason = "independent UI flags, not a state machine")]
struct App {
    paths: Paths,
    db: Db,
    tab: Tab,
    sel: ListState,
    queue: Vec<Job>,
    history: Vec<Job>,
    inbox: Vec<Event>,
    repos: Vec<RepoRow>,
    detail: Option<i64>,
    /// Rendered once per refresh, not per frame (a job can carry a large diff).
    detail_text: String,
    scroll: u16,
    msg: String,
    confirm: Option<(String, Confirm)>,
    color: bool,
    daemon_up: bool,
    paused: bool,
    suspend: Option<Suspend>,
    settings: Vec<crate::settings::Row>,
    /// Why the Settings tab is empty: `config.toml` doesn't load.
    settings_err: String,
    /// Text being typed for the selected setting.
    input: Option<String>,
    /// Unfiltered Repos and Settings rows; `repos` / `settings` are what `filter` lets through.
    repos_all: Vec<RepoRow>,
    settings_all: Vec<crate::settings::Row>,
    /// `/` search on the Repos and Settings tabs; `filtering` while it's being typed.
    filter: String,
    filtering: bool,
    history_all: Vec<Job>,
    /// Enter on the Repos tab: that repo's settings, shown in the detail pane.
    repo_view: Option<crate::forge::RepoId>,
    /// This session's dismissals and ignores, newest last, for `u` (undo).
    undo_jobs: Vec<i64>,
    undo_events: Vec<String>,
    update: Option<String>,
    /// Loaded on opening the tab and after a change (one git call per clone).
    worktrees: Vec<crate::worktrees::Wt>,
}

/// No forge account configured yet: nothing can be watched, so the wizard is the useful first step.
fn needs_setup(paths: &Paths) -> bool {
    Config::load(paths).is_ok_and(|c| c.accounts.is_empty())
}

/// Run the wizard outside the TUI, then wait so its last lines can be read.
fn setup_then_pause(paths: &Paths) {
    if let Err(e) = crate::setup::run(paths) {
        println!("\nsetup stopped: {e:#}");
    }
    print!("\nPress Enter to continue to Night Porter… ");
    let _ = std::io::Write::flush(&mut std::io::stdout());
    let _ = std::io::stdin().read_line(&mut String::new());
}

pub fn run(paths: &Paths) -> Result<()> {
    if needs_setup(paths) && crate::setup::yes("Night Porter isn't set up yet. Run the setup wizard now?", true)? {
        setup_then_pause(paths);
    }
    let mut app = App::new(paths, Db::open(&paths.db())?);
    app.refresh(true);
    app.update = crate::update::banner(&app.db);
    if !app.daemon_up {
        app.msg = "Night Porter's daemon isn't running, so nothing is being watched. Press D to start it.".into();
    } else if let Some(u) = &app.update {
        app.msg.clone_from(u);
    }
    let mut term = ratatui::init();
    let res = app.main_loop(&mut term);
    ratatui::restore();
    res
}

impl App {
    fn new(paths: &Paths, db: Db) -> Self {
        App {
            paths: paths.clone(),
            db,
            tab: Tab::Queue,
            sel: ListState::default().with_selected(Some(0)),
            queue: vec![],
            history: vec![],
            inbox: vec![],
            repos: vec![],
            detail: None,
            detail_text: String::new(),
            scroll: 0,
            msg: "Press ? for help.".into(),
            confirm: None,
            color: std::env::var_os("NO_COLOR").is_none(),
            daemon_up: false,
            paused: false,
            suspend: None,
            settings: vec![],
            settings_err: String::new(),
            input: None,
            repos_all: vec![],
            settings_all: vec![],
            filter: String::new(),
            filtering: false,
            history_all: vec![],
            repo_view: None,
            undo_jobs: vec![],
            undo_events: vec![],
            update: None,
            worktrees: vec![],
        }
    }

    fn main_loop(&mut self, term: &mut ratatui::DefaultTerminal) -> Result<()> {
        let mut last = Instant::now();
        loop {
            term.draw(|f| self.draw(f))?;
            if event::poll(Duration::from_millis(500))?
                && let CEvent::Key(k) = event::read()?
                && k.kind == KeyEventKind::Press
                && !self.key(k)
            {
                return Ok(());
            }
            if let Some(job) = self.suspend.take() {
                ratatui::restore();
                self.msg = match job {
                    Suspend::Setup => {
                        setup_then_pause(&self.paths);
                        "setup finished".into()
                    }
                    Suspend::Editor(path) => crate::settings::edit_in_editor(&path).unwrap_or_else(|e| format!("error: {e:#}")),
                    Suspend::Replies(id) => actions::edit_replies(&self.paths, &self.db, id).unwrap_or_else(|e| format!("error: {e:#}")),
                };
                *term = ratatui::init();
                self.refresh(true);
            }
            if last.elapsed() > Duration::from_secs(2) {
                self.refresh(false);
                last = Instant::now();
            }
        }
    }

    fn refresh(&mut self, repos: bool) {
        let jobs = self.db.recent_jobs(500).unwrap_or_default();
        let day_ago = crate::state::now() - 86400;
        let recent_failure = |j: &Job| j.status == JobStatus::Failed && j.updated_at > day_ago;
        self.queue = jobs.iter().filter(|j| j.status.needs_user() || j.status.is_active() || recent_failure(j)).cloned().collect();
        // Needs-you first, then today's failures, then running.
        self.queue.sort_by_key(|j| (!j.status.needs_user(), j.status != JobStatus::Failed, j.id));
        if let Some(id) = self.detail {
            self.detail_text = self.db.job(id).ok().flatten().map_or_else(|| "job not found".into(), |j| job_text(&self.db, &j));
        } else if let Some(r) = &self.repo_view {
            self.detail_text = Config::load(&self.paths)
                .and_then(|cfg| {
                    let clone = crate::repos::scan(&cfg.code_dir(), cfg.scan_depth, &cfg.accounts).remove(r);
                    crate::settings::repo_text(&self.paths, &cfg, r, clone.as_deref())
                })
                .unwrap_or_else(|e| format!("error: {e:#}"));
        }
        self.history_all = jobs;
        self.history.clone_from(&self.history_all);
        self.inbox = self.db.events_with(Disposition::Inbox).unwrap_or_default().into_iter().map(|(e, _)| e).collect();
        self.paused = actions::paused(&self.db);
        self.daemon_up = self.paths.daemon_running();
        if repos {
            (self.settings_all, self.settings_err) = match crate::settings::rows(&self.paths) {
                Ok(r) => (r, String::new()),
                Err(e) => (vec![], format!("{e:#}")),
            };
            self.repos_all = match Config::load(&self.paths) {
                Ok(cfg) => crate::repos::scan(&cfg.code_dir(), cfg.scan_depth, &cfg.accounts)
                    .into_keys()
                    .map(|id| match cfg.base_settings_for(&self.paths, &id) {
                        Ok(s) => RepoRow {
                            subscribed: s.subscribed,
                            level: s.level.as_str().into(),
                            agent: s.agent.label().into(),
                            window: window_note(&s, &s.clone().at(&jiff::Zoned::now())),
                            id,
                        },
                        Err(e) => RepoRow { subscribed: false, level: "ERROR".into(), agent: format!("{e:#}"), window: String::new(), id },
                    })
                    .collect(),
                Err(e) => {
                    self.msg = format!("config error: {e:#}");
                    vec![]
                }
            };
            // Watched repos first; the scan is already sorted by name.
            self.repos_all.sort_by_key(|r| !r.subscribed);
        }
        self.apply_filter();
        let n = self.len();
        if self.sel.selected().is_some_and(|i| i >= n) {
            self.sel.select(Some(n.saturating_sub(1)));
        }
    }

    /// Case-insensitive substring match on the repo path or the setting key.
    fn apply_filter(&mut self) {
        let f = self.filter.to_lowercase();
        self.repos =
            self.repos_all.iter().filter(|r| format!("{}/{}", r.id.host, r.id.path).to_lowercase().contains(&f)).cloned().collect();
        self.settings = self.settings_all.iter().filter(|r| r.setting.key.contains(&f)).cloned().collect();
        self.history = self.history_all.iter().filter(|j| job_line(j).to_lowercase().contains(&f)).cloned().collect();
        if self.sel.selected().is_some_and(|i| i >= self.len()) {
            self.sel.select(Some(0));
        }
    }

    fn filter_key(&mut self, k: KeyEvent) {
        match k.code {
            KeyCode::Char(c) => self.filter.push(c),
            KeyCode::Backspace => {
                self.filter.pop();
            }
            KeyCode::Esc => {
                self.filter.clear();
                self.filtering = false;
            }
            KeyCode::Enter | KeyCode::Down | KeyCode::Up => self.filtering = false,
            _ => return,
        }
        self.apply_filter();
    }

    fn len(&self) -> usize {
        match self.tab {
            Tab::Queue => self.queue.len(),
            Tab::Inbox => self.inbox.len(),
            Tab::History => self.history.len(),
            Tab::Repos => self.repos.len(),
            Tab::Settings => self.settings.len(),
            Tab::Worktrees => self.worktrees.len(),
            Tab::Help => 0,
        }
    }

    fn selected_job(&self) -> Option<&Job> {
        let i = self.sel.selected()?;
        match self.tab {
            Tab::Queue => self.queue.get(i),
            Tab::History => self.history.get(i),
            Tab::Worktrees => self.worktrees.get(i).map(|w| &w.job),
            _ => None,
        }
    }

    fn style(&self, s: Style) -> Style {
        if self.color { s } else { Style::default().add_modifier(s.add_modifier) }
    }

    /// Returns false to quit.
    fn key(&mut self, k: KeyEvent) -> bool {
        if let Some((_, f)) = self.confirm.take() {
            if matches!(k.code, KeyCode::Char('y' | 'Y')) {
                self.msg = f(self).unwrap_or_else(|e| format!("error: {e:#}"));
                self.refresh(true);
            } else {
                self.msg = "cancelled".into();
            }
            return true;
        }
        if k.modifiers.contains(KeyModifiers::CONTROL) && k.code == KeyCode::Char('c') {
            return false;
        }
        if self.input.is_some() {
            self.input_key(k);
            return true;
        }
        if self.filtering {
            self.filter_key(k);
            return true;
        }
        if self.detail.is_some() || self.repo_view.is_some() {
            self.detail_key(k);
            return true;
        }
        match k.code {
            // Esc first clears a search, then quits.
            KeyCode::Esc if !self.filter.is_empty() => {
                self.filter.clear();
                self.apply_filter();
            }
            KeyCode::Char('q') | KeyCode::Esc => return false,
            KeyCode::Char('1') => self.switch(Tab::Queue),
            KeyCode::Char('2') => self.switch(Tab::Inbox),
            KeyCode::Char('3') => self.switch(Tab::History),
            KeyCode::Char('4') => self.switch(Tab::Repos),
            KeyCode::Char('5') => self.switch(Tab::Settings),
            KeyCode::Char('6') => self.switch(Tab::Worktrees),
            KeyCode::Char('?') | KeyCode::F(1) => self.switch(Tab::Help),
            KeyCode::Tab | KeyCode::Right => {
                let i = TABS.iter().position(|(t, _)| *t == self.tab).unwrap_or(0);
                self.switch(TABS[(i + 1) % TABS.len()].0);
            }
            KeyCode::BackTab | KeyCode::Left => {
                let i = TABS.iter().position(|(t, _)| *t == self.tab).unwrap_or(0);
                self.switch(TABS[(i + TABS.len() - 1) % TABS.len()].0);
            }
            KeyCode::Down | KeyCode::Char('j') if self.tab == Tab::Help => {
                self.scroll = (self.scroll + 1).min(u16::try_from(HELP.lines().count()).unwrap_or(0));
            }
            KeyCode::Up | KeyCode::Char('k') if self.tab == Tab::Help => self.scroll = self.scroll.saturating_sub(1),
            KeyCode::Down | KeyCode::Char('j') => {
                self.sel.select(Some((self.sel.selected().unwrap_or(0) + 1).min(self.len().saturating_sub(1))));
            }
            KeyCode::Up | KeyCode::Char('k') => self.sel.select(Some(self.sel.selected().unwrap_or(0).saturating_sub(1))),
            KeyCode::Home | KeyCode::Char('g') => self.sel.select(Some(0)),
            KeyCode::PageDown => self.sel.select(Some((self.sel.selected().unwrap_or(0) + 10).min(self.len().saturating_sub(1)))),
            KeyCode::PageUp => self.sel.select(Some(self.sel.selected().unwrap_or(0).saturating_sub(10))),
            KeyCode::Char('/') if matches!(self.tab, Tab::Repos | Tab::Settings | Tab::History) => {
                self.filtering = true;
                self.sel.select(Some(0));
            }

            KeyCode::End | KeyCode::Char('G') => self.sel.select(Some(self.len().saturating_sub(1))),
            KeyCode::Char('p') => {
                let p = !self.paused;
                self.msg = match actions::set_paused(&self.db, p) {
                    Ok(()) if p => "paused: no new jobs will start".into(),
                    Ok(()) => "resumed".into(),
                    Err(e) => format!("error: {e:#}"),
                };
                self.paused = p;
            }
            KeyCode::Char('S') => self.suspend = Some(Suspend::Setup),
            KeyCode::Char('D') => self.toggle_daemon(),
            KeyCode::Char('R') if self.tab == Tab::Repos => {
                self.refresh(true);
                self.msg = "rescanned".into();
            }
            KeyCode::Enter if self.tab == Tab::Repos => {
                if let Some(r) = self.sel.selected().and_then(|i| self.repos.get(i)) {
                    self.repo_view = Some(r.id.clone());
                    self.refresh(false);
                }
            }
            KeyCode::Enter if matches!(self.tab, Tab::Queue | Tab::History | Tab::Worktrees) => {
                if let Some(j) = self.selected_job() {
                    self.detail = Some(j.id);
                    self.refresh(false);
                }
            }
            _ => match self.tab {
                Tab::Queue | Tab::History => self.job_key(k),
                Tab::Inbox => self.inbox_key(k),
                Tab::Repos => self.repo_key(k),
                Tab::Settings => self.settings_key(k),
                Tab::Worktrees => self.worktree_key(k),
                Tab::Help => {}
            },
        }
        true
    }

    /// Keys in the job or repo detail pane.
    fn detail_key(&mut self, k: KeyEvent) {
        match k.code {
            KeyCode::Esc | KeyCode::Char('q') | KeyCode::Backspace => {
                self.detail = None;
                self.repo_view = None;
                self.scroll = 0;
            }
            KeyCode::Char('e') if self.repo_view.is_some() => {
                self.suspend = self.repo_view.as_ref().map(|r| Suspend::Editor(self.paths.repo_file(r)));
            }
            KeyCode::Down | KeyCode::Char('j') => self.scroll = self.scroll.saturating_add(1).min(self.max_scroll()),
            KeyCode::Up | KeyCode::Char('k') => self.scroll = self.scroll.saturating_sub(1),
            KeyCode::PageDown | KeyCode::Char(' ') => self.scroll = self.scroll.saturating_add(20).min(self.max_scroll()),
            KeyCode::PageUp => self.scroll = self.scroll.saturating_sub(20),
            _ if self.detail.is_some() => self.job_key(k),
            _ => {}
        }
    }

    /// D: start the daemon, or stop it after asking.
    fn toggle_daemon(&mut self) {
        if self.daemon_up {
            self.confirm = Some((
                "Stop Night Porter? Nothing is watched until you start it again (D). y/N".into(),
                Box::new(|a: &mut App| crate::service::stop(&a.paths)),
            ));
        } else {
            self.msg = crate::service::start(&self.paths).unwrap_or_else(|e| format!("error: {e:#}"));
            self.refresh(false);
        }
    }

    fn max_scroll(&self) -> u16 {
        u16::try_from(self.detail_text.lines().count().saturating_sub(5)).unwrap_or(u16::MAX)
    }

    fn switch(&mut self, t: Tab) {
        self.tab = t;
        self.scroll = 0;
        // A search belongs to one list.
        if !self.filter.is_empty() || self.filtering {
            self.filter.clear();
            self.filtering = false;
            self.apply_filter();
        }
        self.sel.select(Some(0));
        if t == Tab::Repos {
            self.refresh(true);
        }
        if t == Tab::Worktrees {
            self.load_worktrees();
        }
    }

    fn load_worktrees(&mut self) {
        match crate::worktrees::block(crate::worktrees::list(&self.db)) {
            Ok(w) => self.worktrees = w,
            Err(e) => self.msg = format!("error: {e:#}"),
        }
        let n = self.len();
        if self.sel.selected().is_some_and(|i| i >= n) {
            self.sel.select(Some(n.saturating_sub(1)));
        }
    }

    /// x remove (asks first; kept for a week), u bring a removed one back, h take over.
    fn worktree_key(&mut self, k: KeyEvent) {
        let Some(w) = self.sel.selected().and_then(|i| self.worktrees.get(i)) else { return };
        let (id, removed, path) = (w.job.id, w.trashed.is_some(), w.path.display().to_string());
        let which = w.trashed.as_ref().map(|t| t.1.clone());
        match k.code {
            KeyCode::Char('x' | 'd') if !removed => {
                self.confirm = Some((
                    format!("Remove {path}? Its commits and changes are kept for 7 days (u brings it back). y/N"),
                    Box::new(move |a: &mut App| {
                        let cfg = Config::load(&a.paths)?;
                        let r = crate::worktrees::block(crate::worktrees::remove(&a.paths, &cfg, &a.db, id));
                        a.load_worktrees();
                        r
                    }),
                ));
            }
            KeyCode::Char('u') if removed => {
                let r = crate::worktrees::restore(&self.db, id, which.as_deref());
                self.msg = crate::worktrees::block(r).unwrap_or_else(|e| format!("error: {e:#}"));
                self.load_worktrees();
            }
            KeyCode::Char('u') => self.msg = "select a KEPT row to bring it back".into(),
            KeyCode::Char('h') if !removed => self.job_key(k),
            _ => {}
        }
    }

    fn current_job_id(&self) -> Option<i64> {
        self.detail.or_else(|| self.selected_job().map(|j| j.id))
    }

    fn job_key(&mut self, k: KeyEvent) {
        // u still works with an empty list: it brings back the last dismissed job.
        let Some(id) = self.current_job_id().or_else(|| self.undo_jobs.last().copied()) else { return };
        let r = match k.code {
            KeyCode::Char('a') => actions::approve(&self.db, id, false).map(String::from),
            KeyCode::Char('A') => {
                self.confirm = Some((
                    format!("Override blocking checks for job {id}? y/N"),
                    Box::new(move |a: &mut App| actions::approve(&a.db, id, true).map(String::from)),
                ));
                return;
            }
            KeyCode::Char('r') => {
                self.confirm = Some((
                    format!("Reject job {id}? Nothing is pushed or posted. y/N"),
                    Box::new(move |a: &mut App| actions::reject(&a.db, id).map(|()| format!("job {id} rejected"))),
                ));
                return;
            }
            KeyCode::Char('t') => actions::retry(&self.db, id).map(|n| format!("re-queued {n} event(s)")),
            KeyCode::Char('e') => {
                // Checked here so a job that can't be edited says why without leaving the TUI.
                match actions::replies_file(&self.db, id) {
                    Ok(_) => self.suspend = Some(Suspend::Replies(id)),
                    Err(e) => self.msg = format!("error: {e:#}"),
                }
                return;
            }
            KeyCode::Char('m') => actions::amend(&self.db, id).map(String::from),
            KeyCode::Char('x') => actions::dismiss(&self.db, id).map(|()| {
                self.undo_jobs.push(id);
                self.detail = None;
                format!("job {id} dismissed: nothing is pushed or posted. u undoes; History keeps it")
            }),
            KeyCode::Char('u') => {
                let selected_dismissed = self.db.job(id).ok().flatten().is_some_and(|j| j.status == JobStatus::Dismissed);
                match if selected_dismissed { Some(id) } else { self.undo_jobs.pop() } {
                    Some(u) => {
                        self.undo_jobs.retain(|x| *x != u);
                        actions::restore(&self.db, u).map(|s| format!("job {u} restored: {}", s.label()))
                    }
                    None => Ok("nothing to undo".into()),
                }
            }
            KeyCode::Char('X') => {
                self.confirm = Some((
                    format!("Delete job {id} and its log for good? Can't be undone. y/N"),
                    Box::new(move |a: &mut App| {
                        a.detail = None;
                        actions::purge(&mut a.db, id).map(|()| format!("job {id} deleted"))
                    }),
                ));
                return;
            }
            KeyCode::Char('h') => match self.db.job(id) {
                Ok(Some(j)) => crate::handoff::take_over(&self.paths, &self.db, &j),
                _ => Ok("job not found".into()),
            },
            KeyCode::Char('c') => {
                let cmd = self.db.job(id).ok().flatten().map(|j| crate::job::manual_commands(&j)).unwrap_or_default();
                if cmd.is_empty() { Ok("nothing to push for this job".into()) } else { copy(&cmd).map(|()| format!("copied: {cmd}")) }
            }
            KeyCode::Char('y') => {
                let text = self
                    .db
                    .job(id)
                    .ok()
                    .flatten()
                    .map(|j| j.replies.iter().map(|r| r.body.clone()).collect::<Vec<_>>().join("\n\n---\n\n"))
                    .unwrap_or_default();
                copy(&text).map(|()| "replies copied".into())
            }
            _ => return,
        };
        self.msg = r.unwrap_or_else(|e| format!("error: {e:#}"));
        self.refresh(false);
    }

    fn inbox_key(&mut self, k: KeyEvent) {
        if k.code == KeyCode::Char('u') {
            self.msg = match self.undo_events.pop() {
                Some(id) => actions::unignore(&self.db, &id).map_or_else(|e| format!("error: {e:#}"), |()| "back in the inbox".into()),
                None => "nothing to undo (`nightporter inbox list --ignored` shows older ones)".into(),
            };
            self.refresh(false);
            return;
        }
        let Some(e) = self.sel.selected().and_then(|i| self.inbox.get(i)).cloned() else { return };
        let r = match k.code {
            KeyCode::Char('o') => actions::allow_once(&self.db, &e.id).map(|()| "allowed once".into()),
            KeyCode::Char('i') => actions::ignore(&self.db, &e.id).map(|()| {
                self.undo_events.push(e.id.clone());
                "ignored (u undoes)".into()
            }),
            KeyCode::Char('a') => actions::allow_always(&self.paths, &self.db, &e.author, Scope::Repo(&e.repo))
                .map(|n| format!("@{} trusted on {} ({n} released)", e.author, e.repo)),
            KeyCode::Char('A') => {
                let (author, paths) = (e.author.clone(), self.paths.clone());
                self.confirm = Some((
                    format!("Trust @{author} on ALL repos? y/N"),
                    Box::new(move |a: &mut App| {
                        actions::allow_always(&paths, &a.db, &author, Scope::Global)
                            .map(|n| format!("@{author} trusted everywhere ({n} released)"))
                    }),
                ));
                return;
            }
            KeyCode::Char('d') => {
                let (author, repo, paths) = (e.author.clone(), e.repo.clone(), self.paths.clone());
                self.confirm = Some((
                    format!("Deny @{author} on {repo}? y/N"),
                    Box::new(move |a: &mut App| {
                        actions::deny(&paths, &a.db, &author, Scope::Repo(&repo))
                            .map(|n| format!("@{author} denied on {repo} ({n} dropped)"))
                    }),
                ));
                return;
            }
            _ => return,
        };
        self.msg = r.unwrap_or_else(|e| format!("error: {e:#}"));
        self.refresh(false);
    }

    fn repo_key(&mut self, k: KeyEvent) {
        let Some(r) = self.sel.selected().and_then(|i| self.repos.get(i)) else { return };
        let (id, path) = (r.id.clone(), self.paths.repo_file(&r.id));
        let set = move |key: &'static str, val: &'static str| {
            crate::config::edit_file(&path, |d| d[key] = toml_edit::value(val)).map(|()| format!("{} {key} = {val}", id.path))
        };
        let r = match k.code {
            KeyCode::Char(' ' | 's') => {
                let on = !r.subscribed;
                actions::set_subscribed(&self.paths, &r.id, on)
                    .map(|()| format!("{} {}", if on { "subscribed to" } else { "unsubscribed from" }, r.id.path))
            }
            KeyCode::Char('l') => {
                let next = match r.level.as_str() {
                    "observe" => "suggest",
                    "suggest" => "draft",
                    "draft" => "auto",
                    _ => "observe",
                };
                if next == "auto" {
                    self.confirm = Some((
                        format!("Let {} push and reply on its own (auto)? y/N", r.id.path),
                        Box::new(move |_: &mut App| set("level", "auto")),
                    ));
                    return;
                }
                set("level", next)
            }
            KeyCode::Char('a') => {
                let now = AgentKind::ALL.into_iter().find(|a| a.label() == r.agent).unwrap_or(AgentKind::Claude);
                set("agent", now.next().bin())
            }
            KeyCode::Char('e') => {
                self.suspend = Some(Suspend::Editor(self.paths.repo_file(&r.id)));
                return;
            }
            _ => return,
        };
        self.msg = r.unwrap_or_else(|e| format!("error: {e:#}"));
        self.refresh(true);
    }

    fn settings_key(&mut self, k: KeyEvent) {
        if k.code == KeyCode::Char('e') {
            self.suspend = Some(Suspend::Editor(self.paths.config_file()));
            return;
        }
        let Some(row) = self.sel.selected().and_then(|i| self.settings.get(i)) else { return };
        let s = row.setting;
        let r = match k.code {
            KeyCode::Char('d') | KeyCode::Delete => crate::settings::set(&self.paths, s, ""),
            KeyCode::Enter | KeyCode::Char(' ') => match crate::settings::next(s, &row.value) {
                Some(v) if s.confirm == Some(v) => {
                    self.confirm =
                        Some((format!("Set {} = {v}? y/N", s.key), Box::new(move |a: &mut App| crate::settings::set(&a.paths, s, v))));
                    return;
                }
                Some(v) => crate::settings::set(&self.paths, s, v),
                None if s.kind == crate::settings::Kind::Info => Ok(s.help.into()),
                None => {
                    self.input = Some(row.value.clone());
                    return;
                }
            },
            _ => return,
        };
        self.msg = r.unwrap_or_else(|e| format!("not saved: {e:#}"));
        self.refresh(true);
    }

    /// Typing a value for the selected setting. Enter saves (if valid), Esc cancels.
    fn input_key(&mut self, k: KeyEvent) {
        let Some(buf) = self.input.as_mut() else { return };
        match k.code {
            KeyCode::Char(c) => buf.push(c),
            KeyCode::Backspace => {
                buf.pop();
            }
            KeyCode::Esc => {
                self.input = None;
                self.msg = "cancelled".into();
            }
            KeyCode::Enter => {
                let Some(s) = self.sel.selected().and_then(|i| self.settings.get(i)).map(|r| r.setting) else { return };
                match crate::settings::set(&self.paths, s, buf) {
                    Ok(m) => {
                        self.input = None;
                        self.msg = m;
                        self.refresh(true);
                    }
                    // Keep the text so it can be fixed.
                    Err(e) => self.msg = format!("not saved: {e:#}"),
                }
            }
            _ => {}
        }
    }

    /// Top line: daemon, pause, and what's waiting, coloured by urgency (and always in words).
    fn header(&self) -> Line<'static> {
        let needs = self.queue.iter().filter(|j| j.status.needs_user()).count();
        let failed = self.queue.iter().filter(|j| j.status == JobStatus::Failed).count();
        let (good, warn, bad, dim) = (
            Style::new().fg(Color::Green).add_modifier(Modifier::BOLD),
            Style::new().fg(Color::Yellow).add_modifier(Modifier::BOLD),
            Style::new().fg(Color::Red).add_modifier(Modifier::BOLD),
            Style::new().add_modifier(Modifier::DIM),
        );
        let count = |n: usize, s: Style| self.style(if n > 0 { s } else { dim });
        let mut status = vec![
            Span::styled(" Night Porter ", self.style(Style::new().fg(Color::Black).bg(Color::Magenta).add_modifier(Modifier::BOLD))),
            Span::raw(" daemon "),
            if self.daemon_up {
                Span::styled("running", self.style(good))
            } else {
                Span::styled("NOT RUNNING (D starts it)", self.style(bad))
            },
            Span::raw(" · "),
            if self.paused { Span::styled("PAUSED", self.style(warn)) } else { Span::styled("active", self.style(good)) },
            Span::raw(" · "),
            Span::styled(format!("{needs} need you"), count(needs, warn)),
            Span::raw(" · "),
            Span::styled(format!("{failed} failed today"), count(failed, bad)),
            Span::raw(" · "),
            Span::styled(format!("{} in inbox", self.inbox.len()), count(self.inbox.len(), warn)),
        ];
        if self.update.is_some() {
            status.extend([Span::raw(" · "), Span::styled("update available", self.style(Style::new().fg(Color::Cyan)))]);
        }
        Line::from(status)
    }

    fn draw(&mut self, f: &mut Frame) {
        let [head, tabs, body, foot] =
            Layout::vertical([Constraint::Length(1), Constraint::Length(1), Constraint::Min(3), Constraint::Length(2)]).areas(f.area());
        f.render_widget(Paragraph::new(self.header()), head);
        let titles: Vec<String> = TABS.iter().map(|(t, n)| if *t == self.tab { format!("[{n}]") } else { n.to_string() }).collect();
        let idx = TABS.iter().position(|(t, _)| *t == self.tab).unwrap_or(0);
        f.render_widget(
            Tabs::new(titles)
                .select(idx)
                .style(self.style(Style::new().fg(Color::Gray)))
                .highlight_style(self.style(Style::new().fg(Color::Cyan).add_modifier(Modifier::BOLD | Modifier::REVERSED))),
            tabs,
        );

        let detail_title = match (&self.detail, &self.repo_view) {
            (Some(id), _) => Some(format!(" Job {id} — Esc back ")),
            (_, Some(r)) => Some(format!(" {r} — e edit its file, Esc back ")),
            _ => None,
        };
        if let Some(title) = detail_title {
            let p = Paragraph::new(self.detail_text.as_str())
                .wrap(Wrap { trim: false })
                .scroll((self.scroll, 0))
                .block(Block::default().borders(Borders::ALL).border_style(self.style(Style::new().fg(Color::Cyan))).title(title));
            f.render_widget(p, body);
        } else if self.tab == Tab::Settings {
            self.draw_settings(f, body);
        } else if self.tab == Tab::Help {
            f.render_widget(
                Paragraph::new(HELP)
                    .wrap(Wrap { trim: false })
                    .scroll((self.scroll, 0))
                    .block(Block::default().borders(Borders::ALL).title(" Help (↑↓ scroll) ")),
                body,
            );
        } else {
            let items: Vec<ListItem> = match self.tab {
                Tab::Queue | Tab::History => {
                    let list = if self.tab == Tab::Queue { &self.queue } else { &self.history };
                    list.iter().map(|j| ListItem::new(job_line(j)).style(self.style(status_style(j.status)))).collect()
                }
                Tab::Inbox => self
                    .inbox
                    .iter()
                    .map(|e| {
                        ListItem::new(format!(
                            "@{:<18} {:?} on {}#{}: {}",
                            e.author,
                            e.role,
                            e.repo,
                            e.number,
                            e.body.lines().next().unwrap_or_default()
                        ))
                    })
                    .collect(),
                Tab::Repos => self.repo_items(),
                Tab::Worktrees => self.worktree_items(),
                Tab::Settings | Tab::Help => vec![],
            };
            let empty = items.is_empty();
            let title = match self.tab {
                Tab::Repos if !self.filter.is_empty() || self.filtering => {
                    &format!(" Repos matching \"{}\" ({} of {}) ", self.filter, self.repos.len(), self.repos_all.len())
                }
                Tab::History if !self.filter.is_empty() || self.filtering => {
                    &format!(" History matching \"{}\" ({} of {}) ", self.filter, self.history.len(), self.history_all.len())
                }
                Tab::Queue => " Needs you / running (x dismiss what you don't want to act on) ",
                Tab::Inbox => " Unknown authors waiting ",
                Tab::History => " History: every job, newest first (/ search, e.g. dismissed or a repo) ",
                Tab::Repos => {
                    let on = self.repos_all.iter().filter(|r| r.subscribed).count();
                    &format!(" Repos: {on} watched of {} (Enter details, space subscribe, / search) ", self.repos_all.len())
                }
                Tab::Worktrees => " Worktrees Night Porter made, then removed ones you can bring back ",
                Tab::Settings | Tab::Help => "",
            };
            if empty {
                let msg = match self.tab {
                    Tab::Queue => "Nothing needs you. Jobs show up here when your PRs get activity.",
                    Tab::Inbox => "No one is waiting.",
                    Tab::Repos | Tab::History if !self.filter.is_empty() => "Nothing matches the search. Esc clears it.",
                    Tab::Worktrees => "No worktrees. Each job gets one; they're removed when the PR closes or after worktree_idle_days.",
                    Tab::Repos => "No clones found. Press S to run the setup wizard (forges, clone folder, repos).",
                    _ => "Nothing yet.",
                };
                f.render_widget(Paragraph::new(msg).block(Block::default().borders(Borders::ALL).title(title)), body);
            } else {
                let list = List::new(items)
                    .block(Block::default().borders(Borders::ALL).border_style(self.style(Style::new().fg(Color::DarkGray))).title(title))
                    .highlight_symbol("> ")
                    .highlight_style(self.style(Style::new().add_modifier(Modifier::REVERSED)));
                f.render_stateful_widget(list, body, &mut self.sel);
            }
        }
        let (keys, line2) = self.footer();
        f.render_widget(Paragraph::new(vec![Line::from(keys), Line::from(line2)]).style(Style::new().add_modifier(Modifier::DIM)), foot);
    }
}

impl App {
    /// Footer: the keys for this view, then the question or last message.
    fn footer(&self) -> (&'static str, String) {
        let keys = match (self.detail.is_some(), self.tab) {
            // Kept under 80 columns.
            _ if self.repo_view.is_some() => "↑↓ scroll e edit this repo's file Esc back",
            (false, Tab::Repos | Tab::Settings | Tab::History) if self.filtering => "type to search, Enter keep results, Esc clear search",
            (true, _) => "↑↓ a approve e edit m amend r reject x dismiss t retry h take over y copy Esc",
            (_, Tab::Queue) => "Enter open a approve e edit m amend r reject x dismiss h take over q quit",
            (_, Tab::History) => "Enter open x dismiss u restore X delete t retry / search ? q quit",
            (_, Tab::Inbox) => "o once a trust A trust-all i ignore u undo d deny ? help q quit",
            (_, Tab::Repos) => "Enter details space subscribe l level a agent e edit / search ? q",
            (_, Tab::Settings) if self.input.is_some() => "type the value, Enter save, Esc cancel (empty = default)",
            (_, Tab::Settings) => "Enter change d default e edit file / search S setup ? q",
            (_, Tab::Worktrees) => "Enter job x remove u bring back h take over ? help q quit",
            (_, Tab::Help) => "1-6 tabs S setup wizard D start/stop daemon q quit",
        };
        let line2 = match (&self.confirm, &self.input) {
            (Some((q, _)), _) => q.clone(),
            (_, Some(buf)) => {
                let key = self.sel.selected().and_then(|i| self.settings.get(i)).map_or("", |r| r.setting.key);
                format!("{key} = {buf}_   {}", self.msg)
            }
            _ if self.filtering => format!("search: {}_", self.filter),
            _ => self.msg.clone(),
        };
        (keys, line2)
    }

    fn worktree_items(&self) -> Vec<ListItem<'static>> {
        let dim = |w: &crate::worktrees::Wt| if w.trashed.is_some() { Style::new().add_modifier(Modifier::DIM) } else { Style::new() };
        self.worktrees.iter().map(|w| ListItem::new(w.line()).style(self.style(dim(w)))).collect()
    }

    fn repo_items(&self) -> Vec<ListItem<'static>> {
        // Column as wide as the longest visible name, so levels line up.
        let w = self.repos.iter().map(|r| r.id.host.len() + r.id.path.len() + 1).max().unwrap_or(0);
        self.repos
            .iter()
            .map(|r| {
                let name = Span::styled(
                    format!("{:<w$} ", format!("{}/{}", r.id.host, r.id.path)),
                    if r.subscribed { Style::new().add_modifier(Modifier::BOLD) } else { Style::new().add_modifier(Modifier::DIM) },
                );
                ListItem::new(Line::from(vec![
                    if r.subscribed {
                        Span::styled("[x] ", self.style(Style::new().fg(Color::Green).add_modifier(Modifier::BOLD)))
                    } else {
                        Span::styled("[ ] ", self.style(Style::new().add_modifier(Modifier::DIM)))
                    },
                    name,
                    Span::styled(format!("{:<8} ", r.level), self.style(level_style(&r.level))),
                    Span::styled(r.agent.clone(), self.style(Style::new().fg(Color::Magenta))),
                    Span::styled(
                        if r.window.is_empty() { String::new() } else { format!("  ({})", r.window) },
                        Style::new().add_modifier(Modifier::DIM),
                    ),
                ]))
            })
            .collect()
    }

    fn draw_settings(&mut self, f: &mut Frame, body: Rect) {
        let title = if self.filter.is_empty() && !self.filtering {
            format!(" Settings: {} (each repo file can override defaults.*) ", self.paths.config_file().display())
        } else {
            format!(" Settings matching \"{}\" ({} of {}) ", self.filter, self.settings.len(), self.settings_all.len())
        };
        if !self.settings_err.is_empty() {
            let text = format!(
                "config.toml doesn't load, so Night Porter is using the last good config:\n\n  {}\n\nPress e to fix it in your editor. Your edit is checked before it's saved.",
                self.settings_err
            );
            f.render_widget(
                Paragraph::new(text).wrap(Wrap { trim: false }).block(Block::default().borders(Borders::ALL).title(title)),
                body,
            );
            return;
        }
        let [list_area, help_area] = Layout::vertical([Constraint::Min(3), Constraint::Length(3)]).areas(body);
        let items: Vec<ListItem> = self
            .settings
            .iter()
            .map(|r| {
                let value = if r.value.is_empty() { "(none)" } else { r.value.as_str() };
                let shown = match (r.setting.kind, value) {
                    (crate::settings::Kind::Bool, "true") => "[x] on".to_string(),
                    (crate::settings::Kind::Bool, _) => "[ ] off".to_string(),
                    _ => value.to_string(),
                };
                let source = if r.set {
                    Style::new().fg(Color::Yellow).add_modifier(Modifier::BOLD)
                } else {
                    Style::new().add_modifier(Modifier::DIM)
                };
                let value_style = match (r.setting.kind, value) {
                    (crate::settings::Kind::Bool, "true") => Style::new().fg(Color::Green).add_modifier(Modifier::BOLD),
                    (crate::settings::Kind::Bool, _) => Style::new().fg(Color::Red),
                    (_, "(none)") => Style::new().add_modifier(Modifier::DIM),
                    (crate::settings::Kind::Choice(_), v) if level_style(v) != Style::new() => level_style(v),
                    _ => Style::new().fg(Color::White),
                };
                // Global keys and per-repo defaults in different colours, so the two halves read apart.
                let key_style = if r.setting.key.starts_with("defaults.") { Color::Cyan } else { Color::Blue };
                ListItem::new(Line::from(vec![
                    Span::styled(format!("{:<34} ", r.setting.key), self.style(Style::new().fg(key_style))),
                    Span::styled(format!("{:<9} ", if r.set { "[set]" } else { "[default]" }), self.style(source)),
                    Span::styled(shown, self.style(value_style)),
                ]))
            })
            .collect();
        let list = List::new(items)
            .block(Block::default().borders(Borders::ALL).title(title))
            .highlight_symbol("> ")
            .highlight_style(self.style(Style::new().add_modifier(Modifier::REVERSED)));
        f.render_stateful_widget(list, list_area, &mut self.sel);
        let help = self.sel.selected().and_then(|i| self.settings.get(i)).map_or("", |r| r.setting.help);
        f.render_widget(Paragraph::new(help).wrap(Wrap { trim: true }).block(Block::default().borders(Borders::TOP)), help_area);
    }
}

/// Riskier levels stand out; the level is always written out too, so colour is only a hint.
/// What the open schedule window changes about `base`, e.g. `now auto, from window`; empty if nothing.
fn window_note(base: &crate::config::RepoSettings, now: &crate::config::RepoSettings) -> String {
    let mut changed = vec![];
    if now.level != base.level {
        changed.push(now.level.as_str().to_string());
    }
    if now.agent != base.agent {
        changed.push(now.agent.label().to_string());
    }
    if changed.is_empty() { String::new() } else { format!("now {}, from window", changed.join(", ")) }
}

fn level_style(level: &str) -> Style {
    match level {
        "auto" => Style::new().fg(Color::Red).add_modifier(Modifier::BOLD),
        "draft" => Style::new().fg(Color::Yellow),
        "suggest" => Style::new().fg(Color::Cyan),
        "observe" => Style::new().add_modifier(Modifier::DIM),
        "ERROR" => Style::new().fg(Color::Red).add_modifier(Modifier::BOLD | Modifier::REVERSED),
        _ => Style::new(),
    }
}

fn status_style(s: JobStatus) -> Style {
    match s {
        JobStatus::AwaitingApproval | JobStatus::AwaitingRunApproval => Style::new().fg(Color::Yellow).add_modifier(Modifier::BOLD),
        JobStatus::Failed => Style::new().fg(Color::Red),
        JobStatus::Ready => Style::new().fg(Color::Cyan),
        JobStatus::Done => Style::new().fg(Color::Green),
        JobStatus::Running | JobStatus::Approved => Style::new().fg(Color::Blue).add_modifier(Modifier::BOLD),
        JobStatus::Dismissed | JobStatus::Rejected => Style::new().add_modifier(Modifier::DIM),
        JobStatus::Queued => Style::new(),
    }
}

fn job_line(j: &Job) -> String {
    let when = jiff::Timestamp::from_second(j.updated_at).map(|t| t.strftime("%m-%d %H:%M").to_string()).unwrap_or_default();
    format!(
        "#{:<4} {:<20} {} {}#{:<5} {:<8} {}",
        j.id,
        j.status.label(),
        when,
        j.repo,
        j.number,
        j.level,
        j.summary.lines().next().unwrap_or_default()
    )
}

/// Full text of a job, used by the TUI detail view and `nightporter show`.
pub fn job_text(db: &Db, j: &Job) -> String {
    use std::fmt::Write;
    let mut s = String::new();
    let when = |t: i64| jiff::Timestamp::from_second(t).map(|t| t.strftime("%Y-%m-%d %H:%M").to_string()).unwrap_or_default();
    let _ = writeln!(
        s,
        "Job {} — {} — {}#{} ({}) — level {} — attempt {}",
        j.id,
        j.status.label(),
        j.repo,
        j.number,
        j.mode,
        j.level,
        j.attempt
    );
    let _ = writeln!(s, "Created {}, updated {}", when(j.created_at), when(j.updated_at));
    if !j.summary.is_empty() {
        let _ = writeln!(s, "\nSummary: {}", j.summary);
    }
    if !j.error.is_empty() {
        let _ = writeln!(s, "\nError: {}", j.error);
    }
    if let Some(c) = j.meta.cost_usd {
        let _ = writeln!(s, "Cost: ${c:.2}");
    }
    if !j.meta.pr_url.is_empty() {
        let _ = writeln!(s, "Draft PR: {}", j.meta.pr_url);
    }
    if j.status == JobStatus::AwaitingRunApproval {
        let _ = writeln!(s, "\nThis job runs code from someone you don't trust. Build files changed vs base:");
        for f in &j.meta.build_files {
            let _ = writeln!(s, "  {f}");
        }
        if j.meta.build_files.is_empty() {
            let _ = writeln!(s, "  (none)");
        }
        let _ = writeln!(s, "Worktree to inspect: {}\nPress a to allow env setup and tests to run.", j.worktree);
    }
    let _ = writeln!(s, "\nRequests:");
    for e in db.job_events(j.id).unwrap_or_default() {
        let _ = writeln!(s, "  [{}] @{} {:?}: {}", e.id, e.author, e.trigger, e.body.lines().next().unwrap_or_default());
    }
    if !j.checks.is_empty() {
        let _ = writeln!(s, "\nChecks:");
        for c in &j.checks {
            let mark = if c.ok {
                "[ok]  "
            } else if c.blocking {
                "[BLOCK]"
            } else {
                "[ASK] "
            };
            let _ = writeln!(s, "  {mark} {} {}", c.name, c.detail.replace('\n', "\n          "));
        }
    }
    if !j.replies.is_empty() {
        let _ = writeln!(s, "\nReplies to post:");
        if let Some(v) = j.meta.review {
            let _ = writeln!(s, "  (the first one as a review: {})", crate::state::enum_str(&v).replace('_', " ").to_uppercase());
        }
        for r in &j.replies {
            let _ = writeln!(s, "  → {}\n    {}", r.event.as_deref().unwrap_or("(PR comment)"), r.body.replace('\n', "\n    "));
        }
    }
    let cmd = crate::job::manual_commands(j);
    if j.status == JobStatus::Ready && !cmd.is_empty() {
        let _ = writeln!(s, "\nTo push yourself:\n  {cmd}");
    }
    if !j.diff.is_empty() {
        let _ = writeln!(s, "\nDiff:\n{}", j.diff);
    }
    let log = db.job_log(j.id).unwrap_or_default();
    if !log.is_empty() {
        let _ = writeln!(s, "\nLog:");
        for (ts, l) in log {
            let t = jiff::Timestamp::from_second(ts).map(|t| t.strftime("%H:%M:%S").to_string()).unwrap_or_default();
            let _ = writeln!(s, "  {t} {l}");
        }
    }
    s
}

/// Copy to the system clipboard via the platform tool.
fn copy(text: &str) -> Result<()> {
    use std::io::Write;
    let candidates: &[&[&str]] = if cfg!(target_os = "macos") {
        &[&["pbcopy"]]
    } else {
        &[&["wl-copy"], &["xclip", "-selection", "clipboard"], &["xsel", "-b", "-i"]]
    };
    for c in candidates {
        if let Ok(mut child) = std::process::Command::new(c[0]).args(&c[1..]).stdin(std::process::Stdio::piped()).spawn() {
            child.stdin.take().map(|mut i| i.write_all(text.as_bytes()));
            child.wait()?;
            return Ok(());
        }
    }
    anyhow::bail!("no clipboard tool found (pbcopy / wl-copy / xclip)")
}

const HELP: &str = "\
Night Porter watches your PRs/MRs and runs your coding agent on review feedback, CI failures
and merge conflicts, within your rules.

Tabs
  1 Queue    jobs that need you (NEEDS APPROVAL, ready to copy) and running jobs
  2 Inbox    comments from people you don't trust yet: allow once, trust, ignore or deny
  3 History  every job, newest first, including done and dismissed ones (/ searches)
  4 Repos    watched repos first; Enter shows everything about one (prompt, reply style, trust, hours)
  5 Settings every config.toml setting, its value and whether it's [set] or [default]
  6 Worktrees the job worktrees still on disk, and removed ones you can bring back for 7 days

Keys (everywhere): ↑/↓ or j/k move, PgUp/PgDn page, Enter open, Tab/←/→ switch tab, p pause, q quit
       /  search the Repos, Settings or History list (Esc clears)
       S  run the setup wizard again (forges, agent, clone folder, level, hours, repos, service)
       D  start or stop the background daemon (the part that watches your PRs; asks before stopping)
Repos: Enter details   space subscribe/unsubscribe   l next level (observe → suggest → draft → auto)
       a  switch agent (Claude Code → OpenCode → Codex → Gemini CLI, experimental)   e edit the repo's file   R rescan
       l, a set the repo's own value; (now auto, from window) on a row: a schedule window overrides it
Settings: Enter change (switches on/off and choices, otherwise type a value)   d back to default
       e  edit config.toml in $EDITOR. Every change is checked first: an invalid value or file is
          never saved, so a typo can't break the daemon.
Jobs:  a approve   A approve despite blocking checks (asks first)   r reject   t retry
       x dismiss: put it away without replying or pushing (it stays in History)
       u undo a dismiss (or restore the selected dismissed job)   X delete for good (asks first)
       c copy the push command (suggest level)   y copy replies
       h take over in herdr, Rex, tmux, Zellij or a terminal: the job's worktree with a briefed agent
       e edit the replies in $EDITOR before they're posted (checked again on save)
       m amend: after you change files in the job's worktree (by hand or in a take-over), the
          daemon commits them, signs and checks everything again, and asks you once more
Worktrees: x remove (asks first)   u bring back a KEPT one (its commits and uncommitted
       changes; ignored files like node_modules aren't kept)   h take over   Enter the job
Inbox: o allow once   a trust author on this repo   A trust everywhere   i ignore   u undo ignore   d deny

Status words are always shown as text: NEEDS APPROVAL, NEEDS RUN APPROVAL, FAILED, ready to copy.
Checks: [ok] passed, [ASK] needs your approval, [BLOCK] blocking (secrets, protected paths, history).
Set NO_COLOR=1 to turn colours off. Everything here is also available as CLI commands:
`nightporter status`, `show <id>`, `approve <id>`, `dismiss <id>`, `repo <name>`, `stop`, ...

Turning it off: D here, or `nightporter stop`. If it starts at login and you don't want that,
`nightporter service uninstall`. Your repos and config are never touched by stopping it.";

#[cfg(test)]
mod tests {
    use super::*;
    use crate::forge::{ForgeKind, RepoId, Target};
    use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
    use ratatui::backend::TestBackend;

    fn screen(app: &mut App) -> String {
        screen_at(app, 140, 30)
    }
    fn screen_at(app: &mut App, w: u16, h: u16) -> String {
        let mut t = Terminal::new(TestBackend::new(w, h)).unwrap();
        t.draw(|f| app.draw(f)).unwrap();
        t.backend()
            .buffer()
            .content()
            .iter()
            .map(ratatui::buffer::Cell::symbol)
            .collect::<Vec<_>>()
            .chunks(w as usize)
            .map(<[&str]>::concat)
            .collect::<Vec<_>>()
            .join("\n")
    }
    fn press(app: &mut App, c: KeyCode) -> bool {
        app.key(KeyEvent::new(c, KeyModifiers::NONE))
    }

    #[test]
    fn renders_every_view_with_text_labels() {
        let d = tempfile::tempdir().unwrap();
        let paths = Paths::at(&d.path().join("c"), &d.path().join("d"));
        paths.ensure().unwrap();
        let mut db = Db::open(&paths.db()).unwrap();
        let repo = RepoId::new(ForgeKind::GitHub, "github.com", "o/r");
        let id = db.create_job(&repo, Target::Pr, 4, "my_prs", "draft", &[]).unwrap();
        let mut j = db.job(id).unwrap().unwrap();
        j.status = JobStatus::AwaitingApproval;
        j.summary = "renamed foo".into();
        j.checks = vec![crate::state::CheckResult { name: "secrets".into(), ok: false, blocking: true, detail: "x".into() }];
        db.seed_job(&j).unwrap();
        // SAFETY: test-only env; no concurrent readers of NO_COLOR in this module.
        unsafe { std::env::set_var("NO_COLOR", "1") };
        let mut app = App::new(&paths, db);
        unsafe { std::env::remove_var("NO_COLOR") };
        assert!(!app.color);
        app.refresh(true);

        let s = screen(&mut app);
        assert!(s.contains("NEEDS APPROVAL") && s.contains("renamed foo") && s.contains("1 need you"), "{s}");
        assert!(s.contains("daemon NOT RUNNING"));
        assert!(s.contains("> "), "selection marker must not rely on colour");

        assert!(press(&mut app, KeyCode::Enter));
        let s = screen(&mut app);
        assert!(s.contains("[BLOCK] secrets") && s.contains("Esc back"), "{s}");
        // Plain approve refuses a blocking failure; the override asks first.
        press(&mut app, KeyCode::Char('a'));
        assert!(screen(&mut app).contains("blocking check failures"));
        press(&mut app, KeyCode::Char('A'));
        let small = screen_at(&mut app, 80, 24);
        assert!(small.contains("Override blocking checks") && small.contains("y/N"), "prompt must fit 80 columns: {small}");
        press(&mut app, KeyCode::Char('n'));
        assert_eq!(app.db.job(id).unwrap().unwrap().status, JobStatus::AwaitingApproval);
        press(&mut app, KeyCode::Esc);
        assert!(screen_at(&mut app, 80, 24).contains("q quit"), "footer must fit 80 columns");
        // e opens the replies in $EDITOR (via the main loop), m queues an amend.
        press(&mut app, KeyCode::Char('e'));
        assert!(matches!(app.suspend.take(), Some(Suspend::Replies(i)) if i == id) || app.msg.contains("no replies"), "{}", app.msg);
        // Reject asks first.
        press(&mut app, KeyCode::Char('r'));
        press(&mut app, KeyCode::Char('n'));
        assert_eq!(app.db.job(id).unwrap().unwrap().status, JobStatus::AwaitingApproval);

        for (k, want) in [('2', "No one is waiting"), ('3', "History"), ('4', "No clones found"), ('?', "Night Porter watches your PRs")] {
            press(&mut app, KeyCode::Char(k));
            let s = screen(&mut app);
            assert!(s.contains(want), "tab {k}: {s}");
        }
        // Repos tab: l cycles the level in the repo file, auto asks first; a switches agent.
        press(&mut app, KeyCode::Char('4'));
        let row = |level: &str| {
            vec![RepoRow { id: repo.clone(), subscribed: false, level: level.into(), agent: "Claude Code".into(), window: String::new() }]
        };
        app.repos = row("draft");
        let level = |p: &Paths| std::fs::read_to_string(p.repo_file(&repo)).unwrap_or_default();
        press(&mut app, KeyCode::Char('l'));
        assert!(screen(&mut app).contains("(auto)? y/N"));
        press(&mut app, KeyCode::Char('n'));
        assert!(!level(&paths).contains("auto"));
        app.repos = row("auto");
        press(&mut app, KeyCode::Char('l'));
        assert!(level(&paths).contains("level = \"observe\""), "{}", level(&paths));
        app.repos = row("observe");
        press(&mut app, KeyCode::Char('a'));
        assert!(level(&paths).contains("agent = \"opencode\""), "{}", level(&paths));
        press(&mut app, KeyCode::Char('S'));
        assert!(matches!(app.suspend, Some(Suspend::Setup)), "S asks the main loop to run the wizard");
        app.suspend = None;

        // Settings tab: values with their source, typed input validated before saving.
        press(&mut app, KeyCode::Char('5'));
        let s = screen(&mut app);
        assert!(s.contains("poll_interval") && s.contains("[default]") && s.contains("Folder where your clones live"), "{s}");
        app.sel.select(app.settings.iter().position(|r| r.setting.key == "poll_interval"));
        press(&mut app, KeyCode::Enter);
        press(&mut app, KeyCode::Backspace);
        press(&mut app, KeyCode::Backspace);
        for c in "soon".chars() {
            press(&mut app, KeyCode::Char(c));
        }
        press(&mut app, KeyCode::Enter);
        assert!(screen(&mut app).contains("not saved") && app.input.is_some(), "bad value is refused, text kept");
        press(&mut app, KeyCode::Esc);
        app.sel.select(app.settings.iter().position(|r| r.setting.key == "defaults.sandbox"));
        press(&mut app, KeyCode::Enter);
        assert!(screen(&mut app).contains("defaults.sandbox = false? y/N"), "turning the sandbox off asks first");
        press(&mut app, KeyCode::Char('n'));
        // `/` searches the list; Esc clears the search before it would quit.
        press(&mut app, KeyCode::Char('/'));
        for c in "sandbox".chars() {
            press(&mut app, KeyCode::Char(c));
        }
        press(&mut app, KeyCode::Enter);
        assert!(app.settings.iter().all(|r| r.setting.key.contains("sandbox")) && app.settings.len() == 3);
        assert!(screen(&mut app).contains("Settings matching \"sandbox\" (3 of"), "{}", screen(&mut app));
        assert!(screen(&mut app).contains("[x] on"), "bools show as checkboxes");
        assert!(press(&mut app, KeyCode::Esc), "Esc clears the search instead of quitting");
        assert_eq!(app.settings.len(), app.settings_all.len());
        std::fs::write(paths.config_file(), "nope = 1\n").unwrap();
        app.refresh(true);
        assert!(screen(&mut app).contains("Press e to fix it"));
        press(&mut app, KeyCode::Char('e'));
        assert!(matches!(app.suspend, Some(Suspend::Editor(_))));
        app.suspend = None;

        press(&mut app, KeyCode::Char('p'));
        assert!(screen(&mut app).contains("PAUSED"));
        assert!(!press(&mut app, KeyCode::Char('q')));
    }

    #[test]
    fn repos_tab_shows_the_repo_level_and_notes_the_window() {
        let cfg = crate::config::Config::parse(
            "[defaults]\nlevel = \"draft\"\n[[defaults.schedule]]\ndays = \"daily\"\nwindows = [\"00:00-24:00\"]\nlevel = \"auto\"",
        )
        .unwrap();
        let base = cfg.resolve(None).unwrap();
        let now = base.clone().at(&jiff::Zoned::now());
        assert_eq!(window_note(&base, &now), "now auto, from window");
        assert_eq!(window_note(&base, &base), "");
        // The row shows the repo's own level, so `l` visibly moves it (draft -> auto asks first).
        let d = tempfile::tempdir().unwrap();
        let paths = Paths::at(&d.path().join("c"), &d.path().join("d"));
        paths.ensure().unwrap();
        let mut app = App::new(&paths, Db::open(&paths.db()).unwrap());
        press(&mut app, KeyCode::Char('4'));
        let repo = RepoId::new(ForgeKind::GitHub, "github.com", "o/r");
        let window = window_note(&base, &now);
        app.repos = vec![RepoRow { id: repo, subscribed: true, level: base.level.as_str().into(), agent: "Claude Code".into(), window }];
        let s = screen(&mut app);
        assert!(s.contains("draft") && s.contains("(now auto, from window)"), "{s}");
        press(&mut app, KeyCode::Char('l'));
        assert!(screen(&mut app).contains("(auto)? y/N"));
    }

    #[test]
    fn worktrees_tab_removes_and_brings_back() {
        let d = tempfile::tempdir().unwrap();
        let paths = Paths::at(&d.path().join("c"), &d.path().join("d"));
        paths.ensure().unwrap();
        let clone = d.path().join("clone");
        std::fs::create_dir(&clone).unwrap();
        crate::git::tests::repo(&clone);
        let wt = d.path().join("wt/pr-4");
        let url = clone.to_string_lossy().to_string();
        crate::worktrees::block(async {
            crate::git::prepare_worktree(&clone, &wt, &url, "refs/heads/main", "nightporter/pr-4").await.map(|_| ())
        })
        .unwrap();
        std::fs::write(wt.join("mine.txt"), "handoff edit").unwrap();
        let mut db = Db::open(&paths.db()).unwrap();
        let repo = RepoId::new(ForgeKind::GitHub, "github.com", "o/r");
        let id = db.create_job(&repo, Target::Pr, 4, "my_prs", "draft", &[]).unwrap();
        let mut j = db.job(id).unwrap().unwrap();
        j.status = JobStatus::Done;
        j.worktree = wt.to_string_lossy().into();
        j.branch = "nightporter/pr-4".into();
        j.meta.clone = clone.to_string_lossy().into();
        db.seed_job(&j).unwrap();
        let mut app = App::new(&paths, db);
        let key = |c| KeyEvent::new(KeyCode::Char(c), KeyModifiers::NONE);
        app.key(key('6'));
        assert_eq!(app.worktrees.len(), 1);
        app.key(key('u'));
        assert!(app.msg.contains("KEPT"), "{}", app.msg);
        app.key(key('x'));
        assert!(app.confirm.is_some());
        app.key(key('y'));
        assert!(!wt.exists(), "{}", app.msg);
        assert!(app.worktrees[0].trashed.is_some() && app.worktrees[0].line().contains("KEPT"));
        app.key(key('u'));
        assert_eq!(std::fs::read_to_string(wt.join("mine.txt")).unwrap(), "handoff edit", "{}", app.msg);
        assert!(app.worktrees.len() == 1 && app.worktrees[0].trashed.is_none());
    }

    #[test]
    fn dismiss_undo_purge_repo_view_and_daemon_toggle() {
        let d = tempfile::tempdir().unwrap();
        let paths = Paths::at(&d.path().join("c"), &d.path().join("d"));
        paths.ensure().unwrap();
        let mut db = Db::open(&paths.db()).unwrap();
        let repo = RepoId::new(ForgeKind::GitHub, "github.com", "o/r");
        let id = db.create_job(&repo, Target::Pr, 4, "my_prs", "draft", &[]).unwrap();
        db.set_status(id, JobStatus::AwaitingApproval).unwrap();
        let mut app = App::new(&paths, db);
        // Dismiss: gone from the Queue, nothing posted; u brings it back; History keeps it.
        app.refresh(true);
        press(&mut app, KeyCode::Char('x'));
        assert_eq!(app.db.job(id).unwrap().unwrap().status, JobStatus::Dismissed);
        assert!(screen(&mut app).contains("Nothing needs you"), "{}", screen(&mut app));
        press(&mut app, KeyCode::Char('u'));
        assert_eq!(app.db.job(id).unwrap().unwrap().status, JobStatus::AwaitingApproval);
        press(&mut app, KeyCode::Char('x'));
        press(&mut app, KeyCode::Char('3'));
        press(&mut app, KeyCode::Char('/'));
        for c in "dismissed".chars() {
            press(&mut app, KeyCode::Char(c));
        }
        press(&mut app, KeyCode::Enter);
        assert!(screen(&mut app).contains("History matching \"dismissed\" (1 of 1)"), "{}", screen(&mut app));
        assert!(screen_at(&mut app, 80, 24).contains("q quit"), "History footer fits 80 columns");
        press(&mut app, KeyCode::Char('X'));
        press(&mut app, KeyCode::Char('y'));
        assert!(app.db.job(id).unwrap().is_none(), "X deletes for good after asking");
        press(&mut app, KeyCode::Esc);
        // Enter on a repo shows its settings.
        press(&mut app, KeyCode::Char('4'));
        app.repos =
            vec![RepoRow { id: repo.clone(), subscribed: true, level: "draft".into(), agent: "Codex".into(), window: String::new() }];
        press(&mut app, KeyCode::Enter);
        assert!(screen(&mut app).contains("WHAT IT DOES") || screen(&mut app).contains("error"), "{}", screen(&mut app));
        press(&mut app, KeyCode::Esc);
        assert!(app.repo_view.is_none());
        // D on a stopped daemon tries to start it, so it isn't pressed here; it asks before stopping.
        app.daemon_up = true;
        press(&mut app, KeyCode::Char('D'));
        assert!(screen(&mut app).contains("Stop Night Porter?"));
        press(&mut app, KeyCode::Char('n'));
    }

    fn home() -> (tempfile::TempDir, Paths) {
        let d = tempfile::tempdir().unwrap();
        let paths = Paths::at(&d.path().join("c"), &d.path().join("d"));
        paths.ensure().unwrap();
        (d, paths)
    }

    fn repo() -> RepoId {
        RepoId::new(ForgeKind::GitHub, "github.com", "o/r")
    }

    #[test]
    fn needs_setup_only_without_accounts() {
        let (_d, paths) = home();
        assert!(needs_setup(&paths), "no config yet");
        std::fs::write(paths.config_file(), "[[accounts]]\nforge = \"github\"\n").unwrap();
        assert!(!needs_setup(&paths));
        std::fs::write(paths.config_file(), "broken = [").unwrap();
        assert!(!needs_setup(&paths), "a broken config is shown in the TUI, not sent to the wizard");
    }

    #[test]
    fn job_text_shows_every_part_of_a_job() {
        let (_d, paths) = home();
        let mut db = Db::open(&paths.db()).unwrap();
        db.add_event(&crate::state::tests::ev("e1", 4), Disposition::Pending, "").unwrap();
        let id = db.create_job(&repo(), Target::Pr, 4, "my_prs", "draft", &["e1".into()]).unwrap();
        let mut j = db.job(id).unwrap().unwrap();
        j.status = JobStatus::AwaitingRunApproval;
        j.error = "agent crashed".into();
        j.meta.cost_usd = Some(1.234);
        j.meta.pr_url = "https://github.com/o/r/pull/9".into();
        j.worktree = "/tmp/wt".into();
        j.checks = vec![
            crate::state::CheckResult { name: "tests".into(), ok: true, blocking: false, detail: "pass".into() },
            crate::state::CheckResult { name: "size".into(), ok: false, blocking: false, detail: "big\nreally".into() },
        ];
        j.replies = vec![
            crate::forge::Reply { event: Some("e1".into()), body: "done\nline two".into() },
            crate::forge::Reply { event: None, body: "summary".into() },
        ];
        j.diff = "+added".into();
        db.seed_job(&j).unwrap();
        db.log(id, "started").unwrap();
        let t = job_text(&db, &j);
        for want in [
            "\nError: agent crashed",
            "Cost: $1.23",
            "Draft PR: https://github.com/o/r/pull/9",
            "Build files changed vs base:\n  (none)\nWorktree to inspect: /tmp/wt",
            "  [e1] @alice ReviewComment: fix",
            "  [ok]   tests pass",
            "  [ASK]  size big\n          really",
            "  → e1\n    done\n    line two",
            "  → (PR comment)\n    summary",
            "\nDiff:\n+added",
            "\nLog:\n  ",
        ] {
            assert!(t.contains(want), "missing {want:?} in:\n{t}");
        }
        assert!(t.trim_end().ends_with("started"));
        j.meta.build_files = vec!["build.rs".into()];
        assert!(job_text(&db, &j).contains("vs base:\n  build.rs\n"));

        j.status = JobStatus::Ready;
        j.head_before = "aaa".into();
        j.head_after = "bbb".into();
        j.meta.clone = "/src/o/r".into();
        j.meta.push_url = "git@github.com:o/r.git".into();
        j.meta.push_ref = "fix".into();
        let t = job_text(&db, &j);
        assert!(t.contains("To push yourself:\n  git -C ") && !t.contains("Worktree to inspect"), "{t}");
    }

    #[test]
    fn navigation_keys_move_the_selection_and_tabs() {
        let (_d, paths) = home();
        let mut db = Db::open(&paths.db()).unwrap();
        let ids: Vec<i64> = (1..=15)
            .map(|n| {
                let id = db.create_job(&repo(), Target::Pr, n, "my_prs", "draft", &[]).unwrap();
                db.set_status(id, JobStatus::AwaitingApproval).unwrap();
                id
            })
            .collect();
        let mut app = App::new(&paths, db);
        app.refresh(true);
        let at = |app: &App| app.sel.selected().unwrap();
        for (k, want) in [
            (KeyCode::Char('j'), 1),
            (KeyCode::Down, 2),
            (KeyCode::Char('k'), 1),
            (KeyCode::Up, 0),
            (KeyCode::Up, 0),
            (KeyCode::PageDown, 10),
            (KeyCode::PageDown, 14),
            (KeyCode::Down, 14),
            (KeyCode::PageUp, 4),
            (KeyCode::Char('G'), 14),
            (KeyCode::Char('g'), 0),
            (KeyCode::End, 14),
            (KeyCode::Home, 0),
        ] {
            press(&mut app, k);
            assert_eq!(at(&app), want, "after {k:?}");
        }
        assert_eq!(app.selected_job().map(|j| j.id), Some(ids[0]));
        // Tabs wrap around both ways.
        for (k, want) in
            [(KeyCode::Tab, Tab::Inbox), (KeyCode::Right, Tab::History), (KeyCode::BackTab, Tab::Inbox), (KeyCode::Left, Tab::Queue)]
        {
            press(&mut app, k);
            assert_eq!(app.tab, want, "after {k:?}");
        }
        press(&mut app, KeyCode::Left);
        assert_eq!(app.tab, Tab::Help);
        assert_eq!(app.len(), 0);
        press(&mut app, KeyCode::Down);
        press(&mut app, KeyCode::Char('j'));
        press(&mut app, KeyCode::Char('k'));
        assert_eq!(app.scroll, 1, "j/k scroll the help text");
        press(&mut app, KeyCode::Right);
        assert_eq!((app.tab, app.scroll), (Tab::Queue, 0), "switching tabs resets the scroll");
        // Detail view: scroll within the text, Backspace goes back.
        press(&mut app, KeyCode::Enter);
        assert_eq!(app.detail, Some(ids[0]));
        press(&mut app, KeyCode::PageDown);
        press(&mut app, KeyCode::Char(' '));
        assert_eq!(app.scroll, app.max_scroll());
        press(&mut app, KeyCode::PageUp);
        press(&mut app, KeyCode::Char('j'));
        press(&mut app, KeyCode::Char('k'));
        assert_eq!(app.scroll, 0);
        press(&mut app, KeyCode::Backspace);
        assert!(app.detail.is_none());
        assert!(!app.key(KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL)), "Ctrl-C quits");
    }

    #[test]
    fn inbox_keys_decide_and_undo() {
        let (_d, paths) = home();
        let db = Db::open(&paths.db()).unwrap();
        for (id, author) in [("e1", "alice"), ("e2", "bob"), ("e3", "carol"), ("e4", "dave")] {
            let mut e = crate::state::tests::ev(id, 5);
            e.author = author.into();
            db.add_event(&e, Disposition::Inbox, "").unwrap();
        }
        let mut app = App::new(&paths, db);
        app.refresh(true);
        press(&mut app, KeyCode::Char('2'));
        assert!(screen(&mut app).contains("@alice"), "{}", screen(&mut app));
        press(&mut app, KeyCode::Char('u'));
        assert!(app.msg.starts_with("nothing to undo"));
        let first = |app: &App| app.inbox[0].id.clone();
        let e = first(&app);
        press(&mut app, KeyCode::Char('i'));
        assert_eq!(app.msg, "ignored (u undoes)");
        assert_eq!(app.inbox.len(), 3);
        press(&mut app, KeyCode::Char('u'));
        assert_eq!((app.msg.as_str(), app.inbox.len()), ("back in the inbox", 4));
        assert!(app.inbox.iter().any(|x| x.id == e));

        press(&mut app, KeyCode::Char('o'));
        assert_eq!((app.msg.as_str(), app.inbox.len()), ("allowed once", 3));
        let who = app.inbox[0].author.clone();
        press(&mut app, KeyCode::Char('a'));
        assert_eq!(app.msg, format!("@{who} trusted on o/r (1 released)"));
        let who = app.inbox[0].author.clone();
        press(&mut app, KeyCode::Char('A'));
        assert!(screen(&mut app).contains(&format!("Trust @{who} on ALL repos? y/N")));
        press(&mut app, KeyCode::Char('y'));
        assert_eq!(app.msg, format!("@{who} trusted everywhere (1 released)"));
        let who = app.inbox[0].author.clone();
        press(&mut app, KeyCode::Char('d'));
        press(&mut app, KeyCode::Char('y'));
        assert_eq!(app.msg, format!("@{who} denied on o/r (1 dropped)"));
        assert_eq!(app.inbox.len(), 0);
        assert!(screen(&mut app).contains("No one is waiting"));
        press(&mut app, KeyCode::Char('o'));
        assert_eq!(app.msg, format!("@{who} denied on o/r (1 dropped)"), "keys on an empty inbox do nothing");
        let cfg = Config::load(&paths).unwrap();
        let trust = cfg.settings_for(&paths, &repo()).unwrap().trust;
        assert_eq!(trust.allow_users.len(), 2, "one on the repo, one global: {:?}", trust.allow_users);
        assert_eq!(trust.deny_users.len(), 1);
    }

    #[test]
    fn repos_tab_lists_scanned_clones_and_toggles_subscription() {
        let (d, paths) = home();
        let code = d.path().join("code");
        let g = code.join("o/r/.git");
        std::fs::create_dir_all(&g).unwrap();
        std::fs::write(g.join("config"), "[remote \"origin\"]\n\turl = git@github.com:o/r.git\n").unwrap();
        std::fs::write(paths.config_file(), format!("code_dir = {:?}\n[[accounts]]\nforge = \"github\"\n", code.display().to_string()))
            .unwrap();
        let mut app = App::new(&paths, Db::open(&paths.db()).unwrap());
        app.refresh(true);
        press(&mut app, KeyCode::Char('4'));
        assert_eq!(app.repos.len(), 1);
        assert!(!app.repos[0].subscribed);
        press(&mut app, KeyCode::Char(' '));
        assert_eq!(app.msg, "subscribed to o/r");
        assert!(app.repos[0].subscribed, "the row reloads from the repo file");
        press(&mut app, KeyCode::Char('s'));
        assert_eq!(app.msg, "unsubscribed from o/r");
        press(&mut app, KeyCode::Char('R'));
        assert_eq!(app.msg, "rescanned");
        press(&mut app, KeyCode::Char('e'));
        assert!(matches!(&app.suspend, Some(Suspend::Editor(p)) if *p == paths.repo_file(&repo())));
        app.suspend = None;
        // Search: Backspace edits it, Esc drops it.
        press(&mut app, KeyCode::Char('/'));
        for c in "zz".chars() {
            press(&mut app, KeyCode::Char(c));
        }
        assert!(app.repos.is_empty());
        assert!(screen(&mut app).contains("Nothing matches the search"), "{}", screen(&mut app));
        press(&mut app, KeyCode::Backspace);
        press(&mut app, KeyCode::Backspace);
        assert_eq!(app.repos.len(), 1);
        press(&mut app, KeyCode::Char('x'));
        assert!(screen(&mut app).contains("search: x_"));
        press(&mut app, KeyCode::Esc);
        assert!(!app.filtering && app.filter.is_empty() && app.repos.len() == 1);
        // Repo view: e edits the repo file too.
        press(&mut app, KeyCode::Enter);
        assert!(app.repo_view.is_some());
        press(&mut app, KeyCode::Char('e'));
        assert!(matches!(app.suspend, Some(Suspend::Editor(_))));
        press(&mut app, KeyCode::Char('q'));
        assert!(app.repo_view.is_none());
        // A repo file that doesn't parse shows as an error row.
        std::fs::create_dir_all(paths.repo_file(&repo()).parent().unwrap()).unwrap();
        std::fs::write(paths.repo_file(&repo()), "level = \"sometimes\"\n").unwrap();
        app.refresh(true);
        assert_eq!(app.repos[0].level, "ERROR");
    }

    #[test]
    fn settings_keys_reset_and_save_typed_values() {
        let (_d, paths) = home();
        let mut app = App::new(&paths, Db::open(&paths.db()).unwrap());
        app.refresh(true);
        press(&mut app, KeyCode::Char('5'));
        app.sel.select(app.settings.iter().position(|r| r.setting.key == "poll_interval"));
        press(&mut app, KeyCode::Enter);
        assert!(app.input.is_some());
        let n = app.input.as_ref().unwrap().len();
        for _ in 0..n {
            press(&mut app, KeyCode::Backspace);
        }
        for c in "5m".chars() {
            press(&mut app, KeyCode::Char(c));
        }
        press(&mut app, KeyCode::Tab);
        press(&mut app, KeyCode::Enter);
        assert!(app.input.is_none(), "{}", app.msg);
        assert!(std::fs::read_to_string(paths.config_file()).unwrap().contains("poll_interval = \"5m\""));
        press(&mut app, KeyCode::Char('d'));
        assert!(!std::fs::read_to_string(paths.config_file()).unwrap().contains("poll_interval"), "d resets to the default");
        press(&mut app, KeyCode::Enter);
        press(&mut app, KeyCode::Esc);
        assert!(app.input.is_none() && app.msg == "cancelled");
        press(&mut app, KeyCode::Char('z'));
        assert_eq!(app.msg, "cancelled", "unbound keys do nothing");
    }

    #[test]
    fn colours_and_the_update_banner_render() {
        let (_d, paths) = home();
        let mut db = Db::open(&paths.db()).unwrap();
        for s in [JobStatus::Failed, JobStatus::Ready, JobStatus::Running, JobStatus::Queued, JobStatus::Done] {
            let id = db.create_job(&repo(), Target::Pr, 1, "my_prs", "draft", &[]).unwrap();
            db.set_status(id, s).unwrap();
        }
        let mut app = App::new(&paths, db);
        app.color = true;
        app.update = Some("Night Porter 9.9.9 is out".into());
        app.refresh(true);
        let s = screen(&mut app);
        assert!(s.contains("update available") && s.contains("FAILED") && s.contains("ready to copy"), "{s}");
        press(&mut app, KeyCode::Char('3'));
        let s = screen(&mut app);
        assert!(s.contains("  done ") && s.contains("  queued ") && s.contains("  running "), "{s}");
        assert_eq!(status_style(JobStatus::Failed).fg, Some(Color::Red));
        assert_eq!(status_style(JobStatus::Done).fg, Some(Color::Green));
        // Retry on a failed job re-queues its events.
        app.sel.select(app.history.iter().position(|j| j.status == JobStatus::Failed));
        press(&mut app, KeyCode::Char('t'));
        assert_eq!(app.msg, "re-queued 0 event(s)");
    }
}