const path = require('path')

const defaultJkes = {
    apiBase: 'https://jkes.smart-campus.com.cn:50077/api',
    userAgent:
        'Mozilla/5.0 (iPhone; CPU iPhone OS 18_7 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.69(0x18004539) NetType/WIFI Language/zh_CN',
    referer: 'https://servicewechat.com/wxbd35ecd93e780082/11/page-frame.html',
    tlsRejectUnauthorized: true,
    requestTimeoutMs: 30000,
    gpsAltitude: 269.75,
    gpsDefaultAccuracy: 6,
    /** 自动乐跑随机配速：每公里秒数 */
    paceRandomMinSecPerKm: 180,
    paceRandomMaxSecPerKm: 600,
    /** 自动乐跑单次最大公里（用于月底追目标） */
    autoSingleRunMaxKm: 10,
    /** 自动乐跑距离步进（公里） */
    autoDistanceStepKm: 0.1
}

let cached

function getJkesSettings() {
    if (cached) return cached
    try {
        const cfg = require(path.join(__dirname, '../../config.json'))
        cached = { ...defaultJkes, ...(cfg.jkes || {}) }
    } catch {
        cached = { ...defaultJkes }
    }
    return cached
}

function normalizeApiBase(raw) {
    let u = String(raw || defaultJkes.apiBase).trim()
    if (u.startsWith('http://')) {
        u = `https://${u.slice('http://'.length)}`
    }
    return u.replace(/\/$/, '')
}

module.exports = {
    getJkesSettings,
    normalizeApiBase,
    defaultJkes
}
