/**
 * Simple Configuration Service
 * Centralized access to environment variables with fallbacks
 */

// API Configuration
export const getApiBaseUrl = (): string => {
  return import.meta.env.VITE_API_BASE_URL || 'http://localhost:8000';
};

export const getApiTimeout = (): number => {
  return parseInt(import.meta.env.VITE_API_TIMEOUT || '10000');
};

// App Configuration
export const getAppName = (): string => {
  return import.meta.env.VITE_APP_NAME || 'PowerOn';
};

export const getAppVersion = (): string => {
  return import.meta.env.VITE_APP_VERSION || '0.0.0';
};

export const getAppEnvironment = (): string => {
  return import.meta.env.VITE_APP_ENVIRONMENT || 'dev';
};

// Environment Detection
export const isDevelopment = (): boolean => {
  return import.meta.env.MODE === 'development' || getAppEnvironment() === 'dev';
};

export const isProduction = (): boolean => {
  return import.meta.env.MODE === 'production' || getAppEnvironment() === 'prod';
};

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

// Debug Configuration
export const isDebugMode = (): boolean => {
  return import.meta.env.VITE_DEBUG === 'true';
};

export const getLogLevel = (): string => {
  return import.meta.env.VITE_LOG_LEVEL || 'info';
};

export const isConsoleLogsEnabled = (): boolean => {
  return import.meta.env.VITE_ENABLE_CONSOLE_LOGS === 'true';
};

// Microsoft Authentication
export const getMicrosoftClientId = (): string | undefined => {
  return import.meta.env.VITE_MICROSOFT_CLIENT_ID;
};

export const getMicrosoftTenantId = (): string | undefined => {
  return import.meta.env.VITE_MICROSOFT_TENANT_ID;
};

export const getEntraClientSecret = (): string | undefined => {
  return import.meta.env.VITE_ENTRA_CLIENT_SECRET;
};

export const getEntraAuthority = (): string | undefined => {
  return import.meta.env.VITE_ENTRA_AUTHORITY;
};

export const getEntraRedirectPath = (): string | undefined => {
  return import.meta.env.VITE_ENTRA_REDIRECT_PATH;
};

export const getEntraRedirectUri = (): string | undefined => {
  return import.meta.env.VITE_ENTRA_REDIRECT_URI;
};

// Feature Flags (if needed in the future)
export const isFeatureEnabled = (feature: string): boolean => {
  const envKey = `VITE_ENABLE_${feature.toUpperCase()}`;
  return import.meta.env[envKey] === 'true';
};

// Analytics and Monitoring
export const isAnalyticsEnabled = (): boolean => {
  return import.meta.env.VITE_ENABLE_ANALYTICS === 'true';
};

export const isErrorReportingEnabled = (): boolean => {
  return import.meta.env.VITE_ENABLE_ERROR_REPORTING === 'true';
};

export const isPerformanceMonitoringEnabled = (): boolean => {
  return import.meta.env.VITE_ENABLE_PERFORMANCE_MONITORING === 'true';
};

// Development Server (for dev environment)
export const getDevServerPort = (): number => {
  return parseInt(import.meta.env.VITE_DEV_SERVER_PORT || '5176');
};

export const getDevServerHost = (): string => {
  return import.meta.env.VITE_DEV_SERVER_HOST || 'localhost';
};

export const isDevServerHttps = (): boolean => {
  return import.meta.env.VITE_DEV_SERVER_HTTPS === 'true';
};

// Security Configuration
export const isHttpsEnabled = (): boolean => {
  return import.meta.env.VITE_ENABLE_HTTPS === 'true';
};

export const isCspEnabled = (): boolean => {
  return import.meta.env.VITE_ENABLE_CSP === 'true';
};

// Test Configuration
export const isMockDataEnabled = (): boolean => {
  return import.meta.env.VITE_ENABLE_MOCK_DATA === 'true';
};

export const isTestMode = (): boolean => {
  return import.meta.env.VITE_ENABLE_TEST_MODE === 'true';
};

// Convenience object for easy destructuring
export const config = {
  // API
  getApiBaseUrl,
  getApiTimeout,
  
  // App
  getAppName,
  getAppVersion,
  getAppEnvironment,
  
  // Environment
  isDevelopment,
  isProduction,
  isIntegration,
  
  // Debug
  isDebugMode,
  getLogLevel,
  isConsoleLogsEnabled,
  
  // Microsoft Auth
  getMicrosoftClientId,
  getMicrosoftTenantId,
  getEntraClientSecret,
  getEntraAuthority,
  getEntraRedirectPath,
  getEntraRedirectUri,
  
  // Features
  isFeatureEnabled,
  
  // Analytics
  isAnalyticsEnabled,
  isErrorReportingEnabled,
  isPerformanceMonitoringEnabled,
  
  // Dev Server
  getDevServerPort,
  getDevServerHost,
  isDevServerHttps,
  
  // Security
  isHttpsEnabled,
  isCspEnabled,
  
  // Test
  isMockDataEnabled,
  isTestMode,
};
