from rest_framework import serializers

from .models import Member, Job, JobGroup, JobMember


class MemberSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Member
        fields = [
            "id",
            "firstname",
            "surname",
            "nickname",
            "mailaccount",
            "role",
            "description",
            "image",
            "birthday",
        ]


class JobGroupSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = JobGroup
        fields = ["id", "name", "shortterm", "slug"]


class JobSerializer(serializers.HyperlinkedModelSerializer):
    # job_group = JobGroupSerializer()
    job_group = serializers.SlugRelatedField(
        slug_field="slug", queryset=JobGroup.objects
    )

    class Meta:
        model = Job
        fields = ["id", "name", "shortterm", "job_group", "slug"]


class JobMemberSerializer(serializers.HyperlinkedModelSerializer):
    # member = MemberSerializer()
    # job = JobSerializer()
    job = serializers.SlugRelatedField(slug_field="slug", queryset=Job.objects)
    member = serializers.SlugRelatedField(
        slug_field="mailaccount", queryset=Member.objects
    )

    class Meta:
        model = JobMember
        fields = ["id", "job_start", "job_end", "member", "job", "job_role"]


#    def create(self, validated_data):
#        member_data = validated_data.pop('member')
#        print(member_data)
#        member = Member.objects.get(firstname=member_data['firstname'], surname=member_data['surname'])
#
#        job_data = validated_data.pop('job')
#        print(job_data)
#        job = Job.objects.get(name=job_data['name'])

#        jobmember = JobMember.objects.create(member=member, job=job, **validated_data)

#        return jobmember
