/**
 * 系统统计控制器
 * @module controllers/admin/systemStats
 */

/**
 * 获取系统统计数据
 * @function getSystemStats
 * @param {Object} req - Express请求对象
 * @param {Object} res - Express响应对象
 * @param {Function} next - Express中间件next函数
 */
exports.getSystemStats = async (req, res, next) => {
  try {
    // 这里应该从数据库获取真实数据
    const stats = {
      userCount: 1000,
      merchantCount: 50,
      travelCount: 200,
      animalCount: 300,
      orderCount: 1500,
      todayUserCount: 20,
      todayOrderCount: 35
    };
    
    res.json({
      success: true,
      code: 200,
      message: '获取成功',
      data: stats
    });
  } catch (error) {
    next(error);
  }
};

/**
 * 获取用户统计数据
 * @function getUserStats
 * @param {Object} req - Express请求对象
 * @param {Object} res - Express响应对象
 * @param {Function} next - Express中间件next函数
 */
exports.getUserStats = async (req, res, next) => {
  try {
    // 这里应该从数据库获取真实数据
    const userStats = {
      total: 1000,
      byType: [
        { user_type: '普通用户', count: 900 },
        { user_type: '商家', count: 50 },
        { user_type: '管理员', count: 50 }
      ],
      byDate: [
        { date: '2023-04-01', count: 10 },
        { date: '2023-04-02', count: 15 },
        { date: '2023-04-03', count: 20 }
      ]
    };
    
    res.json({
      success: true,
      code: 200,
      message: '获取成功',
      data: userStats
    });
  } catch (error) {
    next(error);
  }
};

/**
 * 获取订单统计数据
 * @function getOrderStats
 * @param {Object} req - Express请求对象
 * @param {Object} res - Express响应对象
 * @param {Function} next - Express中间件next函数
 */
exports.getOrderStats = async (req, res, next) => {
  try {
    // 这里应该从数据库获取真实数据
    const orderStats = {
      total: 1500,
      totalAmount: 100000,
      byStatus: [
        { status: '待支付', count: 100 },
        { status: '已支付', count: 1200 },
        { status: '已取消', count: 200 }
      ],
      byDate: [
        { date: '2023-04-01', count: 50, amount: 2500 },
        { date: '2023-04-02', count: 75, amount: 4000 },
        { date: '2023-04-03', count: 100, amount: 6000 }
      ]
    };
    
    res.json({
      success: true,
      code: 200,
      message: '获取成功',
      data: orderStats
    });
  } catch (error) {
    next(error);
  }
};

/**
 * 获取系统信息
 * @function getSystemInfo
 * @param {Object} req - Express请求对象
 * @param {Object} res - Express响应对象
 * @param {Function} next - Express中间件next函数
 */
exports.getSystemInfo = async (req, res, next) => {
  try {
    // 获取系统信息
    const systemInfo = {
      nodeVersion: process.version,
      platform: process.platform,
      arch: process.arch,
      uptime: Math.floor(process.uptime()),
      databaseVersion: 'MySQL 8.0.28' // 这里应该从数据库获取真实版本信息
    };
    
    res.json({
      success: true,
      code: 200,
      message: '获取成功',
      data: systemInfo
    });
  } catch (error) {
    next(error);
  }
};
// 管理员路由
const express = require('express');
const router = express.Router();
const adminController = require('../controllers/admin');
const systemStatsController = require('../controllers/admin/systemStats');
const { authenticateAdmin } = require('../middleware/auth');

// 引入子路由
const userManagementRoutes = require('./admin/userManagement');
const dataStatisticsRoutes = require('./admin/dataStatistics');
const animalManagementRoutes = require('./admin/animalManagement');
const fileManagementRoutes = require('./admin/fileManagement');

/**
 * @swagger
 * tags:
 *   name: Admin
 *   description: 管理员相关接口
 */

