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

// 获取我的-cp对象.
export const apiGetMyCpObject = async () => {
  try {
    const response = await get(`/user/cp-relationship/pair`)
    return response
  } catch (error) {
    console.error('Failed to get my cp object:', error)
    console.error('error:' + error.response.errorMsg)
    throw error
  }
}

// 发送CP告白信封.
export const apiSendLoveLetter = async (data) => {
  try {
    const response = await post(`/user/cp-relationship/send-love-letter`, data)
    return response
  } catch (error) {
    console.error('Failed to send love letter:', error)
    console.error('error:' + error.response.errorMsg)
    throw error
  }
}

// 查询告白墙.
export const apiGetLoveLetterWall = async () => {
  try {
    const response = await get(`/user/cp-relationship/love-letter-wall`)
    return response
  } catch (error) {
    console.error('Failed to get love letter wall:', error)
    console.error('error:' + error.response.errorMsg)
    throw error
  }
}

// 查询我收到的最新告白信封.
export const apiGetLatestLoveLetter = async () => {
  try {
    const response = await get(`/user/cp-relationship/latest-love-letter`)
    return response
  } catch (error) {
    console.error('Failed to get latest love letter:', error)
    console.error('error:' + error.response.errorMsg)
    throw error
  }
}

// cp排行榜(周榜和赛季榜)
export const apiGetCpRanking = async (type) => {
  try {
    const response = await get(`/activity/week-cp-gift/ranking-data?type=${type}`)
    return response
  } catch (error) {
    console.error('Failed to get cp ranking:', error)
    console.error('error:' + error.response.errorMsg)
    throw error
  }
}

// 赛季榜Top1列表
export const apiGetCpSeasonTop1List = async () => {
  try {
    const response = await get(`/activity/week-cp-gift/season-top1`)
    return response
  } catch (error) {
    console.error('Failed to get cp season top 1:', error)
    console.error('error:' + error.response.errorMsg)
    throw error
  }
}

// 奖励池.
export const apiGetCpRewardPool = async (type) => {
  try {
    const response = await get(`/activity/week-cp-gift/list-reward-pool?type=${type}`)
    return response
  } catch (error) {
    console.error('Failed to get cp reward pool:', error)
    console.error('error:' + error.response.errorMsg)
    throw error
  }
}

// 获取我的剩余抽奖券数量
export const apiGetMyLotteryTicketCount = async (activityId) => {
  try {
    const response = await get(`/activity/lottery/my-ticket-count?activityId=${activityId}`)
    return response
  } catch (error) {
    console.error('Failed to get my lottery ticket count:', error)
    console.error('error:' + error.response.errorMsg)
    throw error
  }
}

// 根据用户id与碎片集合查询用户碎片背包，并且数量大于0
export const apiGetUserFragmentBackpack = async (data) => {
  try {
    const response = await post(
      `/user/backpack/client/list/by/user-id?userId=${data.userId}&fragmentsIds=${data.fragmentsIds}`,
    )
    return response
  } catch (error) {
    console.error('Failed to get user fragment backpack:', error)
    console.error('error:' + error.response.errorMsg)
    throw error
  }
}

// 获取活动详情
export const activityDetail = async (activityCode) => {
  try {
    const response = await get(`/activity/lottery/detail/${activityCode}`)
    return response
  } catch (error) {
    console.error('Failed to fetch get activity detail:', error)
    console.error('error:' + error.response.errorMsg)
    throw error
  }
}

// 获取我的总抽奖次数
export const myTotalDrawCount = async () => {
  try {
    const response = await get(`/activity/lottery/my-total-draw-count`)
    return response
  } catch (error) {
    console.error('Failed to fetch get my total draw count:', error)
    console.error('error:' + error.response.errorMsg)
    throw error
  }
}

// 执行单次抽奖
export const onceDraw = async (data) => {
  try {
    const response = await post('/activity/lottery/draw', data)
    return response
  } catch (error) {
    console.error('Failed to fetch once draw:', error)
    throw error
  }
}

// 执行连抽
export const multiDraw = async (data) => {
  try {
    const response = await post('/activity/lottery/multi-draw', data)
    return response
  } catch (error) {
    console.error('Failed to fetch multiply draw:', error)
    throw error
  }
}

// 获取我的中奖记录(默认最新100条)
export const drawRecords = async (activityCode) => {
  try {
    const response = await get(
      `/activity/lottery/my-records?pageSize=100&activityCode=${activityCode}`,
    )
    return response
  } catch (error) {
    console.error('Failed to fetch get draw records:', error)
    console.error('error:' + error.response.errorMsg)
    throw error
  }
}

// 查询抽奖背包流水(水晶&碎片)
export const apiGetDrawBackpackLogs = async (data) => {
  try {
    const response = await post(`/activity/lottery/backpack/logs`, data)
    return response
  } catch (error) {
    console.error('Failed to fetch get draw backpack logs:', error)
    console.error('error:' + error.response.errorMsg)
    throw error
  }
}

// 用碎片兑换资源
export const exchangeGood = async (data) => {
  try {
    const response = await post(`/activity/week-star/exchange`, data)
    return response
  } catch (error) {
    console.error('Failed to exchange goods:', error)
    console.error('error:' + error.response.errorMsg)
    throw error
  }
}

// 查询签到状态
export const apiGetMySignInStatus = async () => {
  try {
    const response = await get(`/signin/status`)
    return response
  } catch (error) {
    console.error('Failed to fetch get my sign in status:', error)
    console.error('error:' + error.response.errorMsg)
    throw error
  }
}

// 执行签到
export const apiPostMySignIn = async (data) => {
  try {
    const response = await post(`/signin/check-in`, data)
    return response
  } catch (error) {
    console.error('Failed to sign in:', error)
    console.error('error:' + error.response.errorMsg)
    throw error
  }
}

// 补签
export const apiPostMySignInSupplement = async (data) => {
  try {
    const response = await post(`/signin/supplement`, data)
    return response
  } catch (error) {
    console.error('Failed to sign in:', error)
    console.error('error:' + error.response.errorMsg)
    throw error
  }
}
