from rest_framework.serializers import ModelSerializer
from .models import Award, Expert, Paper, Project, WorkExperience

class WorkExperienceSimpleSerializer(ModelSerializer):
    class Meta:
        model = WorkExperience
        fields = ['id', 'name']

class ProjectSimpleSerializer(ModelSerializer):
    class Meta:
        model = Project
        fields = ['id', 'name']

class PaperSimpleSerializer(ModelSerializer):
    class Meta:
        model = Paper
        fields = ['id', 'name']

class AwardSimpleSerializer(ModelSerializer):
    class Meta:
        model = Award
        fields = ['id', 'name']

class ExpertListSerializer(ModelSerializer):
    workexperience_ = WorkExperienceSimpleSerializer(source='workexperience_expert', many=True, read_only=True)
    project_ = ProjectSimpleSerializer(source='project_expert', many=True, read_only=True)
    paper_ = PaperSimpleSerializer(source='paper_expert', many=True, read_only=True)
    award_ = AwardSimpleSerializer(source='award_expert', many=True, read_only=True)
    class Meta:
        model = Expert
        fields = '__all__'
    
    @staticmethod
    def setup_eager_loading(queryset):
        """ Perform necessary eager loading of data. """
        queryset = queryset.prefetch_related('workexperience_expert','project_expert', 'paper_expert', 'award_expert')
        return queryset