from django_filters import rest_framework as filters

from poizonstore.utils import DateFromToRangeFilter
from .models import Checklist, Gift


class GiftFilter(filters.FilterSet):
    for_price = filters.NumberFilter(method='filter_for_price')

    class Meta:
        model = Gift
        fields = ('for_price',)

    def filter_for_price(self, queryset, name, value):
        return queryset.filter(min_price__lte=value)


class ChecklistFilter(filters.FilterSet):
    status = filters.MultipleChoiceFilter(choices=Checklist.Status.CHOICES)
    delivery_code = filters.CharFilter(method='filter_delivery_code')

    created_at = DateFromToRangeFilter()
    status_updated_at = DateFromToRangeFilter()

    class Meta:
        model = Checklist
        fields = ('status', 'delivery_code', 'created_at', 'status_updated_at')

    def filter_delivery_code(self, queryset, name, value):
        return queryset.filter(poizon_tracking__iendswith=value)
