export function formatDate(value: string) {
  return new Date(value).toLocaleDateString("en-US", {
    day: "numeric",
    month: "long",
    year: "numeric",
    // Pinned explicitly — without this, the result depends on the runtime's
    // local system timezone. Timestamps are stored UTC and the server
    // process (Node) usually runs in UTC while the browser runs in the
    // viewer's local timezone (WIB for most users here); for a date near a
    // local-midnight boundary, that mismatch changes the calendar day
    // between SSR and hydration, causing a React hydration warning. Pinning
    // to WIB makes the result deterministic regardless of runtime, and
    // matches the timezone this platform's events actually run in.
    timeZone: "Asia/Jakarta",
  });
}

/** Same Asia/Jakarta pinning rationale as formatDate above — used wherever date and time are shown as separate fields. */
export function formatTime(value: string) {
  return new Date(value).toLocaleTimeString("en-US", {
    hour: "2-digit",
    minute: "2-digit",
    timeZone: "Asia/Jakarta",
  });
}

export function formatCurrency(value: string | number) {
  return new Intl.NumberFormat("en-US", {
    style: "currency",
    currency: "IDR",
    maximumFractionDigits: 0,
  }).format(Number(value));
}

/** Abbreviated currency for KPI cards / chart axes, e.g. "Rp 48.2M", "Rp 800K". */
export function formatCompactCurrency(value: string | number) {
  const n = Number(value);
  const abs = Math.abs(n);
  const sign = n < 0 ? "-" : "";

  if (abs >= 1_000_000_000) {
    return `${sign}Rp ${(abs / 1_000_000_000).toLocaleString("en-US", { maximumFractionDigits: 1 })}B`;
  }
  if (abs >= 1_000_000) {
    return `${sign}Rp ${(abs / 1_000_000).toLocaleString("en-US", { maximumFractionDigits: 1 })}M`;
  }
  if (abs >= 1_000) {
    return `${sign}Rp ${(abs / 1_000).toLocaleString("en-US", { maximumFractionDigits: 0 })}K`;
  }
  return formatCurrency(n);
}
