Pre-launch — Gluecron is in final validation. Public signups and git hosting for non-owner users open after launch review.
CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
claude/adoring-hopper-5x74bqclaude/affectionate-feynman-ykrf1hclaude/architecture-audit-design-wxprenclaude/build-status-update-3MXsfclaude/charming-meitner-mllb5rclaude/compare-gate-gluecron-s4mFQclaude/confident-faraday-tikcwbclaude/continue-work-XMTlIclaude/crontech-gluecron-deploy-7MIECclaude/crontech-platform-setup-SeKfwclaude/design-2026claude/ecstatic-ptolemy-jMdigclaude/enhance-github-integration-QNHdGclaude/fix-aa-loop-issue-PonMQclaude/fix-actions-and-processclaude/fix-desktop-errors-XqoW8claude/fix-red-workflowsclaude/fix-website-access-6FKJNclaude/gatetest-integration-hardeningclaude/github-audit-improvements-bDFr9claude/gluecron-launch-status-FoMRlclaude/hopeful-lamport-olfCTclaude/issue-to-pr-and-protectionsclaude/jolly-heisenberg-2sg1Qclaude/launch-preparation-QmTb6claude/new-session-xk1l7claude/plan-platform-architecture-kkN4yclaude/platform-analysis-roadmap-1nUGLclaude/platform-launch-assessment-8dWV8claude/polish-platform-release-AeDrUclaude/resume-previous-work-KzyLwclaude/review-crontech-handoff-qYEVqclaude/review-project-completeness-lHhS2claude/review-readme-docs-ulqPKclaude/serene-edison-rj87weclaude/setup-multi-repo-dev-BCwNQclaude/ship-fixes-and-tests-Jvz1cclaude/site-audit-competitive-pctlwgclaude/site-migration-vercel-XstpKclaude/standalone-product-repos-XHFTDcopilot/feat-smart-empty-states-keyboard-first-enhancementcopilot/feat-smart-morning-digest-review-context-restorecopilot/fix-and-process-workflowscopilot/update-ai-powered-code-reviewfeat/debt-mapfeat/push-policy-codeowners-hardeningfeat/smart-digest-contextfeat/stage-impactfeat/t1-secret-migrationfeat/u-polishfeat/w-self-hostfeat/w2-claude-configfix/agent-journey-orphan-sweepgatetest/auto-fix-1776586424172gatetest/auto-fix-1776586534814gatetest/auto-fix-1776590685143gatetest/auto-fix-1776590808199mainops/redeploy-retriggerstyle/dxt-cta-themeworktree-agent-a3377aad30d55da26worktree-agent-a7ef607b7ee1d6c74
pulls.tsx58.5 KB · 1781 lines
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
/**
 * Pull request routes — create, list, view, merge, close, comment.
 */

import { Hono } from "hono";
import { eq, and, desc, asc, sql } from "drizzle-orm";
import { db } from "../db";
import {
  pullRequests,
  prComments,
  repositories,
  users,
  issues,
  issueComments,
} from "../db/schema";
import {
  autoAssignFromCodeowners,
  listForPr as listReviewRequestsForPr,
  requestReviewers,
  dismissRequest,
} from "../lib/review-requests";
import { Layout } from "../views/layout";
import { RepoHeader, DiffView } from "../views/components";
import { ReactionsBar } from "../views/reactions";
import { summariseReactions } from "../lib/reactions";
import { loadPrTemplate } from "../lib/templates";
import { renderMarkdown } from "../lib/markdown";
import { softAuth, requireAuth } from "../middleware/auth";
import type { AuthEnv } from "../middleware/auth";
import {
  listBranches,
  getRepoPath,
  resolveRef,
} from "../git/repository";
import type { GitDiffFile } from "../git/repository";
import { html } from "hono/html";
import { reviewDiff, isAiReviewEnabled } from "../lib/ai-review";
import { triagePullRequest } from "../lib/ai-generators";
import { mergeWithAutoResolve } from "../lib/merge-resolver";
import { runAllGateChecks, type GateCheckResult } from "../lib/gate";
import { labels as labelsTable } from "../db/schema";
import {
  matchProtection,
  evaluateProtection,
  countHumanApprovals,
  listRequiredChecks,
  passingCheckNames,
} from "../lib/branch-protection";
import { extractSuggestions } from "../lib/code-suggestions";

const pulls = new Hono<AuthEnv>();

async function resolveRepo(ownerName: string, repoName: string) {
  const [owner] = await db
    .select()
    .from(users)
    .where(eq(users.username, ownerName))
    .limit(1);
  if (!owner) return null;
  const [repo] = await db
    .select()
    .from(repositories)
    .where(
      and(eq(repositories.ownerId, owner.id), eq(repositories.name, repoName))
    )
    .limit(1);
  if (!repo) return null;
  return { owner, repo };
}

// PR Nav helper
const PrNav = ({
  owner,
  repo,
  active,
}: {
  owner: string;
  repo: string;
  active: "code" | "issues" | "pulls" | "commits";
}) => (
  <div class="repo-nav">
    <a href={`/${owner}/${repo}`} class={active === "code" ? "active" : ""}>
      Code
    </a>
    <a
      href={`/${owner}/${repo}/issues`}
      class={active === "issues" ? "active" : ""}
    >
      Issues
    </a>
    <a
      href={`/${owner}/${repo}/pulls`}
      class={active === "pulls" ? "active" : ""}
    >
      Pull Requests
    </a>
    <a
      href={`/${owner}/${repo}/commits`}
      class={active === "commits" ? "active" : ""}
    >
      Commits
    </a>
  </div>
);

