const express = require('express');
const router = express.Router();
const deviceAlertController = require('../controllers/deviceAlertController');
const { jwtAuth } = require('../middleware/auth');

/**
 * @swagger
 * /api/device-alerts/stats:
 *   get:
 *     tags:
 *       - 设备预警
 *     summary: 获取预警统计信息
 *     description: 获取设备预警的统计数据，包括总数、按级别分类、按状态分类等
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: farm_id
 *         schema:
 *           type: integer
 *         description: 养殖场ID
 *       - in: query
 *         name: start_date
 *         schema:
 *           type: string
 *           format: date
 *         description: 开始日期
 *       - in: query
 *         name: end_date
 *         schema:
 *           type: string
 *           format: date
 *         description: 结束日期
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: object
 *                   properties:
 *                     total_alerts:
 *                       type: integer
 *                       description: 总预警数
 *                     unread_alerts:
 *                       type: integer
 *                       description: 未读预警数
 *                     today_alerts:
 *                       type: integer
 *                       description: 今日新增预警数
 *                     alerts_by_level:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           alert_level:
 *                             type: string
 *                           count:
 *                             type: integer
 *                     alerts_by_status:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           status:
 *                             type: string
 *                           count:
 *                             type: integer
 *                     alerts_by_type:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           alert_type:
 *                             type: string
 *                           count:
 *                             type: integer
 */
router.get('/stats', jwtAuth, deviceAlertController.getAlertStats);

/**
 * @swagger
 * /api/device-alerts/export:
 *   get:
 *     tags:
 *       - 设备预警
 *     summary: 导出设备预警列表到Excel
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: alert_level
 *         schema:
 *           type: string
 *         description: 预警级别
 *       - in: query
 *         name: alert_type
 *         schema:
 *           type: string
 *         description: 预警类型
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *         description: 处理状态
 *       - in: query
 *         name: is_read
 *         schema:
 *           type: boolean
 *         description: 是否已读
 *     responses:
 *       200:
 *         description: 导出成功
 *         content:
 *           application/vnd.openxmlformats-officedocument.spreadsheetml.sheet:
 *             schema:
 *               type: string
 *               format: binary
 */
router.get('/export', jwtAuth, deviceAlertController.exportToExcel);

/**
 * @swagger
 * /api/device-alerts:
 *   get:
 *     tags:
 *       - 设备预警
 *     summary: 获取预警列表
 *     description: 分页获取设备预警列表，支持多种筛选条件
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           default: 20
 *         description: 每页数量
 *       - in: query
 *         name: alert_level
 *         schema:
 *           type: string
 *           enum: [low, medium, high, critical]
 *         description: 预警级别
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *           enum: [pending, processing, resolved, ignored]
 *         description: 处理状态
 *       - in: query
 *         name: alert_type
 *         schema:
 *           type: string
 *         description: 预警类型
 *       - in: query
 *         name: farm_id
 *         schema:
 *           type: integer
 *         description: 养殖场ID
 *       - in: query
 *         name: start_date
 *         schema:
 *           type: string
 *           format: date
 *         description: 开始日期
 *       - in: query
 *         name: end_date
 *         schema:
 *           type: string
 *           format: date
 *         description: 结束日期
 *       - in: query
 *         name: is_read
 *         schema:
 *           type: boolean
 *         description: 是否已读
 *       - in: query
 *         name: device_code
 *         schema:
 *           type: string
 *         description: 设备编号
 *       - in: query
 *         name: keyword
 *         schema:
 *           type: string
 *         description: 关键词搜索（标题或内容）
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: object
 *                   properties:
 *                     alerts:
 *                       type: array
 *                       items:
 *                         $ref: '#/components/schemas/DeviceAlert'
 *                     pagination:
 *                       $ref: '#/components/schemas/Pagination'
 */
router.get('/', jwtAuth, deviceAlertController.getAlertList);

/**
 * @swagger
 * /api/device-alerts/{id}:
 *   get:
 *     tags:
 *       - 设备预警
 *     summary: 获取预警详情
 *     description: 根据ID获取设备预警的详细信息
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 预警ID
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   $ref: '#/components/schemas/DeviceAlert'
 *       404:
 *         description: 预警信息不存在
 */
router.get('/:id', jwtAuth, deviceAlertController.getAlertDetail);

/**
 * @swagger
 * /api/device-alerts/{id}/read:
 *   put:
 *     tags:
 *       - 设备预警
 *     summary: 标记预警为已读
 *     description: 将指定的预警标记为已读状态
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 预警ID
 *     responses:
 *       200:
 *         description: 标记成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 message:
 *                   type: string
 *       404:
 *         description: 预警信息不存在
 */
