gor-cli 0.1.0

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

#![allow(clippy::print_stdout, clippy::print_stderr)]

use crate::cli::PrCommand;
use crate::client::Client;
use crate::output::{format_date, print_json};
use crate::repository::{detect_remote, parse_repo_spec};
use anyhow::Context;
use gix::bstr::ByteSlice;
use std::collections::BTreeMap;
use std::io::Write;

/// Run the `gor pr` subcommand.
///
/// # Errors
///
/// Returns an error if the command execution fails.
pub fn run(cmd: PrCommand) -> anyhow::Result<()> {
    match cmd {
        PrCommand::List {
            owner_repo,
            state,
            base,
            head,
            author,
            labels,
            assignee,
            limit,
            web,
            json,
            hostname,
        } => list(
            owner_repo,
            &state,
            base.as_deref(),
            head.as_deref(),
            author.as_deref(),
            &labels,
            assignee.as_deref(),
            limit,
            web,
            json,
            hostname.as_deref(),
        ),
        PrCommand::View {
            number,
            repo,
            web,
            comments,
            json,
            hostname,
        } => view(
            number,
            repo.as_deref(),
            web,
            comments,
            json,
            hostname.as_deref(),
        ),
        PrCommand::Create {
            repo,
            title,
            body,
            base,
            head,
            draft,
            labels,
            assignee,
            milestone,
            project,
            web,
            hostname,
        } => create(
            repo.as_deref(),
            title.as_deref(),
            body.as_deref(),
            base.as_deref(),
            head.as_deref(),
            draft,
            &labels,
            &assignee,
            milestone.as_deref(),
            project,
            web,
            hostname.as_deref(),
        ),
        PrCommand::Close {
            number,
            repo,
            comment,
            hostname,
        } => close(
            number,
            repo.as_deref(),
            comment.as_deref(),
            hostname.as_deref(),
        ),
        PrCommand::Reopen {
            number,
            repo,
            comment,
            hostname,
        } => reopen(
            number,
            repo.as_deref(),
            comment.as_deref(),
            hostname.as_deref(),
        ),
        PrCommand::Comment {
            number,
            repo,
            body,
            body_file,
            web,
            hostname,
        } => pr_comment(
            number,
            repo.as_deref(),
            body.as_deref(),
            body_file.as_deref(),
            web,
            hostname.as_deref(),
        ),
        PrCommand::Merge {
            number,
            repo,
            merge,
            squash,
            rebase,
            body,
            subject,
            delete_branch,
            admin,
            auto,
            hostname,
        } => pr_merge(
            number,
            repo.as_deref(),
            merge,
            squash,
            rebase,
            body.as_deref(),
            subject.as_deref(),
            delete_branch,
            admin,
            auto,
            hostname.as_deref(),
        ),
        PrCommand::Checkout {
            number,
            repo,
            branch,
            recurse_submodules,
            hostname,
        } => pr_checkout(
            number,
            repo.as_deref(),
            branch.as_deref(),
            recurse_submodules,
            hostname.as_deref(),
        ),
        PrCommand::Diff {
            number,
            repo,
            color,
            name_only,
            hostname,
        } => diff(
            number,
            repo.as_deref(),
            &color,
            name_only,
            hostname.as_deref(),
        ),
        PrCommand::Edit {
            number,
            repo,
            title,
            body,
            base,
            add_label,
            remove_label,
            add_assignee,
            remove_assignee,
            milestone,
            hostname,
        } => pr_edit(
            number,
            repo.as_deref(),
            title.as_deref(),
            body.as_deref(),
            base.as_deref(),
            &add_label,
            &remove_label,
            &add_assignee,
            &remove_assignee,
            milestone.as_deref(),
            hostname.as_deref(),
        ),
        PrCommand::Review {
            number,
            repo,
            approve,
            request_changes,
            comment,
            body,
            hostname,
        } => review(
            number,
            repo.as_deref(),
            approve,
            request_changes,
            comment,
            body.as_deref(),
            hostname.as_deref(),
        ),
        PrCommand::Checks {
            number,
            repo,
            watch,
            json,
            hostname,
        } => checks(number, repo.as_deref(), watch, json, hostname.as_deref()),
        PrCommand::Ready {
            number,
            repo,
            hostname,
        } => ready(number, repo.as_deref(), hostname.as_deref()),
    }
}

/// Execute `gor pr list`.
///
/// Lists pull requests for a repository with filtering by state, base branch,
/// head branch, author, labels, and assignee. Supports table output, JSON
/// output, and opening the PR list in a browser.
///
/// # Errors
///
/// Returns an error if the repository cannot be found or the API request fails.
#[allow(clippy::too_many_arguments)]
fn list(
    owner_repo: Option<String>,
    state: &str,
    base: Option<&str>,
    head: Option<&str>,
    author: Option<&str>,
    labels: &[String],
    assignee: Option<&str>,
    limit: u32,
    web: bool,
    json: Option<Vec<String>>,
    hostname: Option<&str>,
) -> anyhow::Result<()> {
    // Resolve the repo spec
    let spec = match owner_repo {
        Some(s) => parse_repo_spec(&s).context("invalid repository spec")?,
        None => detect_remote().ok_or_else(|| {
            anyhow::anyhow!(
                "could not detect repository from current directory; specify OWNER/REPO"
            )
        })?,
    };

    let host = hostname.unwrap_or("github.com");

    // Handle --web flag: open in browser
    if web {
        let web_url = format!("https://{host}/{}/{}/pulls", spec.owner, spec.repo);
        open_in_browser(&web_url);
        return Ok(());
    }

    let client = Client::new(host).context("failed to create HTTP client")?;

    // Build query parameters for the API call
    // The GitHub API doesn't support "merged" as a state value; we use "all"
    // and filter client-side for merged PRs.
    let needs_merged_filter = state == "merged";
    let api_state = if needs_merged_filter { "all" } else { state };

    let mut query_params = vec![
        ("state", api_state.to_string()),
        ("per_page", limit.min(100).to_string()),
    ];

    if let Some(b) = base {
        query_params.push(("base", (*b).to_string()));
    }
    if let Some(h) = head {
        query_params.push(("head", (*h).to_string()));
    }

    let query_string = query_params
        .iter()
        .map(|(k, v)| format!("{k}={v}"))
        .collect::<Vec<_>>()
        .join("&");

    let path = format!("/repos/{}/{}/pulls?{query_string}", spec.owner, spec.repo);

    let response = client.get(&path).context("failed to fetch pull requests")?;

    let status = response.status();
    if status == reqwest::StatusCode::NOT_FOUND {
        anyhow::bail!("repository '{spec}' not found");
    }
    if status == reqwest::StatusCode::UNAUTHORIZED || status == reqwest::StatusCode::FORBIDDEN {
        anyhow::bail!("authentication required to list pull requests for '{spec}'");
    }
    if !status.is_success() {
        anyhow::bail!("failed to list pull requests for '{spec}': HTTP {status}");
    }

    let mut prs: Vec<serde_json::Value> = response
        .json()
        .context("failed to parse pull request response")?;

    // Client-side filtering
    if needs_merged_filter {
        prs.retain(|pr| pr["merged_at"].as_str().is_some());
    }
    if let Some(a) = author {
        prs.retain(|pr| {
            pr["user"]["login"]
                .as_str()
                .is_some_and(|login| login.eq_ignore_ascii_case(a))
        });
    }
    if !labels.is_empty() {
        prs.retain(|pr| {
            let pr_labels: Vec<&str> = pr["labels"]
                .as_array()
                .map(|arr| arr.iter().filter_map(|l| l["name"].as_str()).collect())
                .unwrap_or_default();
            labels
                .iter()
                .all(|label| pr_labels.iter().any(|l| l.eq_ignore_ascii_case(label)))
        });
    }
    if let Some(a) = assignee {
        prs.retain(|pr| {
            pr["assignees"].as_array().is_some_and(|arr| {
                arr.iter().any(|assignee| {
                    assignee["login"]
                        .as_str()
                        .is_some_and(|login| login.eq_ignore_ascii_case(a))
                })
            })
        });
    }

    // Handle --json flag
    if let Some(fields) = json {
        let fields_ref: Option<&[String]> = if fields.is_empty() {
            None
        } else {
            Some(&fields)
        };
        print_json(&prs, fields_ref);
        return Ok(());
    }

    // Default: print formatted table
    print_pr_table(&prs);
    Ok(())
}

