const TZ = "Asia/Dhaka";

export function fmtDate(d: Date | string | null | undefined, opts: Intl.DateTimeFormatOptions = {}) {
  if (!d) return "";
  return new Intl.DateTimeFormat("en-GB", { day: "numeric", month: "short", year: "numeric", timeZone: TZ, ...opts }).format(new Date(d));
}

export function fmtDateTime(d: Date | string | null | undefined) {
  return fmtDate(d, { hour: "2-digit", minute: "2-digit" });
}

/** "12 – 20 Dec 2026" style window, always labelled as an estimate by callers. */
export function fmtWindow(from: Date | string, to: Date | string) {
  const a = new Date(from), b = new Date(to);
  const sameMonth = a.getMonth() === b.getMonth() && a.getFullYear() === b.getFullYear();
  if (sameMonth) return `${fmtDate(a, { day: "numeric", year: undefined, month: undefined })}–${fmtDate(b)}`;
  return `${fmtDate(a, { year: a.getFullYear() === b.getFullYear() ? undefined : "numeric" })} – ${fmtDate(b)}`;
}

export function addDays(d: Date, days: number) {
  const x = new Date(d);
  x.setDate(x.getDate() + days);
  return x;
}

export function addHours(d: Date, hours: number) {
  return new Date(d.getTime() + hours * 3600_000);
}
