/**
 * 统计数据路由
 * @file stats.js
 * @description 定义统计数据相关的API路由
 */

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

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

// 公开获取仪表盘统计数据
publicRoutes.get('/dashboard', statsController.getDashboardStats);

// 公开获取监控数据
publicRoutes.get('/monitoring', statsController.getMonitorData);

// 公开获取月度数据趋势
publicRoutes.get('/monthly-trends', statsController.getMonthlyTrends);

// 公开获取农场总数统计（实时数据库查询）
publicRoutes.get('/farm-count', statsController.getFarmCount);

// 公开获取动物总数统计（实时数据库查询）
publicRoutes.get('/animal-count', statsController.getAnimalCount);

/**
 * @swagger
 * tags:
 *   name: Statistics
 *   description: 统计数据API
 */

/**
 * @swagger
 * /api/stats/dashboard:
 *   get:
 *     summary: 获取仪表盘统计数据
 *     tags: [Statistics]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 成功获取仪表盘统计数据
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   type: object
 *                   properties:
 *                     farmCount:
 *                       type: integer
 *                       example: 12
 *                     animalCount:
 *                       type: integer
 *                       example: 5000
 *                     deviceCount:
 *                       type: integer
 *                       example: 150
 *                     alertCount:
 *                       type: integer
 *                       example: 25
 *                     deviceOnlineRate:
 *                       type: number
 *                       format: float
 *                       example: 0.95
 *                     alertsByLevel:
 *                       type: object
 *                       properties:
 *                         low:
 *                           type: integer
 *                           example: 5
 *                         medium:
 *                           type: integer
 *                           example: 10
 *                         high:
 *                           type: integer
 *                           example: 8
 *                         critical:
 *                           type: integer
 *                           example: 2
 *       401:
 *         description: 未授权
 *       500:
 *         description: 服务器错误
 */
router.get('/dashboard', verifyToken, statsController.getDashboardStats);

/**
 * @swagger
 * /api/stats/farms:
 *   get:
 *     summary: 获取养殖场统计数据
 *     tags: [Statistics]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 成功获取养殖场统计数据
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   type: object
 *                   properties:
 *                     totalFarms:
 *                       type: integer
 *                       example: 12
 *                     farmsByType:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           type:
 *                             type: string
 *                             example: 猪场
 *                           count:
 *                             type: integer
 *                             example: 5
 *                     farmsByStatus:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           status:
 *                             type: string
 *                             example: active
 *                           count:
 *                             type: integer
 *                             example: 10
 *       401:
 *         description: 未授权
 *       500:
 *         description: 服务器错误
 */
router.get('/farms', verifyToken, statsController.getFarmStats);

/**
 * @swagger
 * /api/stats/animals:
 *   get:
 *     summary: 获取动物统计数据
 *     tags: [Statistics]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 成功获取动物统计数据
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   type: object
 *                   properties:
 *                     totalAnimals:
 *                       type: integer
 *                       example: 5000
 *                     animalsByType:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           type:
 *                             type: string
 *                             example: 猪
 *                           count:
 *                             type: integer
 *                             example: 3000
 *                     animalsByHealth:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           health_status:
 *                             type: string
 *                             example: healthy
 *                           count:
 *                             type: integer
 *                             example: 4500
 *       401:
 *         description: 未授权
 *       500:
 *         description: 服务器错误
 */
router.get('/animals', verifyToken, statsController.getAnimalStats);

/**
 * @swagger
 * /api/stats/devices:
 *   get:
 *     summary: 获取设备统计数据
 *     tags: [Statistics]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 成功获取设备统计数据
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   type: object
 *                   properties:
 *                     totalDevices:
 *                       type: integer
 *                       example: 150
 *                     devicesByType:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           type:
 *                             type: string
 *                             example: 温度传感器
 *                           count:
 *                             type: integer
 *                             example: 50
 *                     devicesByStatus:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           status:
 *                             type: string
 *                             example: online
 *                           count:
 *                             type: integer
 *                             example: 140
 *                     onlineRate:
 *                       type: number
 *                       format: float
 *                       example: 0.95
 *       401:
 *         description: 未授权
 *       500:
 *         description: 服务器错误
 */
