package com.tzld.piaoquan.recommend.server.service.recall;

import com.google.common.collect.Lists;
import com.tzld.piaoquan.recommend.server.service.filter.FilterParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dyp
 */
public class FilterParamFactory {
    public static FilterParam create(RecallParam param, List<Long> videoIds) {
        return create(param, videoIds, null, null);
    }

    /**
     * 漏斗版：额外透传 pushFrom + scoresMap，让 FilterService 记录 raw 阶段数据。
     * 任一为 null 时退化为原行为（不记录漏斗 raw）。
     */
    public static FilterParam create(RecallParam param, List<Long> videoIds,
                                     String pushFrom, Map<Long, Double> scoresMap) {
        FilterParam filterParam = new FilterParam();
        filterParam.setVideoIds(videoIds);
        filterParam.setAppType(param.getAppType());
        filterParam.setMid(param.getMid());
        filterParam.setUid(param.getUid());

        // 风险过滤
        filterParam.setRiskUser(param.isRiskUser());
        filterParam.setTestingRiskRegion(param.isTestingRiskRegion());
        filterParam.setRegionCode(param.getRegionCode());
        filterParam.setAbExpCodes(param.getAbExpCodes());
        filterParam.setCityCode(param.getCityCode());
        filterParam.setHotSceneType(param.getHotSceneType());
        filterParam.setClientIp(param.getClientIp());
        filterParam.setRootSessionId(param.getRootSessionId());
        filterParam.setSessionId(param.getSessionId());

        filterParam.setPushFrom(pushFrom);
        filterParam.setScoresMap(scoresMap);
        filterParam.setFunnelContext(param.getFunnelContext());
        return filterParam;
    }

    /**
     * 按位置反推生成 scoresMap (score = ids.size() - i)，用于无原始召回分的 strategy。
     * Strategy 应该把这同一份 scoresMap 既传给 FilterParamFactory.create，又用来给 Video.setRovScore，
     * 保证 filter 前/后口径一致。
     */
    public static Map<Long, Double> positionScores(List<Long> ids) {
        Map<Long, Double> scoresMap = new HashMap<>(ids.size());
        int n = ids.size();
        for (int i = 0; i < n; i++) {
            scoresMap.put(ids.get(i), (double) (n - i));
        }
        return scoresMap;
    }
}
