from django.contrib import admin

from .models import Conversation, Message, ResakaProfile


@admin.register(ResakaProfile)
class ResakaProfileAdmin(admin.ModelAdmin):
    list_display = ("display_name", "handle", "status_text", "created_at")
    search_fields = ("display_name", "handle", "status_text")


class MessageInline(admin.TabularInline):
    model = Message
    extra = 0
    readonly_fields = ("created_at",)


@admin.register(Conversation)
class ConversationAdmin(admin.ModelAdmin):
    list_display = ("subject", "starter", "recipient", "updated_at")
    search_fields = ("subject", "starter__display_name", "recipient__display_name")
    inlines = [MessageInline]


@admin.register(Message)
class MessageAdmin(admin.ModelAdmin):
    list_display = ("conversation", "author", "short_body", "reaction", "is_read", "created_at")
    list_filter = ("is_read", "reaction", "created_at")
    search_fields = ("body", "author__display_name")

    @staticmethod
    def short_body(obj):
        return obj.body[:60]
