/**
 * Server Configuration Service
 * Node.js compatible configuration for server files
 */

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

export const getApiTimeout = () => {
  return parseInt(process.env.VITE_API_TIMEOUT || '10000');
};

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

export const getAppVersion = () => {
  return process.env.VITE_APP_VERSION || '0.0.0';
};

export const getAppEnvironment = () => {
  return process.env.VITE_APP_ENVIRONMENT || 'dev';
};

// Environment Detection
export const isDevelopment = () => {
  return process.env.NODE_ENV === 'development' || getAppEnvironment() === 'dev';
};

export const isProduction = () => {
  return process.env.NODE_ENV === 'production' || getAppEnvironment() === 'prod';
};

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

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

export const getLogLevel = () => {
  return process.env.VITE_LOG_LEVEL || 'info';
};

export const isConsoleLogsEnabled = () => {
  return process.env.VITE_ENABLE_CONSOLE_LOGS === 'true';
};

// Microsoft Authentication
export const getMicrosoftClientId = () => {
  return process.env.VITE_MICROSOFT_CLIENT_ID;
};

export const getMicrosoftTenantId = () => {
  return process.env.VITE_MICROSOFT_TENANT_ID;
};

export const getEntraClientSecret = () => {
  return process.env.VITE_ENTRA_CLIENT_SECRET;
};

export const getEntraAuthority = () => {
  return process.env.VITE_ENTRA_AUTHORITY;
};

export const getEntraRedirectPath = () => {
  return process.env.VITE_ENTRA_REDIRECT_PATH;
};

export const getEntraRedirectUri = () => {
  return process.env.VITE_ENTRA_REDIRECT_URI;
};

// Feature Flags
export const isFeatureEnabled = (feature) => {
  const envKey = `VITE_ENABLE_${feature.toUpperCase()}`;
  return process.env[envKey] === 'true';
};

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

export const isErrorReportingEnabled = () => {
  return process.env.VITE_ENABLE_ERROR_REPORTING === 'true';
};

export const isPerformanceMonitoringEnabled = () => {
  return process.env.VITE_ENABLE_PERFORMANCE_MONITORING === 'true';
};

// Development Server
export const getDevServerPort = () => {
  return parseInt(process.env.VITE_DEV_SERVER_PORT || '5176');
};

export const getDevServerHost = () => {
  return process.env.VITE_DEV_SERVER_HOST || 'localhost';
};

export const isDevServerHttps = () => {
  return process.env.VITE_DEV_SERVER_HTTPS === 'true';
};

// Security Configuration
export const isHttpsEnabled = () => {
  return process.env.VITE_ENABLE_HTTPS === 'true';
};

export const isCspEnabled = () => {
  return process.env.VITE_ENABLE_CSP === 'true';
};

// Test Configuration
export const isMockDataEnabled = () => {
  return process.env.VITE_ENABLE_MOCK_DATA === 'true';
};

export const isTestMode = () => {
  return process.env.VITE_ENABLE_TEST_MODE === 'true';
};

// Server-specific configuration
export const getServerPort = () => {
  return process.env.PORT || 3000;
};

export const getServerHost = () => {
  return process.env.HOST || '0.0.0.0';
};

// 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,
  
  // Server
  getServerPort,
  getServerHost,
};
