import logging
from datetime import datetime, time
import pytz

logger = logging.getLogger("TradingBot.MacroTiming")

class MacroTiming:
    def __init__(self, config):
        self.config = config
        self.timezone = pytz.timezone('UTC')
        logger.info("MacroTiming initialized - Trading allowed at any time")
        
        # Define high-probability trading windows
        self.trading_windows = {
            # London-New York overlap (highest liquidity)
            'london_ny_overlap': {
                'start': time(13, 0),  # 13:00 UTC
                'end': time(16, 0),    # 16:00 UTC
                'weight': 1.0          # Highest weight
            },
            # London open
            'london_open': {
                'start': time(7, 0),   # 07:00 UTC
                'end': time(9, 0),     # 09:00 UTC
                'weight': 0.8
            },
            # NY 10 AM (14:00 UTC) - High volume window
            'ny_10am': {
                'start': time(14, 0),  # 14:00 UTC
                'end': time(15, 0),    # 15:00 UTC
                'weight': 0.9          # High weight due to significant volume
            },
            # NY 2 PM (18:00 UTC) - Power hour
            'ny_2pm': {
                'start': time(18, 0),  # 18:00 UTC
                'end': time(19, 0),    # 19:00 UTC
                'weight': 0.85         # Important price action window
            },
            # Regular NY open
            'ny_open': {
                'start': time(13, 30), # 13:30 UTC
                'end': time(15, 30),   # 15:30 UTC
                'weight': 0.8
            },
            # Asian session (Tokyo)
            'tokyo': {
                'start': time(0, 0),   # 00:00 UTC
                'end': time(6, 0),     # 06:00 UTC
                'weight': 0.6
            }
        }
        
        # Define specific high-probability times
        self.high_prob_times = {
            # Major economic releases
            'fomc': {
                'time': time(18, 0),   # 18:00 UTC
                'window': 60,          # minutes
                'weight': 0.9
            },
            # London fix
            'london_fix': {
                'time': time(15, 0),   # 15:00 UTC
                'window': 30,          # minutes
                'weight': 0.8
            },
            # NY Pre-market
            'ny_premarket': {
                'time': time(13, 15),  # 13:15 UTC
                'window': 30,          # minutes
                'weight': 0.75
            }
        }

    def get_timing_score(self, data_cache):
         """Always returns a timing score of 1.0 (for testing)."""
         logger.debug("get_timing_score (testing) – returning 1.0")
         return 1.0

    def get_current_window_score(self):
         """Always returns maximum score to allow trading at any time."""
         logger.debug("Trading window score: 1.0 (Trading allowed at any time)")
         return 1.0

    def should_trade(self, signal):
         """Always returns True to allow trading at any time."""
         logger.debug("Trade timing always favorable (Trading allowed at any time)")
         return True, 1.0

    def _is_time_in_window(self, current_time, start_time, end_time):
        """Check if current time is within a trading window."""
        if start_time <= end_time:
            return start_time <= current_time <= end_time
        else:  # Window crosses midnight
            return current_time >= start_time or current_time <= end_time

    def _is_time_near_event(self, current_time, event_time, window_minutes):
        """Check if current time is near a specific event."""
        current_minutes = current_time.hour * 60 + current_time.minute
        event_minutes = event_time.hour * 60 + event_time.minute
        
        # Calculate minutes difference, handling day boundary
        diff = min(
            abs(current_minutes - event_minutes),
            abs(current_minutes - (event_minutes + 1440)),  # 1440 = minutes in a day
            abs((current_minutes + 1440) - event_minutes)
        )
        
        return diff <= window_minutes / 2  # Window/2 minutes before and after event

# Example usage
if __name__ == "__main__":
    config = {"trading": {}}
    macro_timing = MacroTiming(config)
    score = macro_timing.get_current_window_score()
    print(f"Current trading window score: {score:.2f}") 