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

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

// Format number with commas
export function formatNumber(num: number, decimals = 0): string {
  return num.toLocaleString('en-US', {
    minimumFractionDigits: decimals,
    maximumFractionDigits: decimals,
  });
}

// Format percentage
export function formatPercent(num: number, decimals = 0): string {
  return `${formatNumber(num * 100, decimals)}%`;
}

// Format score (0-100)
export function formatScore(score: number): string {
  return formatNumber(score, 1);
}

// Get color for score (0-100)
export function getScoreColor(score: number): string {
  if (score >= 75) return 'text-success';
  if (score >= 50) return 'text-primary';
  if (score >= 25) return 'text-warning';
  return 'text-danger';
}

// Get color for probability (0-1)
export function getProbabilityColor(prob: number): string {
  if (prob >= 0.75) return 'text-success';
  if (prob >= 0.5) return 'text-primary';
  if (prob >= 0.25) return 'text-warning';
  return 'text-danger';
}

// Get background color for rank band
export function getRankBandColor(band: string): string {
  if (band.includes('1%') || band.includes('5%')) return 'bg-success';
  if (band.includes('10%') || band.includes('25%')) return 'bg-primary';
  if (band.includes('50%')) return 'bg-warning';
  return 'bg-danger';
}

// Get rank band percentage for bar display
export function getRankBandPercent(band: string): number {
  const mapping: Record<string, number> = {
    'Top 1%': 99,
    'Top 5%': 95,
    'Top 10%': 90,
    'Top 25%': 75,
    'Top 50%': 50,
    'Bottom 50%': 25,
  };
  return mapping[band] || 50;
}

// Truncate text
export function truncate(text: string, maxLength: number): string {
  if (text.length <= maxLength) return text;
  return text.slice(0, maxLength - 3) + '...';
}

// Debounce function
export function debounce<T extends (...args: unknown[]) => void>(
  func: T,
  wait: number
): (...args: Parameters<T>) => void {
  let timeout: NodeJS.Timeout | null = null;
  
  return (...args: Parameters<T>) => {
    if (timeout) clearTimeout(timeout);
    timeout = setTimeout(() => func(...args), wait);
  };
}

// Format file size
export function formatFileSize(bytes: number): string {
  if (bytes < 1024) return `${bytes} B`;
  if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
  return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
}

// Validate image file
export function validateImageFile(file: File): { valid: boolean; error?: string } {
  const validTypes = ['image/jpeg', 'image/png', 'image/webp'];
  const maxSize = 10 * 1024 * 1024; // 10MB
  
  if (!validTypes.includes(file.type)) {
    return { valid: false, error: 'Invalid file type. Use JPEG, PNG, or WebP.' };
  }
  
  if (file.size > maxSize) {
    return { valid: false, error: 'File too large. Maximum 10MB.' };
  }
  
  return { valid: true };
}
