-- Bookmarks, pins, remote status cache, OAuth token persistence.

CREATE TABLE IF NOT EXISTS bookmarks (
  user_id TEXT NOT NULL,
  status_id TEXT NOT NULL,
  created_at TEXT NOT NULL,
  PRIMARY KEY(user_id, status_id)
);

CREATE INDEX IF NOT EXISTS idx_bookmarks_user_time ON bookmarks(user_id, created_at DESC);

CREATE TABLE IF NOT EXISTS pinned_statuses (
  user_id TEXT NOT NULL,
  status_id TEXT NOT NULL,
  created_at TEXT NOT NULL,
  PRIMARY KEY(user_id, status_id)
);

CREATE INDEX IF NOT EXISTS idx_pinned_user ON pinned_statuses(user_id, created_at DESC);

CREATE TABLE IF NOT EXISTS cached_statuses (
  id TEXT PRIMARY KEY,
  object_id TEXT NOT NULL UNIQUE,
  actor TEXT NOT NULL,
  content TEXT NOT NULL,
  summary TEXT NOT NULL DEFAULT '',
  sensitive INTEGER NOT NULL DEFAULT 0,
  language TEXT NOT NULL DEFAULT 'en',
  in_reply_to TEXT,
  url TEXT NOT NULL,
  published TEXT NOT NULL,
  cached_at TEXT NOT NULL
);

CREATE INDEX IF NOT EXISTS idx_cached_statuses_actor_time ON cached_statuses(actor, published DESC);
CREATE INDEX IF NOT EXISTS idx_cached_statuses_time ON cached_statuses(published DESC);

CREATE TABLE IF NOT EXISTS cached_status_attachments (
  cached_status_id TEXT NOT NULL,
  position INTEGER NOT NULL,
  url TEXT NOT NULL,
  preview_url TEXT,
  mime_type TEXT NOT NULL,
  description TEXT,
  PRIMARY KEY(cached_status_id, position)
);

CREATE TABLE IF NOT EXISTS oauth_tokens (
  token TEXT PRIMARY KEY,
  user_id TEXT NOT NULL,
  app_id TEXT NOT NULL,
  scopes TEXT NOT NULL,
  created_at TEXT NOT NULL,
  last_used_at TEXT
);

CREATE INDEX IF NOT EXISTS idx_oauth_tokens_user ON oauth_tokens(user_id, created_at DESC);
