"""Establishment app filters."""
from django.core.validators import EMPTY_VALUES
from django_filters import rest_framework as filters

from establishment import models


class EstablishmentFilter(filters.FilterSet):
    """Establishment filter set."""

    tag_id = filters.NumberFilter(field_name='tags__metadata__id',)
    award_id = filters.NumberFilter(field_name='awards__id',)
    search = filters.CharFilter(method='search_text')
    type = filters.CharFilter(method='by_type')
    subtype = filters.CharFilter(method='by_subtype')
    city_id = filters.CharFilter(field_name='address__city__id')
    city_name = filters.CharFilter(field_name='address__city__name')

    class Meta:
        """Meta class."""

        model = models.Establishment
        fields = (
            'tag_id',
            'award_id',
            'search',
            'type',
            'subtype',
            'city_id',
            'city_name',
        )

    def search_text(self, queryset, name, value):
        """Search text."""
        if value not in EMPTY_VALUES:
            return queryset.search(value, locale=self.request.locale)
        return queryset

    def by_type(self, queryset, name, value):
        if value not in EMPTY_VALUES:
            return queryset.by_type(value)
        return queryset

    def by_subtype(self, queryset, name, value):
        if value not in EMPTY_VALUES:
            return queryset.by_subtype(value)
        return queryset


class EstablishmentTypeTagFilter(filters.FilterSet):
    """Establishment tag filter set."""

    type_id = filters.NumberFilter(field_name='id')

    class Meta:
        """Meta class."""

        model = models.EstablishmentType
        fields = (
            'type_id',
        )


class EmployeeBackFilter(filters.FilterSet):
    """Employee filter set."""

    search = filters.CharFilter(method='search_by_name_or_last_name')

    class Meta:
        """Meta class."""

        model = models.Employee
        fields = (
            'search',
        )

    def search_by_name_or_last_name(self, queryset, name, value):
        """Search by name or last name."""
        if value not in EMPTY_VALUES:
            return queryset.search_by_name_or_last_name(value)
        return queryset
