/**
 * Backdating guard for operational time entries (gate in/out, mile truck
 * times, delivery pickups): times must be recorded as they happen, never
 * dated back. A one-hour grace covers real-world lag (weighbridge queue,
 * operator finishing the form after the event).
 */
export const BACKDATE_GRACE_MS = 60 * 60 * 1000;

/** Local-time "YYYY-MM-DDTHH:mm" for a datetime-local input's `min`. */
export const nowLocalDateTimeInput = (): string =>
  new Date(Date.now() - new Date().getTimezoneOffset() * 60_000)
    .toISOString()
    .slice(0, 16);

/** True when the value is more than the grace period in the past. */
export const isBackdated = (value: string | Date | null | undefined): boolean => {
  if (!value) return false;
  const t = value instanceof Date ? value.getTime() : new Date(value).getTime();
  return Number.isFinite(t) && t < Date.now() - BACKDATE_GRACE_MS;
};