// List PRs
pulls.get("/:owner/:repo/pulls", softAuth, async (c) => {
  const { owner: ownerName, repo: repoName } = c.req.param();
  const user = c.get("user");
  const state = c.req.query("state") || "open";

  const resolved = await resolveRepo(ownerName, repoName);
  if (!resolved) return c.notFound();

  // "draft" is a virtual filter — rows are state='open' + isDraft=true.
  const stateFilter =
    state === "draft"
      ? and(
          eq(pullRequests.state, "open"),
          eq(pullRequests.isDraft, true)
        )
      : eq(pullRequests.state, state);

  const prList = await db
    .select({
      pr: pullRequests,
      author: { username: users.username },
    })
    .from(pullRequests)
    .innerJoin(users, eq(pullRequests.authorId, users.id))
    .where(
      and(eq(pullRequests.repositoryId, resolved.repo.id), stateFilter)
    )
    .orderBy(desc(pullRequests.createdAt));

  const [counts] = await db
    .select({
      open: sql<number>`count(*) filter (where ${pullRequests.state} = 'open')`,
      draft: sql<number>`count(*) filter (where ${pullRequests.state} = 'open' and ${pullRequests.isDraft} = true)`,
      closed: sql<number>`count(*) filter (where ${pullRequests.state} = 'closed')`,
      merged: sql<number>`count(*) filter (where ${pullRequests.state} = 'merged')`,
    })
    .from(pullRequests)
    .where(eq(pullRequests.repositoryId, resolved.repo.id));

  return c.html(
    <Layout title={`Pull Requests — ${ownerName}/${repoName}`} user={user}>
      <RepoHeader owner={ownerName} repo={repoName} />
      <PrNav owner={ownerName} repo={repoName} active="pulls" />
      <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px">
        <div class="issue-tabs">
          <a
            href={`/${ownerName}/${repoName}/pulls?state=open`}
            class={state === "open" ? "active" : ""}
          >
            {counts?.open ?? 0} Open
          </a>
          <a
            href={`/${ownerName}/${repoName}/pulls?state=draft`}
            class={state === "draft" ? "active" : ""}
          >
            {counts?.draft ?? 0} Draft
          </a>
          <a
            href={`/${ownerName}/${repoName}/pulls?state=merged`}
            class={state === "merged" ? "active" : ""}
          >
            {counts?.merged ?? 0} Merged
          </a>
          <a
            href={`/${ownerName}/${repoName}/pulls?state=closed`}
            class={state === "closed" ? "active" : ""}
          >
            {counts?.closed ?? 0} Closed
          </a>
        </div>
        {user && (
          <a
            href={`/${ownerName}/${repoName}/pulls/new`}
            class="btn btn-primary"
          >
            New pull request
          </a>
        )}
      </div>
      {prList.length === 0 ? (
        <div class="empty-state">
          <p>No {state} pull requests.</p>
        </div>
      ) : (
        <div class="issue-list">
          {prList.map(({ pr, author }) => {
            const isDraft = pr.state === "open" && pr.isDraft;
            const stateClass = isDraft
              ? "state-draft"
              : pr.state === "open"
                ? "state-open"
                : pr.state === "merged"
                  ? "state-merged"
                  : "state-closed";
            const stateIcon = isDraft
              ? "\u270E"
              : pr.state === "open"
                ? "\u25CB"
                : pr.state === "merged"
                  ? "\u2B8C"
                  : "\u2713";
            return (
              <div class="issue-item">
                <div class={`issue-state-icon ${stateClass}`}>{stateIcon}</div>
                <div>
                  <div class="issue-title">
                    <a href={`/${ownerName}/${repoName}/pulls/${pr.number}`}>
                      {pr.title}
                    </a>
                    {isDraft && (
                      <span class="issue-badge draft-badge" style="margin-left: 8px; font-size: 11px; padding: 2px 8px">
                        Draft
                      </span>
                    )}
                  </div>
                  <div class="issue-meta">
                    #{pr.number}{" "}
                    {pr.headBranch} → {pr.baseBranch}{" "}
                    by {author.username}{" "}
                    {formatRelative(pr.createdAt)}
                  </div>
                </div>
              </div>
            );
          })}
        </div>
      )}
    </Layout>
  );
});

// New PR form
pulls.get(
  "/:owner/:repo/pulls/new",
  softAuth,
  requireAuth,
  async (c) => {
    const { owner: ownerName, repo: repoName } = c.req.param();
    const user = c.get("user")!;
    const branches = await listBranches(ownerName, repoName);
    const error = c.req.query("error");
    const defaultBase = branches.includes("main") ? "main" : branches[0] || "";
    const template = await loadPrTemplate(ownerName, repoName);

    return c.html(
      <Layout title={`New PR — ${ownerName}/${repoName}`} user={user}>
        <RepoHeader owner={ownerName} repo={repoName} />
        <PrNav owner={ownerName} repo={repoName} active="pulls" />
        <div style="max-width: 800px">
          <h2 style="margin-bottom: 16px">Open a pull request</h2>
          {template && (
            <div style="font-size: 12px; color: var(--text-muted); margin-bottom: 8px">
              Using <code>PULL_REQUEST_TEMPLATE.md</code> from the default branch.
            </div>
          )}
          {error && (
            <div class="auth-error">{decodeURIComponent(error)}</div>
          )}
          <form method="POST" action={`/${ownerName}/${repoName}/pulls/new`}>
            <div style="display: flex; gap: 12px; align-items: center; margin-bottom: 16px">
              <select name="base" style="padding: 6px 12px; background: var(--bg-tertiary); border: 1px solid var(--border); border-radius: var(--radius); color: var(--text); font-size: 13px">
                {branches.map((b) => (
                  <option value={b} selected={b === defaultBase}>
                    {b}
                  </option>
                ))}
              </select>
              <span style="color: var(--text-muted)">&larr;</span>
              <select name="head" style="padding: 6px 12px; background: var(--bg-tertiary); border: 1px solid var(--border); border-radius: var(--radius); color: var(--text); font-size: 13px">
                {branches
                  .filter((b) => b !== defaultBase)
                  .concat(defaultBase === branches[0] ? [] : [branches[0]])
                  .map((b) => (
                    <option value={b}>{b}</option>
                  ))}
              </select>
            </div>
            <div class="form-group">
              <input
                type="text"
                name="title"
                required
                placeholder="Title"
                style="font-size: 16px; padding: 10px 14px"
              />
            </div>
            <div class="form-group">
              <textarea
                name="body"
                rows={8}
                placeholder="Description (Markdown supported)"
                style="font-family: var(--font-mono); font-size: 13px"
              >
                {template || ""}
              </textarea>
            </div>
            <div style="display: flex; gap: 8px">
              <button type="submit" class="btn btn-primary">
                Create pull request
              </button>
              <button
                type="submit"
                name="draft"
                value="1"
                class="btn"
                title="Create a draft PR — skips AI review and cannot be merged until marked ready"
              >
                Create draft
              </button>
            </div>
          </form>
        </div>
      </Layout>
    );
  }
);

// Create PR
pulls.post(
  "/:owner/:repo/pulls/new",
  softAuth,
  requireAuth,
  async (c) => {
    const { owner: ownerName, repo: repoName } = c.req.param();
    const user = c.get("user")!;
    const body = await c.req.parseBody();
    const title = String(body.title || "").trim();
    const prBody = String(body.body || "").trim();
    const baseBranch = String(body.base || "main");
    const headBranch = String(body.head || "");

    if (!title || !headBranch) {
      return c.redirect(
        `/${ownerName}/${repoName}/pulls/new?error=Title+and+branches+are+required`
      );
    }

    if (baseBranch === headBranch) {
      return c.redirect(
        `/${ownerName}/${repoName}/pulls/new?error=Base+and+head+branches+must+be+different`
      );
    }

    const resolved = await resolveRepo(ownerName, repoName);
    if (!resolved) return c.redirect(`/${ownerName}/${repoName}`);

    const isDraft = String(body.draft || "") === "1";

    const [pr] = await db
      .insert(pullRequests)
      .values({
        repositoryId: resolved.repo.id,
        authorId: user.id,
        title,
        body: prBody || null,
        baseBranch,
        headBranch,
        isDraft,
      })
      .returning();

    // Skip AI review on drafts — it runs again when the PR is marked ready.
    if (!isDraft && isAiReviewEnabled()) {
      triggerAiReview(ownerName, repoName, pr.id, title, prBody, baseBranch, headBranch).catch(
        (err) => console.error("[ai-review] Failed:", err)
      );
    }

    // D3 — fire-and-forget AI triage: suggest labels/reviewers on the PR.
    triggerPrTriage({
      ownerName,
      repoName,
      repositoryId: resolved.repo.id,
      prId: pr.id,
      prAuthorId: user.id,
      title,
      body: prBody,
      baseBranch,
      headBranch,
    }).catch((err) => console.error("[pr-triage] Failed:", err));

    // J11 — fire-and-forget CODEOWNERS auto-assign. Reads diff paths
    // between base..head and requests review from the matched owners.
    (async () => {
      try {
        const repoDir = getRepoPath(ownerName, repoName);
        const statProc = Bun.spawn(
          ["git", "diff", "--numstat", `${baseBranch}...${headBranch}`],
          { cwd: repoDir, stdout: "pipe", stderr: "pipe" }
        );
        const stat = await new Response(statProc.stdout).text();
        await statProc.exited;
        const changedPaths = stat
          .trim()
          .split("\n")
          .filter(Boolean)
          .map((line) => line.split("\t")[2])
          .filter(Boolean);
        await autoAssignFromCodeowners({
          repositoryId: resolved.repo.id,
          pullRequestId: pr.id,
          authorId: user.id,
          changedPaths,
          prUrl: `/${ownerName}/${repoName}/pulls/${pr.number}`,
          prTitle: title,
        });
      } catch (err) {
        console.error("[codeowners-assign] Failed:", err);
      }
    })();

    return c.redirect(`/${ownerName}/${repoName}/pulls/${pr.number}`);
  }
);

