from django.contrib import admin
from django.utils.safestring import mark_safe

from tickets.models import Attachment, Comment, TicketList, Ticket, TicketArchive


class TicketListAdmin(admin.ModelAdmin):
    list_filter = ("group",)


class TicketAdmin(admin.ModelAdmin):
    list_display = ("title", "ticket_list",  "status", "created_by", "assigned_to", "created_at", "updated_at", "due_date", "priority")
    list_filter =("ticket_list", "status", "created_by", "assigned_to", "created_at", "updated_at")
    ordering = ("priority",)
    search_fields = ("title", "note")

class TicketArchiveAdmin(admin.ModelAdmin):
    list_display = ("title", "ticket_list",  "status", "created_by", "assigned_to", "created_at", "updated_at", "due_date")
    list_filter = ("ticket_list", "status", "created_by", "assigned_to", "created_at", "updated_at")
    search_fields = ("title", "note")
    readonly_fields = ("title", "ticket_list", "ticket_type", "status", "created_at", "updated_at", "due_date", "created_by", "assigned_to", "display_note")

    def display_note(self, obj):
        return mark_safe(obj.note)


class CommentAdmin(admin.ModelAdmin):
    list_display = ("author", "date", "snippet")


class AttachmentAdmin(admin.ModelAdmin):
    list_display = ("ticket", "added_by", "timestamp", "file")
    autocomplete_fields = ["added_by", "ticket"]


admin.site.register(TicketList, TicketListAdmin)
admin.site.register(Ticket, TicketAdmin)
admin.site.register(TicketArchive, TicketArchiveAdmin)
admin.site.register(Comment, CommentAdmin)
admin.site.register(Attachment, AttachmentAdmin)