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

export type LotteryPoolConfigOption = LotteryPoolOption & {
  t1_weight: number
  t2_weight: number
  t3_weight: number
  t4_weight: number
  t5_weight: number
}

/** 规范化接口返回的彩金池配置（含权重） */
export function parseLotteryPoolConfigOption(raw: Record<string, unknown>): LotteryPoolConfigOption {
  const base = normalizeLotteryPoolOption(raw)
  return {
    ...base,
    t1_weight: Number(raw.t1_weight ?? 0),
    t2_weight: Number(raw.t2_weight ?? 0),
    t3_weight: Number(raw.t3_weight ?? 0),
    t4_weight: Number(raw.t4_weight ?? 0),
    t5_weight: Number(raw.t5_weight ?? 0)
  }
}

/**
 * 色子奖池配置 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、t1_weight～t5_weight，用于一键测试权重档位类型下拉
   * name 映射：default=原 type=0，killScore=原 type=1，up=原 type=2
   */
  async getOptions(params?: Record<string, unknown>): Promise<LotteryPoolConfigOption[]> {
    const res = await request.get<any>({
      url: '/core/dice/lottery_pool_config/DiceLotteryPoolConfig/getOptions',
      params
    })
    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: Record<string, unknown>) => parseLotteryPoolConfigOption(r))
  },

  /**
   * 读取数据
   * @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(params?: { dept_id?: number }) {
    return request.get<{
      id: number
      name: string
      safety_line: number
      kill_enabled: 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',
      params
    })
  },

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

  /**
   * 重置当前彩金池的玩家累计盈利（profit_amount 置为 0）
   */
  resetProfitAmount(params?: { dept_id?: number }) {
    return request.post<any>({
      url: '/core/dice/lottery_pool_config/DiceLotteryPoolConfig/resetProfitAmount',
      data: params || {}
    })
  }
}
