-- 004-user-ui-preferences.sql
-- User-scoped UI preferences for cross-browser page setting sync.

CREATE TABLE IF NOT EXISTS user_ui_preferences (
  id TEXT PRIMARY KEY,
  user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  workspace_id TEXT NOT NULL DEFAULT '',
  source_kind TEXT NOT NULL DEFAULT '',
  scope_kind TEXT NOT NULL,
  scope_id TEXT NOT NULL,
  key TEXT NOT NULL,
  value_json TEXT NOT NULL,
  status TEXT NOT NULL DEFAULT 'active',
  created_at TEXT NOT NULL,
  updated_at TEXT NOT NULL,
  revision INTEGER NOT NULL DEFAULT 1,
  UNIQUE(user_id, workspace_id, source_kind, scope_kind, scope_id, key)
);

CREATE INDEX IF NOT EXISTS idx_user_ui_preferences_lookup
  ON user_ui_preferences(user_id, workspace_id, source_kind, scope_kind, scope_id);
