const DAY_MS = 86_400_000;

export function toLocalDateKey(date: Date): string {
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, "0");
  const day = String(date.getDate()).padStart(2, "0");
  return `${year}-${month}-${day}`;
}

function calendarSerial(key: string): number {
  const [year, month, day] = key.split("-").map(Number);
  return Date.UTC(year, month - 1, day) / DAY_MS;
}

export function getTripDayForLocalDate(startDate: string, endDate: string, now = new Date()): number | null {
  const today = toLocalDateKey(now);
  if (today < startDate || today > endDate) return null;
  return calendarSerial(today) - calendarSerial(startDate) + 1;
}
