import logging
from django.apps import AppConfig

logger = logging.getLogger(__name__)


class NginxmonConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'nginxmon'
    verbose_name = 'Nginx Monitor'

    def ready(self):
        try:
            from nginxmon.models import NginxSettings, NginxAlertProfile
            from nginxmon.tasks import start_fetch_job, schedule_profile, start_cleanup_job

            settings = NginxSettings.get()
            if settings.enabled:
                start_fetch_job(settings.fetch_interval_seconds)

            for profile in NginxAlertProfile.objects.filter(enabled=True):
                schedule_profile(profile)

            start_cleanup_job()
        except Exception as exc:
            logger.warning('nginxmon: could not schedule on startup: %s', exc)
