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.MarketingTargetAssetsGetResponseData;
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;

/**
 * 腾讯广告短剧资产测试
 * https://developers.e.qq.com/v3.0/docs/api/marketing_target_assets/get
 * @author moka
 * @since 2026-02-06
 */
@SpringBootTest(classes = GdtAutoApplication.class)
@ActiveProfiles("test")
class TencentAssetDramaTest {

    @Autowired
    private TencentAssetDramaService assetDramaService;

    @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 testGetMarketingAssets() {
        System.out.println("\n========================================");
        System.out.println("开始获取短剧资产列表");
        System.out.println("账户ID: " + TEST_ACCOUNT_ID);
        System.out.println("资产类型: MARKETING_TARGET_TYPE_SHORT_DRAMA");
        System.out.println("========================================\n");

        try {
            // 调用API获取短剧资产列表
            System.out.println("正在调用腾讯广告API...");
            MarketingTargetAssetsGetResponseData response = assetDramaService.getMarketingAssets(
                ORG_ACCOUNT_ID,
                TEST_JOB_NUMBER,
                TEST_ACCOUNT_ID,
                null,  // 无过滤条件
                1L,    // 第1页
                10L    // 每页10条
            );

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

            // 输出摘要信息
            if (response != null) {
                System.out.println("✅ 成功获取短剧资产列表");
                
                if (response.getPageInfo() != null) {
                    System.out.println("📊 总数: " + response.getPageInfo().getTotalNumber());
                    System.out.println("📊 总页数: " + response.getPageInfo().getTotalPage());
                    System.out.println("📊 当前页: " + response.getPageInfo().getPage());
                    System.out.println("📊 每页大小: " + response.getPageInfo().getPageSize());
                }
                
                if (response.getList() != null && !response.getList().isEmpty()) {
                    System.out.println("\n📋 短剧资产列表:");
                    response.getList().forEach(asset -> {
                        System.out.println("  - ID: " + asset.getMarketingAssetId() + 
                                         ", 名称: " + asset.getMarketingAssetName() +
                                         ", 类型: " + asset.getMarketingAssetType());
                        
                        // 输出properties详情
                        if (asset.getProperties() != null && !asset.getProperties().isEmpty()) {
                            System.out.println("    属性列表:");
                            asset.getProperties().forEach(prop -> {
                                System.out.println("      * " + prop.getPropertyCn() + 
                                                 " (" + prop.getPropertyName() + "): " + 
                                                 prop.getPropertyValue());
                            });
                        }
                    });
                } else {
                    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);
        }
    }

    @Test
    @DisplayName("测试直接使用SDK获取短剧资产")
    void testGetMarketingAssetsBySDK() {
        System.out.println("\n========================================");
        System.out.println("开始测试直接使用SDK获取短剧资产");
        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);
            
            // 调用SDK获取短剧资产列表
            System.out.println("正在调用腾讯广告SDK API...");
            MarketingTargetAssetsGetResponseData response = tencentAds.marketingTargetAssets()
                .marketingTargetAssetsGet(
                    "MARKETING_TARGET_TYPE_SHORT_DRAMA",  // marketing_target_type
                    TEST_ACCOUNT_ID,
                    null,  // filtering
                    1L,    // page
                    10L,   // page_size
                    null,  // organization_id
                    null   // fields
                );

            // 输出响应结果
            System.out.println("\n========== SDK API返回结果 ==========");
            Gson gson = new GsonBuilder().setPrettyPrinting().create();
            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());
                
                // 详细输出每个资产的信息
                response.getList().forEach(asset -> {
                    System.out.println("\n【短剧资产】");
                    System.out.println("  资产ID: " + asset.getMarketingAssetId());
                    System.out.println("  资产名称: " + asset.getMarketingAssetName());
                    System.out.println("  资产类型: " + asset.getMarketingAssetType());
                    System.out.println("  创建时间: " + asset.getCreatedTime());
                    // SDK中MarketingAssetStruct没有isDeleted字段
                    // System.out.println("  是否删除: " + asset.getIsDeleted());
                    
                    if (asset.getProperties() != null) {
                        System.out.println("  属性列表:");
                        asset.getProperties().forEach(prop -> {
                            System.out.println("    - " + prop.getPropertyCn() + 
                                             " [" + prop.getPropertyName() + "]");
                            System.out.println("      类型: " + prop.getPropertyClass());
                            System.out.println("      值: " + prop.getPropertyValue());
                        });
                    }
                });
            } else {
                System.out.println("⚠️  返回结果为空");
            }

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

    @Test
    @DisplayName("测试同步短剧资产到数据库")
    void testSyncMarketingAssets() {
        System.out.println("\n========================================");
        System.out.println("开始同步短剧资产到数据库");
        System.out.println("账户ID: " + TEST_ACCOUNT_ID);
        System.out.println("========================================\n");

        try {
            // 调用同步方法
            System.out.println("正在同步短剧资产...");
            int count = assetDramaService.syncMarketingAssets(
                TEST_JOB_NUMBER,
                ORG_ACCOUNT_ID,
                TEST_ACCOUNT_ID
            );

            // 输出结果
            System.out.println("\n========== 同步结果 ==========");
            System.out.println("✅ 同步完成");
            System.out.println("📊 影响行数: " + count);
            System.out.println("========================================\n");

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

