-- OGraph v2.3 Schema
-- Immutable definitions (hash ID) + Mutable name pointers

-- Drop all v2.2 tables
DROP TABLE IF EXISTS reactions;
DROP TABLE IF EXISTS projections;
DROP TABLE IF EXISTS event_refs;
DROP TABLE IF EXISTS events;
DROP TABLE IF EXISTS objects;
DROP TABLE IF EXISTS projection_defs;
DROP TABLE IF EXISTS event_defs;
DROP TABLE IF EXISTS object_defs;

-- ============================================
-- Definition Versions (immutable, hash ID)
-- ============================================

CREATE TABLE object_def_versions (
  hash TEXT PRIMARY KEY,
  created_at INTEGER NOT NULL DEFAULT (unixepoch() * 1000)
);

CREATE TABLE event_def_versions (
  hash TEXT PRIMARY KEY,
  schema TEXT NOT NULL,
  created_at INTEGER NOT NULL DEFAULT (unixepoch() * 1000)
);

CREATE TABLE projection_def_versions (
  hash TEXT PRIMARY KEY,
  driven_by TEXT NOT NULL,       -- JSON: [event_def_hash, ...]
  params TEXT NOT NULL,
  filter TEXT NOT NULL,
  expression TEXT NOT NULL,
  initial_value TEXT,
  created_at INTEGER NOT NULL DEFAULT (unixepoch() * 1000)
);

-- ============================================
-- Name Pointers (mutable)
-- ============================================

CREATE TABLE object_def_names (
  name TEXT PRIMARY KEY,
  current_hash TEXT NOT NULL REFERENCES object_def_versions(hash),
  updated_at INTEGER NOT NULL DEFAULT (unixepoch() * 1000)
);

CREATE TABLE event_def_names (
  name TEXT PRIMARY KEY,
  current_hash TEXT NOT NULL REFERENCES event_def_versions(hash),
  updated_at INTEGER NOT NULL DEFAULT (unixepoch() * 1000)
);

CREATE TABLE projection_def_names (
  name TEXT PRIMARY KEY,
  current_hash TEXT NOT NULL REFERENCES projection_def_versions(hash),
  updated_at INTEGER NOT NULL DEFAULT (unixepoch() * 1000)
);

-- ============================================
-- Instances
-- ============================================

CREATE TABLE objects (
  id TEXT PRIMARY KEY,
  type_hash TEXT NOT NULL REFERENCES object_def_versions(hash),
  created_at INTEGER NOT NULL DEFAULT (unixepoch() * 1000)
);

CREATE TABLE events (
  id TEXT PRIMARY KEY,
  type_hash TEXT NOT NULL REFERENCES event_def_versions(hash),
  payload TEXT NOT NULL,
  created_at INTEGER NOT NULL DEFAULT (unixepoch() * 1000)
);

CREATE TABLE event_refs (
  event_id TEXT NOT NULL REFERENCES events(id),
  property TEXT NOT NULL,
  ref_id TEXT NOT NULL REFERENCES objects(id),
  PRIMARY KEY (event_id, property)
);
CREATE INDEX idx_event_refs_obj ON event_refs(ref_id);

CREATE TABLE projections (
  def_hash TEXT NOT NULL REFERENCES projection_def_versions(hash),
  params_hash TEXT NOT NULL,
  params TEXT NOT NULL,
  value TEXT,
  created_at INTEGER NOT NULL DEFAULT (unixepoch() * 1000),
  updated_at INTEGER,
  PRIMARY KEY (def_hash, params_hash)
);

CREATE TABLE reactions (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  projection_def_hash TEXT NOT NULL REFERENCES projection_def_versions(hash),
  params_hash TEXT NOT NULL,
  params TEXT NOT NULL,
  webhook_url TEXT NOT NULL,
  created_at INTEGER NOT NULL DEFAULT (unixepoch() * 1000)
);
CREATE INDEX idx_reactions_projection ON reactions(projection_def_hash, params_hash);
