from django_filters import rest_framework as filters
from apps.pm.models import Mtask, Utask
from django.utils import timezone
from datetime import datetime, timedelta
from django.db.models import F


class UtaskFilter(filters.FilterSet):
    tag = filters.CharFilter(method='filter_tag', label='done, not_done')

    class Meta:
        model = Utask
        fields = {
            "state": ["exact", "in"],
            "start_date": ["exact", "gte", "lte"],
            "end_date": ["exact", "gte", "lte"],
            "material": ["exact"],
            "material__type": ["exact"],
            "material__is_hidden": ["exact"],
            "belong_dept": ["exact"],
            "belong_dept__name": ["exact"],
            "mgroup__belong_dept__name": ["exact"],
            "mtask_utask__mgroup": ["exact"],
            "mtask_utask__mgroup__name": ["exact"],
            "mtask_utask__mgroup__belong_dept": ['exact'],
            "mtask_utask__mgroup__belong_dept__name": ['exact']
        }

    def filter_queryset(self, queryset):
        queryset = super().filter_queryset(queryset)
        return queryset.distinct()

    def filter_tag(self, queryset, name, value):
        if value == 'done':
            queryset = queryset.filter(count_ok__gte=F('count'))
        elif value == 'not_done':
            queryset = queryset.filter(count_ok__lt=F('count'))
        return queryset


class MtaskFilter(filters.FilterSet):
    tag = filters.CharFilter(method='filter_tag', label='done, not_done')

    class Meta:
        model = Mtask
        fields = {
            "state": ["exact", "in"],
            "route": ["exact"],
            "route__material_in": ["exact"],
            "is_count_utask": ["exact"],
            "start_date": ["exact", "gte", "lte"],
            "end_date": ["exact", "gte", "lte"],
            "mgroup": ["exact", "in"],
            "mgroup__name": ["exact"],
            "mgroup__cate": ["exact"],
            "mgroup__process": ["exact"],
            "mgroup__process__cate": ["exact"],
            "material_out": ["exact"],
            "material_out__name": ["exact"],
            "material_out__type": ["exact"],
            "material_out__is_hidden": ["exact"],
            "mgroup__belong_dept__name": ["exact"],
            "mgroup__belong_dept": ["exact"],
            "utask": ["exact"]
        }

    def filter_tag(self, queryset, name, value):
        # now = timezone.now()
        # day7_after = now + timedelta(days=7)
        # if value == 'near_done':
        #     queryset = queryset.filter(count_ok__lt=F('count'),
        #                                end_date__lte=day7_after.date(),
        #                                end_date__gte=now.date())
        # elif value == 'out_done':
        #     queryset = queryset.filter(count_ok__lt=F('count'),
        #                                end_date__lt=now.date())
        if value == 'done':
            queryset = queryset.filter(count_ok__gte=F('count'))
        elif value == 'not_done':
            queryset = queryset.filter(count_ok__lt=F('count'))
        return queryset
