/**
 * 日期工具函数
 */

/**
 * 格式化日期为 YYYY-MM-DD
 */
export function formatDate(date: Date | string): string {
  const d = typeof date === 'string' ? new Date(date) : date;
  const year = d.getFullYear();
  const month = String(d.getMonth() + 1).padStart(2, '0');
  const day = String(d.getDate()).padStart(2, '0');
  return `${year}-${month}-${day}`;
}

/**
 * 格式化日期时间为 YYYY-MM-DD HH:mm
 */
export function formatDateTime(dateTime: Date | string): string {
  if (!dateTime) return '';
  const d = typeof dateTime === 'string' ? new Date(dateTime) : dateTime;
  const year = d.getFullYear();
  const month = String(d.getMonth() + 1).padStart(2, '0');
  const day = String(d.getDate()).padStart(2, '0');
  const hours = String(d.getHours()).padStart(2, '0');
  const minutes = String(d.getMinutes()).padStart(2, '0');
  return `${year}-${month}-${day} ${hours}:${minutes}`;
}

/**
 * 格式化日期时间为 YYYY-MM-DD HH:mm:ss
 */
export function formatDateTimeFull(dateTime: Date | string): string {
  if (!dateTime) return '';
  const d = typeof dateTime === 'string' ? new Date(dateTime) : dateTime;
  const year = d.getFullYear();
  const month = String(d.getMonth() + 1).padStart(2, '0');
  const day = String(d.getDate()).padStart(2, '0');
  const hours = String(d.getHours()).padStart(2, '0');
  const minutes = String(d.getMinutes()).padStart(2, '0');
  const seconds = String(d.getSeconds()).padStart(2, '0');
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

/**
 * 格式化日期为 M月D日
 */
export function formatDateShort(date: Date | string): string {
  const d = typeof date === 'string' ? new Date(date) : date;
  return `${d.getMonth() + 1}月${d.getDate()}日`;
}

/**
 * 格式化日期范围为 M月D日 - M月D日
 */
export function formatDateRange(startDate: Date | string, endDate: Date | string): string {
  const start = typeof startDate === 'string' ? new Date(startDate) : startDate;
  const end = typeof endDate === 'string' ? new Date(endDate) : endDate;
  return `${formatDateShort(start)} - ${formatDateShort(end)}`;
}

/**
 * 获取日期描述（今天、明天、后天、周X）
 */
export function getDateDescription(dateStr: string): string {
  const targetDate = new Date(dateStr);
  const today = new Date();

  // 重置时间为 00:00:00 以便比较日期
  today.setHours(0, 0, 0, 0);
  targetDate.setHours(0, 0, 0, 0);

  const diffDays = Math.round((targetDate.getTime() - today.getTime()) / (1000 * 60 * 60 * 24));

  if (diffDays === 0) return '今天';
  if (diffDays === 1) return '明天';
  if (diffDays === 2) return '后天';

  const weekDays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
  return weekDays[targetDate.getDay()];
}

/**
 * 计算两个日期之间的天数
 */
export function calculateNights(checkIn: string, checkOut: string): number {
  const checkInDate = new Date(checkIn);
  const checkOutDate = new Date(checkOut);
  const diffTime = checkOutDate.getTime() - checkInDate.getTime();
  return Math.max(1, Math.ceil(diffTime / (1000 * 60 * 60 * 24)));
}

/**
 * 获取今天和明天的日期
 */
export function getDefaultDates(): { today: string; tomorrow: string } {
  const today = new Date();
  const tomorrow = new Date(today);
  tomorrow.setDate(tomorrow.getDate() + 1);

  return {
    today: formatDate(today),
    tomorrow: formatDate(tomorrow),
  };
}

/**
 * 获取今天的日期字符串 YYYY-MM-DD
 */
export function getTodayString(): string {
  return formatDate(new Date());
}

/**
 * 格式化相对时间（刚刚、X分钟前、X小时前、X天前）
 */
export function formatRelativeTime(dateTime: Date | string): string {
  if (!dateTime) return '';
  const d = typeof dateTime === 'string' ? new Date(dateTime) : dateTime;
  const now = new Date();
  const diff = now.getTime() - d.getTime();
  const minutes = Math.floor(diff / 60000);
  const hours = Math.floor(diff / 3600000);
  const days = Math.floor(diff / 86400000);

  if (minutes < 1) return '刚刚';
  if (minutes < 60) return `${minutes}分钟前`;
  if (hours < 24) return `${hours}小时前`;
  if (days < 7) return `${days}天前`;

  return formatDateTime(d);
}

/**
 * 格式化相对时间（今天、昨天、日期）
 */
export function formatRelativeDate(dateTime: Date | string): string {
  if (!dateTime) return '';
  const d = typeof dateTime === 'string' ? new Date(dateTime) : dateTime;
  const now = new Date();
  const diff = now.getTime() - d.getTime();
  const day = 24 * 60 * 60 * 1000;

  const timeStr = `${String(d.getHours()).padStart(2, '0')}:${String(d.getMinutes()).padStart(2, '0')}`;

  if (diff < day && d.getDate() === now.getDate()) {
    return `今天 ${timeStr}`;
  } else if (diff < 2 * day && d.getDate() === now.getDate() - 1) {
    return `昨天 ${timeStr}`;
  } else {
    return `${d.getMonth() + 1}-${d.getDate()} ${timeStr}`;
  }
}
