const { forwardRequest, briefUrlPath } = require('../../lib/ProxyForwardClient')

function jkesHttpLogLabel(traceId, taskId) {
    let s = ''
    if (traceId) s += `[${traceId}] `
    s += '[jkesHttp]'
    if (taskId) s += ` [${taskId}]`
    return s
}

/**
 * 对 JKES API 的 POST，经 RunForge-ProxyServer 转发（失败时由代理服务自动直连回退）。
 */
async function postJkes(url, data, options = {}) {
    const {
        headers = {},
        timeout = 30000,
        logger = null,
        outboundMode = 'auto',
        taskId = null,
        traceId = null,
        validateStatus
    } = options

    const logPrefix = jkesHttpLogLabel(traceId, taskId)
    logger?.info?.(`${logPrefix} POST ${briefUrlPath(url)} mode=${outboundMode}`)

    return forwardRequest({
        method: 'post',
        url,
        data,
        headers,
        timeout,
        outboundMode,
        validateStatus,
        logger,
        logPrefix,
        scene: 'jkes'
    })
}

module.exports = {
    postJkes
}
