const express = require('express');
const { body, query } = require('express-validator');
const TravelController = require('../controllers/travel');
const { authenticateUser: authenticate, requireRole: requireAdmin } = require('../middleware/auth');

const router = express.Router();

/**
 * @swagger
 * tags:
 *   name: Travel
 *   description: 旅行计划管理相关接口
 */

/**
 * @swagger
 * /travel/plans:
 *   get:
 *     summary: 获取当前用户的旅行计划列表
 *     tags: [Travel]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           minimum: 1
 *         description: 页码
 *       - in: query
 *         name: pageSize
 *         schema:
 *           type: integer
 *           minimum: 1
 *           maximum: 50
 *         description: 每页数量
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *           enum: [planning, in_progress, completed, cancelled]
 *         description: 计划状态
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: object
 *                   properties:
 *                     plans:
 *                       type: array
 *                       items:
 *                         $ref: '#/components/schemas/TravelPlan'
 *                     pagination:
 *                       type: object
 *                       properties:
 *                         page:
 *                           type: integer
 *                         pageSize:
 *                           type: integer
 *                         total:
 *                           type: integer
 *                         totalPages:
 *                           type: integer
 *       401:
 *         description: 未授权
 *       500:
 *         description: 服务器内部错误
 */
router.get('/plans', authenticate, TravelController.getTravelPlans);

/**
 * @swagger
 * /travel/plans/{planId}:
 *   get:
 *     summary: 获取单个旅行计划详情
 *     tags: [Travel]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: planId
 *         required: true
 *         schema:
 *           type: integer
 *         description: 旅行计划ID
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: object
 *                   properties:
 *                     plan:
 *                       $ref: '#/components/schemas/TravelPlan'
 *       401:
 *         description: 未授权
 *       404:
 *         description: 旅行计划不存在
 *       500:
 *         description: 服务器内部错误
 */
router.get('/plans/:planId', authenticate, TravelController.getTravelPlan);

/**
 * @swagger
 * /travel/plans:
 *   post:
 *     summary: 创建旅行计划
 *     tags: [Travel]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - destination
 *               - start_date
 *               - end_date
 *             properties:
 *               destination:
 *                 type: string
 *                 description: 目的地
 *               start_date:
 *                 type: string
 *                 format: date
 *                 description: 开始日期
 *               end_date:
 *                 type: string
 *                 format: date
 *                 description: 结束日期
 *               budget:
 *                 type: number
 *                 description: 预算
 *               companions:
 *                 type: integer
 *                 description: 同行人数
 *               transportation:
 *                 type: string
 *                 description: 交通方式
 *               accommodation:
 *                 type: string
 *                 description: 住宿方式
 *               activities:
 *                 type: string
 *                 description: 活动安排
 *               notes:
 *                 type: string
 *                 description: 备注
 *     responses:
 *       201:
 *         description: 创建成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: object
 *                   properties:
 *                     plan:
 *                       $ref: '#/components/schemas/TravelPlan'
 *                     message:
 *                       type: string
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 *       500:
 *         description: 服务器内部错误
 */
router.post('/plans', 
  authenticate,
  [
    body('destination').notEmpty().withMessage('目的地不能为空'),
    body('start_date').isDate().withMessage('开始日期格式错误'),
    body('end_date').isDate().withMessage('结束日期格式错误')
  ],
  TravelController.createTravelPlan
);

/**
 * @swagger
 * /travel/plans/{planId}:
 *   put:
 *     summary: 更新旅行计划
 *     tags: [Travel]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: planId
 *         required: true
 *         schema:
 *           type: integer
 *         description: 旅行计划ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               destination:
 *                 type: string
 *                 description: 目的地
 *               start_date:
 *                 type: string
 *                 format: date
 *                 description: 开始日期
 *               end_date:
 *                 type: string
 *                 format: date
 *                 description: 结束日期
 *               budget:
 *                 type: number
 *                 description: 预算
 *               companions:
 *                 type: integer
 *                 description: 同行人数
 *               transportation:
 *                 type: string
 *                 description: 交通方式
 *               accommodation:
 *                 type: string
 *                 description: 住宿方式
 *               activities:
 *                 type: string
 *                 description: 活动安排
 *               notes:
 *                 type: string
 *                 description: 备注
 *               status:
 *                 type: string
 *                 enum: [planning, in_progress, completed, cancelled]
 *                 description: 计划状态
 *     responses:
 *       200:
 *         description: 更新成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: object
 *                   properties:
 *                     plan:
 *                       $ref: '#/components/schemas/TravelPlan'
 *                     message:
 *                       type: string
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 *       404:
 *         description: 旅行计划不存在
 *       500:
 *         description: 服务器内部错误
 */
router.put('/plans/:planId', authenticate, TravelController.updateTravelPlan);

/**
 * @swagger
 * /travel/plans/{planId}:
 *   delete:
 *     summary: 删除旅行计划
 *     tags: [Travel]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: planId
 *         required: true
 *         schema:
 *           type: integer
 *         description: 旅行计划ID
 *     responses:
 *       200:
 *         description: 删除成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: object
 *                   properties:
 *                     message:
 *                       type: string
 *                     planId:
 *                       type: integer
 *       401:
 *         description: 未授权
 *       404:
 *         description: 旅行计划不存在
 *       500:
 *         description: 服务器内部错误
 */
router.delete('/plans/:planId', authenticate, TravelController.deleteTravelPlan);

/**
 * @swagger
 * /travel/stats:
 *   get:
 *     summary: 获取用户旅行统计
 *     tags: [Travel]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: object
 *                   properties:
 *                     stats:
 *                       type: object
 *                       properties:
 *                         total_plans:
 *                           type: integer
 *                         completed_plans:
 *                           type: integer
 *                         planning_plans:
 *                           type: integer
 *                         cancelled_plans:
 *                           type: integer
 *                         total_budget:
 *                           type: number
 *       401:
 *         description: 未授权
 *       500:
 *         description: 服务器内部错误
 */
router.get('/stats', authenticate, TravelController.getTravelStats);

/**
 * @swagger
 * /travel/admin/plans:
 *   get:
 *     summary: 获取所有旅行计划（管理员）
 *     tags: [Travel]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           minimum: 1
 *         description: 页码
 *       - in: query
 *         name: pageSize
 *         schema:
 *           type: integer
 *           minimum: 1
 *           maximum: 50
 *         description: 每页数量
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *           enum: [planning, in_progress, completed, cancelled]
 *         description: 计划状态
 *       - in: query
 *         name: userId
 *         schema:
 *           type: integer
 *         description: 用户ID
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: object
 *                   properties:
 *                     plans:
 *                       type: array
 *                       items:
 *                         $ref: '#/components/schemas/TravelPlan'
 *                     pagination:
 *                       type: object
 *                       properties:
 *                         page:
 *                           type: integer
 *                         pageSize:
 *                           type: integer
 *                         total:
 *                           type: integer
 *                         totalPages:
 *                           type: integer
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 *       500:
 *         description: 服务器内部错误
 */
router.get('/admin/plans', authenticate, requireAdmin, TravelController.getAllTravelPlans);

module.exports = router;