import i18n from "@/lib/i18n";

export function formatMoney(value: number | string | null | undefined, currency = "USD"): string {
  if (value == null) return "—"
  const n = typeof value === "string" ? Number(value) : value
  if (!Number.isFinite(n)) return "—"
  try {
    return new Intl.NumberFormat("en-US", {
      style: "currency",
      currency,
      maximumFractionDigits: 2,
    }).format(n)
  } catch {
    return i18n.t("lib.format.money_fallback", { amount: n.toFixed(2), currency })
  }
}

export function formatNumber(value: number | string | null | undefined, digits = 2): string {
  if (value == null) return "—"
  const n = typeof value === "string" ? Number(value) : value
  if (!Number.isFinite(n)) return "—"
  return new Intl.NumberFormat("en-US", {
    maximumFractionDigits: digits,
    minimumFractionDigits: digits > 0 ? Math.min(digits, 2) : 0,
  }).format(n)
}

export function formatGold(value: number | string | null | undefined): string {
  if (value == null) return "—"
  const n = typeof value === "string" ? Number(value) : value
  if (!Number.isFinite(n)) return "—"
  return `${new Intl.NumberFormat("en-US", { maximumFractionDigits: 2, minimumFractionDigits: 2 }).format(n)} oz`
}

export function formatDate(value: string | Date | null | undefined): string {
  if (!value) return "—"
  const d = typeof value === "string" ? new Date(value) : value
  if (isNaN(d.getTime())) return "—"
  return new Intl.DateTimeFormat("ar-EG", {
    year: "numeric",
    month: "short",
    day: "numeric",
  }).format(d)
}

export function formatDateTime(value: string | Date | null | undefined): string {
  if (!value) return "—"
  const d = typeof value === "string" ? new Date(value) : value
  if (isNaN(d.getTime())) return "—"
  return new Intl.DateTimeFormat("ar-EG", {
    year: "numeric",
    month: "short",
    day: "numeric",
    hour: "2-digit",
    minute: "2-digit",
  }).format(d)
}

export function timeAgo(value: string | Date | null | undefined): string {
  if (!value) return "—"
  const d = typeof value === "string" ? new Date(value) : value
  if (isNaN(d.getTime())) return "—"
  const diff = Date.now() - d.getTime()
  const sec = Math.floor(diff / 1000)
  if (sec < 60) return i18n.t('lib.format.text_sec_ago', { sec })
  const min = Math.floor(sec / 60)
  if (min < 60) return i18n.t('lib.format.text_min_ago', { min })
  const hr = Math.floor(min / 60)
  if (hr < 24) return i18n.t('lib.format.text_hr_ago', { hr })
  const day = Math.floor(hr / 24)
  if (day < 30) return i18n.t('lib.format.text_day_ago', { day })
  const month = Math.floor(day / 30)
  if (month < 12) return i18n.t('lib.format.text_month_ago', { month })
  return i18n.t('lib.format.text_year_ago', { years: Math.floor(month / 12) })
}

// Short aliases used throughout the admin pages
export const fmtMoney = formatMoney
export const fmtNumber = formatNumber

export function fmtGrams(value: number | string | null | undefined): string {
  if (value == null) return "—"
  const n = typeof value === "string" ? Number(value) : value
  if (!Number.isFinite(n)) return "—"
  return `${new Intl.NumberFormat("en-US", { maximumFractionDigits: 2, minimumFractionDigits: 2 }).format(n)} g`
}

export function fmtOunces(value: number | string | null | undefined): string {
  if (value == null) return "—"
  const n = typeof value === "string" ? Number(value) : value
  if (!Number.isFinite(n)) return "—"
  return `${new Intl.NumberFormat("en-US", { maximumFractionDigits: 4, minimumFractionDigits: 2 }).format(n)} oz`
}

export function fmtPercent(value: number | string | null | undefined, digits = 2): string {
  if (value == null) return "—"
  const n = typeof value === "string" ? Number(value) : value
  if (!Number.isFinite(n)) return "—"
  return `${new Intl.NumberFormat("en-US", { maximumFractionDigits: digits, minimumFractionDigits: 0 }).format(n)}%`
}

export function initialsOf(name: string | null | undefined): string {
  if (!name) return "??"
  return name
    .split(/\s+/)
    .filter(Boolean)
    .slice(0, 2)
    .map((p) => p[0])
    .join("")
    .toUpperCase()
}