/// Execute `gor pr view`.
///
/// Displays the full details of a single pull request, including title, body,
/// author, state, branch information, labels, review status, merge status, and
/// CI check status. Supports JSON output and opening the PR in a browser.
///
/// # Errors
///
/// Returns an error if the repository cannot be found, the PR does not exist,
/// or the API request fails.
#[allow(clippy::too_many_arguments)]
fn view(
    number: u64,
    repo: Option<&str>,
    web: bool,
    comments: bool,
    json: Option<Vec<String>>,
    hostname: Option<&str>,
) -> anyhow::Result<()> {
    // Resolve the repo spec
    let spec = match repo {
        Some(s) => parse_repo_spec(s).context("invalid repository spec")?,
        None => detect_remote().ok_or_else(|| {
            anyhow::anyhow!(
                "could not detect repository from current directory; specify OWNER/REPO"
            )
        })?,
    };

    let host = hostname.unwrap_or("github.com");

    // Handle --web flag: open in browser
    if web {
        let web_url = format!("https://{host}/{}/{}/pull/{number}", spec.owner, spec.repo);
        open_in_browser(&web_url);
        return Ok(());
    }

    let client = Client::new(host).context("failed to create HTTP client")?;

    // Fetch the PR details
    let path = format!("/repos/{}/{}/pulls/{number}", spec.owner, spec.repo);
    let response = client.get(&path).context("failed to fetch pull request")?;

    let status = response.status();
    if status == reqwest::StatusCode::NOT_FOUND {
        anyhow::bail!("pull request #{number} not found in '{spec}'");
    }
    if status == reqwest::StatusCode::UNAUTHORIZED || status == reqwest::StatusCode::FORBIDDEN {
        anyhow::bail!("authentication required to view pull request #{number}");
    }
    if !status.is_success() {
        anyhow::bail!("failed to view pull request #{number}: HTTP {status}");
    }

    let pr: serde_json::Value = response
        .json()
        .context("failed to parse pull request response")?;

    // Handle --json flag
    if let Some(fields) = json {
        let fields_ref: Option<&[String]> = if fields.is_empty() {
            None
        } else {
            Some(&fields)
        };
        print_json(&pr, fields_ref);
        return Ok(());
    }

    // Fetch reviews for review status
    let reviews_path = format!("/repos/{}/{}/pulls/{number}/reviews", spec.owner, spec.repo);
    let reviews: Vec<serde_json::Value> = client
        .get(&reviews_path)
        .ok()
        .and_then(|r| r.json().ok())
        .unwrap_or_default();

    // Fetch CI check status
    let head_sha = pr["head"]["sha"].as_str().unwrap_or("");
    let ci_status = if head_sha.is_empty() {
        None
    } else {
        let status_path = format!(
            "/repos/{}/{}/commits/{head_sha}/status",
            spec.owner, spec.repo
        );
        client.get(&status_path).ok().and_then(|r| r.json().ok())
    };

    // Fetch comments if --comments flag is set
    let comments_data = if comments {
        let comments_path = format!(
            "/repos/{}/{}/issues/{number}/comments",
            spec.owner, spec.repo
        );
        client
            .get(&comments_path)
            .ok()
            .and_then(|r| r.json().ok())
            .unwrap_or_default()
    } else {
        Vec::<serde_json::Value>::new()
    };

    // Print formatted view
    print_pr_view(&pr, &reviews, ci_status.as_ref(), &comments_data);
    Ok(())
}

/// Execute `gor pr create`.
///
/// Creates a pull request from the current branch to the base branch.
/// Auto-detects the head branch from the current git branch and the base
/// branch from the repository's default branch. Supports draft PRs, labels,
/// assignees, milestones, and project board assignment.
///
/// # Errors
///
/// Returns an error if the repository cannot be found, the PR creation fails,
/// or required fields are missing.
#[allow(clippy::too_many_arguments)]
fn create(
    repo: Option<&str>,
    title: Option<&str>,
    body: Option<&str>,
    base: Option<&str>,
    head: Option<&str>,
    draft: bool,
    labels: &[String],
    assignees: &[String],
    _milestone: Option<&str>,
    _project: Option<u32>,
    web: bool,
    hostname: Option<&str>,
) -> anyhow::Result<()> {
    // Resolve the repo spec
    let spec = match repo {
        Some(s) => parse_repo_spec(s).context("invalid repository spec")?,
        None => detect_remote().ok_or_else(|| {
            anyhow::anyhow!(
                "could not detect repository from current directory; specify OWNER/REPO with --repo"
            )
        })?,
    };

    let host = hostname.unwrap_or("github.com");
    let client = Client::new(host).context("failed to create HTTP client")?;

    // Auto-detect head branch from current git branch if not specified
    let head_branch = if let Some(b) = head {
        b.to_string()
    } else {
        let repo =
            gix::discover(std::env::current_dir().context("failed to get current directory")?)
                .context("failed to discover git repository")?;
        let head_ref = repo.head().context("failed to get HEAD")?;
        head_ref
            .name()
            .shorten()
            .to_str()
            .context("branch name is not valid UTF-8")?
            .to_string()
    };

    // Auto-detect base branch from repo's default branch if not specified
    let base_branch = if let Some(b) = base {
        b.to_string()
    } else {
        let path = format!("/repos/{}/{}", spec.owner, spec.repo);
        let response = client
            .get(&path)
            .context("failed to fetch repository data")?;
        let status = response.status();
        if !status.is_success() {
            anyhow::bail!("failed to fetch repository '{spec}': HTTP {status}");
        }
        let repo_data: serde_json::Value = response
            .json()
            .context("failed to parse repository response")?;
        repo_data["default_branch"]
            .as_str()
            .ok_or_else(|| anyhow::anyhow!("could not determine default branch"))?
            .to_string()
    };

    // Build the request body
    let mut body_map = serde_json::Map::new();
    body_map.insert(
        "title".to_string(),
        serde_json::Value::String(
            title
                .ok_or_else(|| anyhow::anyhow!("PR title is required; use --title"))?
                .to_string(),
        ),
    );
    body_map.insert("head".to_string(), serde_json::Value::String(head_branch));
    body_map.insert("base".to_string(), serde_json::Value::String(base_branch));
    if let Some(b) = body {
        body_map.insert("body".to_string(), serde_json::Value::String(b.to_string()));
    }
    if draft {
        body_map.insert("draft".to_string(), serde_json::Value::Bool(true));
    }

    let body_value = serde_json::Value::Object(body_map);

    // Create the PR
    let path = format!("/repos/{}/{}/pulls", spec.owner, spec.repo);
    let response = client
        .post(&path, &body_value)
        .context("failed to create pull request")?;

    let status = response.status();
    if status == reqwest::StatusCode::NOT_FOUND {
        anyhow::bail!("repository '{spec}' not found");
    }
    if status == reqwest::StatusCode::UNPROCESSABLE_ENTITY {
        let err_body: serde_json::Value = response.json().unwrap_or_default();
        let msg = err_body["message"].as_str().unwrap_or("validation failed");
        anyhow::bail!("failed to create pull request: {msg}");
    }
    if !status.is_success() {
        anyhow::bail!("failed to create pull request: HTTP {status}");
    }

    let pr: serde_json::Value = response
        .json()
        .context("failed to parse pull request response")?;

    let pr_number = pr["number"].as_u64().unwrap_or(0);
    let pr_url = pr["html_url"].as_str().unwrap_or("");

    // Handle --web flag: open in browser
    if web && !pr_url.is_empty() {
        open_in_browser(pr_url);
    }

    // Print success message
    println!(
        "https://github.com/{}/{}/pull/{pr_number}",
        spec.owner, spec.repo
    );

    // Add labels if specified
    if !labels.is_empty() {
        let labels_path = format!(
            "/repos/{}/{}/issues/{pr_number}/labels",
            spec.owner, spec.repo
        );
        let labels_body = serde_json::json!({"labels": labels});
        if let Err(e) = client.post(&labels_path, &labels_body) {
            eprintln!("Warning: failed to add labels: {e}");
        }
    }

    // Add assignees if specified
    if !assignees.is_empty() {
        let assignees_path = format!(
            "/repos/{}/{}/issues/{pr_number}/assignees",
            spec.owner, spec.repo
        );
        let assignees_body = serde_json::json!({"assignees": assignees});
        if let Err(e) = client.post(&assignees_path, &assignees_body) {
            eprintln!("Warning: failed to add assignees: {e}");
        }
    }

    Ok(())
}

