import { format, isSameWeek } from "date-fns";

export const formatTime = (dateInput: string | Date | null | undefined) => {
  if (!dateInput) return "";
  const date = typeof dateInput === 'string' ? new Date(dateInput) : dateInput;
  return format(date, "HH:mm");
};

export const formatTimeWithSeconds = (
  dateInput: string | Date | null | undefined
) => {
  if (!dateInput) return "";
  const date = typeof dateInput === 'string' ? new Date(dateInput) : dateInput;
  return format(date, "HH:mm:ss");
};

export const formatDate = (dateInput: string | Date | null | undefined) => {
  if (!dateInput) return "";
  const date = typeof dateInput === 'string' ? new Date(dateInput) : dateInput;
  const now = new Date();

  if (
    date.getDate() === now.getDate() &&
    date.getMonth() === now.getMonth() &&
    date.getFullYear() === now.getFullYear()
  ) {
    return "Today";
  }

  const tomorrow = new Date(now);
  tomorrow.setDate(now.getDate() + 1);
  if (
    date.getDate() === tomorrow.getDate() &&
    date.getMonth() === tomorrow.getMonth() &&
    date.getFullYear() === tomorrow.getFullYear()
  ) {
    return "Tomorrow";
  }

  if (isSameWeek(date, now, { weekStartsOn: 1 })) {
    return format(date, "EEEE");
  }
  return format(date, "MM/dd");
};

export const formatDateMDY = (dateInput: string | Date | null | undefined) => {
  if (!dateInput) return "";
  const date = typeof dateInput === 'string' ? new Date(dateInput) : dateInput;
  return format(date, "MM/dd/yyyy");
};
