"""Core App Configuration

This module contains the Django app configuration for the core application.
The core app provides shared functionality and utilities used across the entire project.
"""

from django.apps import AppConfig


class CoreConfig(AppConfig):
    """Configuration for the Core application.
    
    This app contains shared functionality, base models, utilities,
    and common components used throughout the Adtlas project.
    """
    
    # App configuration
    name = 'apps.core'
    verbose_name = 'Core'
    default_auto_field = 'django.db.models.BigAutoField'
    
    def ready(self):
        """Initialize the app when Django starts.
        
        This method is called when the app is ready and can be used to:
        - Import signal handlers
        - Register custom checks
        - Initialize app-specific configurations
        - Set up logging
        """
        try:
            # Import signal handlers
            from . import signals  # noqa: F401
            
            # Import and register custom checks
            from . import checks  # noqa: F401
            
            # Initialize logging configuration
            self._setup_logging()
            
            # Register custom template tags
            self._register_template_tags()
            
        except ImportError:
            # Handle cases where signal modules don't exist yet
            pass
    
    def _setup_logging(self):
        """Setup application-specific logging configuration."""
        import logging
        
        # Get or create logger for the core app
        logger = logging.getLogger('apps.core')
        
        # Set default level if not configured
        if not logger.handlers:
            logger.setLevel(logging.INFO)
    
    def _register_template_tags(self):
        """Register custom template tags and filters."""
        try:
            from django.template import engines
            from django.template.backends.django import DjangoTemplates
            
            # Ensure template tags are loaded
            for engine in engines.all():
                if isinstance(engine, DjangoTemplates):
                    try:
                        engine.engine.get_library('core_tags')
                    except Exception:
                        # Template tags not available yet
                        pass
        except Exception:
            # Handle any template tag registration errors
            pass