/// Execute `gor pr close`.
///
/// Closes a pull request by its number. Optionally adds a closing comment.
///
/// # Errors
///
/// Returns an error if the repository cannot be found, the PR does not exist,
/// or the API request fails.
fn close(
    number: u64,
    repo: Option<&str>,
    comment: Option<&str>,
    hostname: Option<&str>,
) -> anyhow::Result<()> {
    // Resolve the repo spec
    let spec = match repo {
        Some(s) => parse_repo_spec(s).context("invalid repository spec")?,
        None => detect_remote().ok_or_else(|| {
            anyhow::anyhow!(
                "could not detect repository from current directory; specify OWNER/REPO with --repo"
            )
        })?,
    };

    let host = hostname.unwrap_or("github.com");
    let client = Client::new(host).context("failed to create HTTP client")?;

    // Add a closing comment if specified
    if let Some(body) = comment {
        let comment_path = format!(
            "/repos/{}/{}/issues/{number}/comments",
            spec.owner, spec.repo
        );
        let comment_body = serde_json::json!({"body": body});
        client
            .post(&comment_path, &comment_body)
            .context("failed to add closing comment")?;
    }

    // Close the PR by setting state to "closed"
    let path = format!("/repos/{}/{}/pulls/{number}", spec.owner, spec.repo);
    let body = serde_json::json!({"state": "closed"});
    let response = client
        .request(
            "PATCH",
            &path,
            &[],
            Some(serde_json::to_vec(&body).unwrap_or_default()),
        )
        .context("failed to close pull request")?;

    let status = response.status();
    if status == reqwest::StatusCode::NOT_FOUND {
        anyhow::bail!("pull request #{number} not found in '{spec}'");
    }
    if !status.is_success() {
        anyhow::bail!("failed to close pull request #{number}: HTTP {status}");
    }

    println!("Closed pull request #{number} in {spec}");
    Ok(())
}

/// Execute `gor pr reopen`.
///
/// Reopens a closed pull request by its number. Optionally adds a comment.
///
/// # Errors
///
/// Returns an error if the repository cannot be found, the PR does not exist,
/// or the API request fails.
fn reopen(
    number: u64,
    repo: Option<&str>,
    comment: Option<&str>,
    hostname: Option<&str>,
) -> anyhow::Result<()> {
    // Resolve the repo spec
    let spec = match repo {
        Some(s) => parse_repo_spec(s).context("invalid repository spec")?,
        None => detect_remote().ok_or_else(|| {
            anyhow::anyhow!(
                "could not detect repository from current directory; specify OWNER/REPO with --repo"
            )
        })?,
    };

    let host = hostname.unwrap_or("github.com");
    let client = Client::new(host).context("failed to create HTTP client")?;

    // Add a comment if specified
    if let Some(body) = comment {
        let comment_path = format!(
            "/repos/{}/{}/issues/{number}/comments",
            spec.owner, spec.repo
        );
        let comment_body = serde_json::json!({"body": body});
        client
            .post(&comment_path, &comment_body)
            .context("failed to add comment")?;
    }

    // Reopen the PR by setting state to "open"
    let path = format!("/repos/{}/{}/pulls/{number}", spec.owner, spec.repo);
    let body = serde_json::json!({"state": "open"});
    let response = client
        .request(
            "PATCH",
            &path,
            &[],
            Some(serde_json::to_vec(&body).unwrap_or_default()),
        )
        .context("failed to reopen pull request")?;

    let status = response.status();
    if status == reqwest::StatusCode::NOT_FOUND {
        anyhow::bail!("pull request #{number} not found in '{spec}'");
    }
    if !status.is_success() {
        anyhow::bail!("failed to reopen pull request #{number}: HTTP {status}");
    }

    println!("Reopened pull request #{number} in {spec}");
    Ok(())
}

/// Execute `gor pr comment`.
///
/// Adds a comment to a pull request's conversation thread.
/// Supports markdown body text, reading from a file or stdin, and
/// opening the PR in a browser after commenting.
///
/// # Errors
///
/// Returns an error if the repository cannot be found, the PR does not exist,
/// or the API request fails.
fn pr_comment(
    number: u64,
    repo: Option<&str>,
    body: Option<&str>,
    body_file: Option<&str>,
    web: bool,
    hostname: Option<&str>,
) -> anyhow::Result<()> {
    let spec = match repo {
        Some(s) => parse_repo_spec(s).context("invalid repository spec")?,
        None => detect_remote().ok_or_else(|| {
            anyhow::anyhow!(
                "could not detect repository from current directory; specify OWNER/REPO with --repo"
            )
        })?,
    };

    let host = hostname.unwrap_or("github.com");

    // Handle --web flag: open in browser
    if web {
        let web_url = format!("https://{host}/{}/{}/pull/{number}", spec.owner, spec.repo);
        open_in_browser(&web_url);
        return Ok(());
    }

    // Resolve the comment body
    let comment_body = match (body, body_file) {
        (Some(b), None) => b.to_string(),
        (None, Some(f)) => {
            if f == "@-" {
                let mut buf = String::new();
                std::io::stdin()
                    .read_line(&mut buf)
                    .context("failed to read from stdin")?;
                buf
            } else {
                std::fs::read_to_string(f)
                    .with_context(|| format!("failed to read body file '{f}'"))?
            }
        }
        (None, None) => anyhow::bail!("either --body or --body-file is required"),
        (Some(_), Some(_)) => unreachable!(), // clap conflicts_with prevents this
    };

    let client = Client::new(host).context("failed to create HTTP client")?;

    let path = format!(
        "/repos/{}/{}/issues/{number}/comments",
        spec.owner, spec.repo
    );
    let request_body = serde_json::json!({"body": comment_body});
    let response = client
        .request("POST", &path, &[], Some(serde_json::to_vec(&request_body)?))
        .context("failed to post comment")?;

    let status = response.status();
    if status == reqwest::StatusCode::NOT_FOUND {
        anyhow::bail!("pull request #{number} not found in '{spec}'");
    }
    if !status.is_success() {
        anyhow::bail!("failed to comment on pull request #{number}: HTTP {status}");
    }

    let comment: serde_json::Value = response
        .json()
        .context("failed to parse comment response")?;

    let comment_url = comment["html_url"].as_str().unwrap_or("");
    println!("{comment_url}");

    Ok(())
}

