

export type ServerVersionInfo = {
  packageName: string;
  versionName: string;
  versionCode: number;
  minSupportedVersionCode: number;
  fileName: string;
};

export type StartupStatus =
  | { state: "checking" }
  | { state: "needs-config" }
  | { state: "offline" }
  | { state: "blocked"; reason: string; server: ServerVersionInfo }
  | { state: "warning"; message: string; server: ServerVersionInfo }
  | { state: "ready"; server: ServerVersionInfo | null };

export function evaluateVersion(
  appBuildCode: number,
  server: ServerVersionInfo
): StartupStatus {
  if (appBuildCode < server.minSupportedVersionCode) {
    return {
      state: "blocked",
      reason: "This scanner app is too old and must be updated before use.",
      server,
    };
  }

  if (appBuildCode !== server.versionCode) {
    return {
      state: "warning",
      message: `A newer version is available. Installed build: ${appBuildCode}, latest build: ${server.versionCode}.`,
      server,
    };
  }

  return {
    state: "ready",
    server,
  };
}