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

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

/**
 * @swagger
 * /api/cattle-batches:
 *   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: status
 *         schema:
 *           type: string
 *           enum: [进行中, 已完成, 已暂停]
 *         description: 状态筛选
 *       - in: query
 *         name: type
 *         schema:
 *           type: string
 *           enum: [育成批次, 繁殖批次, 育肥批次, 隔离批次, 治疗批次]
 *         description: 类型筛选
 *     responses:
 *       200:
 *         description: 成功获取批次列表
 */
router.get('/', requirePermission('cattle:batches:view'), cattleBatchController.getBatches);

/**
 * @swagger
 * /api/cattle-batches/{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:batches:view'), cattleBatchController.getBatchById);

/**
 * @swagger
 * /api/cattle-batches:
 *   post:
 *     summary: 创建批次
 *     tags: [批次管理]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               name:
 *                 type: string
 *                 description: 批次名称
 *               code:
 *                 type: string
 *                 description: 批次编号
 *               type:
 *                 type: string
 *                 enum: [育成批次, 繁殖批次, 育肥批次, 隔离批次, 治疗批次]
 *                 description: 批次类型
 *               startDate:
 *                 type: string
 *                 format: date
 *                 description: 开始日期
 *               expectedEndDate:
 *                 type: string
 *                 format: date
 *                 description: 预计结束日期
 *               targetCount:
 *                 type: integer
 *                 description: 目标牛只数量
 *               manager:
 *                 type: string
 *                 description: 负责人
 *               remark:
 *                 type: string
 *                 description: 备注
 *     responses:
 *       201:
 *         description: 成功创建批次
 *       400:
 *         description: 请求参数错误
 */
router.post('/', requirePermission('cattle:batches:create'), cattleBatchController.createBatch);

/**
 * @swagger
 * /api/cattle-batches/{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:
 *               name:
 *                 type: string
 *                 description: 批次名称
 *               code:
 *                 type: string
 *                 description: 批次编号
 *               type:
 *                 type: string
 *                 enum: [育成批次, 繁殖批次, 育肥批次, 隔离批次, 治疗批次]
 *                 description: 批次类型
 *               startDate:
 *                 type: string
 *                 format: date
 *                 description: 开始日期
 *               expectedEndDate:
 *                 type: string
 *                 format: date
 *                 description: 预计结束日期
 *               actualEndDate:
 *                 type: string
 *                 format: date
 *                 description: 实际结束日期
 *               targetCount:
 *                 type: integer
 *                 description: 目标牛只数量
 *               currentCount:
 *                 type: integer
 *                 description: 当前牛只数量
 *               manager:
 *                 type: string
 *                 description: 负责人
 *               status:
 *                 type: string
 *                 enum: [进行中, 已完成, 已暂停]
 *                 description: 状态
 *               remark:
 *                 type: string
 *                 description: 备注
 *     responses:
 *       200:
 *         description: 成功更新批次
 *       404:
 *         description: 批次不存在
 */
router.put('/:id', requirePermission('cattle:batches:update'), cattleBatchController.updateBatch);

/**
 * @swagger
 * /api/cattle-batches/{id}:
 *   delete:
 *     summary: 删除批次
 *     tags: [批次管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 批次ID
 *     responses:
 *       200:
 *         description: 成功删除批次
 *       404:
 *         description: 批次不存在
 *       400:
 *         description: 批次中还有牛只，无法删除
 */
router.delete('/:id', requirePermission('cattle:batches:delete'), cattleBatchController.deleteBatch);

/**
 * @swagger
 * /api/cattle-batches/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:batches:delete'), cattleBatchController.batchDeleteBatches);

/**
 * @swagger
 * /api/cattle-batches/{id}/animals:
 *   get:
 *     summary: 获取批次中的牛只
 *     tags: [批次管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 批次ID
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: pageSize
 *         schema:
 *           type: integer
 *           default: 10
 *         description: 每页数量
 *     responses:
 *       200:
 *         description: 成功获取批次牛只
 *       404:
 *         description: 批次不存在
 */
router.get('/:id/animals', requirePermission('cattle:batches:view'), cattleBatchController.getBatchAnimals);

/**
 * @swagger
 * /api/cattle-batches/{id}/animals:
 *   post:
 *     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:
 *               animalIds:
 *                 type: array
 *                 items:
 *                   type: integer
 *                 description: 牛只ID数组
 *     responses:
 *       200:
 *         description: 成功添加牛只到批次
 *       404:
 *         description: 批次不存在
 *       400:
 *         description: 部分牛只已在该批次中
 */
router.post('/:id/animals', requirePermission('cattle:batches:update'), cattleBatchController.addAnimalsToBatch);

/**
 * @swagger
 * /api/cattle-batches/{id}/animals/{animalId}:
 *   delete:
 *     summary: 从批次中移除牛只
 *     tags: [批次管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 批次ID
 *       - in: path
 *         name: animalId
 *         required: true
 *         schema:
 *           type: integer
 *         description: 牛只ID
 *     responses:
 *       200:
 *         description: 成功从批次中移除牛只
 *       404:
 *         description: 牛只不在该批次中
 */
router.delete('/:id/animals/:animalId', requirePermission('cattle:batches:update'), cattleBatchController.removeAnimalFromBatch);

module.exports = router;
