"""Django AppConfig for the Discord Audit module."""

from django.apps import AppConfig
from django.utils.text import format_lazy
from django.utils.translation import gettext_lazy as _

from . import __version__
from .permissions import APP_LABEL


class DiscordAuditConfig(AppConfig):
    """
    Wire the Discord Audit module into Django.

    The app label matches the package name (``aa_discord_audit``) by
    convention; permission codenames consequently take the form
    ``aa_discord_audit.<codename>``. ``APP_LABEL`` from
    ``permissions.py`` is the single source of truth — both ``name``
    and ``label`` reference it so a rename ripples through one
    constant, not three string literals.
    """

    name = APP_LABEL
    label = APP_LABEL
    default_auto_field = "django.db.models.BigAutoField"

    verbose_name = format_lazy(
        _("Discord Audit v{version}"),
        version=__version__,
    )

    def ready(self) -> None:
        """
        Run startup validators and connect signal receivers.

        Importing ``signals`` is what wires the post_save / post_delete
        handlers (the receivers register at module import via
        ``post_save.connect(...)``). Importing ``app_settings`` runs
        the validator suite via the explicit call below; the validators
        raise ``ImproperlyConfigured`` on misconfiguration so Django
        surfaces the failure at app load.
        """
        # Side-effect import: registers receivers.
        from . import signals  # noqa: F401

        # Run validators after signal registration so a misconfig
        # message reflects the post-import state.
        from .app_settings import run_startup_validators

        run_startup_validators()
