import dotenv from 'dotenv'

// Load environment variables early
dotenv.config()

export interface AppConfig {
  nodeEnv: string
  port: number
  jwtSecret: string | null
  email: {
    host?: string
    port?: number
    user?: string
    pass?: string
    secure?: boolean
    from?: string
  }
}

export function getConfig(): AppConfig {
  return {
    nodeEnv: process.env.NODE_ENV || 'development',
    port: Number(process.env.PORT || 3004),
    jwtSecret: process.env.JWT_SECRET || null,
    email: {
      host: process.env.EMAIL_HOST,
      port: process.env.EMAIL_PORT ? Number(process.env.EMAIL_PORT) : undefined,
      user: process.env.EMAIL_USER,
      pass: process.env.EMAIL_PASS,
      secure: process.env.EMAIL_SECURE === 'true',
      from: process.env.EMAIL_FROM,
    },
  }
}

export function assertProdSecretsSet(cfg: AppConfig) {
  if (cfg.nodeEnv === 'production') {
    if (!cfg.jwtSecret) {
      throw new Error('JWT_SECRET must be set in production')
    }
  }
}

