-- users (minimal for v0)
CREATE TABLE IF NOT EXISTS users (
  id           INTEGER PRIMARY KEY AUTOINCREMENT,
  email        VARCHAR(255) UNIQUE,
  pass_bcrypt  BLOB,
  created_at   TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- conversations
CREATE TABLE IF NOT EXISTS conversations (
  id           INTEGER PRIMARY KEY AUTOINCREMENT,
  owner_id     INTEGER NOT NULL,
  title        VARCHAR(255),
  created_at   TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- nodes
CREATE TABLE IF NOT EXISTS nodes (
  id              INTEGER PRIMARY KEY AUTOINCREMENT,
  conversation_id INTEGER NOT NULL,
  author_kind     ENUM('user','assistant') NOT NULL,
  content         TEXT NOT NULL,
  created_at      TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- edges (DAG)
CREATE TABLE IF NOT EXISTS edges (
  parent_id    INTEGER NOT NULL,
  child_id     INTEGER NOT NULL,
  PRIMARY KEY(parent_id, child_id)
);

-- branches (named pointers)
CREATE TABLE IF NOT EXISTS branches (
  id              INTEGER PRIMARY KEY AUTOINCREMENT,
  conversation_id INTEGER NOT NULL,
  name            VARCHAR(128) NOT NULL,
  head_node_id    INTEGER NOT NULL,
  UNIQUE (conversation_id, name)
);
