type RuntimeEnv = {
  NEXT_PUBLIC_APP_VERSION?: string;
  NEXT_PUBLIC_APP_BUILD_TIME?: string;
};

export interface ClientRuntimeVersion {
  service: 'web';
  version: string;
  buildTime: string;
}

export interface ServerRuntimeVersion {
  service: 'server';
  version: string;
  buildTime: string;
  imageTag: string;
}

export function getClientRuntimeVersion(
  env: RuntimeEnv = {
    NEXT_PUBLIC_APP_VERSION: process.env.NEXT_PUBLIC_APP_VERSION,
    NEXT_PUBLIC_APP_BUILD_TIME: process.env.NEXT_PUBLIC_APP_BUILD_TIME,
  },
): ClientRuntimeVersion {
  return {
    service: 'web',
    version: env.NEXT_PUBLIC_APP_VERSION || 'unknown',
    buildTime: env.NEXT_PUBLIC_APP_BUILD_TIME || '',
  };
}

export function shouldPromptForNewRuntimeVersion(clientVersion: string, serverVersion: string): boolean {
  if (!clientVersion || !serverVersion) return false;
  if (clientVersion === 'unknown' || serverVersion === 'unknown') return false;
  return clientVersion !== serverVersion;
}
