-- Replace filter (JSONata) with bindings (structured ref matching)
-- Clean slate for projection_def_versions, projection_def_names, projections, reactions

DROP TABLE IF EXISTS reactions;
DROP TABLE IF EXISTS projections;
DROP TABLE IF EXISTS projection_def_names;
DROP TABLE IF EXISTS projection_def_versions;

CREATE TABLE projection_def_versions (
  hash TEXT PRIMARY KEY,
  name TEXT NOT NULL,
  parent_hash TEXT REFERENCES projection_def_versions(hash),
  driven_by TEXT NOT NULL,
  params TEXT NOT NULL,
  bindings TEXT NOT NULL,             -- JSON: { property: "$param" | "literal_id" }
  expression TEXT NOT NULL,
  value_schema TEXT NOT NULL,
  initial_value TEXT NOT NULL,
  created_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)
);

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);
