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

/**
 * @swagger
 * /api/devices:
 *   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: device_type
 *         schema:
 *           type: string
 *         description: 设备类型
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *           enum: [normal, maintenance, fault, offline]
 *         description: 设备状态
 *       - in: query
 *         name: farm_id
 *         schema:
 *           type: integer
 *         description: 养殖场ID
 *       - in: query
 *         name: pen_id
 *         schema:
 *           type: integer
 *         description: 栏舍ID
 *       - 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:
 *                     devices:
 *                       type: array
 *                       items:
 *                         $ref: '#/components/schemas/Device'
 *                     pagination:
 *                       $ref: '#/components/schemas/Pagination'
 */
router.get('/', jwtAuth, deviceController.getDeviceList);

/**
 * @swagger
 * /api/devices/stats:
 *   get:
 *     tags:
 *       - 设备管理
 *     summary: 获取设备统计信息
 *     description: 获取设备的统计数据，包括总数、按状态分类、按类型分类等
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - 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: object
 *                   properties:
 *                     total_devices:
 *                       type: integer
 *                       description: 总设备数
 *                     devices_by_status:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           status:
 *                             type: string
 *                           count:
 *                             type: integer
 *                     devices_by_type:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           device_type:
 *                             type: string
 *                           count:
 *                             type: integer
 */
router.get('/stats', jwtAuth, deviceController.getDeviceStats);

/**
 * @swagger
 * /api/devices/types:
 *   get:
 *     tags:
 *       - 设备管理
 *     summary: 获取设备类型列表
 *     description: 获取系统中所有的设备类型
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: array
 *                   items:
 *                     type: string
 */
router.get('/types', jwtAuth, deviceController.getDeviceTypes);

/**
 * @swagger
 * /api/devices/{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:
 *                   type: object
 *                   properties:
 *                     device:
 *                       $ref: '#/components/schemas/Device'
 *                     alert_stats:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           alert_level:
 *                             type: string
 *                           count:
 *                             type: integer
 *                     recent_alerts:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           id:
 *                             type: integer
 *                           alert_type:
 *                             type: string
 *                           alert_level:
 *                             type: string
 *                           alert_title:
 *                             type: string
 *                           alert_time:
 *                             type: string
 *                             format: date-time
 *                           status:
 *                             type: string
 *       404:
 *         description: 设备不存在
 */
router.get('/:id', jwtAuth, deviceController.getDeviceDetail);

/**
 * @swagger
 * /api/devices:
 *   post:
 *     tags:
 *       - 设备管理
 *     summary: 创建设备
 *     description: 创建新的设备记录
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - device_code
 *               - device_name
 *               - device_type
 *             properties:
 *               device_code:
 *                 type: string
 *                 description: 设备编号
 *               device_name:
 *                 type: string
 *                 description: 设备名称
 *               device_type:
 *                 type: string
 *                 description: 设备类型
 *               device_model:
 *                 type: string
 *                 description: 设备型号
 *               manufacturer:
 *                 type: string
 *                 description: 制造商
 *               installation_location:
 *                 type: string
 *                 description: 安装位置
 *               installation_date:
 *                 type: string
 *                 format: date
 *                 description: 安装日期
 *               farm_id:
 *                 type: integer
 *                 description: 养殖场ID
 *               pen_id:
 *                 type: integer
 *                 description: 栏舍ID
 *               status:
 *                 type: string
 *                 enum: [normal, maintenance, fault, offline]
 *                 default: normal
 *                 description: 设备状态
 *     responses:
 *       200:
 *         description: 创建成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 message:
 *                   type: string
 *                 data:
 *                   $ref: '#/components/schemas/Device'
 *       400:
 *         description: 设备编号已存在
 */
router.post('/', jwtAuth, deviceController.createDevice);

/**
 * @swagger
 * /api/devices/{id}:
 *   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
 *             properties:
 *               device_code:
 *                 type: string
 *                 description: 设备编号
 *               device_name:
 *                 type: string
 *                 description: 设备名称
 *               device_type:
 *                 type: string
 *                 description: 设备类型
 *               device_model:
 *                 type: string
 *                 description: 设备型号
 *               manufacturer:
 *                 type: string
 *                 description: 制造商
 *               installation_location:
 *                 type: string
 *                 description: 安装位置
 *               installation_date:
 *                 type: string
 *                 format: date
 *                 description: 安装日期
 *               farm_id:
 *                 type: integer
 *                 description: 养殖场ID
 *               pen_id:
 *                 type: integer
 *                 description: 栏舍ID
 *               status:
 *                 type: string
 *                 enum: [normal, maintenance, fault, offline]
 *                 description: 设备状态
 *     responses:
 *       200:
 *         description: 更新成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 message:
 *                   type: string
 *                 data:
 *                   $ref: '#/components/schemas/Device'
 *       404:
 *         description: 设备不存在
 *       400:
 *         description: 设备编号已存在
 */
router.put('/:id', jwtAuth, deviceController.updateDevice);

/**
 * @swagger
 * /api/devices/{id}:
 *   delete:
 *     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: 设备不存在
 *       400:
 *         description: 该设备存在预警记录，无法删除
 */
router.delete('/:id', jwtAuth, deviceController.deleteDevice);

/**
 * @swagger
 * components:
 *   schemas:
 *     Device:
 *       type: object
 *       properties:
 *         id:
 *           type: integer
 *           description: 设备ID
 *         device_code:
 *           type: string
 *           description: 设备编号
 *         device_name:
 *           type: string
 *           description: 设备名称
 *         device_type:
 *           type: string
 *           description: 设备类型
 *         device_model:
 *           type: string
 *           description: 设备型号
 *         manufacturer:
 *           type: string
 *           description: 制造商
 *         installation_location:
 *           type: string
 *           description: 安装位置
 *         installation_date:
 *           type: string
 *           format: date
 *           description: 安装日期
 *         status:
 *           type: string
 *           enum: [normal, maintenance, fault, offline]
 *           description: 设备状态
 *         farm_id:
 *           type: integer
 *           description: 养殖场ID
 *         pen_id:
 *           type: integer
 *           description: 栏舍ID
 *         created_by:
 *           type: integer
 *           description: 创建人ID
 *         updated_by:
 *           type: integer
 *           description: 更新人ID
 *         created_at:
 *           type: string
 *           format: date-time
 *           description: 创建时间
 *         updated_at:
 *           type: string
 *           format: date-time
 *           description: 更新时间
 *         creator:
 *           $ref: '#/components/schemas/User'
 */

module.exports = router;