package com.sonic.frog.domain.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.sonic.frog.enums.HeartbeatLevelEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneId;

/**
 * 用户与AI的心动关系
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@TableName("ai_user_heartbeat_relation")
@ApiModel("用户与AI的心动关系")
public class AiUserHeartbeatRelation {
    @ApiModelProperty("id")
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    @ApiModelProperty("用户id")
    @TableField("user_id")
    private Long userId;

    @ApiModelProperty("AI的id")
    @TableField("ai_id")
    private Long aiId;

    @ApiModelProperty("对应心动等级字典code")
    @TableField("heartbeat_level")
    private HeartbeatLevelEnum heartbeatLevel;

    @ApiModelProperty("当前用户心动值")
    @TableField("heartbeat_val")
    private BigDecimal heartbeatVal;

    @ApiModelProperty("心动分")
    @TableField("heartbeat_score")
    private BigDecimal heartbeatScore;

    @ApiModelProperty("已扣减心动值")
    @TableField("subtract_heartbeat_val")
    private BigDecimal subtractHeartbeatVal;

    @ApiModelProperty("首次聊天时间 用于计算相识天数")
    @TableField("first_chat_time")
    private LocalDateTime firstChatTime;

    @ApiModelProperty("最后聊天时间 用于24小时未聊天计算扣减心动值")
    @TableField("last_chat_time")
    private LocalDateTime lastChatTime;

    @ApiModelProperty("最后一次计算扣减心动值时间")
    @TableField("last_subtract_time")
    private LocalDateTime lastSubtractTime;

    @ApiModelProperty("关系显示开关 默认关闭 0:关闭 1:打开")
    @TableField("is_show")
    private Boolean isShow;

    @ApiModelProperty("是否第一次降级 0:否 1:是")
    @TableField("is_first_downgrade")
    private Boolean isFirstDowngrade;

    @ApiModelProperty("创建时间")
    @TableField("create_time")
    private LocalDateTime createTime;

    @ApiModelProperty("编辑时间")
    @TableField("edit_time")
    private LocalDateTime editTime;

    /**
     * 计算相识天数
     *
     * @return
     */
    public Integer getDayCount() {
        if (firstChatTime == null) {
            return 0;
        }
        long startTime = firstChatTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        long endTime = LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        return (int) ((endTime - startTime) / (1000 * 60 * 60 * 24));
    }
}