// View single PR
pulls.get("/:owner/:repo/pulls/:number", softAuth, async (c) => {
  const { owner: ownerName, repo: repoName } = c.req.param();
  const prNum = parseInt(c.req.param("number"), 10);
  const user = c.get("user");
  const tab = c.req.query("tab") || "conversation";

  const resolved = await resolveRepo(ownerName, repoName);
  if (!resolved) return c.notFound();

  const [pr] = await db
    .select()
    .from(pullRequests)
    .where(
      and(
        eq(pullRequests.repositoryId, resolved.repo.id),
        eq(pullRequests.number, prNum)
      )
    )
    .limit(1);

  if (!pr) return c.notFound();

  const [author] = await db
    .select()
    .from(users)
    .where(eq(users.id, pr.authorId))
    .limit(1);

  const comments = await db
    .select({
      comment: prComments,
      author: { username: users.username },
    })
    .from(prComments)
    .innerJoin(users, eq(prComments.authorId, users.id))
    .where(eq(prComments.pullRequestId, pr.id))
    .orderBy(asc(prComments.createdAt));

  // Reactions for the PR body + each comment, in parallel.
  const [prReactions, ...prCommentReactions] = await Promise.all([
    summariseReactions("pr", pr.id, user?.id),
    ...comments.map((row) =>
      summariseReactions("pr_comment", row.comment.id, user?.id)
    ),
  ]);

  const canManage =
    user &&
    (user.id === resolved.owner.id || user.id === pr.authorId);

  const error = c.req.query("error");

  // Get gate check status for open PRs
  let gateChecks: GateCheckResult[] = [];
  if (pr.state === "open") {
    const headSha = await resolveRef(ownerName, repoName, pr.headBranch);
    if (headSha) {
      const aiComments = comments.filter(({ comment }) => comment.isAiReview);
      const aiApproved = aiComments.length === 0 || aiComments.some(
        ({ comment }) => comment.body.includes("**Approved**")
      );
      const gateResult = await runAllGateChecks(
        ownerName, repoName, pr.baseBranch, pr.headBranch, headSha, aiApproved
      );
      gateChecks = gateResult.checks;
    }
  }

  // J11 — requested reviewers (CODEOWNERS auto-assign + manual add).
  const reviewRequests = await listReviewRequestsForPr(pr.id);

  // J16 — auto-merge opt-in state.
  let autoMergeState: {
    enabled: boolean;
    mergeMethod: string;
    lastStatus: string | null;
    notifiedReady: boolean;
  } = { enabled: false, mergeMethod: "merge", lastStatus: null, notifiedReady: false };
  try {
    const { getAutoMergeForPr } = await import("../lib/pr-auto-merge");
    const am = await getAutoMergeForPr(pr.id);
    if (am) autoMergeState = am as typeof autoMergeState;
  } catch {
    /* ignore */
  }

  // Get diff for "Files changed" tab
  let diffRaw = "";
  let diffFiles: GitDiffFile[] = [];
  if (tab === "files") {
    const repoDir = getRepoPath(ownerName, repoName);
    const proc = Bun.spawn(
      ["git", "diff", `${pr.baseBranch}...${pr.headBranch}`],
      { cwd: repoDir, stdout: "pipe", stderr: "pipe" }
    );
    diffRaw = await new Response(proc.stdout).text();
    await proc.exited;

    const statProc = Bun.spawn(
      ["git", "diff", "--numstat", `${pr.baseBranch}...${pr.headBranch}`],
      { cwd: repoDir, stdout: "pipe", stderr: "pipe" }
    );
    const stat = await new Response(statProc.stdout).text();
    await statProc.exited;

    diffFiles = stat
      .trim()
      .split("\n")
      .filter(Boolean)
      .map((line) => {
        const [add, del, filePath] = line.split("\t");
        return {
          path: filePath,
          status: "modified",
          additions: add === "-" ? 0 : parseInt(add, 10),
          deletions: del === "-" ? 0 : parseInt(del, 10),
          patch: "",
        };
      });
  }

  return c.html(
    <Layout
      title={`${pr.title} #${pr.number} — ${ownerName}/${repoName}`}
      user={user}
    >
      <RepoHeader owner={ownerName} repo={repoName} />
      <PrNav owner={ownerName} repo={repoName} active="pulls" />
      <div class="issue-detail">
        <h2>
          {pr.title}{" "}
          <span style="color: var(--text-muted); font-weight: 400">
            #{pr.number}
          </span>
        </h2>
        <div style="margin: 8px 0 20px; display: flex; align-items: center; gap: 8px">
          {pr.state === "open" && pr.isDraft ? (
            <span class="issue-badge draft-badge">
              {"\u270E Draft"}
            </span>
          ) : (
            <span
              class={`issue-badge ${pr.state === "open" ? "badge-open" : pr.state === "merged" ? "badge-merged" : "badge-closed"}`}
            >
              {pr.state === "open"
                ? "\u25CB Open"
                : pr.state === "merged"
                  ? "\u2B8C Merged"
                  : "\u2713 Closed"}
            </span>
          )}
          <span style="color: var(--text-muted); font-size: 14px">
            <strong style="color: var(--text)">
              {author?.username}
            </strong>{" "}
            wants to merge <code>{pr.headBranch}</code> into{" "}
            <code>{pr.baseBranch}</code>
          </span>
        </div>

        <ReviewersPanel
          reviewers={reviewRequests}
          canManage={canManage}
          ownerName={ownerName}
          repoName={repoName}
          prNumber={pr.number}
        />

        <div class="issue-tabs" style="margin-bottom: 20px">
          <a
            href={`/${ownerName}/${repoName}/pulls/${pr.number}`}
            class={tab === "conversation" ? "active" : ""}
          >
            Conversation
          </a>
          <a
            href={`/${ownerName}/${repoName}/pulls/${pr.number}?tab=files`}
            class={tab === "files" ? "active" : ""}
          >
            Files changed
          </a>
        </div>

        {tab === "files" ? (
          <DiffView raw={diffRaw} files={diffFiles} />
        ) : (
          <>
            {pr.body && (
              <div class="issue-comment-box">
                <div class="comment-header">
                  <strong>{author?.username}</strong> commented{" "}
                  {formatRelative(pr.createdAt)}
                </div>
                <div class="markdown-body">
                  {html([renderMarkdown(pr.body)] as unknown as TemplateStringsArray)}
                </div>
                <div style="padding: 0 16px 12px">
                  <ReactionsBar
                    targetType="pr"
                    targetId={pr.id}
                    summaries={prReactions}
                    canReact={!!user}
                  />
                </div>
              </div>
            )}

            {comments.map(({ comment, author: commentAuthor }, i) => (
              <div
                class={`issue-comment-box ${comment.isAiReview ? "ai-review" : ""}`}
              >
                <div class="comment-header">
                  <strong>{commentAuthor.username}</strong>
                  {comment.isAiReview && (
                    <span class="badge" style="margin-left: 8px; background: rgba(31, 111, 235, 0.15); color: var(--text-link); border-color: var(--accent)">
                      AI Review
                    </span>
                  )}
                  {" "}
                  commented {formatRelative(comment.createdAt)}
                  {comment.filePath && (
                    <span style="margin-left: 8px; font-family: var(--font-mono); font-size: 11px">
                      {comment.filePath}
                      {comment.lineNumber ? `:${comment.lineNumber}` : ""}
                    </span>
                  )}
                </div>
                <div class="markdown-body">
                  {html([renderMarkdown(comment.body)] as unknown as TemplateStringsArray)}
                </div>
                {(() => {
                  const suggestions = extractSuggestions(comment.body);
                  if (
                    suggestions.length === 0 ||
                    !comment.filePath ||
                    !comment.lineNumber ||
                    pr.state !== "open"
                  ) {
                    return null as unknown as JSX.Element;
                  }
                  const canApply =
                    !!user &&
                    (user.id === resolved.owner.id ||
                      user.id === pr.authorId ||
                      user.id === comment.authorId);
                  if (!canApply) return null as unknown as JSX.Element;
                  return (
                    <div style="padding: 0 16px 12px; display: flex; gap: 6px; flex-wrap: wrap; border-top: 1px solid var(--border); margin-top: 4px; padding-top: 10px">
                      {suggestions.map((_, si) => (
                        <form
                          method="POST"
                          action={`/${ownerName}/${repoName}/pulls/${pr.number}/comments/${comment.id}/apply-suggestion`}
                          style="display: inline"
                        >
                          <input type="hidden" name="index" value={String(si)} />
                          <button
                            type="submit"
                            class="btn btn-primary"
                            style="padding: 3px 10px; font-size: 12px"
                          >
                            {suggestions.length === 1
                              ? "Commit suggestion"
                              : `Commit suggestion ${si + 1}`}
                          </button>
                        </form>
                      ))}
                    </div>
                  );
                })()}
                <div style="padding: 0 16px 12px">
                  <ReactionsBar
                    targetType="pr_comment"
                    targetId={comment.id}
                    summaries={prCommentReactions[i] || []}
                    canReact={!!user}
                  />
                </div>
              </div>
            ))}

            {error && (
              <div class="auth-error" style="margin-top: 16px; padding: 12px; background: rgba(248, 81, 73, 0.1); border: 1px solid var(--red); border-radius: var(--radius); color: var(--red)">
                {decodeURIComponent(error)}
              </div>
            )}

            {pr.state === "open" && canManage && (
              <AutoMergePanel
                owner={ownerName}
                repo={repoName}
                prNumber={pr.number}
                state={autoMergeState}
              />
            )}

            {pr.state === "open" && gateChecks.length > 0 && (
              <div style="margin-top: 20px; padding: 16px; background: var(--bg-secondary); border: 1px solid var(--border); border-radius: var(--radius)">
                <h3 style="margin: 0 0 12px; font-size: 14px">Gate Checks</h3>
                {gateChecks.map((check) => (
                  <div style="display: flex; align-items: center; gap: 8px; padding: 6px 0; border-bottom: 1px solid var(--border)">
                    <span style={`font-size: 16px; color: ${check.passed ? "var(--green)" : "var(--red)"}`}>
                      {check.passed ? "\u2713" : "\u2717"}
                    </span>
                    <strong style="font-size: 13px">{check.name}</strong>
                    <span style="font-size: 12px; color: var(--text-muted); margin-left: auto">{check.details}</span>
                  </div>
                ))}
                <div style="margin-top: 8px; font-size: 12px; color: var(--text-muted)">
                  {gateChecks.every((c) => c.passed)
                    ? "All checks passed — ready to merge"
                    : gateChecks.some((c) => !c.passed && c.name === "Merge check")
                      ? "Conflicts detected — GlueCron AI will attempt auto-resolution on merge"
                      : "Some checks failed — resolve issues before merging"}
                </div>
              </div>
            )}

            {user && pr.state === "open" && (
              <div style="margin-top: 20px">
                <form
                  method="POST"
                  action={`/${ownerName}/${repoName}/pulls/${pr.number}/comment`}
                >
                  <div class="form-group">
                    <textarea
                      name="body"
                      rows={6}
                      required
                      placeholder="Leave a comment... (Markdown supported)"
                      style="font-family: var(--font-mono); font-size: 13px"
                    />
                  </div>
                  <div style="display: flex; gap: 8px">
                    <button type="submit" class="btn btn-primary">
                      Comment
                    </button>
                    {canManage && (
                      <>
                        {pr.isDraft ? (
                          <button
                            type="submit"
                            formaction={`/${ownerName}/${repoName}/pulls/${pr.number}/ready`}
                            class="btn"
                            style="background: rgba(63, 185, 80, 0.15); border-color: var(--green); color: var(--green)"
                            title="Mark this draft PR as ready for review — triggers AI review"
                          >
                            Ready for review
                          </button>
                        ) : (
                          <>
                            <button
                              type="submit"
                              formaction={`/${ownerName}/${repoName}/pulls/${pr.number}/merge`}
                              class="btn"
                              style={`background: ${gateChecks.every((c) => c.passed) ? "rgba(63, 185, 80, 0.15)" : "rgba(248, 81, 73, 0.1)"}; border-color: ${gateChecks.every((c) => c.passed) ? "var(--green)" : "var(--red)"}; color: ${gateChecks.every((c) => c.passed) ? "var(--green)" : "var(--red)"}`}
                            >
                              {gateChecks.every((c) => c.passed)
                                ? "Merge pull request"
                                : gateChecks.some((c) => !c.passed && c.name === "Merge check")
                                  ? "Merge with auto-resolve"
                                  : "Merge pull request"}
                            </button>
                            <button
                              type="submit"
                              formaction={`/${ownerName}/${repoName}/pulls/${pr.number}/enqueue`}
                              class="btn"
                              title="Queue this PR — gates will re-run against latest base before merge"
                            >
                              Add to merge queue
                            </button>
                            <button
                              type="submit"
                              formaction={`/${ownerName}/${repoName}/pulls/${pr.number}/draft`}
                              class="btn"
                              title="Convert back to draft"
                            >
                              Convert to draft
                            </button>
                          </>
                        )}
                        <button
                          type="submit"
                          formaction={`/${ownerName}/${repoName}/pulls/${pr.number}/close`}
                          class="btn btn-danger"
                        >
                          Close
                        </button>
                      </>
                    )}
                  </div>
                </form>
              </div>
            )}
          </>
        )}
      </div>
    </Layout>
  );
});

