import { request, authRequest } from '../utils/request'

export function wechatLogin(code, invite_code) {
  const data = invite_code ? { code, invite_code } : { code }
  return request({ url: '/api/app/users/weixin/login', method: 'POST', data })
}

export function getInventory(user_id, page = 1, page_size = 20) {
  return authRequest({ url: `/api/app/users/${user_id}/inventory`, method: 'GET', data: { page, page_size } })
}

export function bindPhone(user_id, code, extraHeader = {}) {
  return authRequest({ url: `/api/app/users/${user_id}/phone/bind`, method: 'POST', data: { code }, header: extraHeader })
}

export function getUserStats(user_id) {
  return authRequest({ url: `/api/app/users/${user_id}/stats`, method: 'GET' })
}

export function getPointsBalance(user_id) {
  return authRequest({ url: `/api/app/users/${user_id}/points/balance`, method: 'GET' })
}

export function getPointsRecords(user_id, page = 1, page_size = 20) {
  return authRequest({ url: `/api/app/users/${user_id}/points`, method: 'GET', data: { page, page_size } })
}

export function getOrders(user_id, status, page = 1, page_size = 20) {
  const data = { page, page_size }
  if (status) data.status = status
  return authRequest({ url: `/api/app/users/${user_id}/orders`, method: 'GET', data })
}

// 获取订单详情
export function getOrderDetail(order_id) {
  return authRequest({ url: `/api/app/orders/${order_id}`, method: 'GET' })
}

// 取消订单
export function cancelOrder(order_id, reason = '') {
  const data = reason ? { reason } : {}
  return authRequest({ url: `/api/app/orders/${order_id}/cancel`, method: 'POST', data })
}

export function listAddresses(user_id) {
  return authRequest({ url: `/api/app/users/${user_id}/addresses`, method: 'GET' })
}

export function addAddress(user_id, payload) {
  return authRequest({ url: `/api/app/users/${user_id}/addresses`, method: 'POST', data: payload })
}

export function updateAddress(user_id, address_id, payload) {
  return authRequest({ url: `/api/app/users/${user_id}/addresses/${address_id}`, method: 'PUT', data: payload })
}

export function deleteAddress(user_id, address_id) {
  return authRequest({ url: `/api/app/users/${user_id}/addresses/${address_id}`, method: 'DELETE' })
}

export function setDefaultAddress(user_id, address_id) {
  return authRequest({ url: `/api/app/users/${user_id}/addresses/${address_id}/default`, method: 'PUT' })
}

export function getActivityDetail(activity_id) {
  return authRequest({ url: `/api/app/activities/${activity_id}`, method: 'GET' })
}

export function getActivityIssues(activity_id) {
  return authRequest({ url: `/api/app/activities/${activity_id}/issues`, method: 'GET' })
}

export function getActivityIssueRewards(activity_id, issue_id) {
  return authRequest({ url: `/api/app/activities/${activity_id}/issues/${issue_id}/rewards`, method: 'GET' })
}

export function getIssueDrawLogs(activity_id, issue_id) {
  return authRequest({ url: `/api/app/activities/${activity_id}/issues/${issue_id}/draw_logs`, method: 'GET' })
}

export function drawActivityIssue(activity_id, issue_id) {
  return authRequest({ url: `/api/app/activities/${activity_id}/issues/${issue_id}/draw`, method: 'POST' })
}

export function getIssueChoices(activity_id, issue_id) {
  return authRequest({ url: `/api/app/activities/${activity_id}/issues/${issue_id}/choices`, method: 'GET' })
}

export function getProductDetail(product_id) {
  return authRequest({ url: `/api/app/products/${product_id}`, method: 'GET' })
}

export function redeemInventory(user_id, ids) {
  return authRequest({ url: `/api/app/users/${user_id}/inventory/redeem`, method: 'POST', data: { inventory_ids: ids } })
}

export function requestShipping(user_id, ids) {
  return authRequest({ url: `/api/app/users/${user_id}/inventory/request-shipping`, method: 'POST', data: { inventory_ids: ids } })
}

export function cancelShipping(user_id, batch_no) {
  return authRequest({ url: `/api/app/users/${user_id}/inventory/cancel-shipping`, method: 'POST', data: { batch_no } })
}

export function createAddressShare(user_id, inventory_id) {
  return authRequest({ url: `/api/app/users/${user_id}/inventory/address-share/create`, method: 'POST', data: { inventory_id } })
}

export function revokeAddressShare(user_id, inventory_id) {
  return authRequest({ url: `/api/app/users/${user_id}/inventory/address-share/revoke`, method: 'POST', data: { inventory_id } })
}

export function getItemCards(user_id, status, page = 1, page_size = 20) {
  const data = { page, page_size }
  if (status !== undefined) data.status = status
  return authRequest({ url: `/api/app/users/${user_id}/item_cards`, method: 'GET', data })
}

export function getUserCoupons(user_id, status, page = 1, page_size = 20) {
  const data = { page, page_size }
  if (status !== undefined) data.status = status
  return authRequest({ url: `/api/app/users/${user_id}/coupons`, method: 'GET', data })
}

