import request from '@/utils/http'

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

  /**
   * 获取 DiceLotteryPoolConfig 列表数据，含 id、name、type、t1_weight～t5_weight，用于一键测试权重档位类型下拉
   * type：0=付费抽奖券，1=免费抽奖券；付费默认选 type=0，免费默认选 type=1
   */
  async getOptions(): Promise<
    Array<{
      id: number
      name: string
      type: number
      t1_weight: number
      t2_weight: number
      t3_weight: number
      t4_weight: number
      t5_weight: number
    }>
  > {
    const res = await request.get<any>({
      url: '/core/dice/lottery_pool_config/DiceLotteryPoolConfig/getOptions'
    })
    const rows = Array.isArray(res) ? res : (Array.isArray((res as any)?.data) ? (res as any).data : [])
    if (!Array.isArray(rows)) return []
    return rows.map((r: any) => ({
      id: Number(r.id),
      name: String(r.name ?? r.id ?? ''),
      type: Number(r.type ?? 0),
      t1_weight: Number(r.t1_weight ?? 0),
      t2_weight: Number(r.t2_weight ?? 0),
      t3_weight: Number(r.t3_weight ?? 0),
      t4_weight: Number(r.t4_weight ?? 0),
      t5_weight: Number(r.t5_weight ?? 0)
    }))
  },

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

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

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

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

  /**
   * 获取当前彩金池（Redis 实例化，无则按 type=0 创建），含 profit_amount 实时值
   */
  getCurrentPool() {
    return request.get<{
      id: number
      name: string
      safety_line: number
      t1_weight: number
      t2_weight: number
      t3_weight: number
      t4_weight: number
      t5_weight: number
      profit_amount: number
    }>({
      url: '/core/dice/lottery_pool_config/DiceLotteryPoolConfig/getCurrentPool'
    })
  },

  /**
   * 更新当前彩金池：仅 safety_line、t1_weight～t5_weight，不可改 profit_amount
   */
  updateCurrentPool(params: {
    safety_line?: number
    t1_weight?: number
    t2_weight?: number
    t3_weight?: number
    t4_weight?: number
    t5_weight?: number
  }) {
    return request.post<any>({
      url: '/core/dice/lottery_pool_config/DiceLotteryPoolConfig/updateCurrentPool',
      data: params
    })
  }
}
