prctrl 0.9.0

Terminal-native GitHub PR management. Stay on top of code reviews without leaving your terminal.
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
use anyhow::Result;
use chrono::{DateTime, Utc};
use futures::future::join_all;
use octocrab::Octocrab;
use serde::Serialize;

#[derive(Debug, Clone, Serialize)]
pub struct PendingReview {
    pub repo: String,
    pub pr_number: u64,
    pub pr_title: String,
    pub pr_author: String,
    pub pr_url: String,
    pub created_at: DateTime<Utc>,
    pub additions: u64,
    pub deletions: u64,
    pub draft: bool,
    pub branch: String,
}

pub async fn fetch_pr_by_number(
    token: &str,
    org: &str,
    repos: &[String],
    pr_number: u64,
) -> Result<Vec<PendingReview>> {
    let client = Octocrab::builder()
        .personal_token(token.to_string())
        .build()?;

    let mut results = vec![];

    for repo in repos {
        match client.pulls(org, repo).get(pr_number).await {
            Ok(pr) => {
                let author = pr.user.as_ref().map(|u| u.login.clone()).unwrap_or_default();
                results.push(PendingReview {
                    repo: repo.clone(),
                    pr_number: pr.number,
                    pr_title: pr.title.clone().unwrap_or_default(),
                    pr_author: author,
                    pr_url: pr.html_url.map(|u| u.to_string()).unwrap_or_default(),
                    created_at: pr.created_at.unwrap_or_default(),
                    additions: pr.additions.unwrap_or(0) as u64,
                    deletions: pr.deletions.unwrap_or(0) as u64,
                    draft: pr.draft.unwrap_or(false),
                    branch: pr.head.label.clone().unwrap_or_default(),
                });
            }
            Err(_) => continue,
        }
    }

    Ok(results)
}

pub async fn fetch_pending_reviews(
    token: &str,
    org: &str,
    repos: &[String],
    username: &str,
    teams: &[String],
    include_mine: bool,
    include_drafts: bool,
    exclude_prefixes: &[String],
    crew_members: &[String],
) -> Result<Vec<PendingReview>> {
    let client = Octocrab::builder()
        .personal_token(token.to_string())
        .build()?;

    let mut pending = vec![];

    for repo in repos {
        let prs = client
            .pulls(org, repo)
            .list()
            .state(octocrab::params::State::Open)
            .per_page(50)
            .send()
            .await?;

        for pr in prs.items {
            if !include_drafts && pr.draft.unwrap_or(false) {
                continue;
            }

            let title = pr.title.clone().unwrap_or_default();
            if exclude_prefixes.iter().any(|p| !p.is_empty() && title.starts_with(p)) {
                continue;
            }

            let author = pr.user.as_ref().map(|u| u.login.as_str()).unwrap_or("");

            // --crew mode: only show PRs authored by crew members
            if !crew_members.is_empty() {
                if !crew_members.iter().any(|m| m == author) {
                    continue;
                }
            } else {
                if !include_mine && author == username {
                    continue;
                }

                let user_requested = pr
                    .requested_reviewers
                    .as_deref()
                    .unwrap_or(&[])
                    .iter()
                    .any(|r| r.login == username);

                let team_requested = if teams.is_empty() {
                    false
                } else {
                    pr.requested_teams
                        .as_deref()
                        .unwrap_or(&[])
                        .iter()
                        .any(|t| teams.contains(&t.slug.to_lowercase()))
                };

                if !user_requested && !team_requested {
                    continue;
                }
            }

            // Fetch individual PR to get diff stats
            let detail = client.pulls(org, repo).get(pr.number).await?;

            pending.push(PendingReview {
                repo: repo.clone(),
                pr_number: pr.number,
                pr_title: title.clone(),
                pr_author: author.to_string(),
                pr_url: pr.html_url.map(|u| u.to_string()).unwrap_or_default(),
                created_at: pr.created_at.unwrap_or_default(),
                additions: detail.additions.unwrap_or(0) as u64,
                deletions: detail.deletions.unwrap_or(0) as u64,
                draft: pr.draft.unwrap_or(false),
                branch: pr.head.label.clone().unwrap_or_default(),
            });
        }
    }

    // Sort: oldest first (most urgent)
    pending.sort_by_key(|r| r.created_at);
    Ok(pending)
}

