from django.contrib import admin
from .models import *
from unfold.admin import TabularInline

from import_export.admin import ImportExportModelAdmin
from unfold.contrib.import_export.forms import ExportForm, ImportForm, SelectableFieldsExportForm
from unfold.contrib.forms.widgets import ArrayWidget, WysiwygWidget
from django.contrib.postgres.fields import ArrayField
from unfold.contrib.filters.admin import (
    ChoicesDropdownFilter,
    MultipleChoicesDropdownFilter,
)
from utils.admin import ModelAdmin



class MessageInline(TabularInline):
    model = Message
    extra = 1

@admin.register(ContactUsModel)
class ContactUsAdmin(ModelAdmin):
    list_filter = ['type', 'is_reviewed']
    list_display = ['full_name', 'phone', 'email', 'message', 'is_reviewed']
    compressed_fields = True
    warn_unsaved_form = True
    readonly_fields = ['full_name', 'email', 'phone', 'type', 'message', ]


@admin.register(Ticket)
class TicketAdmin(ModelAdmin, ImportExportModelAdmin):
    import_form_class = ImportForm
    export_form_class = ExportForm
    
    search_fields = ['subject', 'messages__content']
    list_filter = [('status', ChoicesDropdownFilter), ('ticket_category', ChoicesDropdownFilter)]
    list_filter_submit = True
    compressed_fields = True
    warn_unsaved_form = True

    formfield_overrides = {
        ArrayField: {
            "widget": ArrayWidget,
        }
    }
    readonly_fields = ('created_at', 'updated_at')
    list_display = ['subject', 'ticket_category', 'customer', 'admin', 'status', 'created_at']
    inlines = [MessageInline]
    radio_fields = {'status': admin.VERTICAL}

@admin.register(Message)
class MessageAdmin(ModelAdmin, ImportExportModelAdmin):
    import_form_class = ImportForm
    export_form_class = ExportForm
    list_display = ['ticket', 'sender', 'content_display','created_at']
    search_fields = ['content', ]
    compressed_fields = True
    warn_unsaved_form = True

    formfield_overrides = {
        models.TextField: {
            "widget": WysiwygWidget,
        },
        ArrayField: {
            "widget": ArrayWidget,
        }
    }
    def content_display(self, obj):
        return obj.content[0:35] + '...'
    content_display.short_description = 'محتوای پیام'

@admin.register(Attachment)
class AttachmentAdmin(ModelAdmin, ImportExportModelAdmin):
    list_display = ['name', 'uploaded_by']