"""AgentBill Configurable Logging — v10.1.0

Provides a standardized logging interface for the Python SDK.
Uses Python's built-in ``logging`` module. SDKs are silent by default
(WARNING level). Users can enable debug output via ``set_log_level()``.

Usage:
    from agentbill import get_logger, set_log_level

    # Enable debug logging
    set_log_level("DEBUG")

    # Or in code
    log = get_logger("my_module")
    log.info("Something happened")
"""

import logging
from typing import Optional, Union

# Root namespace for all AgentBill loggers
_NAMESPACE = "agentbill"

# Create root logger — silent by default (WARNING)
_root_logger = logging.getLogger(_NAMESPACE)
if not _root_logger.handlers:
    _handler = logging.StreamHandler()
    _handler.setFormatter(
        logging.Formatter("[%(name)s] %(levelname)s: %(message)s")
    )
    _root_logger.addHandler(_handler)
    _root_logger.setLevel(logging.WARNING)


def get_logger(name: Optional[str] = None) -> logging.Logger:
    """Return a namespaced logger.

    Args:
        name: Sub-logger name (e.g. ``"http"``, ``"tracing"``).
              If ``None``, returns the root ``agentbill`` logger.

    Returns:
        A ``logging.Logger`` instance under the ``agentbill`` namespace.
    """
    if name:
        return logging.getLogger(f"{_NAMESPACE}.{name}")
    return _root_logger


def set_log_level(level: Union[str, int]) -> None:
    """Set the log level for the entire AgentBill SDK.

    Args:
        level: Log level — ``"DEBUG"``, ``"INFO"``, ``"WARNING"``, ``"ERROR"``,
               or an ``int`` from the ``logging`` module.

    Example:
        >>> from agentbill import set_log_level
        >>> set_log_level("DEBUG")  # See all SDK internals
    """
    if isinstance(level, str):
        level = getattr(logging, level.upper(), logging.WARNING)
    _root_logger.setLevel(level)


__all__ = ["get_logger", "set_log_level"]
