gitlab-tracker 0.3.2

A fast terminal TUI dashboard for tracking GitLab Merge Requests across branches
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
use crate::config::AppConfig;
use crate::models::{GitlabMrState, MergeabilityStatus, Pipeline, PipelineState, TrackedMr};
use crate::ui::table::badge_label;
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span, Text};

/// Renders the pipeline list view for the Inspector panel.
///
/// Shows the last fetched pipelines for the selected MR with their jobs,
/// grouped by pipeline run. Displayed when the user presses [P].
/// Renders the Time Log view for the Inspector panel (redmine feature only).
///
/// Shows a progress bar (spent vs estimate), then the list of time entries
/// fetched from Redmine for the linked ticket.
pub fn render_time_log_text(
    mr: &TrackedMr,
    entries: &[gitlab_tracker_core::TimeEntry],
) -> ratatui::text::Text<'static> {
    /// Formats hours (f32) as "Xh Ym" for display.
    fn fmt_hours(hours: f32) -> String {
        let total_mins = (hours * 60.0).round() as u32;
        let h = total_mins / 60;
        let m = total_mins % 60;
        match (h, m) {
            (0, m) => format!("{}m", m),
            (h, 0) => format!("{}h", h),
            (h, m) => format!("{}h {}m", h, m),
        }
    }

    /// Formats seconds (u32) as "Xh Ym" for display.
    fn fmt_secs(secs: u32) -> String {
        fmt_hours(secs as f32 / 3600.0)
    }

    let mut lines = vec![
        Line::from(vec![Span::styled(
            format!("Time Log — MR !{}", mr.id),
            Style::default()
                .fg(Color::Cyan)
                .add_modifier(Modifier::BOLD | Modifier::UNDERLINED),
        )]),
        Line::from(vec![Span::raw("")]),
    ];

    // ── Progress bar (estimate vs spent) ─────────────────────────────────────
    if let Some(ticket) = &mr.linked_ticket {
        let estimate_secs = ticket.time_estimate.unwrap_or(0);
        let spent_secs = ticket.time_spent.unwrap_or(0);

        if estimate_secs > 0 {
            let ratio = (spent_secs as f32 / estimate_secs as f32).min(1.0);
            let bar_width: usize = 30;
            let filled = (ratio * bar_width as f32).round() as usize;
            let empty = bar_width.saturating_sub(filled);
            let bar = format!("{}{}", "".repeat(filled), "".repeat(empty));

            let pct = (spent_secs as f32 / estimate_secs as f32 * 100.0).round() as u32;
            let bar_color = if spent_secs > estimate_secs {
                Color::Red
            } else if pct >= 80 {
                Color::Yellow
            } else {
                Color::Green
            };

            lines.push(Line::from(vec![
                Span::raw("Estimate : "),
                Span::styled(
                    fmt_secs(estimate_secs),
                    Style::default()
                        .fg(Color::Cyan)
                        .add_modifier(Modifier::BOLD),
                ),
            ]));
            lines.push(Line::from(vec![
                Span::raw("Spent    : "),
                Span::styled(
                    fmt_secs(spent_secs),
                    Style::default().fg(bar_color).add_modifier(Modifier::BOLD),
                ),
                Span::raw("  "),
                Span::styled(bar, Style::default().fg(bar_color)),
                Span::raw(format!("  {}%", pct)),
            ]));
        } else {
            // No estimate — just show total spent.
            let total_hours: f32 = entries.iter().map(|e| e.hours).sum();
            lines.push(Line::from(vec![
                Span::raw("Total    : "),
                Span::styled(
                    fmt_hours(total_hours),
                    Style::default()
                        .fg(Color::Cyan)
                        .add_modifier(Modifier::BOLD),
                ),
            ]));
        }
        lines.push(Line::from(vec![Span::raw("")]));
    }

    // ── Entries list ──────────────────────────────────────────────────────────
    lines.push(Line::from(vec![
        Span::styled("── ", Style::default().fg(Color::DarkGray)),
        Span::styled(
            "Entries",
            Style::default()
                .fg(Color::Yellow)
                .add_modifier(Modifier::BOLD),
        ),
        Span::styled(
            " ──────────────────────────────",
            Style::default().fg(Color::DarkGray),
        ),
    ]));

    if entries.is_empty() {
        lines.push(Line::from(vec![Span::styled(
            "No time entries recorded yet.",
            Style::default().fg(Color::DarkGray),
        )]));
    } else {
        for entry in entries {
            // Line 1: date | duration | activity | user
            lines.push(Line::from(vec![
                Span::styled(entry.spent_on.clone(), Style::default().fg(Color::DarkGray)),
                Span::raw("  "),
                Span::styled(
                    fmt_hours(entry.hours),
                    Style::default()
                        .fg(Color::Cyan)
                        .add_modifier(Modifier::BOLD),
                ),
                Span::raw("  "),
                Span::styled(
                    entry.activity.name.clone(),
                    Style::default()
                        .fg(Color::Magenta)
                        .add_modifier(Modifier::BOLD),
                ),
                Span::raw("  "),
                Span::styled(entry.user.clone(), Style::default().fg(Color::White)),
            ]));
            // Line 2: comment (indented), shown only when non-empty.
            if !entry.comment.is_empty() {
                lines.push(Line::from(vec![
                    Span::raw("  "),
                    Span::styled(
                        format!("\"{}\"", entry.comment),
                        Style::default().fg(Color::DarkGray),
                    ),
                ]));
            }
        }

        // Summary footer.
        let total_hours: f32 = entries.iter().map(|e| e.hours).sum();
        lines.push(Line::from(vec![Span::raw("")]));
        lines.push(Line::from(vec![Span::styled(
            "─────────────────────────────────────",
            Style::default().fg(Color::DarkGray),
        )]));
        lines.push(Line::from(vec![Span::styled(
            format!(
                "Total: {} entries — {} logged",
                entries.len(),
                fmt_hours(total_hours)
            ),
            Style::default()
                .fg(Color::White)
                .add_modifier(Modifier::BOLD),
        )]));
    }

    ratatui::text::Text::from(lines)
}

