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

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

/**
 * Format a number as currency
 * @param value The number to format
 * @param currency The currency code, defaults to PLN
 * @returns Formatted currency string
 */
export function formatCurrency(
  value: number,
  currency: string = "PLN",
): string {
  return new Intl.NumberFormat("pl-PL", {
    style: "currency",
    currency,
    minimumFractionDigits: 2,
  }).format(value);
}

/**
 * Format a date string to a readable format
 * @param dateString Date string to format
 * @param format Format option: 'short', 'medium', 'long'
 * @returns Formatted date string
 */
export function formatDate(
  dateString: string,
  format: "short" | "medium" | "long" = "medium",
): string {
  const date = new Date(dateString);

  const options: Intl.DateTimeFormatOptions = {
    day: "numeric",
    month: format === "short" ? "numeric" : "long",
    year: "numeric",
  };

  if (format === "long") {
    options.weekday = "long";
    options.hour = "2-digit";
    options.minute = "2-digit";
  }

  return new Intl.DateTimeFormat("pl-PL", options).format(date);
}

/**
 * Check if the app is currently online
 * @returns Boolean indicating online status
 */
export function isOnline(): boolean {
  return navigator.onLine;
}

/**
 * Get random color for charts and visualizations
 * @returns Hex color code
 */
export function getRandomColor(): string {
  const colors = [
    "#FF6384",
    "#36A2EB",
    "#FFCE56",
    "#4BC0C0",
    "#9966FF",
    "#FF9F40",
    "#8AC926",
    "#1982C4",
    "#6A4C93",
    "#F94144",
  ];
  return colors[Math.floor(Math.random() * colors.length)];
}

/**
 * Generate a unique ID for local storage entries
 * @returns Unique ID string
 */
export function generateId(): string {
  return Date.now().toString(36) + Math.random().toString(36).substr(2, 5);
}