export function getCouponStats(user_id) {
  return authRequest({ url: `/api/app/users/${user_id}/coupons/stats`, method: 'GET' })
}

export function getCouponUsage(user_id, user_coupon_id, page = 1, page_size = 20) {
  return authRequest({ url: `/api/app/users/${user_id}/coupons/${user_coupon_id}/usage`, method: 'GET', data: { page, page_size } })
}

export function redeemCouponByPoints(user_id, coupon_id) {
  return authRequest({ url: `/api/app/users/${user_id}/points/redeem-coupon`, method: 'POST', data: { coupon_id } })
}

export function redeemCoupon(user_id, code) {
  return authRequest({ url: `/api/app/users/${user_id}/coupons/redeem`, method: 'POST', data: { code } })
}

export function joinLottery(data) {
  return authRequest({ url: '/api/app/lottery/join', method: 'POST', data })
}

export function createWechatOrder(data) {
  return authRequest({ url: '/api/app/pay/wechat/jsapi/preorder', method: 'POST', data })
}

export function getLotteryResult(order_no) {
  return authRequest({ url: '/api/app/lottery/result', method: 'GET', data: { order_no } })
}

export function getUserPoints(user_id, page = 1, page_size = 20) {
  return authRequest({ url: `/api/app/users/${user_id}/points`, method: 'GET', data: { page, page_size } })
}

export function redeemProductByPoints(user_id, product_id, quantity) {
  return authRequest({ url: `/api/app/users/${user_id}/points/redeem-product`, method: 'POST', data: { product_id, quantity } })
}

export function redeemItemCardByPoints(user_id, item_card_id, quantity = 1) {
  return authRequest({ url: `/api/app/users/${user_id}/points/redeem-item-card`, method: 'POST', data: { item_card_id, quantity } })
}

export function getStoreItems(kind = 'product', page = 1, page_size = 20) {
  return authRequest({ url: '/api/app/store/items', method: 'GET', data: { kind, page, page_size } })
}

export function getTasks(page = 1, page_size = 20) {
  return authRequest({ url: '/api/app/task-center/tasks', method: 'GET', data: { page, page_size } })
}

export function getTaskProgress(task_id, user_id) {
  return authRequest({ url: `/api/app/task-center/tasks/${task_id}/progress/${user_id}`, method: 'GET' })
}

export function claimTaskReward(task_id, user_id, tier_id) {
  return authRequest({ url: `/api/app/task-center/tasks/${task_id}/claim/${user_id}`, method: 'POST', data: { tier_id } })
}

export function getShipments(user_id, page = 1, page_size = 20) {
  return authRequest({ url: `/api/app/users/${user_id}/shipments`, method: 'GET', data: { page, page_size } })
}

// 获取用户邀请记录
export function getUserInvites(user_id, page = 1, page_size = 20) {
  return authRequest({ url: `/api/app/users/${user_id}/invites`, method: 'GET', data: { page, page_size } })
}

// ============================================
// 兼容性适配接口 (适配 pages/mine/index.vue)
// ============================================

export function getUserInfo() {
  const user_info = uni.getStorageSync('user_info')
  if (user_info) return Promise.resolve(user_info)
  return authRequest({ url: '/api/app/users/info', method: 'GET' })
}

export const getUserTasks = getTasks
export function getInviteRecords(page = 1, page_size = 20) {
  const user_id = uni.getStorageSync('user_id')
  return getUserInvites(user_id, page, page_size)
}

// ============================================
// 对对碰游戏 (Matching Game) 接口
// ============================================

/**
 * 开始游戏
 * @param {number} issue_id - 对应的活动期次ID
 */
export function startMatchingGame(issue_id) {
  return authRequest({ url: '/api/app/matching/start', method: 'POST', data: { issue_id } })
}

/**
 * 执行配对 (下一轮)
 * @param {string} game_id - start接口返回的游戏ID
 */
export function playMatchingGame(game_id) {
  return authRequest({ url: '/api/app/matching/play', method: 'POST', data: { game_id } })
}

/**
 * 获取所有启用的卡牌配置
 */
export function getMatchingCardTypes() {
  return authRequest({ url: '/api/app/matching/card_types', method: 'GET' })
}

export function createMatchingPreorder({ issue_id, position, coupon_id = 0, item_card_id = 0 }) {
  return authRequest({
    url: '/api/app/matching/preorder',
    method: 'POST',
    data: { issue_id, position, coupon_id, item_card_id }
  })
}

export function checkMatchingGame(game_id, total_pairs) {
  if (game_id && typeof game_id === 'object') {
    total_pairs = game_id.total_pairs
    game_id = game_id.game_id
  }
  return authRequest({
    url: '/api/app/matching/check',
    method: 'POST',
    data: { game_id, total_pairs }
  })
}

/**
 * 支付成功后获取游戏数据
 * @param {string} game_id - 游戏ID
 */
export function getMatchingGameCards(game_id) {
  return authRequest({
    url: '/api/app/matching/cards',
    method: 'GET',
    data: { game_id }
  })
}