pub fn render_pipelines_text(mr: &TrackedMr) -> Text<'static> {
    let mut lines = vec![
        Line::from(vec![Span::styled(
            format!("Pipelines — MR !{}", mr.id),
            Style::default()
                .fg(Color::Cyan)
                .add_modifier(Modifier::BOLD | Modifier::UNDERLINED),
        )]),
        Line::from(vec![Span::raw("")]),
    ];

    if mr.pipelines.is_empty() {
        lines.push(Line::from(vec![Span::styled(
            "No pipelines found for this MR.",
            Style::default().fg(Color::DarkGray),
        )]));
    } else {
        for pipeline in &mr.pipelines {
            lines.extend(render_pipeline_block(pipeline));
            lines.push(Line::from(vec![Span::raw("")]));
        }
    }

    Text::from(lines)
}

/// Renders a single pipeline block with its status header and job list.
fn render_pipeline_block(pipeline: &Pipeline) -> Vec<Line<'static>> {
    let (status_icon, status_color) = pipeline_status_style(&pipeline.status);

    // Format the creation timestamp to a compact readable form (drop sub-seconds and timezone).
    let date_display = pipeline
        .created_at
        .as_deref()
        .map(|s| s.get(..19).unwrap_or(s).replace('T', " "))
        .unwrap_or_else(|| "unknown date".to_string());

    let mut lines = vec![Line::from(vec![
        Span::styled(
            format!("#{} ", pipeline.id),
            Style::default()
                .fg(Color::White)
                .add_modifier(Modifier::BOLD),
        ),
        Span::styled(
            status_icon,
            Style::default()
                .fg(status_color)
                .add_modifier(Modifier::BOLD),
        ),
        Span::styled(
            format!("  {}", date_display),
            Style::default().fg(Color::DarkGray),
        ),
    ])];

    if pipeline.jobs.is_empty() {
        lines.push(Line::from(vec![Span::styled(
            "  No jobs.",
            Style::default().fg(Color::DarkGray),
        )]));
        return lines;
    }

    // Group jobs by stage for readability.
    let mut current_stage = String::new();
    for job in &pipeline.jobs {
        if job.stage != current_stage {
            current_stage = job.stage.clone();
            lines.push(Line::from(vec![Span::styled(
                format!("{}", current_stage),
                Style::default()
                    .fg(Color::Yellow)
                    .add_modifier(Modifier::BOLD),
            )]));
        }

        let (job_icon, job_color) = job_status_style(&job.status);
        let duration = job
            .duration
            .map(|d| format!(" ({:.0}s)", d))
            .unwrap_or_default();

        lines.push(Line::from(vec![
            Span::raw("    "),
            Span::styled(job_icon, Style::default().fg(job_color)),
            Span::raw(format!(" {}{}", job.name, duration)),
        ]));
    }

    lines
}

