package com.tzld.piaoquan.ad.engine.server.controller;

import com.tzld.piaoquan.ad.engine.commons.enums.GuaranteedTypeEnum;
import com.tzld.piaoquan.ad.engine.service.score.RankService;
import com.tzld.piaoquan.ad.engine.commons.dto.AdPlatformCreativeDTO;
import com.tzld.piaoquan.ad.engine.service.score.deprecated.BidRankRecommendRequestParam;
import com.tzld.piaoquan.ad.engine.commons.param.RankRecommendRequestParam;
import com.tzld.piaoquan.recommend.feature.domain.ad.base.AdRankItem;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/recommend")
public class AdRecommendController {

    private final static Logger log = LoggerFactory.getLogger(AdRecommendController.class);
    @Autowired
    @Qualifier("rankServiceImpl")
    RankService rankService;

    @RequestMapping("/top1/basic")
    public Map<String, Object> adRecommendTop1Basic(@RequestBody RankRecommendRequestParam request) {
        Map<String, Object> map = new HashMap<>();
        if (CollectionUtils.isEmpty(request.getAdIdList())) {
            map.put("code", "1");
            map.put("msg", "score error");
            return map;
        }
        try {
            AdRankItem rankResult = rankService.adItemRank(request);
            if (Objects.nonNull(rankResult)) {
                map.put("code", "0");
                map.put("msg", "success");
                Map<String, Object> contentMap = new HashMap<>();
                contentMap.put("adId", rankResult.getAdId());
                contentMap.put("adScore", rankResult.getScore());
                Double ctcvrScore = rankResult.getScoreMap().get("ctcvrScore");
                if (ctcvrScore != null) {
                    contentMap.put("ecpm", ctcvrScore * rankResult.getCpa() * 1000);
                }
                if (rankResult.getExt().get("isGuaranteed") != null && rankResult.getExt().get("isGuaranteed").equals(true)) {
                    contentMap.put("type", GuaranteedTypeEnum.IS_GUARANTEED.getType());
                } else {
                    contentMap.put("type", GuaranteedTypeEnum.NOT_GUARANTEED.getType());
                }
                map.put("content", contentMap);
                return map;
            }
        } catch (Exception e) {
            log.error("模型排序异常: ", e);
        }

        map.put("code", "1");
        map.put("msg", "score error");
        return map;
    }

    @RequestMapping("/top1/video/ad/thompson")
    public Map<String, Object> adRecommendTop1VideoAdThompson(@RequestBody RankRecommendRequestParam request) {
        AdRankItem rankResult = rankService.adItemRankWithVideoAdThompson(request);
        HashMap map = new HashMap();
        map.put("code", "0");
        map.put("msg", "success");
        HashMap contentMap = new HashMap<>();
        contentMap.put("adId", rankResult.getAdId());
        contentMap.put("adScore", rankResult.getScore());
        map.put("content", contentMap);
        return map;
    }

    @RequestMapping("/top1/bid/basic")
    public Map<String, Object> adRecommendTop1BidBasic(@RequestBody BidRankRecommendRequestParam request) {
        AdPlatformCreativeDTO rankResult = rankService.adBidRank(request);
        HashMap map = new HashMap();
        map.put("code", "0");
        map.put("msg", "success");
        map.put("content", rankResult);
        return map;
    }

    @RequestMapping("/top1/bid/new/pid")
    public Map<String, Object> adRecommendTop1BidNewPid(@RequestBody BidRankRecommendRequestParam request) {
        AdPlatformCreativeDTO rankResult = rankService.adBidRankNewPid(request);
        HashMap map = new HashMap();
        map.put("code", "0");
        map.put("msg", "success");
        map.put("content", rankResult);
        return map;
    }

}
