require('dotenv').config()

const config = {
  nodeEnv: process.env.NODE_ENV || 'development',
  port: parseInt(process.env.PORT, 10) || 3000,
  region: process.env.TENCENT_REGION || 'ap-guangzhou',
  db: {
    host: process.env.DB_HOST,
    port: parseInt(process.env.DB_PORT, 10) || 3306,
    user: process.env.DB_USER || 'root',
    password: process.env.DB_PASSWORD,
    database: process.env.DB_NAME || 'jw_beauty'
  },
  cos: {
    secretId: process.env.TENCENT_SECRET_ID,
    secretKey: process.env.TENCENT_SECRET_KEY,
    bucket: process.env.COS_BUCKET || 'jw-bucket-1426323813',
    region: process.env.COS_REGION || process.env.TENCENT_REGION || 'ap-guangzhou',
    cdnDomain: process.env.COS_CDN_DOMAIN || 'tx.vsai.net.cn'
  },
  wechat: {
    appid: process.env.WECHAT_APPID,
    secret: process.env.WECHAT_SECRET
  },
  jwt: {
    secret: process.env.JWT_SECRET || 'dev-user-secret',
    adminSecret: process.env.ADMIN_JWT_SECRET || 'dev-admin-secret',
    expiresIn: '7d'
  },
  admin: {
    username: process.env.ADMIN_USERNAME || 'admin',
    password: process.env.ADMIN_PASSWORD || 'admin'
  }
}

if (config.nodeEnv === 'production') {
  if (config.jwt.secret === 'dev-user-secret') throw new Error('JWT_SECRET must be set in production')
  if (config.jwt.adminSecret === 'dev-admin-secret') throw new Error('ADMIN_JWT_SECRET must be set in production')
  if (config.admin.username === 'admin' || config.admin.password === 'admin') throw new Error('ADMIN_USERNAME and ADMIN_PASSWORD must be changed from defaults in production')
}

module.exports = config
