"""Main logging interface for the Trax platform.

This module provides a simple interface for setting up and using the logging system.
"""

import logging
from typing import Optional

from .config import LoggingConfig, setup_logging, get_logger, set_log_level, enable_debug_mode, disable_debug_mode
from .metrics import (
    get_metrics_collector,
    get_health_monitor,
    timing_context,
    async_timing_context,
    timing_decorator,
    async_timing_decorator,
    log_operation_timing,
    increment_operation_counter,
    get_operation_metrics,
    export_all_metrics,
    start_health_monitoring,
    stop_health_monitoring
)

# Global configuration
_logging_config: Optional[LoggingConfig] = None
_logging_initialized = False


def initialize_logging(config: Optional[LoggingConfig] = None) -> None:
    """Initialize the logging system.
    
    Args:
        config: Optional logging configuration. If None, uses default configuration.
    """
    global _logging_config, _logging_initialized
    
    if _logging_initialized:
        return
    
    _logging_config = config or LoggingConfig()
    setup_logging(_logging_config)
    _logging_initialized = True


def get_logging_config() -> Optional[LoggingConfig]:
    """Get the current logging configuration."""
    return _logging_config


def is_initialized() -> bool:
    """Check if logging has been initialized."""
    return _logging_initialized


# Convenience functions
def get_logger(name: str) -> logging.Logger:
    """Get a logger with the given name.
    
    Args:
        name: The logger name (usually __name__)
        
    Returns:
        A configured logger instance
    """
    if not _logging_initialized:
        initialize_logging()
    
    return logging.getLogger(name)


def set_level(level: str) -> None:
    """Set the logging level.
    
    Args:
        level: The logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
    """
    if not _logging_initialized:
        initialize_logging()
    
    set_log_level(level)


def enable_debug() -> None:
    """Enable debug mode."""
    if not _logging_initialized:
        initialize_logging()
    
    enable_debug_mode()


def disable_debug() -> None:
    """Disable debug mode."""
    if not _logging_initialized:
        initialize_logging()
    
    disable_debug_mode()


# Export main classes and functions
__all__ = [
    'LoggingConfig',
    'initialize_logging',
    'get_logging_config',
    'is_initialized',
    'get_logger',
    'set_level',
    'enable_debug',
    'disable_debug',
    
    # Metrics and monitoring
    'get_metrics_collector',
    'get_health_monitor',
    'timing_context',
    'async_timing_context',
    'timing_decorator',
    'async_timing_decorator',
    'log_operation_timing',
    'increment_operation_counter',
    'get_operation_metrics',
    'export_all_metrics',
    'start_health_monitoring',
    'stop_health_monitoring',
]