pub async fn fetch_my_open_prs(
    token: &str,
    org: &str,
    repos: &[String],
    username: &str,
    include_drafts: bool,
    exclude_prefixes: &[String],
) -> Result<Vec<PendingReview>> {
    let client = Octocrab::builder()
        .personal_token(token.to_string())
        .build()?;

    let mut my_prs = vec![];

    for repo in repos {
        let prs = client
            .pulls(org, repo)
            .list()
            .state(octocrab::params::State::Open)
            .per_page(50)
            .send()
            .await?;

        for pr in prs.items {
            if !include_drafts && pr.draft.unwrap_or(false) {
                continue;
            }

            let title = pr.title.clone().unwrap_or_default();
            if exclude_prefixes.iter().any(|p| !p.is_empty() && title.starts_with(p)) {
                continue;
            }

            let author = pr.user.as_ref().map(|u| u.login.as_str()).unwrap_or("");
            if author != username {
                continue;
            }

            // Fetch individual PR to get diff stats
            let detail = client.pulls(org, repo).get(pr.number).await?;

            my_prs.push(PendingReview {
                repo: repo.clone(),
                pr_number: pr.number,
                pr_title: title.clone(),
                pr_author: author.to_string(),
                pr_url: pr.html_url.map(|u| u.to_string()).unwrap_or_default(),
                created_at: pr.created_at.unwrap_or_default(),
                additions: detail.additions.unwrap_or(0) as u64,
                deletions: detail.deletions.unwrap_or(0) as u64,
                draft: pr.draft.unwrap_or(false),
                branch: pr.head.label.clone().unwrap_or_default(),
            });
        }
    }

    // Sort: oldest first (most urgent)
    my_prs.sort_by_key(|r| r.created_at);
    Ok(my_prs)
}

use serde::Deserialize;

#[derive(Debug, Clone, Deserialize)]
pub struct PullRequestFile {
    pub filename: String,
    pub status: String,
    pub additions: u64,
    pub deletions: u64,
}

pub async fn fetch_pr_files(
    token: &str,
    org: &str,
    repo: &str,
    pr_number: u64,
) -> Result<Vec<PullRequestFile>> {
    let client = Octocrab::builder()
        .personal_token(token.to_string())
        .build()?;

    let files: Vec<PullRequestFile> = client
        .pulls(org, repo)
        .list_files(pr_number)
        .await?
        .into_iter()
        .map(|f| {
            let status_str = match f.status {
                octocrab::models::repos::DiffEntryStatus::Added => "added",
                octocrab::models::repos::DiffEntryStatus::Removed => "removed",
                octocrab::models::repos::DiffEntryStatus::Modified => "modified",
                octocrab::models::repos::DiffEntryStatus::Renamed => "renamed",
                octocrab::models::repos::DiffEntryStatus::Copied => "copied",
                octocrab::models::repos::DiffEntryStatus::Changed => "changed",
                octocrab::models::repos::DiffEntryStatus::Unchanged => "unchanged",
                _ => "unknown",
            };
            PullRequestFile {
                filename: f.filename,
                status: status_str.to_string(),
                additions: f.additions,
                deletions: f.deletions,
            }
        })
        .collect();

    Ok(files)
}

#[derive(Debug, Clone, Serialize)]
pub struct PullRequestLabel {
    pub id: String,
    pub name: String,
    pub color: String,
    pub description: Option<String>,
}

pub async fn fetch_pr_labels(
    token: &str,
    org: &str,
    repo: &str,
    pr_number: u64,
) -> Result<Vec<PullRequestLabel>> {
    let client = Octocrab::builder()
        .personal_token(token.to_string())
        .build()?;

    let labels: Vec<PullRequestLabel> = client
        .pulls(org, repo)
        .get(pr_number)
        .await?
        .labels
        .unwrap_or_default()
        .into_iter()
        .map(|l| PullRequestLabel {
            id: l.id.to_string(),
            name: l.name,
            color: l.color,
            description: l.description,
        })
        .collect();

    Ok(labels)
}

