from django.contrib import admin
from .models import Group, Membership, Post, Comment, PostLike # <<<< 导入 PostLike

class MembershipInline(admin.TabularInline):
    model = Membership
    extra = 1
    autocomplete_fields = ['user']

class GroupAdmin(admin.ModelAdmin):
    list_display = ('name', 'creator', 'created_at', 'get_tags_display')
    list_filter = ('created_at', 'tags')
    search_fields = ('name', 'description', 'creator__email', 'creator__nickname')
    inlines = [MembershipInline]
    filter_horizontal = ('tags',)

    def get_tags_display(self, obj):
        return ", ".join([tag.name for tag in obj.tags.all()])
    get_tags_display.short_description = '相关标签'

class MembershipAdmin(admin.ModelAdmin):
    list_display = ('user_display', 'group', 'date_joined')
    list_filter = ('group', 'date_joined')
    search_fields = ('user__email', 'user__nickname', 'group__name')
    autocomplete_fields = ['user', 'group']

    def user_display(self, obj):
        if hasattr(obj.user, 'display_name') and obj.user.display_name:
            return obj.user.display_name
        return obj.user.email
    user_display.short_description = '用户'
    user_display.admin_order_field = 'user__email'

class PostAdmin(admin.ModelAdmin):
    list_display = ('title_or_author', 'group', 'author_display', 'created_at', 'short_content')
    list_filter = ('group', 'created_at', 'author')
    search_fields = ('title', 'content', 'author__email', 'author__nickname', 'group__name')
    autocomplete_fields = ['group', 'author']

    def title_or_author(self, obj):
        return obj.title if obj.title else f"来自 {self.author_display(obj)} 的动态"
    title_or_author.short_description = '标题/作者动态'

    def author_display(self, obj):
        if hasattr(obj.author, 'display_name'):
            return obj.author.display_name
        return obj.author.email
    author_display.short_description = '作者'
    author_display.admin_order_field = 'author__email'

    def short_content(self, obj):
        return obj.content[:50] + '...' if len(obj.content) > 50 else obj.content
    short_content.short_description = '内容预览'

class CommentAdmin(admin.ModelAdmin):
    list_display = ('author_display_comment', 'post_identifier_comment', 'short_content_comment', 'created_at', 'updated_at')
    list_filter = ('created_at', 'post', 'author')
    search_fields = ('content', 'author__email', 'author__nickname', 'post__title')
    autocomplete_fields = ['post', 'author']

    def author_display_comment(self, obj):
        if hasattr(obj.author, 'display_name') and obj.author.display_name:
            return obj.author.display_name
        return obj.author.email
    author_display_comment.short_description = '评论作者'
    author_display_comment.admin_order_field = 'author__email'

    def post_identifier_comment(self, obj):
        return obj.post.title if obj.post.title else f"帖子ID: {obj.post.id}"
    post_identifier_comment.short_description = '所属帖子'
    post_identifier_comment.admin_order_field = 'post__title'
        
    def short_content_comment(self, obj):
        return obj.content[:75] + '...' if len(obj.content) > 75 else obj.content
    short_content_comment.short_description = '评论内容预览'

class PostLikeAdmin(admin.ModelAdmin): # <<<< 新增 PostLikeAdmin
    list_display = ('user_display', 'post_identifier', 'created_at')
    list_filter = ('created_at', 'post__group', 'post', 'user') # 可以按小组、帖子、用户筛选
    search_fields = ('user__email', 'user__nickname', 'post__title')
    autocomplete_fields = ['user', 'post'] # 方便选择用户和帖子

    def user_display(self, obj):
        if hasattr(obj.user, 'display_name') and obj.user.display_name:
            return obj.user.display_name
        return obj.user.email
    user_display.short_description = '点赞用户'
    user_display.admin_order_field = 'user__email'

    def post_identifier(self, obj):
        return obj.post.title if obj.post.title else f"帖子ID: {obj.post.id}"
    post_identifier.short_description = '被点赞的帖子'
    post_identifier.admin_order_field = 'post__title'


# 注册所有模型到Admin站点
admin.site.register(Group, GroupAdmin)
admin.site.register(Membership, MembershipAdmin)
admin.site.register(Post, PostAdmin)
admin.site.register(Comment, CommentAdmin)
admin.site.register(PostLike, PostLikeAdmin) # <<<< 新增注册 PostLike