/// Maps a `PipelineState` to a display icon and its colour.
fn pipeline_status_style(state: &PipelineState) -> (&'static str, Color) {
    match state {
        PipelineState::Success => ("✔ passed", Color::Green),
        PipelineState::Failed => ("✘ failed", Color::Red),
        PipelineState::Running => ("⟳ running", Color::Cyan),
        PipelineState::Pending => ("◔ pending", Color::Yellow),
        PipelineState::Canceled => ("⊘ canceled", Color::DarkGray),
        PipelineState::Skipped => ("⊝ skipped", Color::DarkGray),
        PipelineState::Created => ("○ created", Color::White),
        PipelineState::Unknown => ("? unknown", Color::DarkGray),
    }
}

/// Maps a job status string (as returned by the GitLab API) to icon + colour.
fn job_status_style(status: &str) -> (&'static str, Color) {
    match status {
        "success" => ("", Color::Green),
        "failed" => ("", Color::Red),
        "running" => ("", Color::Cyan),
        "pending" => ("", Color::Yellow),
        "canceled" => ("", Color::DarkGray),
        "skipped" => ("", Color::DarkGray),
        "created" => ("", Color::White),
        _ => ("?", Color::DarkGray),
    }
}

pub fn create_chip_span(label: &str, config: &AppConfig) -> Span<'static> {
    let (bg, fg) = config.get_label_style(label);
    Span::styled(
        format!(" {} ", label),
        Style::default().bg(bg).fg(fg).add_modifier(Modifier::BOLD),
    )
}

