"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: ksp-servicestelle@sgdnord.rlp.de
Created on: 18.08.22

"""
from django.db.models import QuerySet, Q

from konova.filters.table_filters import AbstractTableFilter, CheckboxTableFilter, QueryTableFilter, \
    SelectionTableFilter


class SelectionCompensationTableFilter(SelectionTableFilter):
    """ Specialization of regular SelectionTableFilter for compensation model

    """
    def filter_reg_office(self, queryset, name, value):
        return queryset.filter(
            intervention__responsible__registration_office=value
        )

    def filter_cons_office(self, queryset, name, value):
        return queryset.filter(
            intervention__responsible__conservation_office=value
        )


class QueryCompensationTableFilter(QueryTableFilter):
    """ Specialization of regular QueryTableFilter for compensation model

    """
    def filter_file_number(self, queryset, name, value) -> QuerySet:
        queryset = queryset.filter(
            Q(intervention__responsible__registration_file_number__icontains=value) |
            Q(intervention__responsible__conservation_file_number__icontains=value)
        )
        return queryset


class CheckboxCompensationTableFilter(CheckboxTableFilter):
    """ Specialization of regular CheckboxTableFilter for compensation model

    """

    def filter_show_all(self, queryset, name, value) -> QuerySet:
        """ Filters queryset depending on value of 'show_all' setting

        Args:
            queryset ():
            name ():
            value ():

        Returns:

        """
        if not value:
            user_teams = self.user.shared_teams
            result = queryset.filter(
                Q(intervention__users__in=[self.user]) |  # requesting user has access
                Q(intervention__teams__in=user_teams)
            ).distinct()
            return result
        else:
            return queryset

    def filter_show_recorded(self, queryset, name, value) -> QuerySet:
        """ Filters queryset depending on value of 'show_recorded' setting

        Args:
            queryset ():
            name ():
            value ():

        Returns:

        """
        if not value:
            return queryset.filter(
                intervention__recorded=None,
            )
        else:
            return queryset


class CompensationTableFilter(AbstractTableFilter):
    """ TableFilter for compensations

    Based widely on InterventionTableFilter.
    Just some minor changes for Compensation model.

    """
    def __init__(self, user=None, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.user = user
        qs = kwargs.get("queryset", None)
        request_data = kwargs.get("data", None)

        # Overwrite all filters for special needs of compensations
        self.selection_filter = SelectionCompensationTableFilter(
            data=request_data,
            queryset=qs,
        )
        self.query_filter = QueryCompensationTableFilter(
            data=request_data,
            queryset=self.selection_filter.qs,
        )
        self.checkbox_filter = CheckboxCompensationTableFilter(
            user=user,
            data=request_data,
            queryset=self.query_filter.qs,
        )
        # Overwrite final queryset as well
        self.qs = self.checkbox_filter.qs