// Add comment to PR
pulls.post(
  "/:owner/:repo/pulls/:number/comment",
  softAuth,
  requireAuth,
  async (c) => {
    const { owner: ownerName, repo: repoName } = c.req.param();
    const prNum = parseInt(c.req.param("number"), 10);
    const user = c.get("user")!;
    const body = await c.req.parseBody();
    const commentBody = String(body.body || "").trim();

    if (!commentBody) {
      return c.redirect(`/${ownerName}/${repoName}/pulls/${prNum}`);
    }

    const resolved = await resolveRepo(ownerName, repoName);
    if (!resolved) return c.redirect(`/${ownerName}/${repoName}`);

    const [pr] = await db
      .select()
      .from(pullRequests)
      .where(
        and(
          eq(pullRequests.repositoryId, resolved.repo.id),
          eq(pullRequests.number, prNum)
        )
      )
      .limit(1);

    if (!pr) return c.redirect(`/${ownerName}/${repoName}/pulls`);

    await db.insert(prComments).values({
      pullRequestId: pr.id,
      authorId: user.id,
      body: commentBody,
    });

    return c.redirect(`/${ownerName}/${repoName}/pulls/${prNum}`);
  }
);

// Merge PR — with green gate enforcement and auto conflict resolution
pulls.post(
  "/:owner/:repo/pulls/:number/merge",
  softAuth,
  requireAuth,
  async (c) => {
    const { owner: ownerName, repo: repoName } = c.req.param();
    const prNum = parseInt(c.req.param("number"), 10);
    const user = c.get("user")!;

    const resolved = await resolveRepo(ownerName, repoName);
    if (!resolved) return c.redirect(`/${ownerName}/${repoName}`);

    const [pr] = await db
      .select()
      .from(pullRequests)
      .where(
        and(
          eq(pullRequests.repositoryId, resolved.repo.id),
          eq(pullRequests.number, prNum)
        )
      )
      .limit(1);

    if (!pr || pr.state !== "open") {
      return c.redirect(`/${ownerName}/${repoName}/pulls/${prNum}`);
    }

    // Draft PRs cannot be merged — must be marked ready first.
    if (pr.isDraft) {
      return c.redirect(
        `/${ownerName}/${repoName}/pulls/${prNum}?error=${encodeURIComponent(
          "This PR is a draft. Mark it as ready for review before merging."
        )}`
      );
    }

    // Resolve head SHA
    const headSha = await resolveRef(ownerName, repoName, pr.headBranch);
    if (!headSha) {
      return c.redirect(
        `/${ownerName}/${repoName}/pulls/${prNum}?error=${encodeURIComponent("Head branch not found")}`
      );
    }

    // Check if AI review approved this PR
    const aiComments = await db
      .select()
      .from(prComments)
      .where(
        and(
          eq(prComments.pullRequestId, pr.id),
          eq(prComments.isAiReview, true)
        )
      );
    const aiApproved = aiComments.length === 0 || aiComments.some(
      (c) => c.body.includes("**Approved**") || c.body.includes("approved: true") || c.body.toLowerCase().includes("lgtm")
    );

    // Run all green gate checks (GateTest + mergeability + AI review)
    const gateResult = await runAllGateChecks(
      ownerName,
      repoName,
      pr.baseBranch,
      pr.headBranch,
      headSha,
      aiApproved
    );

    // If GateTest or AI review failed (hard blocks), reject the merge
    const hardFailures = gateResult.checks.filter(
      (check) => !check.passed && check.name !== "Merge check"
    );
    if (hardFailures.length > 0) {
      const errorMsg = hardFailures
        .map((f) => `${f.name}: ${f.details}`)
        .join("; ");
      return c.redirect(
        `/${ownerName}/${repoName}/pulls/${prNum}?error=${encodeURIComponent(errorMsg)}`
      );
    }

    // D5 — Branch-protection enforcement. Looks up the matching rule for the
    // base branch and blocks the merge if requireAiApproval / requireGreenGates
    // / requireHumanReview / requiredApprovals are not satisfied. Independent
    // of repo-global settings, so owners can lock specific branches down
    // further than the repo default.
    const protectionRule = await matchProtection(
      resolved.repo.id,
      pr.baseBranch
    );
    if (protectionRule) {
      const humanApprovals = await countHumanApprovals(pr.id);
      const required = await listRequiredChecks(protectionRule.id);
      const passingNames = required.length > 0
        ? await passingCheckNames(resolved.repo.id, headSha)
        : [];
      const decision = evaluateProtection(
        protectionRule,
        {
          aiApproved,
          humanApprovalCount: humanApprovals,
          gateResultGreen: hardFailures.length === 0,
          hasFailedGates: hardFailures.length > 0,
          passingCheckNames: passingNames,
        },
        required.map((r) => r.checkName)
      );
      if (!decision.allowed) {
        return c.redirect(
          `/${ownerName}/${repoName}/pulls/${prNum}?error=${encodeURIComponent(
            decision.reasons.join(" ")
          )}`
        );
      }
    }

    // Attempt the merge — with auto conflict resolution if needed
    const repoDir = getRepoPath(ownerName, repoName);
    const mergeCheck = gateResult.checks.find((c) => c.name === "Merge check");
    const hasConflicts = mergeCheck && !mergeCheck.passed;

    if (hasConflicts && isAiReviewEnabled()) {
      // Use Claude to auto-resolve conflicts
      const mergeResult = await mergeWithAutoResolve(
        ownerName,
        repoName,
        pr.baseBranch,
        pr.headBranch,
        `Merge pull request #${pr.number}: ${pr.title}`
      );

      if (!mergeResult.success) {
        return c.redirect(
          `/${ownerName}/${repoName}/pulls/${prNum}?error=${encodeURIComponent(mergeResult.error || "Auto-merge failed")}`
        );
      }

      // Post a comment about the auto-resolution
      if (mergeResult.resolvedFiles.length > 0) {
        await db.insert(prComments).values({
          pullRequestId: pr.id,
          authorId: user.id,
          body: `**Auto-resolved merge conflicts** in:\n${mergeResult.resolvedFiles.map((f) => `- \`${f}\``).join("\n")}\n\nConflicts were automatically resolved by GlueCron AI.`,
          isAiReview: true,
        });
      }
    } else {
      // Standard merge — fast-forward or clean merge
      const ffProc = Bun.spawn(
        [
          "git",
          "update-ref",
          `refs/heads/${pr.baseBranch}`,
          `refs/heads/${pr.headBranch}`,
        ],
        { cwd: repoDir, stdout: "pipe", stderr: "pipe" }
      );
      const ffExit = await ffProc.exited;

      if (ffExit !== 0) {
        return c.redirect(
          `/${ownerName}/${repoName}/pulls/${prNum}?error=${encodeURIComponent("Merge failed — unable to update branch ref")}`
        );
      }
    }

    await db
      .update(pullRequests)
      .set({
        state: "merged",
        mergedAt: new Date(),
        mergedBy: user.id,
        updatedAt: new Date(),
      })
      .where(eq(pullRequests.id, pr.id));

    // J7 — closing keywords. Scan PR title + body for "closes #N" style refs
    // and auto-close each matching open issue with a back-link comment. Bounded
    // to the same repo for v1 (cross-repo refs ignored). Failures never block
    // the merge redirect.
    try {
      const { extractClosingRefsMulti } = await import("../lib/close-keywords");
      const refs = extractClosingRefsMulti([pr.title, pr.body]);
      for (const n of refs) {
        const [issue] = await db
          .select()
          .from(issues)
          .where(
            and(
              eq(issues.repositoryId, resolved.repo.id),
              eq(issues.number, n)
            )
          )
          .limit(1);
        if (!issue || issue.state !== "open") continue;
        await db
          .update(issues)
          .set({ state: "closed", closedAt: new Date(), updatedAt: new Date() })
          .where(eq(issues.id, issue.id));
        await db.insert(issueComments).values({
          issueId: issue.id,
          authorId: user.id,
          body: `Closed by pull request #${pr.number}.`,
        });
      }
    } catch {
      // Never block the merge on close-keyword failures.
    }

    return c.redirect(`/${ownerName}/${repoName}/pulls/${prNum}`);
  }
);

