CREATE TABLE IF NOT EXISTS custom_world_profiles (
  user_id TEXT NOT NULL,
  profile_id TEXT NOT NULL,
  payload_json JSONB NOT NULL,
  visibility TEXT NOT NULL DEFAULT 'draft',
  published_at TEXT,
  author_display_name TEXT NOT NULL DEFAULT '玩家',
  world_name TEXT NOT NULL DEFAULT '',
  subtitle TEXT NOT NULL DEFAULT '',
  summary_text TEXT NOT NULL DEFAULT '',
  cover_image_src TEXT,
  theme_mode TEXT NOT NULL DEFAULT 'mythic',
  playable_npc_count INTEGER NOT NULL DEFAULT 0,
  landmark_count INTEGER NOT NULL DEFAULT 0,
  updated_at TEXT NOT NULL,
  PRIMARY KEY (user_id, profile_id),
  FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);

CREATE INDEX IF NOT EXISTS custom_world_profiles_user_updated_idx
  ON custom_world_profiles (user_id, updated_at DESC);

CREATE INDEX IF NOT EXISTS custom_world_profiles_published_idx
  ON custom_world_profiles (visibility, published_at DESC, updated_at DESC);
