/** Generate date windows for paginated SEAP API calls */
export function generateDateWindows(
  startDate: Date,
  endDate: Date,
  windowDays: number
): Array<{ start: string; end: string }> {
  const windows: Array<{ start: string; end: string }> = [];
  const current = new Date(startDate);

  while (current < endDate) {
    const windowEnd = new Date(current);
    windowEnd.setDate(windowEnd.getDate() + windowDays);
    if (windowEnd > endDate) windowEnd.setTime(endDate.getTime());

    windows.push({
      start: formatForSeap(current),
      end: formatForSeap(windowEnd, true),
    });

    current.setDate(current.getDate() + windowDays);
  }

  return windows;
}

function formatForSeap(date: Date, endOfDay = false): string {
  const y = date.getFullYear();
  const m = String(date.getMonth() + 1).padStart(2, '0');
  const d = String(date.getDate()).padStart(2, '0');
  const time = endOfDay ? '23:59:59' : '00:00:00';
  return `${y}-${m}-${d}T${time}`;
}

export function monthsAgo(months: number): Date {
  const d = new Date();
  d.setMonth(d.getMonth() - months);
  d.setHours(0, 0, 0, 0);
  return d;
}
