export async function getSessionId(requestBody: object) {
    try {
        const response = await fetch("http://ywpt.hx.cn/dmhx/get_sessionid", {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
            },
            body: JSON.stringify(requestBody),
        });

        // 首先检查响应内容类型
        const contentType = response.headers.get("content-type");
        if (!contentType || !contentType.includes("application/json")) {
            // 如果不是JSON，读取原始文本进行调试
            const rawText = await response.text();
            console.warn("服务器返回非JSON响应:", rawText);

            // 尝试解析可能的JSON响应（即使Content-Type不正确）
            try {
                const data = JSON.parse(rawText);
                if (response.ok && data.sessionid) {
                    return { success: true, sessionId: data.sessionid };
                } else {
                    return {
                        success: false,
                        error: data.message || "服务器返回非JSON格式",
                    };
                }
            } catch (parseError) {
                return {
                    success: false,
                    error: `服务器响应格式错误: ${rawText.substring(0, 100)}...`,
                };
            }
        }

        // 如果是JSON，正常解析
        const data = await response.json();

        if (response.ok && data.sessionid) {
            return { success: true, sessionId: data.sessionid };
        } else {
            return { success: false, error: data.message || "未知错误" };
        }
    } catch (error: any) {
        console.error("Error in getSessionId:", error);
        return { success: false, error: error.message };
    }
}

export async function sendMessage(requestBody: object) {
    try {
        const response = await fetch(`http://ywpt.hx.cn/dmhx/human`, {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
            },
            body: JSON.stringify(requestBody),
        });
        const data = await response.json();

        if (response.ok) {
            return { success: true };
        } else {
            return { success: false, error: data.message || "未知错误" };
        }
    } catch (error: any) {
        console.error("Error in sendMessage:", error);
        return { success: false, error: error.message };
    }
}

export async function getJob(requestBody: object) {
    try {
        const response = await fetch(
            `https://qd.zhaopinzao8dian.com/api/app/job/metaInfo`,
            {
                method: "POST",
                headers: {
                    "Content-Type": "application/json",
                },
                body: JSON.stringify(requestBody),
            },
        );
        const resData = await response.json();

        if (resData.code === 200) {
            return { success: true, data: resData.data };
        } else {
            throw new Error(resData.msg);
        }
    } catch (error: any) {
        console.error("Error in sendMessage:", error);
        return { success: false, error: error.message };
    }
}
