# 用户关注和收藏 Admin 管理界面

from django.contrib import admin
from django.utils.html import format_html
from django.utils.translation import gettext_lazy as _
from django.urls import reverse

from blog.models_social import UserFollow, ArticleFavorite, ArticleLike


@admin.register(UserFollow)
class UserFollowAdmin(admin.ModelAdmin):
    """用户关注管理界面"""

    list_display = [
        'id',
        'follower_link',
        'following_link',
        'creation_time'
    ]

    list_filter = [
        'creation_time'
    ]

    search_fields = [
        'follower__username',
        'following__username'
    ]

    readonly_fields = [
        'follower',
        'following',
        'creation_time'
    ]

    list_per_page = 50

    def has_add_permission(self, request):
        """禁止手动添加"""
        return False

    def follower_link(self, obj):
        """关注者链接"""
        url = reverse('admin:accounts_bloguser_change', args=[obj.follower.id])
        return format_html('<a href="{}">{}</a>', url, obj.follower.username)

    follower_link.short_description = '关注者'

    def following_link(self, obj):
        """被关注者链接"""
        url = reverse('admin:accounts_bloguser_change', args=[obj.following.id])
        return format_html('<a href="{}">{}</a>', url, obj.following.username)

    following_link.short_description = '被关注者'


@admin.register(ArticleFavorite)
class ArticleFavoriteAdmin(admin.ModelAdmin):
    """文章收藏管理界面"""

    list_display = [
        'id',
        'user_link',
        'article_link',
        'note_display',
        'creation_time'
    ]

    list_filter = [
        'creation_time'
    ]

    search_fields = [
        'user__username',
        'article__title',
        'note'
    ]

    readonly_fields = [
        'user',
        'article',
        'creation_time'
    ]

    list_per_page = 50

    def has_add_permission(self, request):
        """禁止手动添加"""
        return False

    def user_link(self, obj):
        """用户链接"""
        url = reverse('admin:accounts_bloguser_change', args=[obj.user.id])
        return format_html('<a href="{}">{}</a>', url, obj.user.username)

    user_link.short_description = '用户'

    def article_link(self, obj):
        """文章链接"""
        url = reverse('admin:blog_article_change', args=[obj.article.id])
        title = obj.article.title
        if len(title) > 50:
            title = title[:47] + '...'
        return format_html('<a href="{}">{}</a>', url, title)

    article_link.short_description = '文章'

    def note_display(self, obj):
        """显示备注"""
        if obj.note:
            if len(obj.note) > 30:
                return obj.note[:27] + '...'
            return obj.note
        return '-'

    note_display.short_description = '备注'


@admin.register(ArticleLike)
class ArticleLikeAdmin(admin.ModelAdmin):
    """文章点赞管理界面"""

    list_display = [
        'id',
        'user_link',
        'article_link',
        'creation_time'
    ]

    list_filter = [
        'creation_time'
    ]

    search_fields = [
        'user__username',
        'article__title'
    ]

    readonly_fields = [
        'user',
        'article',
        'creation_time'
    ]

    list_per_page = 50

    def has_add_permission(self, request):
        """禁止手动添加"""
        return False

    def user_link(self, obj):
        """用户链接"""
        url = reverse('admin:accounts_bloguser_change', args=[obj.user.id])
        return format_html('<a href="{}">{}</a>', url, obj.user.username)

    user_link.short_description = '用户'

    def article_link(self, obj):
        """文章链接"""
        url = reverse('admin:blog_article_change', args=[obj.article.id])
        title = obj.article.title
        if len(title) > 50:
            title = title[:47] + '...'
        return format_html('<a href="{}">{}</a>', url, title)

    article_link.short_description = '文章'

