package com.tzld.piaoquan.recommend.server.service.rank.strategy;


import com.tzld.piaoquan.recommend.feature.domain.video.base.UserFeature;
import com.tzld.piaoquan.recommend.server.common.base.RankItem;
import com.tzld.piaoquan.recommend.server.common.enums.AppTypeEnum;
import com.tzld.piaoquan.recommend.server.model.Video;
import com.tzld.piaoquan.recommend.server.service.flowpool.FlowPoolConstants;
import com.tzld.piaoquan.recommend.server.service.rank.RankParam;
import com.tzld.piaoquan.recommend.server.service.rank.RankService;

import com.tzld.piaoquan.recommend.server.service.recall.RecallResult;
import com.tzld.piaoquan.recommend.server.service.score.ScoreParam;
import com.tzld.piaoquan.recommend.server.service.score.ScorerUtils;
import com.tzld.piaoquan.recommend.server.util.CommonCollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author 孙铭泽
 * @desc 对流量池增加排序Thompson sampling 策略
 * @date 2024-01-17
 */
@Service
@Slf4j
public class RankStrategyFlowThompsonModel extends RankService {

    public List<Video> mergeAndRankFlowPoolRecall(RankParam param) {
        if (param.getAppType() == AppTypeEnum.LAO_HAO_KAN_VIDEO.getCode()
                || param.getAppType() == AppTypeEnum.ZUI_JING_QI.getCode()) {
            if (param.getAbCode().equals("60054")
                    || param.getAbCode().equals("60068")
                    || param.getAbCode().equals("60081")
                    || param.getAbCode().equals("60084")) {
                return extractAndSort(param, FlowPoolConstants.QUICK_PUSH_FORM);
            } else {
                return Collections.emptyList();
            }
        } else {
            List<Video> quickFlowPoolVideos = sortFlowPoolByThompson(param, FlowPoolConstants.QUICK_PUSH_FORM);
            if (CollectionUtils.isNotEmpty(quickFlowPoolVideos)) {
                return quickFlowPoolVideos;
            } else {
                return sortFlowPoolByThompson(param, FlowPoolConstants.PUSH_FORM);
            }
        }
    }

    public List<Video> sortFlowPoolByThompson(RankParam param, String pushFrom) {

        //初始化 userid
        UserFeature userFeature = new UserFeature();
        userFeature.setMid(param.getMid());

        // 初始化RankItem
        Optional<RecallResult.RecallData> data = param.getRecallResult().getData().stream()
                .filter(d -> d.getPushFrom().equals(pushFrom))
                .findFirst();
        if (!data.isPresent()){
            return Collections.emptyList();
        }
        List<Video> videoList = data.get().getVideos();
        if (videoList == null) {
            return Collections.emptyList();
        }
        List<RankItem> rankItems = new ArrayList<>();
        for (int i = 0; i < videoList.size(); i++) {
            RankItem rankItem = new RankItem(videoList.get(i));
            rankItems.add(rankItem);
        }

        // 初始化上下文参数
        ScoreParam scoreParam = convert(param);
        List<RankItem> rovRecallScore = ScorerUtils.getScorerPipeline(ScorerUtils.FLOWPOOL_CONF)
                .scoring(scoreParam, userFeature, rankItems);

        if (rovRecallScore == null) {
            return Collections.emptyList();
        }

        return CommonCollectionUtils.toList(rovRecallScore, i -> {
            // hard code 将排序分数 赋值给video的sortScore
            Video v = i.getVideo();
            v.setSortScore(i.getScore());
            return v;
        });
    }

}
