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

// 应用认证中间件
router.use(verifyToken)

/**
 * @swagger
 * tags:
 *   name: ElectronicFence
 *   description: 电子围栏管理
 */

/**
 * @swagger
 * /api/electronic-fence:
 *   get:
 *     summary: 获取围栏列表
 *     tags: [ElectronicFence]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           default: 10
 *         description: 每页数量
 *       - in: query
 *         name: search
 *         schema:
 *           type: string
 *         description: 搜索关键词
 *       - in: query
 *         name: type
 *         schema:
 *           type: string
 *           enum: [collector, grazing, safety]
 *         description: 围栏类型
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *           enum: [grazing, not_grazing]
 *         description: 放牧状态
 *       - in: query
 *         name: farm_id
 *         schema:
 *           type: integer
 *         description: 农场ID
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: array
 *                   items:
 *                     type: object
 *                     properties:
 *                       id:
 *                         type: integer
 *                       name:
 *                         type: string
 *                       type:
 *                         type: string
 *                       coordinates:
 *                         type: array
 *                         items:
 *                           type: object
 *                           properties:
 *                             lng:
 *                               type: number
 *                             lat:
 *                               type: number
 *                       grazingStatus:
 *                         type: string
 *                       insideCount:
 *                         type: integer
 *                       outsideCount:
 *                         type: integer
 *                 total:
 *                   type: integer
 *                 page:
 *                   type: integer
 *                 limit:
 *                   type: integer
 */
router.get('/', 
  requirePermission('smart_fence:view'),
  electronicFenceController.getFences
)

/**
 * @swagger
 * /api/electronic-fence/{id}:
 *   get:
 *     summary: 获取围栏详情
 *     tags: [ElectronicFence]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 围栏ID
 *     responses:
 *       200:
 *         description: 获取成功
 *       404:
 *         description: 围栏不存在
 */
router.get('/:id', 
  requirePermission('smart_fence:view'),
  electronicFenceController.getFenceById
)

/**
 * @swagger
 * /api/electronic-fence:
 *   post:
 *     summary: 创建围栏
 *     tags: [ElectronicFence]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - name
 *               - coordinates
 *             properties:
 *               name:
 *                 type: string
 *                 description: 围栏名称
 *               type:
 *                 type: string
 *                 enum: [collector, grazing, safety]
 *                 default: collector
 *                 description: 围栏类型
 *               description:
 *                 type: string
 *                 description: 围栏描述
 *               coordinates:
 *                 type: array
 *                 items:
 *                   type: object
 *                   properties:
 *                     lng:
 *                       type: number
 *                     lat:
 *                       type: number
 *                 description: 围栏坐标点数组
 *               farm_id:
 *                 type: integer
 *                 description: 关联农场ID
 *     responses:
 *       201:
 *         description: 创建成功
 *       400:
 *         description: 参数错误
 */
router.post('/', 
  requirePermission('smart_fence:create'),
  electronicFenceController.createFence
)

/**
 * @swagger
 * /api/electronic-fence/{id}:
 *   put:
 *     summary: 更新围栏
 *     tags: [ElectronicFence]
 *     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
 *               type:
 *                 type: string
 *                 enum: [collector, grazing, safety]
 *               description:
 *                 type: string
 *               coordinates:
 *                 type: array
 *                 items:
 *                   type: object
 *                   properties:
 *                     lng:
 *                       type: number
 *                     lat:
 *                       type: number
 *               grazing_status:
 *                 type: string
 *                 enum: [grazing, not_grazing]
 *     responses:
 *       200:
 *         description: 更新成功
 *       404:
 *         description: 围栏不存在
 */
router.put('/:id', 
  requirePermission('smart_fence:update'),
  electronicFenceController.updateFence
)

/**
 * @swagger
 * /api/electronic-fence/{id}:
 *   delete:
 *     summary: 删除围栏
 *     tags: [ElectronicFence]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 围栏ID
 *     responses:
 *       200:
 *         description: 删除成功
 *       404:
 *         description: 围栏不存在
 */
router.delete('/:id', 
  requirePermission('smart_fence:delete'),
  electronicFenceController.deleteFence
)

/**
 * @swagger
 * /api/electronic-fence/{id}/stats:
 *   put:
 *     summary: 更新围栏统计信息
 *     tags: [ElectronicFence]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 围栏ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               inside_count:
 *                 type: integer
 *                 description: 安全区域内动物数量
 *               outside_count:
 *                 type: integer
 *                 description: 安全区域外动物数量
 *     responses:
 *       200:
 *         description: 更新成功
 *       404:
 *         description: 围栏不存在
 */
router.put('/:id/stats', 
  requirePermission('smart_fence:update'),
  electronicFenceController.updateFenceStats
)

/**
 * @swagger
 * /api/electronic-fence/{id}/check-point:
 *   get:
 *     summary: 检查点是否在围栏内
 *     tags: [ElectronicFence]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 围栏ID
 *       - in: query
 *         name: lng
 *         required: true
 *         schema:
 *           type: number
 *         description: 经度
 *       - in: query
 *         name: lat
 *         required: true
 *         schema:
 *           type: number
 *         description: 纬度
 *     responses:
 *       200:
 *         description: 检查完成
 *       400:
 *         description: 参数错误
 *       404:
 *         description: 围栏不存在
 */
router.get('/:id/check-point', 
  requirePermission('smart_fence:view'),
  electronicFenceController.checkPointInFence
)

/**
 * @swagger
 * /api/electronic-fence/stats/overview:
 *   get:
 *     summary: 获取围栏统计概览
 *     tags: [ElectronicFence]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: object
 *                   properties:
 *                     totalFences:
 *                       type: integer
 *                     totalInside:
 *                       type: integer
 *                     totalOutside:
 *                       type: integer
 *                     byType:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           type:
 *                             type: string
 *                           count:
 *                             type: integer
 *                           total_inside:
 *                             type: integer
 *                           total_outside:
 *                             type: integer
 */
router.get('/stats/overview', 
  requirePermission('smart_fence:view'),
  electronicFenceController.getFenceStats
)

module.exports = router
