const express = require('express');
const { query } = require('express-validator');
const DataStatisticsController = require('../../controllers/admin/dataStatistics');
const { requireRole } = require('../../middleware/auth');

const router = express.Router();

/**
 * @swagger
 * tags:
 *   name: Admin Data Statistics
 *   description: 管理员数据统计相关接口
 */

/**
 * @swagger
 * components:
 *   schemas:
 *     SystemOverview:
 *       type: object
 *       properties:
 *         users:
 *           type: object
 *           properties:
 *             total_users:
 *               type: integer
 *               description: 用户总数
 *             active_users:
 *               type: integer
 *               description: 活跃用户数
 *             new_users_today:
 *               type: integer
 *               description: 今日新增用户
 *             new_users_week:
 *               type: integer
 *               description: 本周新增用户
 *         travels:
 *           type: object
 *           properties:
 *             total_travels:
 *               type: integer
 *               description: 旅行总数
 *             published_travels:
 *               type: integer
 *               description: 已发布旅行
 *             new_travels_today:
 *               type: integer
 *               description: 今日新增旅行
 *         animals:
 *           type: object
 *           properties:
 *             total_animals:
 *               type: integer
 *               description: 动物总数
 *             available_animals:
 *               type: integer
 *               description: 可认领动物
 *             claimed_animals:
 *               type: integer
 *               description: 已认领动物
 *         orders:
 *           type: object
 *           properties:
 *             total_orders:
 *               type: integer
 *               description: 订单总数
 *             completed_orders:
 *               type: integer
 *               description: 已完成订单
 *             total_revenue:
 *               type: number
 *               description: 总收入
 *     TrendData:
 *       type: object
 *       properties:
 *         date:
 *           type: string
 *           format: date
 *           description: 日期
 *         new_users:
 *           type: integer
 *           description: 新增用户数
 *         cumulative_users:
 *           type: integer
 *           description: 累计用户数
 */

/**
 * @swagger
 * /admin/statistics/overview:
 *   get:
 *     summary: 获取系统概览统计
 *     tags: [Admin Data Statistics]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   $ref: '#/components/schemas/SystemOverview'
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 */
router.get('/overview',
  requireRole(['admin', 'super_admin']),
  DataStatisticsController.getSystemOverview
);

/**
 * @swagger
 * /admin/statistics/user-growth:
 *   get:
 *     summary: 获取用户增长趋势
 *     tags: [Admin Data Statistics]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: period
 *         schema:
 *           type: string
 *           enum: [7d, 30d, 90d, 365d]
 *           default: 30d
 *         description: 统计周期
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: object
 *                   properties:
 *                     period:
 *                       type: string
 *                     trendData:
 *                       type: array
 *                       items:
 *                         $ref: '#/components/schemas/TrendData'
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 */
router.get('/user-growth',
  requireRole(['admin', 'super_admin']),
  [
    query('period').optional().isIn(['7d', '30d', '90d', '365d'])
  ],
  DataStatisticsController.getUserGrowthTrend
);

/**
 * @swagger
 * /admin/statistics/business:
 *   get:
 *     summary: 获取业务数据统计
 *     tags: [Admin Data Statistics]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: period
 *         schema:
 *           type: string
 *           enum: [7d, 30d, 90d]
 *           default: 30d
 *         description: 统计周期
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: object
 *                   properties:
 *                     period:
 *                       type: string
 *                     travelStats:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           date:
 *                             type: string
 *                             format: date
 *                           new_travels:
 *                             type: integer
 *                           published_travels:
 *                             type: integer
 *                           matched_travels:
 *                             type: integer
 *                     claimStats:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           date:
 *                             type: string
 *                             format: date
 *                           new_claims:
 *                             type: integer
 *                           approved_claims:
 *                             type: integer
 *                           rejected_claims:
 *                             type: integer
 *                     orderStats:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           date:
 *                             type: string
 *                             format: date
 *                           new_orders:
 *                             type: integer
 *                           completed_orders:
 *                             type: integer
 *                           daily_revenue:
 *                             type: number
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 */
router.get('/business',
  requireRole(['admin', 'super_admin']),
  [
    query('period').optional().isIn(['7d', '30d', '90d'])
  ],
  DataStatisticsController.getBusinessStatistics
);