/// Represents a single file's diff with metadata.
#[derive(Debug, Clone)]
pub struct PullRequestDiff {
    pub filename: String,
    pub status: String, // "added", "removed", "modified", "renamed"
    pub additions: u64,
    pub deletions: u64,
    pub patch: Option<String>,
    pub language: Option<String>,
}

/// Represents merge conflict status for a PR
#[derive(Debug, Clone, Serialize)]
pub struct MergeConflictStatus {
    pub repo: String,
    pub pr_number: u64,
    pub pr_title: String,
    pub has_conflicts: bool,
    pub mergeable: Option<bool>,
    pub rebaseable: Option<bool>,
}

/// Represents CI check run status for a commit
#[derive(Debug, Clone, Serialize)]
pub struct CiStatus {
    pub repo: String,
    pub pr_number: u64,
    pub pr_title: String,
    pub head_sha: String,
    pub overall_status: String, // "success", "failure", "pending", "error", "cancelled"
    pub checks: Vec<CiCheck>,
}

/// Individual CI check within a commit
#[derive(Debug, Clone, Serialize)]
pub struct CiCheck {
    pub name: String,
    pub status: String,       // "completed", "in_progress", "queued", "pending", "waiting", "requested"
    pub conclusion: Option<String>, // "success", "failure", "neutral", "cancelled", "skipped", "timed_out", "action_required"
    pub app_name: String,     // "GitHub Actions", "CircleCI", "Jenkins", etc.
    pub started_at: Option<String>,
    pub completed_at: Option<String>,
}

pub async fn fetch_pr_diff(
    token: &str,
    org: &str,
    repo: &str,
    pr_number: u64,
) -> Result<Vec<PullRequestDiff>> {
    let client = Octocrab::builder()
        .personal_token(token.to_string())
        .build()?;

    let files: Vec<PullRequestDiff> = client
        .pulls(org, repo)
        .list_files(pr_number)
        .await?
        .into_iter()
        .map(|f| {
            let status_str = match f.status {
                octocrab::models::repos::DiffEntryStatus::Added => "added",
                octocrab::models::repos::DiffEntryStatus::Removed => "removed",
                octocrab::models::repos::DiffEntryStatus::Modified => "modified",
                octocrab::models::repos::DiffEntryStatus::Renamed => "renamed",
                octocrab::models::repos::DiffEntryStatus::Copied => "copied",
                octocrab::models::repos::DiffEntryStatus::Changed => "changed",
                octocrab::models::repos::DiffEntryStatus::Unchanged => "unchanged",
                _ => "unknown",
            };
            // Extract language from filename extension
            let language = f.filename.split('.').last().map(|ext| {
                match ext {
                    "ts" | "tsx" => "typescript",
                    "js" | "jsx" | "mjs" | "cjs" => "javascript",
                    "py" => "python",
                    "go" => "go",
                    "java" => "java",
                    "rb" => "ruby",
                    "cs" => "csharp",
                    "cpp" | "cc" | "cxx" => "cpp",
                    "c" | "h" => "c",
                    "swift" => "swift",
                    "kt" | "kts" => "kotlin",
                    "scala" => "scala",
                    "rs" => "rust",
                    "php" => "php",
                    "ex" | "exs" => "elixir",
                    "erl" => "erlang",
                    "hs" => "haskell",
                    "ml" | "mli" => "ocaml",
                    "fs" | "fsx" => "fsharp",
                    "lua" => "lua",
                    "r" => "r",
                    "sql" => "sql",
                    "sh" | "bash" | "zsh" => "bash",
                    "ps1" => "powershell",
                    "yml" | "yaml" => "yaml",
                    "toml" => "toml",
                    "json" => "json",
                    "xml" => "xml",
                    "html" | "htm" => "html",
                    "css" | "scss" | "sass" | "less" => "css",
                    "md" | "markdown" => "markdown",
                    "dockerfile" => "dockerfile",
                    "tf" => "hcl",
                    "proto" => "protobuf",
                    _ => ext,
                }.to_string()
            });
            PullRequestDiff {
                filename: f.filename,
                status: status_str.to_string(),
                additions: f.additions,
                deletions: f.deletions,
                patch: f.patch,
                language,
            }
        })
        .collect();

    Ok(files)
}

