// Plain utility, no client-only behavior — kept out of lib/products.ts
// (which is "use client" for its useProducts() hook) specifically so
// Server Components can still call it directly. Any export from a
// "use client" module becomes a client-only reference as far as Next.js's
// RSC boundary is concerned, even a pure function with zero hooks — a
// Server Component importing formatPrice from products.ts fails at
// runtime with "Attempted to call formatPrice() from the server but
// formatPrice is on the client."
export function formatPrice(value: number): string {
  return `${value.toFixed(2).replace(".", ",")} €`;
}

// null when there's nothing to advertise — compareAtPrice unset, or set
// but not actually higher than price (e.g. cleared back down without
// nulling the field). Callers use this both to gate whether to render
// strikethrough/badge UI at all and to fill in the "-XX%" label.
export function discountPercent(price: number, compareAtPrice: number | null): number | null {
  if (compareAtPrice === null || compareAtPrice <= price) return null;
  return Math.round((1 - price / compareAtPrice) * 100);
}

const MONTHS_DE = [
  "Januar", "Februar", "März", "April", "Mai", "Juni",
  "Juli", "August", "September", "Oktober", "November", "Dezember",
];

// "03. Juli 2025" — spelled-out German month, not Intl.DateTimeFormat,
// so the exact mockup format doesn't depend on the server's ICU locale data.
export function formatDate(iso: string): string {
  const d = new Date(iso);
  const day = String(d.getDate()).padStart(2, "0");
  return `${day}. ${MONTHS_DE[d.getMonth()]} ${d.getFullYear()}`;
}

// "Juli 2026" — for legal pages' "Stand: …" line, derived from the
// LegalPages doc's own updatedAt so it always reflects when the template
// was actually last edited, instead of a hand-typed string an admin has
// to remember to bump on every content change.
export function formatMonthYear(iso: string): string {
  const d = new Date(iso);
  return `${MONTHS_DE[d.getMonth()]} ${d.getFullYear()}`;
}
