package chat.vojo.proxy.core

import java.util.Calendar
import java.util.Locale

fun formatBytes(b: Long): String {
    if (b < 1024) return "$b Б"
    val units = listOf("КБ", "МБ", "ГБ", "ТБ")
    var v = b / 1024.0
    var i = 0
    while (v >= 1024 && i < units.size - 1) { v /= 1024; i++ }
    return String.format(Locale.US, "%.1f %s", v, units[i])
}

fun formatUptime(since: Long, nowMs: Long): String {
    if (since <= 0) return "—"
    val s = ((nowMs - since) / 1000).coerceAtLeast(0)
    val h = s / 3600
    val m = (s % 3600) / 60
    val sec = s % 60
    return if (h > 0) String.format(Locale.US, "%d:%02d:%02d", h, m, sec)
    else String.format(Locale.US, "%02d:%02d", m, sec)
}

/** HH:MM for the timeline rail, matching the design's tabular-nums time column. */
fun formatClock(epochMs: Long): String {
    val c = Calendar.getInstance().apply { timeInMillis = epochMs }
    return String.format(Locale.US, "%02d:%02d", c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE))
}