router.put('/:id/read', jwtAuth, deviceAlertController.markAsRead);

/**
 * @swagger
 * /api/device-alerts/batch/read:
 *   put:
 *     tags:
 *       - 设备预警
 *     summary: 批量标记预警为已读
 *     description: 批量将多个预警标记为已读状态
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - alert_ids
 *             properties:
 *               alert_ids:
 *                 type: array
 *                 items:
 *                   type: integer
 *                 description: 预警ID列表
 *     responses:
 *       200:
 *         description: 批量标记成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 message:
 *                   type: string
 */
router.put('/batch/read', jwtAuth, deviceAlertController.batchMarkAsRead);

/**
 * @swagger
 * /api/device-alerts/{id}/handle:
 *   put:
 *     tags:
 *       - 设备预警
 *     summary: 处理预警
 *     description: 处理指定的设备预警，更新处理状态和备注
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 预警ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - status
 *             properties:
 *               status:
 *                 type: string
 *                 enum: [processing, resolved, ignored]
 *                 description: 处理状态
 *               handle_remark:
 *                 type: string
 *                 description: 处理备注
 *     responses:
 *       200:
 *         description: 处理成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 message:
 *                   type: string
 *       404:
 *         description: 预警信息不存在
 */
router.put('/:id/handle', jwtAuth, deviceAlertController.handleAlert);

/**
 * @swagger
 * /api/device-alerts:
 *   post:
 *     tags:
 *       - 设备预警
 *     summary: 创建预警
 *     description: 创建新的设备预警（系统内部使用）
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - device_id
 *               - alert_type
 *               - alert_level
 *               - alert_title
 *               - alert_content
 *             properties:
 *               device_id:
 *                 type: integer
 *                 description: 设备ID
 *               alert_type:
 *                 type: string
 *                 description: 预警类型
 *               alert_level:
 *                 type: string
 *                 enum: [low, medium, high, critical]
 *                 description: 预警级别
 *               alert_title:
 *                 type: string
 *                 description: 预警标题
 *               alert_content:
 *                 type: string
 *                 description: 预警内容
 *               farm_id:
 *                 type: integer
 *                 description: 养殖场ID
 *               pen_id:
 *                 type: integer
 *                 description: 栏舍ID
 *     responses:
 *       200:
 *         description: 创建成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 message:
 *                   type: string
 *                 data:
 *                   $ref: '#/components/schemas/DeviceAlert'
 */
router.post('/', jwtAuth, deviceAlertController.createAlert);

/**
 * @swagger
 * components:
 *   schemas:
 *     DeviceAlert:
 *       type: object
 *       properties:
 *         id:
 *           type: integer
 *           description: 预警ID
 *         device_id:
 *           type: integer
 *           description: 设备ID
 *         alert_type:
 *           type: string
 *           description: 预警类型
 *         alert_level:
 *           type: string
 *           enum: [low, medium, high, critical]
 *           description: 预警级别
 *         alert_title:
 *           type: string
 *           description: 预警标题
 *         alert_content:
 *           type: string
 *           description: 预警内容
 *         alert_time:
 *           type: string
 *           format: date-time
 *           description: 预警时间
 *         status:
 *           type: string
 *           enum: [pending, processing, resolved, ignored]
 *           description: 处理状态
 *         handler_id:
 *           type: integer
 *           description: 处理人ID
 *         handle_time:
 *           type: string
 *           format: date-time
 *           description: 处理时间
 *         handle_remark:
 *           type: string
 *           description: 处理备注
 *         farm_id:
 *           type: integer
 *           description: 养殖场ID
 *         pen_id:
 *           type: integer
 *           description: 栏舍ID
 *         is_read:
 *           type: boolean
 *           description: 是否已读
 *         read_time:
 *           type: string
 *           format: date-time
 *           description: 阅读时间
 *         created_at:
 *           type: string
 *           format: date-time
 *           description: 创建时间
 *         updated_at:
 *           type: string
 *           format: date-time
 *           description: 更新时间
 *         device:
 *           $ref: '#/components/schemas/Device'
 *         handler:
 *           $ref: '#/components/schemas/User'
 *     Pagination:
 *       type: object
 *       properties:
 *         current_page:
 *           type: integer
 *           description: 当前页码
 *         per_page:
 *           type: integer
 *           description: 每页数量
 *         total:
 *           type: integer
 *           description: 总记录数
 *         total_pages:
 *           type: integer
 *           description: 总页数
 */

module.exports = router;