/// Execute `gor pr merge`.
///
/// Merges a pull request into its base branch. Supports merge commit, squash,
/// and rebase strategies. Can delete the head branch after merging, bypass
/// branch protection with admin privileges, and enable auto-merge.
///
/// # Errors
///
/// Returns an error if the repository cannot be found, the PR does not exist,
/// the merge fails, or the API request fails.
#[allow(clippy::too_many_arguments, clippy::fn_params_excessive_bools)]
fn pr_merge(
    number: u64,
    repo: Option<&str>,
    _merge: bool,
    squash: bool,
    rebase: bool,
    body: Option<&str>,
    subject: Option<&str>,
    delete_branch: bool,
    _admin: bool,
    _auto: bool,
    hostname: Option<&str>,
) -> anyhow::Result<()> {
    // Resolve the repo spec
    let spec = match repo {
        Some(s) => parse_repo_spec(s).context("invalid repository spec")?,
        None => detect_remote().ok_or_else(|| {
            anyhow::anyhow!(
                "could not detect repository from current directory; specify OWNER/REPO with --repo"
            )
        })?,
    };

    let host = hostname.unwrap_or("github.com");
    let client = Client::new(host).context("failed to create HTTP client")?;

    // Determine merge method
    let merge_method = if squash {
        "squash"
    } else if rebase {
        "rebase"
    } else {
        "merge"
    };

    // Build the request body
    let mut body_map = serde_json::Map::new();
    body_map.insert(
        "merge_method".to_string(),
        serde_json::Value::String(merge_method.to_string()),
    );
    if let Some(s) = subject {
        body_map.insert(
            "commit_title".to_string(),
            serde_json::Value::String(s.to_string()),
        );
    }
    if let Some(b) = body {
        body_map.insert(
            "commit_message".to_string(),
            serde_json::Value::String(b.to_string()),
        );
    }

    let body_value = serde_json::Value::Object(body_map);

    // Merge the PR
    let path = format!("/repos/{}/{}/pulls/{number}/merge", spec.owner, spec.repo);
    let response = client
        .request("PUT", &path, &[], Some(serde_json::to_vec(&body_value)?))
        .context("failed to merge pull request")?;

    let status = response.status();
    if status == reqwest::StatusCode::NOT_FOUND {
        anyhow::bail!("pull request #{number} not found in '{spec}'");
    }
    if status == reqwest::StatusCode::METHOD_NOT_ALLOWED {
        anyhow::bail!("pull request #{number} cannot be merged");
    }
    if !status.is_success() {
        let err_body: serde_json::Value = response.json().unwrap_or_default();
        let msg = err_body["message"].as_str().unwrap_or("merge failed");
        anyhow::bail!("failed to merge pull request #{number}: {msg}");
    }

    let result: serde_json::Value = response.json().context("failed to parse merge response")?;

    let sha = result["sha"].as_str().unwrap_or("");
    let merged = result["merged"].as_bool().unwrap_or(false);

    if merged {
        println!("Merged pull request #{number} in {spec} (SHA: {sha})");
    } else {
        let msg = result["message"].as_str().unwrap_or("unknown reason");
        anyhow::bail!("failed to merge pull request #{number}: {msg}");
    }

    // Delete the head branch if requested
    if delete_branch {
        // First, get the PR details to find the head branch ref
        let pr_path = format!("/repos/{}/{}/pulls/{number}", spec.owner, spec.repo);
        let pr_response = client
            .get(&pr_path)
            .context("failed to fetch pull request details")?;
        if let Ok(pr_data) = pr_response.json::<serde_json::Value>() {
            if let (Some(ref_name), Some(repo_name)) = (
                pr_data["head"]["ref"].as_str(),
                pr_data["head"]["repo"]["full_name"].as_str(),
            ) {
                // Only delete if the head branch is in the same repo
                if repo_name == spec.to_string() {
                    let delete_path = format!(
                        "/repos/{}/{}/git/refs/heads/{ref_name}",
                        spec.owner, spec.repo
                    );
                    if let Err(e) = client.request("DELETE", &delete_path, &[], None) {
                        eprintln!("Warning: failed to delete head branch '{ref_name}': {e}");
                    } else {
                        println!("Deleted head branch '{ref_name}'");
                    }
                }
            }
        }
    }

    Ok(())
}

/// Execute `gor pr checkout`.
///
/// Fetches and checks out a pull request's head branch locally.
/// Adds the remote if not already present. Supports custom local branch
/// names.
///
/// # Errors
///
/// Returns an error if the repository cannot be found, the PR does not exist,
/// or the checkout fails.
fn pr_checkout(
    number: u64,
    repo: Option<&str>,
    branch: Option<&str>,
    _recurse_submodules: bool,
    hostname: Option<&str>,
) -> anyhow::Result<()> {
    // Resolve the repo spec
    let spec = match repo {
        Some(s) => parse_repo_spec(s).context("invalid repository spec")?,
        None => detect_remote().ok_or_else(|| {
            anyhow::anyhow!(
                "could not detect repository from current directory; specify OWNER/REPO with --repo"
            )
        })?,
    };

    let host = hostname.unwrap_or("github.com");
    let client = Client::new(host).context("failed to create HTTP client")?;

    // Fetch PR details to get head branch info
    let path = format!("/repos/{}/{}/pulls/{number}", spec.owner, spec.repo);
    let response = client.get(&path).context("failed to fetch pull request")?;

    let status = response.status();
    if status == reqwest::StatusCode::NOT_FOUND {
        anyhow::bail!("pull request #{number} not found in '{spec}'");
    }
    if !status.is_success() {
        anyhow::bail!("failed to fetch pull request #{number}: HTTP {status}");
    }

    let pr: serde_json::Value = response
        .json()
        .context("failed to parse pull request response")?;

    let head_ref = pr["head"]["ref"]
        .as_str()
        .ok_or_else(|| anyhow::anyhow!("could not determine head branch"))?;
    let _head_sha = pr["head"]["sha"]
        .as_str()
        .ok_or_else(|| anyhow::anyhow!("could not determine head SHA"))?;
    let head_repo_full_name = pr["head"]["repo"]["full_name"].as_str();
    let head_clone_url = pr["head"]["repo"]["clone_url"].as_str();

    // Determine the local branch name
    let local_branch = branch.unwrap_or(head_ref);

    // Open the local git repo
    let local_repo =
        gix::discover(std::env::current_dir().context("failed to get current directory")?)
            .context("failed to discover git repository")?;

    // Determine the remote name to use
    let remote_name = if head_repo_full_name.is_some()
        && head_repo_full_name != Some(spec.to_string().as_str())
    {
        // PR is from a fork — use the fork owner as remote name
        let fork_owner = head_repo_full_name
            .and_then(|n| n.split('/').next())
            .unwrap_or("fork");

        // Check if the remote already exists
        let remote_exists = local_repo.find_remote(fork_owner).is_ok();

        if !remote_exists {
            if let Some(clone_url) = head_clone_url {
                eprintln!("Adding remote '{fork_owner}' -> {clone_url}");
                let config_path = local_repo.git_dir().join("config");
                let mut file = std::fs::OpenOptions::new()
                    .append(true)
                    .open(&config_path)
                    .context("failed to open git config")?;
                let url_escaped = clone_url.replace('"', "\\\"");
                writeln!(
                    file,
                    "[remote \"{fork_owner}\"]\n\turl = {url_escaped}\n\tfetch = +refs/heads/*:refs/remotes/{fork_owner}/*"
                )
                .context("failed to write remote config")?;
            }
        }

        fork_owner.to_string()
    } else {
        // PR is from the same repo — use origin
        "origin".to_string()
    };

    // Fetch the PR head branch using gix
    eprintln!("Fetching remote '{remote_name}' with branch '{head_ref}'...");

    let workdir = local_repo
        .workdir()
        .ok_or_else(|| anyhow::anyhow!("bare repository cannot check out"))?;
    let workdir_str = workdir.to_str().unwrap_or(".");

    // Fetch the specific ref from the remote using system git
    let fetch_status = std::process::Command::new("git")
        .args([
            "-C",
            workdir_str,
            "fetch",
            &remote_name,
            &format!("+refs/heads/{head_ref}:refs/remotes/{remote_name}/{head_ref}"),
        ])
        .status()
        .context("failed to run git fetch")?;

    if !fetch_status.success() {
        anyhow::bail!("failed to fetch from remote '{remote_name}'");
    }

    // Create or update the local branch and check it out
    eprintln!("Checking out '{local_branch}'...");

    let checkout_status = std::process::Command::new("git")
        .args([
            "-C",
            workdir_str,
            "checkout",
            "-B",
            local_branch,
            &format!("refs/remotes/{remote_name}/{head_ref}"),
        ])
        .status()
        .context("failed to run git checkout")?;

    if !checkout_status.success() {
        anyhow::bail!("failed to checkout branch '{local_branch}'");
    }

    println!("Checked out PR #{number} as '{local_branch}'");
    Ok(())
}