/**
 * @swagger
 * /admin/login:
 *   post:
 *     summary: 管理员登录
 *     tags: [Admin]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - username
 *               - password
 *             properties:
 *               username:
 *                 type: string
 *                 description: 用户名
 *               password:
 *                 type: string
 *                 description: 密码
 *     responses:
 *       200:
 *         description: 登录成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 code:
 *                   type: integer
 *                 message:
 *                   type: string
 *                 data:
 *                   type: object
 *                   properties:
 *                     admin:
 *                       $ref: '#/components/schemas/Admin'
 *                     token:
 *                       type: string
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 用户名或密码错误
 */
router.post('/login', adminController.login);

// 需要认证的接口
/**
 * @swagger
 * /admin/profile:
 *   get:
 *     summary: 获取管理员个人信息
 *     tags: [Admin]
 *     security:
 *       - BearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 code:
 *                   type: integer
 *                 message:
 *                   type: string
 *                 data:
 *                   type: object
 *                   properties:
 *                     admin:
 *                       $ref: '#/components/schemas/Admin'
 *       401:
 *         description: 未授权
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ErrorResponse'
 */
router.get('/profile', authenticateAdmin, adminController.getProfile);

/**
 * @swagger
 * /admin:
 *   get:
 *     summary: 获取管理员列表
 *     tags: [Admin]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           minimum: 1
 *         description: 页码
 *       - in: query
 *         name: pageSize
 *         schema:
 *           type: integer
 *           minimum: 1
 *           maximum: 100
 *         description: 每页数量
 *       - in: query
 *         name: username
 *         schema:
 *           type: string
 *         description: 用户名搜索
 *       - in: query
 *         name: role
 *         schema:
 *           type: string
 *         description: 角色筛选
 *       - in: query
 *         name: status
 *         schema:
 *           type: integer
 *         description: 状态筛选
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 code:
 *                   type: integer
 *                 message:
 *                   type: string
 *                 data:
 *                   type: object
 *                   properties:
 *                     admins:
 *                       type: array
 *                       items:
 *                         $ref: '#/components/schemas/Admin'
 *                     pagination:
 *                       $ref: '#/components/schemas/Pagination'
 *       401:
 *         description: 未授权
 */
router.get('/', adminController.getList);

/**
 * @swagger
 * /admin:
 *   post:
 *     summary: 创建管理员
 *     tags: [Admin]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - username
 *               - password
 *             properties:
 *               username:
 *                 type: string
 *                 description: 用户名
 *               password:
 *                 type: string
 *                 description: 密码
 *               email:
 *                 type: string
 *                 description: 邮箱
 *               nickname:
 *                 type: string
 *                 description: 昵称
 *               role:
 *                 type: string
 *                 description: 角色
 *     responses:
 *       201:
 *         description: 创建成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 code:
 *                   type: integer
 *                 message:
 *                   type: string
 *                 data:
 *                   type: object
 *                   properties:
 *                     admin:
 *                       $ref: '#/components/schemas/Admin'
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 *       409:
 *         description: 用户名已存在
 */
router.post('/', adminController.create);

/**
 * @swagger
 * /admin/{id}:
 *   put:
 *     summary: 更新管理员
 *     tags: [Admin]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 管理员ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               email:
 *                 type: string
 *                 description: 邮箱
 *               nickname:
 *                 type: string
 *                 description: 昵称
 *               role:
 *                 type: string
 *                 description: 角色
 *               status:
 *                 type: integer
 *                 description: 状态
 *     responses:
 *       200:
 *         description: 更新成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 code:
 *                   type: integer
 *                 message:
 *                   type: string
 *                 data:
 *                   type: object
 *                   properties:
 *                     admin:
 *                       $ref: '#/components/schemas/Admin'
 *       400:
 *         description: 不能修改自己的角色
 *       401:
 *         description: 未授权
 *       404:
 *         description: 管理员不存在
 */
router.put('/:id', adminController.update);

/**
 * @swagger
 * /admin/{id}:
 *   delete:
 *     summary: 删除管理员
 *     tags: [Admin]
 *     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
 *                 code:
 *                   type: integer
 *                 message:
 *                   type: string
 *       400:
 *         description: 不能删除自己
 *       401:
 *         description: 未授权
 *       404:
 *         description: 管理员不存在
 */
router.delete('/:id', adminController.delete);

