package com.test.musichouduan.util;

import com.test.musichouduan.entity.Singer;
import java.time.Duration;
import java.time.LocalDateTime;

/**
 * 歌手热度分数计算工具类
 */
public class HotSingerScoreUtil {

    // 定义各种交互的权重
    private static final double FANS_WEIGHT = 2.0; // 粉丝数（收藏数）
    private static final double COMMENT_WEIGHT = 1.5;

    // 时间衰减因子中的指数
    private static final double TIME_DECAY_EXPONENT = 1.8;

    /**
     * 计算单个歌手的热度分数
     * 算法: 热度 = (交互权重和) / (时间衰减因子)
     *
     * @param singer 歌手实体对象
     * @return 计算出的热度分数
     */
    public static double calculateHotScore(Singer singer) {
        // 1. 计算交互权重和
        long fansCount = singer.getCollectCount() != null ? singer.getCollectCount() : 0;
        long commentCount = singer.getCommentCount() != null ? singer.getCommentCount() : 0;

        double interactionScore = (fansCount * FANS_WEIGHT) +
                                  (commentCount * COMMENT_WEIGHT);

        // 2. 计算时间衰减因子
        LocalDateTime createTime = singer.getCreateTime();
        LocalDateTime now = LocalDateTime.now();
        long hoursElapsed = Duration.between(createTime, now).toHours();

        double timeDecayFactor = Math.pow(hoursElapsed + 2, TIME_DECAY_EXPONENT);

        // 3. 计算最终热度分数
        return interactionScore / timeDecayFactor;
    }
}
