const TravelRegistrationService = require('../services/travelRegistration');
const { success } = require('../utils/response');
const { AppError } = require('../utils/errors');

/**
 * 旅行活动报名控制器
 */
class TravelRegistrationController {
  /**
   * 报名参加旅行活动
   */
  static async registerForTravel(req, res, next) {
    try {
      const { travelId } = req.params;
      const { message, emergencyContact, emergencyPhone } = req.body;
      const userId = req.userId;

      if (!travelId) {
        throw new AppError('旅行活动ID不能为空', 400);
      }

      const registration = await TravelRegistrationService.registerForTravel({
        userId,
        travelId: parseInt(travelId),
        message,
        emergencyContact,
        emergencyPhone
      });

      res.json(success({
        registration,
        message: '报名成功，等待审核'
      }));
    } catch (error) {
      next(error);
    }
  }

  /**
   * 取消报名
   */
  static async cancelRegistration(req, res, next) {
    try {
      const { registrationId } = req.params;
      const userId = req.userId;

      if (!registrationId) {
        throw new AppError('报名记录ID不能为空', 400);
      }

      await TravelRegistrationService.cancelRegistration(parseInt(registrationId), userId);

      res.json(success({
        message: '取消报名成功'
      }));
    } catch (error) {
      next(error);
    }
  }

  /**
   * 获取用户的报名记录
   */
  static async getUserRegistrations(req, res, next) {
    try {
      const { page, pageSize, status } = req.query;
      const userId = req.userId;

      const result = await TravelRegistrationService.getUserRegistrations({
        userId,
        page: parseInt(page) || 1,
        pageSize: parseInt(pageSize) || 10,
        status
      });

      res.json(success(result));
    } catch (error) {
      next(error);
    }
  }

  /**
   * 获取旅行活动的报名列表（活动发起者可查看）
   */
  static async getTravelRegistrations(req, res, next) {
    try {
      const { travelId } = req.params;
      const { page, pageSize, status } = req.query;
      const userId = req.userId;

      if (!travelId) {
        throw new AppError('旅行活动ID不能为空', 400);
      }

      const result = await TravelRegistrationService.getTravelRegistrations({
        travelId: parseInt(travelId),
        organizerId: userId,
        page: parseInt(page) || 1,
        pageSize: parseInt(pageSize) || 10,
        status
      });

      res.json(success(result));
    } catch (error) {
      next(error);
    }
  }

  /**
   * 审核报名申请（活动发起者操作）
   */
  static async reviewRegistration(req, res, next) {
    try {
      const { registrationId } = req.params;
      const { action, rejectReason } = req.body;
      const userId = req.userId;

      if (!registrationId) {
        throw new AppError('报名记录ID不能为空', 400);
      }

      if (!['approve', 'reject'].includes(action)) {
        throw new AppError('操作类型无效', 400);
      }

      const result = await TravelRegistrationService.reviewRegistration({
        registrationId: parseInt(registrationId),
        organizerId: userId,
        action,
        rejectReason
      });

      res.json(success({
        registration: result,
        message: action === 'approve' ? '审核通过' : '已拒绝申请'
      }));
    } catch (error) {
      next(error);
    }
  }

  /**
   * 获取报名统计信息
   */
  static async getRegistrationStats(req, res, next) {
    try {
      const { travelId } = req.params;
      const userId = req.userId;

      if (!travelId) {
        throw new AppError('旅行活动ID不能为空', 400);
      }

      const stats = await TravelRegistrationService.getRegistrationStats(parseInt(travelId), userId);

      res.json(success({ stats }));
    } catch (error) {
      next(error);
    }
  }
}

module.exports = TravelRegistrationController;