interface Config {
  wrenApiKey: string;
  isProduction: boolean;
}

// Load environment variables - Next.js requires direct static references
// First try window.env (for Docker), then fall back to process.env (for development)
const getWrenApiKey = (): string => {
  // Check window.env for Docker deployment
  if (typeof window !== 'undefined' && (window as any).env?.WREN_API_TOKEN) {
    return (window as any).env.WREN_API_TOKEN;
  }

  // Fall back to Next.js environment variable (must be direct reference for client-side)
  return process.env.NEXT_PUBLIC_WREN_API_TOKEN || '';
};

const wrenApiKey = getWrenApiKey();

// Initialize config
export const config: Config = {
  wrenApiKey: wrenApiKey || '',
  isProduction: process.env.NODE_ENV === 'production'
};

// Validate required environment variables
if (!config.wrenApiKey) {
  console.error('Missing required environment variable: NEXT_PUBLIC_WREN_API_TOKEN');
  console.error('Current environment:', typeof window !== 'undefined' && (window as any)?.env ? JSON.stringify((window as any).env) : 'No window.env available');
}

// Log config in development
if (!config.isProduction) {
  console.log('Config:', {
    ...config,
    wrenApiKey: config.wrenApiKey ? '[REDACTED]' : 'MISSING'
  });
}
