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/player/DicePlayer/index',
      params
    })
  },

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

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

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

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

  /**
   * 仅更新状态（列表内开关用）
   */
  updateStatus(params: { id: number | string; status: number }) {
    return request.put<any>({
      url: '/core/dice/player/DicePlayer/updateStatus',
      data: params
    })
  },

  /**
   * 获取玩家游戏链接
   */
  getGameUrl(params: { id: number | string; lang?: 'zh' | 'en' }) {
    return request.get<Api.Common.ApiData>({
      url: '/core/dice/player/DicePlayer/getGameUrl',
      params
    })
  },

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

  /**
   * 获取后台管理员选项（SystemUser），供 admin_id 下拉使用
   * @returns [ { id, username, realname, label } ]
   */
  async getSystemUserOptions(params?: Record<string, unknown>): Promise<
    Array<{ id: number; username: string; realname: string; label: string }>
  > {
    const res = await request.get<any>({
      url: '/core/dice/player/DicePlayer/getSystemUserOptions',
      params
    })
    const rows = (Array.isArray(res) ? res : (res?.data ?? [])) as Array<{
      id: number
      username: string
      realname: string
      label: string
    }>
    return rows.map((r) => ({
      id: Number(r.id),
      username: String(r.username ?? ''),
      realname: String(r.realname ?? ''),
      label: String(r.label ?? r.username ?? r.id ?? '')
    }))
  },

  /**
   * 超管：按渠道树状展示全部管理员；非超管：扁平列表
   */
  async getSystemUserTreeOptions(params?: Record<string, unknown>): Promise<
    Array<{
      id: number | string
      label: string
      disabled?: boolean
      children?: Array<{ id: number; username: string; realname: string; label: string }>
    }>
  > {
    const res = await request.get<any>({
      url: '/core/dice/player/DicePlayer/getSystemUserTreeOptions',
      params
    })
    const rows = (Array.isArray(res) ? res : (res?.data ?? [])) as Array<{
      id: number | string
      label: string
      disabled?: boolean
      children?: Array<{ id: number; username: string; realname: string; label: string }>
    }>
    return rows
  }
}
