package cn.reghao.tnb.content.app.vod.service;

import cn.reghao.tnb.common.web.WebResult;
import cn.reghao.tnb.common.db.PageScroll;
import cn.reghao.tnb.content.api.dto.VideoCard;
import cn.reghao.tnb.content.api.dto.VideoPostCard;
import cn.reghao.tnb.content.app.vod.db.mapper.VideoPostMapper;
import cn.reghao.tnb.content.app.vod.model.po.VideoPost;
import cn.reghao.tnb.content.app.vod.model.query.VideoQuery;
import cn.reghao.tnb.content.app.vod.model.vo.BannerVideoVO;
import cn.reghao.tnb.user.api.iface.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2025-06-04 09:59:49
 */
@Service
public class VideoQueryService {
    @DubboReference(check = false, retries = 0, timeout = 3000)
    private UserService userService;

    private final int pageSize = 12;
    private final ContentPermission contentPermission;
    private final VideoPostMapper videoPostMapper;
    private final VideoPostQuery videoPostQuery;

    public VideoQueryService(ContentPermission contentPermission, VideoPostMapper videoPostMapper,
                             VideoPostQuery videoPostQuery) {
        this.contentPermission = contentPermission;
        this.videoPostMapper = videoPostMapper;
        this.videoPostQuery = videoPostQuery;
    }

    /**
     * 视频稿件时间线
     *
     * @param
     * @return
     * @date 2023-08-26 01:08:740
     */
    public PageScroll<VideoCard> getVideoTimeline(long userId, String nextIdStr) {
        List<Long> followings = userService.getFollowingIds(userId);
        followings.add(userId);

        int nextId;
        if (nextIdStr.equals("0")) {
            nextId = 99_999_999;
        } else {
            VideoPost videoPost = videoPostMapper.findByVideoId(nextIdStr);
            if (videoPost == null) {
                return PageScroll.empty();
            }

            nextId = videoPost.getId();
        }

        List<VideoPostCard> list = videoPostMapper.findByUserIds(followings, pageSize, nextId);
        if (list.isEmpty()) {
            return PageScroll.empty();
        }

        List<VideoCard> list1 = list.stream()
                .map(videoPostCard -> videoPostQuery.getVideoCard(videoPostCard, true))
                .collect(Collectors.toList());
        nextIdStr = list.get(list.size()-1).getVideoId();
        return PageScroll.pageList(0, nextIdStr, nextIdStr, list1);
    }

    public List<BannerVideoVO> getBannerVideos() {
        return videoPostQuery.getBannerVideos();
    }

    public String getSimilarVideos(String videoId) {
        VideoPost videoPost = videoPostMapper.findByVideoId(videoId);
        if (videoPost == null) {
            return WebResult.notFound();
        }

        LocalDateTime publishAt = videoPost.getPublishAt();
        long publishBy = videoPost.getPublishBy();
        List<Integer> scopes = contentPermission.getUserScopes();
        VideoQuery videoQuery = new VideoQuery.Builder().scope(scopes).userId(publishBy).build();
        List<VideoPostCard> list = videoPostMapper.findByNextVideos(videoQuery, publishAt);
        List<VideoCard> list1 = list.stream()
                .map(videoPostCard -> videoPostQuery.getVideoCard(videoPostCard, true))
                .collect(Collectors.toList());
        return WebResult.success(list1);
    }
}
