package com.test.musichouduan.util;

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

/**
 * 歌单热度分数计算工具类
 */
public class HotPlaylistScoreUtil {

    // 定义各种交互的权重
    private static final double PLAY_WEIGHT = 1.0;
    private static final double COLLECT_WEIGHT = 2.0;
    private static final double COMMENT_WEIGHT = 1.5;

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

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

        double interactionScore = (playCount * PLAY_WEIGHT) +
                                  (collectCount * COLLECT_WEIGHT) +
                                  (commentCount * COMMENT_WEIGHT);

        // 2. 计算时间衰减因子
        LocalDateTime createTime = playlist.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;
    }
}