/// Represents a review event from GitHub history
#[derive(Debug, Clone, serde::Serialize)]
pub struct ReviewActivity {
    pub repo: String,
    pub pr_number: u64,
    pub pr_title: String,
    pub author: String,
    pub reviewed_at: chrono::DateTime<chrono::Utc>,
    pub state: String, // "APPROVED", "CHANGES_REQUESTED", "COMMENTED"
}

pub async fn fetch_my_review_activity(
    token: &str,
    org: &str,
    repos: &[String],
    username: &str,
    days: u32,
) -> Result<Vec<ReviewActivity>> {
    let client = Octocrab::builder()
        .personal_token(token.to_string())
        .build()?;

    let since = chrono::Utc::now() - chrono::Duration::days(days as i64);
    let mut all_reviews = vec![];

    for repo in repos {
        // List all PRs (open + closed) and check if the user reviewed them
        let prs = client
            .pulls(org, repo)
            .list()
            .state(octocrab::params::State::All)
            .per_page(100)
            .send()
            .await?;

        for pr in prs.items {
            // Check if this PR was updated within our window
            let updated_at = pr.updated_at
                .unwrap_or_else(|| pr.created_at.unwrap_or_else(chrono::Utc::now));
            if updated_at < since {
                continue;
            }

            // Get the timeline (includes reviews) for this PR
            let timeline: Vec<serde_json::Value> = client
                .get(
                    format!(
                        "/repos/{}/{}/issues/{}/timeline",
                        org, repo, pr.number
                    ),
                    None::<&str>,
                )
                .await
                .unwrap_or_default();

            for event in timeline {
                // Look for "PullRequestReview" events by our username
                if event.get("event").and_then(|e| e.as_str()) == Some("pull_request_review") {
                    if let Some(user_obj) = event.get("user") {
                        if user_obj.get("login").and_then(|l| l.as_str()) == Some(username) {
                            let reviewed_at = event
                                .get("submitted_at")
                                .and_then(|t| t.as_str())
                                .and_then(|s| chrono::DateTime::parse_from_rfc3339(s).ok())
                                .map(|dt| dt.with_timezone(&chrono::Utc))
                                .unwrap_or(updated_at);

                            let state = event
                                .get("state")
                                .and_then(|s| s.as_str())
                                .unwrap_or("COMMENTED")
                                .to_string();

                            let author = pr.user.as_ref()
                                .map(|u| u.login.clone())
                                .unwrap_or_default();

                            all_reviews.push(ReviewActivity {
                                repo: repo.clone(),
                                pr_number: pr.number,
                                pr_title: pr.title.clone().unwrap_or_default(),
                                author,
                                reviewed_at,
                                state,
                            });
                            break; // one review per user per PR
                        }
                    }
                }
            }
        }
    }

    // Sort by most recent first
    all_reviews.sort_by(|a, b| b.reviewed_at.cmp(&a.reviewed_at));
    Ok(all_reviews)
}

/// Fetch merge conflict status for a list of pending reviews
pub async fn fetch_merge_conflict_status(
    token: &str,
    org: &str,
    reviews: &[PendingReview],
) -> Result<Vec<MergeConflictStatus>> {
    let client = Octocrab::builder()
        .personal_token(token.to_string())
        .build()?;

    let mut results = Vec::new();

    // Fetch PR details to get mergeable status
    // Group by repo to minimize API calls per repo
    use std::collections::HashMap;
    let mut by_repo: HashMap<String, Vec<u64>> = HashMap::new();
    for review in reviews {
        by_repo.entry(review.repo.clone())
            .or_insert_with(Vec::new)
            .push(review.pr_number);
    }

    for (repo, pr_numbers) in by_repo {
        for pr_number in pr_numbers {
            match client.pulls(org, &repo).get(pr_number).await {
                Ok(pr) => {
                    let has_conflicts = pr.mergeable == Some(false);
                    let mergeable_status = MergeConflictStatus {
                        repo: repo.clone(),
                        pr_number,
                        pr_title: pr.title.unwrap_or_default(),
                        has_conflicts,
                        mergeable: pr.mergeable,
                        rebaseable: pr.rebaseable,
                    };
                    results.push(mergeable_status);
                }
                Err(e) => {
                    // Log but don't fail - just skip this PR
                    eprintln!("Warning: Failed to fetch PR #{} in {}: {}", pr_number, repo, e);
                }
            }
        }
    }

    Ok(results)
}

