/**
 * Runtime Configuration Types
 *
 * Configuration loaded at container startup from secrets.
 * Mirrors Kubernetes deployment patterns where secrets are mounted as files
 * and read at runtime before application initialization.
 */

/**
 * Application runtime configuration
 * Loaded from window.CONFIG set by /config.js
 */
export interface AppConfig {
  /** Google Maps JavaScript API key for map visualization */
  googleMapsApiKey: string;
  /** Google Maps Map ID for vector basemap and advanced markers */
  googleMapsMapId?: string;
}

/**
 * Window augmentation for runtime config
 * config.js is loaded before the React app initializes
 */
declare global {
  interface Window {
    CONFIG?: AppConfig;
  }
}

/**
 * Get application configuration with validation
 * Throws if required configuration is missing
 *
 * @returns Application configuration object
 * @throws Error if configuration is not loaded or invalid
 */
export function getConfig(): AppConfig {
  if (!window.CONFIG) {
    throw new Error(
      'Application configuration not loaded. Ensure config.js is loaded before the app initializes.'
    );
  }

  return window.CONFIG;
}

/**
 * Get Google Maps API key
 * Returns empty string if key is not available (graceful fallback)
 *
 * @returns Google Maps API key or empty string
 */
export function getGoogleMapsApiKey(): string {
  try {
    const config = getConfig();
    return config.googleMapsApiKey || '';
  } catch {
    console.warn(
      'Google Maps API key not available. Maps functionality will be limited.'
    );
    return '';
  }
}

/**
 * Get Google Maps Map ID (optional)
 *
 * @returns Google Maps Map ID or empty string
 */
export function getGoogleMapsMapId(): string {
  try {
    const config = getConfig();
    return config.googleMapsMapId || '';
  } catch {
    console.warn('Google Maps Map ID not available. Advanced map features may be limited.');
    return '';
  }
}

/**
 * Check if configuration is available
 * Useful for conditional feature enablement
 *
 * @returns true if config is loaded, false otherwise
 */
export function isConfigLoaded(): boolean {
  return !!window.CONFIG;
}
