import pathlib
import importlib
from loguru import logger

from .core import (
    add_acast,
    acaster,
    acast,
    add_acast_if_supported,
    __g_mappings__,
)

__all__ = ["add_acast", "acaster", "acast", "add_acast_if_supported"]

def register_all_backends():
    """Import all backend cast modules (casts_*.py) to register their casts."""
    package_dir = pathlib.Path(__file__).parent

    for file in package_dir.iterdir():
        # Only consider Python files starting with "casts_" and skip __init__.py
        if file.suffix == ".py" and file.name.startswith("casts_"):
            mod_name = f"{__package__}.{file.stem}"
            try:
                importlib.import_module(mod_name)
                logger.debug(f"Successfully imported backend: {mod_name}")
            except ImportError:
                # Expected if used doesn't have the lib installed
                logger.warning(f"Failed to import module: {mod_name}")
                pass
            
register_all_backends()