package com.fs.newAdv.integration.client.advertiser;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.fs.newAdv.integration.client.AbstractApiClient;
import com.fs.common.constant.SystemConstant;
import com.fs.common.utils.SnowflakeUtil;
import com.fs.newAdv.domain.PromotionAccount;
import com.fs.newAdv.domain.SiteStatistics;
import com.fs.newAdv.enums.AdvertiserTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

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

/**
 * VIVOAPI客户端
 *
 * @author zhangqin
 * @see <a href="https://open-ad.vivo.com.cn/doc?id=636">VIVO API文档</a>
 */
@Slf4j
@Component
public class VIVOApiClient extends AbstractApiClient {

    /**
     * VIVO 回传API地址
     */
    private static final String CONVERSION_API_URL = "https://marketing-api.vivo.com.cn/openapi/v1/advertiser/behavior/upload";

    /**
     * 回传转化数据
     *
     * @param conversionData 转化数据
     * @return 是否成功
     */
    @Override
    public boolean reportConversion(Map<String, Object> conversionData) {
        String url = CONVERSION_API_URL +
                "?access_token=" + conversionData.get("accessToken") +
                "&timestamp=" + conversionData.get("timestamp") +
                "&nonce=" + SnowflakeUtil.nextId() +
                "&advertiser_id=" + conversionData.get("advertiserId");
        return executeWithLog(AdvertiserTypeEnum.VIVO, url, conversionData, () -> {
            // 构建请求参数
            Map<String, Object> requestBody = buildConversionParams(conversionData);
            // 发送HTTP请求
            return HttpRequest.post(url)
                    .header("Content-Type", "application/json")
                    .body(JSONUtil.toJsonStr(requestBody))
                    .timeout(SystemConstant.API_TIMEOUT)
                    .execute();
        });
    }

    /**
     * 构建转化回传参数
     *
     * @param conversionData 转化数据
     * @return 请求参数
     */
    protected Map<String, Object> buildConversionParams(Map<String, Object> conversionData) {
        Map<String, Object> params = new HashMap<>();
        // 事件类型
        params.put("srcType", "web");
        // 事件源id
        params.put("srcId", conversionData.get("srcId"));
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> dataList = new HashMap<>();
        dataList.put("cvType", conversionData.get("eventType"));
        dataList.put("cvTime", conversionData.get("timestamp"));
        dataList.put("requestId", conversionData.get("requestId"));
        dataList.put("creativeId", conversionData.get("creativeId"));
        list.add(dataList);
        params.put("dataList", list);
        return params;
    }


    @Override
    public AdvertiserTypeEnum getAdvertiserType() {
        return AdvertiserTypeEnum.VIVO;
    }

    @Override
    public SiteStatistics getDataReport(PromotionAccount account,String ideaId,String startDate,String endDate) {
        return null;
    }
}

