# -*- coding: utf-8 -*-

import datetime
import pandas as pd
import numpy as np
import tables as tb
import hashlib
from collections import defaultdict
from pyscd.progress import Progress
import logging
logging.basicConfig(level=logging.DEBUG)
log = logging.getLogger(__name__)


class SlowlyChangingDimension(object):
    """A class for accessing a slowly changing dimension of types 1 and 2.
    """

    def __init__(self, connection,
                 lookupatts, type1atts, type2atts,
                 key='scd_id',
                 fromatt='scd_valid_from',
                 toatt='scd_valid_to',
                 maxto='2199-12-31',
                 versionatt='scd_version',
                 currentatt='scd_current',
                 hashatt='scd_hash',
                 asof=None,
                 verbose=True):
        """
        Parameters
        ----------

        connection
            Required. The Tables.Table pointing to this dimension. The table
            should already have exists and include the dimension specific
            columns:
            - scd_id          = tb.Int64Col(pos=0)
            - scd_valid_from  = tb.Int64Col(pos=1)
            - scd_valid_to    = tb.Int64Col(pos=2)
            - scd_version     = tb.Int16Col(pos=3)
            - scd_current     = tb.BoolCol(pos=4)

        lookupatts
            Required. A list of the columns that uniquely identify a dimension
            member.

        type1atts
            Required. A list of the columns that should have type1 updates
            applied.

        type2atts
            Required. A list of the columns that should have version tracking.

        key
            Optional. String with the name of the primary key of the dimension
            table.

        fromatt
            Optional. String with the name of the column telling from when the
            version becomes valid.
            Default 'scd_valid_from'.

        toatt
            Optional. String with the name of the column telling ultil when the
            version is valid.
            Default 'scd_valid_to'.

        maxto
            Optional. The date to use for toatt for new versions. Must be a date