
import configparser


def uvicorn_config(dlogger, logger_names=None):
    """generate uvicorn log config using dlogger.
    
    args:
        dlogger: dlogger instance (usually root logger)
        logger_names: list of logger names to route through dlogger
    
    returns:
        dict for uvicorn log_config
    """
    if logger_names is None:
        logger_names = ["uvicorn", "uvicorn.access"]

    loggers = {}
    for name in logger_names:
        loggers[name] = {"handlers": ["dlogger"], "level": "DEBUG"}

    return {
        "version": 1,
        "disable_existing_loggers": False,
        "handlers": {
            "dlogger": {
                "()": "dlogger.handlers.compat.CompatHandler",
                "dlogger": dlogger,
            }
        },
        "loggers": loggers,
    }


def load(config_path: str = "dlogger.conf"):
    """load dlogger config from file and generate uvicorn config.
    
    Search order:
        1. Absolute path
        2. Next to the main script (sys.argv[0])
        3. Current working directory
    
    args:
        config_path: path to dlogger.conf
    
    returns:
        dict for uvicorn log_config
    
    raises:
        FileNotFoundError: if config file does not exist
    """
    import os
    import sys
    
    search_paths = []
    
    # 1. Absolute path
    if os.path.isabs(config_path):
        search_paths.append(config_path)
    
    # 2. Next to the main script
    if sys.argv and sys.argv[0]:
        script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
        search_paths.append(os.path.join(script_dir, config_path))
    
    # 3. Current working directory
    search_paths.append(os.path.abspath(config_path))
    
    # Find first existing file
    found_path = None
    for path in search_paths:
        if os.path.exists(path):
            found_path = path
            break
    
    if not found_path:
        raise FileNotFoundError(f"Config file not found: {config_path}")
    
    config = configparser.ConfigParser()
    config.read(found_path)

    logger_names = []
    if config.has_section("loggers"):
        logger_names = [k.strip() for k in config.get("loggers", "keys").split(",")]

    from dlogger import get_logger

    for name in logger_names:
        section = f"logger_{name}"
        if not config.has_section(section):
            continue

        lgr = get_logger(name)

        level = config.get(section, "level", fallback="DEBUG")
        log_file = config.get(section, "log_file", fallback=None)
        rotation = config.get(section, "rotation", fallback=None)
        retention = config.get(section, "retention", fallback=None)
        compression = config.getboolean(section, "compression", fallback=False)
        time_format = config.get(section, "time_format", fallback="%Y-%m-%d %H:%M:%S")

        lgr.configure(
            level=level,
            log_file=log_file,
            rotation=rotation,
            retention=retention,
            compression=compression,
            time_format=time_format,
        )

    root_logger = get_logger("root")
    return uvicorn_config(root_logger, logger_names=logger_names)
