import django_filters
from django.db.models import Q

from .models import Event, Status, Prospect


class ProspectFilter(django_filters.FilterSet):
    zip_code = django_filters.CharFilter(lookup_expr='istartswith', label='Code postal')
    events = django_filters.ModelMultipleChoiceFilter(
        queryset=Event.objects.all(),
        field_name='events',
    )
    city = django_filters.CharFilter(lookup_expr='icontains', label='Ville')
    name = django_filters.CharFilter(method='filter_name', label='Nom')

    def filter_name(self, queryset, name, value):
        return queryset.filter(
            Q(first_name__icontains=value) | Q(last_name__icontains=value) | Q(entity_name__icontains=value)
        )

    class Meta:
        model = Prospect
        fields = ['name', 'city', 'events', 'zip_code']
