import request from '@/utils/http'

/**
 * 奖励对照（dice_reward）API
 */
export default {
  /**
   * 分页列表，按 direction 区分顺时针(0)/逆时针(1)
   * @param params direction(必), tier(选), page, limit, orderField, orderType
   */
  list(params: Record<string, any>) {
    return request.get<Api.Common.ApiPage>({
      url: '/core/dice/reward/DiceReward/index',
      params
    })
  },

  /**
   * 权重编辑弹窗：按档位分组获取当前方向的配置+权重（单方向）
   * @param direction 0=顺时针 1=逆时针
   */
  weightRatioList(direction: 0 | 1) {
    return request.get<Api.Common.ApiData>({
      url: '/core/dice/reward/DiceReward/weightRatioList',
      params: { direction }
    })
  },

  /**
   * 权重编辑弹窗：按档位分组获取配置+顺时针/逆时针权重（dice_reward 双方向）
   */
  weightRatioListWithDirection() {
    return request.get<Api.Common.ApiData>({
      url: '/core/dice/reward/DiceReward/weightRatioListWithDirection'
    })
  },

  /**
   * 权重编辑弹窗：按 DiceReward 主键 id 批量更新 weight
   * @param items [{ id: DiceReward.id, weight: 1-10000 }, ...]
   */
  batchUpdateWeights(items: Array<{ id: number; weight: number }>) {
    return request.post<any>({
      url: '/core/dice/reward/DiceReward/batchUpdateWeights',
      data: { items }
    })
  },

  /**
   * 权重编辑弹窗：批量更新当前方向的权重（单方向）
   */
  batchUpdateWeightsByDirection(direction: 0 | 1, items: Array<{ id: number; weight: number }>) {
    return request.post<any>({
      url: '/core/dice/reward/DiceReward/batchUpdateWeightsByDirection',
      data: { direction, items }
    })
  },

  /**
   * 一键测试权重：创建测试记录并启动后台执行，按付费/免费、顺逆方向交替抽奖
   * 可选 lottery_config_id；不选则传 paid_tier_weights / free_tier_weights（T1-T5）
   */
  startWeightTest(params: {
    lottery_config_id?: number
    paid_lottery_config_id?: number
    free_lottery_config_id?: number
    s_count?: number
    n_count?: number
    paid_s_count?: number
    paid_n_count?: number
    free_s_count?: number
    free_n_count?: number
    paid_tier_weights?: Record<string, number>
    free_tier_weights?: Record<string, number>
  }) {
    return request.post<{ record_id: number }>({
      url: '/core/dice/reward/DiceReward/startWeightTest',
      data: params
    })
  },

  /**
   * 查询一键测试进度
   */
  getTestProgress(recordId: number) {
    return request.get<{
      total_play_count: number
      over_play_count: number
      status: number
      remark: string | null
      result_counts: Record<number, number> | null
      tier_counts: Record<string, number> | null
    }>({
      url: '/core/dice/reward/DiceReward/getTestProgress',
      params: { record_id: recordId }
    })
  }
}
