from datetime import datetime, time
import pytz

KST = pytz.timezone("Asia/Seoul")
US_EASTERN = pytz.timezone("US/Eastern")

class MarketSchedule:
    """
    Checks if the market is open based on current time and market type.
    """
    
    @staticmethod
    def is_market_open(market: str) -> bool:
        """
        :param market: 'Domestic' or 'Overseas'
        """
        if market == "Domestic":
            return MarketSchedule._is_domestic_open()
        elif market == "Overseas":
            return MarketSchedule._is_overseas_open()
        return False

    @staticmethod
    def _is_domestic_open() -> bool:
        now = datetime.now(KST)
        
        # 1. Weekend Check (0=Mon, 4=Fri, 5=Sat, 6=Sun)
        if now.weekday() >= 5:
            return False
            
        # 2. Time Check (09:00 ~ 15:30)
        current_time = now.time()
        start = time(9, 0)
        end = time(15, 30)
        
        return start <= current_time <= end

    @staticmethod
    def _is_overseas_open() -> bool:
        # US Market: 09:30 ~ 16:00 (US Eastern Time)
        # pytz handles DST automatically for US/Eastern
        now = datetime.now(US_EASTERN)
        
        # 1. Weekend Check
        if now.weekday() >= 5:
            return False
            
        # 2. Time Check
        current_time = now.time()
        start = time(9, 30)
        end = time(16, 0)
        
        return start <= current_time <= end

market_schedule = MarketSchedule()
