# path: spyhunter.py
import os
import importlib
import sys
import traceback

STATIC_MENU = {
    "1": "Quick Start",
    "2": "ADB/Device Settings",
    "3": "Detection Setup",
    "4": "Scan"
}

MODULE_MAPPING = {
    "1": "modules.quick_start",
    "2": "modules.adb_settings",
    "3": "modules.detection_setup",
    "4": "modules.scan"
}

BANNER = r"""
  _________              ___ ___               __                
 /   _____/_____ ___.__./   |   \ __ __  _____/  |_  ___________ 
 \_____  \\____ <   |  /    ~    \  |  \/    \   __\/ __ \_  __ \
 /        \  |_> >___  \    Y    /  |  /   |  \  | \  ___/|  | \/
/_______  /   __// ____|\___|_  /|____/|___|  /__|  \___  >__|   
          |__|                                                                               
  Version 0.0.1 by SsSnake — Grand Butcher of The DCR
"""

def print_banner():
    os.system('cls' if os.name == 'nt' else 'clear')
    print(BANNER)

def load_custom_modules():
    menu = {}
    custom_path = os.path.join(os.path.dirname(__file__), "modules")
    if not os.path.exists(custom_path):
        os.makedirs(custom_path)
    for fname in os.listdir(custom_path):
        if fname.endswith(".py") and fname not in ["__init__.py"]:
            try:
                modname = f"modules.{fname[:-3]}"
                mod = importlib.import_module(modname)
                if hasattr(mod, "MENU_NAME") and hasattr(mod, "main"):
                    key = str(len(STATIC_MENU) + len(menu) + 1)
                    menu[key] = (mod.MENU_NAME, modname)
            except Exception:
                print(f"[WARN] Failed to load module {fname}: {traceback.format_exc(limit=1)}")
    return menu

def show_menu(static_menu, custom_menu):
    for key, name in static_menu.items():
        print(f"  {key}) {name}")
    for key, (name, _) in custom_menu.items():
        print(f"  {key}) {name}")
    print("\n[INFO] Press 'q' at any time to quit.\n")

def main():
    while True:
        try:
            print_banner()
            custom_menu = load_custom_modules()
            show_menu(STATIC_MENU, custom_menu)
            choice = input("Enter your selection: ").strip().lower()

            if choice == 'q':
                print("[INFO] Exiting SpyHunter...")
                sys.exit(0)

            if choice in STATIC_MENU:
                module_path = MODULE_MAPPING[choice]
            elif choice in custom_menu:
                module_path = custom_menu[choice][1]
            else:
                print("[ERROR] Invalid choice. Try again.")
                input("Press Enter to continue...")
                continue

            # Load and run the module
            module = importlib.import_module(module_path)
            module.main()

        except KeyboardInterrupt:
            print("\n[INFO] User aborted with Ctrl+C.")
            sys.exit(0)
        except Exception as e:
            print(f"[FATAL] An error occurred: {e}")
            traceback.print_exc()
            input("Press Enter to return to main menu...")

if __name__ == "__main__":
    main()