/**
 * @swagger
 * /admin/system/stats:
 *   get:
 *     summary: 获取系统统计数据
 *     tags: [Admin]
 *     security:
 *       - BearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 code:
 *                   type: integer
 *                 message:
 *                   type: string
 *                 data:
 *                   type: object
 *                   properties:
 *                     userCount:
 *                       type: integer
 *                       description: 用户总数
 *                     merchantCount:
 *                       type: integer
 *                       description: 商家数量
 *                     travelCount:
 *                       type: integer
 *                       description: 旅行计划数
 *                     animalCount:
 *                       type: integer
 *                       description: 动物数量
 *                     orderCount:
 *                       type: integer
 *                       description: 订单总数
 *                     todayUserCount:
 *                       type: integer
 *                       description: 今日新增用户数
 *                     todayOrderCount:
 *                       type: integer
 *                       description: 今日新增订单数
 *       401:
 *         description: 未授权
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ErrorResponse'
 */
router.get('/system/stats', authenticateAdmin, systemStatsController.getSystemStats);

/**
 * @swagger
 * /admin/system/user-stats:
 *   get:
 *     summary: 获取用户统计数据
 *     tags: [Admin]
 *     security:
 *       - BearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 code:
 *                   type: integer
 *                 message:
 *                   type: string
 *                 data:
 *                   type: object
 *                   properties:
 *                     total:
 *                       type: integer
 *                       description: 用户总数
 *                     byType:
 *                       type: array
 *                       description: 按类型统计
 *                       items:
 *                         type: object
 *                         properties:
 *                           user_type:
 *                             type: string
 *                           count:
 *                             type: integer
 *                     byDate:
 *                       type: array
 *                       description: 按日期统计
 *                       items:
 *                         type: object
 *                         properties:
 *                           date:
 *                             type: string
 *                           count:
 *                             type: integer
 *       401:
 *         description: 未授权
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ErrorResponse'
 */
router.get('/system/user-stats', authenticateAdmin, systemStatsController.getUserStats);

/**
 * @swagger
 * /admin/system/order-stats:
 *   get:
 *     summary: 获取订单统计数据
 *     tags: [Admin]
 *     security:
 *       - BearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 code:
 *                   type: integer
 *                 message:
 *                   type: string
 *                 data:
 *                   type: object
 *                   properties:
 *                     total:
 *                       type: integer
 *                       description: 订单总数
 *                     totalAmount:
 *                       type: number
 *                       description: 订单总金额
 *                     byStatus:
 *                       type: array
 *                       description: 按状态统计
 *                       items:
 *                         type: object
 *                         properties:
 *                           status:
 *                             type: string
 *                           count:
 *                             type: integer
 *                     byDate:
 *                       type: array
 *                       description: 按日期统计
 *                       items:
 *                         type: object
 *                         properties:
 *                           date:
 *                             type: string
 *                           count:
 *                             type: integer
 *                           amount:
 *                             type: number
 *       401:
 *         description: 未授权
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ErrorResponse'
 */
router.get('/system/order-stats', authenticateAdmin, systemStatsController.getOrderStats);

/**
 * @swagger
 * /admin/system/info:
 *   get:
 *     summary: 获取系统信息
 *     tags: [Admin]
 *     security:
 *       - BearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 code:
 *                   type: integer
 *                 message:
 *                   type: string
 *                 data:
 *                   type: object
 *                   properties:
 *                     nodeVersion:
 *                       type: string
 *                       description: Node.js版本
 *                     platform:
 *                       type: string
 *                       description: 运行平台
 *                     arch:
 *                       type: string
 *                       description: 系统架构
 *                     uptime:
 *                       type: integer
 *                       description: 运行时间(秒)
 *                     databaseVersion:
 *                       type: string
 *                       description: 数据库版本
 *       401:
 *         description: 未授权
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ErrorResponse'
 */
router.get('/system/info', authenticateAdmin, systemStatsController.getSystemInfo);

// 注册子路由
router.use('/users', userManagementRoutes);
router.use('/statistics', dataStatisticsRoutes);
router.use('/animals', animalManagementRoutes);
router.use('/files', fileManagementRoutes);

module.exports = router;