const path = require('path');
const crypto = require('crypto');

function loadConfig() {
  // [安全] JWT 密钥：优先用环境变量；未设置时生成随机密钥（避免所有部署共用弱密钥）
  let jwtSecret = process.env.JWT_SECRET;
  if (!jwtSecret) {
    jwtSecret = 'litemall_' + crypto.randomBytes(32).toString('hex');
    console.warn('⚠️  未设置 JWT_SECRET，已生成随机密钥。重启后登录态会失效，生产请固定写入 .env。');
  }
  return {
    port: parseInt(process.env.PORT) || 3001,
    host: process.env.HOST || "0.0.0.0",
    jwtSecret,
    jwtExpiresIn: process.env.JWT_EXPIRES_IN || '7d',
    orderTimeoutMinutes: parseInt(process.env.ORDER_TIMEOUT_MINUTES) || 30,
    uploadDir: process.env.UPLOAD_DIR || 'uploads',
    dbPath: path.join(__dirname, '..', 'data', 'litemall.db'),
  };
}

module.exports = loadConfig();
