import { clsx, type ClassValue } from "clsx";
import { formatDistanceToNow } from "date-fns";
import { twMerge } from "tailwind-merge";

export function formatCurrency(amount: number, currency: string = "USD"): string {
  const numericAmount = Number(amount);
  if (isNaN(numericAmount)) return `${currency} 0.00`;

  try {
    return new Intl.NumberFormat("en-US", {
      style: "currency",
      currency: currency.toUpperCase(),
      minimumFractionDigits: 2,
      maximumFractionDigits: currency === "BTC" ? 8 : 2,
    }).format(numericAmount);
  } catch (error) {
    // Fallback for unsupported currencies
    return `${currency} ${numericAmount.toFixed(currency === "BTC" ? 8 : 2)}`;
  }
}

export function formatNumber(num: number): string {
  return new Intl.NumberFormat("en-US").format(num);
}

export function formatDate(dateInput?: string | Date): string {
  const date = typeof dateInput === "string" ? new Date(dateInput) : dateInput;
  return new Intl.DateTimeFormat("en-US", {
    year: "numeric",
    month: "short",
    day: "numeric",
  }).format(date);
}

export function formatPercentage(value: number): string {
  return new Intl.NumberFormat("en-US", {
    style: "percent",
    minimumFractionDigits: 2,
    maximumFractionDigits: 2,
    signDisplay: "exceptZero",
  }).format(value / 100);
}

export function cn(...inputs: ClassValue[]) {
  return twMerge(clsx(inputs));
}

export function formatDateTime(dateString: string): string {
  if (!dateString) return "N/A";

  const date = new Date(dateString);
  return new Intl.DateTimeFormat("en-US", {
    year: "numeric",
    month: "short",
    day: "numeric",
    hour: "2-digit",
    minute: "2-digit",
  }).format(date);
}

export const formatTimeAgo = (date: string | Date) => {
  if (!date) return "N/A";
  try {
    const parsedDate = date instanceof Date ? date : new Date(date);
    return formatDistanceToNow(parsedDate, { addSuffix: true });
  } catch (err) {
    return "Invalid date";
  }
};
