package com.moka.gdtauto.service;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.moka.gdtauto.GdtAutoApplication;
import com.moka.gdtauto.client.TencentAdsApiClientFactory;
import com.tencent.ads.model.v3.*;
import com.tencent.ads.v3.TencentAds;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

import java.util.ArrayList;
import java.util.List;

/**
 * 腾讯广告转化归因查询测试
 * https://developers.e.qq.com/v3.0/docs/api/conversions/get
 * 
 * @author moka
 * @since 2026-02-06
 */
@SpringBootTest(classes = GdtAutoApplication.class)
@ActiveProfiles("test")
class TencentConversionGetTest {

    @Autowired
    private TencentAdsApiClientFactory clientFactory;

    private static final Long ORG_ACCOUNT_ID = 33453856L;
    private static final Long TEST_ACCOUNT_ID = 69781948L;
    private static final String TEST_JOB_NUMBER = "12458";

    @Test
    @DisplayName("测试查询转化归因-按优化目标和营销目标过滤")
    void testGetConversions() {
        System.out.println("\n========================================");
        System.out.println("开始查询转化归因");
        System.out.println("账户ID: " + TEST_ACCOUNT_ID);
        System.out.println("========================================\n");

        try {
            // 获取TencentAds实例
            TencentAds tencentAds = clientFactory.getTencentAds(TEST_JOB_NUMBER, ORG_ACCOUNT_ID);
            tencentAds.setDebug(true);
            
            // 构建过滤条件
            List<FilteringStruct> filtering = buildFiltering();
            
            // 输出请求参数
            System.out.println("========== 请求参数 ==========");
            Gson gson = new GsonBuilder().setPrettyPrinting().create();
            System.out.println("Account ID: " + TEST_ACCOUNT_ID);
            System.out.println("Filtering: " + gson.toJson(filtering));
            System.out.println("========================================\n");
            List<String> fields = List.of("conversion_id", "conversion_name", "optimization_goal");
            // 调用查询转化归因接口
            System.out.println("正在调用腾讯广告查询转化归因API...");
            ConversionsGetResponseData response = tencentAds.conversions()
                .conversionsGet(
                    TEST_ACCOUNT_ID,
                    filtering,
                    null,  // page
                    null,  // page_size
                    fields   // fields
                );

            // 输出响应结果
            System.out.println("\n========== API返回结果 ==========");
            System.out.println(gson.toJson(response));
            System.out.println("========================================\n");

            // 输出摘要信息
            if (response != null && response.getList() != null) {
                System.out.println("✅ 查询成功");
                System.out.println("📊 转化归因数量: " + response.getList().size());
                System.out.println("\n转化归因列表：");
                for (ConversionsGetListStruct conversion : response.getList()) {
                    System.out.println("  - 转化ID: " + conversion.getConversionId());
                    System.out.println("    转化名称: " + conversion.getConversionName());
                    System.out.println("    优化目标: " + conversion.getOptimizationGoal());
                    System.out.println();
                }
            } else {
                System.out.println("⚠️  返回结果为空");
            }

        } catch (Exception e) {
            System.err.println("\n❌ API调用失败");
            System.err.println("错误信息: " + e.getMessage());
            e.printStackTrace();
            throw new RuntimeException("测试失败", e);
        }
    }

    /**
     * 构建过滤条件
     */
    private List<FilteringStruct> buildFiltering() {
        List<FilteringStruct> filtering = new ArrayList<>();
        
        // 过滤条件1：优化目标 = OPTIMIZATIONGOAL_APP_REGISTER
        FilteringStruct filter1 = new FilteringStruct();
        filter1.setField("optimization_goal");
        filter1.setOperator(FilterOperator.EQUALS);
        List<String> values1 = new ArrayList<>();
        values1.add("OPTIMIZATIONGOAL_APP_REGISTER");
        filter1.setValues(values1);
        filtering.add(filter1);
        
        // 过滤条件2：营销目标 = MARKETING_GOAL_USER_GROWTH
        FilteringStruct filter2 = new FilteringStruct();
        filter2.setField("marketing_goal");
        filter2.setOperator(FilterOperator.EQUALS);
        List<String> values2 = new ArrayList<>();
        values2.add("MARKETING_GOAL_USER_GROWTH");
        filter2.setValues(values2);
        filtering.add(filter2);
        
        // 过滤条件3：营销资产类型 = MARKETING_TARGET_TYPE_SHORT_DRAMA
        FilteringStruct filter3 = new FilteringStruct();
        filter3.setField("marketing_target_type");
        filter3.setOperator(FilterOperator.EQUALS);
        List<String> values3 = new ArrayList<>();
        values3.add("MARKETING_TARGET_TYPE_SHORT_DRAMA");
        filter3.setValues(values3);
        filtering.add(filter3);
        
        return filtering;
    }
}
