/**
 * 批量为各个控制器添加导出路由的脚本
 * 使用说明：node scripts/add-export-routes.js
 */

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

// 需要添加导出功能的路由配置
const routeConfigs = [
  {
    routeFile: 'routes/users.js',
    exportPath: '/export',
    controllerMethod: 'exportToExcel',
    permission: 'users:read',
    description: '导出用户列表到Excel'
  },
  {
    routeFile: 'routes/deviceAlerts.js',
    exportPath: '/export',
    controllerMethod: 'exportToExcel',
    permission: 'device_alerts:read',
    description: '导出设备预警列表到Excel'
  },
  {
    routeFile: 'routes/insuranceTypes.js',
    exportPath: '/export',
    controllerMethod: 'exportToExcel',
    permission: 'insurance_types:read',
    description: '导出保险类型列表到Excel'
  },
  {
    routeFile: 'routes/policies.js',
    exportPath: '/export',
    controllerMethod: 'exportToExcel',
    permission: 'policies:read',
    description: '导出保单列表到Excel'
  },
  {
    routeFile: 'routes/livestockClaims.js',
    exportPath: '/export',
    controllerMethod: 'exportToExcel',
    permission: 'livestock_claims:read',
    description: '导出理赔列表到Excel'
  },
  {
    routeFile: 'routes/installationTasks.js',
    exportPath: '/export',
    controllerMethod: 'exportToExcel',
    permission: 'installation_tasks:read',
    description: '导出待安装任务列表到Excel'
  },
  {
    routeFile: 'routes/livestockPolicies.js',
    exportPath: '/export',
    controllerMethod: 'exportToExcel',
    permission: 'livestock_policies:read',
    description: '导出生资保单列表到Excel'
  },
  {
    routeFile: 'routes/operationLogs.js',
    exportPath: '/export',
    controllerMethod: 'exportToExcel',
    permission: 'operation_logs:read',
    description: '导出操作日志到Excel'
  }
];

console.log('🚀 开始为各个路由添加导出功能...\n');

routeConfigs.forEach(config => {
  const filePath = path.join(__dirname, '..', config.routeFile);
  
  try {
    // 读取路由文件
    let content = fs.readFileSync(filePath, 'utf8');
    
    // 检查是否已经存在导出路由
    if (content.includes(`router.get('${config.exportPath}'`) || content.includes(`router.get("${config.exportPath}"`)) {
      console.log(`⏭️  ${config.routeFile} - 导出路由已存在，跳过`);
      return;
    }
    
    // 在module.exports之前添加导出路由
    const exportRoute = `
/**
 * @swagger
 * ${config.exportPath}:
 *   get:
 *     summary: ${config.description}
 *     tags: [Export]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 导出成功
 *         content:
 *           application/vnd.openxmlformats-officedocument.spreadsheetml.sheet:
 *             schema:
 *               type: string
 *               format: binary
 */
router.get('${config.exportPath}', jwtAuth, requirePermission('${config.permission}'), controller.${config.controllerMethod});

`;
    
    // 在module.exports之前插入
    content = content.replace(/module\.exports\s*=\s*router;/, exportRoute + 'module.exports = router;');
    
    // 写回文件
    fs.writeFileSync(filePath, content, 'utf8');
    console.log(`✅ ${config.routeFile} - 导出路由添加成功`);
    
  } catch (error) {
    console.error(`❌ ${config.routeFile} - 添加失败: ${error.message}`);
  }
});

console.log('\n✨ 路由添加完成！');
console.log('⚠️  注意：还需要为各个控制器添加 exportToExcel 方法');

