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

def main():
    app_logger = setup_app_logger("Sentinel")
    app_logger.info("Sentinel 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')

    # 用于保存所有启动的 wrapper，方便后续退出时清理
    wrappers = []

    # 遍历 JSON 数组中的每一个组别配置
    for item in groups_conf:
        cfg = GroupConfig.from_json(item)
        if not cfg.enable:
            app_logger.info(f"Group [{cfg.identifier}] is disabled. Skipping.")
            continue
            
        app_logger.info(f"Starting wrapper for group [{cfg.identifier}]...")
        wrapper = GCOWrapper(gco_class=SentinelGCO, gco_cfg=cfg, redis_conf=redis_conf)
        wrapper.load()
        wrapper.start()
        wrappers.append(wrapper)
    
    app_logger.info(f"Successfully started {len(wrappers)} Sentinel groups. Press Ctrl+C to stop.")
    
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        app_logger.info("Shutting down Sentinels...")
        for wrapper in wrappers:
            wrapper.stop()

if __name__ == "__main__":
    main()