"""Admin configuration for malware detection."""

from django.contrib import admin

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


@admin.register(MalwareDetection)
class MalwareDetectionAdmin(admin.ModelAdmin):
    """Admin for MalwareDetection model with a simple dashboard and filters."""

    list_display = (
        "id",
        "path",
        "status",
        "backend",
        "error_code",
        "created_at",
        "updated_at",
    )
    list_filter = ("status",)
    search_fields = ("path", "id")
    readonly_fields = ("id", "created_at", "updated_at")
    ordering = ("-created_at",)
    actions = ("reschedule_processing_task",)

    def reschedule_processing_task(self, request, queryset):
        """Reschedule the processing task for a malware detection record."""
        backend = MalwareDetectionHandler()()
        for malware_detection in queryset:
            backend.reschedule_processing_task(malware_detection)

        self.message_user(
            request,
            "Malware detection tasks successfully rescheduled",
        )
