import type { AxiosProgressEvent, GenericAbortSignal } from 'axios'
import { post, get, put, patch } from '@/utils/request'
import { useAuthStore, useSettingStore } from '@/store'

export function fetchChatAPI<T = any>(
    prompt: string,
    options?: { conversationId?: string; parentMessageId?: string },
    signal?: GenericAbortSignal
) {
    return post<T>({
        url: '/chat/chat',
        data: { prompt, options },
        signal
    })
}

export function fetchChatConfig<T = any>() {
    return post<T>({
        url: '/chat/config'
    })
}

export function fetchChatAPIProcess<T = any>(params: {
    url?: string | null
    prompt: string
    options?: { conversationId?: string; parentMessageId?: string }
    signal?: GenericAbortSignal
    onDownloadProgress?: (progressEvent: AxiosProgressEvent) => void
}) {
    const settingStore = useSettingStore()
    const authStore = useAuthStore()

    let data: Record<string, any> = {
        prompt: params.prompt,
        options: params.options
    }

    console.log(authStore.isChatGPTAPI)

    // if (authStore.isChatGPTAPI) {
    data = {
        ...data,
        systemMessage: settingStore.systemMessage,
        temperature: settingStore.temperature,
        top_p: settingStore.top_p
    }
    // }

    return post<T>({
        url: params.url || '/chat/chat-process1',
        data,
        signal: params.signal,
        onDownloadProgress: params.onDownloadProgress
    })
}

export function fetchSession<T>() {
    return post<T>({
        url: '/chat/session'
    })
}

export function fetchVerify<T>(token: string) {
    return post<T>({
        url: '/verify',
        data: { token }
    })
}

export function fetchPhoneLogin<T>(phone: string | number, code: string | number, invitor?: string | number) {
    return post<T>({
        url: '/auth/phoneLogin',
        data: { phone, code, invitor }
    })
}

export function fetchMy<T>() {
    return get<T>({
        url: '/users/my'
    })
}

export function fetchSendVerify<T>(data: { phone: string | number; token: string; sig: string; sessionId: string }) {
    return post<T>({
        url: '/sms/sendVerify',
        data
    })
}

export function fetchOpenid<T>(code: string) {
    return get<T>({
        url: '/weixin/code2openid',
        data: { code }
    })
}

export function fetchJsapiSign<T>(url: string) {
    return post<T>({
        url: '/weixin/jsapiSign',
        data: { url }
    })
}

export function fetchPay<T>(openid: string) {
    return get<T>({
        url: '/weixin/pay',
        data: { openid }
    })
}

export function fetchRedirectUrl<T>(url: string, state?: string) {
    return post<T>({
        url: '/weixin/redirectUrl',
        data: { url, state }
    })
}

export function fetchMyMember<T>() {
    return get<T>({
        url: '/membership/get'
    })
}

export function fetchGetMemberships<T>() {
    return get<Array<T>>({
        url: '/membership/plans'
    })
}

export function fetchMembershipRenew<T>(planId: string | number) {
    return post<T>({
        url: '/membership/renew',
        data: {
            planId,
            type: 'JSAPI',
            openid: window.sessionStorage.getItem('openid')
        }
    })
}

export function fetchUserBalance<T>() {
    return get<T>({
        url: '/userBalance/get'
    })
}

export function fetchUserBalanceRecords<T>() {
    return get<Array<T>>({
        url: '/userBalance/records/get'
    })
}

export function fetchWithdraw<T>(data: any) {
    return post<Array<T>>({
        url: '/withdraw/apply',
        data: data
    })
}

export function fetchCommissionRecords<T>() {
    return get<Array<T>>({
        url: '/commission/records/get'
    })
}

export function fetchSystemMessage<T>() {
    return get<T>({
        url: '/sys-config/system_message'
    })
}

export function fetchMasks<T>(num: number) {
    return get<T>({
        url: '/admin/mask/getRandom/' + num
    })
}

export function fetchGetMask<T>(id: any) {
    return get<T>({
        url: `/admin/mask/get/${id}`
    })
}

export function fetchGetMoments<T>(data: any) {
    return post<T>({
        url: `/moments`,
        data: data
    })
}

export function fetchGetMomentsDetail<T>(id: any) {
    return get<T>({
        url: `/moments/get/${id}`
    })
}

export function fetchChatRoles<T>(num: number) {
    return get<T>({
        url: '/chatRole/getRandom/' + num
    })
}

export function fetchGetChatRole<T>(id: any) {
    return get<T>({
        url: '/chatRole/get/' + id
    })
}

export function fetchGetCompany<T>() {
    return get<T>({
        url: '/org/'
    })
}

export function fetchGetUserFileds<T>(userId: any, orgId: any) {
    return get<T>({
        url: '/form/getUserFields',
        data: {
            userId,
            orgId
        }
    })
}

export function fetchUserFileds<T>(datas: any) {
    return post<T>({
        url: '/form/userFileds',
        data: datas
    })
}

export function fetchPaperOrders<T>(data: any) {
    return post<T>({
        url: '/paper/orders',
        data
    })
}

export function fetchGenPaper<T>(id: number) {
    return post<T>({
        url: `/paper/orders/${id}/gen`
    })
}

export function fetchPaperResults<T>(data: any) {
    return post<T>({
        url: '/paper/results',
        data
    })
}

export function fetchCreatePaperOrder<T>(data: any) {
    return put<T>({
        url: '/paper/orders',
        data
    })
}

export function fetchUpdatePaperOrder<T>(id: number, data: any) {
    return patch<T>({
        url: `/paper/orders/${id}`,
        data
    })
}
