const mq = require('./index')
const { assertRunforgeTaskIngress, publishRunforgeTask } = require('./runforgeTaskMq')
const mqNames = require('./jkesMqNames')
const { buildStartRunPayload } = require('../jkes/prepareAutoRunTask')

/**
 * 将乐跑任务写入 MQ，由 Worker 执行（JKES）
 * @returns {Promise<boolean>} 是否投递成功
 */
async function enqueueLepaoStartRun(studentNum, logger, options = {}) {
    try {
        const channel = await mq.getChannel(mqNames.channelAccountAutorun)
        await assertRunforgeTaskIngress(channel, logger)
        const payload = buildStartRunPayload(studentNum, options, { prefix: 'lepao:account' })
        publishRunforgeTask(channel, payload)
        return true
    } catch (e) {
        logger?.error?.(`投递自动乐跑任务失败 ${studentNum}: ${e.message || e}`)
        return false
    }
}

module.exports = { enqueueLepaoStartRun }
