package shop.alien.entity.second.vo;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import shop.alien.entity.second.SecondGoods;
import shop.alien.entity.store.StoreComment;
import shop.alien.entity.store.StoreImg;

import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Data
@JsonInclude
@ApiModel(value = "二手商品推荐", description = "推荐")
public class SecondGoodsRecommendVo extends SecondGoods {

    @ApiModelProperty(value = "用户名称")
    private String userName;

    @ApiModelProperty(value = "用户PHONE")
    private String userPhone;

    @ApiModelProperty(value = "用户头像")
    private String userImage;

    @ApiModelProperty(value = "距离")
    private String dist;

    @ApiModelProperty(value = "是否关注")
    private Integer fansStatus = 0;

    @ApiModelProperty(value = "是否点赞")
    private String likeStatus;

    @ApiModelProperty(value = "是否收藏")
    private String collectStatus;

    @ApiModelProperty(value = "评论数量")
    private String commentCount;

    @ApiModelProperty(value = "评论列表")
    private List<SecondCommentVo> commentList;

    @TableField(value = "release_time", fill = FieldFill.INSERT)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @ApiModelProperty(value = "发布时间")
    private Date releaseTime;

    @ApiModelProperty(value = "视频第一帧")
    private String videoFirstFrame;

    @ApiModelProperty(value = "话题列表")
    @JsonProperty("topicList")
    private List<String> topicList() {
        return parseTopic(getTopic());
    }

    @ApiModelProperty(value = "图片列表")
    private String[] imgList;

    @ApiModelProperty(value = "发布时间")
    @JsonProperty("timeAgo")
    public String getFormattedTimeAgo() {
        return getTimeAgo(getReleaseTime());
    }


    /**
     * 解析话题列表
     *
     * @param topic 话题列表字符串
     * @return 解析后的话题列表
     */
    public List<String> parseTopic(String topic) {
        if (topic != null && !topic.isEmpty()) {
            return Arrays.asList(topic.split(","));
        } else {
            return Collections.emptyList();
        }
    }

    public String getTimeAgo(Date releaseTime) {
        if (releaseTime == null) {
            return "未知时间";
        }

        long now = System.currentTimeMillis();
        long diff = now - releaseTime.getTime();

        // 新增分钟级判断
        long minutes = TimeUnit.MILLISECONDS.toMinutes(diff);
        if (minutes < 1) {
            return "刚刚";
        } else if (minutes < 60) {
            return minutes + "分钟前";
        }

        long hours = TimeUnit.MILLISECONDS.toHours(diff);
        long days = TimeUnit.MILLISECONDS.toDays(diff);
        long months = days / 31;
        long years = days / 365;

        if (hours < 24) {
            return hours + "小时前";
        } else if (days <= 31) {
            return days + "天前";
        } else if (years >= 1) {
            return years + "年前";
        } else {
            return months + "个月前";
        }
    }
}
