import request from '@/utils/http'
import { normalizeLotteryPoolOption, type LotteryPoolOption } from '@/views/plugin/dice/utils/lotteryPoolDisplay'

/**
 * 玩家抽奖记录 API接口
 */
export default {
  /**
   * 获取数据列表
   * @param params 搜索参数
   * @returns 数据列表
   */
  list(params: Record<string, any>) {
    return request.get<Api.Common.ApiPage>({
      url: '/core/dice/play_record/DicePlayRecord/index',
      params
    })
  },

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

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

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

  /** 获取玩家选项（id、username） */
  getPlayerOptions(params?: Record<string, unknown>) {
    return request.get<{ id: number; username: string }[]>({
      url: '/core/dice/play_record/DicePlayRecord/getPlayerOptions',
      params
    })
  },

  /** 获取彩金池配置选项（含奖池名称） */
  async getLotteryConfigOptions(params?: Record<string, unknown>): Promise<LotteryPoolOption[]> {
    const res = await request.get<any>({
      url: '/core/dice/play_record/DicePlayRecord/getLotteryConfigOptions',
      params
    })
    const rows = (Array.isArray(res) ? res : (res?.data ?? [])) as Array<Record<string, unknown>>
    return rows.map((r) => normalizeLotteryPoolOption(r))
  }
}
