"""
FuelTrack - Personal Nutrition & Activity Tracker
"""

import json
import os
from datetime import datetime, timedelta

# Load config
CONFIG_PATH = os.path.join(os.path.dirname(__file__), 'config.json')

def load_config():
    with open(CONFIG_PATH, 'r') as f:
        return json.load(f)

def calculate_tdee(user):
    """Calculate TDEE using Mifflin-St Jeor equation (male)"""
    # BMR = 10*weight + 6.25*height - 5*age + 5
    # Assuming age 30 for now (can be added to config)
    age = 30
    weight = user['weight_kg']
    height = user['height_cm']
    
    bmr = (10 * weight) + (6.25 * height) - (5 * age) + 5
    
    # Activity multiplier: very active (1.725)
    tdee = bmr * 1.725
    
    return round(tdee)

def calculate_daily_targets(user, tdee):
    """Calculate daily calorie/macro targets for weight loss"""
    goal_weight = user['goal_weight_kg']
    current_weight = user['weight_kg']
    weight_to_lose = current_weight - goal_weight
    
    # Calories: 7700 per kg / 8 weeks = ~1000 cal deficit/day
    target_calories = tdee - 1000
    
    # Minimum safe floor
    if target_calories < 1500:
        target_calories = 1500
    
    # Macros (40/40/20 split for active people)
    protein_g = (target_calories * 0.40) / 4
    carbs_g = (target_calories * 0.40) / 4
    fat_g = (target_calories * 0.20) / 9
    
    return {
        'calories': round(target_calories),
        'protein_g': round(protein_g),
        'carbs_g': round(carbs_g),
        'fat_g': round(fat_g)
    }

def get_activities(days=7):
    """Fetch recent activities from Strava"""
    # This will be implemented with actual API call
    pass

def analyze_food_from_image(image_path):
    """
    Analyze food from image and estimate nutrition
    This is where we'll integrate the AI food recognition
    """
    # TODO: Integrate with LogMeal/Calorie Mama API
    pass

def main():
    config = load_config()
    user = config['user']
    
    print("=" * 50)
    print("FUELTRACK - Daily Nutrition & Activity Tracker")
    print("=" * 50)
    print(f"\nUser: {user['name']}")
    print(f"Current: {user['weight_kg']}kg | Goal: {user['goal_weight_kg']}kg")
    
    # Calculate TDEE
    tdee = calculate_tdee(user)
    print(f"\n📊 Your TDEE (maintenance): {tdee} cal/day")
    
    # Calculate targets
    targets = calculate_daily_targets(user, tdee)
    print(f"🎯 Daily Target (for {user['timeline_weeks']} week goal):")
    print(f"   Calories: {targets['calories']} cal")
    print(f"   Protein:  {targets['protein_g']}g")
    print(f"   Carbs:    {targets['carbs_g']}g")
    print(f"   Fat:      {targets['fat_g']}g")
    
    print("\n" + "=" * 50)

if __name__ == "__main__":
    main()