/**
 * 格式化日期时间为本地字符串
 * @param {string|Date} date
 * @returns {string}
 */
export function formatDateTime(date) {
  if (!date) return '-'
  const d = typeof date === 'string' ? new Date(date) : date
  if (Number.isNaN(d.getTime())) return '-'
  return d.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit',
  })
}

/**
 * 根据房间创建/更新时间计算在线时长描述
 * @param {string} status - RoomStatus: waiting | playing | finished
 * @param {string|Date} createdAt
 * @param {string|Date} updatedAt
 * @returns {string}
 */
export function formatRoomDuration(status, createdAt, updatedAt) {
  if (!createdAt) return '-'
  const start = typeof createdAt === 'string' ? new Date(createdAt) : createdAt
  const end = status === 'playing' ? new Date() : (updatedAt ? (typeof updatedAt === 'string' ? new Date(updatedAt) : updatedAt) : start)
  if (Number.isNaN(start.getTime())) return '-'
  const ms = Math.max(0, end - start)
  const sec = Math.floor(ms / 1000)
  const min = Math.floor(sec / 60)
  const hour = Math.floor(min / 60)
  if (hour > 0) return `${hour}小时${min % 60}分钟`
  if (min > 0) return `${min}分钟`
  return `${sec}秒`
}
