# Ruby Calculator - Interactive calculator built with Ruby on Rails
# Author: RSK World (Molla Samser, Founder | Rima Khatun, Designer & Tester)
# Contact: help@rskworld.in | +91 93305 39277
# Website: https://rskworld.in
# Year: 2026
# Description: Build a fully functional calculator using Ruby on Rails with modern UI

class HealthCalculator
  def self.calculate_bmi(weight, height_cm)
    height_m = height_cm / 100.0
    bmi = weight / (height_m ** 2)
    
    category = case bmi
              when < 18.5
                'Underweight'
              when 18.5..24.9
                'Normal weight'
              when 25.0..29.9
                'Overweight'
              when 30.0..34.9
                'Obesity Class I'
              when 35.0..39.9
                'Obesity Class II'
              else
                'Obesity Class III'
              end
    
    {
      weight: weight,
      height_cm: height_cm,
      bmi: bmi.round(2),
      category: category,
      ideal_weight_min: (18.5 * height_m ** 2).round(1),
      ideal_weight_max: (24.9 * height_m ** 2).round(1)
    }
  end
  
  def self.calculate_bmr(weight, height_cm, age, gender)
    if gender.downcase == 'male'
      bmr = 88.362 + (13.397 * weight) + (4.799 * height_cm) - (5.677 * age)
    else
      bmr = 447.593 + (9.247 * weight) + (3.098 * height_cm) - (4.330 * age)
    end
    
    {
      weight: weight,
      height_cm: height_cm,
      age: age,
      gender: gender,
      bmr: bmr.round(2)
    }
  end
  
  def self.calculate_daily_calories(bmr, activity_level)
    activity_multipliers = {
      'sedentary' => 1.2,
      'light' => 1.375,
      'moderate' => 1.55,
      'active' => 1.725,
      'very_active' => 1.9
    }
    
    multiplier = activity_multipliers[activity_level] || 1.2
    daily_calories = bmr * multiplier
    
    {
      bmr: bmr,
      activity_level: activity_level,
      daily_calories: daily_calories.round(2),
      weight_loss_calories: (daily_calories - 500).round(2),
      weight_gain_calories: (daily_calories + 500).round(2)
    }
  end
  
  def self.calculate_body_fat_percentage(weight, height_cm, age, gender, waist_cm = nil, hip_cm = nil, neck_cm = nil)
    if gender.downcase == 'male'
      if waist_cm && neck_cm
        # US Navy formula for men
        body_fat = 495 / (1.0324 - 0.19077 * Math.log10(waist_cm - neck_cm) + 0.15456 * Math.log10(height_cm)) - 450
      else
        # BMI-based formula for men
        bmi = weight / ((height_cm / 100) ** 2)
        body_fat = (1.20 * bmi) + (0.23 * age) - 16.2
      end
    else
      if waist_cm && hip_cm && neck_cm
        # US Navy formula for women
        body_fat = 495 / (1.29579 - 0.35004 * Math.log10(waist_cm + hip_cm - neck_cm) + 0.22100 * Math.log10(height_cm)) - 450
      else
        # BMI-based formula for women
        bmi = weight / ((height_cm / 100) ** 2)
        body_fat = (1.20 * bmi) + (0.23 * age) - 5.4
      end
    end
    
    category = case body_fat
              when < 6
                gender.downcase == 'male' ? 'Essential fat' : 'Essential fat'
              when 6..13
                'Athletes'
              when 14..17
                'Fitness'
              when 18..24
                'Average'
              else
                'Obese'
              end
    
    {
      weight: weight,
      height_cm: height_cm,
      age: age,
      gender: gender,
      body_fat_percentage: body_fat.round(2),
      category: category,
      fat_mass: (weight * body_fat / 100).round(2),
      lean_mass: (weight * (1 - body_fat / 100)).round(2)
    }
  end
  
  def self.calculate_ideal_weight(height_cm, gender, body_frame = 'medium')
    base_weight = gender.downcase == 'male' ? 50 : 45.5
    height_over_5ft = [(height_cm - 152.4) / 2.54, 0].max
    
    frame_adjustments = {
      'small' => -2.5,
      'medium' => 0,
      'large' => 2.5
    }
    
    ideal_weight = base_weight + (2.3 * height_over_5ft) + frame_adjustments[body_frame]
    
    {
      height_cm: height_cm,
      gender: gender,
      body_frame: body_frame,
      ideal_weight: ideal_weight.round(1),
      weight_range_min: (ideal_weight - 5).round(1),
      weight_range_max: (ideal_weight + 5).round(1)
    }
  end
  
  def self.calculate_water_intake(weight, activity_level, climate = 'moderate')
    base_water = weight * 0.033 # 33ml per kg
    
    activity_adjustments = {
      'sedentary' => 0,
      'light' => 0.5,
      'moderate' => 1.0,
      'active' => 1.5,
      'very_active' => 2.0
    }
    
    climate_adjustments = {
      'cold' => -0.5,
      'moderate' => 0,
      'hot' => 1.0,
      'very_hot' => 1.5
    }
    
    total_water = base_water + activity_adjustments[activity_level] + climate_adjustments[climate]
    
    {
      weight: weight,
      activity_level: activity_level,
      climate: climate,
      daily_water_liters: total_water.round(2),
      daily_water_ounces: (total_water * 33.814).round(1),
      daily_water_glasses: (total_water * 4.227).round(1)
    }
  end
  
  def self.calculate_1rm(weight, reps)
    # Using the Epley formula
    if reps == 1
      one_rep_max = weight
    else
      one_rep_max = weight * (1 + reps / 30.0)
    end
    
    # Training percentages
    training_percentages = {
      'Strength (85-95%)' => (one_rep_max * 0.9).round(1),
      'Power (80-90%)' => (one_rep_max * 0.85).round(1),
      'Hypertrophy (75-85%)' => (one_rep_max * 0.8).round(1),
      'Endurance (65-75%)' => (one_rep_max * 0.7).round(1)
    }
    
    {
      weight_lifted: weight,
      reps_performed: reps,
      estimated_1rm: one_rep_max.round(2),
      training_weights: training_percentages
    }
  end
  
  def self.calculate_target_heart_rate(age, intensity_level = 'moderate')
    max_heart_rate = 220 - age
    
    intensity_ranges = {
      'very_light' => (50..60),
      'light' => (60..70),
      'moderate' => (70..80),
      'hard' => (80..90),
      'maximum' => (90..100)
    }
    
    range = intensity_ranges[intensity_level] || intensity_ranges['moderate']
    target_min = (max_heart_rate * range.begin / 100.0).round
    target_max = (max_heart_rate * range.end / 100.0).round
    
    {
      age: age,
      max_heart_rate: max_heart_rate,
      intensity_level: intensity_level,
      target_heart_rate_min: target_min,
      target_heart_rate_max: target_max,
      target_range: "#{target_min}-#{target_max} bpm"
    }
  end
  
  def self.calculate_calories_burned(weight, activity, duration_minutes)
    # MET values for different activities (Metabolic Equivalent of Task)
    met_values = {
      'sleeping' => 0.95,
      'sitting' => 1.3,
      'walking' => 3.5,
      'running' => 8.0,
      'cycling' => 6.0,
      'swimming' => 7.0,
      'weightlifting' => 6.0,
      'yoga' => 2.5,
      'dancing' => 4.5,
      'basketball' => 8.0,
      'soccer' => 7.0,
      'tennis' => 7.0,
      'climbing_stairs' => 8.0
    }
    
    met = met_values[activity.downcase] || 3.5
    hours = duration_minutes / 60.0
    calories_burned = met * weight * hours
    
    {
      weight: weight,
      activity: activity,
      duration_minutes: duration_minutes,
      met_value: met,
      calories_burned: calories_burned.round(2),
      calories_per_minute: (calories_burned / duration_minutes).round(2)
    }
  end
end
