import os
import sys
from dotenv import load_dotenv

sys.path.append(os.path.join(os.path.dirname(__file__), 'src'))

# from strategies.cn_strategy import CN_Strategy
# from strategies.us_strategy import US_Strategy
# from strategies.hk_strategy import HK_Strategy
# from strategies.jp_strategy import JP_Strategy

def get_strategy(market, stock_code, tushare_token=None, av_key=None):
    market = market.upper()
    if market == 'CN':
        from strategies.cn_strategy import CN_Strategy
        return CN_Strategy(stock_code, tushare_token)
    elif market == 'US':
        from strategies.us_strategy import US_Strategy
        return US_Strategy(stock_code, av_key)
    elif market == 'HK':
        from strategies.hk_strategy import HK_Strategy
        ifind_token = os.getenv('IFIND_REFRESH_TOKEN')
        return HK_Strategy(stock_code, ifind_token)
    elif market == 'JP':
        from strategies.jp_strategy import JP_Strategy
        ifind_token = os.getenv('IFIND_REFRESH_TOKEN')
        return JP_Strategy(stock_code, ifind_token)
    elif market == 'VN':
        from strategies.vn_strategy import VN_Strategy
        ifind_token = os.getenv('IFIND_REFRESH_TOKEN')
        return VN_Strategy(stock_code, ifind_token)
    else:
        raise ValueError(f"Unsupported market: {market}")

def main():
    load_dotenv()
    tushare_token = os.getenv('TUSHARE_TOKEN')
    av_key = os.getenv('ALPHA_VANTAGE_KEY')

    if len(sys.argv) > 2:
        market = sys.argv[1]
        symbol = sys.argv[2]
        strategy = get_strategy(market, symbol, tushare_token, av_key)
        strategy.execute()
    else:
        print("Usage: python run_fetcher.py <MARKET> <SYMBOL>")
        print("Running default test cases:")
        
        # Test CN
        cn_strategy = get_strategy('CN', '600519.SH', tushare_token)
        cn_strategy.execute()
        
        # Test US
        us_strategy = get_strategy('US', 'AAPL', av_key=av_key)
        us_strategy.execute()

        # Test HK
        hk_strategy = get_strategy('HK', '00700.HK', av_key=av_key)
        hk_strategy.execute()

        # Test JP
        jp_strategy = get_strategy('JP', '7203.T') # Toyota
        jp_strategy.execute()

if __name__ == "__main__":
    main()
