#!/usr/bin/env python3
"""
Exercise configurations for pose analysis.
Defines angle thresholds, rep detection, and form feedback for each exercise.
"""

from typing import Dict, Optional, Any

# Landmark reference (MediaPipe Pose):
# NOSE, LEFT_EYE, RIGHT_EYE, LEFT_EAR, RIGHT_EAR
# LEFT_SHOULDER, RIGHT_SHOULDER
# LEFT_ELBOW, RIGHT_ELBOW
# LEFT_WRIST, RIGHT_WRIST
# LEFT_HIP, RIGHT_HIP
# LEFT_KNEE, RIGHT_KNEE
# LEFT_ANKLE, RIGHT_ANKLE

EXERCISE_CONFIGS: Dict[str, Dict[str, Any]] = {
    "squat": {
        "name": "Bodyweight Squat",
        "description": "Stand with feet shoulder-width apart, lower until thighs are parallel to ground",
        "muscles": ["quads", "glutes", "hamstrings", "core"],
        "difficulty": "beginner",
        "view": "side",  # Best camera angle
        
        # Primary angle used for rep detection
        "primary_angle": "knee_angle",
        
        # Phase thresholds (degrees)
        "thresholds": {
            "standing": 160,  # Angle above this = standing
            "bottom": 100,    # Angle below this = bottom of squat
        },
        
        # Angles to track
        "angles": {
            "knee_angle": {
                "points": ("LEFT_HIP", "LEFT_KNEE", "LEFT_ANKLE"),
                "min_good": 80,   # Don't go too deep
                "max_good": 110,  # Get deep enough
                "feedback": {
                    "too_low": "Going too deep - watch those knees!",
                    "too_high": "Get lower! Aim for parallel thighs.",
                },
            },
            "hip_angle": {
                "points": ("LEFT_SHOULDER", "LEFT_HIP", "LEFT_KNEE"),
                "min_good": 60,
                "max_good": 120,
                "feedback": {
                    "too_low": "You're folding too far forward",
                    "too_high": "Hinge at the hips more",
                },
            },
            "back_angle": {
                "points": ("NOSE", "LEFT_SHOULDER", "LEFT_HIP"),
                "min_good": 150,  # Keep back relatively straight
                "max_good": 180,
                "feedback": {
                    "too_low": "Keep your chest up - you're leaning forward!",
                },
            },
        },
        
        # Rep detection sequence
        "rep_sequence": ["standing", "descending", "bottom", "ascending", "standing"],
        
        # Tutorial resources
        "tutorial_url": "https://www.youtube.com/watch?v=aclHkVaku9U",
    },
    
    "pushup": {
        "name": "Push-up",
        "description": "Start in plank, lower chest to ground, push back up",
        "muscles": ["chest", "triceps", "shoulders", "core"],
        "difficulty": "beginner",
        "view": "side",
        
        "primary_angle": "elbow_angle",
        
        "thresholds": {
            "standing": 160,  # Arms extended
            "bottom": 90,     # Chest near ground
        },
        
        "angles": {
            "elbow_angle": {
                "points": ("LEFT_SHOULDER", "LEFT_ELBOW", "LEFT_WRIST"),
                "min_good": 70,
                "max_good": 100,
                "feedback": {
                    "too_low": "Don't go too low - protect those shoulders",
                    "too_high": "Go deeper! Get that chest lower.",
                },
            },
            "body_alignment": {
                "points": ("LEFT_SHOULDER", "LEFT_HIP", "LEFT_ANKLE"),
                "min_good": 160,  # Body should be straight
                "max_good": 180,
                "feedback": {
                    "too_low": "Keep your hips up - no sagging!",
                },
            },
        },
        
        "rep_sequence": ["standing", "descending", "bottom", "ascending", "standing"],
        "tutorial_url": "https://www.youtube.com/watch?v=IODxDxX7oi4",
    },
    
    "lunge": {
        "name": "Forward Lunge",
        "description": "Step forward, lower back knee toward ground, push back up",
        "muscles": ["quads", "glutes", "hamstrings"],
        "difficulty": "beginner",
        "view": "side",
        
        "primary_angle": "front_knee_angle",
        
        "thresholds": {
            "standing": 160,
            "bottom": 100,
        },
        
        "angles": {
            "front_knee_angle": {
                "points": ("LEFT_HIP", "LEFT_KNEE", "LEFT_ANKLE"),
                "min_good": 80,
                "max_good": 100,
                "feedback": {
                    "too_low": "Front knee too bent - don't let it go past your toes",
                    "too_high": "Get lower! Aim for 90 degrees.",
                },
            },
            "torso_angle": {
                "points": ("NOSE", "LEFT_SHOULDER", "LEFT_HIP"),
                "min_good": 160,
                "max_good": 180,
                "feedback": {
                    "too_low": "Keep your torso upright!",
                },
            },
        },
        
        "rep_sequence": ["standing", "descending", "bottom", "ascending", "standing"],
        "tutorial_url": "https://www.youtube.com/watch?v=QOVaHwm-Q6U",
    },
    
    "plank": {
        "name": "Plank Hold",
        "description": "Hold a straight body position on forearms and toes",
        "muscles": ["core", "shoulders", "back"],
        "difficulty": "beginner",
        "view": "side",
        "is_hold": True,  # This is a hold, not reps
        
        "angles": {
            "body_alignment": {
                "points": ("LEFT_SHOULDER", "LEFT_HIP", "LEFT_ANKLE"),
                "min_good": 165,
                "max_good": 180,
                "feedback": {
                    "too_low": "Hips sagging! Engage your core and lift.",
                    "too_high": "Hips too high - flatten out your body.",
                },
            },
            "head_position": {
                "points": ("NOSE", "LEFT_SHOULDER", "LEFT_HIP"),
                "min_good": 160,
                "max_good": 180,
                "feedback": {
                    "too_low": "Keep your head in line with your spine - look at the floor.",
                },
            },
        },
        
        "tutorial_url": "https://www.youtube.com/watch?v=ASdvN_XEl_c",
    },
    
    "deadlift": {
        "name": "Deadlift (Bodyweight/Light)",
        "description": "Hinge at hips, keeping back straight, lower and raise torso",
        "muscles": ["hamstrings", "glutes", "back", "core"],
        "difficulty": "intermediate",
        "view": "side",
        
        "primary_angle": "hip_angle",
        
        "thresholds": {
            "standing": 160,
            "bottom": 90,
        },
        
        "angles": {
            "hip_angle": {
                "points": ("LEFT_SHOULDER", "LEFT_HIP", "LEFT_KNEE"),
                "min_good": 70,
                "max_good": 110,
                "feedback": {
                    "too_low": "Don't go too low - feel the stretch in hamstrings",
                    "too_high": "Get a deeper hip hinge",
                },
            },
            "back_angle": {
                "points": ("NOSE", "LEFT_SHOULDER", "LEFT_HIP"),
                "min_good": 160,
                "max_good": 180,
                "feedback": {
                    "too_low": "STOP! Your back is rounding. Keep it flat!",
                },
            },
            "knee_bend": {
                "points": ("LEFT_HIP", "LEFT_KNEE", "LEFT_ANKLE"),
                "min_good": 160,
                "max_good": 180,
                "feedback": {
                    "too_low": "Keep your legs straighter - this isn't a squat",
                },
            },
        },
        
        "rep_sequence": ["standing", "descending", "bottom", "ascending", "standing"],
        "tutorial_url": "https://www.youtube.com/watch?v=op9kVnSso6Q",
    },
    
    "bicep_curl": {
        "name": "Bicep Curl",
        "description": "Curl weight from arms extended to shoulders",
        "muscles": ["biceps", "forearms"],
        "difficulty": "beginner",
        "view": "side",
        
        "primary_angle": "elbow_angle",
        
        "thresholds": {
            "standing": 150,  # Arms extended
            "bottom": 50,     # Fully curled (note: inverted for curls)
        },
        
        "angles": {
            "elbow_angle": {
                "points": ("LEFT_SHOULDER", "LEFT_ELBOW", "LEFT_WRIST"),
                "min_good": 30,   # Full contraction
                "max_good": 50,
                "feedback": {
                    "too_high": "Squeeze harder at the top!",
                },
            },
            "shoulder_stability": {
                "points": ("LEFT_HIP", "LEFT_SHOULDER", "LEFT_ELBOW"),
                "min_good": 0,
                "max_good": 20,
                "feedback": {
                    "too_high": "Keep your elbows pinned - don't swing!",
                },
            },
        },
        
        "rep_sequence": ["standing", "descending", "bottom", "ascending", "standing"],
        "tutorial_url": "https://www.youtube.com/watch?v=ykJmrZ5v0Oo",
    },
}


def get_exercise_config(exercise: str) -> Optional[Dict[str, Any]]:
    """Get configuration for an exercise by name."""
    return EXERCISE_CONFIGS.get(exercise.lower().replace(" ", "_").replace("-", "_"))


def list_exercises() -> list[str]:
    """List all available exercise names."""
    return list(EXERCISE_CONFIGS.keys())


def get_exercise_info(exercise: str) -> Optional[Dict[str, str]]:
    """Get basic info about an exercise (without technical details)."""
    config = get_exercise_config(exercise)
    if not config:
        return None
        
    return {
        "name": config["name"],
        "description": config["description"],
        "muscles": config["muscles"],
        "difficulty": config["difficulty"],
        "tutorial_url": config.get("tutorial_url"),
    }
