export function formatDate(value, withTime = false) {
  if (!value) return '—';
  const date = new Date(value);
  if (Number.isNaN(date.getTime())) return String(value);
  return new Intl.DateTimeFormat('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    ...(withTime ? { hour: '2-digit', minute: '2-digit', hour12: false } : {})
  }).format(date);
}

export function dateRange(start, end) {
  if (!start && !end) return '时间待发布';
  const startDate = start ? new Date(start) : null;
  const endDate = end ? new Date(end) : null;
  if (!startDate || Number.isNaN(startDate.getTime())) return formatDate(end);
  if (!endDate || Number.isNaN(endDate.getTime())) return formatDate(start);

  const day = value => new Intl.DateTimeFormat('zh-CN', {
    year: 'numeric', month: '2-digit', day: '2-digit'
  }).format(value);
  return `${day(startDate)} — ${day(endDate)}`;
}

export function registrationLabel(state) {
  return {
    open: '报名开放',
    upcoming: '即将开放',
    closed: '报名结束'
  }[state] || '已发布';
}

export function roleHome(user) {
  if (!user) return 'login';
  if (user.role === 'candidate') return 'candidate/dashboard';
  if (user.role === 'admission_school') return 'admission_school/dashboard';
  return 'admin/dashboard';
}
