#ht: 用户数据模型模块，定义自定义用户模型
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.urls import reverse
from django.utils.timezone import now
from django.utils.translation import gettext_lazy as _
from djangoblog.utils import get_current_site


class BlogUser(AbstractUser):
    """ht: 博客用户模型，扩展Django默认用户模型"""
    nickname = models.CharField(_('nick name'), max_length=100, blank=True)
    creation_time = models.DateTimeField(_('creation time'), default=now)
    last_modify_time = models.DateTimeField(_('last modify time'), default=now)
    source = models.CharField(_('create source'), max_length=100, blank=True)

    def get_absolute_url(self):
        """ht: 获取用户详情页URL"""
        return reverse(
            'blog:author_detail', kwargs={
                'author_name': self.username})

    def __str__(self):
        """ht: 字符串表示，返回邮箱"""
        return self.email

    def get_full_url(self):
        """ht: 获取完整的用户URL（包含域名）"""
        site = get_current_site().domain
        url = "https://{site}{path}".format(site=site,
                                            path=self.get_absolute_url())
        return url

    class Meta:
        ordering = ['-id']  # ht: 默认按ID倒序排列
        verbose_name = _('user')
        verbose_name_plural = verbose_name
        get_latest_by = 'id'