// Toggle draft state — mark a PR as "ready for review". Triggers AI review if it
// hasn't run yet on this PR.
pulls.post(
  "/:owner/:repo/pulls/:number/ready",
  softAuth,
  requireAuth,
  async (c) => {
    const { owner: ownerName, repo: repoName } = c.req.param();
    const prNum = parseInt(c.req.param("number"), 10);
    const user = c.get("user")!;

    const resolved = await resolveRepo(ownerName, repoName);
    if (!resolved) return c.redirect(`/${ownerName}/${repoName}`);

    const [pr] = await db
      .select()
      .from(pullRequests)
      .where(
        and(
          eq(pullRequests.repositoryId, resolved.repo.id),
          eq(pullRequests.number, prNum)
        )
      )
      .limit(1);
    if (!pr) return c.redirect(`/${ownerName}/${repoName}/pulls`);

    // Only the author or repo owner can toggle draft state.
    if (pr.authorId !== user.id && resolved.owner.id !== user.id) {
      return c.redirect(`/${ownerName}/${repoName}/pulls/${prNum}`);
    }

    if (pr.state === "open" && pr.isDraft) {
      await db
        .update(pullRequests)
        .set({ isDraft: false, updatedAt: new Date() })
        .where(eq(pullRequests.id, pr.id));

      if (isAiReviewEnabled()) {
        triggerAiReview(
          ownerName,
          repoName,
          pr.id,
          pr.title,
          pr.body,
          pr.baseBranch,
          pr.headBranch
        ).catch((err) => console.error("[ai-review] ready trigger failed:", err));
      }
    }

    return c.redirect(`/${ownerName}/${repoName}/pulls/${prNum}`);
  }
);

