import { Config } from '../constants/Config';

/**
 * Calculates the distance between two latitude/longitude points in kilometers
 * using the Haversine formula.
 */
export const calculateDistance = (lat1: number, lon1: number, lat2: number, lon2: number): number => {
  const R = 6371; // Radius of the Earth in km
  const dLat = (lat2 - lat1) * (Math.PI / 180);
  const dLon = (lon2 - lon1) * (Math.PI / 180);
  const a =
    Math.sin(dLat / 2) * Math.sin(dLat / 2) +
    Math.cos(lat1 * (Math.PI / 180)) *
      Math.cos(lat2 * (Math.PI / 180)) *
      Math.sin(dLon / 2) *
      Math.sin(dLon / 2);
  const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  return R * c;
};

/**
 * Calculates calories burned using MET (Metabolic Equivalent of Task) formula
 * and adjusts based on heart rate.
 */
export const calculateMetKcal = (
  type: 'RUNNING' | 'CYCLING' | 'WALKING',
  weightKg: number,
  hr: number,
  durationSeconds: number
): number => {
  const met = Config.MET_VALUES[type] || 3.8;
  const minutes = durationSeconds / 60;
  
  // Standard MET formula: Kcal = MET * 3.5 * weight (kg) / 200 * time (minutes)
  const baseKcal = (met * 3.5 * weightKg) / 200 * minutes;
  
  // Heart rate intensity multiplier (centered at 130 BPM)
  const hrIntensityMultiplier = hr > 0 ? (hr / 130) : 1.0;
  
  return baseKcal * hrIntensityMultiplier;
};
