CREATE TABLE reducers (
  name TEXT PRIMARY KEY,
  driven_by TEXT NOT NULL,
  params TEXT NOT NULL,
  filter TEXT NOT NULL,
  expression TEXT NOT NULL,
  mode TEXT NOT NULL DEFAULT 'fold' CHECK(mode IN ('fold', 'latest', 'window')),
  window_size INTEGER,
  initial_value TEXT
);

CREATE TABLE projections (
  reducer TEXT NOT NULL REFERENCES reducers(name),
  params TEXT NOT NULL,
  params_hash TEXT NOT NULL,
  value TEXT,
  updated_by TEXT,
  updated_at INTEGER,
  live INTEGER NOT NULL DEFAULT 0,
  PRIMARY KEY (reducer, params_hash)
);

CREATE TABLE event_log (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  evt_oid TEXT NOT NULL,
  processed_reducers TEXT,
  processed_reactions TEXT,
  created_at INTEGER NOT NULL DEFAULT (unixepoch() * 1000)
);
CREATE INDEX idx_event_log_created ON event_log(created_at);