/// Fetch CI (GitHub Actions / checks) status for a list of pending reviews
pub async fn fetch_ci_status(
    token: &str,
    org: &str,
    reviews: &[PendingReview],
) -> Result<Vec<CiStatus>> {
    let client = Octocrab::builder()
        .personal_token(token.to_string())
        .build()?;

    let mut results = Vec::new();

    // Group by repo to fetch combined status per repo
    use std::collections::HashMap;
    let mut by_repo: HashMap<String, Vec<&PendingReview>> = HashMap::new();
    for review in reviews {
        by_repo.entry(review.repo.clone())
            .or_insert_with(Vec::new)
            .push(review);
    }

    for (repo, reviews_in_repo) in by_repo {
        // Get the head SHAs for all PRs in this repo
        for review in reviews_in_repo {
            match client.pulls(org, &repo).get(review.pr_number).await {
                Ok(pr) => {
                    let head_sha = pr.head.sha.clone();
                    let pr_title = pr.title.clone().unwrap_or_default();

                    // Fetch combined status for this commit via GitHub status API
                    #[derive(serde::Deserialize)]
                    struct CombinedStatus {
                        state: String,
                    }

                    let combined_status_url = format!(
                        "/repos/{}/{}/commits/{}/status",
                        org, repo, head_sha
                    );

                    let overall_status: String = client
                        .get(&combined_status_url, None::<&str>)
                        .await
                        .map(|s: CombinedStatus| s.state)
                        .unwrap_or_else(|_| "unknown".to_string());

                    // Fetch check runs (GitHub Actions, etc.) via check-runs endpoint
                    #[derive(serde::Deserialize)]
                    struct CheckRunsResponse {
                        total_count: u32,
                        check_runs: Vec<CheckRunDto>,
                    }

                    #[derive(serde::Deserialize)]
                    #[serde(rename_all = "camelCase")]
                    struct CheckRunDto {
                        name: String,
                        status: String,
                        conclusion: Option<String>,
                        app_name: String,
                        started_at: Option<String>,
                        completed_at: Option<String>,
                    }

                    let check_runs_url = format!(
                        "/repos/{}/{}/commits/{}/check-runs",
                        org, repo, head_sha
                    );

                    let checks: Vec<CiCheck> = client
                        .get(&check_runs_url, None::<&str>)
                        .await
                        .map(|response: CheckRunsResponse| {
                            response.check_runs.into_iter().map(|cr| {
                                CiCheck {
                                    name: cr.name,
                                    status: cr.status,
                                    conclusion: cr.conclusion,
                                    app_name: cr.app_name,
                                    started_at: cr.started_at,
                                    completed_at: cr.completed_at,
                                }
                            }).collect()
                        })
                        .unwrap_or_default();

                    results.push(CiStatus {
                        repo: repo.clone(),
                        pr_number: review.pr_number,
                        pr_title,
                        head_sha,
                        overall_status,
                        checks,
                    });
                }
                Err(e) => {
                    eprintln!("Warning: Failed to fetch PR #{} in {}: {}", review.pr_number, repo, e);
                }
            }
        }
    }

    Ok(results)
}

/// Represents a GitHub notification/mention for the current user.
#[derive(Debug, Clone, serde::Serialize)]
pub struct Mention {
    /// Repository name (e.g. "org/repo")
    pub repo: String,
    /// PR or issue number
    pub pr_number: u64,
    /// PR or issue title
    pub pr_title: String,
    /// URL to the PR/issue
    pub pr_url: String,
    /// Whether this notification is unread
    pub unread: bool,
    /// Why the user is receiving this notification (mention, review_requested, etc.)
    pub reason: String,
    /// When the notification was last updated
    pub updated_at: chrono::DateTime<chrono::Utc>,
    /// Preview of the last comment that triggered the mention (if available)
    pub last_comment_preview: String,
}

