import os
import sys
import yaml
import importlib

class ProjectUtil:
    @staticmethod
    def scan_project(root_path):
        sys.path.append(root_path)
        if not os.path.exists(root_path) or not os.path.isdir(root_path):
            raise ValueError(f"The provided root_path '{root_path}' is not a valid directory.")
    
        parent_dir = os.path.dirname(root_path)
        sys.path.insert(0, parent_dir)
        
        def import_all_modules(path, package_name):
            for root, dirs, files in os.walk(path):
                relative_path = os.path.relpath(root, root_path)
                if relative_path == '.':
                    module_package = package_name
                else:
                    module_package = f"{package_name}.{relative_path.replace(os.sep, '.')}"
                for file in files:
                    if file.endswith(".py") and file != "__init__.py":
                        module_name = file[:-3]
                        full_module_name = f"{module_package}.{module_name}"
                        if full_module_name not in sys.modules:
                            importlib.import_module(full_module_name)
                dirs[:] = [d for d in dirs if not d.startswith('.')]
        
        package_name = os.path.basename(root_path)
        import_all_modules(root_path, package_name)
        
    @staticmethod
    def scan_configs(root_path):
        configs = {}
        for root, dirs, files in os.walk(root_path):
            for file in files:
                if file.endswith(('.yaml', '.yml')):
                    if file.startswith('__'):
                        continue
                    file_path = os.path.join(root, file)
                    with open(file_path, 'r', encoding='utf-8') as f:
                        try:
                            content = yaml.safe_load(f)
                            configs[os.path.splitext(file)[0]] = content
                        except yaml.YAMLError as e:
                            print(f"Error reading {file_path}: {e}")
        
        return configs