CREATE TABLE IF NOT EXISTS user_navigation_recent (
  id TEXT PRIMARY KEY,
  user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  workspace_id TEXT NOT NULL DEFAULT '',
  kind TEXT NOT NULL,
  source_kind TEXT NOT NULL DEFAULT 'local_folder',
  root_uri TEXT NOT NULL,
  target_key TEXT NOT NULL,
  relative_path TEXT,
  document_id TEXT,
  title TEXT NOT NULL,
  status TEXT NOT NULL DEFAULT 'active',
  metadata_json TEXT NOT NULL DEFAULT '{}',
  visited_at TEXT NOT NULL,
  created_at TEXT NOT NULL,
  updated_at TEXT NOT NULL,
  revision INTEGER NOT NULL DEFAULT 1,
  UNIQUE(user_id, kind, target_key)
);

CREATE INDEX IF NOT EXISTS idx_user_navigation_recent_lookup
  ON user_navigation_recent(user_id, kind, status, visited_at DESC);
