/** Today's Hong Kong calendar date as UTC midnight — the form every calendar
 *  date is stored in. Comparing dates this way keeps the answer independent of
 *  the host clock and of the hour a job happens to run: a note set for the 28th
 *  is due on the 28th, not a day either side of it. */
export function hkToday(): Date {
  return new Date(`${new Intl.DateTimeFormat('en-CA', { timeZone: 'Asia/Hong_Kong' }).format(new Date())}T00:00:00.000Z`);
}

/** A calendar date typed as "YYYY-MM-DD", parsed to the same UTC midnight every
 *  stored date uses. `new Date('2026-07-01')` already does this, but
 *  `new Date('2026-07-01T00:00:00')` (or any string a client pads differently)
 *  does not — so every date that arrives as text goes through here rather than
 *  each caller guessing. */
export function parseDay(iso: string): Date {
  return new Date(`${iso.slice(0, 10)}T00:00:00.000Z`);
}

/** The Hong Kong day a date falls on, as "YYYYMMDD" — right for a stored
 *  calendar date (UTC midnight is 8am in Hong Kong) and for a moment alike, so
 *  a receipt taken at 1am on the 1st belongs to the new month, not the old. */
export function hkDay(d: Date): string {
  return hkIsoDay(d).replace(/-/g, '');
}

/** The same Hong Kong day as "YYYY-MM-DD", the form a report hands a reader. */
export function hkIsoDay(d: Date): string {
  return new Intl.DateTimeFormat('en-CA', { timeZone: 'Asia/Hong_Kong' }).format(d);
}