/// Renders a section header separator with a coloured title.
fn section_header(title: &'static str) -> Line<'static> {
    Line::from(vec![
        Span::styled("── ", Style::default().fg(Color::DarkGray)),
        Span::styled(
            title,
            Style::default()
                .fg(Color::Yellow)
                .add_modifier(Modifier::BOLD),
        ),
        Span::styled(
            " ──────────────────────────────",
            Style::default().fg(Color::DarkGray),
        ),
    ])
}

pub fn render_safe_inspector_text(mr: &TrackedMr, config: &AppConfig) -> Text<'static> {
    // Format ISO 8601 timestamp to a more readable form (date + time, drop sub-seconds).
    let updated_at_display = mr
        .updated_at
        .as_deref()
        .map(|s| s.get(..19).unwrap_or(s).replace('T', " "))
        .unwrap_or_else(|| "Unknown".to_string());

    // Compute the activity badge (icon + color) based on configurable thresholds.
    let (badge_icon, badge_color) = config.activity_badge(mr.updated_at.as_deref());

    // Build the state badge using the same colour coding as the table column.
    // badge_label() centers the text to BADGE_WIDTH chars — no manual padding needed.
    let (state_text, state_fg, state_bg) = match mr.state {
        GitlabMrState::Opened => ("OPEN", Color::Black, Color::Green),
        GitlabMrState::Merged => ("MERGED", Color::Black, Color::Magenta),
        GitlabMrState::Closed => ("CLOSED", Color::Black, Color::Red),
    };
    let state_label = badge_label(state_text);

    // Build the git clone command for the source branch — used in the [Y]ank hint.
    // Derives the SSH clone URL from the web URL: replaces the HTTPS scheme and host
    // with the git@ SSH equivalent (standard GitLab convention).
    let git_clone_cmd = {
        // e.g. "https://gitlab.com/org/project/-/merge_requests/42"
        //   -> "git clone -b feat/my-branch git@gitlab.com:org/project.git"
        let ssh_url = mr
            .web_url
            .split("/-/")
            .next()
            .unwrap_or("")
            .replacen("https://", "git@", 1)
            .replacen('/', ":", 1);
        format!("git clone -b {} {}.git", mr.source_branch, ssh_url)
    };

    // ── SECTION 1: Identity ──────────────────────────────────────────────────
    let mut lines = vec![
        section_header("Identity"),
        Line::from(vec![Span::styled(
            format!("MR ID    : !{}", mr.id),
            Style::default()
                .fg(Color::Cyan)
                .add_modifier(Modifier::BOLD | Modifier::UNDERLINED),
        )]),
        Line::from(vec![
            Span::raw("State    : "),
            Span::styled(
                state_label,
                Style::default()
                    .fg(state_fg)
                    .bg(state_bg)
                    .add_modifier(Modifier::BOLD),
            ),
        ]),
        Line::from(vec![
            Span::raw("Branch   : "),
            Span::styled(
                mr.source_branch.clone(),
                Style::default()
                    .fg(Color::LightBlue)
                    .add_modifier(Modifier::BOLD),
            ),
            Span::raw(""),
            Span::styled(
                mr.target_branch.clone(),
                Style::default().fg(Color::DarkGray),
            ),
        ]),
        Line::from(vec![
            Span::raw("Clone    : "),
            Span::styled(git_clone_cmd.clone(), Style::default().fg(Color::DarkGray)),
            Span::raw("  "),
            Span::styled(
                "[Y] copy",
                Style::default()
                    .fg(Color::Yellow)
                    .add_modifier(Modifier::BOLD),
            ),
        ]),
        Line::from(vec![
            Span::raw("URL      : "),
            Span::styled(mr.web_url.clone(), Style::default().fg(Color::DarkGray)),
        ]),
    ];

    // ── SECTION 2: People ────────────────────────────────────────────────────
    lines.push(Line::from(vec![Span::raw("")]));
    lines.push(section_header("People"));
    lines.push(Line::from(vec![
        Span::raw("Author   : "),
        Span::styled(
            mr.author.clone(),
            Style::default().add_modifier(Modifier::BOLD),
        ),
    ]));
    lines.push(Line::from(vec![
        Span::raw("Assignee : "),
        Span::styled(
            mr.assignee.clone(),
            Style::default().add_modifier(Modifier::BOLD),
        ),
    ]));

    // Reviewers: listed inline, or dimmed "None" if empty.
    if mr.reviewers.is_empty() {
        lines.push(Line::from(vec![
            Span::raw("Reviewers: "),
            Span::styled("None", Style::default().fg(Color::DarkGray)),
        ]));
    } else {
        for (i, reviewer) in mr.reviewers.iter().enumerate() {
            let label = if i == 0 { "Reviewers: " } else { "           " };
            lines.push(Line::from(vec![
                Span::raw(label),
                Span::styled(
                    reviewer.clone(),
                    Style::default()
                        .fg(Color::LightBlue)
                        .add_modifier(Modifier::BOLD),
                ),
            ]));
        }
    }

    // Notes / comments indicator — always shown, highlights when non-zero.
    let (notes_text, notes_fg, notes_bg) = if mr.user_notes_count == 0 {
        (
            "  ✔ No comments  ".to_string(),
            Color::DarkGray,
            Color::Black,
        )
    } else {
        (
            format!("  💬 {} comment(s) — review pending  ", mr.user_notes_count),
            Color::Black,
            Color::Yellow,
        )
    };
    lines.push(Line::from(vec![
        Span::raw("Notes    : "),
        Span::styled(
            notes_text,
            Style::default()
                .fg(notes_fg)
                .bg(notes_bg)
                .add_modifier(Modifier::BOLD),
        ),
    ]));

    // ── SECTION 3: Planning ──────────────────────────────────────────────────
    lines.push(Line::from(vec![Span::raw("")]));
    lines.push(section_header("Planning"));
    lines.push(Line::from(vec![
        Span::raw("Milestone: "),
        Span::styled(mr.milestone.clone(), Style::default().fg(Color::Cyan)),
    ]));

    // Milestone due date — show with urgency colouring when set.
    let (due_text, due_color) = match mr.milestone_due_date.as_deref() {
        None | Some("") => ("Not set".to_string(), Color::DarkGray),
        Some(date) => {
            // Colour the date based on proximity: red if past, yellow if within 7 days,
            // green otherwise. We do a simple lexicographic comparison against today's date
            // (YYYY-MM-DD format sorts correctly without parsing).
            let today = chrono::Utc::now().format("%Y-%m-%d").to_string();
            let in_7_days = (chrono::Utc::now() + chrono::Duration::days(7))
                .format("%Y-%m-%d")
                .to_string();
            let color = if date < today.as_str() {
                Color::Red
            } else if date <= in_7_days.as_str() {
                Color::Yellow
            } else {
                Color::Green
            };
            (date.to_string(), color)
        }
    };
    lines.push(Line::from(vec![
        Span::raw("Due date : "),
        Span::styled(
            due_text,
            Style::default().fg(due_color).add_modifier(Modifier::BOLD),
        ),
    ]));

    // ── SECTION 4: Status ────────────────────────────────────────────────────
    lines.push(Line::from(vec![Span::raw("")]));
    lines.push(section_header("Status"));

    // Mergeability — only meaningful for open MRs.
    // badge_label() centers the text to BADGE_WIDTH, matching the State badge width.
    if mr.state == GitlabMrState::Opened {
        let (merge_text, merge_fg, merge_bg) = match mr.mergeability {
            MergeabilityStatus::Mergeable => ("MERGEABLE", Color::Black, Color::LightGreen),
            MergeabilityStatus::Conflict => ("CONFLICT", Color::White, Color::Red),
            MergeabilityStatus::NeedsRebase => ("REBASE", Color::Black, Color::Yellow),
            MergeabilityStatus::NotOpen => ("CLOSED", Color::Black, Color::Red),
            MergeabilityStatus::Draft => ("DRAFT", Color::White, Color::Rgb(80, 80, 80)),
            MergeabilityStatus::DiscussionsNotResolved => {
                ("DISCUSSIONS", Color::Black, Color::LightMagenta)
            }
            MergeabilityStatus::CiMustPass => ("CI MUST PASS", Color::Black, Color::LightYellow),
            MergeabilityStatus::CiStillRunning => ("CI STILL RUNNING", Color::Black, Color::Yellow),
            MergeabilityStatus::NotApproved => ("NOT APPROVED", Color::Black, Color::LightRed),
            MergeabilityStatus::RequestedChanges => ("REQUESTED CHANGES", Color::White, Color::Red),
            MergeabilityStatus::Unknown => ("UNKNOWN", Color::DarkGray, Color::Black),
        };
        lines.push(Line::from(vec![
            Span::raw("Merge    : "),
            Span::styled(
                badge_label(merge_text),
                Style::default()
                    .fg(merge_fg)
                    .bg(merge_bg)
                    .add_modifier(Modifier::BOLD),
            ),
        ]));
    }

    // merged_by / merged_at — only shown for merged MRs.
    if mr.state == GitlabMrState::Merged {
        let merged_by_display = mr.merged_by.as_deref().unwrap_or("Unknown");
        let merged_at_display = mr
            .merged_at
            .as_deref()
            .map(|s| s.get(..19).unwrap_or(s).replace('T', " "))
            .unwrap_or_else(|| "Unknown".to_string());
        lines.push(Line::from(vec![
            Span::raw("Merged by: "),
            Span::styled(
                merged_by_display.to_string(),
                Style::default()
                    .fg(Color::Magenta)
                    .add_modifier(Modifier::BOLD),
            ),
        ]));
        lines.push(Line::from(vec![
            Span::raw("Merged at: "),
            Span::styled(merged_at_display, Style::default().fg(Color::Magenta)),
        ]));
    }

    lines.push(Line::from(vec![
        Span::raw("Updated  : "),
        Span::styled(updated_at_display, Style::default().fg(Color::Yellow)),
        Span::raw("  "),
        Span::styled(
            badge_icon,
            Style::default()
                .fg(badge_color)
                .add_modifier(Modifier::BOLD),
        ),
    ]));

    // ── SECTION 5: Labels ────────────────────────────────────────────────────
    lines.push(Line::from(vec![Span::raw("")]));
    lines.push(section_header("Labels"));

    if !mr.labels.is_empty() {
        let mut label_spans: Vec<Span<'static>> = vec![];
        for label in &mr.labels {
            label_spans.push(create_chip_span(label, config));
            label_spans.push(Span::raw(" "));
        }
        lines.push(Line::from(label_spans));
    } else {
        lines.push(Line::from(vec![Span::styled(
            "None",
            Style::default().dark_gray(),
        )]));
    }

    // ── Tracker ticket ───────────────────────────────────────────────────────
    // Rendered for any active tracker provider — field is None when none is configured.
    {
        /// Formats a duration in seconds as "Xh Ym" for display.
        fn format_duration(secs: u32) -> String {
            if secs == 0 {
                return "".to_string();
            }
            let h = secs / 3600;
            let m = (secs % 3600) / 60;
            match (h, m) {
                (0, m) => format!("{}m", m),
                (h, 0) => format!("{}h", h),
                (h, m) => format!("{}h {}m", h, m),
            }
        }

        lines.push(Line::from(vec![Span::raw("")]));
        lines.push(section_header("Tracker"));
        match &mr.linked_ticket {
            Some(ticket) => {
                lines.push(Line::from(vec![
                    Span::raw("Ticket   : "),
                    Span::styled(
                        format!("#{}", ticket.id),
                        Style::default()
                            .fg(Color::LightMagenta)
                            .add_modifier(Modifier::BOLD),
                    ),
                    Span::raw("  "),
                    Span::styled(ticket.subject.clone(), Style::default().fg(Color::White)),
                ]));
                lines.push(Line::from(vec![
                    Span::raw("Status   : "),
                    Span::styled(
                        ticket.status.clone(),
                        Style::default()
                            .fg(Color::Cyan)
                            .add_modifier(Modifier::BOLD),
                    ),
                ]));
                lines.push(Line::from(vec![
                    Span::raw("Author   : "),
                    Span::styled(
                        ticket
                            .author
                            .clone()
                            .unwrap_or_else(|| "Unknown".to_string()),
                        Style::default().add_modifier(Modifier::BOLD),
                    ),
                ]));
                lines.push(Line::from(vec![
                    Span::raw("Assignee : "),
                    Span::styled(
                        ticket
                            .assignee
                            .clone()
                            .unwrap_or_else(|| "Unassigned".to_string()),
                        Style::default()
                            .fg(Color::LightBlue)
                            .add_modifier(Modifier::BOLD),
                    ),
                ]));

                // Time tracking — shown only when at least one value is set by Redmine.
                let has_estimate = ticket.time_estimate.map(|v| v > 0).unwrap_or(false);
                let has_spent = ticket.time_spent.map(|v| v > 0).unwrap_or(false);
                if has_estimate || has_spent {
                    let estimate_str = ticket
                        .time_estimate
                        .map(format_duration)
                        .unwrap_or_else(|| "".to_string());
                    let spent_str = ticket
                        .time_spent
                        .map(format_duration)
                        .unwrap_or_else(|| "".to_string());

                    // Colour the spent value relative to the estimate:
                    // green < 80 %, yellow 80–100 %, red when over budget.
                    let spent_color = match (ticket.time_estimate, ticket.time_spent) {
                        (Some(est), Some(spent)) if est > 0 => {
                            let ratio = spent as f32 / est as f32;
                            if ratio >= 1.0 {
                                Color::Red
                            } else if ratio >= 0.8 {
                                Color::Yellow
                            } else {
                                Color::Green
                            }
                        }
                        _ => Color::DarkGray,
                    };

                    lines.push(Line::from(vec![
                        Span::raw("Estimate : "),
                        Span::styled(
                            estimate_str,
                            Style::default()
                                .fg(Color::Cyan)
                                .add_modifier(Modifier::BOLD),
                        ),
                    ]));
                    lines.push(Line::from(vec![
                        Span::raw("Spent    : "),
                        Span::styled(
                            spent_str,
                            Style::default()
                                .fg(spent_color)
                                .add_modifier(Modifier::BOLD),
                        ),
                    ]));
                }

                lines.push(Line::from(vec![
                    Span::raw("URL      : "),
                    Span::styled(ticket.url.clone(), Style::default().fg(Color::DarkGray)),
                    Span::raw("  "),
                    Span::styled(
                        "[T] open",
                        Style::default()
                            .fg(Color::Yellow)
                            .add_modifier(Modifier::BOLD),
                    ),
                ]));
            }
            None => {
                lines.push(Line::from(vec![Span::styled(
                    "No linked ticket detected.",
                    Style::default().fg(Color::DarkGray),
                )]));
            }
        }
    }

    // ── Description ──────────────────────────────────────────────────────────
    lines.push(Line::from(vec![Span::raw("")]));
    lines.push(section_header("Description"));
    lines.push(Line::from(vec![Span::raw("")]));

    if mr.description.trim().is_empty() {
        lines.push(Line::from(vec![Span::styled(
            "No description text provided.",
            Style::default().dark_gray(),
        )]));
        return Text::from(lines);
    }

    for raw_line in mr.description.lines() {
        let trimmed = raw_line.trim();

        if trimmed.starts_with("# ") || trimmed.starts_with("## ") {
            let text = trimmed.trim_start_matches('#').trim();
            lines.push(Line::from(vec![Span::styled(
                text.to_string(),
                Style::default()
                    .fg(ratatui::style::Color::Cyan)
                    .add_modifier(Modifier::BOLD),
            )]));
        } else if trimmed.starts_with("### ") {
            let text = trimmed.trim_start_matches("### ");
            lines.push(Line::from(vec![Span::styled(
                text.to_string(),
                Style::default()
                    .fg(ratatui::style::Color::Yellow)
                    .add_modifier(Modifier::BOLD),
            )]));
        } else if trimmed == "---" {
            lines.push(Line::from(vec![Span::styled(
                "───────────────────────────────────",
                Style::default().fg(ratatui::style::Color::DarkGray),
            )]));
        } else {
            let mut spans = Vec::new();
            let mut line_content = raw_line.to_string();
            if trimmed.starts_with("- ") {
                spans.push(Span::styled(
                    "",
                    Style::default().fg(ratatui::style::Color::Yellow),
                ));
                line_content = raw_line.replacen("- ", "", 1);
            } else if trimmed.starts_with("* ") {
                spans.push(Span::styled(
                    "",
                    Style::default().fg(ratatui::style::Color::Yellow),
                ));
                line_content = raw_line.replacen("* ", "", 1);
            }

            let bold_parts = line_content.split("**");
            let mut is_bold = false;

            for bold_part in bold_parts {
                let code_parts = bold_part.split('`');
                let mut is_code = false;

                for code_part in code_parts {
                    let mut style = Style::default();
                    if is_bold {
                        style = style.add_modifier(Modifier::BOLD);
                    }
                    if is_code {
                        style = style.fg(ratatui::style::Color::Magenta);
                    }

                    spans.push(Span::styled(code_part.to_string(), style));
                    is_code = !is_code;
                }
                is_bold = !is_bold;
            }
            lines.push(Line::from(spans));
        }
    }

    Text::from(lines)
}