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

// 公开API路由，不需要验证token
const publicRoutes = express.Router();
router.use('/public', publicRoutes);

// 公开获取栏舍列表
publicRoutes.get('/', cattlePenController.getPens);

// 公开获取栏舍详情
publicRoutes.get('/:id', cattlePenController.getPenById);

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

/**
 * @swagger
 * /api/cattle-pens:
 *   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: 成功获取栏舍列表
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: object
 *                   properties:
 *                     list:
 *                       type: array
 *                       items:
 *                         $ref: '#/components/schemas/CattlePen'
 *                     total:
 *                       type: integer
 *                     page:
 *                       type: integer
 *                     pageSize:
 *                       type: integer
 *                 message:
 *                   type: string
 */
router.get('/', requirePermission('cattle:pens:view'), cattlePenController.getPens);

/**
 * @swagger
 * /api/cattle-pens/{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:pens:view'), cattlePenController.getPenById);

/**
 * @swagger
 * /api/cattle-pens:
 *   post:
 *     summary: 创建栏舍
 *     tags: [栏舍管理]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             $ref: '#/components/schemas/CattlePenInput'
 *     responses:
 *       201:
 *         description: 成功创建栏舍
 *       400:
 *         description: 请求参数错误
 */
router.post('/', requirePermission('cattle:pens:create'), cattlePenController.createPen);

/**
 * @swagger
 * /api/cattle-pens/{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:
 *             $ref: '#/components/schemas/CattlePenInput'
 *     responses:
 *       200:
 *         description: 成功更新栏舍
 *       404:
 *         description: 栏舍不存在
 */
router.put('/:id', requirePermission('cattle:pens:update'), cattlePenController.updatePen);

/**
 * @swagger
 * /api/cattle-pens/{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:pens:delete'), cattlePenController.deletePen);

/**
 * @swagger
 * /api/cattle-pens/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:pens:delete'), cattlePenController.batchDeletePens);

/**
 * @swagger
 * /api/cattle-pens/{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:pens:view'), cattlePenController.getPenAnimals);

module.exports = router;
