/**
 * Universal Configuration - Works in both Node.js and Browser
 */

// Helper to get environment variable (works in both Node.js and browser)
const getEnvVar = (key, defaultValue = undefined) => {
  // Node.js environment
  if (typeof process !== 'undefined' && process.env) {
    return process.env[key] || defaultValue;
  }
  
  // Browser environment (Vite)
  if (typeof import !== 'undefined' && import.meta && import.meta.env) {
    return import.meta.env[key] || defaultValue;
  }
  
  return defaultValue;
};

// API Configuration
export const getApiBaseUrl = () => {
  return getEnvVar('VITE_API_BASE_URL', 'http://localhost:8000');
};

export const getApiTimeout = () => {
  return parseInt(getEnvVar('VITE_API_TIMEOUT', '10000'));
};

// App Configuration
export const getAppName = () => {
  return getEnvVar('VITE_APP_NAME', 'PowerOn');
};

export const getAppVersion = () => {
  return getEnvVar('VITE_APP_VERSION', '0.0.0');
};

export const getAppEnvironment = () => {
  return getEnvVar('VITE_APP_ENVIRONMENT', 'dev');
};

// Environment Detection
export const isDevelopment = () => {
  const mode = getEnvVar('NODE_ENV') || getEnvVar('MODE');
  const appEnv = getAppEnvironment();
  return mode === 'development' || appEnv === 'dev';
};

export const isProduction = () => {
  const mode = getEnvVar('NODE_ENV') || getEnvVar('MODE');
  const appEnv = getAppEnvironment();
  return mode === 'production' || appEnv === 'prod';
};

export const isIntegration = () => {
  return getAppEnvironment() === 'int';
};

// Debug Configuration
export const isDebugMode = () => {
  return getEnvVar('VITE_DEBUG') === 'true';
};

export const getLogLevel = () => {
  return getEnvVar('VITE_LOG_LEVEL', 'info');
};

// Microsoft Authentication
export const getMicrosoftClientId = () => {
  return getEnvVar('VITE_MICROSOFT_CLIENT_ID');
};

export const getMicrosoftTenantId = () => {
  return getEnvVar('VITE_MICROSOFT_TENANT_ID');
};

export const getEntraAuthority = () => {
  return getEnvVar('VITE_ENTRA_AUTHORITY');
};

export const getEntraRedirectUri = () => {
  return getEnvVar('VITE_ENTRA_REDIRECT_URI');
};

// Convenience object
export const config = {
  getApiBaseUrl,
  getApiTimeout,
  getAppName,
  getAppVersion,
  getAppEnvironment,
  isDevelopment,
  isProduction,
  isIntegration,
  isDebugMode,
  getLogLevel,
  getMicrosoftClientId,
  getMicrosoftTenantId,
  getEntraAuthority,
  getEntraRedirectUri,
};
