from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import viewsets

from .models import Member, JobMember, JobGroup, Job
from .serializers import (
    MemberSerializer,
    JobSerializer,
    JobGroupSerializer,
    JobMemberSerializer,
)


class MemberViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to be viewed or edited.
    """

    queryset = Member.all_members.order_by("nickname")
    serializer_class = MemberSerializer

    # permission_classes = [permissions.IsAuthenticated]
    filter_backends = [DjangoFilterBackend]
    filterset_fields = ["nickname", "mailaccount"]
    # lookup_field = 'name'

    def pre_save(self, obj):
        obj.image = self.request.FILES.get("image")


class JobGroupViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to be viewed or edited.
    """

    queryset = JobGroup.all_jobgroups.all()
    serializer_class = JobGroupSerializer

    filter_backends = [DjangoFilterBackend]
    filterset_fields = ["name", "slug"]
    # lookup_field = 'name'


class JobViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to be viewed or edited.
    """

    queryset = Job.objects.all()
    serializer_class = JobSerializer
    filter_backends = [DjangoFilterBackend]
    filterset_fields = ["name", "slug"]
    # lookup_field = 'slug'


class JobMemberViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to be viewed or edited.
    """

    queryset = JobMember.objects.all()
    serializer_class = JobMemberSerializer
    filter_backends = [DjangoFilterBackend]
    filterset_fields = ["member", "job", "job_role", "job_start"]
    # lookup_field = 'nickname'
