import request from '@/utils/http'

/**
 * 奖励配置 API接口
 */
export default {
  /**
   * 获取数据列表
   * @param params 搜索参数
   * @returns 数据列表
   */
  list(params: Record<string, any>) {
    return request.get<Api.Common.ApiPage>({
      url: '/core/dice/reward_config/DiceRewardConfig/index',
      params
    })
  },

  /**
   * 读取数据
   * @param id 数据ID
   * @returns 数据详情
   */
  read(id: number | string) {
    return request.get<Api.Common.ApiData>({
      url: '/core/dice/reward_config/DiceRewardConfig/read?id=' + id
    })
  },

  /**
   * 创建数据
   * @param params 数据参数
   * @returns 执行结果
   */
  save(params: Record<string, any>) {
    return request.post<any>({
      url: '/core/dice/reward_config/DiceRewardConfig/save',
      data: params
    })
  },

  /**
   * 更新数据
   * @param params 数据参数
   * @returns 执行结果
   */
  update(params: Record<string, any>) {
    return request.put<any>({
      url: '/core/dice/reward_config/DiceRewardConfig/update',
      data: params
    })
  },

  /**
   * 删除数据
   * @param id 数据ID
   * @returns 执行结果
   */
  delete(params: Record<string, any>) {
    return request.del<any>({
      url: '/core/dice/reward_config/DiceRewardConfig/destroy',
      data: params
    })
  },

  /**
   * 批量更新奖励索引配置（第一页：id、grid_number、ui_text、real_ev、tier、remark）
   */
  batchUpdate(
    items: Array<{ id: number; grid_number?: number; ui_text?: string; real_ev?: number; tier?: string; remark?: string }>,
    extra?: Record<string, any>
  ) {
    return request.post<any>({
      url: '/core/dice/reward_config/DiceRewardConfig/batchUpdate',
      data: { items, ...(extra || {}) }
    })
  },

  /**
   * 按规则生成并保存奖励索引（需 dice:reward_config:index:tierRecommend 权限）
   */
  generateIndexByRules(
    items: Array<{
      id: number
      grid_number?: number
      ui_text?: string
      ui_text_en?: string
      real_ev?: number
      tier?: string
      remark?: string
    }>,
    extra?: Record<string, any>
  ) {
    return request.post<any>({
      url: '/core/dice/reward_config/DiceRewardConfig/generateIndexByRules',
      data: { items, ...(extra || {}) }
    })
  },

  /**
   * T1-T5、BIGWIN 权重配比：按档位分组获取配置列表
   */
  weightRatioList(params?: Record<string, unknown>) {
    return request.get<Api.Common.ApiData>({
      url: '/core/dice/reward_config/DiceRewardConfig/weightRatioList',
      params
    })
  },

  /**
   * T1-T5、BIGWIN 权重配比：批量更新顺时针/逆时针权重（写入 dice_reward）
   */
  /** 按 DiceReward 主键 id 批量更新 weight；items: [{ id, weight }, ...] */
  batchUpdateWeights(
    items: Array<{ id: number; weight: number }>,
    extra?: Record<string, unknown>
  ) {
    return request.post<any>({
      url: '/core/dice/reward_config/DiceRewardConfig/batchUpdateWeights',
      data: { items, ...(extra || {}) }
    })
  },

  /**
   * 大奖权重：按 grid_number 批量保存 BIGWIN 权重（无需 reward id，不存在则自动创建）
   */
  saveBigwinWeightsByGrid(
    items: Array<{ grid_number: number; weight: number }>,
    extra?: Record<string, unknown>
  ) {
    return request.post<any>({
      url: '/core/dice/reward_config/DiceRewardConfig/saveBigwinWeightsByGrid',
      data: { items, ...(extra || {}) }
    })
  },

  /**
   * 创建奖励对照：按当前奖励配置为顺时针(0)、逆时针(1)生成所有色子可能对应的 dice_reward 记录，权重默认 1，可在奖励对照页权重编辑中调整
   */
  createRewardReference(params?: Record<string, any>) {
    return request.post<{
      created_clockwise: number
      created_counterclockwise: number
      updated_clockwise: number
      updated_counterclockwise: number
    }>({
      url: '/core/dice/reward_config/DiceRewardConfig/createRewardReference',
      data: params || {}
    })
  },

  /**
   * 创建奖励对照（预览）：不写库，返回将要生成的对照与权重预览
   */
  createRewardReferencePreview(params?: Record<string, any>) {
    return request.post<any>({
      url: '/core/dice/reward_config/DiceRewardConfig/createRewardReferencePreview',
      data: params || {}
    })
  }
}
