import request, { type ApiResponse, type PagedResult } from '@/utils/request'

const PRIZE_LEVEL_BASE_URL = '/admin/business/prize-levels'

/** 奖品等级列表请求 */
export interface PrizeLevelListRequest {
  page?: number
  pageSize?: number
  title?: string
}

/** 奖品等级响应 */
export interface PrizeLevelResponse {
  id: number
  title: string
  pro: number
  imgUrl?: string
  color?: string
  specialImgUrl?: string
  sort: number
  updatedAt?: string
  prizeCount: number
}

/** 创建奖品等级请求 */
export interface PrizeLevelCreateRequest {
  title: string
  pro: number
  imgUrl?: string
  color?: string
  specialImgUrl?: string
  sort: number
}

/** 更新奖品等级请求 */
export interface PrizeLevelUpdateRequest {
  title: string
  pro: number
  imgUrl?: string
  color?: string
  specialImgUrl?: string
  sort: number
}

/** 奖品等级选项 */
export interface PrizeLevelOptionResponse {
  id: number
  title: string
  color?: string
}

/** 概率统计 */
export interface PrizeLevelStatsResponse {
  totalProbability: number
}

/**
 * 获取奖品等级列表
 */
export function getPrizeLevels(params: PrizeLevelListRequest): Promise<ApiResponse<PagedResult<PrizeLevelResponse>>> {
  return request({
    url: PRIZE_LEVEL_BASE_URL,
    method: 'get',
    params
  })
}

/**
 * 获取奖品等级详情
 */
export function getPrizeLevel(id: number): Promise<ApiResponse<PrizeLevelResponse>> {
  return request({
    url: `${PRIZE_LEVEL_BASE_URL}/${id}`,
    method: 'get'
  })
}

/**
 * 创建奖品等级
 */
export function createPrizeLevel(data: PrizeLevelCreateRequest): Promise<ApiResponse<{ id: number }>> {
  return request({
    url: PRIZE_LEVEL_BASE_URL,
    method: 'post',
    data
  })
}

/**
 * 更新奖品等级
 */
export function updatePrizeLevel(id: number, data: PrizeLevelUpdateRequest): Promise<ApiResponse<string>> {
  return request({
    url: `${PRIZE_LEVEL_BASE_URL}/${id}`,
    method: 'put',
    data
  })
}

/**
 * 删除奖品等级
 */
export function deletePrizeLevel(id: number): Promise<ApiResponse<string>> {
  return request({
    url: `${PRIZE_LEVEL_BASE_URL}/${id}`,
    method: 'delete'
  })
}

/**
 * 获取奖品等级选项（用于下拉选择）
 */
export function getPrizeLevelOptions(): Promise<ApiResponse<PrizeLevelOptionResponse[]>> {
  return request({
    url: `${PRIZE_LEVEL_BASE_URL}/options`,
    method: 'get'
  })
}

/**
 * 获取概率统计
 */
export function getPrizeLevelStats(): Promise<ApiResponse<PrizeLevelStatsResponse>> {
  return request({
    url: `${PRIZE_LEVEL_BASE_URL}/stats`,
    method: 'get'
  })
}

/**
 * 根据盒子类型获取奖品等级选项
 * @param goodsType 盒子类型
 * @param isChildPrize 是否子奖品（宝箱内奖品）
 */
export function getPrizeLevelOptionsByType(goodsType: number, isChildPrize: boolean = false): Promise<ApiResponse<PrizeLevelOptionResponse[]>> {
  return request({
    url: `${PRIZE_LEVEL_BASE_URL}/options-by-type`,
    method: 'get',
    params: { goodsType, isChildPrize }
  })
}
