function roundKm(value) {
    const n = Number(value)
    if (!Number.isFinite(n)) return 0
    return Math.round(n * 100) / 100
}

/**
 * 乐跑扣费：跑前按预计距离预扣，结算时按 min(预计, 实际) 计费。
 * 多扣退还差额，少扣不补收。
 * @returns {{ plannedKm: number, actualKm: number, billableKm: number, refundKm: number }}
 */
function resolveLepaoBilling(plannedKm, actualKm) {
    const planned = roundKm(plannedKm)
    const actual = roundKm(actualKm)
    const billableKm = roundKm(Math.min(planned, actual))
    const refundKm = roundKm(Math.max(0, planned - actual))
    return { plannedKm: planned, actualKm: actual, billableKm, refundKm }
}

module.exports = {
    roundKm,
    resolveLepaoBilling
}