/// Print a formatted pull request detail view.
///
/// Displays title, metadata, body, review status, merge status, CI checks,
/// and optionally comments.
fn print_pr_view(
    pr: &serde_json::Value,
    reviews: &[serde_json::Value],
    ci_status: Option<&serde_json::Value>,
    comments: &[serde_json::Value],
) {
    // Title
    let title = pr["title"].as_str().unwrap_or("(no title)");
    println!("{title}");
    let separator_len = title.len().min(80);
    println!("{}", "".repeat(separator_len));
    println!();

    // Metadata
    let state = pr["state"].as_str().unwrap_or("unknown");
    let display_state = if pr["merged_at"].as_str().is_some() {
        "merged"
    } else {
        state
    };
    let author = pr["user"]["login"].as_str().unwrap_or("unknown");
    let created = pr["created_at"]
        .as_str()
        .map_or_else(|| "".to_string(), format_date);
    let updated = pr["updated_at"]
        .as_str()
        .map_or_else(|| "".to_string(), format_date);
    let base_branch = pr["base"]["ref"].as_str().unwrap_or("?");
    let head_branch = pr["head"]["ref"].as_str().unwrap_or("?");

    let labels_str = pr["labels"]
        .as_array()
        .map(|arr| {
            arr.iter()
                .filter_map(|l| l["name"].as_str())
                .collect::<Vec<_>>()
                .join(", ")
        })
        .unwrap_or_default();

    println!("State:  {display_state}");
    println!("Author: {author}");
    println!("Created: {created}");
    println!("Updated: {updated}");
    println!("Branches: {base_branch}{head_branch}");
    if !labels_str.is_empty() {
        println!("Labels: {labels_str}");
    }
    println!();

    // Body
    let body = pr["body"].as_str().unwrap_or("");
    if !body.is_empty() {
        println!("{body}");
        println!();
    }

    // Review status
    print_review_status(reviews);

    // Merge status
    print_merge_status(pr);

    // CI checks
    print_ci_status(ci_status);

    // Comments
    if !comments.is_empty() {
        println!("── Comments ──");
        println!();
        for comment in comments {
            let comment_author = comment["user"]["login"].as_str().unwrap_or("unknown");
            let comment_date = comment["created_at"]
                .as_str()
                .map_or_else(|| "".to_string(), format_date);
            let comment_body = comment["body"].as_str().unwrap_or("");
            println!("{comment_author} commented on {comment_date}");
            println!();
            println!("{comment_body}");
            println!();
        }
    }
}

/// Print the review status section.
fn print_review_status(reviews: &[serde_json::Value]) {
    // Aggregate the latest review state per reviewer
    // Reviews are ordered oldest-first; we want the latest per user
    let mut latest_state: BTreeMap<&str, &str> = BTreeMap::new();
    for review in reviews {
        let user = review["user"]["login"].as_str();
        let state_val = review["state"].as_str();
        if let (Some(u), Some(s)) = (user, state_val) {
            if s != "COMMENTED" && s != "DISMISSED" {
                // APPROVED or CHANGES_REQUESTED override previous
                latest_state.insert(u, s);
            } else if s == "COMMENTED" && !latest_state.contains_key(u) {
                // Only set COMMENTED if no approval/change request yet
                latest_state.insert(u, s);
            }
        }
    }

    if latest_state.is_empty() {
        return;
    }

    println!("── Review Status ──");

    let mut approved: Vec<&str> = Vec::new();
    let mut changes_requested: Vec<&str> = Vec::new();
    let mut commented: Vec<&str> = Vec::new();

    for (user, state_val) in &latest_state {
        match *state_val {
            "APPROVED" => approved.push(user),
            "CHANGES_REQUESTED" => changes_requested.push(user),
            _ => commented.push(user),
        }
    }

    if !approved.is_empty() {
        println!("Approved by: {}", approved.join(", "));
    }
    if !changes_requested.is_empty() {
        println!("Changes requested by: {}", changes_requested.join(", "));
    }
    if !commented.is_empty() {
        println!("Commented by: {}", commented.join(", "));
    }

    println!();
}

/// Print the merge status section.
fn print_merge_status(pr: &serde_json::Value) {
    println!("── Merge Status ──");

    let mergeable = pr["mergeable"].as_bool();
    let mergeable_status = match mergeable {
        Some(true) => "yes",
        Some(false) => "no (conflicts)",
        None => "unknown (checking)",
    };
    println!("Mergeable: {mergeable_status}");

    if let Some(merged_at) = pr["merged_at"].as_str() {
        let merged_date = format_date(merged_at);
        let merged_by = pr["merged_by"]["login"].as_str().unwrap_or("unknown");
        println!("Merged: yes ({merged_date}) by {merged_by}");
    } else {
        println!("Merged: no");
    }

    println!();
}

/// Print the CI check status section.
fn print_ci_status(ci_status: Option<&serde_json::Value>) {
    let statuses = ci_status
        .and_then(|s| s["statuses"].as_array())
        .cloned()
        .unwrap_or_default();

    if statuses.is_empty() {
        return;
    }

    println!("── CI Checks ──");

    for check in &statuses {
        let name = check["context"].as_str().unwrap_or("?");
        let state_val = check["state"].as_str().unwrap_or("unknown");
        let (icon, display_state) = match state_val {
            "success" => ("", "success"),
            "failure" => ("", "failure"),
            "pending" => ("", "pending"),
            _ => ("", state_val),
        };
        println!("  {icon} {name} ({display_state})");
    }

    println!();
}

/// Print a formatted pull request list table.
///
/// Columns: NUMBER, TITLE, AUTHOR, HEAD BRANCH, LABELS, STATE
fn print_pr_table(prs: &[serde_json::Value]) {
    if prs.is_empty() {
        println!("No pull requests found.");
        return;
    }

    // Column widths
    let num_width = 8;
    let title_width = 50;
    let author_width = 14;
    let branch_width = 14;
    let labels_width = 14;
    let state_width = 8;

    // Header
    println!(
        "{:>num_width$}  {:<title_width$}  {:<author_width$}  {:<branch_width$}  {:<labels_width$}  {:<state_width$}",
        "NUMBER", "TITLE", "AUTHOR", "HEAD BRANCH", "LABELS", "STATE",
    );

    for pr in prs {
        let number = pr["number"]
            .as_u64()
            .map_or_else(|| "".to_string(), |n| n.to_string());
        let title = pr["title"].as_str().unwrap_or("");
        let author = pr["user"]["login"].as_str().unwrap_or("");
        let head_branch = pr["head"]["ref"].as_str().unwrap_or("");
        let state = pr["state"].as_str().unwrap_or("");

        // Determine display state: if merged_at is set, show "merged"
        let display_state = if pr["merged_at"].as_str().is_some() {
            "merged"
        } else {
            state
        };

        let labels_str = pr["labels"]
            .as_array()
            .map(|arr| {
                arr.iter()
                    .filter_map(|l| l["name"].as_str())
                    .collect::<Vec<_>>()
                    .join(", ")
            })
            .unwrap_or_default();
        let labels_display = if labels_str.is_empty() {
            "".to_string()
        } else {
            labels_str
        };

        let title_truncated = crate::cmd::util::truncate(title, title_width);
        let author_truncated = crate::cmd::util::truncate(author, author_width);
        let branch_truncated = crate::cmd::util::truncate(head_branch, branch_width);
        let labels_truncated = crate::cmd::util::truncate(&labels_display, labels_width);

        println!(
            "{number:>num_width$}  {title_truncated:<title_width$}  {author_truncated:<author_width$}  {branch_truncated:<branch_width$}  {labels_truncated:<labels_width$}  {display_state:<state_width$}",
        );
    }
}

/// Open a URL in the default browser using the system's default handler.
fn open_in_browser(url: &str) {
    #[cfg(target_os = "linux")]
    {
        let _ = std::process::Command::new("xdg-open").arg(url).spawn();
    }
    #[cfg(target_os = "macos")]
    {
        let _ = std::process::Command::new("open").arg(url).spawn();
    }
    #[cfg(target_os = "windows")]
    {
        let _ = std::process::Command::new("cmd")
            .args(["/c", "start", url])
            .spawn();
    }
    #[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
    {
        println!("Open {url} in your browser");
    }
}

