"""
Logging utilities for sync engine.

Provides structured logging with fallback to stdlib logging if structlog is unavailable.
"""

from typing import Any, Protocol


class Logger(Protocol):
    """Protocol defining the logger interface for sync engine."""

    def info(self, msg: str, **kwargs: Any) -> None:
        """Log info message with optional context."""
        ...

    def debug(self, msg: str, **kwargs: Any) -> None:
        """Log debug message with optional context."""
        ...

    def warning(self, msg: str, **kwargs: Any) -> None:
        """Log warning message with optional context."""
        ...

    def error(self, msg: str, **kwargs: Any) -> None:
        """Log error message with optional context."""
        ...


_logger: Logger  # Type annotation for module-level logger

try:
    import structlog

    STRUCTLOG_AVAILABLE = True
    _logger = structlog.get_logger()

except ImportError:
    # Fallback to stdlib logging if structlog not available
    import logging

    STRUCTLOG_AVAILABLE = False

    class LoggerWrapper:
        """Wrapper to make stdlib logger compatible with structlog keyword args."""

        def __init__(self, name: str = __name__) -> None:
            self._logger = logging.getLogger(name)

        def info(self, msg: str, **kwargs: Any) -> None:
            """Log info message with optional context."""
            self._logger.info(f"{msg} {kwargs}" if kwargs else msg)

        def debug(self, msg: str, **kwargs: Any) -> None:
            """Log debug message with optional context."""
            self._logger.debug(f"{msg} {kwargs}" if kwargs else msg)

        def warning(self, msg: str, **kwargs: Any) -> None:
            """Log warning message with optional context."""
            self._logger.warning(f"{msg} {kwargs}" if kwargs else msg)

        def error(self, msg: str, **kwargs: Any) -> None:
            """Log error message with optional context."""
            self._logger.error(f"{msg} {kwargs}" if kwargs else msg)

    _logger = LoggerWrapper()


def get_logger() -> Logger:
    """
    Get a logger instance (structlog or stdlib wrapper).

    Returns structlog logger if available, otherwise provides a stdlib
    logging wrapper with compatible API. The fallback maintains the same
    interface (info, debug, warning, error methods with **kwargs support)
    ensuring graceful degradation when structlog is not installed.

    Returns:
        Logger instance with info, debug, warning, error methods
    """
    return _logger
