prlens 0.1.0

One queue for all your PRs — aggregates GitHub and Bitbucket review requests into a single interactive view
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
//! Interactive TUI for the prlens PR review queue.
//!
//! Shows a full-width ratatui table with j/k (or arrow key) navigation.
//! Pressing Enter opens the selected PR in the browser via `open::that`.
//! Press q or Esc to quit.

use anyhow::Context;
use crossterm::{
    event::{self, Event, KeyCode, KeyEventKind},
    execute,
    terminal::{EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode, enable_raw_mode},
};
use ratatui::{
    Terminal,
    backend::CrosstermBackend,
    layout::{Alignment, Constraint, Direction, Flex, Layout, Rect},
    style::{Color, Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Borders, Cell, Paragraph, Row, Table, TableState, Wrap},
};
// Wave 2 imports — added now to avoid re-touching imports when Wave 2 uses them
#[allow(unused_imports)]
use ratatui::layout::Position;
#[allow(unused_imports)]
use ratatui::widgets::Clear;
use std::sync::mpsc;
use std::time::{Duration, Instant};

use crate::models::{CiStatus, PullRequest, ReviewStatus};

/// Run the interactive PR table TUI, showing a loading spinner until `rx` delivers data.
///
/// Enters the alternate screen immediately so there is no gap between launch and display.
/// Restores terminal state on exit even if an error occurs inside the loop.
///
/// In non-TTY environments (tests, piped output) the function returns immediately after
/// consuming the channel value so callers do not block.
pub fn run_interactive(
    rx: mpsc::Receiver<(Vec<PullRequest>, Vec<String>)>,
    config: &crate::config::Config,
    initial_profile: Option<usize>,
) -> anyhow::Result<()> {
    use std::io::IsTerminal;
    if !std::io::stdout().is_terminal() {
        drop(rx.recv());
        return Ok(());
    }

    // --- Setup terminal ---
    enable_raw_mode().context("failed to enable raw mode")?;
    let mut stdout = std::io::stdout();
    execute!(stdout, EnterAlternateScreen).context("failed to enter alternate screen")?;
    let backend = CrosstermBackend::new(stdout);
    let mut terminal = Terminal::new(backend).context("failed to create terminal")?;

    // Run the loop; always restore terminal even on error.
    let result = run_tui_loop(&mut terminal, rx, config, initial_profile);

    // --- Restore terminal ---
    disable_raw_mode().context("failed to disable raw mode")?;
    execute!(terminal.backend_mut(), LeaveAlternateScreen)
        .context("failed to leave alternate screen")?;
    terminal.show_cursor().context("failed to show cursor")?;

    result
}

/// Mode gate for the TUI event dispatch. Tracks whether user is navigating, typing a search
/// query, or viewing the detail overlay for the selected PR.
#[derive(Default, PartialEq)]
enum AppMode {
    #[default]
    Normal,
    Searching,
    ShowingDetail,
}

/// Insert a character at the current cursor position (character-index, not byte-index).
/// Byte-safe: handles multi-byte UTF-8 characters (e.g., accented letters, CJK).
fn enter_char(s: &mut String, cursor: &mut usize, c: char) {
    let byte_pos = s.char_indices().nth(*cursor).map(|(i, _)| i).unwrap_or(s.len());
    s.insert(byte_pos, c);
    *cursor = (*cursor + 1).min(s.chars().count());
}

/// Delete the character immediately before the cursor.
/// If cursor is at position 0, this is a no-op.
fn delete_char(s: &mut String, cursor: &mut usize) {
    if *cursor == 0 {
        return;
    }
    let before: String = s.chars().take(*cursor - 1).collect();
    let after: String = s.chars().skip(*cursor).collect();
    *s = before + &after;
    *cursor = cursor.saturating_sub(1);
}

/// Compute a centered popup area within `area` using ratatui's Flex::Center layout.
/// Source: ratatui 0.29.0 examples/popup.rs
#[allow(dead_code)] // used in Wave 2 (TUI-05 detail overlay)
fn popup_area(area: Rect, percent_x: u16, percent_y: u16) -> Rect {
    let vertical = Layout::vertical([Constraint::Percentage(percent_y)]).flex(Flex::Center);
    let horizontal = Layout::horizontal([Constraint::Percentage(percent_x)]).flex(Flex::Center);
    let [area] = vertical.areas(area);
    let [area] = horizontal.areas(area);
    area
}

