// Runtime configuration for Kubernetes deployments
// This allows environment variables to be injected at container startup

interface RuntimeConfig {
  VITE_API_URL: string;
  VITE_APP_TITLE: string;
  VITE_APP_VERSION: string;
}

declare global {
  interface Window {
    __RUNTIME_CONFIG__?: RuntimeConfig;
  }
}

// Types are defined in vite-env.d.ts

// Get configuration from runtime or fall back to build-time environment variables
function getRuntimeConfig(): RuntimeConfig {
  // First try to get from window (injected at runtime in Kubernetes)
  if (typeof window !== 'undefined' && window.__RUNTIME_CONFIG__) {
    return window.__RUNTIME_CONFIG__;
  }

  // Fall back to build-time environment variables (development/local)
  return {
    VITE_API_URL: import.meta.env.VITE_API_URL || 'http://localhost:8000',
    VITE_APP_TITLE: import.meta.env.VITE_APP_TITLE || 'PanIA Dashboard',
    VITE_APP_VERSION: import.meta.env.VITE_APP_VERSION || '1.0.0',
  };
}

export const config = getRuntimeConfig();

// Helper function to get the API base URL
export function getApiUrl(): string {
  return config.VITE_API_URL;
}

// Helper function to get app title
export function getAppTitle(): string {
  return config.VITE_APP_TITLE;
}

// Helper function to get app version
export function getAppVersion(): string {
  return config.VITE_APP_VERSION;
}

// Helper to check if running in Kubernetes
export function isKubernetesEnvironment(): boolean {
  return typeof window !== 'undefined' && !!window.__RUNTIME_CONFIG__;
}

// Debug function to log current configuration
export function logConfig(): void {
  console.log('Current configuration:', {
    ...config,
    isKubernetes: isKubernetesEnvironment(),
    source: isKubernetesEnvironment() ? 'runtime' : 'build-time'
  });
}