-- Add action type support to reactions (Phase 2: emit_event)
-- Pre-launch: drop and recreate reactions table

DROP TABLE IF EXISTS reactions;

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,
  action TEXT NOT NULL DEFAULT 'webhook',  -- 'webhook' | 'emit_event'
  webhook_url TEXT,                         -- required when action = 'webhook'
  emit_event_type TEXT,                     -- required when action = 'emit_event'
  emit_payload_template TEXT,               -- JSONata: optional template for emit_event
  created_at INTEGER NOT NULL DEFAULT (unixepoch() * 1000)
);
CREATE INDEX idx_reactions_projection ON reactions(projection_def_hash, params_hash);
