// Central config loader. Keep shared (base) config here and move
// environment-specific values into config.development.js / config.production.js.

const env = process.env.NODE_ENV || 'development';

let envConfig = {};
try {
  if (env === 'production') {
    envConfig = require('./config.production.js');
  } else {
    envConfig = require('./config.development.js');
  }
} catch (error) {
  console.warn(`Warning: failed to load ${env} config, using defaults.`);
}

// Shared config (all environments)
const baseConfig = {
  "port": {
    "node": 9099,
    "web": 9091
  },
  "allowUrls": [
    "/admin_api/sys_user/login",
    "/admin_api/sys_user/authorityMenus",
    "/admin_api/sys_user/register",
    "/file/",
    "/sys_file/",
    "/api/docs",
    "api/swagger.json",
    "/api/auth/verify",
    // 转发层路由白名单（框架不鉴权，由控制器内部做 Token 鉴权）
    // 与 swagger 一致的无 /api 前缀路径
    "/admin/",
    "/applet/",
    "/equipment/",
    "/favor/",
    "/finder/",
    "/friend/",
    "/group/",
    "/label/",
    "/login/",
    "/message/",
    "/other/",
    "/pay/",
    "/qy/",
    "/shop/",
    "/sns/",
    "/user/",
    "/ws/",
    "/api/admin/",
    "/api/applet/",
    "/api/equipment/",
    "/api/favor/",
    "/api/finder/",
    "/api/friend/",
    "/api/group/",
    "/api/label/",
    "/api/login/",
    "/api/message/",
    "/api/other/",
    "/api/pay/",
    "/api/qy/",
    "/api/shop/",
    "/api/sns/",
    "/api/user/",
    "/api/ws/"
  ]
};

const finalConfig = {
  ...baseConfig,
  ...envConfig,
  // Deep-merge wechat if present
  wechat: {
    ...(baseConfig.wechat || {}),
    ...(envConfig.wechat || {})
  }
};

console.log(`Config env: ${env}`);
console.log(`Config qywx env: ${finalConfig.qywx?.env || 'unknown'}`);

module.exports = finalConfig;
