CREATE TABLE IF NOT EXISTS thread_reads (
  thread_id TEXT NOT NULL,
  agent_id TEXT NOT NULL,
  last_read_message_id TEXT NOT NULL,
  last_read_at TEXT NOT NULL,
  PRIMARY KEY(thread_id, agent_id),
  FOREIGN KEY(thread_id) REFERENCES threads(thread_id),
  FOREIGN KEY(last_read_message_id) REFERENCES messages(message_id)
);

CREATE INDEX IF NOT EXISTS idx_thread_reads_agent
  ON thread_reads(agent_id, last_read_at);
