import request from '@/utils/request'
import { AI_REQUEST_CONFIG, withAiRequestTimeout } from './aiRequest'
import { requestAiAnalysis } from './ai'

const normalizeLotteryModuleResult = (res: any) => ({
    ...(res?.data || {}),
    remain_count: res?.remain_count,
    unlocked: res?.unlocked,
    charged: res?.charged
})

/**
 * @description 开奖列表
 */
export function getLotteryDrawList(data: {
    category_id?: number
    page_no?: number
    page_size?: number
}) {
    return request.get({ url: '/lottery/drawList', data }, { withToken: false })
}

/**
 * @description 开奖详情
 */
export function getLotteryDrawDetail(data: { id: number }) {
    return request.get({ url: '/lottery/drawDetail', data }, { withToken: false })
}

/**
 * @description 各彩种最新开奖
 */
export function getLatestDraw(data?: { category_id?: number }) {
    return request.get({ url: '/lottery/latestDraw', data }, { withToken: false })
}

/**
 * @description 彩种游戏列表（一级分类+二级彩种）
 */
export function getLotteryGameList() {
    return request.get({ url: '/lottery/gameList' }, { withToken: false })
}

/**
 * @description 各彩种最新开奖（新表 draw_result）
 */
export function getLatestDrawResult(data?: { code?: string }) {
    return request.get({ url: '/lottery/latestDrawResult', data }, { withToken: false })
}

/**
 * @description 开奖历史列表（新表 draw_result）
 */
export function getDrawResultList(data: { code: string; page_no?: number; page_size?: number }) {
    return request.get({ url: '/lottery/drawResultList', data }, { withToken: false })
}

/**
 * @description AI预测分析
 */
export function getLotteryAiPredict(data: { category_id?: number; code?: string }) {
    return requestAiAnalysis({ type: 'lottery', ...data })
}

/**
 * @description AI分析 - 历史开奖数据
 */
export function getLotteryAiHistory(data: { code: string }) {
    return request.get({ url: '/lottery/aiHistory', data }, { withToken: false })
}

/**
 * @description AI分析 - 热冷号
 */
export function getLotteryAiHotCold(data: { code: string }) {
    return requestAiAnalysis({ type: 'lottery_module', module: 'hot_cold', ...data })
        .then(normalizeLotteryModuleResult)
}

/**
 * @description AI分析 - 趋势+推荐组合
 */
export function getLotteryAiTrend(data: { code: string }) {
    return requestAiAnalysis({ type: 'lottery_module', module: 'trend', ...data })
        .then(normalizeLotteryModuleResult)
}

/**
 * @description AI分析 - 统计数据
 */
export function getLotteryAiStats(data: { code: string }) {
    return requestAiAnalysis({ type: 'lottery_module', module: 'stats', ...data })
        .then(normalizeLotteryModuleResult)
}

/**
 * @description AI分析 - 波色+生肖（六合彩专属）
 */
export function getLotteryAiColorZodiac(data: { code: string }) {
    return requestAiAnalysis({ type: 'lottery_module', module: 'color_zodiac', ...data })
        .then(normalizeLotteryModuleResult)
}

/**
 * @description AI分析历史记录（按彩种+期号查询）
 */
export function getLotteryAiAnalysisHistory(data: { code: string; issue: string }) {
    return request.get({ url: '/lottery/aiAnalysisHistory', data }, { withToken: false })
}