/**
 * @swagger
 * /admin/statistics/geographic:
 *   get:
 *     summary: 获取地域分布统计
 *     tags: [Admin Data Statistics]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: object
 *                   properties:
 *                     userDistribution:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           province:
 *                             type: string
 *                           city:
 *                             type: string
 *                           user_count:
 *                             type: integer
 *                     provinceStats:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           province:
 *                             type: string
 *                           user_count:
 *                             type: integer
 *                           farmer_count:
 *                             type: integer
 *                           merchant_count:
 *                             type: integer
 *                     destinationStats:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           destination:
 *                             type: string
 *                           travel_count:
 *                             type: integer
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 */
router.get('/geographic',
  requireRole(['admin', 'super_admin']),
  DataStatisticsController.getGeographicDistribution
);

/**
 * @swagger
 * /admin/statistics/user-behavior:
 *   get:
 *     summary: 获取用户行为分析
 *     tags: [Admin Data Statistics]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: object
 *                   properties:
 *                     activityStats:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           activity_level:
 *                             type: string
 *                           user_count:
 *                             type: integer
 *                     levelDistribution:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           level:
 *                             type: string
 *                           user_count:
 *                             type: integer
 *                           avg_points:
 *                             type: number
 *                           avg_travel_count:
 *                             type: number
 *                           avg_claim_count:
 *                             type: number
 *                     behaviorStats:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           behavior_type:
 *                             type: string
 *                           user_count:
 *                             type: integer
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 */
router.get('/user-behavior',
  requireRole(['admin', 'super_admin']),
  DataStatisticsController.getUserBehaviorAnalysis
);

/**
 * @swagger
 * /admin/statistics/revenue:
 *   get:
 *     summary: 获取收入统计
 *     tags: [Admin Data Statistics]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: period
 *         schema:
 *           type: string
 *           enum: [7d, 30d, 90d, 365d]
 *           default: 30d
 *         description: 统计周期
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: object
 *                   properties:
 *                     period:
 *                       type: string
 *                     revenueTrend:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           date:
 *                             type: string
 *                             format: date
 *                           daily_revenue:
 *                             type: number
 *                           completed_orders:
 *                             type: integer
 *                           total_orders:
 *                             type: integer
 *                     revenueSource:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           order_type:
 *                             type: string
 *                           order_count:
 *                             type: integer
 *                           total_revenue:
 *                             type: number
 *                           avg_order_value:
 *                             type: number
 *                     paymentMethodStats:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           payment_method:
 *                             type: string
 *                           order_count:
 *                             type: integer
 *                           total_amount:
 *                             type: number
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 */
router.get('/revenue',
  requireRole(['admin', 'super_admin']),
  [
    query('period').optional().isIn(['7d', '30d', '90d', '365d'])
  ],
  DataStatisticsController.getRevenueStatistics
);

/**
 * @swagger
 * /admin/statistics/export:
 *   get:
 *     summary: 导出统计报告
 *     tags: [Admin Data Statistics]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: reportType
 *         schema:
 *           type: string
 *           enum: [overview, users, revenue]
 *           default: overview
 *         description: 报告类型
 *       - in: query
 *         name: period
 *         schema:
 *           type: string
 *           enum: [7d, 30d, 90d]
 *           default: 30d
 *         description: 统计周期
 *       - in: query
 *         name: format
 *         schema:
 *           type: string
 *           enum: [csv, json]
 *           default: csv
 *         description: 导出格式
 *     responses:
 *       200:
 *         description: 导出成功
 *         content:
 *           text/csv:
 *             schema:
 *               type: string
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: object
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 */
router.get('/export',
  requireRole(['admin', 'super_admin']),
  [
    query('reportType').optional().isIn(['overview', 'users', 'revenue']),
    query('period').optional().isIn(['7d', '30d', '90d']),
    query('format').optional().isIn(['csv', 'json'])
  ],
  DataStatisticsController.exportStatisticsReport
);

module.exports = router;