// Business-time helpers (see inventory-server/docs/TIME.md).
//
// THE CONVENTION: a business day runs 1am Eastern → 1am Eastern, which is
// identical to the America/Chicago calendar date. The SERVER owns all
// boundary math — the client sends named ranges or date-only strings
// ('YYYY-MM-DD', meaning business dates), never toISOString() of a
// browser-local midnight.

export const BUSINESS_TZ = 'America/Chicago';
export const OFFICE_TZ = 'America/New_York';

/** The current business date as 'YYYY-MM-DD' (Chicago calendar date). */
export function businessTodayStr(): string {
  // en-CA formats as YYYY-MM-DD
  return new Intl.DateTimeFormat('en-CA', { timeZone: BUSINESS_TZ }).format(new Date());
}

/** Business date of an instant. */
export function businessDateOf(date: Date): string {
  return new Intl.DateTimeFormat('en-CA', { timeZone: BUSINESS_TZ }).format(date);
}

/**
 * Calendar date of a browser-local Date, as 'YYYY-MM-DD' — for converting a
 * locally-constructed period boundary (e.g. "March 2026" → Mar 1 local) into
 * the date-only string the server resolves to business-day bounds.
 */
export function toDateOnly(date: Date): string {
  const y = date.getFullYear();
  const m = String(date.getMonth() + 1).padStart(2, '0');
  const d = String(date.getDate()).padStart(2, '0');
  return `${y}-${m}-${d}`;
}
