from rest_framework import serializers
from .models import *
from muscle.models import ExerciseMuscle
from equipment.models import WorkoutEquipment
from muscle.serializers import ExerciseMuscleSerializer
from equipment.serializers import WorkoutEquipmentSerializer

class ExerciseMuscleSerializer(serializers.ModelSerializer):
    class Meta:
        model = ExerciseMuscle
        fields = '__all__'

class ExerciseSerializer(serializers.ModelSerializer):
    muscles = serializers.SerializerMethodField()
    equipment = serializers.SerializerMethodField()
    audio_url = serializers.ReadOnlyField()
    video_url = serializers.ReadOnlyField()

    class Meta:
        model = Exercise
        fields = '__all__'

    def get_muscles(self, obj): 
        objs = ExerciseMuscle.objects.filter(exercise=obj)
        data = ExerciseMuscleSerializer(objs, many=True).data
        return data
    
    def get_equipment(self, obj): 
        objs = WorkoutEquipment.objects.filter(exercise=obj)
        data = WorkoutEquipmentSerializer(objs, many=True).data
        return data