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

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

/**
 * @swagger
 * /api/cattle-transfer-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: fromPen
 *         schema:
 *           type: integer
 *         description: 转出栏舍ID
 *       - in: query
 *         name: toPen
 *         schema:
 *           type: integer
 *         description: 转入栏舍ID
 *       - in: query
 *         name: dateRange
 *         schema:
 *           type: array
 *           items:
 *             type: string
 *             format: date
 *         description: 日期范围
 *     responses:
 *       200:
 *         description: 成功获取转栏记录列表
 */
router.get('/', requirePermission('cattle:transfer:view'), cattleTransferRecordController.getTransferRecords);

/**
 * @swagger
 * /api/cattle-transfer-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:transfer:view'), cattleTransferRecordController.getTransferRecordById);

/**
 * @swagger
 * /api/cattle-transfer-records:
 *   post:
 *     summary: 创建转栏记录
 *     tags: [转栏记录管理]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               animalId:
 *                 type: integer
 *                 description: 动物ID
 *               fromPenId:
 *                 type: integer
 *                 description: 转出栏舍ID
 *               toPenId:
 *                 type: integer
 *                 description: 转入栏舍ID
 *               transferDate:
 *                 type: string
 *                 format: date
 *                 description: 转栏日期
 *               reason:
 *                 type: string
 *                 enum: [正常调栏, 疾病治疗, 配种需要, 产房准备, 隔离观察, 其他]
 *                 description: 转栏原因
 *               operator:
 *                 type: string
 *                 description: 操作人员
 *               status:
 *                 type: string
 *                 enum: [已完成, 进行中]
 *                 description: 状态
 *               remark:
 *                 type: string
 *                 description: 备注
 *     responses:
 *       201:
 *         description: 成功创建转栏记录
 *       400:
 *         description: 请求参数错误
 */
router.post('/', requirePermission('cattle:transfer:create'), cattleTransferRecordController.createTransferRecord);

/**
 * @swagger
 * /api/cattle-transfer-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:
 *               fromPenId:
 *                 type: integer
 *                 description: 转出栏舍ID
 *               toPenId:
 *                 type: integer
 *                 description: 转入栏舍ID
 *               transferDate:
 *                 type: string
 *                 format: date
 *                 description: 转栏日期
 *               reason:
 *                 type: string
 *                 enum: [正常调栏, 疾病治疗, 配种需要, 产房准备, 隔离观察, 其他]
 *                 description: 转栏原因
 *               operator:
 *                 type: string
 *                 description: 操作人员
 *               status:
 *                 type: string
 *                 enum: [已完成, 进行中]
 *                 description: 状态
 *               remark:
 *                 type: string
 *                 description: 备注
 *     responses:
 *       200:
 *         description: 成功更新转栏记录
 *       404:
 *         description: 转栏记录不存在
 */
router.put('/:id', requirePermission('cattle:transfer:update'), cattleTransferRecordController.updateTransferRecord);

/**
 * @swagger
 * /api/cattle-transfer-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:transfer:delete'), cattleTransferRecordController.deleteTransferRecord);

/**
 * @swagger
 * /api/cattle-transfer-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:transfer:delete'), cattleTransferRecordController.batchDeleteTransferRecords);

/**
 * @swagger
 * /api/cattle-transfer-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:transfer:view'), cattleTransferRecordController.getAvailableAnimals);

module.exports = router;
