package me.qwq.doghouse.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import java.util.List;
import java.util.Locale;

import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.baomidou.mybatisplus.annotation.TableField;
import java.io.Serializable;
import java.time.LocalDateTime;

import com.fasterxml.jackson.annotation.JsonFormat;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import me.qwq.doghouse.constants.UsefulRegex;
import me.qwq.doghouse.enums.CommentStatusEnum;
import me.qwq.doghouse.enums.PostTypeEnum;
import me.qwq.doghouse.interfaces.IAvatarUrl;
import me.qwq.doghouse.interfaces.IPostPassMess;
import me.qwq.doghouse.pojo.bo.IpInfo;
import me.qwq.doghouse.validator.CommentValid;
import me.qwq.doghouse.validator.CommentValidator;

import org.hibernate.validator.constraints.Length;

import jakarta.validation.constraints.*;
import jakarta.validation.constraints.Pattern.Flag;

/**
 * 
 * Comment entity
 *
 * @author Doghole
 * @since 2021-11-19
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@CommentValid
public class Comment extends Model<Comment> implements Serializable, IPostPassMess<Comment>, IAvatarUrl {

private static final long serialVersionUID=1L;

    /**
     * 主键id
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * 关联的blog主键
     */
    @TableField("post_id")
    private Long postId;

    /**
     * 关联 Post 标题
     */
    @TableField(exist=false)
    private String postTitle;
    /**
     * 关联 Post 类型
     */
    @TableField(exist=false)
    private PostTypeEnum type;
    /**
     * 关联 Post 密码
     */
    @TableField(exist=false)
    private String password;
    
    /**
     * 自定义路径
     */
    @TableField(exist=false)
    private String postPageName;

    /**
     * 评论者名称
     */
    @TableField("author")
    @NotBlank(message = "请输入称呼")
    @Length(max = 10, message = CommentValidator.AUTHOR_LENGTH_OVERFLOW)
    private String author;
    
    /**
     * 评论人的邮箱
     */
    @TableField("email")
    @NotBlank(message = CommentValidator.CONTENT_CANNOT_BE_EMPTY)
    @Email(message = CommentValidator.EMAIL_INVALID)
    @Size(max = 100, message = CommentValidator.EMAIL_LENGTH_OVERFLOW)
    private String email;
    
    public Comment setEmail(String email) {
        if (email != null) email = email.toLowerCase(Locale.ENGLISH);
        this.email = email;
        return this;
    }

    /**
     * 网址
     */
    @TableField("url")
    @Pattern(regexp  = UsefulRegex.BLANK + UsefulRegex.OR + UsefulRegex.DOMAIN, 
            flags   = Flag.CASE_INSENSITIVE,
            message = CommentValidator.URL_INVALID)
    @Size(max = 50, message = CommentValidator.URL_LENGTH_OVERFLOW)
    private String url;

    /**
     * 评论内容
     */
    @TableField("content")
    @NotBlank(message = "请输入评论内容")
    @Length(min = 1, max = 6000, message = CommentValidator.CONTENT_LENGTH_MISMATCH)
    private String content;

    /**
     * 评论提交时间
     */
    @TableField("create_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime createTime;

    /**
     * 评论时的ip地址
     */
    @TableField("ip")
    private String ip;
    
    @TableField("agent")
    private String agent;

    /**
     * 回复内容
     */
    @TableField("reply_to")
    private Long replyTo;
    
    /**
     * 回复层级
     */
    @TableField("level")
    private Integer level;

    /**
     * 是否审核通过 0-未审核 1-审核通过
     */
    @TableField("comment_status")
    private CommentStatusEnum commentStatus;
    
    public String getCommentStatusNote() {
        return commentStatus.getNote();
    }

    /**
     * 是否删除 0-未删除 1-已删除
     */
    @TableField("is_deleted")
    private Boolean isDeleted = false;
    
    @TableField("is_markdown")
    private boolean markdown = false;
    
    @TableField("subscribe_reply")
    private boolean subscribeReply = false;

    /**
     * 邮件退订令牌
     */
    @TableField("unsubscribe_token")
    private String unsubscribeToken;
    
    @TableField("mail_sent")
    private boolean mailSent = false;
    
    @TableField(exist=false)
    private List<Comment> subComments;  
    
    @TableField(exist=false)
    private IpInfo ipInfo;
    
    /**
     * 是否可编辑
     */
    @TableField(exist=false)
    private boolean editable = false;

    @Override
    public String toString() {
        return new StringBuilder()
                .append("Comment[id=").append(id).append(", ")
                .append("author=").append(author).append(", ")
                .append("email").append(email).append(", ")
                .append("url=").append(url).append(", ")
                .append("content=").append(content).append(", ")
                .append("replyTo=").append(replyTo).append(", ")
                .append("isMarkdown=").append(markdown).append(", ")
                .append("subscribeReply=").append(subscribeReply).append("]")
                .toString();
    }

}
