import django_filters

from .models import Item
from django.contrib.auth.models import User

from django.db.models import Q



def get_rad(request):
    if request is None:
        return User.objects.none()

    queryset = User.objects.filter(groups__name="rcr_radiology_assessor")

    return queryset

def get_onc(request):
    if request is None:
        return User.objects.none()

    queryset = User.objects.filter(groups__name="rcr_oncology_assessor")

    return queryset

def get_assessor(request):
    #if request is None:
    #    return User.objects.none()

    queryset = User.objects.filter(Q(groups__name="rcr_oncology_assessor") | Q(groups__name="rcr_radiology_assessor") )

    return queryset

class OutcomeFilter(django_filters.FilterSet):
    class Meta:
        model = Item
        fields = { 
            "outcome": ["exact"],
        }


class ItemFilter(django_filters.FilterSet):
    assigned_to_radiology = django_filters.ModelMultipleChoiceFilter(
        queryset=get_rad, null_label="No user"
    )
    assigned_to_oncology = django_filters.ModelMultipleChoiceFilter(
        queryset=get_onc, null_label="No user"
    )
    assessed_by = django_filters.ModelMultipleChoiceFilter(
        queryset=get_assessor, null_label="No user"
    )
    specialty = django_filters.MultipleChoiceFilter(choices=Item.SPECIALTY_CHOICES
    )

    class Meta:
        model = Item
        fields = { 
            "name": ["icontains"],
            "description": ["icontains"],
            "content_type": ["icontains"],
            "created_on": ["exact"],
            "specialty": ["exact"],
            "targeted_to_group": ["exact"],
            "assigned_to_radiology": ["exact"],
            "assigned_to_oncology": ["exact"],
            "assessed_by": ["exact"],
            "completed_radiology": ["exact"],
            "completed_oncology": ["exact"],
            "radiology_categories": ["exact"],
            "oncology_category": ["exact"],
            "needs_review": ["exact"],
            "outcome": ["exact"],

         }

    #def __init__(
    #    self,
    #    data=None,
    #    queryset=None,
    #    prefix=None,
    #    strict=None,
    #    user=None,
    #    request=None,
    #):
    #    if not request.user.groups.filter(name="rapid_checker").exists():
    #        queryset = queryset.filter(open_access=True) | queryset.filter(
    #            author__id=request.user.id
    #        )
    #    super(RapidFilter, self).__init__(
    #        data=data, queryset=queryset, prefix=prefix, request=request
    #    )
    #    pass