/// Execute `gor pr diff`.
///
/// Shows the unified diff of a pull request. Supports color control and
/// name-only mode.
///
/// # Errors
///
/// Returns an error if the PR cannot be found or the API request fails.
fn diff(
    number: u64,
    repo: Option<&str>,
    _color: &str,
    name_only: bool,
    hostname: Option<&str>,
) -> anyhow::Result<()> {
    let spec = match repo {
        Some(s) => parse_repo_spec(s).context("invalid repository spec")?,
        None => detect_remote().ok_or_else(|| {
            anyhow::anyhow!(
                "could not detect repository from current directory; specify OWNER/REPO"
            )
        })?,
    };

    let host = hostname.unwrap_or("github.com");
    let client = Client::new(host).context("failed to create HTTP client")?;

    if name_only {
        // Fetch list of changed files
        let files_path = format!(
            "/repos/{}/{}/pulls/{number}/files?per_page=100",
            spec.owner, spec.repo
        );
        let resp = client
            .get(&files_path)
            .context("failed to fetch PR files")?;
        let status = resp.status();
        if status == reqwest::StatusCode::NOT_FOUND {
            anyhow::bail!("pull request #{number} not found in '{spec}'");
        }
        if !status.is_success() {
            anyhow::bail!("failed to fetch PR files: HTTP {status}");
        }
        let files: Vec<serde_json::Value> =
            resp.json().context("failed to parse files response")?;
        for file in &files {
            let filename = file["filename"].as_str().unwrap_or("");
            let status_str = file["status"].as_str().unwrap_or("");
            let additions = file["additions"].as_u64().unwrap_or(0);
            let deletions = file["deletions"].as_u64().unwrap_or(0);
            println!("{status_str:8} +{additions:4} -{deletions:4}  {filename}");
        }
    } else {
        // Fetch the diff
        let diff_path = format!("/repos/{}/{}/pulls/{number}", spec.owner, spec.repo);
        let resp = client
            .request(
                "GET",
                &diff_path,
                &["Accept: application/vnd.github.v3.diff".to_string()],
                None,
            )
            .context("failed to fetch PR diff")?;
        let status = resp.status();
        if status == reqwest::StatusCode::NOT_FOUND {
            anyhow::bail!("pull request #{number} not found in '{spec}'");
        }
        if !status.is_success() {
            anyhow::bail!("failed to fetch PR diff: HTTP {status}");
        }
        let diff_text = resp.text().context("failed to read diff response")?;
        println!("{diff_text}");
    }

    Ok(())
}

/// Execute `gor pr edit`.
///
/// Edits a pull request's title, body, base branch, labels, assignees,
/// or milestone.
///
/// # Errors
///
/// Returns an error if the PR cannot be found or the API request fails.
#[allow(clippy::too_many_arguments)]
fn pr_edit(
    number: u64,
    repo: Option<&str>,
    title: Option<&str>,
    body: Option<&str>,
    base: Option<&str>,
    add_label: &[String],
    remove_label: &[String],
    add_assignee: &[String],
    remove_assignee: &[String],
    milestone: Option<&str>,
    hostname: Option<&str>,
) -> anyhow::Result<()> {
    let spec = match repo {
        Some(s) => parse_repo_spec(s).context("invalid repository spec")?,
        None => detect_remote().ok_or_else(|| {
            anyhow::anyhow!(
                "could not detect repository from current directory; specify OWNER/REPO"
            )
        })?,
    };

    let host = hostname.unwrap_or("github.com");
    let client = Client::new(host).context("failed to create HTTP client")?;

    let mut body_map = serde_json::Map::new();
    if let Some(t) = title {
        body_map.insert(
            "title".to_string(),
            serde_json::Value::String(t.to_string()),
        );
    }
    if let Some(b) = body {
        body_map.insert("body".to_string(), serde_json::Value::String(b.to_string()));
    }
    if let Some(b) = base {
        body_map.insert("base".to_string(), serde_json::Value::String(b.to_string()));
    }
    if let Some(m) = milestone {
        if let Ok(id) = m.parse::<u64>() {
            body_map.insert(
                "milestone".to_string(),
                serde_json::Value::Number(serde_json::Number::from(id)),
            );
        } else {
            body_map.insert(
                "milestone".to_string(),
                serde_json::Value::String(m.to_string()),
            );
        }
    }

    // Handle labels: fetch current, add, remove
    if !add_label.is_empty() || !remove_label.is_empty() {
        let get_path = format!("/repos/{}/{}/pulls/{number}", spec.owner, spec.repo);
        let current: serde_json::Value = client
            .get(&get_path)
            .context("failed to fetch current PR")?
            .json()
            .context("failed to parse PR response")?;
        let current_labels: Vec<String> = current["labels"]
            .as_array()
            .map(|arr| {
                arr.iter()
                    .filter_map(|l| l["name"].as_str().map(String::from))
                    .collect()
            })
            .unwrap_or_default();
        let mut new_labels = current_labels;
        for label in add_label {
            if !new_labels.contains(label) {
                new_labels.push(label.clone());
            }
        }
        new_labels.retain(|l| !remove_label.contains(l));
        body_map.insert(
            "labels".to_string(),
            serde_json::Value::Array(
                new_labels
                    .iter()
                    .map(|l| serde_json::Value::String(l.clone()))
                    .collect(),
            ),
        );
    }

    // Handle assignees
    if !add_assignee.is_empty() || !remove_assignee.is_empty() {
        let get_path = format!("/repos/{}/{}/pulls/{number}", spec.owner, spec.repo);
        let current: serde_json::Value = client
            .get(&get_path)
            .context("failed to fetch current PR")?
            .json()
            .context("failed to parse PR response")?;
        let current_assignees: Vec<String> = current["assignees"]
            .as_array()
            .map(|arr| {
                arr.iter()
                    .filter_map(|a| a["login"].as_str().map(String::from))
                    .collect()
            })
            .unwrap_or_default();
        let mut new_assignees = current_assignees;
        for a in add_assignee {
            if !new_assignees.contains(a) {
                new_assignees.push(a.clone());
            }
        }
        new_assignees.retain(|a| !remove_assignee.contains(a));
        body_map.insert(
            "assignees".to_string(),
            serde_json::Value::Array(
                new_assignees
                    .iter()
                    .map(|a| serde_json::Value::String(a.clone()))
                    .collect(),
            ),
        );
    }

    if body_map.is_empty() {
        anyhow::bail!("no changes specified");
    }

    let path = format!("/repos/{}/{}/pulls/{number}", spec.owner, spec.repo);
    let body_value = serde_json::Value::Object(body_map);
    let response = client
        .request("PATCH", &path, &[], Some(serde_json::to_vec(&body_value)?))
        .context("failed to edit PR")?;

    let status = response.status();
    if status == reqwest::StatusCode::NOT_FOUND {
        anyhow::bail!("pull request #{number} not found in '{spec}'");
    }
    if !status.is_success() {
        let err_body: serde_json::Value = response.json().unwrap_or_default();
        let msg = err_body["message"].as_str().unwrap_or("edit failed");
        anyhow::bail!("failed to edit PR #{number}: {msg}");
    }

    let pr: serde_json::Value = response.json().context("failed to parse response")?;
    let pr_number = pr["number"].as_u64().unwrap_or(number);
    let pr_title = pr["title"].as_str().unwrap_or("");
    let pr_base = pr["base"]["ref"].as_str().unwrap_or("");
    let pr_labels: Vec<&str> = pr["labels"]
        .as_array()
        .map(|arr| arr.iter().filter_map(|l| l["name"].as_str()).collect())
        .unwrap_or_default();
    let pr_assignees: Vec<&str> = pr["assignees"]
        .as_array()
        .map(|arr| arr.iter().filter_map(|a| a["login"].as_str()).collect())
        .unwrap_or_default();
    let pr_milestone = pr["milestone"]["title"].as_str().unwrap_or("");

    println!("✓ Updated PR #{pr_number}: {pr_title}");
    println!("  Base:      {pr_base}");
    println!("  Labels:    {}", pr_labels.join(", "));
    println!("  Assignees: {}", pr_assignees.join(", "));
    println!("  Milestone: {pr_milestone}");
    Ok(())
}

