def calculate_keto_macros(weight_kg, body_fat_percentage, activity_level, goal):
    # Constants for caloric needs
    caloric_needs = {
        "sedentary": 1.2,
        "lightly_active": 1.375,
        "moderately_active": 1.55,
        "very_active": 1.725,
        "extra_active": 1.9
    }
    
    # Constants for goals
    goal_calories = {
        "weight_loss": -500,
        "maintenance": 0,
        "weight_gain": 500
    }
    
    # Calculate lean body mass
    lean_body_mass = weight_kg * (1 - body_fat_percentage / 100)
    
    # Calculate Basal Metabolic Rate (BMR)
    bmr = 370 + (21.6 * lean_body_mass)
    
    # Calculate total daily energy expenditure (TDEE)
    tdee = bmr * caloric_needs[activity_level]
    
    # Adjust TDEE based on goal
    daily_calories = tdee + goal_calories[goal]
    
    # Calculate macros
    protein = lean_body_mass * 2.2  # 2.2 grams of protein per kg of lean body mass
    carbs = 20  # fixed 20 grams of carbs for keto
    fat = (daily_calories - (protein * 4) - (carbs * 4)) / 9  # remaining calories from fat
    
    return {
        "daily_calories": daily_calories,
        "protein_grams": protein,
        "carbs_grams": carbs,
        "fat_grams": fat
    }

# Example usage
weight_kg = 70  # weight in kilograms
body_fat_percentage = 20  # body fat percentage
activity_level = "moderately_active"  # activity level (sedentary, lightly_active, moderately_active, very_active, extra_active)
goal = "maintenance"  # goal (weight_loss, maintenance, weight_gain)

macros = calculate_keto_macros(weight_kg, body_fat_percentage, activity_level, goal)
print(f"Daily Calories: {macros['daily_calories']}")
print(f"Protein: {macros['protein_grams']} grams")
print(f"Carbs: {macros['carbs_grams']} grams")
print(f"Fat: {macros['fat_grams']} grams")