export function cn(...classes: (string | boolean | undefined | null)[]): string {
  return classes.filter(Boolean).join(' ');
}

export function formatDuration(minutes: number): string {
  if (minutes < 60) return `${minutes}min`;
  const hours = Math.floor(minutes / 60);
  const mins = minutes % 60;
  return mins > 0 ? `${hours}h ${mins}min` : `${hours}h`;
}

export function formatTime(seconds: number): string {
  const h = Math.floor(seconds / 3600);
  const m = Math.floor((seconds % 3600) / 60);
  const s = Math.floor(seconds % 60);
  if (h > 0) {
    return `${h}:${m.toString().padStart(2, '0')}:${s.toString().padStart(2, '0')}`;
  }
  return `${m}:${s.toString().padStart(2, '0')}`;
}

export function formatDate(dateString: string): string {
  const date = new Date(dateString);
  return date.toLocaleDateString('es-ES', {
    year: 'numeric',
    month: 'long',
    day: 'numeric',
  });
}

export function formatDateShort(dateString: string): string {
  const date = new Date(dateString);
  return date.toLocaleDateString('es-ES', {
    year: 'numeric',
    month: 'short',
    day: 'numeric',
  });
}

export function formatRelativeTime(dateString: string): string {
  const now = new Date();
  const date = new Date(dateString);
  const diff = now.getTime() - date.getTime();
  const seconds = Math.floor(diff / 1000);
  const minutes = Math.floor(seconds / 60);
  const hours = Math.floor(minutes / 60);
  const days = Math.floor(hours / 24);

  if (seconds < 60) return 'Hace un momento';
  if (minutes < 60) return `Hace ${minutes}m`;
  if (hours < 24) return `Hace ${hours}h`;
  if (days < 7) return `Hace ${days}d`;
  return formatDateShort(dateString);
}

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

export function formatNumber(num: number): string {
  if (num >= 1_000_000) return `${(num / 1_000_000).toFixed(1)}M`;
  if (num >= 1_000) return `${(num / 1_000).toFixed(1)}K`;
  return num.toString();
}

export function formatRating(rating: number): string {
  return rating.toFixed(1);
}

export function getProgressColor(progress: number): string {
  if (progress >= 90) return 'bg-green-500';
  if (progress >= 50) return 'bg-primary';
  return 'bg-accent';
}

export function generateId(): string {
  return Math.random().toString(36).substring(2, 11);
}

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

export function getDeviceIcon(type: string): string {
  switch (type) {
    case 'web': return '💻';
    case 'mobile': return '📱';
    case 'tv': return '📺';
    case 'tablet': return '📋';
    default: return '🖥️';
  }
}

export function getRoleLabel(role: string): string {
  switch (role) {
    case 'admin': return 'Administrador';
    case 'moderator': return 'Moderador';
    case 'user': return 'Usuario';
    default: return role;
  }
}

export function getStatusLabel(status: string): string {
  switch (status) {
    case 'active': return 'Activo';
    case 'suspended': return 'Suspendido';
    case 'expired': return 'Expirado';
    default: return status;
  }
}

export function getStatusColor(status: string): string {
  switch (status) {
    case 'active': return 'badge-success';
    case 'suspended': return 'badge-warning';
    case 'expired': return 'badge-danger';
    default: return 'badge-primary';
  }
}

export function isTokenExpired(token: string): boolean {
  try {
    const payload = JSON.parse(atob(token.split('.')[1]));
    return payload.exp * 1000 < Date.now();
  } catch {
    return true;
  }
}

export function getImageUrl(url: string | undefined, fallback: string): string {
  if (!url || url.trim() === '') return fallback;
  return url;
}
