/**
 * Environment variable validation for production builds
 * Ensures all required environment variables are set before deployment
 */

const requiredEnvVars = [
  'NEXT_PUBLIC_SITE_URL',
  'NODE_ENV',
] as const

const optionalEnvVars = [
  'PORT',
  'HOSTNAME',
  'NEXT_PUBLIC_GA_ID',
] as const

export function validateEnvironment() {
  const missingVars: string[] = []

  // Check required variables
  for (const varName of requiredEnvVars) {
    if (!process.env[varName]) {
      missingVars.push(varName)
    }
  }

  if (missingVars.length > 0) {
    console.error('❌ Missing required environment variables:')
    missingVars.forEach(varName => {
      console.error(`   - ${varName}`)
    })
    console.error('\n💡 Check .env.example for reference')
    throw new Error('Environment validation failed')
  }

  // Log configuration (safe - no secrets)
  if (process.env.NODE_ENV === 'production') {
    console.log('✅ Environment validation passed')
    console.log(`   - NEXT_PUBLIC_SITE_URL: ${process.env.NEXT_PUBLIC_SITE_URL}`)
    console.log(`   - PORT: ${process.env.PORT || '3030'}`)
  }
}

// Run validation for production builds
if (process.env.NODE_ENV === 'production') {
  validateEnvironment()
}
