"""
Logging centralizado para el Agent CLI con Rich handler.
"""
import logging

from rich.logging import RichHandler


def setup_logger(name: str = "jama-agent", level: str = "INFO") -> logging.Logger:
    """Configura y retorna un logger con Rich handler para output bonito."""
    logger = logging.getLogger(name)

    # Evitar duplicar handlers si se llama múltiples veces
    if logger.handlers:
        return logger

    logger.setLevel(getattr(logging, level.upper(), logging.INFO))

    handler = RichHandler(
        rich_tracebacks=True,
        show_time=True,
        show_path=False,
        markup=True,
    )
    handler.setLevel(logger.level)

    fmt = logging.Formatter("%(message)s", datefmt="[%X]")
    handler.setFormatter(fmt)

    logger.addHandler(handler)

    return logger


# Logger global del agent
log = setup_logger()
