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

class TravelController {
  // 获取旅行计划列表
  static async getTravelPlans(req, res, next) {
    try {
      const { page, pageSize, status } = req.query;
      
      const result = await TravelService.getTravelPlans({
        userId: req.userId,
        page: parseInt(page) || 1,
        pageSize: parseInt(pageSize) || 10,
        status
      });

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

  // 获取单个旅行计划详情
  static async getTravelPlan(req, res, next) {
    try {
      const { planId } = req.params;
      
      if (!planId) {
        throw new AppError('旅行计划ID不能为空', 400);
      }

      const plan = await TravelService.getTravelPlanById(planId);
      res.json(success({ plan }));
    } catch (error) {
      next(error);
    }
  }

  // 创建旅行计划
  static async createTravelPlan(req, res, next) {
    try {
      const {
        destination,
        start_date,
        end_date,
        budget,
        companions,
        transportation,
        accommodation,
        activities,
        notes
      } = req.body;

      if (!destination || !start_date || !end_date) {
        throw new AppError('目的地、开始日期和结束日期不能为空', 400);
      }

      const planId = await TravelService.createTravelPlan(req.userId, {
        destination,
        start_date,
        end_date,
        budget,
        companions,
        transportation,
        accommodation,
        activities,
        notes
      });

      const plan = await TravelService.getTravelPlanById(planId);

      res.status(201).json(success({
        plan,
        message: '旅行计划创建成功'
      }));
    } catch (error) {
      next(error);
    }
  }

  // 更新旅行计划
  static async updateTravelPlan(req, res, next) {
    try {
      const { planId } = req.params;
      
      if (!planId) {
        throw new AppError('旅行计划ID不能为空', 400);
      }

      const plan = await TravelService.updateTravelPlan(planId, req.userId, req.body);
      
      res.json(success({
        plan,
        message: '旅行计划更新成功'
      }));
    } catch (error) {
      next(error);
    }
  }

  // 删除旅行计划
  static async deleteTravelPlan(req, res, next) {
    try {
      const { planId } = req.params;
      
      if (!planId) {
        throw new AppError('旅行计划ID不能为空', 400);
      }

      await TravelService.deleteTravelPlan(planId, req.userId);
      
      res.json(success({
        message: '旅行计划删除成功',
        planId: parseInt(planId)
      }));
    } catch (error) {
      next(error);
    }
  }

  // 获取用户旅行统计
  static async getTravelStats(req, res, next) {
    try {
      const stats = await TravelService.getUserTravelStats(req.userId);
      
      res.json(success({ stats }));
    } catch (error) {
      next(error);
    }
  }

  // 获取所有旅行计划（管理员功能）
  static async getAllTravelPlans(req, res, next) {
    try {
      const { page, pageSize, status, userId } = req.query;
      
      const result = await TravelService.getTravelPlans({
        userId,
        page: parseInt(page) || 1,
        pageSize: parseInt(pageSize) || 10,
        status
      });

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

module.exports = TravelController;