/**
 * Environment Configuration for Filevine Extension
 * Manages dev vs production settings
 */

/**
 * Configuration for different environments
 */
const ENVIRONMENTS = {
  development: {
    proxyUrl: 'http://localhost:3001/api/messages',
    // Development auth token (matches proxy-server/.env)
    proxyAuthToken: 'b6602061be5f38c5c5c61c46d9b91ce149c4ff440adf5b28b6d443924612a9c5'
  },
  production: {
    // Railway production proxy
    proxyUrl: 'https://filevine-extension-production.up.railway.app/api/messages',
    proxyAuthToken: '45c3dad343853583957a3bdeed3f30fbdf6c609d1196a2f2342a20f2bd440262'
  }
};

/**
 * Detect current environment
 * Development: version contains 'dev' or we're running on localhost
 * Production: everything else
 */
function detectEnvironment() {
  try {
    const manifest = chrome.runtime.getManifest();
    const version = manifest.version || '';

    // Check if version indicates development (e.g., "0.1.0-dev" or "0.1.0.dev")
    if (version.includes('dev') || version.includes('DEV')) {
      return 'development';
    }

    // Check if we're in an unpacked extension (development)
    if (chrome.runtime.id === undefined || chrome.runtime.id.length === 0) {
      return 'development';
    }

    // Default to production for safety
    return 'production';
  } catch (error) {
    console.warn('[Environment] Error detecting environment, defaulting to production:', error);
    return 'production';
  }
}

// Detect and export current environment
const ENV = detectEnvironment();
console.log(`[Environment] Detected environment: ${ENV}`);

/**
 * Current configuration based on environment
 */
export const config = ENVIRONMENTS[ENV];

/**
 * Current environment name
 */
export const environment = ENV;

/**
 * Check if running in development mode
 */
export const isDevelopment = ENV === 'development';

/**
 * Check if running in production mode
 */
export const isProduction = ENV === 'production';

// Log configuration (with token masked)
console.log('[Environment] Configuration loaded:');
console.log('   Proxy URL:', config.proxyUrl);
console.log('   Auth Token:', config.proxyAuthToken.substring(0, 16) + '...');
