"""
Copyright 2020 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

"""
This module contains the top-down erro debaising.
The top_down_error module can give user result, according to which
column group of column top-down error exist for some perticular
slicing. It will check top-down error for all related groups and
suggest the user to do operation on those groups.
"""
import pandas
from util import aspects
from util.enums import SummaryOperators, Filters, Oversights
from util import constants 

def top_down_error(table, metric, all_dimensions, slice_compare_column, 
                            slice1, slice2, summary_operator, **kwargs):
    """This function will implement the top down error debaising

    Args:
        table: Type-pandas.dataframe
            It has the contents of the csv file
        metric: Type-string
            It is the name of the column according to which we will do 
            grouping, summary operator is applied on metric. Metric 
            could a column containing strings, if we are applying count 
            operator on it.
        dimensions: Type-list of str or None
            It is the name of column we want.
            In query:'compare batsman A and B according to total_runs',
             dimension is 'batsman'. we group by dimensions.
        all_dimension: Type-list of str
            It contains list of all dimensions
        slice_compare_column: Type-string
            name of the slice-compare column.
        slice1: Type-string
            the first value of comparision
        slice2: Type-string
            the second value of comparision
        date_range: Type-tuple
            Tuple of start_date and end_date
        date_column_name: Type-str
            It is the name of column which contains date
        day_first: Type-str
            It is required by datetime.strp_time to parse the date in 
            the format Format Codes
https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior
        slices: Type-List of tuples
            Tuple represents the conditon to keep the row.
            (column_name, filter, value)
            column_name - is the value of the column that the
            condition is applied upon.
            filter - Filters enum members, ex. Filters.IN
        summary_operator: Type-summary_operators enum members
            It denotes the summary operator, after grouping by dimensions.
            ex. SummaryOperators.MAX, SummaryOperators.SUM

    Note-summary_operator is always applied on metric column passed,
         and only when grouping is done

    Returns:
        return a list of dictionary where each dictionary represent
               a debiasing suggestion according to the new column.
    """

    date_column_name = kwargs.get('date_column_name', 'date')
    date_range = kwargs.get('date_range', None)
    day_first = kwargs.get('day_first', '%Y-%m-%d')

    slices = kwargs.get('slices', None)

    dimensions = kwargs.get('dimensions', None)

    table = aspects.apply_date_range(table, date_range,
                                     date_column_name, day_first)