// Utility composable for event date handling with timezone support
export const useEventDateUtils = () => {
  const TIMEZONE = "America/Toronto";

  // Format a date to a specific format
  const formatDate = (date, options = {}) => {
    const dateObj = date instanceof Date ? date : new Date(date);
    const { month = "short", day = "numeric", year = "numeric" } = options;

    return new Intl.DateTimeFormat("en-US", {
      month,
      day,
      year,
    }).format(dateObj);
  };

  // Format event date range
  const formatDateRange = (startDate, endDate, compact = false) => {
    if (!startDate || !endDate) return "No dates";

    const start = new Date(startDate);
    const end = new Date(endDate);

    const startMonth = start.toLocaleDateString("en-US", { month: "short" });
    const endMonth = end.toLocaleDateString("en-US", { month: "short" });
    const startDay = start.getDate();
    const endDay = end.getDate();
    const year = end.getFullYear();

    if (compact) {
      if (
        start.getMonth() === end.getMonth() &&
        start.getFullYear() === end.getFullYear()
      ) {
        return `${startMonth} ${startDay}-${endDay}`;
      }
      return `${startMonth} ${startDay} - ${endMonth} ${endDay}`;
    }

    if (
      start.getMonth() === end.getMonth() &&
      start.getFullYear() === end.getFullYear()
    ) {
      return `${startMonth} ${startDay}-${endDay}, ${year}`;
    } else if (start.getFullYear() === end.getFullYear()) {
      return `${startMonth} ${startDay} - ${endMonth} ${endDay}, ${year}`;
    } else {
      return `${formatDate(startDate)} - ${formatDate(endDate)}`;
    }
  };

  // Check if a date is in the past
  const isPastDate = (date) => {
    const dateObj = date instanceof Date ? date : new Date(date);
    const now = new Date();
    return dateObj < now;
  };

  // Check if a date is today
  const isToday = (date) => {
    const dateObj = date instanceof Date ? date : new Date(date);
    const today = new Date();
    return (
      dateObj.getDate() === today.getDate() &&
      dateObj.getMonth() === today.getMonth() &&
      dateObj.getFullYear() === today.getFullYear()
    );
  };

  // Get a readable time string
  const formatTime = (date, includeSeconds = false) => {
    const dateObj = date instanceof Date ? date : new Date(date);
    const options = {
      hour: "2-digit",
      minute: "2-digit",
      ...(includeSeconds && { second: "2-digit" }),
    };
    return new Intl.DateTimeFormat("en-US", options).format(dateObj);
  };

  return {
    TIMEZONE,
    formatDate,
    formatDateRange,
    isPastDate,
    isToday,
    formatTime,
  };
};
