import config from "@/config.js"
import useUserStore from '@/stores/useUserStore';

/**
 * @param url  String，请求的地址，默认：none
 * @param data  Object，请求的参数，默认：{}
 * @returns promise
 **/
export default function StreamRequest(url, data = {}, onDataReceived, onError, onComplete) {
    const userStore = useUserStore();
    const Authorization = userStore.token ? encodeURIComponent(userStore.token) : '';

    const headers = {
        "Authorization": Authorization,
        "Accept": "text/event-stream",
        "Content-Type": "application/json;charset=UTF-8"
    };
    return new Promise(async (resolve, reject) => {
        try {
            const response = await fetch(config.StreamBaseURl + url, {
                method: "POST",
                headers,
                body: JSON.stringify(data)
            });

            if (!response.ok) {
                throw new Error(`HTTP 错误: ${response.status}`);
            }

            const reader = response.body.getReader();
            const decoder = new TextDecoder("utf-8");

            let buffer = "";
            while (true) {
                const {
                    done,
                    value
                } = await reader.read();

                if (done) break;

                buffer += decoder.decode(value, {
                    stream: true
                });

                let lines = buffer.split("\n");
                buffer = lines.pop(); // 可能是不完整的 JSON 片段，留待下次解析
                for (let line of lines) {
                    if (line.startsWith("data: ")) {
                        const jsonData = line.slice(6).trim();
                        if (jsonData === "[DONE]") {
                            onComplete && onComplete();
                            resolve();
                            return;
                        }

                        try {
                            const parsedData = JSON.parse(jsonData);
                            const content = parsedData?.choices?.[0]?.delta?.content ??
                                parsedData?.choices?.[0]?.delta?.reasoning_content ??
                                "";
                            if (content) {
                                onDataReceived && onDataReceived(content);
                            }
                        } catch (e) {
                            console.error("JSON 解析失败:", e, "原始数据:", jsonData);
                        }
                    }
                }
            }

            onComplete && onComplete();
            resolve();
        } catch (error) {
            console.error("Stream 请求失败:", error);
            onError && onError(error);
            reject(error);
        }
    });
}


/**
 * @param url  String，请求的地址，默认：none
 * @param data  Object，请求的参数，默认：{}
 * @param method  String，请求的方式，默认：GET
 * @param loading  Boolean，是否需要loading ，默认：false
 * @param header  Object，headers，默认：{}
 * @returns promise
 **/
export function chatRequest(url, data = {}, method = 'GET', loading = false, headers = {}) {
    if (loading) {
        uni.showLoading({
            title: '请稍后',
            mask: true
        })
    }
    let Authorization = ''
    if (useUserStore().token) {
        Authorization = `${useUserStore().token}`
    }

    const header = headers || {};
    header["Authorization"] = encodeURIComponent(Authorization);
    return new Promise((resolve, reject) => {
        uni.request({
            url: config.StreamBaseURl + url,
            method: method,
            data: data,
            header,
            success: resData => {
                // 响应拦截
                if (resData.statusCode === 200) {
                    const {
                        code,
                        msg
                    } = resData.data
                    if (code === 200) {
                        resolve(resData.data)
                        return
                    }
                    uni.showToast({
                        title: msg,
                        icon: 'none'
                    })
                }
                if (resData.data?.code === 401 || resData.data?.code === 402) {
                    useUserStore().logOut()
                }
                const err = new Error('请求出现异常，请联系工作人员')
                err.error = resData
                reject(err)
            },
            fail: (err) => {
                reject(err)
            },
            complete: () => {
                if (loading) {
                    uni.hideLoading();
                }
            }
        });
    })
}