from __future__ import annotations

import logging
from typing import Optional

from pedros.has_dep import has_dep

__all__ = ["setup_logging", "get_logger"]


def setup_logging(level: int = logging.INFO) -> None:
    """
    Configure the application's logging behavior.

    This function attempts to use Rich's ``RichHandler`` for enhanced,
    colorful, and trace-friendly logging. If Rich is not installed,
    it silently falls back to Python's standard logging configuration.
    See more about Rich (https://pypi.org/project/rich/).

    :param level: Logging level to use. Defaults to ``logging.INFO``.
    :type level: int
    :return: None
    """
    fmt = None
    datefmt = None
    handlers = []

    if has_dep("rich"):
        from rich.logging import RichHandler

        handler = RichHandler(rich_tracebacks=True)
        handlers.append(handler)
    else:
        fmt = "%(asctime)s | %(levelname)-8s | %(message)s"
        datefmt = "%Y-%m-%d %H:%M:%S"
        handler = logging.StreamHandler()
        handlers.append(handler)

    logging.basicConfig(
        level=level,
        format=fmt,
        datefmt=datefmt,
        handlers=handlers,
        force=True,
    )


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

    If no name is provided, the module's ``__name__`` is used.

    :param name: Name of the logger. If ``None``, defaults to the current module.
    :type name: str or None
    :return: A configured logger instance.
    """
    return logging.getLogger(name or __name__)
