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, params?: Record<string, unknown>) {
    return request.get<Api.Common.ApiData>({
      url: '/core/dice/reward/DiceReward/weightRatioList',
      params: { direction, ...(params || {}) }
    })
  },

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

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

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

  /**
   * 一键测试权重：创建测试记录并启动后台执行
   * chain_free_mode=true：仅模拟付费次数；付费抽到再来一次则插入免费抽奖（同底注、付费金额 0）
   */
  startWeightTest(params: {
    ante?: number
    ante_config_id?: number
    lottery_config_id?: number
    paid_lottery_config_id?: number
    free_lottery_config_id?: number
    chain_free_mode?: boolean
    kill_mode_enabled?: boolean
    test_safety_line?: 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 }
    })
  }
}
