from django.shortcuts import redirect
from django.urls import reverse
from core.models import SystemSettings

class ActivationMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        path = request.path_info
        
        # Bypass for static & media files
        if path.startswith('/static/') or path.startswith('/media/'):
            return self.get_response(request)

        # Bypass for the activation page itself to prevent infinite redirects
        if path == reverse('activate_system'):
            return self.get_response(request)

        # Check activation status
        system = SystemSettings.objects.first()
        if system and not system.is_activated:
            return redirect('activate_system')

        return self.get_response(request)
