"""
this is pack of tools that should be used externally
"""
import logging
from datetime import datetime, timedelta, date
import symbol


from algo.data.reader import get_data_reader
from algo.data.common import Symbol
from algo.util import get_week_start_day
from algo.data.cache import has_week_data, set_day_cache


log = logging.getLogger(__name__)


def cache_from_influx(weeks_back: int = 19):
    """
    reads last 4 weeks and caches it into the DynamoCache.
    First week is current one if time of execution is at least Saturday UTC
    Otherwise start week will be previous.

    side note:
    data older than week of 2022-10-10 - 2022-10-15 is obtained from local(in house) listener
    """
    # deciding what week to start with,, current week is sunday to saturday so only day current week will be included
    # if today is Saturday. If today is sunday - the week will be previous, from the point of week of Sun - Sat.
    today = datetime.fromisoformat('2022-10-15')   # datetime.utcnow()
    start_week = get_week_start_day(today)
    if today.weekday() != 5:  # Saturday
        log.info('not a Saturday(UTC), so starting with prviouse week')
        start_week = get_week_start_day(today - timedelta(days=7))
    log.info('verifying/building if missing cache for last %s weeks, starting %s', weeks_back, start_week)
    data_reader = get_data_reader()

    for weeks_back in range(weeks_back):
        current_start_datetime = start_week - timedelta(days=weeks_back*7)
        current_start_date = current_start_datetime.date()

        log.info('check cache week started %s', current_start_date)
        for cur_symbol in Symbol:
            log.info('checking %s', cur_symbol)
            if has_week_data(cur_symbol, current_start_date):
                log.info('data is available for the %s, week started %s', cur_symbol, current_start_date)
                continue

            # otherwise making sure that there is enough data to cache it
            days_with_data = 0
            for day_of_week in range(7):
                current_week_date = current_start_date + timedelta(days=day_of_week)
                log.info('verifying dataset availability for the day %s', current_week_date)
                data_stream = data_reader.day_data(current_week_date, cur_symbol)
                all_data = list(data_stream)
                log.info('data count: %s', len(all_data))
                if len(all_data) > 0:
                    days_with_data += 1

            if days_with_data < 5:
                log.info('not enough(%s) data for caching %s, %s', days_with_data, cur_symbol, current_start_date)
                # to the next symbol
                continue

            log.info('days available: %s; caching data %s, %s', days_with_data, cur_symbol, current_start_date)

            for day_of_week in range(7):
                current_week_date = current_start_date + timedelta(days=day_of_week)
                data_stream = data_reader.day_data(current_week_date, cur_symbol)
                set_day_cache(cur_symbol, current_week_date, list(data_stream))
