"""Core infrastructure Django app configuration."""

import importlib
import logging

from django.apps import AppConfig

logger = logging.getLogger(__name__)


class CoreConfig(AppConfig):
    """Django app configuration for the core infrastructure module.

    This app must be loaded before any feature apps (testgen, optimizer, code_repair)
    to ensure the handler registry is initialized and protocols are available.
    """

    default_auto_field = "django.db.models.BigAutoField"
    name = "core"
    verbose_name = "Core Infrastructure"

    def ready(self) -> None:
        """Initialize core infrastructure when Django starts."""
        logger.info("Core app initializing...")

        # Import language handlers from core.languages
        # This ensures the handlers get registered via their __init__.py
        for module_name, label in [
            ("core.languages.python", "Python"),
            ("core.languages.js_ts", "JavaScript/TypeScript"),
            ("core.languages.java", "Java"),
        ]:
            try:
                importlib.import_module(module_name)
                logger.info("%s language handler imported and registered", label)
            except ImportError:
                logger.warning("Could not import %s language handler", label, exc_info=True)

        logger.info("Core app initialization complete")
