"""Management command to reschedule the processing analysis."""

from datetime import timedelta

from django.conf import settings
from django.core.management.base import BaseCommand
from django.utils.timezone import now

from lasuite.malware_detection.handler import MalwareDetectionHandler
from lasuite.malware_detection.models import MalwareDetection, MalwareDetectionStatus


class Command(BaseCommand):
    """Management command to reschedule the processing analysis."""

    def handle(self, *args, **options):
        """Handle the command."""
        reschedule_days = getattr(settings, "MALWARE_DETECTION_RESCHEDULE_PROCESSING_ANALYSIS_DAYS", 3)

        reschedule_date = now() - timedelta(days=reschedule_days)

        malware_detections = MalwareDetection.objects.filter(
            status=MalwareDetectionStatus.PROCESSING, created_at__lte=reschedule_date
        )

        backend = MalwareDetectionHandler()()

        for malware_detection in malware_detections:
            backend.reschedule_processing_task(malware_detection)
