export type UserRole = "user" | "admin";

export interface ProfileSettings {
  skipBootByDefault?: boolean;
  muteConsoleUi?: boolean;
  /** Preferred home / browse shell */
  preferredUi?: string;
}

export interface Profile {
  id: string;
  displayName: string;
  avatarId: string;
  createdAt: number;
  settings: ProfileSettings;
}

export interface PlayHistoryEntry {
  profileId: string;
  gameSlug: string;
  lastPlayedAt: number;
  sessions: number;
  totalSeconds: number;
}

export interface FavoriteEntry {
  profileId: string;
  gameSlug: string;
  favoritedAt: number;
}

export interface AchievementUnlock {
  profileId: string;
  achievementId: string;
  unlockedAt: number;
}

export interface AccountSettings {
  skipBootByDefault?: boolean;
  muteConsoleUi?: boolean;
  showContinueRow?: boolean;
  reduceMotion?: boolean;
}

export type AuthProvider = "local" | "discord" | "oauth";

export interface Account {
  id: string;
  username: string;
  /** SHA-256 hex of password + salt — omitted for OAuth accounts */
  passwordHash?: string;
  salt?: string;
  displayName: string;
  avatarId: string;
  bio?: string;
  role: UserRole;
  competitiveOptIn: boolean;
  competitiveOptedAt?: number;
  linkedProfileId?: string;
  createdAt: number;
  competitiveBanned?: boolean;
  /** Home UI skin id from uiSkins */
  preferredUi?: string;
  /** True until player finishes /setup/ui */
  needsUiSetup?: boolean;
  settings: AccountSettings;
  updatedAt?: number;
  /** How this account authenticates (default local) */
  authProvider?: AuthProvider;
}

export interface LeaderboardEntry {
  id: string;
  accountId: string;
  displayName: string;
  gameSlug: string;
  kind: "score" | "play_time" | "achievements";
  value: number;
  updatedAt: number;
}

export interface AnalyticsEvent {
  id: string;
  type:
    | "session_start"
    | "session_end"
    | "score"
    | "achievement"
    | "signup"
    | "login"
    | "logout"
    | "page_view"
    | "favorite_add"
    | "favorite_remove"
    | "ui_skin_set"
    | "profile_create"
    | "contribute"
    | "boot_complete"
    | "onboarding_skip";
  accountId?: string;
  profileId?: string;
  gameSlug?: string;
  /** Free-form label (path, skin id, etc.) */
  label?: string;
  value?: number;
  at: number;
}

export interface AuditLogEntry {
  id: string;
  adminId: string;
  action: string;
  target?: string;
  at: number;
  detail?: string;
}

export type AchievementRule =
  | { type: "first_play" }
  | { type: "play_minutes"; minutes: number; gameSlug?: string }
  | { type: "sessions"; count: number; gameSlug?: string }
  | { type: "favorite_count"; count: number }
  | { type: "systems_played"; count: number }
  | { type: "games_played"; count: number }
  | { type: "single_game_minutes"; minutes: number }
  | { type: "single_game_sessions"; count: number };

export interface AchievementDef {
  id: string;
  title: string;
  description: string;
  gameSlug?: string;
  system?: string;
  points: number;
  rule: AchievementRule;
}