// Convert a PR back to draft.
pulls.post(
  "/:owner/:repo/pulls/:number/draft",
  softAuth,
  requireAuth,
  async (c) => {
    const { owner: ownerName, repo: repoName } = c.req.param();
    const prNum = parseInt(c.req.param("number"), 10);
    const user = c.get("user")!;

    const resolved = await resolveRepo(ownerName, repoName);
    if (!resolved) return c.redirect(`/${ownerName}/${repoName}`);

    const [pr] = await db
      .select()
      .from(pullRequests)
      .where(
        and(
          eq(pullRequests.repositoryId, resolved.repo.id),
          eq(pullRequests.number, prNum)
        )
      )
      .limit(1);
    if (!pr) return c.redirect(`/${ownerName}/${repoName}/pulls`);

    if (pr.authorId !== user.id && resolved.owner.id !== user.id) {
      return c.redirect(`/${ownerName}/${repoName}/pulls/${prNum}`);
    }

    if (pr.state === "open" && !pr.isDraft) {
      await db
        .update(pullRequests)
        .set({ isDraft: true, updatedAt: new Date() })
        .where(eq(pullRequests.id, pr.id));
    }

    return c.redirect(`/${ownerName}/${repoName}/pulls/${prNum}`);
  }
);

// Close PR
pulls.post(
  "/:owner/:repo/pulls/:number/close",
  softAuth,
  requireAuth,
  async (c) => {
    const { owner: ownerName, repo: repoName } = c.req.param();
    const prNum = parseInt(c.req.param("number"), 10);

    const resolved = await resolveRepo(ownerName, repoName);
    if (!resolved) return c.redirect(`/${ownerName}/${repoName}`);

    await db
      .update(pullRequests)
      .set({
        state: "closed",
        closedAt: new Date(),
        updatedAt: new Date(),
      })
      .where(
        and(
          eq(pullRequests.repositoryId, resolved.repo.id),
          eq(pullRequests.number, prNum)
        )
      );

    return c.redirect(`/${ownerName}/${repoName}/pulls/${prNum}`);
  }
);

/**
 * Trigger AI code review asynchronously after PR creation.
 * Runs the diff through Claude and posts review comments.
 */
async function triggerAiReview(
  ownerName: string,
  repoName: string,
  prId: string,
  title: string,
  body: string | null,
  baseBranch: string,
  headBranch: string
): Promise<void> {
  const repoDir = getRepoPath(ownerName, repoName);

  // Get the diff between branches
  const proc = Bun.spawn(
    ["git", "diff", `${baseBranch}...${headBranch}`],
    { cwd: repoDir, stdout: "pipe", stderr: "pipe" }
  );
  const diffText = await new Response(proc.stdout).text();
  await proc.exited;

  if (!diffText.trim()) return;

  const result = await reviewDiff(
    `${ownerName}/${repoName}`,
    title,
    body,
    baseBranch,
    headBranch,
    diffText
  );

  // We need a system user for AI reviews — use the PR author for now
  // Get the PR to find the author
  const [pr] = await db
    .select()
    .from(pullRequests)
    .where(eq(pullRequests.id, prId))
    .limit(1);

  if (!pr) return;

  // Post summary comment
  const statusEmoji = result.approved ? "**Approved**" : "**Changes Requested**";
  let commentBody = `## AI Code Review ${statusEmoji}\n\n${result.summary}`;

  if (result.comments.length > 0) {
    commentBody += "\n\n### Issues Found\n";
    for (const comment of result.comments) {
      const location = comment.filePath
        ? `\`${comment.filePath}${comment.lineNumber ? `:${comment.lineNumber}` : ""}\``
        : "";
      commentBody += `\n---\n${location}\n\n${comment.body}\n`;
    }
  }

  await db.insert(prComments).values({
    pullRequestId: prId,
    authorId: pr.authorId,
    body: commentBody,
    isAiReview: true,
  });

  // Post individual file-level comments
  for (const comment of result.comments) {
    if (comment.filePath) {
      await db.insert(prComments).values({
        pullRequestId: prId,
        authorId: pr.authorId,
        body: comment.body,
        isAiReview: true,
        filePath: comment.filePath,
        lineNumber: comment.lineNumber,
      });
    }
  }

  console.log(
    `[ai-review] Review posted for PR ${prId}: ${result.approved ? "approved" : "changes requested"}, ${result.comments.length} comments`
  );
}

/**
 * D3 — AI PR triage. Runs Claude Haiku on the PR title/body + diff summary and
 * posts an AI-authored comment suggesting labels, reviewers, and priority.
 * Nothing is auto-applied — the PR author remains in control.
 */
