-- Slice 3 / §10: the PR flow per §10 in full.
--
-- The cache shape from 002_cache.sql already carries cached_prs for
-- rfc_branch kind. Slice 3 needs two additions on top: a column to
-- record the resolution PR that supersedes an original under §10.9,
-- and a column to record the merge commit on no-fast-forward merges
-- per §10.5 so the PR page can render against the right ancestry.
--
-- The pr_seen cursor in 005 already covers §10.3 — last_seen_commit_sha
-- accents new hunks, last_seen_message_id accents new conversation
-- messages (review-kind threads on the branch are thread_messages too).

ALTER TABLE cached_prs ADD COLUMN superseded_by_pr_number INTEGER;
ALTER TABLE cached_prs ADD COLUMN merge_commit_sha TEXT;

CREATE INDEX idx_cached_prs_superseded ON cached_prs (superseded_by_pr_number);

-- §10.9: when a resolution branch is cut from main's tip, we record the
-- original branch it replays so the audit log and the PR header can
-- carry the relationship. The original PR auto-closes on the resolution
-- PR's merge via the cache reconciler reading the trailer on the merge
-- commit.
CREATE TABLE pr_resolution_branches (
    id                  INTEGER PRIMARY KEY AUTOINCREMENT,
    rfc_slug            TEXT NOT NULL,
    original_pr_number  INTEGER NOT NULL,
    original_branch     TEXT NOT NULL,
    resolution_branch   TEXT NOT NULL,
    created_at          TEXT NOT NULL DEFAULT (datetime('now')),
    UNIQUE (rfc_slug, resolution_branch)
);

CREATE INDEX idx_pr_resolution_original ON pr_resolution_branches (rfc_slug, original_pr_number);