/// Build the detail overlay Paragraph widget for a selected PR.
/// Shows all available PullRequest fields in a bordered, centered popup.
fn build_detail_block<'a>(
    pr: &'a crate::models::PullRequest,
    jira: &crate::config::JiraConfig,
    panel_blue: Color,
    hint_color: Color,
) -> Paragraph<'a> {
    let jira_enabled = jira.base_url.is_some();
    // Colour palette for the detail view
    let lbl  = Style::default().fg(panel_blue);                              // "Title:", "Author:", …
    let val  = Style::default().fg(Color::Rgb(205, 214, 244));               // value text (Catppuccin text)
    let add_color = Color::Rgb(166, 227, 161);                               // green — additions
    let del_color = Color::Rgb(243, 139, 168);                               // red  — deletions
    let url_style = Style::default()
        .fg(Color::Rgb(116, 199, 236))
        .add_modifier(Modifier::UNDERLINED);

    let ci_color = match &pr.ci_status {
        Some(CiStatus::Success)                           => Color::Rgb(166, 227, 161),
        Some(CiStatus::Failed)                            => Color::Rgb(243, 139, 168),
        Some(CiStatus::Pending) | Some(CiStatus::Running) => Color::Rgb(249, 226, 175),
        None | Some(CiStatus::Cancelled)                  => Color::Rgb(88, 91, 112),
    };
    let review_color = match &pr.review_status {
        ReviewStatus::NeedsReview      => Color::Rgb(250, 179, 135),
        ReviewStatus::Approved         => Color::Rgb(166, 227, 161),
        ReviewStatus::ChangesRequested => Color::Rgb(243, 139, 168),
        ReviewStatus::Mixed            => Color::Rgb(249, 226, 175),
        ReviewStatus::InReview         => Color::Rgb(137, 220, 235),
    };

    let provider_icon = match pr.provider.as_str() {
        "github"    => "\u{E709} github",
        "bitbucket" => "\u{E703} bitbucket",
        other       => other,
    };

    let labels_joined = if pr.labels.is_empty() {
        "(none)".to_string()
    } else {
        pr.labels.join(", ")
    };
    let additions = pr.additions.map(|n| n.to_string()).unwrap_or_else(|| "?".to_string());
    let deletions = pr.deletions.map(|n| n.to_string()).unwrap_or_else(|| "?".to_string());

    let mut lines: Vec<Line<'a>> = vec![
        Line::from(vec![
            Span::styled("Title:    ", lbl),
            Span::styled(pr.title.clone(), val.add_modifier(Modifier::BOLD)),
        ]),
        Line::from(vec![
            Span::styled("Author:   ", lbl),
            Span::styled(pr.author.login.clone(), val),
        ]),
        Line::from(vec![
            Span::styled("URL:      ", lbl),
            Span::styled(pr.url.clone(), url_style),
        ]),
        Line::from(vec![
            Span::styled("Branch:   ", lbl),
            Span::styled(format!("{} \u{2192} {}", pr.head_branch, pr.base_branch), val),
        ]),
        Line::from(vec![
            Span::styled("Labels:   ", lbl),
            Span::styled(labels_joined, val),
        ]),
        Line::from(vec![
            Span::styled("Comments: ", lbl),
            Span::styled(pr.comment_count.to_string(), val),
        ]),
        Line::from(vec![
            Span::styled("Changes:  ", lbl),
            Span::styled(format!("+{additions}"), Style::default().fg(add_color)),
            Span::styled(" / ", val),
            Span::styled(format!("-{deletions}"), Style::default().fg(del_color)),
        ]),
        Line::from(vec![
            Span::styled("Review:   ", lbl),
            Span::styled(format_review_status(&pr.review_status), Style::default().fg(review_color)),
        ]),
        Line::from(vec![
            Span::styled("CI:       ", lbl),
            Span::styled(
                format!("{} {}", format_ci_status(&pr.ci_status),
                    match &pr.ci_status {
                        Some(CiStatus::Success)  => "passing",
                        Some(CiStatus::Failed)   => "failed",
                        Some(CiStatus::Pending)  => "pending",
                        Some(CiStatus::Running)  => "running",
                        Some(CiStatus::Cancelled) | None => "",
                    }
                ),
                Style::default().fg(ci_color),
            ),
        ]),
        Line::from(vec![
            Span::styled("Age:      ", lbl),
            Span::styled(format_age(&pr.updated_at), val),
        ]),
        Line::from(vec![
            Span::styled("Provider: ", lbl),
            Span::styled(provider_icon, val),
        ]),
    ];

    if jira_enabled {
        if let Some(key) = extract_jira_key(&pr.title) {
            lines.push(Line::from(vec![
                Span::styled("Jira:     ", lbl),
                Span::styled(format!("{key}  (J to open)"), val),
            ]));
        }
    }

    let block = Block::default()
        .borders(Borders::ALL)
        .border_style(Style::default().fg(panel_blue))
        .title(Line::from(Span::styled(
            " PR Detail ",
            Style::default().fg(panel_blue).add_modifier(Modifier::BOLD),
        )))
        .title_bottom(Line::from(Span::styled(
            " Esc / Space to close ",
            Style::default().fg(hint_color),
        )));

    Paragraph::new(lines)
        .block(block)
        .wrap(Wrap { trim: false })
}

/// Extract the first Jira-style ticket key (e.g. "PROJ-123") from a PR title string.
#[allow(dead_code)] // used in Wave 2 (TUI-08/JIRA-01 Jira URL opening)
/// Pattern: one or more ASCII uppercase letters, followed by a hyphen, followed by one or more
/// ASCII digits. Returns the first match found, or None.
fn extract_jira_key(title: &str) -> Option<String> {
    let chars: Vec<char> = title.chars().collect();
    let mut i = 0;
    while i < chars.len() {
        if chars[i].is_ascii_uppercase() {
            let key_start = i;
            // Consume uppercase letters (project key prefix)
            while i < chars.len() && chars[i].is_ascii_uppercase() {
                i += 1;
            }
            let key_len = i - key_start;
            // Must be followed by a hyphen and at least one digit
            if key_len >= 1 && i < chars.len() && chars[i] == '-' {
                i += 1; // skip hyphen
                let num_start = i;
                while i < chars.len() && chars[i].is_ascii_digit() {
                    i += 1;
                }
                if i > num_start {
                    return Some(chars[key_start..i].iter().collect());
                }
            }
        } else {
            i += 1;
        }
    }
    None
}

/// Compute the indices of rows to display after applying draft, search, and profile filters.
/// Returns indices into `rows_data` / `is_draft` for rows that should be rendered.
///
/// `profiles_filter`: when `Some(&profile)`, additionally filters rows to those whose
/// repo (cells[4]) or status (cells[5]) match the profile's constraints.
fn compute_displayed_indices(
    rows_data: &[[String; 9]],
    repo_full_names: &[String],
    pr_providers: &[String],
    is_draft: &[bool],
    show_drafts: bool,
    search_query: &str,
    profiles_filter: Option<&crate::config::ProfileConfig>,
) -> Vec<usize> {
    rows_data
        .iter()
        .enumerate()
        .filter(|(idx, cells)| {
            if !show_drafts && *idx < is_draft.len() && is_draft[*idx] {
                return false;
            }
            // Profile filter: PR must match at least one filter group (OR between groups).
            // Within each group all set fields must match (AND).
            if let Some(profile) = profiles_filter {
                if !profile.filters.is_empty() {
                let full = repo_full_names.get(*idx).map(|s| s.to_lowercase()).unwrap_or_default();
                let prov = pr_providers.get(*idx).map(|s| s.to_lowercase()).unwrap_or_default();
                let status_cell = cells[5].to_lowercase();
                let matches_any = profile.filters.iter().any(|fg| {
                    if let Some(p) = &fg.provider {
                        if !p.is_empty() && prov != p.to_lowercase() { return false; }
                    }
                    if let Some(o) = &fg.org {
                        if !o.is_empty() {
                            let org_part = full.split('/').next().unwrap_or("");
                            if !org_part.contains(&o.to_lowercase()) { return false; }
                        }
                    }
                    if let Some(r) = &fg.repo {
                        if !r.is_empty() && !full.contains(&r.to_lowercase()) { return false; }
                    }
                    if let Some(s) = &fg.status {
                        if !s.is_empty() && !status_cell.contains(&s.to_lowercase()) { return false; }
                    }
                    true
                });
                if !matches_any {
                    return false;
                }
                }
            }
            if search_query.is_empty() {
                return true;
            }
            let q = search_query.to_lowercase();
            cells[2].to_lowercase().contains(&q)  // title
                || cells[3].to_lowercase().contains(&q) // author
                || cells[4].to_lowercase().contains(&q) // repo (short name for search)
        })
        .map(|(idx, _)| idx)
        .collect()
}

