const express = require('express');
const router = express.Router();
const cattleExitRecordController = require('../controllers/cattleExitRecordController');
const { verifyToken } = require('../middleware/auth');
const { requirePermission } = require('../middleware/permission');

// 所有路由都需要认证
router.use(verifyToken);

/**
 * @swagger
 * /api/cattle-exit-records:
 *   get:
 *     summary: 获取离栏记录列表
 *     tags: [离栏记录管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: pageSize
 *         schema:
 *           type: integer
 *           default: 10
 *         description: 每页数量
 *       - in: query
 *         name: search
 *         schema:
 *           type: string
 *         description: 搜索关键词
 *       - in: query
 *         name: exitReason
 *         schema:
 *           type: string
 *           enum: [出售, 死亡, 淘汰, 转场, 其他]
 *         description: 离栏原因筛选
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *           enum: [已确认, 待确认, 已取消]
 *         description: 状态筛选
 *       - in: query
 *         name: dateRange
 *         schema:
 *           type: array
 *           items:
 *             type: string
 *             format: date
 *         description: 日期范围
 *     responses:
 *       200:
 *         description: 成功获取离栏记录列表
 */
router.get('/', requirePermission('cattle:exit:view'), cattleExitRecordController.getExitRecords);

/**
 * @swagger
 * /api/cattle-exit-records/{id}:
 *   get:
 *     summary: 获取离栏记录详情
 *     tags: [离栏记录管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 离栏记录ID
 *     responses:
 *       200:
 *         description: 成功获取离栏记录详情
 *       404:
 *         description: 离栏记录不存在
 */
router.get('/:id', requirePermission('cattle:exit:view'), cattleExitRecordController.getExitRecordById);

/**
 * @swagger
 * /api/cattle-exit-records:
 *   post:
 *     summary: 创建离栏记录
 *     tags: [离栏记录管理]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               animalId:
 *                 type: integer
 *                 description: 动物ID
 *               exitDate:
 *                 type: string
 *                 format: date
 *                 description: 离栏日期
 *               exitReason:
 *                 type: string
 *                 enum: [出售, 死亡, 淘汰, 转场, 其他]
 *                 description: 离栏原因
 *               originalPenId:
 *                 type: integer
 *                 description: 原栏舍ID
 *               destination:
 *                 type: string
 *                 description: 去向
 *               disposalMethod:
 *                 type: string
 *                 enum: [屠宰, 转售, 掩埋, 焚烧, 其他]
 *                 description: 处理方式
 *               handler:
 *                 type: string
 *                 description: 处理人员
 *               status:
 *                 type: string
 *                 enum: [已确认, 待确认, 已取消]
 *                 description: 状态
 *               remark:
 *                 type: string
 *                 description: 备注
 *     responses:
 *       201:
 *         description: 成功创建离栏记录
 *       400:
 *         description: 请求参数错误
 */
router.post('/', requirePermission('cattle:exit:create'), cattleExitRecordController.createExitRecord);

/**
 * @swagger
 * /api/cattle-exit-records/{id}:
 *   put:
 *     summary: 更新离栏记录
 *     tags: [离栏记录管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 离栏记录ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               exitDate:
 *                 type: string
 *                 format: date
 *                 description: 离栏日期
 *               exitReason:
 *                 type: string
 *                 enum: [出售, 死亡, 淘汰, 转场, 其他]
 *                 description: 离栏原因
 *               destination:
 *                 type: string
 *                 description: 去向
 *               disposalMethod:
 *                 type: string
 *                 enum: [屠宰, 转售, 掩埋, 焚烧, 其他]
 *                 description: 处理方式
 *               handler:
 *                 type: string
 *                 description: 处理人员
 *               status:
 *                 type: string
 *                 enum: [已确认, 待确认, 已取消]
 *                 description: 状态
 *               remark:
 *                 type: string
 *                 description: 备注
 *     responses:
 *       200:
 *         description: 成功更新离栏记录
 *       404:
 *         description: 离栏记录不存在
 */
router.put('/:id', requirePermission('cattle:exit:update'), cattleExitRecordController.updateExitRecord);

/**
 * @swagger
 * /api/cattle-exit-records/{id}:
 *   delete:
 *     summary: 删除离栏记录
 *     tags: [离栏记录管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 离栏记录ID
 *     responses:
 *       200:
 *         description: 成功删除离栏记录
 *       404:
 *         description: 离栏记录不存在
 */
router.delete('/:id', requirePermission('cattle:exit:delete'), cattleExitRecordController.deleteExitRecord);

/**
 * @swagger
 * /api/cattle-exit-records/batch-delete:
 *   post:
 *     summary: 批量删除离栏记录
 *     tags: [离栏记录管理]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               ids:
 *                 type: array
 *                 items:
 *                   type: integer
 *                 description: 离栏记录ID数组
 *     responses:
 *       200:
 *         description: 成功批量删除离栏记录
 *       400:
 *         description: 请求参数错误
 */
router.post('/batch-delete', requirePermission('cattle:exit:delete'), cattleExitRecordController.batchDeleteExitRecords);

/**
 * @swagger
 * /api/cattle-exit-records/{id}/confirm:
 *   post:
 *     summary: 确认离栏记录
 *     tags: [离栏记录管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 离栏记录ID
 *     responses:
 *       200:
 *         description: 成功确认离栏记录
 *       404:
 *         description: 离栏记录不存在
 *       400:
 *         description: 记录已确认，无需重复操作
 */
router.post('/:id/confirm', requirePermission('cattle:exit:update'), cattleExitRecordController.confirmExitRecord);

/**
 * @swagger
 * /api/cattle-exit-records/available-animals:
 *   get:
 *     summary: 获取可用的牛只列表
 *     tags: [离栏记录管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: search
 *         schema:
 *           type: string
 *         description: 搜索关键词
 *     responses:
 *       200:
 *         description: 成功获取可用牛只列表
 */
router.get('/available-animals', requirePermission('cattle:exit:view'), cattleExitRecordController.getAvailableAnimals);

module.exports = router;
