-- §5 / §6.4 / §11.1: per-branch visibility and contribute settings.
-- These rows are app data, not cache. They describe what the app permits
-- for a given branch; the bot enforces them before acting. Absence of a
-- row means defaults: read_public=1, contribute_mode='just-me'.

CREATE TABLE branch_visibility (
    id              INTEGER PRIMARY KEY AUTOINCREMENT,
    rfc_slug        TEXT NOT NULL,
    branch_name     TEXT NOT NULL,
    read_public     INTEGER NOT NULL DEFAULT 1,
    contribute_mode TEXT NOT NULL DEFAULT 'just-me' CHECK (contribute_mode IN ('just-me', 'specific', 'any-contributor')),
    UNIQUE (rfc_slug, branch_name)
);

CREATE TABLE branch_contribute_grants (
    id              INTEGER PRIMARY KEY AUTOINCREMENT,
    rfc_slug        TEXT NOT NULL,
    branch_name     TEXT NOT NULL,
    grantee_user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    granted_by      INTEGER NOT NULL REFERENCES users(id) ON DELETE SET NULL,
    granted_at      TEXT NOT NULL DEFAULT (datetime('now')),
    UNIQUE (rfc_slug, branch_name, grantee_user_id)
);

CREATE INDEX idx_grants_lookup ON branch_contribute_grants (rfc_slug, branch_name);
CREATE INDEX idx_grants_grantee ON branch_contribute_grants (grantee_user_id);

-- §5 / §7.2: starred RFCs pin to the top of the current sort order.
CREATE TABLE stars (
    id          INTEGER PRIMARY KEY AUTOINCREMENT,
    user_id     INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    rfc_slug    TEXT NOT NULL,
    starred_at  TEXT NOT NULL DEFAULT (datetime('now')),
    UNIQUE (user_id, rfc_slug)
);

CREATE INDEX idx_stars_user ON stars (user_id);
CREATE INDEX idx_stars_rfc ON stars (rfc_slug);
