package com.tzld.piaoquan.ad.engine.service.predict.v2;

import com.alibaba.fastjson.JSONObject;
import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;
import com.tzld.piaoquan.ad.engine.commons.util.JSONUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.*;

@Slf4j
@Component
public class UserLayerRootSessionIdPredict extends BasicPredict {

    @ApolloJsonValue("${user.layer.root.session.id.tail.predict.config:[]}")
    private List<RootSessionIdTailConfigItem> configItems;

    @Override
    public Map<String, Object> predict(PredictContext ctx) {

        String rootSessionId = ctx.getRootSessionId();
        String userLayer = ctx.getUserLayer();
        String shareType = ctx.getShareType();
        if (CollectionUtils.isEmpty(configItems) || StringUtils.isAnyBlank(rootSessionId, userLayer, shareType)) {
            return Collections.emptyMap();
        }

        double score = this.calcScoreByMid(ctx.getMid());
        String appType = ctx.getAppType();
        String tail = rootSessionId.substring(rootSessionId.length() - 1);
        shareType = ctx.getShareType()
                .replace("return", "")
                .replace("mids", "");

        for (RootSessionIdTailConfigItem item : configItems) {
            if (item.getAppType().contains(appType) && item.getTail().contains(tail)) {
                Map<String, Object> returnMap = new HashMap<>();
                double threshold;
                String thresholdKey = "";

                // 生成可用的Key列表
                List<String> keys = Arrays.asList(userLayer + "_" + shareType, "default_" + shareType, "default_threshold");
                for (String key : keys) {
                    if (item.getConfig().containsKey(key)) {
                        thresholdKey = key;
                        break;
                    }
                }


                threshold = item.getConfig().getOrDefault(thresholdKey, 0.0);

                if (score < threshold) {
                    returnMap.putAll(rtnAdPredict(ctx));
                    returnMap.put("model", this.name());
                } else {
                    returnMap.putAll(rtnNoAdPredict(ctx));
                    returnMap.put("no_ad_strategy", this.name());
                }

                returnMap.put("score", score);
                returnMap.put("threshold", threshold);
                returnMap.put("userLayer", userLayer);

                JSONObject logJson = new JSONObject();
                logJson.putAll(returnMap);
                logJson.put("mid", ctx.getMid());
                logJson.put("appType", appType);
                logJson.put("rootSessionIdTail", tail);
                logJson.put("shareType", shareType);

                logJson.put("expId", "userLayerRootSessionIdTailExp");
                logJson.put("thresholdParamKey", thresholdKey);
                logJson.put("adPlatformType", ctx.getAdPlatformType());
                logJson.put("abCode", ctx.getAdAbCode());
                logJson.put("configItem", item);
                logJson.put("rootSessionId", ctx.getRootSessionId());

                log.info("广告跳出选择 -- userLayerRootSessionId尾号实验结果: {}, 参数: {}",
                        JSONUtils.toJson(returnMap), logJson.toJSONString());

                return returnMap;
            }
        }


        return Collections.emptyMap();
    }

    @Override
    public String name() {
        return "userLayerRootSessionIdTailModel";
    }
}
