package me.qwq.doghouse.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;

import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import jakarta.validation.constraints.Pattern.Flag;

import com.baomidou.mybatisplus.annotation.TableField;
import java.io.Serializable;

import org.springframework.stereotype.Component;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import me.qwq.doghouse.component.Rk;
import me.qwq.doghouse.util.GravatarUtils;

/**
 * 后台管理员信息
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@Component
public class User implements Serializable {

    static final long serialVersionUID=1L;

    static final String USERNAME_CANNOT_BE_EMPTY = "Username cannot be empty/用户昵称不允许为空";
    static final String USERNAME_LENGTH_OVERFLOW = "Username length overflow/用户昵称过长";
    static final String EMAIL_CANNOT_BE_EMPTY = "Email cannot be empty/邮箱地址不允许为空";
    static final String EMAIL_INVALID = "Email is invaild/邮箱地址非法";
    static final String EMAIL_LENGTH_OVERFLOW = "Email length overflow/邮箱地址过长";
    static final String INVALID_REQUEST = "Invalid request/非法请求";
    static final String INTERNAL_ERROR = "Internal error/内部错误";
    static final String URL_INVALID = "Url is invaild/网站地址非法";
    static final String URL_LENGTH_OVERFLOW = "Url length overflow/网站地址过长";

    /**
     * 管理员id
     */
    @TableId(value = "user_id", type = IdType.AUTO)
    private Integer userId;

    /**
     * 管理员登陆名称
     */
    @TableField("username")
    private String username;

    /**
     * 管理员登陆密码
     */
    @TableField("password")
    private String password;

    /**
     * 管理员显示昵称
     */
    @TableField("nickname")
    @NotBlank(message = USERNAME_CANNOT_BE_EMPTY)
    @Size(max = 25, message = USERNAME_LENGTH_OVERFLOW)
    private String nickname;
    
    /**
     * 管理员显示昵称
     */
    @TableField("email")
    @NotBlank(message = EMAIL_CANNOT_BE_EMPTY)
    @Email(
            flags   = Flag.CASE_INSENSITIVE,
            message = EMAIL_INVALID)
    private String email;
    
    @TableField("site")
    private String site;

    /**
     * 头像
     * @return
     */
    public String getAvatarUrl() {
        return GravatarUtils.getProxyAvatarUrl(
                Rk.Gravatar.prepareDogHash(getEmail()), 30);
    }

    /**
     * 头像
     * @return
     */
    public String getAvatarUrl(Integer size) {
        return GravatarUtils.getProxyAvatarUrl(
                Rk.Gravatar.prepareDogHash(getEmail()), size);
    }

}
