from django.contrib import admin

from .models import (
    Announcement,
    AnnouncementComment,
    AnnouncementRead,
    Notification,
)


@admin.register(Announcement)
class AnnouncementAdmin(admin.ModelAdmin):
    list_display = ('title', 'created_by', 'published', 'comment_count', 'created_at', 'updated_at')
    list_filter = ('published', 'created_at')
    search_fields = ('title', 'body')

    @admin.display(description='Comments')
    def comment_count(self, obj):
        return obj.comments.count()


@admin.register(AnnouncementRead)
class AnnouncementReadAdmin(admin.ModelAdmin):
    list_display = ('announcement', 'user', 'read_at')
    search_fields = ('announcement__title', 'user__username')
    readonly_fields = ('announcement', 'user', 'read_at')

    def has_add_permission(self, request):
        return False

    def has_change_permission(self, request, obj=None):
        return False


@admin.register(AnnouncementComment)
class AnnouncementCommentAdmin(admin.ModelAdmin):
    list_display = ('announcement', 'user', 'body_preview', 'created_at')
    search_fields = ('announcement__title', 'user__username', 'body')

    @admin.display(description='Body')
    def body_preview(self, obj):
        return obj.body[:80]


@admin.register(Notification)
class NotificationAdmin(admin.ModelAdmin):
    list_display = ('user', 'kind', 'project', 'actor', 'read', 'created_at')
    list_filter = ('kind', 'read')
    search_fields = ('user__username', 'text', 'project__title')
