package com.luojia_channel.modules.post.task;

import com.luojia_channel.common.utils.RedisUtil;
import com.luojia_channel.modules.post.algorithm.PostSelector;
import com.luojia_channel.modules.post.entity.Comment;
import com.luojia_channel.modules.post.entity.Post;
import com.luojia_channel.modules.post.mapper.CommentMapper;
import com.luojia_channel.modules.post.mapper.PostMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;

@Component
@RequiredArgsConstructor
public class UpdateScoreTask {

    private static final int BATCH_SIZE = 100;
    // 每半小时更新热度
    private static final long POST_UPDATE_INTERVAL = 30 * 60;
    private static final long COMMENT_UPDATE_INTERVAL = 30 * 60;

    private final PostSelector postSelector;
    private final PostMapper postMapper;
    private final CommentMapper commentMapper;
    private final RedisUtil redisUtil;

    // 定时更新帖子热度分数
    @Scheduled(fixedRate = POST_UPDATE_INTERVAL * 1000)
    public void updatePostScores() {
        // 扩大更新范围到最近30天的帖子
        LocalDateTime startTime = LocalDateTime.now().minusDays(30);

        LambdaQueryWrapper<Post> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ge(Post::getCreateTime, startTime);

        long totalPosts = postMapper.selectCount(queryWrapper);
        long batches = (totalPosts + BATCH_SIZE - 1) / BATCH_SIZE;

        for (int i = 0; i < batches; i++) {
            queryWrapper.last("LIMIT " + i * BATCH_SIZE + ", " + BATCH_SIZE);
            List<Post> posts = postMapper.selectList(queryWrapper);
            posts.forEach(post -> postSelector.updatePostScore(post.getId()));
        }
    }

    // 定时更新评论热度分数
    @Scheduled(fixedRate = COMMENT_UPDATE_INTERVAL * 1000)
    public void updateCommentScores() {
        // 扩大更新范围到最近15天的评论
        LocalDateTime startTime = LocalDateTime.now().minusDays(15);

        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ge(Comment::getCreateTime, startTime);

        long totalComments = commentMapper.selectCount(queryWrapper);
        long batches = (totalComments + BATCH_SIZE - 1) / BATCH_SIZE;

        for (int i = 0; i < batches; i++) {
            queryWrapper.last("LIMIT " + i * BATCH_SIZE + ", " + BATCH_SIZE);
            List<Comment> comments = commentMapper.selectList(queryWrapper);
            comments.forEach(comment -> postSelector.updateCommentScore(comment.getId()));
        }
    }

    // 定期清理过期的缓存数据
    @Scheduled(cron = "0 0 4 * * ?") // 每天凌晨4点执行
    public void cleanExpiredData() {
        LocalDateTime expiredTime = LocalDateTime.now().minusDays(60);

        // 清理过期的帖子缓存
        LambdaQueryWrapper<Post> postQueryWrapper = new LambdaQueryWrapper<>();
        postQueryWrapper.le(Post::getCreateTime, expiredTime);
        List<Post> expiredPosts = postMapper.selectList(postQueryWrapper);
        expiredPosts.forEach(post -> {
            redisUtil.zRemove("post:hot:", post.getId());
            redisUtil.zRemove("post:time:", post.getId());
        });

        // 清理过期的评论缓存
        LambdaQueryWrapper<Comment> commentQueryWrapper = new LambdaQueryWrapper<>();
        commentQueryWrapper.le(Comment::getCreateTime, expiredTime);
        List<Comment> expiredComments = commentMapper.selectList(commentQueryWrapper);
        expiredComments.forEach(comment -> {
            // 评论相关缓存清理
        });
    }
}
