/**
 * KRA - Date Utility Functions
 */
import dayjs from 'dayjs';
import relativeTime from 'dayjs/plugin/relativeTime';
import 'dayjs/locale/zh-cn';

// Configure dayjs
dayjs.extend(relativeTime);
dayjs.locale('zh-cn');

/**
 * Format date to string
 * @param date - Date to format (Date, string, number)
 * @param pattern - Format pattern (default: 'YYYY-MM-DD HH:mm:ss')
 * @returns Formatted date string
 */
export function formatDate(
  date: Date | string | number | undefined | null,
  pattern: string = 'YYYY-MM-DD HH:mm:ss'
): string {
  if (!date) return '';
  return dayjs(date).format(pattern);
}

/**
 * Format date to date only string
 * @param date - Date to format
 * @returns Formatted date string (YYYY-MM-DD)
 */
export function formatDateOnly(date: Date | string | number | undefined | null): string {
  return formatDate(date, 'YYYY-MM-DD');
}

/**
 * Format date to time only string
 * @param date - Date to format
 * @returns Formatted time string (HH:mm:ss)
 */
export function formatTimeOnly(date: Date | string | number | undefined | null): string {
  return formatDate(date, 'HH:mm:ss');
}

/**
 * Format date to relative time (e.g., "3 hours ago")
 * @param date - Date to format
 * @returns Relative time string
 */
export function formatRelativeTime(date: Date | string | number | undefined | null): string {
  if (!date) return '';
  return dayjs(date).fromNow();
}

/**
 * Format timestamp to string (compatible with KRA backend)
 * @param times - Timestamp or date string
 * @param pattern - Format pattern
 * @returns Formatted date string
 */
export function formatTimeToStr(
  times: Date | string | number | undefined | null,
  pattern?: string
): string {
  return formatDate(times, pattern);
}

/**
 * Get start of day
 * @param date - Date
 * @returns Start of day Date object
 */
export function getStartOfDay(date: Date | string | number = new Date()): Date {
  return dayjs(date).startOf('day').toDate();
}

/**
 * Get end of day
 * @param date - Date
 * @returns End of day Date object
 */
export function getEndOfDay(date: Date | string | number = new Date()): Date {
  return dayjs(date).endOf('day').toDate();
}

/**
 * Get date range for today
 * @returns [startOfToday, endOfToday]
 */
export function getTodayRange(): [Date, Date] {
  const today = new Date();
  return [getStartOfDay(today), getEndOfDay(today)];
}

/**
 * Get date range for this week
 * @returns [startOfWeek, endOfWeek]
 */
export function getThisWeekRange(): [Date, Date] {
  const now = dayjs();
  return [now.startOf('week').toDate(), now.endOf('week').toDate()];
}

/**
 * Get date range for this month
 * @returns [startOfMonth, endOfMonth]
 */
export function getThisMonthRange(): [Date, Date] {
  const now = dayjs();
  return [now.startOf('month').toDate(), now.endOf('month').toDate()];
}

/**
 * Check if date is today
 * @param date - Date to check
 * @returns true if date is today
 */
export function isToday(date: Date | string | number): boolean {
  return dayjs(date).isSame(dayjs(), 'day');
}

/**
 * Add days to date
 * @param date - Base date
 * @param days - Number of days to add
 * @returns New Date object
 */
export function addDays(date: Date | string | number, days: number): Date {
  return dayjs(date).add(days, 'day').toDate();
}

export default {
  formatDate,
  formatDateOnly,
  formatTimeOnly,
  formatRelativeTime,
  formatTimeToStr,
  getStartOfDay,
  getEndOfDay,
  getTodayRange,
  getThisWeekRange,
  getThisMonthRange,
  isToday,
  addDays,
};
