/** Formats a minute count as a short clinician-facing duration. */
export function formatDurationMinutes(minutes) {
  if (minutes == null || Number.isNaN(Number(minutes))) return '—'
  const total = Math.max(0, Number(minutes))
  if (total < 60) {
    const m = Math.round(total)
    return `${m} minute${m === 1 ? '' : 's'}`
  }
  const hoursExact = total / 60
  if (Math.abs(hoursExact - Math.round(hoursExact)) < 0.05) {
    const h = Math.round(hoursExact)
    return `${h} hour${h === 1 ? '' : 's'}`
  }
  const h = Math.floor(total / 60)
  const m = Math.round(total % 60)
  if (m === 0) return `${h} hour${h === 1 ? '' : 's'}`
  return `${h}h ${m}m`
}

/** Scenario duration pill: "8 hours of monitoring". */
export function formatMonitoringDuration(durationMinutes) {
  return `${formatDurationMinutes(durationMinutes)} of monitoring`
}

/** Wall-clock time for a scenario at a given speed multiplier. */
export function formatWallClockForSpeed(durationMinutes, speed) {
  if (durationMinutes == null || !speed) return '—'
  return formatDurationMinutes(Number(durationMinutes) / Number(speed))
}

/** Simulated clock: "03:20 of 08:00 elapsed". */
export function formatSimClock(lastOffsetMinutes, totalOffsetMinutes) {
  const fmt = (mins) => {
    const total = Math.max(0, Math.floor(Number(mins) || 0))
    const h = Math.floor(total / 60)
    const m = total % 60
    return `${String(h).padStart(2, '0')}:${String(m).padStart(2, '0')}`
  }
  return `${fmt(lastOffsetMinutes)} of ${fmt(totalOffsetMinutes)} elapsed`
}

/** Wall-clock elapsed from elapsedRealSeconds. */
export function formatElapsedReal(seconds) {
  const s = Math.max(0, Math.floor(Number(seconds) || 0))
  const m = Math.floor(s / 60)
  const rem = s % 60
  if (m >= 60) {
    const h = Math.floor(m / 60)
    return `${h}h ${m % 60}m`
  }
  return `${m}:${String(rem).padStart(2, '0')}`
}

export function formatSpeedBadge(speed) {
  if (speed == null) return '—'
  return `${speed}×`
}

export function runStatusLabel(status) {
  const map = {
    PENDING: 'Pending',
    RUNNING: 'Running',
    COMPLETED: 'Completed',
    CANCELLED: 'Cancelled',
    FAILED: 'Failed',
  }
  return map[status] ?? status ?? '—'
}

export function runStatusVariant(status) {
  if (status === 'FAILED') return 'critical'
  if (status === 'CANCELLED') return 'warning'
  if (status === 'RUNNING') return 'success'
  if (status === 'COMPLETED') return 'success'
  return 'info'
}

const TAG_CHIP_CLASS = {
  sepsis: 'bg-red-100 text-red-800 dark:bg-red-900/30 dark:text-red-300',
  control: 'bg-emerald-100 text-emerald-800 dark:bg-emerald-900/30 dark:text-emerald-300',
  'mimic-iv': 'bg-violet-100 text-violet-800 dark:bg-violet-900/30 dark:text-violet-300',
  'real-data': 'bg-violet-100 text-violet-800 dark:bg-violet-900/30 dark:text-violet-300',
  stable: 'bg-sky-100 text-sky-800 dark:bg-sky-900/30 dark:text-sky-300',
  baseline: 'bg-sky-100 text-sky-800 dark:bg-sky-900/30 dark:text-sky-300',
}

export function tagChipClass(tag) {
  return TAG_CHIP_CLASS[tag]
    ?? 'bg-gray-100 text-gray-700 dark:bg-gray-800 dark:text-gray-300'
}

export function isControlTag(tag) {
  return tag === 'control'
}

export function isRealDataTag(tag) {
  return tag === 'real-data' || tag === 'mimic-iv'
}
