#!/usr/bin/env python3
"""Update meal-history.md from data.json history"""
import json
from datetime import datetime

DATA_FILE = '/home/baymoond/apitools.baymoondev.com/fueltrack/data.json'
HISTORY_FILE = '/home/baymoond/apitools.baymoondev.com/fueltrack/meal-history.md'

with open(DATA_FILE) as f:
    data = json.load(f)

history = data.get('meals', [])
now = datetime.now().strftime('%Y-%m-%d %H:%M')

# Build markdown
md = """# Meal History

| Date | Meals | Calories | Protein | Carbs | Fat |
|------|-------|----------|---------|-------|-----|
"""

for day in reversed(history):
    date = day.get('date', 'N/A')
    meals = day.get('meals', [])
    totals = day.get('totals', {})
    meal_count = len(meals)
    cal = totals.get('consumed', 0)
    protein = totals.get('protein', 0)
    carbs = totals.get('carbs', 0)
    fat = totals.get('fat', 0)
    md += f"| {date} | {meal_count} | {cal} | {protein}g | {carbs}g | {fat}g |\n"

# Add user info and footer
md += f"""
---

## User Profile

| Setting | Value |
|---------|-------|
| Current Weight | 88kg |
| Goal Weight | 80kg |
| Daily Target | 2,187 cal (-1,000 deficit) |
| Goal | 88kg → 80kg |
| Timeline | 8 weeks |
| Protein | 219g |
| Carbs | 219g |
| Fat | 49g |

---

## Activity Schedule (Weekly)

- Gym: 3x
- Tennis: 1-2x
- Boxing: 1-2x
- Running: 1x
- Walking: 2x (6-8km each)

---

*Last updated: {now} GMT+2*
*Data source: #fuel-track Slack channel*
"""

with open(HISTORY_FILE, 'w') as f:
    f.write(md)

print(f"Updated {HISTORY_FILE} with {len(history)} history entries")