-- 006: PR Reviews, Branch Protection, Fork enhancements

CREATE TABLE IF NOT EXISTS pr_review (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    pull_request_id UUID NOT NULL REFERENCES pull_request(id) ON DELETE CASCADE,
    author_id UUID NOT NULL REFERENCES "user"(id) ON DELETE CASCADE,
    state TEXT NOT NULL,
    body TEXT NULL,
    commit_sha TEXT NULL,
    submitted_at TIMESTAMPTZ NULL,
    dismissed_at TIMESTAMPTZ NULL,
    dismissed_by UUID NULL REFERENCES "user"(id) ON DELETE CASCADE,
    dismiss_reason TEXT NULL,
    created_at TIMESTAMPTZ NOT NULL,
    updated_at TIMESTAMPTZ NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_pr_review_pull_request_id ON pr_review (pull_request_id);
CREATE INDEX IF NOT EXISTS idx_pr_review_author_id ON pr_review (author_id);

CREATE TABLE IF NOT EXISTS pr_review_comment (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    review_id UUID NOT NULL REFERENCES pr_review(id) ON DELETE CASCADE,
    pull_request_id UUID NOT NULL REFERENCES pull_request(id) ON DELETE CASCADE,
    author_id UUID NOT NULL REFERENCES "user"(id) ON DELETE CASCADE,
    body TEXT NOT NULL,
    path TEXT NOT NULL,
    line INTEGER NULL,
    original_line INTEGER NULL,
    start_line INTEGER NULL,
    original_start_line INTEGER NULL,
    diff_hunk TEXT NULL,
    in_reply_to_id UUID NULL REFERENCES pr_review_comment(id) ON DELETE CASCADE,
    edited_at TIMESTAMPTZ NULL,
    created_at TIMESTAMPTZ NOT NULL,
    updated_at TIMESTAMPTZ NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_pr_review_comment_review_id ON pr_review_comment (review_id);
CREATE INDEX IF NOT EXISTS idx_pr_review_comment_pull_request_id ON pr_review_comment (pull_request_id);
CREATE INDEX IF NOT EXISTS idx_pr_review_comment_author_id ON pr_review_comment (author_id);
CREATE INDEX IF NOT EXISTS idx_pr_review_comment_path ON pr_review_comment (path);

CREATE TABLE IF NOT EXISTS branch_protection_rule (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    repo_id UUID NOT NULL REFERENCES repo(id) ON DELETE CASCADE,
    pattern TEXT NOT NULL,
    require_approvals INTEGER NOT NULL DEFAULT 0,
    require_status_checks BOOLEAN NOT NULL DEFAULT false,
    required_status_checks TEXT[] NOT NULL DEFAULT '{}',
    require_linear_history BOOLEAN NOT NULL DEFAULT false,
    allow_force_pushes BOOLEAN NOT NULL DEFAULT false,
    allow_deletions BOOLEAN NOT NULL DEFAULT false,
    require_signed_commits BOOLEAN NOT NULL DEFAULT false,
    require_code_owner_review BOOLEAN NOT NULL DEFAULT false,
    dismiss_stale_reviews BOOLEAN NOT NULL DEFAULT false,
    restrict_pushes BOOLEAN NOT NULL DEFAULT false,
    push_allowances UUID[] NOT NULL DEFAULT '{}',
    restrict_review_dismissal BOOLEAN NOT NULL DEFAULT false,
    dismissal_allowances UUID[] NOT NULL DEFAULT '{}',
    require_conversation_resolution BOOLEAN NOT NULL DEFAULT false,
    created_by UUID NULL REFERENCES "user"(id) ON DELETE CASCADE,
    created_at TIMESTAMPTZ NOT NULL,
    updated_at TIMESTAMPTZ NOT NULL,
    CONSTRAINT uq_branch_protection_rule UNIQUE (repo_id, pattern)
);
CREATE INDEX IF NOT EXISTS idx_branch_protection_rule_repo_id ON branch_protection_rule (repo_id);

CREATE TABLE IF NOT EXISTS repo_fork (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    parent_repo_id UUID NOT NULL REFERENCES repo(id) ON DELETE CASCADE,
    fork_repo_id UUID NOT NULL REFERENCES repo(id) ON DELETE CASCADE,
    forked_by UUID NOT NULL REFERENCES "user"(id) ON DELETE CASCADE,
    created_at TIMESTAMPTZ NOT NULL,
    CONSTRAINT uq_repo_fork UNIQUE (parent_repo_id, fork_repo_id)
);
CREATE INDEX IF NOT EXISTS idx_repo_fork_parent ON repo_fork (parent_repo_id);
CREATE INDEX IF NOT EXISTS idx_repo_fork_fork ON repo_fork (fork_repo_id);
