import request from './request.js'
import config from './config.js'

const { endpoints } = config

// 用户服务
export const userService = {
  // 登录
  login: (data) => request.post(endpoints.USER.LOGIN, data),
  
  // 注册
  register: (data) => request.post(endpoints.USER.REGISTER, data),
  
  // 获取用户信息
  getProfile: () => request.get(endpoints.USER.PROFILE),
  
  // 更新用户信息
  updateProfile: (data) => request.put(endpoints.USER.UPDATE_PROFILE, data),
  
  // 上传头像
  uploadAvatar: (filePath) => request.upload(endpoints.USER.UPLOAD_AVATAR, filePath),
  
  // 退出登录
  logout: () => {
    uni.removeStorageSync('token')
    uni.removeStorageSync('refreshToken')
    return Promise.resolve()
  }
}

// 旅行计划服务
export const travelService = {
  // 获取旅行计划列表
  getList: (params = {}) => request.get(endpoints.TRAVEL.LIST, params),
  
  // 获取旅行计划详情
  getDetail: (id) => request.get(`${endpoints.TRAVEL.DETAIL}/${id}`),
  
  // 创建旅行计划
  create: (data) => request.post(endpoints.TRAVEL.CREATE, data),
  
  // 更新旅行计划
  update: (id, data) => request.put(`${endpoints.TRAVEL.UPDATE}/${id}`, data),
  
  // 删除旅行计划
  deletePlan: (id) => request.delete(`${endpoints.TRAVEL.DELETE}/${id}`),
  
  // 加入旅行计划
  joinPlan: (travelId) => request.post(`${endpoints.TRAVEL.JOIN}/${travelId}`),
  
  // 退出旅行计划
  quitPlan: (travelId) => request.post(`${endpoints.TRAVEL.QUIT}/${travelId}`),
  
  // 点赞旅行计划
  likePlan: (travelId) => request.post(`${endpoints.TRAVEL.LIKE}/${travelId}`),
  
  // 取消点赞旅行计划
  unlikePlan: (travelId) => request.post(`${endpoints.TRAVEL.UNLIKE}/${travelId}`),
  
  // 添加评论
  addComment: (travelId, data) => request.post(`${endpoints.TRAVEL.COMMENT}/${travelId}`, data),
  
  // 获取评论列表
  getComments: (travelId, params = {}) => request.get(`${endpoints.TRAVEL.COMMENTS}/${travelId}`, params),
  
  // 获取我的旅行计划
  getMyPlans: (params = {}) => request.get(endpoints.TRAVEL.MY_PLANS, params),
  
  // 搜索旅行计划
  search: (keyword, params = {}) => request.get(endpoints.TRAVEL.SEARCH, { keyword, ...params })
}

// 动物认养服务
export const animalService = {
  // 获取动物列表
  getList: (params = {}) => request.get(endpoints.ANIMAL.LIST, params),
  
  // 获取动物详情
  getDetail: (id) => request.get(`${endpoints.ANIMAL.DETAIL}/${id}`),
  
  // 认养动物
  adopt: (animalId, data) => request.post(`${endpoints.ANIMAL.ADOPT}/${animalId}`, data),
  
  // 获取我的动物
  getMyAnimals: (params = {}) => request.get(endpoints.ANIMAL.MY_ANIMALS, params),
  
  // 获取动物分类
  getCategories: () => request.get(endpoints.ANIMAL.CATEGORIES)
}

// 送花服务
export const flowerService = {
  // 获取花束列表
  getList: (params = {}) => request.get(endpoints.FLOWER.LIST, params),
  
  // 获取花束详情
  getDetail: (id) => request.get(`${endpoints.FLOWER.DETAIL}/${id}`),
  
  // 下单
  order: (data) => request.post(endpoints.FLOWER.ORDER, data),
  
  // 获取我的订单
  getMyOrders: (params = {}) => request.get(endpoints.FLOWER.MY_ORDERS, params),
  
  // 获取花束分类
  getCategories: () => request.get(endpoints.FLOWER.CATEGORIES)
}

// 订单服务
export const orderService = {
  // 获取订单列表
  getList: (params = {}) => request.get(endpoints.ORDER.LIST, params),
  
  // 获取订单详情
  getDetail: (id) => request.get(`${endpoints.ORDER.DETAIL}/${id}`),
  
  // 取消订单
  cancel: (id) => request.post(`${endpoints.ORDER.CANCEL}/${id}`),
  
  // 支付订单
  pay: (id) => request.post(`${endpoints.ORDER.PAY}/${id}`),
  
  // 确认收货
  confirm: (id) => request.post(`${endpoints.ORDER.CONFIRM}/${id}`)
}

// 支付服务
export const paymentService = {
  // 创建支付
  create: (data) => request.post(endpoints.PAYMENT.CREATE, data),
  
  // 查询支付状态
  query: (paymentId) => request.get(`${endpoints.PAYMENT.QUERY}/${paymentId}`),
  
  // 退款
  refund: (paymentId, data) => request.post(`${endpoints.PAYMENT.REFUND}/${paymentId}`, data)
}

