from .module1 import MyClass_Mod1
from .module2 import MyClass_Mod2
import os
import sys
import importlib
import pkgutil
import subprocess

def main():
    """Command-line interface handler for multiple commands."""
    if len(sys.argv) < 2:
        print("Usage:")
        print("  skeltymlops <method_name>  # Call a method in any class")
        print("  skeltymlops new <folder_name>  # Create a folder and clone a repo")
        sys.exit(1)

    command = sys.argv[1]

    if command == "new":
        if len(sys.argv) < 3:
            print("Error: Please specify a folder name.")
            sys.exit(1)

        folder_name = sys.argv[2]
        github_repo = "https://github.com/jeanpierrenashef/E-learning-website"

        try:
            os.makedirs(folder_name, exist_ok=True)
            print(f"✅ Folder '{folder_name}' created successfully.")

            # Clone the repo inside the folder
            subprocess.run(["git", "clone", github_repo, folder_name], check=True)
            print(f"✅ Repository cloned into '{folder_name}'.")
        except Exception as e:
            print(f"❌ Error: {e}")
            sys.exit(1)

    else:
        method_name = command
        package_name = __name__  

        for _, module_name, _ in pkgutil.iter_modules(__path__):
            module = importlib.import_module(f"{package_name}.{module_name}")

            for attr_name in dir(module):
                attr = getattr(module, attr_name)
                if isinstance(attr, type):  
                    obj = attr() 
                    if hasattr(obj, method_name):
                        getattr(obj, method_name)() 
                        return 

        print(f"Error: Method '{method_name}' not found in any module.")
        sys.exit(1)
