<?php
function systemPrompt() {
    return <<<EOT
You are a professional Fitness & Nutrition Expert. 
Your role is to generate **personalized Gym Diet Plans** based on the user’s input.  
Always follow these rules:

1. Output format: Return JSON with the following structure only (no markdown, no explanations):

{
  "meta": {
    "label": "70kg — Muscle Building, Veg, ₹2500 budget"
  },
  "meals": [
    {
      "name": "Breakfast",
      "time": "8:00 AM",
      "items": [
        { "food": "Oats", "qty": "50g", "protein": 8, "carbs": 30, "fat": 3, "kcal": 190 },
        { "food": "Milk", "qty": "200ml", "protein": 7, "carbs": 10, "fat": 7, "kcal": 130 }
      ],
      "mealTotals": { "protein": 15, "carbs": 40, "fat": 10, "kcal": 320 },
      "alternatives": ["Replace oats with 2 chapatis", "Milk can be replaced with curd"]
    }
  ],
  "totals": {
    "protein": 150,
    "carbs": 280,
    "fat": 65,
    "kcal": 2200
  },
  "tips": [
    "Stay consistent with meal timing",
    "Drink at least 3L of water daily"
  ],
  "supplements": [
    "Whey protein (optional)",
    "Multivitamin (optional)"
  ]
}

2. Macronutrient guidelines:
- Protein: 1.6–2.2 g per kg bodyweight
- Fat: 20–30% of total calories
- Carbs: remaining calories
- Adjust calories based on goal:
   • Muscle building: +300 kcal surplus
   • Weight loss: −300 kcal deficit
   • Fat loss: higher protein, moderate carb deficit

3. Meal plan rules:
- Always include 5–6 meals per day (Breakfast, Snack, Lunch, Pre-workout, Post-workout, Dinner, Bedtime snack).
- Foods must be commonly available and affordable in India.
- Always include per-meal breakdown: protein, carbs, fat, kcal.
- Always include totals, tips, and supplements.

4. Supplements:
- Suggest only if budget allows.
- Mark them as optional.

⚠️ IMPORTANT:
- Output must be **valid JSON only**.
- Do not wrap in ```json fences.
- Do not include explanations.
EOT;
}

function userPrompt($weight, $height, $age, $gender, $goal, $diet, $budget, $wake, $bed, $workout, $notes) {
    return "
I want you to act as a professional Fitness & Nutrition Expert and create a personalized Gym Diet Plan for me.

Here are my details:
- Weight: {$weight} kg
- Height: {$height} cm
- Age: {$age} years
- Gender: {$gender}
- Goal: {$goal}
- Diet Preference: {$diet}
- Monthly Budget: ₹{$budget}
- Morning Wake-up Time: {$wake}
- Bedtime: {$bed}
- Workout Time: {$workout}
- Additional Notes: {$notes}

Please design a daily meal plan that:
1. Matches my calorie and macro needs based on age and goal.
2. Aligns with my wake-up, workout, and sleep timings.
3. Uses affordable, easily available Indian foods.
4. Includes meal timing (Breakfast, Snacks, Lunch, Pre/Post-workout, Dinner, Bedtime Snack).
5. Shows per-meal macros and total daily macros.
6. Suggests supplements if affordable and beneficial.
7. Includes tips for consistency and performance.

⚠️ Reminder:
- Output JSON must strictly follow the required structure.
- No markdown, no extra text.
- Include meals, totals, tips, and supplements.
EOT";
}

function workoutPrompt($fitnessLevel, $equipment, $days, $goal, $duration) {
    $equipLabel = ['none' => 'bodyweight only (no equipment)', 'dumbbells' => 'home gym with dumbbells', 'full_gym' => 'full commercial gym'][strtolower($equipment)] ?? $equipment;
    return "
Create a personalized weekly workout plan based on these details:
- Fitness Level: {$fitnessLevel}
- Equipment: {$equipLabel}
- Training Days per Week: {$days}
- Goal: {$goal}
- Session Duration: {$duration} minutes

Design a structured weekly workout plan that:
1. Splits training days optimally based on goal and days available.
2. Lists each exercise with sets, reps, rest period, and a brief form cue.
3. Includes a warmup and cooldown for each session.
4. Lists rest days explicitly.
5. Provides 3–5 tips for progression and recovery.

⚠️ IMPORTANT — Reply ONLY with valid JSON in this exact structure (no markdown, no fences):
{
  \"json\": {
    \"meta\": { \"label\": \"Intermediate | Full Gym | 4 days/week | Muscle Building\" },
    \"schedule\": [
      {
        \"day\": \"Monday\",
        \"focus\": \"Chest & Triceps\",
        \"warmup\": \"5 min treadmill or jumping jacks\",
        \"exercises\": [
          { \"name\": \"Bench Press\", \"sets\": 4, \"reps\": \"8-10\", \"rest\": \"90s\", \"notes\": \"Keep back flat\" }
        ],
        \"cooldown\": \"5 min static stretching\"
      }
    ],
    \"rest_days\": [\"Thursday\", \"Saturday\", \"Sunday\"],
    \"tips\": [\"Progressive overload every week\", \"Sleep 7-8 hours for recovery\"],
    \"notes\": \"Deload every 4th week by reducing weight 40%\"
  },
  \"summary\": \"4-day intermediate muscle-building plan using full gym equipment.\"
}
Always include: schedule[], rest_days[], tips[], notes.
";
}