/// Fetch GitHub notifications where the user was mentioned or directly involved.
pub async fn fetch_mentions(
    token: &str,
    _username: &str,
    unread_only: bool,
    limit: usize,
) -> Result<Vec<Mention>> {
    let client = Octocrab::builder()
        .personal_token(token.to_string())
        .build()?;

    // Use GitHub's notifications API
    #[derive(serde::Deserialize)]
    struct Notification {
        id: String,
        unread: bool,
        reason: String,
        updated_at: String,
        #[serde(rename = "subject")]
        subject: NotificationSubject,
        repository: NotificationRepo,
    }

    #[derive(serde::Deserialize)]
    struct NotificationSubject {
        title: String,
        #[serde(rename = "type")]
        notification_type: String,
        url: Option<String>,
    }

    #[derive(serde::Deserialize)]
    struct NotificationRepo {
        full_name: String,
    }

    let all_notifications: Vec<Notification> = client
        .get("notifications", Some(&[("per_page", "50")]))
        .await
        .unwrap_or_default();

    let mut mentions = Vec::new();

    for notification in all_notifications {
        // Filter: only PR/issue notifications (not repository, discussions, etc.)
        if notification.subject.notification_type != "PullRequest"
            && notification.subject.notification_type != "Issue" {
            continue;
        }

        // Filter: only if it involves this user (mention, review_requested, assign, author, team_mention)
        let relevant_reasons = ["mention", "review_requested", "assign", "author", "team_mention", "cm"];
        if !relevant_reasons.contains(&notification.reason.as_str()) {
            continue;
        }

        // Filter: unread only
        if unread_only && !notification.unread {
            continue;
        }

        // Parse the PR number from the subject URL
        // URL format: https://api.github.com/repos/{org}/{repo}/pulls/{number}
        let pr_number: u64 = notification
            .subject
            .url
            .as_ref()
            .and_then(|url| {
                url.split('/')
                    .last()
                    .and_then(|s| s.parse().ok())
            })
            .unwrap_or(0);

        // Convert API URL to web URL
        let pr_url = notification
            .subject
            .url
            .as_ref()
            .map(|url| {
                url.replace("https://api.github.com/repos/", "https://github.com/")
                    .replace("/pulls/", "/pull/")
            })
            .unwrap_or_else(|| {
                format!(
                    "https://github.com/{}/pull/{}",
                    notification.repository.full_name, pr_number
                )
            });

        // Parse updated_at
        let updated_at = chrono::DateTime::parse_from_rfc3339(&notification.updated_at)
            .map(|dt| dt.with_timezone(&chrono::Utc))
            .unwrap_or_else(|_| chrono::Utc::now());

        mentions.push(Mention {
            repo: notification.repository.full_name,
            pr_number,
            pr_title: notification.subject.title,
            pr_url,
            unread: notification.unread,
            reason: notification.reason,
            updated_at,
            last_comment_preview: String::new(),
        });

        if mentions.len() >= limit {
            break;
        }
    }

    // Sort by most recently updated
    mentions.sort_by(|a, b| b.updated_at.cmp(&a.updated_at));

    Ok(mentions)
}

/// Represents GitHub API rate limit information.
#[derive(Debug, Clone, serde::Serialize)]
pub struct RateLimitInfo {
    /// Resource type (e.g., "core", "search", "graphql")
    pub resource: String,
    /// Current limit
    pub limit: u32,
    /// Remaining requests in current window
    pub remaining: u32,
    /// Reset timestamp
    pub reset: chrono::DateTime<chrono::Utc>,
    /// Seconds until reset
    pub reset_in_seconds: i64,
}

/// Represents overall GitHub API health status.
#[derive(Debug, Clone, serde::Serialize)]
pub struct HealthStatus {
    /// API rate limits
    pub rate_limits: Vec<RateLimitInfo>,
    /// GitHub API server current time
    pub server_time: chrono::DateTime<chrono::Utc>,
    /// Whether the authenticated user has a valid token
    pub authenticated: bool,
    /// GitHub username (if authenticated)
    pub username: Option<String>,
    /// Whether the rate limit is being hit (remaining < 10% of limit)
    pub rate_limit_warning: bool,
}

