-- ENE Schema — Research Stack Entropy Database
-- Auto-created by auto_pipeline.py --db-only

CREATE SCHEMA IF NOT EXISTS ene;

CREATE TABLE IF NOT EXISTS ene.packages (
  pkg TEXT PRIMARY KEY, package_type TEXT, title TEXT, content TEXT,
  tags TEXT[], source TEXT, domain TEXT, provenance TEXT
);

CREATE TABLE IF NOT EXISTS ene.receipts (
  id UUID PRIMARY KEY, package_id TEXT REFERENCES ene.packages(pkg),
  receipt_type TEXT, status TEXT, toolchain TEXT, theorem_id TEXT
);

CREATE TABLE IF NOT EXISTS ene.sidon_labels (
  id UUID PRIMARY KEY, label_set_name TEXT, strand_index INT,
  label_value INT, sidon_slack INT
);

CREATE TABLE IF NOT EXISTS ene.braid_strands (
  id UUID PRIMARY KEY, snapshot_id TEXT, strand_index INT,
  phase_x INT, phase_y INT
);

CREATE TABLE IF NOT EXISTS ene.eigensolid_snapshots (
  id UUID PRIMARY KEY, root_hash TEXT, crossing_matrix INT[],
  sidon_slack INT, step_count INT, residual_series INT[],
  write_timing INT, scar_absence BOOLEAN
);

CREATE TABLE IF NOT EXISTS ene.gossip_surface_nodes (
  id UUID PRIMARY KEY, node_id TEXT, label TEXT,
  surface_type TEXT, coordinate INT[]
);

CREATE TABLE IF NOT EXISTS ene.gossip_surface_edges (
  id UUID PRIMARY KEY, source_id TEXT REFERENCES ene.gossip_surface_nodes(node_id),
  target_id TEXT REFERENCES ene.gossip_surface_nodes(node_id),
  edge_type TEXT, weight INT
);

CREATE TABLE IF NOT EXISTS ene.relations (
  id UUID PRIMARY KEY, source_id TEXT, target_id TEXT,
  relation_type TEXT, weight FLOAT, provenance TEXT
);

CREATE TABLE IF NOT EXISTS ene.sessions (
  id UUID PRIMARY KEY, title TEXT, actor TEXT, toolchain TEXT, summary TEXT
);

CREATE TABLE IF NOT EXISTS ene.ingest_events (
  id UUID PRIMARY KEY, session_id UUID, event_type TEXT,
  payload TEXT, created_at TIMESTAMPTZ DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS ene.prover_state (
  id UUID PRIMARY KEY, theorem_id TEXT, status TEXT,
  proof_count INT, last_proved_at TIMESTAMPTZ
);

CREATE TABLE IF NOT EXISTS ene.prover_instances (
  id UUID PRIMARY KEY, instance_type TEXT, config TEXT,
  active BOOLEAN, last_seen TIMESTAMPTZ
);

CREATE TABLE IF NOT EXISTS ene.rrc_classifications (
  id UUID PRIMARY KEY, equation_id TEXT, shape TEXT,
  pist_label TEXT, spectral_radius FLOAT, weak_axes INT,
  score FLOAT, classified_at TIMESTAMPTZ DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS ene.rrc_predictions (
  id UUID PRIMARY KEY, equation_id TEXT, proxy_pred TEXT,
  exact_pred TEXT, matrix_hash TEXT, confidence FLOAT,
  predicted_at TIMESTAMPTZ DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS ene.rrc_learn_history (
  id UUID PRIMARY KEY, equation_id TEXT, previous_shape TEXT,
  new_shape TEXT, change_reason TEXT,
  learned_at TIMESTAMPTZ DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS ene.wiki_pages (
  slug TEXT PRIMARY KEY, title TEXT, latest_revision INT
);

CREATE TABLE IF NOT EXISTS ene.wiki_revisions (
  slug TEXT REFERENCES ene.wiki_pages(slug), revision INT,
  title TEXT, text TEXT, author TEXT, content_hash TEXT,
  PRIMARY KEY (slug, revision)
);

CREATE TABLE IF NOT EXISTS ene.wiki_categories (slug TEXT, category TEXT, PRIMARY KEY (slug, category));
CREATE TABLE IF NOT EXISTS ene.wiki_links (slug TEXT, target_slug TEXT, target_title TEXT, PRIMARY KEY (slug, target_slug));

-- RRC composite shape tracking
CREATE TABLE IF NOT EXISTS ene.shape_predictions (
  id UUID PRIMARY KEY, equation_id TEXT, shape TEXT,
  model_version TEXT, confidence FLOAT,
  evidence TEXT[], predicted_at TIMESTAMPTZ DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS ene.shape_ground_truth (
  id UUID PRIMARY KEY, equation_id TEXT, shape TEXT,
  verified_by TEXT, verified_at TIMESTAMPTZ DEFAULT NOW()
);

-- Index for fast lookups
CREATE INDEX IF NOT EXISTS idx_package_type ON ene.packages(package_type);
CREATE INDEX IF NOT EXISTS idx_receipt_theorem ON ene.receipts(theorem_id);
CREATE INDEX IF NOT EXISTS idx_rrc_equation ON ene.rrc_classifications(equation_id);
CREATE INDEX IF NOT EXISTS idx_shape_equation ON ene.shape_predictions(equation_id);