async function triggerPrTriage(args: {
  ownerName: string;
  repoName: string;
  repositoryId: string;
  prId: string;
  prAuthorId: string;
  title: string;
  body: string;
  baseBranch: string;
  headBranch: string;
}): Promise<void> {
  try {
    // Gather candidate reviewers (top contributors from recent commits).
    const repoDir = getRepoPath(args.ownerName, args.repoName);
    const shortlogProc = Bun.spawn(
      ["git", "shortlog", "-sn", "--no-merges", "-50", args.baseBranch],
      { cwd: repoDir, stdout: "pipe", stderr: "pipe" }
    );
    const shortlogOut = await new Response(shortlogProc.stdout).text();
    await shortlogProc.exited;
    const authorNames = shortlogOut
      .trim()
      .split("\n")
      .map((l) => l.trim().split(/\s+/).slice(1).join(" "))
      .filter(Boolean)
      .slice(0, 10);

    // Look up usernames matching these author names (best-effort match).
    const candidateUsernames: string[] = [];
    if (authorNames.length > 0) {
      try {
        const matches = await db
          .select({ username: users.username, displayName: users.displayName })
          .from(users)
          .limit(100);
        for (const u of matches) {
          if (
            authorNames.some(
              (n) =>
                u.username === n ||
                (u.displayName && u.displayName === n)
            )
          ) {
            candidateUsernames.push(u.username);
          }
        }
      } catch {
        /* ignore */
      }
    }
    // Always include the repo owner as a candidate reviewer.
    try {
      const [ownerRow] = await db
        .select({ username: users.username })
        .from(users)
        .where(eq(users.username, args.ownerName))
        .limit(1);
      if (ownerRow && !candidateUsernames.includes(ownerRow.username)) {
        candidateUsernames.push(ownerRow.username);
      }
    } catch {
      /* ignore */
    }

    // Load repo labels.
    const availableLabels = await db
      .select({ name: labelsTable.name })
      .from(labelsTable)
      .where(eq(labelsTable.repositoryId, args.repositoryId))
      .then((rows) => rows.map((r) => r.name))
      .catch(() => [] as string[]);

    // Short diff summary (numstat only to keep prompt small).
    const statProc = Bun.spawn(
      ["git", "diff", "--numstat", `${args.baseBranch}...${args.headBranch}`],
      { cwd: repoDir, stdout: "pipe", stderr: "pipe" }
    );
    const diffSummary = await new Response(statProc.stdout).text();
    await statProc.exited;

    const result = await triagePullRequest(
      args.title,
      args.body,
      diffSummary,
      availableLabels,
      candidateUsernames
    );

    // Skip posting if we have absolutely nothing useful to say.
    if (
      !result.summary &&
      result.suggestedLabels.length === 0 &&
      result.suggestedReviewerUsernames.length === 0
    ) {
      return;
    }

    const priorityEmoji =
      result.priority === "critical"
        ? "**Critical**"
        : result.priority === "high"
          ? "**High**"
          : result.priority === "low"
            ? "**Low**"
            : "**Medium**";
    const parts: string[] = [`## AI Triage\n`];
    if (result.summary) parts.push(`${result.summary}\n`);
    parts.push(`- **Priority:** ${priorityEmoji}`);
    parts.push(`- **Risk area:** ${result.riskArea}`);
    if (result.suggestedLabels.length > 0) {
      parts.push(
        `- **Suggested labels:** ${result.suggestedLabels.map((l) => `\`${l}\``).join(", ")}`
      );
    }
    if (result.suggestedReviewerUsernames.length > 0) {
      parts.push(
        `- **Suggested reviewers:** ${result.suggestedReviewerUsernames.map((u) => `@${u}`).join(", ")}`
      );
    }
    parts.push(
      `\n_Suggestions only — nothing was auto-applied. The PR author remains in control._`
    );

    await db.insert(prComments).values({
      pullRequestId: args.prId,
      authorId: args.prAuthorId,
      body: parts.join("\n"),
      isAiReview: true,
    });
  } catch (err) {
    console.error("[pr-triage]", err);
  }
}

function formatRelative(date: Date | string): string {
  const d = typeof date === "string" ? new Date(date) : date;
  const now = new Date();
  const diffMs = now.getTime() - d.getTime();
  const diffMins = Math.floor(diffMs / 60000);
  if (diffMins < 1) return "just now";
  if (diffMins < 60) return `${diffMins}m ago`;
  const diffHours = Math.floor(diffMins / 60);
  if (diffHours < 24) return `${diffHours}h ago`;
  const diffDays = Math.floor(diffHours / 24);
  if (diffDays < 30) return `${diffDays}d ago`;
  return d.toLocaleDateString("en-US", {
    month: "short",
    day: "numeric",
    year: "numeric",
  });
}

// ---------------------------------------------------------------------------
// J11 — Reviewer request management (manual add + dismiss)
// ---------------------------------------------------------------------------
pulls.post(
  "/:owner/:repo/pulls/:number/reviewers",
  softAuth,
  requireAuth,
  async (c) => {
    const { owner: ownerName, repo: repoName } = c.req.param();
    const prNum = parseInt(c.req.param("number"), 10);
    const user = c.get("user")!;
    const form = await c.req.parseBody();
    const usernameRaw = String(form.username || "").trim().replace(/^@/, "");
    if (!usernameRaw) {
      return c.redirect(`/${ownerName}/${repoName}/pulls/${prNum}`);
    }
    const resolved = await resolveRepo(ownerName, repoName);
    if (!resolved) return c.notFound();
    const [pr] = await db
      .select()
      .from(pullRequests)
      .where(
        and(
          eq(pullRequests.repositoryId, resolved.repo.id),
          eq(pullRequests.number, prNum)
        )
      )
      .limit(1);
    if (!pr) return c.notFound();
    const canManage =
      user.id === resolved.owner.id || user.id === pr.authorId;
    if (!canManage) return c.redirect(`/${ownerName}/${repoName}/pulls/${prNum}`);
    const [target] = await db
      .select()
      .from(users)
      .where(eq(users.username, usernameRaw))
      .limit(1);
    if (!target) {
      return c.redirect(
        `/${ownerName}/${repoName}/pulls/${prNum}?error=${encodeURIComponent("User not found")}`
      );
    }
    if (target.id === pr.authorId) {
      return c.redirect(
        `/${ownerName}/${repoName}/pulls/${prNum}?error=${encodeURIComponent("PR author cannot be a reviewer")}`
      );
    }
    await requestReviewers(pr.id, [target.id], user.id, "manual");
    return c.redirect(`/${ownerName}/${repoName}/pulls/${prNum}`);
  }
);

pulls.post(
  "/:owner/:repo/pulls/:number/reviewers/:reviewerId/dismiss",
  softAuth,
  requireAuth,
  async (c) => {
    const { owner: ownerName, repo: repoName, reviewerId } = c.req.param();
    const prNum = parseInt(c.req.param("number"), 10);
    const user = c.get("user")!;
    const resolved = await resolveRepo(ownerName, repoName);
    if (!resolved) return c.notFound();
    const [pr] = await db
      .select()
      .from(pullRequests)
      .where(
        and(
          eq(pullRequests.repositoryId, resolved.repo.id),
          eq(pullRequests.number, prNum)
        )
      )
      .limit(1);
    if (!pr) return c.notFound();
    const canManage =
      user.id === resolved.owner.id ||
      user.id === pr.authorId ||
      user.id === reviewerId;
    if (!canManage) return c.redirect(`/${ownerName}/${repoName}/pulls/${prNum}`);
    await dismissRequest(pr.id, reviewerId);
    return c.redirect(`/${ownerName}/${repoName}/pulls/${prNum}`);
  }
);

interface ReviewersPanelProps {
  reviewers: Awaited<ReturnType<typeof listReviewRequestsForPr>>;
  canManage: boolean | null | undefined;
  ownerName: string;
  repoName: string;
  prNumber: number;
}

function ReviewersPanel(props: ReviewersPanelProps) {
  const { reviewers, canManage, ownerName, repoName, prNumber } = props;
  const stateColor = (s: string) =>
    s === "approved"
      ? "var(--green)"
      : s === "changes_requested"
        ? "var(--red)"
        : s === "dismissed"
          ? "var(--text-muted)"
          : "var(--text)";
  return (
    <div
      style="margin: 0 0 20px; padding: 12px 16px; background: var(--bg-secondary); border: 1px solid var(--border); border-radius: var(--radius)"
      data-testid="reviewers-panel"
    >
      <div style="display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px">
        <h3 style="margin: 0; font-size: 14px">Reviewers</h3>
        <span style="color: var(--text-muted); font-size: 12px">
          {reviewers.length === 0
            ? "No reviewers requested"
            : `${reviewers.filter((r) => r.state === "pending").length} pending · ${reviewers.filter((r) => r.state === "approved").length} approved`}
        </span>
      </div>
      {reviewers.length > 0 && (
        <ul style="list-style: none; padding: 0; margin: 0 0 8px">
          {reviewers.map((r) => (
            <li style="display: flex; align-items: center; gap: 8px; padding: 4px 0; font-size: 13px">
              <a href={`/${r.username}`}>
                <strong>@{r.username}</strong>
              </a>
              <span
                style={`font-size: 11px; padding: 1px 6px; border-radius: 10px; background: rgba(139, 148, 158, 0.15); color: ${stateColor(r.state)}; text-transform: capitalize`}
              >
                {r.state.replace("_", " ")}
              </span>
              <span style="font-size: 11px; color: var(--text-muted)">
                via {r.source}
              </span>
              {canManage && r.state !== "dismissed" && (
                <form
                  method="POST"
                  action={`/${ownerName}/${repoName}/pulls/${prNumber}/reviewers/${r.reviewerId}/dismiss`}
                  style="margin-left: auto"
                >
                  <button
                    type="submit"
                    class="btn"
                    style="padding: 2px 8px; font-size: 11px"
                    title="Dismiss this review request"
                  >
                    Dismiss
                  </button>
                </form>
              )}
            </li>
          ))}
        </ul>
      )}
      {canManage && (
        <form
          method="POST"
          action={`/${ownerName}/${repoName}/pulls/${prNumber}/reviewers`}
          style="display: flex; gap: 6px"
        >
          <input
            type="text"
            name="username"
            placeholder="Request review from @username"
            required
            style="flex: 1; padding: 4px 8px; font-size: 12px; background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius); color: var(--text)"
          />
          <button type="submit" class="btn" style="padding: 4px 10px; font-size: 12px">
            Request
          </button>
        </form>
      )}
    </div>
  );
}

// J16 — PR auto-merge toggle
pulls.post(
  "/:owner/:repo/pulls/:number/auto-merge",
  softAuth,
  requireAuth,
  async (c) => {
    const { owner: ownerName, repo: repoName } = c.req.param();
    const prNum = parseInt(c.req.param("number"), 10);
    const user = c.get("user")!;
    const body = await c.req.parseBody();
    const method = String(body.mergeMethod || "merge").trim();

    const resolved = await resolveRepo(ownerName, repoName);
    if (!resolved) return c.redirect(`/${ownerName}/${repoName}`);
    const [pr] = await db
      .select()
      .from(pullRequests)
      .where(
        and(
          eq(pullRequests.repositoryId, resolved.repo.id),
          eq(pullRequests.number, prNum)
        )
      )
      .limit(1);
    if (!pr) return c.notFound();
    const canManage =
      user.id === resolved.owner.id || user.id === pr.authorId;
    if (!canManage) {
      return c.redirect(`/${ownerName}/${repoName}/pulls/${prNum}`);
    }
    if (pr.state !== "open" || pr.isDraft) {
      return c.redirect(`/${ownerName}/${repoName}/pulls/${prNum}`);
    }
    try {
      const { enableAutoMerge, isValidMergeMethod } = await import(
        "../lib/pr-auto-merge"
      );
      await enableAutoMerge({
        pullRequestId: pr.id,
        enabledBy: user.id,
        mergeMethod: isValidMergeMethod(method) ? (method as any) : "merge",
      });
    } catch (err) {
      console.error("[pr-auto-merge] enable route failed:", err);
    }
    return c.redirect(`/${ownerName}/${repoName}/pulls/${prNum}`);
  }
);

pulls.post(
  "/:owner/:repo/pulls/:number/auto-merge/disable",
  softAuth,
  requireAuth,
  async (c) => {
    const { owner: ownerName, repo: repoName } = c.req.param();
    const prNum = parseInt(c.req.param("number"), 10);
    const user = c.get("user")!;
    const resolved = await resolveRepo(ownerName, repoName);
    if (!resolved) return c.redirect(`/${ownerName}/${repoName}`);
    const [pr] = await db
      .select()
      .from(pullRequests)
      .where(
        and(
          eq(pullRequests.repositoryId, resolved.repo.id),
          eq(pullRequests.number, prNum)
        )
      )
      .limit(1);
    if (!pr) return c.notFound();
    const canManage =
      user.id === resolved.owner.id || user.id === pr.authorId;
    if (!canManage) {
      return c.redirect(`/${ownerName}/${repoName}/pulls/${prNum}`);
    }
    try {
      const { disableAutoMerge } = await import("../lib/pr-auto-merge");
      await disableAutoMerge(pr.id);
    } catch (err) {
      console.error("[pr-auto-merge] disable route failed:", err);
    }
    return c.redirect(`/${ownerName}/${repoName}/pulls/${prNum}`);
  }
);

// J16 — UI panel
interface AutoMergePanelProps {
  owner: string;
  repo: string;
  prNumber: number;
  state: {
    enabled: boolean;
    mergeMethod: string;
    lastStatus: string | null;
    notifiedReady: boolean;
  };
}

function AutoMergePanel(props: AutoMergePanelProps) {
  const { owner, repo, prNumber, state } = props;
  let statusLabel: string;
  let statusColor: string;
  if (!state.enabled) {
    statusLabel = "Auto-merge is off";
    statusColor = "var(--text-muted)";
  } else if (state.lastStatus === "success" || state.notifiedReady) {
    statusLabel = "All checks passed \u2014 ready to merge";
    statusColor = "var(--green)";
  } else if (state.lastStatus === "failure" || state.lastStatus === "error") {
    statusLabel = "Checks failing \u2014 auto-merge paused";
    statusColor = "var(--red)";
  } else if (state.lastStatus === "pending") {
    statusLabel = "Waiting for checks to finish";
    statusColor = "var(--yellow)";
  } else {
    statusLabel = "Enabled \u2014 waiting for first check report";
    statusColor = "var(--yellow)";
  }

  return (
    <div style="margin-top: 20px; padding: 12px 16px; background: var(--bg-secondary); border: 1px solid var(--border); border-radius: var(--radius)">
      <div style="display: flex; align-items: center; gap: 12px">
        <div style={`font-size: 13px; font-weight: 600; color: ${statusColor}; flex: 1`}>
          {"\u26A1"} Auto-merge: {statusLabel}
        </div>
        {state.enabled ? (
          <form
            method="POST"
            action={`/${owner}/${repo}/pulls/${prNumber}/auto-merge/disable`}
            style="margin: 0"
          >
            <button type="submit" class="btn" style="padding: 4px 10px; font-size: 12px">
              Disable auto-merge
            </button>
          </form>
        ) : (
          <form
            method="POST"
            action={`/${owner}/${repo}/pulls/${prNumber}/auto-merge`}
            style="margin: 0; display: flex; gap: 6px; align-items: center"
          >
            <select
              name="mergeMethod"
              style="padding: 4px 8px; font-size: 12px; background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius); color: var(--text)"
            >
              <option value="merge">Merge</option>
              <option value="squash">Squash</option>
              <option value="rebase">Rebase</option>
            </select>
            <button type="submit" class="btn" style="padding: 4px 10px; font-size: 12px">
              Enable auto-merge
            </button>
          </form>
        )}
      </div>
      {state.enabled && (
        <div style="margin-top: 6px; font-size: 11px; color: var(--text-muted)">
          Chosen method: <code>{state.mergeMethod}</code>. The PR stays open
          until all commit statuses report success. You'll be notified on the
          PR when it becomes ready to merge.
        </div>
      )}
    </div>
  );
}

export default pulls;