-- Drop old projection-related tables (clean slate, pre-launch)
DROP TABLE IF EXISTS reactions;
DROP TABLE IF EXISTS projections;
DROP TABLE IF EXISTS projection_def_names;
DROP TABLE IF EXISTS projection_def_versions;

-- Recreate projection_def_versions WITHOUT driven_by, bindings, expression
CREATE TABLE projection_def_versions (
  hash TEXT PRIMARY KEY,
  name TEXT NOT NULL,
  parent_hash TEXT REFERENCES projection_def_versions(hash),
  params TEXT NOT NULL,
  value_schema TEXT NOT NULL,
  initial_value TEXT NOT NULL,
  created_at INTEGER NOT NULL DEFAULT (unixepoch() * 1000)
);

-- New: per-source bindings + expression
CREATE TABLE projection_def_sources (
  projection_hash TEXT NOT NULL REFERENCES projection_def_versions(hash),
  event_def_hash TEXT NOT NULL,
  bindings TEXT NOT NULL,
  expression TEXT NOT NULL,
  PRIMARY KEY (projection_hash, event_def_hash)
);
CREATE INDEX idx_pds_event ON projection_def_sources(event_def_hash);

-- Recreate name pointers
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)
);

-- Recreate instance tables
CREATE TABLE projections (
  def_hash TEXT NOT NULL REFERENCES projection_def_versions(hash),
  params_hash TEXT NOT NULL,
  params TEXT NOT NULL,
  value TEXT NOT NULL,
  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);
