-- Release Assets - binary attachments for releases
CREATE TABLE IF NOT EXISTS repo_release_asset (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    release_id UUID NOT NULL REFERENCES repo_release(id) ON DELETE CASCADE,
    filename TEXT NOT NULL,
    size_bytes BIGINT NOT NULL,
    mime_type TEXT NOT NULL DEFAULT 'application/octet-stream',
    storage_path TEXT NOT NULL,
    url TEXT,
    download_count BIGINT NOT NULL DEFAULT 0,
    uploaded_by UUID REFERENCES "user"(id) ON DELETE SET NULL,
    created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
    deleted_at TIMESTAMPTZ
);
CREATE INDEX IF NOT EXISTS idx_repo_release_asset_release ON repo_release_asset(release_id, deleted_at);
CREATE INDEX IF NOT EXISTS idx_repo_release_asset_deleted ON repo_release_asset(deleted_at) WHERE deleted_at IS NOT NULL;

-- PR Templates (mirrors issue_template structure)
CREATE TABLE IF NOT EXISTS pr_template (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    repo_id UUID NOT NULL REFERENCES repo(id) ON DELETE CASCADE,
    name TEXT NOT NULL,
    description TEXT,
    title_template TEXT,
    body_template TEXT NOT NULL DEFAULT '',
    labels TEXT[] NOT NULL DEFAULT '{}',
    active BOOLEAN NOT NULL DEFAULT true,
    created_by UUID REFERENCES "user"(id) ON DELETE SET NULL,
    created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_pr_template_repo ON pr_template(repo_id);

-- PR Review Requests
CREATE TABLE IF NOT EXISTS pr_review_request (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    pull_request_id UUID NOT NULL REFERENCES pull_request(id) ON DELETE CASCADE,
    reviewer_id UUID NOT NULL REFERENCES "user"(id) ON DELETE CASCADE,
    requested_by UUID NOT NULL REFERENCES "user"(id) ON DELETE CASCADE,
    created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_pr_review_request_pr ON pr_review_request(pull_request_id);
CREATE UNIQUE INDEX IF NOT EXISTS idx_pr_review_request_pr_reviewer ON pr_review_request(pull_request_id, reviewer_id);
