CREATE TABLE IF NOT EXISTS theme_profiles (
  id TEXT PRIMARY KEY,
  name TEXT NOT NULL,
  base_theme TEXT NOT NULL DEFAULT 'light',
  values_json TEXT NOT NULL DEFAULT '{}',
  created_by INTEGER,
  created_at TEXT NOT NULL DEFAULT (datetime('now')),
  updated_at TEXT NOT NULL DEFAULT (datetime('now')),
  FOREIGN KEY(created_by) REFERENCES users(id) ON DELETE SET NULL
);

CREATE TABLE IF NOT EXISTS user_theme_preferences (
  user_id INTEGER PRIMARY KEY,
  theme_id TEXT NOT NULL DEFAULT 'inherit',
  density TEXT NOT NULL DEFAULT 'comfortable',
  reduced_motion TEXT NOT NULL DEFAULT 'system',
  updated_at TEXT NOT NULL DEFAULT (datetime('now')),
  FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE
);

CREATE INDEX IF NOT EXISTS idx_theme_profiles_updated ON theme_profiles(updated_at DESC);
