/**
 * 电子围栏坐标点路由
 * 处理围栏坐标点的API请求
 */

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

const router = express.Router();

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

/**
 * @swagger
 * tags:
 *   name: ElectronicFencePoints
 *   description: 电子围栏坐标点管理
 */

/**
 * @swagger
 * /api/electronic-fence-points/fence/{fenceId}:
 *   get:
 *     summary: 获取围栏的所有坐标点
 *     tags: [ElectronicFencePoints]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: fenceId
 *         required: true
 *         schema:
 *           type: integer
 *         description: 围栏ID
 *       - in: query
 *         name: point_type
 *         schema:
 *           type: string
 *           enum: [corner, control, marker]
 *         description: 坐标点类型
 *       - in: query
 *         name: is_active
 *         schema:
 *           type: boolean
 *         description: 是否激活
 *     responses:
 *       200:
 *         description: 获取成功
 *       404:
 *         description: 围栏不存在
 *       500:
 *         description: 服务器错误
 */
router.get('/fence/:fenceId', 
  requirePermission('smart_fence:view'),
  electronicFencePointController.getFencePoints
);

/**
 * @swagger
 * /api/electronic-fence-points/{id}:
 *   get:
 *     summary: 获取坐标点详情
 *     tags: [ElectronicFencePoints]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 坐标点ID
 *     responses:
 *       200:
 *         description: 获取成功
 *       404:
 *         description: 坐标点不存在
 *       500:
 *         description: 服务器错误
 */
router.get('/:id', 
  requirePermission('smart_fence:view'),
  electronicFencePointController.getPointById
);

/**
 * @swagger
 * /api/electronic-fence-points:
 *   post:
 *     summary: 创建坐标点
 *     tags: [ElectronicFencePoints]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - fence_id
 *               - point_order
 *               - longitude
 *               - latitude
 *             properties:
 *               fence_id:
 *                 type: integer
 *                 description: 围栏ID
 *               point_order:
 *                 type: integer
 *                 description: 坐标点顺序
 *               longitude:
 *                 type: number
 *                 description: 经度
 *               latitude:
 *                 type: number
 *                 description: 纬度
 *               point_type:
 *                 type: string
 *                 enum: [corner, control, marker]
 *                 default: corner
 *                 description: 坐标点类型
 *               description:
 *                 type: string
 *                 description: 坐标点描述
 *     responses:
 *       201:
 *         description: 创建成功
 *       400:
 *         description: 请求参数错误
 *       404:
 *         description: 围栏不存在
 *       500:
 *         description: 服务器错误
 */
router.post('/', 
  requirePermission('smart_fence:create'),
  electronicFencePointController.createPoint
);

/**
 * @swagger
 * /api/electronic-fence-points/batch:
 *   post:
 *     summary: 批量创建坐标点
 *     tags: [ElectronicFencePoints]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - fence_id
 *               - points
 *             properties:
 *               fence_id:
 *                 type: integer
 *                 description: 围栏ID
 *               points:
 *                 type: array
 *                 items:
 *                   type: object
 *                   properties:
 *                     lng:
 *                       type: number
 *                       description: 经度
 *                     lat:
 *                       type: number
 *                       description: 纬度
 *                     type:
 *                       type: string
 *                       enum: [corner, control, marker]
 *                       description: 坐标点类型
 *                     description:
 *                       type: string
 *                       description: 坐标点描述
 *     responses:
 *       201:
 *         description: 创建成功
 *       400:
 *         description: 请求参数错误
 *       404:
 *         description: 围栏不存在
 *       500:
 *         description: 服务器错误
 */
router.post('/batch', 
  requirePermission('smart_fence:create'),
  electronicFencePointController.createPoints
);

/**
 * @swagger
 * /api/electronic-fence-points/{id}:
 *   put:
 *     summary: 更新坐标点
 *     tags: [ElectronicFencePoints]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 坐标点ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               point_order:
 *                 type: integer
 *                 description: 坐标点顺序
 *               longitude:
 *                 type: number
 *                 description: 经度
 *               latitude:
 *                 type: number
 *                 description: 纬度
 *               point_type:
 *                 type: string
 *                 enum: [corner, control, marker]
 *                 description: 坐标点类型
 *               description:
 *                 type: string
 *                 description: 坐标点描述
 *               is_active:
 *                 type: boolean
 *                 description: 是否激活
 *     responses:
 *       200:
 *         description: 更新成功
 *       404:
 *         description: 坐标点不存在
 *       500:
 *         description: 服务器错误
 */