fn run_tui_loop(
    terminal: &mut Terminal<CrosstermBackend<std::io::Stdout>>,
    rx: mpsc::Receiver<(Vec<PullRequest>, Vec<String>)>,
    config: &crate::config::Config,
    initial_profile: Option<usize>,
) -> anyhow::Result<()> {
    // Catppuccin Mocha palette — defined once, reused across both loading and ready states.
    let header_style = Style::default()
        .add_modifier(Modifier::BOLD)
        .fg(Color::Rgb(203, 166, 247)); // Mocha Mauve
    let selected_style = Style::default()
        .bg(Color::Rgb(203, 166, 247))
        .fg(Color::Rgb(30, 30, 46))
        .add_modifier(Modifier::BOLD);
    let ci_color_success = Color::Rgb(166, 227, 161);
    let ci_color_failed  = Color::Rgb(243, 139, 168);
    let ci_color_pending = Color::Rgb(249, 226, 175);
    let ci_color_none    = Color::Rgb(88, 91, 112);
    let status_color_needs_review   = Color::Rgb(250, 179, 135);
    let status_color_approved       = Color::Rgb(166, 227, 161);
    let status_color_changes        = Color::Rgb(243, 139, 168);
    let status_color_mixed          = Color::Rgb(249, 226, 175);
    let status_color_in_review      = Color::Rgb(137, 220, 235);
    let draft_icon_color = Color::Rgb(116, 199, 236);
    let panel_blue       = Color::Rgb(137, 180, 250);
    let hint_color       = Color::Rgb(116, 199, 236);

    // Loading state
    const SPINNER: [&str; 10] = ["", "", "", "", "", "", "", "", "", ""];
    let mut spinner_tick: usize = 0;
    let mut last_tick = Instant::now();

    let error_color = Color::Rgb(243, 139, 168); // Catppuccin Maroon (matches changes-requested)

    // Ready state — populated once the channel delivers data.
    let mut prs: Option<Vec<PullRequest>>     = None;
    let mut provider_errors: Vec<String>      = Vec::new();
    let mut rows_data: Vec<[String; 9]>       = Vec::new();
    let mut repo_full_names: Vec<String>      = Vec::new();
    let mut pr_providers: Vec<String>         = Vec::new();
    let mut ci_styles: Vec<Style>             = Vec::new();
    let mut status_styles: Vec<Style>         = Vec::new();
    let mut is_draft: Vec<bool>               = Vec::new();
    let mut table_state = TableState::default();

    // AppMode state machine — gates key event dispatch.
    let mut mode = AppMode::Normal;
    let mut search_query = String::new();
    let mut cursor_pos: usize = 0;
    let mut show_drafts: bool = false;
    // Profile switching state — None = no filter (all PRs); Some(i) = active profile index.
    let mut active_profile_idx: Option<usize> = initial_profile;

    // Marquee / scroll state for the selected row's title column.
    let mut scroll_offset: usize = 0;
    let mut last_scroll_tick = Instant::now();
    const SCROLL_INTERVAL_MS: u64 = 200; // advance one char every 200 ms → ~5 chars/sec
    let mut last_selected: Option<usize> = None;

    // Filtered navigation state — updated by the draw closure each frame and consumed
    // by event handlers in the same iteration. Allows Enter/Space to resolve the correct
    // raw PR index even though displayed_indices is only computed inside the draw closure.
    let mut last_displayed_len: usize = 0;
    let mut displayed_snapshot: Vec<usize> = Vec::new();

    loop {
        // --- Poll channel while still loading ---
        if prs.is_none() {
            match rx.try_recv() {
                Ok((loaded, errors)) => {
                    provider_errors = errors;
                    // Pre-compute display data once.
                    for pr in &loaded {
                        let src = match pr.provider.as_str() {
                            "github"    => "\u{E709}".to_string(),
                            "bitbucket" => "\u{E703}".to_string(),
                            other       => other.to_string(),
                        };
                        rows_data.push([
                            src,
                            pr.number.to_string(),
                            pr.title.clone(),
                            pr.author.login.clone(),
                            pr.repo_full_name.split('/').last()
                                .unwrap_or(&pr.repo_full_name).to_string(),
                            format_review_status(&pr.review_status).to_string(),
                            format_ci_status(&pr.ci_status).to_string(),
                            pr.reviewers.len().to_string(),
                            format_age(&pr.updated_at),
                        ]);
                        repo_full_names.push(pr.repo_full_name.clone());
                        pr_providers.push(pr.provider.clone());
                        is_draft.push(pr.draft);
                        ci_styles.push(Style::default().fg(match &pr.ci_status {
                            Some(CiStatus::Success)                            => ci_color_success,
                            Some(CiStatus::Failed)                             => ci_color_failed,
                            Some(CiStatus::Pending) | Some(CiStatus::Running)  => ci_color_pending,
                            None | Some(CiStatus::Cancelled)                   => ci_color_none,
                        }));
                        status_styles.push(Style::default().fg(match &pr.review_status {
                            ReviewStatus::NeedsReview      => status_color_needs_review,
                            ReviewStatus::Approved         => status_color_approved,
                            ReviewStatus::ChangesRequested => status_color_changes,
                            ReviewStatus::Mixed            => status_color_mixed,
                            ReviewStatus::InReview         => status_color_in_review,
                        }));
                    }
                    if !loaded.is_empty() {
                        table_state.select(Some(0));
                    }
                    prs = Some(loaded);
                }
                Err(mpsc::TryRecvError::Empty) => {
                    // Advance spinner at ~100 ms.
                    if last_tick.elapsed() >= Duration::from_millis(100) {
                        spinner_tick = (spinner_tick + 1) % SPINNER.len();
                        last_tick = Instant::now();
                    }
                }
                Err(mpsc::TryRecvError::Disconnected) => {
                    return Err(anyhow::anyhow!("Provider fetch task ended unexpectedly"));
                }
            }
        }

        // --- Advance marquee offset ---
        let current_sel = table_state.selected();
        if current_sel != last_selected {
            scroll_offset = 0;
            last_selected = current_sel;
        }
        if prs.is_some() && last_scroll_tick.elapsed() >= Duration::from_millis(SCROLL_INTERVAL_MS) {
            scroll_offset = scroll_offset.wrapping_add(1);
            last_scroll_tick = Instant::now();
        }

        // --- Draw ---
        terminal.draw(|frame| {
            let area = frame.area();
            match &prs {
                // Loading screen
                None => {
                    let block = Block::default()
                        .borders(Borders::ALL)
                        .border_style(Style::default().fg(panel_blue))
                        .title(Line::from(Span::styled(
                            " prlens ",
                            Style::default().fg(panel_blue).add_modifier(Modifier::BOLD),
                        )))
                        .title_bottom(Line::from(Span::styled(
                            " q to cancel ",
                            Style::default().fg(hint_color),
                        )));
                    let inner = block.inner(area);
                    frame.render_widget(block, area);

                    let [_, mid, _] = Layout::default()
                        .direction(Direction::Vertical)
                        .constraints([Constraint::Fill(1), Constraint::Length(1), Constraint::Fill(1)])
                        .areas(inner);

                    frame.render_widget(
                        Paragraph::new(Line::from(vec![
                            Span::styled(
                                SPINNER[spinner_tick],
                                Style::default().fg(panel_blue).add_modifier(Modifier::BOLD),
                            ),
                            Span::raw("  Fetching pull requests…"),
                        ]))
                        .alignment(Alignment::Center),
                        mid,
                    );
                }

                // Empty result
                Some(loaded) if loaded.is_empty() => {
                    let block = Block::default()
                        .borders(Borders::ALL)
                        .border_style(Style::default().fg(panel_blue))
                        .title(Line::from(Span::styled(
                            " prlens — 0 PRs waiting ",
                            Style::default().fg(panel_blue).add_modifier(Modifier::BOLD),
                        )))
                        .title_bottom(Line::from(Span::styled(
                            " q to exit ",
                            Style::default().fg(hint_color),
                        )));
                    let inner = block.inner(area);
                    frame.render_widget(block, area);

                    let [_, mid, _] = Layout::default()
                        .direction(Direction::Vertical)
                        .constraints([Constraint::Fill(1), Constraint::Length(1), Constraint::Fill(1)])
                        .areas(inner);

                    frame.render_widget(
                        Paragraph::new("No pull requests found.").alignment(Alignment::Center),
                        mid,
                    );
                }

                // PR table
                Some(_) => {
                    // Split area: table on top, error bar at bottom (wraps on narrow terminals).
                    let (table_area, error_area) = if provider_errors.is_empty() {
                        (area, None)
                    } else {
                        // Compute how many terminal lines each error occupies when wrapped.
                        let usable_width = area.width.saturating_sub(3).max(1) as usize; // " ⚠ " prefix = 3 chars
                        let total_lines: u16 = provider_errors.iter()
                            .map(|e| ((e.chars().count() + usable_width - 1) / usable_width).max(1) as u16)
                            .sum();
                        let [top, bot] = Layout::default()
                            .direction(Direction::Vertical)
                            .constraints([Constraint::Fill(1), Constraint::Length(total_lines)])
                            .areas(area);
                        (top, Some(bot))
                    };
                    let area = table_area;

                    // Split off search bar area at bottom when in Searching mode.
                    let (table_area, search_bar_area) = if mode == AppMode::Searching {
                        let [top, bot] = Layout::vertical([
                            Constraint::Fill(1),
                            Constraint::Length(3),
                        ]).areas(area);
                        (top, Some(bot))
                    } else {
                        (area, None)
                    };
                    let area = table_area;

                    let widths = [
                        Constraint::Length(3),   // Src
                        Constraint::Length(6),   // #
                        Constraint::Fill(3),     // Title — primary real estate
                        Constraint::Fill(1),     // Author
                        Constraint::Fill(1),     // Repo
                        Constraint::Min(5),      // Status
                        Constraint::Length(3),   // CI
                        Constraint::Length(4),   // Rev.
                        Constraint::Length(5),   // Age
                    ];

                    let header = Row::new(["Src", "#", "Title", "Author", "Repo", "Status", "CI", "Rev.", "Age"])
                        .style(header_style);

                    // Compute exact title column width using the same layout ratatui applies.
                    // Table block has Borders::ALL → inner width = area.width - 2.
                    // Default column_spacing=1 → interleave Length(1) spacers between the 9 columns.
                    // Title is column index 2 → interleaved position 4.
                    let title_col_width = {
                        let inner = Rect { x: 0, y: 0, width: area.width.saturating_sub(2), height: 1 };
                        let mut lc: Vec<Constraint> = Vec::with_capacity(widths.len() * 2 - 1);
                        for (i, w) in widths.iter().enumerate() {
                            if i > 0 { lc.push(Constraint::Length(1)); }
                            lc.push(*w);
                        }
                        Layout::horizontal(lc).split(inner)[4].width as usize
                    };

                    // Compute the filtered/search-narrowed index list for this frame.
                    let active_profile = active_profile_idx.and_then(|i| config.profiles.get(i));
                    let displayed_indices =
                        compute_displayed_indices(&rows_data, &repo_full_names, &pr_providers, &is_draft, show_drafts, &search_query, active_profile);

                    let rows: Vec<Row> = displayed_indices
                        .iter()
                        .enumerate()
                        .map(|(display_pos, &raw_idx)| {
                            let cells = &rows_data[raw_idx];
                            let is_selected = table_state.selected() == Some(display_pos);

                            // Pre-compute the scrolled title string for the selected row.
                            // Only scroll when the title is clipped (longer than available column width).
                            let title_str = &cells[2];
                            let title_chars = title_str.chars().count();
                            let gap = "   "; // 3-space separator between repetitions
                            let padded: String = format!("{}{}", title_str, gap);
                            let cycle_len = padded.chars().count();
                            let needs_scroll = is_selected && title_chars > title_col_width;
                            let offset = if needs_scroll { scroll_offset % cycle_len } else { 0 };
                            let scrolled_title: String = padded
                                .chars()
                                .cycle()
                                .skip(offset)
                                .take(title_str.chars().count() + gap.len())
                                .collect();

                            let styled: Vec<Cell> = cells.iter().enumerate().map(|(i, c)| {
                                match i {
                                    2 if is_selected && is_draft[raw_idx] => Cell::from(Line::from(vec![
                                        Span::styled("\u{F040} ", Style::default().fg(draft_icon_color)),
                                        Span::raw(scrolled_title.clone()),
                                    ])),
                                    2 if is_selected => Cell::from(scrolled_title.clone()),
                                    2 if is_draft[raw_idx] => Cell::from(Line::from(vec![
                                        Span::styled("\u{F040} ", Style::default().fg(draft_icon_color)),
                                        Span::raw(c.clone()),
                                    ])),
                                    5 => Cell::from(c.as_str()).style(status_styles[raw_idx]),
                                    6 => Cell::from(c.as_str()).style(ci_styles[raw_idx]),
                                    _ => Cell::from(c.as_str()),
                                }
                            }).collect();
                            Row::new(styled)
                        })
                        .collect();

                    let visible_count = displayed_indices.len();

                    // Build title as multi-span Line with styled badges for profile and draft state.
                    let title_line = {
                        let base_text = format!(
                            " prlens — {visible_count} PR{} waiting ",
                            if visible_count == 1 { "" } else { "s" },
                        );
                        let mut spans = vec![
                            Span::styled(base_text, Style::default().fg(panel_blue).add_modifier(Modifier::BOLD)),
                        ];
                        if let Some(idx) = active_profile_idx {
                            if let Some(p) = config.profiles.get(idx) {
                                spans.push(Span::styled(
                                    format!(" {} ", p.name),
                                    Style::default()
                                        .fg(Color::Rgb(30, 30, 46))
                                        .bg(Color::Rgb(166, 227, 161))
                                        .add_modifier(Modifier::BOLD),
                                ));
                                spans.push(Span::styled(" ", Style::default().fg(panel_blue)));
                            }
                        }
                        // Draft badge: grey when hidden (default/quiet), amber when shown (active).
                        let (draft_label, draft_fg, draft_bg) = if show_drafts {
                            (" drafts on ", Color::Rgb(30, 30, 46), Color::Rgb(249, 226, 175))
                        } else {
                            (" drafts off ", Color::Rgb(205, 214, 244), Color::Rgb(88, 91, 112))
                        };
                        spans.push(Span::styled(
                            draft_label,
                            Style::default().fg(draft_fg).bg(draft_bg).add_modifier(Modifier::BOLD),
                        ));
                        spans.push(Span::styled(" ", Style::default().fg(panel_blue)));
                        Line::from(spans)
                    };

                    // Hint bar: keys in bright white/bold, descriptions in hint_color cyan.
                    let key_style  = Style::default().fg(Color::Rgb(205, 214, 244)).add_modifier(Modifier::BOLD);
                    let desc_style = Style::default().fg(hint_color);
                    let draft_label = if show_drafts { " hide drafts  " } else { " show drafts  " };
                    let mut hint_spans = vec![
                        Span::styled(" /", key_style), Span::styled(" search  ", desc_style),
                        Span::styled("Space", key_style), Span::styled(" detail  ", desc_style),
                        Span::styled("d", key_style), Span::styled(draft_label, desc_style),
                        Span::styled("j/k", key_style), Span::styled(" nav  ", desc_style),
                        Span::styled("Enter", key_style), Span::styled(" open  ", desc_style),
                        Span::styled("q", key_style), Span::styled(" quit ", desc_style),
                    ];
                    if config.jira.base_url.is_some() {
                        hint_spans.insert(6, Span::styled("J", key_style));
                        hint_spans.insert(7, Span::styled(" jira  ", desc_style));
                    }
                    if !config.profiles.is_empty() {
                        hint_spans.insert(6, Span::styled("Tab", key_style));
                        let profile_hint_label = match active_profile_idx.and_then(|i| config.profiles.get(i)) {
                            Some(p) => format!(" [{}]  ", p.name),
                            None    => " profile  ".to_string(),
                        };
                        hint_spans.insert(7, Span::styled(profile_hint_label, desc_style));
                    }
                    let hint_line = Line::from(hint_spans);
                    let table = Table::new(rows, widths)
                        .header(header)
                        .block(
                            Block::default()
                                .borders(Borders::ALL)
                                .border_style(Style::default().fg(panel_blue))
                                .title(title_line)
                                .title_bottom(hint_line),
                        )
                        .row_highlight_style(selected_style);

                    frame.render_stateful_widget(table, area, &mut table_state);

                    // Render detail overlay when ShowingDetail mode is active.
                    if mode == AppMode::ShowingDetail {
                        if let Some(display_i) = table_state.selected() {
                            if let Some(&raw_i) = displayed_snapshot.get(display_i) {
                                if let Some(pr) = prs.as_ref().and_then(|v| v.get(raw_i)) {
                                    let overlay_area = popup_area(area, 70, 80);
                                    frame.render_widget(Clear, overlay_area);
                                    frame.render_widget(
                                        build_detail_block(pr, &config.jira, panel_blue, hint_color),
                                        overlay_area,
                                    );
                                }
                            }
                        }
                    }

                    // Render search bar at bottom of table area when in Searching mode.
                    if mode == AppMode::Searching {
                        if let Some(sa) = search_bar_area {
                            let input_block = Block::default()
                                .borders(Borders::ALL)
                                .border_style(Style::default().fg(panel_blue))
                                .title(Line::from(Span::styled(
                                    " Search ",
                                    Style::default().fg(panel_blue),
                                )));
                            let input_paragraph = Paragraph::new(search_query.as_str())
                                .block(input_block);
                            frame.render_widget(input_paragraph, sa);
                            // Position the terminal cursor inside the search input.
                            frame.set_cursor_position(Position::new(
                                sa.x + cursor_pos as u16 + 1, // +1 for left border
                                sa.y + 1,                      // +1 for top border
                            ));
                        }
                    }

                    // Export the current frame's filtered snapshot so event handlers can
                    // resolve display positions to raw PR indices without access to this closure.
                    displayed_snapshot = displayed_indices.clone();
                    last_displayed_len = displayed_indices.len();

                    // Render provider error bar below table (red, one line each).
                    if let Some(err_area) = error_area {
                        let lines: Vec<Line> = provider_errors.iter()
                            .map(|e| Line::from(Span::styled(
                                format!("{e}"),
                                Style::default().fg(error_color),
                            )))
                            .collect();
                        frame.render_widget(
                            Paragraph::new(lines).wrap(Wrap { trim: false }),
                            err_area,
                        );
                    }
                }
            }
        })?;

        // --- Events ---
        // Use a short timeout while loading so the spinner stays responsive.
        // Also use a short timeout in Searching mode for sub-frame input latency (Pitfall 5).
        let timeout = if prs.is_none() || mode == AppMode::Searching {
            Duration::from_millis(50)
        } else {
            Duration::from_millis(200)
        };
        if event::poll(timeout)? {
            if let Event::Key(key) = event::read()? {
                if key.kind != KeyEventKind::Press { continue; }
                let pr_count = prs.as_ref().map_or(0, |v| v.len());
                match mode {
                    AppMode::Normal => match key.code {
                        KeyCode::Char('q') | KeyCode::Esc => break,
                        KeyCode::Char('/') => {
                            mode = AppMode::Searching;
                            search_query.clear();
                            cursor_pos = 0;
                        }
                        KeyCode::Char(' ') if last_displayed_len > 0 => {
                            mode = AppMode::ShowingDetail;
                        }
                        KeyCode::Char('d') => {
                            show_drafts = !show_drafts;
                            // Clamp selection after draft visibility change.
                            let new_len = compute_displayed_indices(
                                &rows_data, &repo_full_names, &pr_providers, &is_draft, show_drafts, &search_query,
                                active_profile_idx.and_then(|i| config.profiles.get(i)),
                            ).len();
                            if let Some(sel) = table_state.selected() {
                                if sel >= new_len && new_len > 0 {
                                    table_state.select(Some(new_len - 1));
                                } else if new_len == 0 {
                                    table_state.select(None);
                                }
                            }
                        }
                        KeyCode::Tab if !config.profiles.is_empty() => {
                            // Cycle: None → Some(0) → … → Some(last) → None.
                            active_profile_idx = match active_profile_idx {
                                None => Some(0),
                                Some(i) if i + 1 < config.profiles.len() => Some(i + 1),
                                Some(_) => None,
                            };
                            // Reset cursor to row 0 after profile change.
                            let new_profile = active_profile_idx.and_then(|i| config.profiles.get(i));
                            let new_len = compute_displayed_indices(
                                &rows_data, &repo_full_names, &pr_providers, &is_draft, show_drafts, &search_query, new_profile,
                            ).len();
                            if new_len == 0 {
                                table_state.select(None);
                            } else {
                                table_state.select(Some(0));
                            }
                        }
                        KeyCode::Char('J') if last_displayed_len > 0 => {
                            // Capital J: open Jira ticket for the selected PR.
                            if let Some(ref base_url) = config.jira.base_url {
                                if let Some(display_i) = table_state.selected() {
                                    let raw_i_opt = displayed_snapshot.get(display_i).copied()
                                        .or_else(|| prs.as_ref().and_then(|v| if display_i < v.len() { Some(display_i) } else { None }));
                                    if let Some(raw_i) = raw_i_opt {
                                        if let Some(pr) = prs.as_ref().and_then(|v| v.get(raw_i)) {
                                            if let Some(key_str) = extract_jira_key(&pr.title) {
                                                let jira_url = format!("{}/browse/{}", base_url.trim_end_matches('/'), key_str);
                                                let _ = open::that(jira_url);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        KeyCode::Char('j') | KeyCode::Down if last_displayed_len > 0 => {
                            let len = last_displayed_len;
                            let next = match table_state.selected() {
                                Some(i) if i + 1 < len => i + 1,
                                _ => 0,
                            };
                            table_state.select(Some(next));
                        }
                        KeyCode::Char('k') | KeyCode::Up if last_displayed_len > 0 => {
                            let len = last_displayed_len;
                            let prev = match table_state.selected() {
                                Some(0) | None => len.saturating_sub(1),
                                Some(i) => i - 1,
                            };
                            table_state.select(Some(prev));
                        }
                        KeyCode::Enter if last_displayed_len > 0 => {
                            if let Some(display_i) = table_state.selected() {
                                if let Some(&raw_i) = displayed_snapshot.get(display_i) {
                                    if let Some(pr) = prs.as_ref().and_then(|v| v.get(raw_i)) {
                                        let _ = open::that(&pr.url);
                                    }
                                }
                            }
                        }
                        _ => {}
                    },
                    AppMode::Searching => match key.code {
                        KeyCode::Esc => {
                            mode = AppMode::Normal;
                            search_query.clear();
                            cursor_pos = 0;
                            // Clamp selection after clearing search.
                            let new_len = compute_displayed_indices(
                                &rows_data, &repo_full_names, &pr_providers, &is_draft, show_drafts, &search_query,
                                active_profile_idx.and_then(|i| config.profiles.get(i)),
                            ).len();
                            if let Some(sel) = table_state.selected() {
                                if sel >= new_len && new_len > 0 {
                                    table_state.select(Some(new_len - 1));
                                } else if new_len == 0 {
                                    table_state.select(None);
                                }
                            }
                        }
                        KeyCode::Backspace => {
                            delete_char(&mut search_query, &mut cursor_pos);
                            // Clamp selection after narrowing filter.
                            let new_len = compute_displayed_indices(
                                &rows_data, &repo_full_names, &pr_providers, &is_draft, show_drafts, &search_query,
                                active_profile_idx.and_then(|i| config.profiles.get(i)),
                            ).len();
                            if let Some(sel) = table_state.selected() {
                                if sel >= new_len && new_len > 0 {
                                    table_state.select(Some(new_len - 1));
                                } else if new_len == 0 {
                                    table_state.select(None);
                                }
                            }
                        }
                        KeyCode::Char(c) => {
                            enter_char(&mut search_query, &mut cursor_pos, c);
                            // Clamp selection after narrowing filter.
                            let new_len = compute_displayed_indices(
                                &rows_data, &repo_full_names, &pr_providers, &is_draft, show_drafts, &search_query,
                                active_profile_idx.and_then(|i| config.profiles.get(i)),
                            ).len();
                            if let Some(sel) = table_state.selected() {
                                if sel >= new_len && new_len > 0 {
                                    table_state.select(Some(new_len - 1));
                                } else if new_len == 0 {
                                    table_state.select(None);
                                }
                            }
                        }
                        _ => {}
                    },
                    AppMode::ShowingDetail => match key.code {
                        KeyCode::Char(' ') | KeyCode::Esc => {
                            mode = AppMode::Normal;
                        }
                        KeyCode::Char('J') => {
                            if let Some(ref base_url) = config.jira.base_url {
                                if let Some(display_i) = table_state.selected() {
                                    if let Some(&raw_i) = displayed_snapshot.get(display_i) {
                                        if let Some(pr) = prs.as_ref().and_then(|v| v.get(raw_i)) {
                                            if let Some(key_str) = extract_jira_key(&pr.title) {
                                                let jira_url = format!("{}/browse/{}", base_url.trim_end_matches('/'), key_str);
                                                let _ = open::that(jira_url);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        _ => {}
                    },
                }
            }
        }
    }

    Ok(())
}

// --- Private formatting helpers (mirrors display.rs — kept local to avoid pub API churn) ---

fn format_review_status(status: &ReviewStatus) -> &'static str {
    match status {
        ReviewStatus::NeedsReview => "review",
        ReviewStatus::Approved => "approved",
        ReviewStatus::ChangesRequested => "changes",
        ReviewStatus::Mixed => "mixed",
        ReviewStatus::InReview => "in review",
    }
}

fn format_ci_status(ci: &Option<CiStatus>) -> &'static str {
    match ci {
        None => "\u{F068}",                                          // nf-fa-minus
        Some(CiStatus::Success) => "\u{F058}",                      // nf-fa-check_circle
        Some(CiStatus::Failed) => "\u{F057}",                       // nf-fa-times_circle
        Some(CiStatus::Pending) | Some(CiStatus::Running) => "\u{F110}", // nf-fa-spinner
        Some(CiStatus::Cancelled) => "\u{F068}",                    // nf-fa-minus
    }
}

fn format_age(dt: &chrono::DateTime<chrono::Utc>) -> String {
    let elapsed = chrono::Utc::now().signed_duration_since(*dt);
    let secs = elapsed.num_seconds();
    if secs < 0 {
        return "future".to_string();
    }
    if secs < 60 {
        return "now".to_string();
    }
    let mins = elapsed.num_minutes();
    if mins < 60 {
        return format!("{}m", mins);
    }
    let hours = elapsed.num_hours();
    if hours < 24 {
        return format!("{}h", hours);
    }
    let days = elapsed.num_days();
    if days < 7 {
        return format!("{}d", days);
    }
    let weeks = days / 7;
    if weeks < 8 {
        return format!("{}w", weeks);
    }
    let months = days / 30;
    if months < 12 {
        return format!("{}mo", months);
    }
    format!("{}y", days / 365)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::models::{PrIdentifier, PrState, User};
    use chrono::{Duration, Utc};

    // Wave 0 test stubs — TUI-04, TUI-05, TUI-07, TUI-08, JIRA-01

    #[test]
    fn enter_char_unicode() {
        let mut s = "héllo".to_string();
        let mut cursor: usize = 5;
        enter_char(&mut s, &mut cursor, '!');
        assert_eq!(s, "héllo!");
        assert_eq!(cursor, 6);

        let mut s2 = String::new();
        let mut cursor2: usize = 0;
        enter_char(&mut s2, &mut cursor2, 'a');
        assert_eq!(s2, "a");
        assert_eq!(cursor2, 1);
    }

    #[test]
    fn delete_char_removes_last() {
        let mut s = "abc".to_string();
        let mut cursor: usize = 3;
        delete_char(&mut s, &mut cursor);
        assert_eq!(s, "ab");
        assert_eq!(cursor, 2);

        // cursor at 0 — no-op
        let mut s2 = "abc".to_string();
        let mut cursor2: usize = 0;
        delete_char(&mut s2, &mut cursor2);
        assert_eq!(s2, "abc");
        assert_eq!(cursor2, 0);
    }

    #[test]
    fn popup_area_contained() {
        let area = Rect { x: 0, y: 0, width: 80, height: 24 };
        let result = popup_area(area, 70, 80);
        assert!(result.x >= 0, "x should be >= 0");
        assert!(result.x + result.width <= 80, "x+width should fit in 80");
        assert!(result.y >= 0, "y should be >= 0");
        assert!(result.y + result.height <= 24, "y+height should fit in 24");
        assert!(result.width < 80, "width should be less than parent");
        assert!(result.height < 24, "height should be less than parent");
    }

    #[test]
    fn search_filter() {
        let rows_data: Vec<[String; 9]> = vec![
            ["".to_string(), "1".to_string(), "fix PROJ-123".to_string(), "bob".to_string(), "repo".to_string(), "".to_string(), "".to_string(), "0".to_string(), "1h".to_string()],
            ["".to_string(), "2".to_string(), "chore: cleanup".to_string(), "carol".to_string(), "repo".to_string(), "".to_string(), "".to_string(), "0".to_string(), "1h".to_string()],
            ["".to_string(), "3".to_string(), "feat: alice feature".to_string(), "dave".to_string(), "repo".to_string(), "".to_string(), "".to_string(), "0".to_string(), "1h".to_string()],
        ];
        let is_draft = vec![false, false, false];

        let indices = compute_displayed_indices(&rows_data, &[], &[], &is_draft, false, "alice", None);
        assert_eq!(indices, vec![2]);

        let indices_all = compute_displayed_indices(&rows_data, &[], &[], &is_draft, false, "", None);
        assert_eq!(indices_all, vec![0, 1, 2]);
    }

    #[test]
    fn draft_filter_hides_drafts() {
        let rows_data: Vec<[String; 9]> = vec![
            ["".to_string(), "1".to_string(), "PR one".to_string(), "a".to_string(), "r".to_string(), "".to_string(), "".to_string(), "0".to_string(), "1h".to_string()],
            ["".to_string(), "2".to_string(), "PR two draft".to_string(), "b".to_string(), "r".to_string(), "".to_string(), "".to_string(), "0".to_string(), "1h".to_string()],
            ["".to_string(), "3".to_string(), "PR three".to_string(), "c".to_string(), "r".to_string(), "".to_string(), "".to_string(), "0".to_string(), "1h".to_string()],
        ];
        let is_draft = vec![false, true, false];
        let indices = compute_displayed_indices(&rows_data, &[], &[], &is_draft, false, "", None);
        assert_eq!(indices, vec![0, 2]);
    }

    #[test]
    fn draft_filter_shows_drafts() {
        let rows_data: Vec<[String; 9]> = vec![
            ["".to_string(), "1".to_string(), "PR one".to_string(), "a".to_string(), "r".to_string(), "".to_string(), "".to_string(), "0".to_string(), "1h".to_string()],
            ["".to_string(), "2".to_string(), "PR two draft".to_string(), "b".to_string(), "r".to_string(), "".to_string(), "".to_string(), "0".to_string(), "1h".to_string()],
            ["".to_string(), "3".to_string(), "PR three".to_string(), "c".to_string(), "r".to_string(), "".to_string(), "".to_string(), "0".to_string(), "1h".to_string()],
        ];
        let is_draft = vec![false, true, false];
        let indices = compute_displayed_indices(&rows_data, &[], &[], &is_draft, true, "", None);
        assert_eq!(indices, vec![0, 1, 2]);
    }

    #[test]
    fn profile_filter_by_repo() {
        let rows_data: Vec<[String; 9]> = vec![
            ["".to_string(), "1".to_string(), "PR one".to_string(), "a".to_string(), "my-repo".to_string(), "review".to_string(), "".to_string(), "0".to_string(), "1h".to_string()],
            ["".to_string(), "2".to_string(), "PR two".to_string(), "b".to_string(), "other-repo".to_string(), "review".to_string(), "".to_string(), "0".to_string(), "1h".to_string()],
        ];
        let is_draft = vec![false, false];
        let profile = crate::config::ProfileConfig {
            name: "work".to_string(),
            filters: vec![crate::config::FilterGroup { repo: Some("my-repo".to_string()), ..Default::default() }],
        };
        let full_names = vec!["org/my-repo".to_string(), "org/other-repo".to_string()];
        let indices = compute_displayed_indices(&rows_data, &full_names, &[], &is_draft, false, "", Some(&profile));
        assert_eq!(indices, vec![0]);
    }

    #[test]
    fn profile_filter_by_org() {
        let rows_data: Vec<[String; 9]> = vec![
            ["".to_string(), "1".to_string(), "PR one".to_string(), "a".to_string(), "acme-service".to_string(), "review".to_string(), "".to_string(), "0".to_string(), "1h".to_string()],
            ["".to_string(), "2".to_string(), "PR two".to_string(), "b".to_string(), "other-service".to_string(), "review".to_string(), "".to_string(), "0".to_string(), "1h".to_string()],
        ];
        let is_draft = vec![false, false];
        let profile = crate::config::ProfileConfig {
            name: "acme".to_string(),
            filters: vec![crate::config::FilterGroup { org: Some("acme".to_string()), ..Default::default() }],
        };
        let full_names = vec!["acme/acme-service".to_string(), "other/other-service".to_string()];
        let indices = compute_displayed_indices(&rows_data, &full_names, &[], &is_draft, false, "", Some(&profile));
        assert_eq!(indices, vec![0]);
    }

    #[test]
    fn profile_filter_org_ignores_repo_name() {
        // A PR under "christoph.wedenig/dynatrace-app" must NOT match org = "dynatrace".
        // Only the org portion (before '/') is checked, not the full "org/repo" string.
        let rows_data: Vec<[String; 9]> = vec![
            ["".to_string(), "1".to_string(), "Private PR".to_string(), "a".to_string(), "dynatrace-app".to_string(), "review".to_string(), "".to_string(), "0".to_string(), "1h".to_string()],
            ["".to_string(), "2".to_string(), "DT PR".to_string(), "b".to_string(), "some-service".to_string(), "review".to_string(), "".to_string(), "0".to_string(), "1h".to_string()],
        ];
        let is_draft = vec![false, false];
        let profile = crate::config::ProfileConfig {
            name: "work".to_string(),
            filters: vec![crate::config::FilterGroup { org: Some("dynatrace".to_string()), ..Default::default() }],
        };
        let full_names = vec![
            "christoph.wedenig/dynatrace-app".to_string(),  // org does NOT start with dynatrace
            "Dynatrace-apps/some-service".to_string(),       // org starts with dynatrace
        ];
        let indices = compute_displayed_indices(&rows_data, &full_names, &[], &is_draft, false, "", Some(&profile));
        assert_eq!(indices, vec![1], "private repo whose repo-name contains filter keyword must not match");
    }

    #[test]
    fn profile_filter_none_passes_all() {
        let rows_data: Vec<[String; 9]> = vec![
            ["".to_string(), "1".to_string(), "PR one".to_string(), "a".to_string(), "repo-a".to_string(), "review".to_string(), "".to_string(), "0".to_string(), "1h".to_string()],
            ["".to_string(), "2".to_string(), "PR two".to_string(), "b".to_string(), "repo-b".to_string(), "review".to_string(), "".to_string(), "0".to_string(), "1h".to_string()],
        ];
        let is_draft = vec![false, false];
        let indices = compute_displayed_indices(&rows_data, &[], &[], &is_draft, false, "", None);
        assert_eq!(indices, vec![0, 1]);
    }

    #[test]
    fn jira_key_extraction() {
        assert_eq!(extract_jira_key("fix PROJ-123: description"), Some("PROJ-123".to_string()));
    }

    #[test]
    fn jira_key_extraction_none() {
        assert_eq!(extract_jira_key("no ticket here"), None);
    }

    #[test]
    fn jira_key_extraction_first() {
        assert_eq!(extract_jira_key("AB-1 and CD-2"), Some("AB-1".to_string()));
    }

    fn make_pr(provider: &str, number: u64, title: &str, url: &str) -> PullRequest {
        PullRequest {
            id: PrIdentifier {
                provider: provider.to_string(),
                owner: "acme".to_string(),
                repo: "widget".to_string(),
                number,
            },
            number,
            title: title.to_string(),
            url: url.to_string(),
            author: User {
                login: "alice".to_string(),
                display_name: None,
                avatar_url: None,
            },
            reviewers: vec![],
            repo_full_name: "acme/widget".to_string(),
            provider: provider.to_string(),
            head_branch: "feat/test".to_string(),
            base_branch: "main".to_string(),
            state: PrState::Open,
            review_status: ReviewStatus::NeedsReview,
            ci_status: None,
            draft: false,
            created_at: Utc::now(),
            updated_at: Utc::now() - Duration::hours(1),
            labels: vec![],
            comment_count: 0,
            additions: None,
            deletions: None,
        }
    }

    #[test]
    fn run_interactive_empty_does_not_panic() {
        // Non-TTY environment (test runner): run_interactive reads the channel and returns Ok(())
        // without entering raw mode.
        let (tx, rx) = std::sync::mpsc::sync_channel(1);
        tx.send((vec![], vec![])).unwrap();
        let result = run_interactive(rx, &crate::config::Config::default(), None);
        assert!(result.is_ok());
    }

    #[test]
    fn format_age_minutes() {
        let dt = Utc::now() - Duration::minutes(45);
        assert_eq!(format_age(&dt), "45m");
    }

    #[test]
    fn format_age_hours() {
        let dt = Utc::now() - Duration::hours(3);
        assert_eq!(format_age(&dt), "3h");
    }

    #[test]
    fn format_review_status_all_variants() {
        assert_eq!(format_review_status(&ReviewStatus::NeedsReview), "review");
        assert_eq!(format_review_status(&ReviewStatus::Approved), "approved");
        assert_eq!(format_review_status(&ReviewStatus::ChangesRequested), "changes");
        assert_eq!(format_review_status(&ReviewStatus::Mixed), "mixed");
        assert_eq!(format_review_status(&ReviewStatus::InReview), "in review");
    }

    #[test]
    fn format_ci_status_icons() {
        assert_eq!(format_ci_status(&None), "\u{F068}");
        assert_eq!(format_ci_status(&Some(CiStatus::Success)), "\u{F058}");
        assert_eq!(format_ci_status(&Some(CiStatus::Failed)), "\u{F057}");
        assert_eq!(format_ci_status(&Some(CiStatus::Pending)), "\u{F110}");
        assert_eq!(format_ci_status(&Some(CiStatus::Cancelled)), "\u{F068}");
    }

    #[test]
    fn provider_badge_github() {
        let pr = make_pr("github", 1, "Test", "https://github.com/acme/widget/pull/1");
        let src = match pr.provider.as_str() {
            "github" => "\u{E709}",
            "bitbucket" => "\u{E703}",
            other => other,
        };
        assert_eq!(src, "\u{E709}");
    }

    #[test]
    fn provider_badge_bitbucket() {
        let pr = make_pr("bitbucket", 1, "Test", "https://bitbucket.org/acme/widget/pull-requests/1");
        let src = match pr.provider.as_str() {
            "github" => "\u{E709}",
            "bitbucket" => "\u{E703}",
            other => other,
        };
        assert_eq!(src, "\u{E703}");
    }
}