import time
import json
from vs_types import GroupConfig
from gco_wrapper import GCOWrapper
from logger_setup import setup_app_logger

# 导入拆分后的两个类
from booker_builtin import BuiltinBookerGCO
from booker_order import OrderBookerGCO

def main():
    app_logger = setup_app_logger("Booker")
    app_logger.info("Booker Logger is ready! All VSC macros are hooked.")

    with open("config/config.json", "r", encoding="utf-8") as f:
        cfg_data = json.load(f)

    redis_conf = cfg_data.get('redis')
    groups_conf = cfg_data.get('group_list')
    wrappers = []

    for item in groups_conf:
        cfg = GroupConfig.from_json(item)
        if not cfg.enable: continue
            
        # === 核心：根据配置自动选择实现类 ===
        if cfg.booker.account_source == "order":
            gco_class = OrderBookerGCO
            app_logger.info(f"[{cfg.identifier}] Mode: ORDER (Bound)")
        else:
            gco_class = BuiltinBookerGCO
            app_logger.info(f"[{cfg.identifier}] Mode: BUILT-IN (Unbound)")

        wrapper = GCOWrapper(gco_class=gco_class, gco_cfg=cfg, redis_conf=redis_conf)
        wrapper.load()
        wrapper.start()
        wrappers.append(wrapper)
    
    app_logger.info(f"Successfully started {len(wrappers)} Booker groups. Press Ctrl+C to stop.")
    
    try:
        while True: time.sleep(1)
    except KeyboardInterrupt:
        app_logger.info("Shutting down Bookers...")
        for wrapper in wrappers: wrapper.stop()

if __name__ == "__main__":
    main()