package com.moka.gdtauto.util;

import java.math.BigDecimal;
import java.math.RoundingMode;

import org.springframework.beans.BeanUtils;

import com.tencent.ads.model.v3.AdgroupsAddRequest;


import com.tencent.ads.model.v3.MarketingAssetOuterSpec;
import com.tencent.ads.model.v3.MarketingCarrierType;
import com.tencent.ads.model.v3.MarketingGoal;
import com.tencent.ads.model.v3.MarketingTargetType;
import com.tencent.ads.model.v3.BidMode;

import com.tencent.ads.model.v3.BidScene;

import com.tencent.ads.model.v3.WriteTargetingSetting;
import com.tencent.ads.model.v3.OptimizationGoal;
import com.tencent.ads.model.v3.SearchExpandTargetingSwitch;
import com.tencent.ads.model.v3.SmartTargetingMode;



public class AdgroupsAddReqPool {
    
    private static final AdgroupsAddRequest leadRequest = buildLEADRequest();
    private static final AdgroupsAddRequest roiRequest = buildROIRequest();


    public static AdgroupsAddRequest getAdgroupsAddRequest(
        Long accountId ,String groupName, String wxCorpId,
         String beginDate, String endDate, 
         Integer targetConfId, Integer siteConfId, 
        Long bidAmount, Long conversionId) {
        AdgroupsAddRequest request = new AdgroupsAddRequest();
        BeanUtils.copyProperties(leadRequest, request);
        request.setAccountId(accountId);
        request.setAdgroupName(groupName);
        request.getMarketingAssetOuterSpec().setMarketingAssetOuterId(wxCorpId);
        request.setBeginDate(beginDate);
        request.setEndDate(endDate);
        request.setTargeting(TargetConfUtil.getTargetingSetting(targetConfId));
        request.setSiteSet(SiteConfUtil.getSiteConf(siteConfId));
        request.setBidAmount(bidAmount);
        request.setConversionId(conversionId);
        request.setSmartTargetingMode(SmartTargetingMode.SMART_TARGETING_MANUAL);
        return request;
    }

    public static AdgroupsAddRequest getROIAdgroupsAddRequest(
        Long accountId ,String groupName, Long marketingAssetId,
         String beginDate, String endDate, 
         Integer targetConfId, Integer siteConfId, 
        Long bidAmount, Long conversionId, BigDecimal deepConversionWorthRate) {
        AdgroupsAddRequest request = new AdgroupsAddRequest();
        BeanUtils.copyProperties(roiRequest, request);
        request.setAccountId(accountId);
        request.setAdgroupName(groupName);
        request.setMarketingAssetId(marketingAssetId);
        request.setBeginDate(beginDate);
        request.setEndDate(endDate);
        request.setTargeting(TargetConfUtil.getTargetingSetting(targetConfId));
        request.setSiteSet(SiteConfUtil.getSiteConf(siteConfId));
        request.setBidAmount(bidAmount);
        request.setConversionId(conversionId);
        // 保留2位小数
        request.setDeepConversionWorthRate(deepConversionWorthRate.setScale(2, RoundingMode.HALF_UP).doubleValue());
        request.setSmartTargetingMode(SmartTargetingMode.SMART_TARGETING_MANUAL);
        return request;
    }


    private static  AdgroupsAddRequest buildLEADRequest() {
        AdgroupsAddRequest request = new AdgroupsAddRequest();

        // 基础字段
        request.setAdgroupName("");
        request.setAccountId(0L);
        //营销目的类型 : 线索留资
        request.setMarketingGoal(MarketingGoal.LEAD_RETENTION);
        //营销载体类型 : 页面跳转
        request.setMarketingCarrierType(MarketingCarrierType.JUMP_PAGE);
        //营销资产外部规格 : 企业微信
        MarketingAssetOuterSpec outerSpec = new MarketingAssetOuterSpec();
        outerSpec.setMarketingTargetType(MarketingTargetType.WECHAT_WORK);
        
        // 产品外部 id 数据 : 企业微信ID 
        outerSpec.setMarketingAssetOuterId("");
        request.setMarketingAssetOuterSpec(outerSpec);
        
        // 投放时间
        request.setBeginDate("2026-02-05");
        request.setEndDate("2026-02-06");
        
        // 优化目标
        request.setOptimizationGoal(OptimizationGoal.PAGE_SCAN_CODE);
        
        // 时间序列（全部1表示24小时投放，共7天*48个半小时=336位）
        request.setTimeSeries("111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111");
        
        // 投放站点

        request.setSiteSet(SiteConfUtil.getSiteConf(1));
        
        // 出价金额（单位：分）
        request.setBidAmount(100L);
        // 出价场景 常规投放-稳定投放
        request.setBidScene(BidScene.NORMAL_AVERAGE);
        
        // 日预算（单位：分）设置为 0 表示不设预算
        // TODO 测试期间设置上限
        // request.setDailyBudget(10000L);
        request.setDailyBudget(0L);
        // 出价模式
        request.setBidMode(BidMode.OCPM);

        // 转化 id
        request.setConversionId(67676141L);
        
        // 出价场景
        request.setBidScene(BidScene.UNKNOWN);
        
        // 定向设置
        WriteTargetingSetting targeting = TargetConfUtil.getTargetingSetting(1);
        request.setTargeting(targeting);

        // 创意增强 MAX 开关
        request.setAutoDerivedCreativeEnabled(true);

        return request;
    }

    /**
     * 构建ROI请求对象
     */
    private static  AdgroupsAddRequest buildROIRequest() {
        AdgroupsAddRequest request = new AdgroupsAddRequest();

        // 基础字段
        request.setAdgroupName("");
        request.setAccountId(0L);
        request.setMarketingGoal(MarketingGoal.USER_GROWTH);
        request.setMarketingCarrierType(MarketingCarrierType.JUMP_PAGE);
        // 营销资产ID 比如短剧ID
        request.setMarketingAssetId(0L);

        
        // 投放时间
        request.setBeginDate("2026-02-06");
        request.setEndDate("2027-02-06");
        
        // 优化目标
        request.setOptimizationGoal(OptimizationGoal.APP_REGISTER);
        
        // 时间序列（全部1表示24小时投放，共7天*48个半小时=336位）
        request.setTimeSeries("111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111");
        
        // 投放站点
      

        request.setSiteSet(SiteConfUtil.getSiteConf(1));
        
        // 出价金额（单位：分）
        request.setBidAmount(100L);
        
        // 日预算（单位：分）设置为 0 表示不设预算
        // TODO 测试期间设置上限
        request.setDailyBudget(10000L);
        // 期望ROI
        request.setDeepConversionWorthRate(0.2);
    
        // 出价模式
        request.setBidMode(BidMode.OCPM);

        // 转化 id
        request.setConversionId(72627668L);
        
        // 出价场景
        request.setBidScene(BidScene.UNKNOWN);
        
        // 定向设置
        request.setTargeting(TargetConfUtil.getTargetingSetting(1));

        // 创意增强 MAX 开关
        request.setAutoDerivedCreativeEnabled(true);
        
        //搜索定向拓展开关
        request.setSearchExpandTargetingSwitch(SearchExpandTargetingSwitch.CLOSE);

        return request;
    }
}