/// Execute `gor pr review`.
///
/// Submits a review on a pull request with approve, request_changes,
/// or comment state.
///
/// # Errors
///
/// Returns an error if the PR cannot be found or the API request fails.
fn review(
    number: u64,
    repo: Option<&str>,
    approve: bool,
    request_changes: bool,
    _comment: bool,
    body: Option<&str>,
    hostname: Option<&str>,
) -> anyhow::Result<()> {
    let spec = match repo {
        Some(s) => parse_repo_spec(s).context("invalid repository spec")?,
        None => detect_remote().ok_or_else(|| {
            anyhow::anyhow!(
                "could not detect repository from current directory; specify OWNER/REPO"
            )
        })?,
    };

    let host = hostname.unwrap_or("github.com");
    let client = Client::new(host).context("failed to create HTTP client")?;

    let event = if approve {
        "APPROVE"
    } else if request_changes {
        "REQUEST_CHANGES"
    } else {
        "COMMENT"
    };

    let mut body_map = serde_json::Map::new();
    body_map.insert(
        "event".to_string(),
        serde_json::Value::String(event.to_string()),
    );
    if let Some(b) = body {
        body_map.insert("body".to_string(), serde_json::Value::String(b.to_string()));
    }

    let path = format!("/repos/{}/{}/pulls/{number}/reviews", spec.owner, spec.repo);
    let body_value = serde_json::Value::Object(body_map);
    let response = client
        .post(&path, &body_value)
        .context("failed to submit review")?;

    let status = response.status();
    if status == reqwest::StatusCode::NOT_FOUND {
        anyhow::bail!("pull request #{number} not found in '{spec}'");
    }
    if !status.is_success() {
        let err_body: serde_json::Value = response.json().unwrap_or_default();
        let msg = err_body["message"].as_str().unwrap_or("review failed");
        anyhow::bail!("failed to submit review for PR #{number}: {msg}");
    }

    let review: serde_json::Value = response.json().context("failed to parse response")?;
    let state = review["state"].as_str().unwrap_or(event);
    println!("✓ Submitted {state} review on PR #{number}");
    Ok(())
}

/// Execute `gor pr checks`.
///
/// Shows CI check status for a pull request. Supports --watch for polling
/// and --json for structured output.
///
/// # Errors
///
/// Returns an error if the PR cannot be found or the API request fails.
#[allow(clippy::needless_pass_by_value)]
fn checks(
    number: u64,
    repo: Option<&str>,
    watch: bool,
    json: Option<Vec<String>>,
    hostname: Option<&str>,
) -> anyhow::Result<()> {
    let spec = match repo {
        Some(s) => parse_repo_spec(s).context("invalid repository spec")?,
        None => detect_remote().ok_or_else(|| {
            anyhow::anyhow!(
                "could not detect repository from current directory; specify OWNER/REPO"
            )
        })?,
    };

    let host = hostname.unwrap_or("github.com");
    let client = Client::new(host).context("failed to create HTTP client")?;

    // Fetch the PR to get the head SHA
    let pr_path = format!("/repos/{}/{}/pulls/{number}", spec.owner, spec.repo);
    let pr_resp = client.get(&pr_path).context("failed to fetch PR")?;
    let pr_status = pr_resp.status();
    if pr_status == reqwest::StatusCode::NOT_FOUND {
        anyhow::bail!("pull request #{number} not found in '{spec}'");
    }
    if !pr_status.is_success() {
        anyhow::bail!("failed to fetch PR #{number}: HTTP {pr_status}");
    }
    let pr: serde_json::Value = pr_resp.json().context("failed to parse PR response")?;
    let head_sha = pr["head"]["sha"].as_str().context("PR missing head SHA")?;

    loop {
        let checks_path = format!(
            "/repos/{}/{}/commits/{head_sha}/check-runs?per_page=100",
            spec.owner, spec.repo
        );
        let resp = client
            .get(&checks_path)
            .context("failed to fetch check runs")?;
        let status = resp.status();
        if !status.is_success() {
            anyhow::bail!("failed to fetch checks: HTTP {status}");
        }
        let data: serde_json::Value = resp.json().context("failed to parse checks response")?;
        let check_runs = data["check_runs"]
            .as_array()
            .map_or(&[] as &[serde_json::Value], |a| a);

        if let Some(ref fields) = json {
            let fields_ref: Option<&[String]> = if fields.is_empty() {
                None
            } else {
                Some(fields)
            };
            print_json(&check_runs, fields_ref);
            return Ok(());
        }

        if check_runs.is_empty() {
            println!("No checks found for PR #{number}.");
            return Ok(());
        }

        let mut passed = 0u32;
        let mut failed = 0u32;
        let mut pending = 0u32;

        for check in check_runs {
            let name = check["name"].as_str().unwrap_or("");
            let check_status = check["status"].as_str().unwrap_or("unknown");
            let conclusion = check["conclusion"].as_str().unwrap_or("pending");
            let details_url = check["html_url"].as_str().unwrap_or("");

            let icon = match (check_status, conclusion) {
                ("completed", "success" | "neutral") => {
                    passed += 1;
                    ""
                }
                ("completed", "failure" | "timed_out" | "cancelled" | "action_required") => {
                    failed += 1;
                    ""
                }
                _ => {
                    pending += 1;
                    ""
                }
            };

            println!("{icon} {name:<40} {check_status:<12} {conclusion:<16} {details_url}");
        }

        println!();
        println!("{passed} passed, {failed} failed, {pending} pending");

        if !watch || pending == 0 {
            if failed > 0 {
                std::process::exit(1);
            }
            return Ok(());
        }

        std::thread::sleep(std::time::Duration::from_secs(5));
    }
}

/// Execute `gor pr ready`.
///
/// Marks a draft pull request as ready for review.
///
/// # Errors
///
/// Returns an error if the PR does not exist or the API request fails.
fn ready(number: u64, repo: Option<&str>, hostname: Option<&str>) -> anyhow::Result<()> {
    let host = hostname.unwrap_or("github.com");
    let client = Client::new(host).context("failed to create HTTP client")?;

    let spec = if let Some(r) = repo {
        parse_repo_spec(r).with_context(|| format!("invalid repository: {r}"))?
    } else {
        detect_remote().context("could not detect repository from git remote")?
    };

    let path = format!("/repos/{}/{}/pulls/{number}", spec.owner, spec.repo);

    // First, fetch the PR to check if it's a draft.
    let response = client.get(&path).context("failed to fetch PR")?;
    let status = response.status();
    if !status.is_success() {
        anyhow::bail!("failed to fetch PR #{number}: HTTP {status}");
    }

    let pr: serde_json::Value = response.json().context("failed to parse PR response")?;
    let is_draft = pr["draft"].as_bool().unwrap_or(false);

    if !is_draft {
        println!("PR #{number} is already ready for review.");
        return Ok(());
    }

    // Mark the PR as ready by setting draft to false.
    let body = serde_json::json!({"draft": false});
    let body_bytes = serde_json::to_vec(&body).context("failed to serialize body")?;
    let update_response = client
        .request("PATCH", &path, &[], Some(body_bytes))
        .context("failed to update PR")?;

    let update_status = update_response.status();
    if !update_status.is_success() {
        let err_body: serde_json::Value = update_response.json().unwrap_or_default();
        let msg = err_body["message"].as_str().unwrap_or("update failed");
        anyhow::bail!("failed to mark PR #{number} as ready: {msg}");
    }

    println!("PR #{number} is now ready for review.");
    Ok(())
}