router.put('/:id', 
  requirePermission('smart_fence:update'),
  electronicFencePointController.updatePoint
);

/**
 * @swagger
 * /api/electronic-fence-points/fence/{fenceId}:
 *   put:
 *     summary: 更新围栏的所有坐标点
 *     tags: [ElectronicFencePoints]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: fenceId
 *         required: true
 *         schema:
 *           type: integer
 *         description: 围栏ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - points
 *             properties:
 *               points:
 *                 type: array
 *                 items:
 *                   type: object
 *                   properties:
 *                     lng:
 *                       type: number
 *                       description: 经度
 *                     lat:
 *                       type: number
 *                       description: 纬度
 *                     type:
 *                       type: string
 *                       enum: [corner, control, marker]
 *                       description: 坐标点类型
 *                     description:
 *                       type: string
 *                       description: 坐标点描述
 *     responses:
 *       200:
 *         description: 更新成功
 *       400:
 *         description: 请求参数错误
 *       404:
 *         description: 围栏不存在
 *       500:
 *         description: 服务器错误
 */
router.put('/fence/:fenceId', 
  requirePermission('smart_fence:update'),
  electronicFencePointController.updateFencePoints
);

/**
 * @swagger
 * /api/electronic-fence-points/{id}:
 *   delete:
 *     summary: 删除坐标点
 *     tags: [ElectronicFencePoints]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 坐标点ID
 *     responses:
 *       200:
 *         description: 删除成功
 *       404:
 *         description: 坐标点不存在
 *       500:
 *         description: 服务器错误
 */
router.delete('/:id', 
  requirePermission('smart_fence:delete'),
  electronicFencePointController.deletePoint
);

/**
 * @swagger
 * /api/electronic-fence-points/fence/{fenceId}:
 *   delete:
 *     summary: 删除围栏的所有坐标点
 *     tags: [ElectronicFencePoints]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: fenceId
 *         required: true
 *         schema:
 *           type: integer
 *         description: 围栏ID
 *     responses:
 *       200:
 *         description: 删除成功
 *       500:
 *         description: 服务器错误
 */
router.delete('/fence/:fenceId', 
  requirePermission('smart_fence:delete'),
  electronicFencePointController.deleteFencePoints
);

/**
 * @swagger
 * /api/electronic-fence-points/fence/{fenceId}/bounds:
 *   get:
 *     summary: 获取围栏边界框
 *     tags: [ElectronicFencePoints]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: fenceId
 *         required: true
 *         schema:
 *           type: integer
 *         description: 围栏ID
 *     responses:
 *       200:
 *         description: 获取成功
 *       404:
 *         description: 围栏没有坐标点
 *       500:
 *         description: 服务器错误
 */
router.get('/fence/:fenceId/bounds', 
  requirePermission('smart_fence:view'),
  electronicFencePointController.getFenceBounds
);

/**
 * @swagger
 * /api/electronic-fence-points/search:
 *   get:
 *     summary: 搜索坐标点
 *     tags: [ElectronicFencePoints]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: fence_id
 *         schema:
 *           type: integer
 *         description: 围栏ID
 *       - in: query
 *         name: point_type
 *         schema:
 *           type: string
 *           enum: [corner, control, marker]
 *         description: 坐标点类型
 *       - in: query
 *         name: longitude_min
 *         schema:
 *           type: number
 *         description: 最小经度
 *       - in: query
 *         name: longitude_max
 *         schema:
 *           type: number
 *         description: 最大经度
 *       - in: query
 *         name: latitude_min
 *         schema:
 *           type: number
 *         description: 最小纬度
 *       - in: query
 *         name: latitude_max
 *         schema:
 *           type: number
 *         description: 最大纬度
 *       - in: query
 *         name: description
 *         schema:
 *           type: string
 *         description: 描述关键词
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           default: 10
 *         description: 每页数量
 *     responses:
 *       200:
 *         description: 搜索成功
 *       500:
 *         description: 服务器错误
 */
router.get('/search', 
  requirePermission('smart_fence:view'),
  electronicFencePointController.searchPoints
);

module.exports = router;
