import { get, post } from '../utils/http.js'

// 获取排行榜和我的排名
export const getRankingListAndMyRanking = async (data) => {
  try {
    const response = await post(`/ranking/list`, data)
    return response
  } catch (error) {
    console.error('Failed to get list and my ranking:', error)
    console.error('error:' + error.response.errorMsg)
    throw error
  }
}

// 获取本周奖励和礼物
export const getThisWeekRewardsAndGifts = async (templateId) => {
  try {
    const response = await get(`/activity/king-queen/effective?templateId=${templateId}`)
    return response
  } catch (error) {
    console.error('Failed to get this week rewards and gifts:', error)
    console.error('error:' + error.response.errorMsg)
    throw error
  }
}

// 获取总榜前三
export const getOverallRankingTop3 = async (data) => {
  try {
    const response = await post(`/ranking/top-three-overall`, data)
    return response
  } catch (error) {
    console.error('Failed to get history top one:', error)
    console.error('error:' + error.response.errorMsg)
    throw error
  }
}

// 获取总榜
export const getOverallRanking = async (topN, data) => {
  try {
    const response = await post(`/ranking/top-three-overall?topN=${topN}`, data)
    return response
  } catch (error) {
    console.error('Failed to get history top one:', error)
    console.error('error:' + error.response.errorMsg)
    throw error
  }
}

// 获取历史榜首
export const getHistoryTopOne = async (activityType) => {
  try {
    let data = { activityType }
    const response = await post(`/ranking/historical-top1`, data)
    return response
  } catch (error) {
    console.error('Failed to get history top one:', error)
    console.error('error:' + error.response.errorMsg)
    throw error
  }
}

// 获取指定类型活动资源
export const apiGetActivityResource = async (activityType) => {
  try {
    const response = await get(
      `/props-activity-cnf/client/listActivityResource?sysOrigin=LIKEI&activityType=${activityType}`,
    )
    return response
  } catch (error) {
    console.error('Failed to get activity resource:', error)
    console.error('error:' + (error.response?.errorMsg || error.errorMsg || error.message || ''))
    throw error
  }
}

// 获取每日充值活动等级任务信息
export const getDailyRechargeInfo = async () => {
  try {
    const response = await get('/activity/daily-recharge/info')
    return response
  } catch (error) {
    console.error('Failed to get daily recharge info:', error)
    console.error('error:' + (error.response?.errorMsg || error.errorMsg || error.message || ''))
    throw error
  }
}

// 领取每日充值活动 task1 历史累计奖励
export const claimDailyRechargeTask1 = async (data) => {
  try {
    const response = await post('/activity/daily-recharge/claim/task1', data)
    return response
  } catch (error) {
    console.error('Failed to claim daily recharge task1 reward:', error)
    console.error('error:' + (error.response?.errorMsg || error.errorMsg || error.message || ''))
    throw error
  }
}

// 领取每日充值活动 task2 当前等级奖励
export const claimDailyRechargeTask2 = async (data) => {
  try {
    const response = await post('/activity/daily-recharge/claim/task2', data)
    return response
  } catch (error) {
    console.error('Failed to claim daily recharge task2 reward:', error)
    console.error('error:' + (error.response?.errorMsg || error.errorMsg || error.message || ''))
    throw error
  }
}

// 查询用户票券数量
export const apiGetCardTicketList = async (activityId) => {
  try {
    const response = await get(
      `/activity/card/ticket/list?activityId=${encodeURIComponent(activityId)}`,
    )
    return response
  } catch (error) {
    console.error('Failed to get card ticket list:', error)
    console.error('error:' + (error.response?.errorMsg || error.errorMsg || error.message || ''))
    throw error
  }
}

// 查询奖励池奖品列表
export const apiGetCardRewardPrizeList = async (activityId, poolType) => {
  try {
    const response = await get(
      `/activity/card/reward/prize/list?activityId=${encodeURIComponent(activityId)}&poolType=${encodeURIComponent(poolType)}`,
    )
    return response
  } catch (error) {
    console.error('Failed to get card reward prize list:', error)
    console.error('error:' + (error.response?.errorMsg || error.errorMsg || error.message || ''))
    throw error
  }
}

// 查询中奖飘窗列表
export const apiGetCardRewardMarquee = async (activityId) => {
  try {
    const response = await get(
      `/activity/card/reward/marquee?activityId=${encodeURIComponent(activityId)}`,
    )
    return response
  } catch (error) {
    console.error('Failed to get card reward marquee:', error)
    console.error('error:' + (error.response?.errorMsg || error.errorMsg || error.message || ''))
    throw error
  }
}

// 抽卡（消耗抽卡票或金币，抽 R 级卡片）
export const apiPostCardDraw = async (data) => {
  try {
    const response = await post('/activity/card/draw', data)
    return response
  } catch (error) {
    console.error('Failed to draw card:', error)
    console.error('error:' + (error.response?.errorMsg || error.errorMsg || error.message || ''))
    throw error
  }
}

// 合成卡片（R×5→SR，SR×3→SSR）
export const apiPostCardCombine = async (data) => {
  try {
    const response = await post('/activity/card/combine', data)
    return response
  } catch (error) {
    console.error('Failed to combine card:', error)
    console.error('error:' + (error.response?.errorMsg || error.errorMsg || error.message || ''))
    throw error
  }
}

// 兑换奖励票（卡片 → STANDARD/PREMIUM/LUXURY 票）
export const apiPostCardRedeem = async (data) => {
  try {
    const response = await post('/activity/card/redeem', data)
    return response
  } catch (error) {
    console.error('Failed to redeem card reward ticket:', error)
    console.error('error:' + (error.response?.errorMsg || error.errorMsg || error.message || ''))
    throw error
  }
}

// 奖励池抽奖（消耗奖励票，抽取实际奖励）
export const apiPostCardRewardDraw = async (data) => {
  try {
    const response = await post('/activity/card/reward/draw', data)
    return response
  } catch (error) {
    console.error('Failed to draw card reward:', error)
    console.error('error:' + (error.response?.errorMsg || error.errorMsg || error.message || ''))
    throw error
  }
}

// 查询用户卡包
export const apiPostCardBagList = async (data) => {
  try {
    const response = await post('/activity/card/bag/list', data)
    return response
  } catch (error) {
    console.error('Failed to get card bag list:', error)
    console.error('error:' + (error.response?.errorMsg || error.errorMsg || error.message || ''))
    throw error
  }
}

// 查询中奖记录
export const apiPostCardRewardRecordList = async (data = {}) => {
  try {
    const response = await post('/activity/card/reward/record/list', {
      size: 10,
      current: 1,
      ...data,
    })
    return response
  } catch (error) {
    console.error('Failed to get card reward record list:', error)
    console.error('error:' + (error.response?.errorMsg || error.errorMsg || error.message || ''))
    throw error
  }
}
