from django.contrib import admin
from unfold.admin import ModelAdmin

from jobs.models import Job, JobEvent


@admin.register(Job)
class JobAdmin(ModelAdmin):
    list_display = ("id", "type", "status", "priority", "attempts", "max_attempts", "locked_by", "available_at")
    list_filter = ("status", "type")
    search_fields = ("id", "type", "idempotency_key", "locked_by")
    readonly_fields = (
        "id",
        "attempts",
        "locked_by",
        "locked_until",
        "last_error",
        "result",
        "created_at",
        "updated_at",
        "finished_at",
    )
    ordering = ("-created_at",)
    list_per_page = 50


@admin.register(JobEvent)
class JobEventAdmin(ModelAdmin):
    list_display = ("id", "job", "type", "attempt", "worker_id", "created_at")
    list_filter = ("type",)
    search_fields = ("job__id", "worker_id", "message")
    readonly_fields = ("job", "type", "attempt", "worker_id", "message", "data", "created_at")
    ordering = ("-id",)
    list_per_page = 100