#[cfg(test)]
#[allow(clippy::expect_used)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn print_pr_table_basic() {
        let prs = vec![json!({
            "number": 42,
            "title": "Fix authentication bug in login flow",
            "state": "open",
            "merged_at": null,
            "user": { "login": "octocat" },
            "head": { "ref": "fix-auth" },
            "labels": [
                { "name": "bug" },
                { "name": "security" }
            ]
        })];
        // Should not panic
        print_pr_table(&prs);
    }

    #[test]
    fn print_pr_table_merged() {
        let prs = vec![json!({
            "number": 100,
            "title": "Add new feature",
            "state": "closed",
            "merged_at": "2024-01-15T10:30:00Z",
            "user": { "login": "dev-user" },
            "head": { "ref": "feature-branch" },
            "labels": []
        })];
        // Should not panic; merged PR should show "merged" state
        print_pr_table(&prs);
    }

    #[test]
    fn print_pr_table_empty() {
        let prs: Vec<serde_json::Value> = vec![];
        // Should not panic
        print_pr_table(&prs);
    }

    #[test]
    fn print_pr_table_multiple() {
        let prs = vec![
            json!({
                "number": 1,
                "title": "First PR",
                "state": "open",
                "merged_at": null,
                "user": { "login": "alice" },
                "head": { "ref": "feature-a" },
                "labels": [{"name": "enhancement"}]
            }),
            json!({
                "number": 2,
                "title": "Second PR with a very long title that should be truncated in the table output",
                "state": "open",
                "merged_at": null,
                "user": { "login": "bob" },
                "head": { "ref": "feature-b" },
                "labels": [{"name": "bug"}, {"name": "docs"}]
            }),
        ];
        // Should not panic
        print_pr_table(&prs);
    }

    #[test]
    fn print_pr_table_null_fields() {
        let prs = vec![json!({
            "number": 99,
            "title": null,
            "state": null,
            "merged_at": null,
            "user": null,
            "head": null,
            "labels": null
        })];
        // Should not panic with null fields
        print_pr_table(&prs);
    }

    #[test]
    fn open_in_browser_does_not_panic() {
        // Just verify it doesn't panic — actual browser opening is a no-op in tests
        open_in_browser("https://github.com/octocat/hello-world/pulls");
    }

    #[test]
    fn print_pr_view_basic() {
        let pr = json!({
            "number": 42,
            "title": "Fix authentication bug",
            "state": "open",
            "merged_at": null,
            "user": { "login": "octocat" },
            "created_at": "2024-01-15T10:30:00Z",
            "updated_at": "2024-01-16T12:00:00Z",
            "body": "This PR fixes the authentication bug.",
            "base": { "ref": "main" },
            "head": { "ref": "fix-auth", "sha": "abc123" },
            "labels": [
                { "name": "bug" },
                { "name": "security" }
            ],
            "mergeable": true,
            "merged_by": null
        });
        let reviews: Vec<serde_json::Value> = vec![];
        let ci_status: Option<serde_json::Value> = None;
        let comments: Vec<serde_json::Value> = vec![];
        // Should not panic
        print_pr_view(&pr, &reviews, ci_status.as_ref(), &comments);
    }

    #[test]
    fn print_pr_view_merged() {
        let pr = json!({
            "number": 100,
            "title": "Add new feature",
            "state": "closed",
            "merged_at": "2024-01-15T10:30:00Z",
            "user": { "login": "dev-user" },
            "created_at": "2024-01-10T08:00:00Z",
            "updated_at": "2024-01-15T10:30:00Z",
            "body": "This adds a new feature.",
            "base": { "ref": "main" },
            "head": { "ref": "feature-branch", "sha": "def456" },
            "labels": [],
            "mergeable": null,
            "merged_by": { "login": "admin" }
        });
        let reviews: Vec<serde_json::Value> = vec![];
        let ci_status: Option<serde_json::Value> = None;
        let comments: Vec<serde_json::Value> = vec![];
        // Should not panic
        print_pr_view(&pr, &reviews, ci_status.as_ref(), &comments);
    }

    #[test]
    fn print_pr_view_with_reviews() {
        let pr = json!({
            "number": 42,
            "title": "Fix bug",
            "state": "open",
            "merged_at": null,
            "user": { "login": "octocat" },
            "created_at": "2024-01-15T10:30:00Z",
            "updated_at": "2024-01-16T12:00:00Z",
            "body": "Fixes a bug.",
            "base": { "ref": "main" },
            "head": { "ref": "fix-bug", "sha": "abc123" },
            "labels": [],
            "mergeable": true,
            "merged_by": null
        });
        let reviews = vec![
            json!({
                "user": { "login": "reviewer1" },
                "state": "APPROVED"
            }),
            json!({
                "user": { "login": "reviewer2" },
                "state": "CHANGES_REQUESTED"
            }),
            json!({
                "user": { "login": "reviewer3" },
                "state": "COMMENTED"
            }),
        ];
        let ci_status: Option<serde_json::Value> = None;
        let comments: Vec<serde_json::Value> = vec![];
        // Should not panic
        print_pr_view(&pr, &reviews, ci_status.as_ref(), &comments);
    }

    #[test]
    fn print_pr_view_with_ci() {
        let pr = json!({
            "number": 42,
            "title": "Fix bug",
            "state": "open",
            "merged_at": null,
            "user": { "login": "octocat" },
            "created_at": "2024-01-15T10:30:00Z",
            "updated_at": "2024-01-16T12:00:00Z",
            "body": "Fixes a bug.",
            "base": { "ref": "main" },
            "head": { "ref": "fix-bug", "sha": "abc123" },
            "labels": [],
            "mergeable": true,
            "merged_by": null
        });
        let reviews: Vec<serde_json::Value> = vec![];
        let ci_status = Some(json!({
            "statuses": [
                { "context": "CI / test", "state": "success" },
                { "context": "CI / lint", "state": "failure" },
                { "context": "CI / build", "state": "pending" }
            ]
        }));
        let comments: Vec<serde_json::Value> = vec![];
        // Should not panic
        print_pr_view(&pr, &reviews, ci_status.as_ref(), &comments);
    }

    #[test]
    fn print_pr_view_with_comments() {
        let pr = json!({
            "number": 42,
            "title": "Fix bug",
            "state": "open",
            "merged_at": null,
            "user": { "login": "octocat" },
            "created_at": "2024-01-15T10:30:00Z",
            "updated_at": "2024-01-16T12:00:00Z",
            "body": "Fixes a bug.",
            "base": { "ref": "main" },
            "head": { "ref": "fix-bug", "sha": "abc123" },
            "labels": [],
            "mergeable": true,
            "merged_by": null
        });
        let reviews: Vec<serde_json::Value> = vec![];
        let ci_status: Option<serde_json::Value> = None;
        let comments = vec![
            json!({
                "user": { "login": "reviewer1" },
                "created_at": "2024-01-16T14:00:00Z",
                "body": "Looks good to me!"
            }),
            json!({
                "user": { "login": "octocat" },
                "created_at": "2024-01-16T15:00:00Z",
                "body": "Thanks for the review!"
            }),
        ];
        // Should not panic
        print_pr_view(&pr, &reviews, ci_status.as_ref(), &comments);
    }

    #[test]
    fn print_pr_view_null_fields() {
        let pr = json!({
            "number": 99,
            "title": null,
            "state": null,
            "merged_at": null,
            "user": null,
            "created_at": null,
            "updated_at": null,
            "body": null,
            "base": null,
            "head": null,
            "labels": null,
            "mergeable": null,
            "merged_by": null
        });
        let reviews: Vec<serde_json::Value> = vec![];
        let ci_status: Option<serde_json::Value> = None;
        let comments: Vec<serde_json::Value> = vec![];
        // Should not panic with null fields
        print_pr_view(&pr, &reviews, ci_status.as_ref(), &comments);
    }

    #[test]
    fn print_review_status_empty() {
        let reviews: Vec<serde_json::Value> = vec![];
        // Should not panic
        print_review_status(&reviews);
    }

    #[test]
    fn print_review_status_with_reviews() {
        let reviews = vec![
            json!({
                "user": { "login": "alice" },
                "state": "APPROVED"
            }),
            json!({
                "user": { "login": "bob" },
                "state": "CHANGES_REQUESTED"
            }),
            json!({
                "user": { "login": "carol" },
                "state": "COMMENTED"
            }),
        ];
        // Should not panic
        print_review_status(&reviews);
    }

    #[test]
    fn print_merge_status_mergeable() {
        let pr = json!({
            "mergeable": true,
            "merged_at": null,
            "merged_by": null
        });
        // Should not panic
        print_merge_status(&pr);
    }

    #[test]
    fn print_merge_status_conflicts() {
        let pr = json!({
            "mergeable": false,
            "merged_at": null,
            "merged_by": null
        });
        // Should not panic
        print_merge_status(&pr);
    }

    #[test]
    fn print_merge_status_merged() {
        let pr = json!({
            "mergeable": null,
            "merged_at": "2024-01-15T10:30:00Z",
            "merged_by": { "login": "admin" }
        });
        // Should not panic
        print_merge_status(&pr);
    }

    #[test]
    fn print_ci_status_empty() {
        let ci_status: Option<serde_json::Value> = None;
        // Should not panic
        print_ci_status(ci_status.as_ref());
    }

    #[test]
    fn print_ci_status_with_checks() {
        let ci_status = Some(json!({
            "statuses": [
                { "context": "CI / test", "state": "success" },
                { "context": "CI / lint", "state": "failure" },
                { "context": "CI / build", "state": "pending" }
            ]
        }));
        // Should not panic
        print_ci_status(ci_status.as_ref());
    }
}