/// Fetch GitHub API rate limits and health status.
pub async fn fetch_health_status(token: &str) -> Result<HealthStatus> {
    let client = octocrab::Octocrab::builder()
        .personal_token(token.to_string())
        .build()?;

    #[derive(serde::Deserialize)]
    struct RateLimitResponse {
        resources: RateResources,
        rate: RateLimitEntry,
    }

    #[derive(serde::Deserialize)]
    struct RateResources {
        #[serde(rename = "core")]
        core: RateLimitEntry,
        #[serde(rename = "search")]
        search: RateLimitEntry,
        #[serde(rename = "graphql")]
        graphql: Option<RateLimitEntry>,
    }

    #[derive(serde::Deserialize)]
    struct RateLimitEntry {
        limit: u32,
        remaining: u32,
        reset: u64,
        #[serde(rename = "used")]
        used: u32,
        #[serde(rename = "resource")]
        resource: Option<String>,
    }

    let rate_limits: Vec<RateLimitInfo> = client
        .get("rate_limit", None::<&str>)
        .await
        .map(|response: RateLimitResponse| {
            let now = chrono::Utc::now();
            let mut limits = vec![];

            // Core API
            let reset_core = chrono::DateTime::from_timestamp(response.rate.reset as i64, 0)
                .unwrap_or(now);
            limits.push(RateLimitInfo {
                resource: "core".to_string(),
                limit: response.rate.limit,
                remaining: response.rate.remaining,
                reset: reset_core,
                reset_in_seconds: (reset_core - now).num_seconds(),
            });

            // Search API
            let reset_search = chrono::DateTime::from_timestamp(response.resources.search.reset as i64, 0)
                .unwrap_or(now);
            limits.push(RateLimitInfo {
                resource: "search".to_string(),
                limit: response.resources.search.limit,
                remaining: response.resources.search.remaining,
                reset: reset_search,
                reset_in_seconds: (reset_search - now).num_seconds(),
            });

            // GraphQL (optional)
            if let Some(graphql) = response.resources.graphql {
                let reset_graphql = chrono::DateTime::from_timestamp(graphql.reset as i64, 0)
                    .unwrap_or(now);
                limits.push(RateLimitInfo {
                    resource: "graphql".to_string(),
                    limit: graphql.limit,
                    remaining: graphql.remaining,
                    reset: reset_graphql,
                    reset_in_seconds: (reset_graphql - now).num_seconds(),
                });
            }

            limits
        })
        .unwrap_or_default();

    // Try to get authenticated user info
    #[derive(serde::Deserialize)]
    struct UserResponse {
        login: String,
    }

    let (authenticated, username) = match client.get::<UserResponse, _, _>("user", None::<&str>).await {
        Ok(user) => (true, Some(user.login)),
        Err(_) => (false, None),
    };

    let server_time = chrono::Utc::now();

    // Check if any rate limit is critically low (< 10%)
    let rate_limit_warning = rate_limits.iter().any(|r| {
        r.limit > 0 && (r.remaining as f64 / r.limit as f64) < 0.1
    });

    Ok(HealthStatus {
        rate_limits,
        server_time,
        authenticated,
        username,
        rate_limit_warning,
    })
}

/// Represents a single event in a PR's timeline.
#[derive(Debug, Clone, serde::Serialize)]
pub struct TimelineEvent {
    /// Event type: "PullRequestReview", "Comment", "LabelEvent", "AssignEvent", "MilestoneEvent", "ClosedEvent", "ReopenedEvent", "MergedEvent", etc.
    pub event: String,
    /// When this event occurred
    pub created_at: chrono::DateTime<chrono::Utc>,
    /// Actor (user) who triggered this event
    pub actor: Option<String>,
    /// Event-specific data (e.g., review state, comment body preview)
    pub data: serde_json::Value,
}