router.get('/devices', verifyToken, statsController.getDeviceStats);

/**
 * @swagger
 * /api/stats/alerts:
 *   get:
 *     summary: 获取预警统计数据
 *     tags: [Statistics]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 成功获取预警统计数据
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   type: object
 *                   properties:
 *                     totalAlerts:
 *                       type: integer
 *                       example: 25
 *                     alertsByType:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           type:
 *                             type: string
 *                             example: 温度异常
 *                           count:
 *                             type: integer
 *                             example: 10
 *                     alertsByLevel:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           level:
 *                             type: string
 *                             example: high
 *                           count:
 *                             type: integer
 *                             example: 8
 *                     alertsByStatus:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           status:
 *                             type: string
 *                             example: active
 *                           count:
 *                             type: integer
 *                             example: 15
 *                     recentAlerts:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           id:
 *                             type: integer
 *                             example: 1
 *                           type:
 *                             type: string
 *                             example: 温度异常
 *                           level:
 *                             type: string
 *                             example: high
 *                           message:
 *                             type: string
 *                             example: 温度超过阈值
 *                           createdAt:
 *                             type: string
 *                             format: date-time
 *                             example: 2023-01-01T12:00:00Z
 *       401:
 *         description: 未授权
 *       500:
 *         description: 服务器错误
 */
router.get('/alerts', verifyToken, statsController.getAlertStats);

/**
 * @swagger
 * /api/stats/monitoring:
 *   get:
 *     summary: 获取实时监控数据
 *     tags: [Statistics]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 成功获取实时监控数据
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   type: object
 *                   properties:
 *                     deviceStatus:
 *                       type: object
 *                       properties:
 *                         online:
 *                           type: integer
 *                           example: 140
 *                         offline:
 *                           type: integer
 *                           example: 10
 *                         maintenance:
 *                           type: integer
 *                           example: 5
 *                     recentAlerts:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           id:
 *                             type: integer
 *                             example: 1
 *                           type:
 *                             type: string
 *                             example: 温度异常
 *                           level:
 *                             type: string
 *                             example: high
 *                           message:
 *                             type: string
 *                             example: 温度超过阈值
 *                           createdAt:
 *                             type: string
 *                             format: date-time
 *                             example: 2023-01-01T12:00:00Z
 *                     environmentalData:
 *                       type: object
 *                       properties:
 *                         temperature:
 *                           type: array
 *                           items:
 *                             type: object
 *                             properties:
 *                               timestamp:
 *                                 type: string
 *                                 format: date-time
 *                                 example: 2023-01-01T12:00:00Z
 *                               value:
 *                                 type: number
 *                                 format: float
 *                                 example: 25.5
 *                         humidity:
 *                           type: array
 *                           items:
 *                             type: object
 *                             properties:
 *                               timestamp:
 *                                 type: string
 *                                 format: date-time
 *                                 example: 2023-01-01T12:00:00Z
 *                               value:
 *                                 type: number
 *                                 format: float
 *                                 example: 60.2
 *       401:
 *         description: 未授权
 *       500:
 *         description: 服务器错误
 */
router.get('/monitoring', verifyToken, statsController.getMonitorData);

/**
 * @swagger
 * /api/stats/monthly-trends:
 *   get:
 *     summary: 获取月度数据趋势
 *     tags: [Statistics]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 成功获取月度数据趋势
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   type: object
 *                   properties:
 *                     xAxis:
 *                       type: array
 *                       items:
 *                         type: string
 *                       description: 月份标签
 *                     series:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           name:
 *                             type: string
 *                           type:
 *                             type: string
 *                           data:
 *                             type: array
 *                             items:
 *                               type: number
 *                           itemStyle:
 *                             type: object
 *                           areaStyle:
 *                             type: object
 *       500:
 *         description: 服务器错误
 */
router.get('/monthly-trends', verifyToken, statsController.getMonthlyTrends);

module.exports = router;