import logger from './logger'

const required = ['JWT_SECRET', 'JWT_REFRESH_SECRET', 'ENCRYPTION_KEY']
const defaults: Record<string, string> = {
  JWT_SECRET: 'dev-secret',
  JWT_REFRESH_SECRET: 'dev-refresh-secret',
  ENCRYPTION_KEY: 'default-32-byte-encryption-key!!',
}

export function validateEnv(): void {
  const missing: string[] = []
  for (const key of required) {
    if (!process.env[key] || process.env[key] === defaults[key]) {
      missing.push(key)
    }
  }
  if (missing.length > 0 && process.env.NODE_ENV === 'production') {
    logger.fatal({ missing }, '以下环境变量未设置或使用了默认值，生产环境禁止启动')
    process.exit(1)
  }
  if (missing.length > 0) {
    logger.warn({ missing }, '以下环境变量使用了默认值，仅限开发环境')
  }
}
