const AUTHENTICATION_MODE = {
  NONE: 'none',
  AURORA_HUB: 'aurora-hub',
  LOCAL_PROVIDER: 'local-provider',
} as const;

export type AuthenticationMode = (typeof AUTHENTICATION_MODE)[keyof typeof AUTHENTICATION_MODE];

const VALID_MODES: AuthenticationMode[] = [
  AUTHENTICATION_MODE.NONE,
  AUTHENTICATION_MODE.AURORA_HUB,
  AUTHENTICATION_MODE.LOCAL_PROVIDER,
];

/**
 * Resolves the authentication mode from the raw value of `OAUTH_STRATEGY`.
 *
 * - Defaults to `'none'` when the raw value is absent or empty (clean install).
 * - Throws a clear bootstrap-time error listing all valid values when the raw value is unrecognized.
 * - MUST be called at runtime (inside a `useFactory` or constructor), NOT at module-eval time,
 *   to respect `.env` load order via `ConfigModule.forRoot({ isGlobal: true })`.
 */
export function resolveAuthenticationMode(raw: string | undefined): AuthenticationMode {
  const trimmed = raw?.trim() ?? '';

  if (trimmed === '') {
    return AUTHENTICATION_MODE.NONE;
  }

  if ((VALID_MODES as string[]).includes(trimmed)) {
    return trimmed as AuthenticationMode;
  }

  throw new Error(`Invalid OAUTH_STRATEGY="${trimmed}". Valid values: ${VALID_MODES.join(', ')}.`);
}
