import { format, formatDistanceToNow, differenceInMinutes, parseISO } from 'date-fns';

export const formatRaceTime = (dateString: string): string => {
  const date = parseISO(dateString);
  return format(date, 'h:mm a');
};

export const formatRaceDate = (dateString: string): string => {
  const date = parseISO(dateString);
  return format(date, 'EEE, MMM d');
};

export const formatCountdown = (dateString: string): string => {
  const date = parseISO(dateString);
  const now = new Date();
  const minutesUntil = differenceInMinutes(date, now);
  
  if (minutesUntil < 0) {
    return 'Started';
  } else if (minutesUntil < 1) {
    return 'Starting now';
  } else if (minutesUntil < 60) {
    return `${minutesUntil}m`;
  } else if (minutesUntil < 1440) {
    const hours = Math.floor(minutesUntil / 60);
    const mins = minutesUntil % 60;
    return mins > 0 ? `${hours}h ${mins}m` : `${hours}h`;
  } else {
    return formatDistanceToNow(date, { addSuffix: false });
  }
};

export const formatDistance = (meters: number): string => {
  return `${meters}m`;
};

export const formatPrizeMoney = (amount: number): string => {
  if (amount >= 1000000) {
    return `$${(amount / 1000000).toFixed(1)}M`;
  } else if (amount >= 1000) {
    return `$${(amount / 1000).toFixed(0)}K`;
  }
  return `$${amount}`;
};

export const getBetTypeName = (betType: string): string => {
  const names: Record<string, string> = {
    best_bet: 'Best Bet',
    win: 'Win',
    place: 'Place',
    quinella: 'Quinella',
    exacta: 'Exacta',
    trifecta: 'Trifecta',
  };
  return names[betType] || betType;
};

export const getConfidenceColor = (confidence: string): string => {
  switch (confidence) {
    case 'high':
      return '#22c55e';
    case 'medium':
      return '#f59e0b';
    case 'low':
      return '#ef4444';
    default:
      return '#6b7280';
  }
};