/// Fetch the timeline of events for a specific PR.
pub async fn fetch_pr_timeline(
    token: &str,
    org: &str,
    repo: &str,
    pr_number: u64,
) -> Result<Vec<TimelineEvent>> {
    let client = Octocrab::builder()
        .personal_token(token.to_string())
        .build()?;

    let timeline_url = format!(
        "/repos/{}/{}/issues/{}/timeline",
        org, repo, pr_number
    );

    let events: Vec<serde_json::Value> = client
        .get(&timeline_url, Some(&[("per_page", "100")]))
        .await
        .unwrap_or_default();

    let mut timeline = Vec::new();

    for event in events {
        let event_type = event.get("event")
            .and_then(|e| e.as_str())
            .unwrap_or("unknown")
            .to_string();

        let created_at = event.get("created_at")
            .and_then(|t| t.as_str())
            .and_then(|s| chrono::DateTime::parse_from_rfc3339(s).ok())
            .map(|dt| dt.with_timezone(&chrono::Utc))
            .unwrap_or_else(chrono::Utc::now);

        let actor = event.get("actor")
            .or_else(|| event.get("user"))
            .and_then(|a| a.get("login"))
            .and_then(|l| l.as_str())
            .map(String::from);

        // Extract event-specific data
        let mut data = serde_json::json!({});

        match event_type.as_str() {
            "PullRequestReview" => {
                if let Some(state) = event.get("state").and_then(|s| s.as_str()) {
                    data["review_state"] = serde_json::json!(state);
                }
                if let Some(body) = event.get("body").and_then(|b| b.as_str()) {
                    let preview: String = body.chars().take(200).collect();
                    data["body_preview"] = serde_json::json!(preview);
                }
            }
            "Comment" | "IssueComment" => {
                if let Some(body) = event.get("body").and_then(|b| b.as_str()) {
                    let preview: String = body.chars().take(200).collect();
                    data["body_preview"] = serde_json::json!(preview);
                }
            }
            "labeled" => {
                if let Some(label) = event.get("label").and_then(|l| l.get("name")) {
                    data["label"] = label.clone();
                }
            }
            "unlabeled" => {
                if let Some(label) = event.get("label").and_then(|l| l.get("name")) {
                    data["label"] = label.clone();
                }
            }
            "assigned" | "unassigned" => {
                if let Some(assignee) = event.get("assignee").and_then(|a| a.get("login")) {
                    data["assignee"] = assignee.clone();
                }
            }
            "merged" => {
                if let Some(merge_commit_sha) = event.get("merge_commit_sha") {
                    data["merge_commit_sha"] = merge_commit_sha.clone();
                }
            }
            "closed" => {
                if let Some(merged) = event.get("merged") {
                    data["merged"] = merged.clone();
                }
            }
            _ => {
                // For other event types, include relevant fields
                if let Some(label) = event.get("label").and_then(|l| l.get("name")) {
                    data["label"] = label.clone();
                }
            }
        }

        timeline.push(TimelineEvent {
            event: event_type,
            created_at,
            actor,
            data,
        });
    }

    // Timeline API returns events newest-first, but we want chronological order
    timeline.reverse();

    Ok(timeline)
}

/// Send an emoji reaction to a PR/issue to get author's attention without leaving a comment.
/// 
/// GitHub supports these reaction emojis:
/// - `+1` (thumbs up)
/// - `-1` (thumbs down)
/// - `laugh` (😂)
/// - `confused` (😕)
/// - `heart` (❤️)
/// - `hooray` (🎉)
/// - `rocket` (🚀)
/// - `eyes` (👀)
pub async fn add_pr_reaction(
    token: &str,
    org: &str,
    repo: &str,
    pr_number: u64,
    reaction: &str,
) -> Result<()> {
    let client = octocrab::Octocrab::builder()
        .personal_token(token.to_string())
        .build()?;

    // Map friendly names to GitHub content values
    let content = match reaction {
        "+1" | "thumbsup" | "thumbs_up" => "+1",
        "-1" | "thumbsdown" | "thumbs_down" => "-1",
        "laugh" | "laughed" | "lol" => "laugh",
        "confused" | "unsure" => "confused",
        "heart" | "love" => "heart",
        "hooray" | "party" | "celebrate" => "hooray",
        "rocket" | "rockets" => "rocket",
        "eyes" | "looking" | "👀" => "eyes",
        _ => reaction, // Use as-is if it doesn't match
    };

    let url = format!(
        "/repos/{}/{}/issues/{}/reactions",
        org, repo, pr_number
    );

    #[derive(serde::Serialize)]
    struct ReactionPayload {
        content: String,
    }

    let _: serde_json::Value = client
        .post(&url, Some(&ReactionPayload { content: content.to_string() })) // Uses POST /repos/{owner}/{repo}/issues/{issue_number}/reactions
        .await?;

    Ok(())
}