type DateInput = string | number | Date | null | undefined

export function formatDateTime(value: DateInput, fallback = '-') {
  if (!value) return fallback
  const date = value instanceof Date ? value : new Date(value)
  if (Number.isNaN(date.getTime())) {
    return typeof value === 'string' && value.trim() ? value.replace('T', ' ') : fallback
  }
  return [
    date.getFullYear(),
    pad(date.getMonth() + 1),
    pad(date.getDate()),
  ].join('-') + ` ${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`
}

export function formatDateMinute(value: DateInput, fallback = '-') {
  const formatted = formatDateTime(value, fallback)
  return formatted === fallback ? fallback : formatted.slice(0, 16)
}

function pad(value: number) {
  return String(value).padStart(2, '0')
}
