import { format, parseISO, startOfDay } from "date-fns";
import { utcToZonedTime, zonedTimeToUtc } from "date-fns-tz";

export function dateStringToUtcTimeStamp(
  dateString: string | undefined
): number {
  // If dateString is provided, parse it, otherwise use the current date
  const date: Date = dateString && dateString !== "ANY" ? parseISO(dateString) : new Date();

  // Set the time to 00:00:00 and convert the date to UTC
  const utcDate = zonedTimeToUtc(startOfDay(date), "UTC");

  // Return the timestamp in milliseconds since Unix Epoch
  return utcDate.getTime();
}

export function utcTimeStampToDateString(utcTimestamp: number | null): string {
  // If utcTimestamp is null, use the current date
  const date = utcTimestamp !== null ? new Date(utcTimestamp) : new Date();

  // Convert the date to a UTC-zoned date
  const utcDate = utcToZonedTime(date, "UTC");

  // Format the UTC date to 'YYYY-MM-DD'
  return format(utcDate, "yyyy-MM-dd");
}