export type DateRange = { from: Date | null; to: Date | null; fromStr: string; toStr: string };

/** Parses ?from=YYYY-MM-DD&to=YYYY-MM-DD from report search params. No dates = all time. */
export function parseDateRange(params: { from?: string; to?: string }): DateRange {
  const fromStr = params.from ?? "";
  const toStr = params.to ?? "";
  const from = fromStr ? new Date(`${fromStr}T00:00:00`) : null;
  const to = toStr ? new Date(`${toStr}T23:59:59.999`) : null;
  return { from, to, fromStr, toStr };
}

export function dateRangeWhere(range: DateRange) {
  if (!range.from && !range.to) return undefined;
  return {
    ...(range.from ? { gte: range.from } : {}),
    ...(range.to ? { lte: range.to } : {}),
  };
}
