from datetime import datetime, timedelta
from scripts.constants.app_configuration import DateRange, required_tz
from loguru import logger
import pytz


class KairosStartEndDate:
    @staticmethod
    def training_start_end_date():
        try:
            logger.info("Fetching the start and end time for the historical data datetime fetch")
            local_timezone = pytz.timezone(required_tz)
            start_date_timestamp = DateRange.start_date
            end_date_timestamp = DateRange.end_date
            if (start_date_timestamp is not None) and (start_date_timestamp.lower() != "none"):
                start_date = datetime.fromtimestamp((int(start_date_timestamp)/1000)).strftime('%Y-%m-%d %H:%M:%S')
                start_date = datetime.strptime(start_date, "%Y-%m-%d %H:%M:%S")
                start_date = start_date.astimezone(local_timezone)

            else:
                start_date = datetime.now(pytz.utc) - timedelta(days=int(DateRange.start_relative_days))
                start_date = start_date.astimezone(local_timezone).replace(hour=5, minute=0, second=0, microsecond=0)

            if (end_date_timestamp is not None) and (end_date_timestamp.lower() != "none"):
                end_date = datetime.fromtimestamp((int(end_date_timestamp)/1000)).strftime('%Y-%m-%d %H:%M:%S')
                end_date = datetime.strptime(end_date, "%Y-%m-%d %H:%M:%S")
            else:
                end_date = datetime.now(pytz.utc) - timedelta(days=int(DateRange.end_relative_days))

            s_date = start_date
            e_date = end_date.astimezone(local_timezone).replace(hour=5, minute=0, second=0, microsecond=0)
            return s_date, e_date
        except Exception as e:
            logger.exception(f"Exception occurred - {e}", exc_info=True)

    @staticmethod
    def get_start_time_and_end_time():
        try:
            logger.info("Finding the start and end time for the data fetch - relative recent data")
            # Time Intervals
            # 01:00-01:15, 01:15-01:30, 01:30-01:45, 01:45-02:00
            current_time = datetime.now()
            if current_time.minute >= 45:
                # timeframe to consider 30-45
                start_time_value = current_time.replace(minute=30, second=0, microsecond=0)
            elif current_time.minute >= 30:
                # timeframe to consider 15-30
                start_time_value = current_time.replace(minute=15, second=0, microsecond=0)
            elif current_time.minute >= 10:
                # timeframe to consider 00-15
                start_time_value = current_time.replace(minute=0, second=0, microsecond=0)
            else:
                # timeframe to consider 45-00
                start_time_value = current_time.replace(minute=45, second=0, microsecond=0)
                start_time_value = start_time_value - timedelta(hours=1, minutes=0)
            start_time = start_time_value
            end_time = start_time + timedelta(hours=0, minutes=14, seconds=59)
            start_time = int(start_time.timestamp() * 1000)
            end_time = int(end_time.timestamp() * 1000)
            return start_time, end_time
        except Exception as e:
            logger.debug('Exception while inference time')
            logger.exception(f'Exception - {e}')
