const express = require('express');
const router = express.Router();
const installationTaskController = require('../controllers/installationTaskController');
const { jwtAuth, requirePermission } = require('../middleware/auth');

/**
 * @swagger
 * tags:
 *   name: InstallationTasks
 *   description: 待安装任务管理
 */

/**
 * @swagger
 * /api/installation-tasks/stats:
 *   get:
 *     summary: 获取安装任务统计数据
 *     tags: [InstallationTasks]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 */
router.get('/stats', jwtAuth, requirePermission('installation_tasks:read'), installationTaskController.getInstallationTaskStats);

/**
 * @swagger
 * /api/installation-tasks/export:
 *   get:
 *     summary: 导出待安装任务到Excel
 *     tags: [InstallationTasks]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: policyNumber
 *         schema:
 *           type: string
 *         description: 保单编号
 *       - in: query
 *         name: keyword
 *         schema:
 *           type: string
 *         description: 关键字搜索
 *     responses:
 *       200:
 *         description: 导出成功
 *         content:
 *           application/vnd.openxmlformats-officedocument.spreadsheetml.sheet:
 *             schema:
 *               type: string
 *               format: binary
 */
router.get('/export', jwtAuth, requirePermission('installation_tasks:read'), installationTaskController.exportToExcel);

/**
 * @swagger
 * /api/installation-tasks:
 *   get:
 *     summary: 获取待安装任务列表
 *     tags: [InstallationTasks]
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           default: 10
 *         description: 每页数量
 *       - in: query
 *         name: policyNumber
 *         schema:
 *           type: string
 *         description: 保单编号搜索
 *       - in: query
 *         name: customerName
 *         schema:
 *           type: string
 *         description: 客户姓名搜索
 *       - in: query
 *         name: keyword
 *         schema:
 *           type: string
 *         description: 关键字搜索
 *       - in: query
 *         name: installationStatus
 *         schema:
 *           type: string
 *           enum: [待安装, 安装中, 已安装, 安装失败, 已取消]
 *         description: 安装状态筛选
 *       - in: query
 *         name: priority
 *         schema:
 *           type: string
 *           enum: [低, 中, 高, 紧急]
 *         description: 优先级筛选
 *     responses:
 *       200:
 *         description: 获取成功
 */
router.get('/', jwtAuth, requirePermission('installation_tasks:read'), installationTaskController.getInstallationTasks);

/**
 * @swagger
 * /api/installation-tasks:
 *   post:
 *     summary: 创建待安装任务
 *     tags: [InstallationTasks]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - applicationNumber
 *               - policyNumber
 *               - productName
 *               - customerName
 *             properties:
 *               applicationNumber:
 *                 type: string
 *                 description: 申请单号
 *               policyNumber:
 *                 type: string
 *                 description: 保单编号
 *               productName:
 *                 type: string
 *                 description: 产品名称
 *               customerName:
 *                 type: string
 *                 description: 客户姓名
 *               idType:
 *                 type: string
 *                 enum: [身份证, 护照, 军官证, 士兵证, 港澳台居民居住证, 其他]
 *                 description: 证件类型
 *               idNumber:
 *                 type: string
 *                 description: 证件号码
 *               livestockSupplyType:
 *                 type: string
 *                 description: 养殖生资种类
 *               pendingDevices:
 *                 type: array
 *                 description: 待安装设备
 *               priority:
 *                 type: string
 *                 enum: [低, 中, 高, 紧急]
 *                 description: 优先级
 *               assignedTo:
 *                 type: integer
 *                 description: 分配给用户ID
 *     responses:
 *       201:
 *         description: 创建成功
 */
router.post('/', jwtAuth, requirePermission('installation_tasks:create'), installationTaskController.createInstallationTask);

/**
 * @swagger
 * /api/installation-tasks/{id}:
 *   get:
 *     summary: 根据ID获取待安装任务详情
 *     tags: [InstallationTasks]
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 任务ID
 *     responses:
 *       200:
 *         description: 获取成功
 *       404:
 *         description: 任务不存在
 */
router.get('/:id', jwtAuth, requirePermission('installation_tasks:read'), installationTaskController.getInstallationTaskById);

/**
 * @swagger
 * /api/installation-tasks/{id}:
 *   put:
 *     summary: 更新待安装任务
 *     tags: [InstallationTasks]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 任务ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               installationStatus:
 *                 type: string
 *                 enum: [待安装, 安装中, 已安装, 安装失败, 已取消]
 *               priority:
 *                 type: string
 *                 enum: [低, 中, 高, 紧急]
 *               notes:
 *                 type: string
 *     responses:
 *       200:
 *         description: 更新成功
 */
router.put('/:id', jwtAuth, requirePermission('installation_tasks:update'), installationTaskController.updateInstallationTask);

/**
 * @swagger
 * /api/installation-tasks/{id}:
 *   delete:
 *     summary: 删除待安装任务
 *     tags: [InstallationTasks]
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 任务ID
 *     responses:
 *       200:
 *         description: 删除成功
 *       404:
 *         description: 任务不存在
 */
router.delete('/:id', jwtAuth, requirePermission('installation_tasks:delete'), installationTaskController.deleteInstallationTask);

/**
 * @swagger
 * /api/installation-tasks/batch/operate:
 *   post:
 *     summary: 批量操作待安装任务
 *     tags: [InstallationTasks]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - ids
 *               - operation
 *             properties:
 *               ids:
 *                 type: array
 *                 items:
 *                   type: integer
 *                 description: 任务ID数组
 *               operation:
 *                 type: string
 *                 enum: [assign, updateStatus, delete]
 *                 description: 操作类型
 *               data:
 *                 type: object
 *                 description: 操作数据
 *     responses:
 *       200:
 *         description: 操作成功
 */
router.post('/batch/operate', jwtAuth, requirePermission('installation_tasks:update'), installationTaskController.batchOperateInstallationTasks);

module.exports = router;