-- ============================================================================
-- StellaOps Proof-Driven Moats Database Schema
-- ============================================================================
-- Purpose: Four-tier backport detection with cryptographic proof generation
-- Version: 1.0.0
-- Compatible with: PostgreSQL 16+
-- ============================================================================

-- Schema: proof_moats
-- Contains all proof-driven backport detection tables
CREATE SCHEMA IF NOT EXISTS proof_moats;

SET search_path TO proof_moats, public;

-- ============================================================================
-- TIER 1: Distro Advisories (Highest Confidence)
-- ============================================================================

-- Table: distro_release
-- Tracks distribution releases for versioning context
CREATE TABLE IF NOT EXISTS distro_release (
    release_id TEXT PRIMARY KEY,
    distro_name TEXT NOT NULL,
    release_version TEXT NOT NULL,
    release_codename TEXT,
    eol_date TIMESTAMPTZ,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX idx_distro_release_name ON distro_release(distro_name);

-- Table: distro_advisory
-- Official distro security advisories (DSA, USN, RHSA, etc.)
CREATE TABLE IF NOT EXISTS distro_advisory (
    advisory_id TEXT PRIMARY KEY,
    distro_name TEXT NOT NULL,
    advisory_type TEXT NOT NULL,
    title TEXT NOT NULL,
    description TEXT,
    severity TEXT,
    published_at TIMESTAMPTZ NOT NULL,
    source_url TEXT,
    raw_data JSONB NOT NULL,
    ingested_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX idx_distro_advisory_distro ON distro_advisory(distro_name);
CREATE INDEX idx_distro_advisory_published ON distro_advisory(published_at DESC);
CREATE INDEX idx_distro_advisory_raw_data ON distro_advisory USING gin(raw_data);

