from django.shortcuts import render
from exercise.models import Exercise
from muscle.models import Muscle, ExerciseMuscle
from equipment.models import Equipment, WorkoutEquipment
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import status

# Create your views here.
@api_view(['GET'])
def sync_equipment(request):
    all_exercise = Exercise.objects.all()
    for exercise in all_exercise:
        all_equipment =  exercise.equipment_required.split(',')
        for equipment in all_equipment:
            if len(equipment) > 0:
                try:
                    equipment_obj = Equipment.objects.get(name=equipment)
                    WorkoutEquipment.objects.create(exercise=exercise, equipment=equipment_obj).save()
                except Equipment.DoesNotExist:
                    pass
                

    return Response(status=status.HTTP_200_OK)

@api_view(['GET'])
def sync_muscle_groups(request):
    all_exercise = Exercise.objects.all()
    for exercise in all_exercise:
        all_muscle_groups =  exercise.muscle_groups.split(',')
        for muscle_group in all_muscle_groups:
            if len(muscle_group) > 0:
                try:
                    print(muscle_group)
                    muscle_obj = Muscle.objects.get(name=muscle_group)
                    print(muscle_obj)
                    ExerciseMuscle.objects.create(exercise=exercise, muscle=muscle_obj).save()
                except MuscleGroup.DoesNotExist:
                    pass
                

    return Response(status=status.HTTP_200_OK)