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

import cn.hutool.http.HttpRequest;
import com.fs.newAdv.integration.client.AbstractApiClient;
import com.fs.common.constant.SystemConstant;
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.HashMap;
import java.util.Map;

/**
 * 爱奇艺API客户端
 *
 * @author zhangqin
 * @see <a href="https://iq.feishu.cn/wiki/YbDzwp0K3iy5IOkLHjGcBKcxnBc">爱奇艺 API文档</a>
 */
@Slf4j
@Component
public class IQIYIApiClient extends AbstractApiClient {

    /**
     * 爱奇艺 回传API地址
     */
    private static final String CONVERSION_API_URL = "http://tc.cupid.iqiyi.com/dsp_lpapi";

    /**
     * 回传转化数据到爱奇艺
     *
     * @param conversionData 转化数据
     * @return 是否成功
     */
    @Override
    public boolean reportConversion(Map<String, Object> conversionData) {
        return executeWithLog(AdvertiserTypeEnum.OCEANENGINE, CONVERSION_API_URL, conversionData, () -> {
            // 构建请求参数
            Map<String, Object> body = buildConversionParams(conversionData);
            // 发送HTTP请求
            return HttpRequest.get(CONVERSION_API_URL)
                    .form(body)
                    .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("impress_id", conversionData.get("traceId"));
        params.put("event_type", conversionData.get("eventType"));
        // 转化价值（可选）
        if (conversionData.containsKey("value")) {
            params.put("pmao", conversionData.get("value"));
        }
        return params;
    }

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

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

