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

/**
 * @swagger
 * tags:
 *   name: Dashboard
 *   description: 仪表板相关接口
 */

/**
 * @swagger
 * /api/dashboard/stats:
 *   get:
 *     summary: 获取仪表板统计数据
 *     tags: [Dashboard]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 成功获取统计数据
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 status:
 *                   type: string
 *                   example: success
 *                 data:
 *                   type: object
 *                   properties:
 *                     totalApplications:
 *                       type: integer
 *                       description: 总申请数
 *                     todayApplications:
 *                       type: integer
 *                       description: 今日申请数
 *                     monthApplications:
 *                       type: integer
 *                       description: 本月申请数
 *                     applicationGrowthRate:
 *                       type: number
 *                       description: 申请增长率
 *                     totalPolicies:
 *                       type: integer
 *                       description: 总保单数
 *                     activePolicies:
 *                       type: integer
 *                       description: 有效保单数
 *                     policyGrowthRate:
 *                       type: number
 *                       description: 保单增长率
 *                     totalClaims:
 *                       type: integer
 *                       description: 总理赔数
 *                     pendingClaims:
 *                       type: integer
 *                       description: 待处理理赔数
 *                     claimProcessingRate:
 *                       type: string
 *                       description: 理赔处理率
 *                     totalUsers:
 *                       type: integer
 *                       description: 总用户数
 *                     quickStats:
 *                       type: object
 *                       properties:
 *                         newApplicationsToday:
 *                           type: integer
 *                         pendingReviews:
 *                           type: integer
 *                         activeUsers:
 *                           type: integer
 *                         systemAlerts:
 *                           type: integer
 *                 message:
 *                   type: string
 *                   example: 获取仪表板统计数据成功
 *                 timestamp:
 *                   type: string
 *                   format: date-time
 *       401:
 *         description: 未授权
 *       500:
 *         description: 服务器内部错误
 */
router.get('/stats', jwtAuth, checkPermission('dashboard', 'read'), dashboardController.getStats);

/**
 * @swagger
 * /api/dashboard/recent-activities:
 *   get:
 *     summary: 获取最近活动
 *     tags: [Dashboard]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           default: 20
 *         description: 返回记录数量限制
 *     responses:
 *       200:
 *         description: 成功获取最近活动
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 status:
 *                   type: string
 *                   example: success
 *                 data:
 *                   type: array
 *                   items:
 *                     type: object
 *                     properties:
 *                       id:
 *                         type: integer
 *                       type:
 *                         type: string
 *                         description: 活动类型
 *                       title:
 *                         type: string
 *                         description: 活动标题
 *                       description:
 *                         type: string
 *                         description: 活动描述
 *                       timestamp:
 *                         type: string
 *                         format: date-time
 *                       user:
 *                         type: string
 *                         description: 操作用户
 *                       level:
 *                         type: string
 *                         description: 日志级别
 *                 message:
 *                   type: string
 *                   example: 获取最近活动成功
 *                 timestamp:
 *                   type: string
 *                   format: date-time
 *       401:
 *         description: 未授权
 *       500:
 *         description: 服务器内部错误
 */
router.get('/recent-activities', jwtAuth, checkPermission('dashboard', 'read'), dashboardController.getRecentActivities);

/**
 * @swagger
 * /api/dashboard/chart-data:
 *   get:
 *     summary: 获取图表数据
 *     tags: [Dashboard]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: type
 *         schema:
 *           type: string
 *           enum: [applications, policies, claims, policy_status]
 *           default: applications
 *         description: 图表数据类型
 *       - in: query
 *         name: period
 *         schema:
 *           type: string
 *           enum: [7d, 30d, 90d]
 *           default: 7d
 *         description: 时间周期
 *     responses:
 *       200:
 *         description: 成功获取图表数据
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 status:
 *                   type: string
 *                   example: success
 *                 data:
 *                   type: array
 *                   items:
 *                     type: object
 *                     properties:
 *                       date:
 *                         type: string
 *                         format: date
 *                       value:
 *                         type: integer
 *                 message:
 *                   type: string
 *                   example: 获取图表数据成功
 *                 timestamp:
 *                   type: string
 *                   format: date-time
 *       401:
 *         description: 未授权
 *       500:
 *         description: 服务器内部错误
 */
router.get('/chart-data', jwtAuth, checkPermission('dashboard', 'read'), dashboardController.getChartData);

module.exports = router;