from django.apps import AppConfig
import logging

logger = logging.getLogger(__name__)


class BackupConfig(AppConfig):
    default_auto_field = "django.db.models.BigAutoField"
    name = "horilla_backup"

    def ready(self):
        from django.urls import include, path
        from horilla.urls import urlpatterns

        urlpatterns.append(
            path("backup/", include("horilla_backup.urls")),
        )
        
        # Initialize scheduler for active backup jobs
        try:
            from .models import GoogleDriveBackup
            from .scheduler import start_gdrive_backup_job
            
            # Check if there's an active Google Drive backup configuration
            if GoogleDriveBackup.objects.filter(active=True).exists():
                logger.info("Found active Google Drive backup configuration, starting scheduler")
                start_gdrive_backup_job()
            else:
                logger.info("No active Google Drive backup configuration found")
        except Exception as e:
            logger.warning(f"Could not initialize Google Drive backup scheduler: {e}")
            
        super().ready()
