const express = require('express');
const { body, query } = require('express-validator');
const TravelRegistrationController = require('../controllers/travelRegistration');
const { authenticateUser: authenticate } = require('../middleware/auth');

const router = express.Router();

/**
 * @swagger
 * tags:
 *   name: TravelRegistration
 *   description: 旅行活动报名管理相关接口
 */

/**
 * @swagger
 * components:
 *   schemas:
 *     TravelRegistration:
 *       type: object
 *       properties:
 *         id:
 *           type: integer
 *           description: 报名记录ID
 *         travel_plan_id:
 *           type: integer
 *           description: 旅行活动ID
 *         user_id:
 *           type: integer
 *           description: 报名用户ID
 *         message:
 *           type: string
 *           description: 报名留言
 *         emergency_contact:
 *           type: string
 *           description: 紧急联系人
 *         status:
 *           type: string
 *           enum: [pending, approved, rejected, cancelled]
 *           description: 报名状态
 *         applied_at:
 *           type: string
 *           format: date-time
 *           description: 报名时间
 *         responded_at:
 *           type: string
 *           format: date-time
 *           description: 审核时间
 *         reject_reason:
 *           type: string
 *           description: 拒绝原因
 *         username:
 *           type: string
 *           description: 用户名
 *         real_name:
 *           type: string
 *           description: 真实姓名
 *         avatar_url:
 *           type: string
 *           description: 头像URL
 */

/**
 * @swagger
 * /travel-registration/{travelId}/register:
 *   post:
 *     summary: 报名参加旅行活动
 *     tags: [TravelRegistration]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: travelId
 *         required: true
 *         schema:
 *           type: integer
 *         description: 旅行活动ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               message:
 *                 type: string
 *                 description: 报名留言
 *                 example: 希望能和大家一起愉快旅行
 *               emergencyContact:
 *                 type: string
 *                 description: 紧急联系人
 *                 example: 张三
 *               emergencyPhone:
 *                 type: string
 *                 description: 紧急联系电话
 *                 example: 13800138000
 *     responses:
 *       200:
 *         description: 报名成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   type: object
 *                   properties:
 *                     registration:
 *                       $ref: '#/components/schemas/TravelRegistration'
 *                     message:
 *                       type: string
 *                       example: 报名成功，等待审核
 *       400:
 *         description: 请求参数错误或业务逻辑错误
 *       401:
 *         description: 未授权
 *       404:
 *         description: 旅行活动不存在
 */
router.post('/:travelId/register', 
  authenticate,
  [
    body('emergencyContact').optional().isLength({ min: 1, max: 50 }).withMessage('紧急联系人长度应在1-50字符之间'),
    body('emergencyPhone').optional().isMobilePhone('zh-CN').withMessage('紧急联系电话格式不正确'),
    body('message').optional().isLength({ max: 500 }).withMessage('报名留言不能超过500字符')
  ],
  TravelRegistrationController.registerForTravel
);

/**
 * @swagger
 * /travel-registration/{registrationId}/cancel:
 *   put:
 *     summary: 取消报名
 *     tags: [TravelRegistration]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: registrationId
 *         required: true
 *         schema:
 *           type: integer
 *         description: 报名记录ID
 *     responses:
 *       200:
 *         description: 取消成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   type: object
 *                   properties:
 *                     message:
 *                       type: string
 *                       example: 取消报名成功
 *       400:
 *         description: 请求参数错误或业务逻辑错误
 *       401:
 *         description: 未授权
 *       404:
 *         description: 报名记录不存在
 */
router.put('/:registrationId/cancel', authenticate, TravelRegistrationController.cancelRegistration);

/**
 * @swagger
 * /travel-registration/my-registrations:
 *   get:
 *     summary: 获取用户的报名记录
 *     tags: [TravelRegistration]
 *     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: [pending, approved, rejected, cancelled]
 *         description: 报名状态
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   type: object
 *                   properties:
 *                     registrations:
 *                       type: array
 *                       items:
 *                         $ref: '#/components/schemas/TravelRegistration'
 *                     pagination:
 *                       type: object
 *                       properties:
 *                         page:
 *                           type: integer
 *                         pageSize:
 *                           type: integer
 *                         total:
 *                           type: integer
 *                         totalPages:
 *                           type: integer
 *       401:
 *         description: 未授权
 */
router.get('/my-registrations', authenticate, TravelRegistrationController.getUserRegistrations);

/**
 * @swagger
 * /travel-registration/{travelId}/registrations:
 *   get:
 *     summary: 获取旅行活动的报名列表（活动发起者可查看）
 *     tags: [TravelRegistration]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: travelId
 *         required: true
 *         schema:
 *           type: integer
 *         description: 旅行活动ID
 *       - 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: [pending, approved, rejected, cancelled]
 *         description: 报名状态
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   type: object
 *                   properties:
 *                     registrations:
 *                       type: array
 *                       items:
 *                         $ref: '#/components/schemas/TravelRegistration'
 *                     pagination:
 *                       type: object
 *                       properties:
 *                         page:
 *                           type: integer
 *                         pageSize:
 *                           type: integer
 *                         total:
 *                           type: integer
 *                         totalPages:
 *                           type: integer
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 *       404:
 *         description: 旅行活动不存在
 */
router.get('/:travelId/registrations', authenticate, TravelRegistrationController.getTravelRegistrations);

/**
 * @swagger
 * /travel-registration/{registrationId}/review:
 *   put:
 *     summary: 审核报名申请（活动发起者操作）
 *     tags: [TravelRegistration]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: registrationId
 *         required: true
 *         schema:
 *           type: integer
 *         description: 报名记录ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - action
 *             properties:
 *               action:
 *                 type: string
 *                 enum: [approve, reject]
 *                 description: 审核操作
 *                 example: approve
 *               rejectReason:
 *                 type: string
 *                 description: 拒绝原因（拒绝时必填）
 *                 example: 活动要求不符合
 *     responses:
 *       200:
 *         description: 审核成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   type: object
 *                   properties:
 *                     registration:
 *                       $ref: '#/components/schemas/TravelRegistration'
 *                     message:
 *                       type: string
 *                       example: 审核通过
 *       400:
 *         description: 请求参数错误或业务逻辑错误
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 *       404:
 *         description: 报名记录不存在
 */
router.put('/:registrationId/review', 
  authenticate,
  [
    body('action').isIn(['approve', 'reject']).withMessage('操作类型必须是approve或reject'),
    body('rejectReason').optional().isLength({ min: 1, max: 200 }).withMessage('拒绝原因长度应在1-200字符之间')
  ],
  TravelRegistrationController.reviewRegistration
);

/**
 * @swagger
 * /travel-registration/{travelId}/stats:
 *   get:
 *     summary: 获取报名统计信息
 *     tags: [TravelRegistration]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: travelId
 *         required: true
 *         schema:
 *           type: integer
 *         description: 旅行活动ID
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   type: object
 *                   properties:
 *                     stats:
 *                       type: object
 *                       properties:
 *                         total_applications:
 *                           type: integer
 *                           description: 总申请数
 *                         pending_count:
 *                           type: integer
 *                           description: 待审核数
 *                         approved_count:
 *                           type: integer
 *                           description: 已通过数
 *                         rejected_count:
 *                           type: integer
 *                           description: 已拒绝数
 *                         cancelled_count:
 *                           type: integer
 *                           description: 已取消数
 *       401:
 *         description: 未授权
 *       403:
 *         description: 权限不足
 *       404:
 *         description: 旅行活动不存在
 */
router.get('/:travelId/stats', authenticate, TravelRegistrationController.getRegistrationStats);

module.exports = router;