/** Small formatting helpers shared across pages. */

export function fmtHoursDecimal(seconds: number | null | undefined): string {
  if (seconds == null) return '—'
  return (seconds / 3600).toFixed(2)
}

/** "HH:MM" — minutes rounded to nearest. */
export function fmtHoursMinutes(seconds: number | null | undefined): string {
  if (seconds == null) return '—'
  const totalMins = Math.round(seconds / 60)
  const h = Math.floor(totalMins / 60)
  const m = totalMins % 60
  return `${h}:${m.toString().padStart(2, '0')}`
}

/** "HH:MM:SS" — exact, never rounded. */
export function fmtHoursMinutesSeconds(
  seconds: number | null | undefined,
): string {
  if (seconds == null) return '—'
  const total = Math.max(0, Math.round(seconds))
  const h = Math.floor(total / 3600)
  const m = Math.floor((total % 3600) / 60)
  const s = total % 60
  return `${h}:${m.toString().padStart(2, '0')}:${s.toString().padStart(2, '0')}`
}

/** "$1,593.89" */
export function fmtUsd(amount: number | null | undefined): string {
  if (amount == null) return '—'
  return amount.toLocaleString('en-US', {
    style: 'currency',
    currency: 'USD',
    minimumFractionDigits: 2,
    maximumFractionDigits: 2,
  })
}

/** "$1,593.89 (113:51)" combo for the Summary cells. */
export function fmtMoneyHours(
  seconds: number | null | undefined,
  hourlyRate: number,
): string {
  if (seconds == null || seconds === 0) return '—'
  const dollars = (seconds / 3600) * hourlyRate
  return `${fmtUsd(dollars)} (${fmtHoursMinutes(seconds)})`
}

/** Convert a naive timestamp like '2026-04-25T14:10:47' to 'YYYY-MM-DD HH:MM'. */
export function fmtTimestampShort(ts: string | null | undefined): string {
  if (!ts) return '—'
  return ts.replace('T', ' ').slice(0, 16)
}