// 系统服务
export const systemService = {
  // 获取系统配置
  getConfig: () => request.get(endpoints.SYSTEM.CONFIG),
  
  // 获取公告列表
  getNotices: (params = {}) => request.get(endpoints.SYSTEM.NOTICE, params),
  
  // 提交反馈
  submitFeedback: (data) => request.post(endpoints.SYSTEM.FEEDBACK, data)
}

// 首页数据服务
export const homeService = {
  // 获取首页数据
  getHomeData: () => request.get('/home/data'),
  
  // 获取轮播图
  getBanners: () => request.get('/home/banners'),
  
  // 获取推荐旅行计划
  getRecommendedTravels: () => request.get('/home/recommended-travels'),
  
  // 获取热门动物
  getHotAnimals: () => request.get('/home/hot-animals'),
  
  // 获取精选花束
  getFeaturedFlowers: () => request.get('/home/featured-flowers')
}

// 搜索服务
export const searchService = {
  // 全局搜索
  search: (params = {}) => request.get(endpoints.SEARCH.GLOBAL, params),
  
  // 获取搜索建议
  getSuggestions: (keyword) => request.get(endpoints.SEARCH.SUGGESTIONS, { keyword }),
  
  // 旅行计划搜索
  searchTravel: (params = {}) => request.get(endpoints.SEARCH.TRAVEL, params),
  
  // 动物搜索
  searchAnimal: (params = {}) => request.get(endpoints.SEARCH.ANIMAL, params),
  
  // 花束搜索
  searchFlower: (params = {}) => request.get(endpoints.SEARCH.FLOWER, params),
  
  // 用户搜索
  searchUser: (params = {}) => request.get(endpoints.SEARCH.USER, params)
}

// 推广服务
export const promotionService = {
  // 获取推广数据
  getPromotionData: () => request.get(endpoints.PROMOTION.DATA),
  
  // 获取邀请记录
  getRecentRecords: (params = {}) => request.get(endpoints.PROMOTION.RECORDS, params),
  
  // 获取所有邀请记录
  getAllRecords: (params = {}) => request.get(endpoints.PROMOTION.ALL_RECORDS, params),
  
  // 生成邀请二维码
  generateQRCode: () => request.get(endpoints.PROMOTION.QRCODE),
  
  // 获取奖励明细
  getRewardDetails: (params = {}) => request.get(endpoints.PROMOTION.REWARD_DETAILS, params),
  
  // 提现申请
  applyWithdraw: (data) => request.post(endpoints.PROMOTION.WITHDRAW, data),
  
  // 获取提现记录
  getWithdrawRecords: (params = {}) => request.get(endpoints.PROMOTION.WITHDRAW_RECORDS, params)
}

// 认证服务
export const authService = {
  // 手机号登录
  phoneLogin: (data) => request.post(endpoints.AUTH.PHONE_LOGIN, data),
  
  // 微信登录
  wechatLogin: (data) => request.post(endpoints.AUTH.WECHAT_LOGIN, data),
  
  // 密码登录
  passwordLogin: (data) => request.post(endpoints.AUTH.PASSWORD_LOGIN, data),
  
  // 发送短信验证码
  sendSmsCode: (phone) => request.post(endpoints.AUTH.SEND_SMS_CODE, { phone }),
  
  // 验证token
  checkToken: (token) => request.post(endpoints.AUTH.CHECK_TOKEN, { token }),
  
  // 刷新token
  refreshToken: (refreshToken) => request.post(endpoints.AUTH.REFRESH_TOKEN, { refreshToken }),
  
  // 绑定手机号
  bindPhone: (data) => request.post(endpoints.AUTH.BIND_PHONE, data),
  
  // 修改密码
  changePassword: (data) => request.post(endpoints.AUTH.CHANGE_PASSWORD, data),
  
  // 重置密码
  resetPassword: (data) => request.post(endpoints.AUTH.RESET_PASSWORD, data)
}

// 工具函数
export const apiUtils = {
  // 生成分页参数
  generatePagination: (page = 1, pageSize = 10) => ({
    page,
    pageSize,
    skip: (page - 1) * pageSize
  }),
  
  // 处理上传进度
  handleUploadProgress: (progressEvent) => {
    const percent = Math.round((progressEvent.loaded * 100) / progressEvent.total)
    return percent
  },
  
  // 处理下载进度
  handleDownloadProgress: (progressEvent) => {
    const percent = Math.round((progressEvent.loaded * 100) / progressEvent.total)
    return percent
  },
  
  // 格式化错误信息
  formatError: (error) => {
    if (error.code) {
      return error.message
    }
    return '网络连接失败，请检查网络设置'
  }
}

// 默认导出所有服务
export default {
  userService,
  travelService,
  animalService,
  flowerService,
  orderService,
  paymentService,
  systemService,
  homeService,
  authService,
  searchService,
  promotionService,
  apiUtils
}