export type LauncherPhase =
  | "booting"
  | "unconfigured"
  | "signed-out"
  | "device-code"
  | "verifying"
  | "downloading"
  | "installing"
  | "ready"
  | "launching"
  | "running"
  | "error";

export type LauncherModuleId =
  | "keystrokes"
  | "cps"
  | "ping"
  | "serverAddress"
  | "resources"
  | "coordinates"
  | "minimap";

export type LauncherModules = Record<LauncherModuleId, boolean>;

export interface LauncherSettings {
  minimumMemoryMb: number;
  maximumMemoryMb: number;
  width: number;
  height: number;
  autoConnect: boolean;
  closeLauncherOnGameStart: boolean;
  modules: LauncherModules;
}

export interface LauncherAccount {
  displayName: string;
}

export interface DeviceCodeChallenge {
  userCode: string;
  verificationUri: string;
  expiresAt: number;
}

export interface LauncherProgress {
  label: string;
  detail: string;
  completedBytes: number;
  totalBytes: number;
  percent: number;
}

export interface LauncherFailure {
  title: string;
  message: string;
  recoverable: boolean;
}

export interface ServerStatus {
  host: string;
  port: number;
  online: boolean | null;
  playersOnline: number | null;
  playersMaximum: number | null;
}

export interface ClientInstallation {
  state: "missing" | "installed" | "verified";
  version: string | null;
  installedAt: string | null;
  components: number;
}

export interface MicrosoftConfiguration {
  configured: boolean;
  clientId: string;
  source: "missing" | "embedded" | "local";
}

export interface CommunityPlayerRanking {
  name: string;
  platform: "JAVA" | "BEDROCK";
  value: number;
  secondary: number;
}

export interface CommunityClanRanking {
  name: string;
  tag: string;
  level: number;
  members: number;
  claims: number;
  bankBalance: number;
  power: number;
}

export interface CommunityServer {
  id: string;
  label: string;
  players: number;
}

export interface CommunitySnapshot {
  state: "loading" | "live" | "unavailable";
  updatedAt: string | null;
  playersOnline: number | null;
  servers: CommunityServer[];
  fortunes: CommunityPlayerRanking[];
  playtime: CommunityPlayerRanking[];
  clans: CommunityClanRanking[];
  pvpAvailable: boolean;
}

export interface LauncherSnapshot {
  phase: LauncherPhase;
  launcherVersion: string;
  gameVersion: string;
  statusMessage: string;
  account: LauncherAccount | null;
  deviceCode: DeviceCodeChallenge | null;
  progress: LauncherProgress | null;
  error: LauncherFailure | null;
  server: ServerStatus;
  client: ClientInstallation;
  microsoft: MicrosoftConfiguration;
  community: CommunitySnapshot;
  settings: LauncherSettings;
}

/**
 * Narrow capability surface exposed by the sandboxed preload. Renderer code
 * must not receive credentials, refresh tokens, filesystem paths or arbitrary
 * shell/URL primitives.
 */
export interface BglandsLauncherApi {
  getSnapshot(): Promise<LauncherSnapshot>;
  connectMicrosoft(): Promise<void>;
  cancelMicrosoftLogin(): Promise<void>;
  openMicrosoftLogin(): Promise<void>;
  disconnectMicrosoft(): Promise<void>;
  play(): Promise<void>;
  repair(): Promise<void>;
  openLogs(): Promise<void>;
  openSupport(): Promise<void>;
  saveSettings(settings: LauncherSettings): Promise<void>;
  configureMicrosoft(clientId: string): Promise<void>;
  openMicrosoftSetup(): Promise<void>;
  onSnapshot(listener: (snapshot: LauncherSnapshot) => void): () => void;
}

declare global {
  interface Window {
    bglands: BglandsLauncherApi;
  }
}

export {};
