import axios from 'axios'

export async function sendStartLepao(account) {
    const params = new URLSearchParams()
    params.append('account', account)

    const endpoint = "http://222.178.152.79:100/api_v1/startSport"
    const res = await axios.post(endpoint, params, {
        proxy: false,
        headers: {
            "User-Agent": 'zhong gong le pao/1.7 (iPhone; iOS 26.2; Scale/3.00)'
        }
    })

    const data = res.data
    if (!data || data.status !== 1 || !data.data || !data.data.id || !data.data.startTime) {
        console.log(`提交乐跑任务失败！${data?.message ?? "未知错误"}`)
        throw new Error(data?.message ?? "提交乐跑任务失败！请联系客服或稍后再试")
    }
    return { task_id: data.data.id, startTime: data.data.startTime }
}

export async function sendStopLepao(data) {
    const params = new URLSearchParams()
    params.append('account', data.account)
    params.append('calorie', data.calorie)
    params.append('distance', data.distance)
    params.append('distribution', data.distribution)
    params.append('duration', data.duration)
    params.append('endTime', data.endTime)
    params.append('id', data.id)
    params.append('maxDistribution', '0.00')
    params.append('pathLine', data.pathLine)
    params.append('startTime', data.startTime)
    params.append('str1', 'iPhone')

    const endpoint = "http://222.178.152.79:100/api_v1/endSport"
    const res = await axios.post(endpoint, params, {
        proxy: false,
        headers: {
            "User-Agent": 'zhong gong le pao/1.7 (iPhone; iOS 26.2; Scale/3.00)'
        }
    })

    const resData = res.data
    console.log(`${data.account}乐跑结束返回数据：${JSON.stringify(resData)}`)
    if (!resData || resData.status !== 1 || !resData.data) {
        console.log(`乐跑任务进行失败！${resData?.message ?? "未知错误"}`)
        throw new Error(resData?.message ?? "乐跑任务进行失败！请联系客服或稍后再试")
    }
    return resData.data
}

export async function lepaoUserInfo(student_num) {
    const params = new URLSearchParams()
    params.append('account', student_num)

    const endpoint = "http://222.178.152.79:100/api_v1/getUserInfo"
    const res = await axios.post(endpoint, params, {
        proxy: false,
        headers: {
            "User-Agent": 'zhong gong le pao/1.7 (iPhone; iOS 26.2; Scale/3.00)'
        }
    })
    const data = res.data
    if (!data || data.status !== 1 || !data.data || !data.data.id || !data.data.nickName || !data.data.department || !data.data.frequency) {
        console.log(`获取乐跑用户信息失败！${data?.message ?? "未知错误"}`)
        throw new Error(data?.message ?? "无法获取用户信息，请联系客服或稍后再试")
    }
    return data.data
}

export async function lepaoAuth(student_num, password) {
    const endpoint = "http://222.178.152.79:100/api_v1/login"
    const params = new URLSearchParams()
    params.append('password', password)
    params.append('account', student_num)

    const res = await axios.post(endpoint, params, {
        proxy: false,
        headers: {
            "User-Agent": 'zhong gong le pao/1.7 (iPhone; iOS 26.2; Scale/3.00)'
        }
    })
    const data = res.data
    if (!data || data.status !== 1) {
        throw new Error(data?.message ?? "无法验证乐跑账号，请联系客服或稍后再试")
    }
    return true
}
