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.BrandAddResponseData;
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.io.File;

/**
 * 腾讯广告品牌形象创建测试
 * https://developers.e.qq.com/v3.0/docs/api/brand/add
 * @author moka
 * @since 2026-02-26
 * mvn test -Dtest=TencentBrandAddTest#testAddBrand -Dspring.profiles.active=test
 */
@SpringBootTest(classes = GdtAutoApplication.class)
@ActiveProfiles("test")
class TencentBrandAddTest {

    @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 testAddBrand() {
        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);

            // 品牌形象图片文件路径（请替换为实际图片路径）
            // 注意：图片尺寸必须为 512x512，大小不超过 400KB，格式：*.jpg、*.jpeg、*.png
            String imagePath = "/Users/pudongliang/moka_workspace/gdt-auto/brand.jpg";
            File imageFile = new File(imagePath);
            
            if (!imageFile.exists()) {
                System.err.println("❌ 品牌形象图片文件不存在: " + imagePath);
                System.err.println("请修改 imagePath 为实际的图片文件路径");
                System.err.println("⚠️  注意：图片尺寸必须为 512x512，大小不超过 400KB");
                return;
            }

            String brandName = "情感美文";
            
            // 输出请求参数
            System.out.println("\n========== 请求参数 ==========");
            System.out.println("账户ID: " + TEST_ACCOUNT_ID);
            System.out.println("品牌名称: " + brandName);
            System.out.println("图片路径: " + imagePath);
            System.out.println("========================================\n");

            // 调用创建品牌形象接口
            System.out.println("正在调用腾讯广告创建品牌形象API...");
            BrandAddResponseData response = tencentAds.brand()
                .brandAdd(
                    TEST_ACCOUNT_ID,      // accountId
                    brandName,            // name
                    imageFile             // brandImageFile
                );

            // 输出响应结果
            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("✅ 成功创建品牌形象");
                System.out.println("📊 账户ID: " + response.getAccountId());
                System.out.println("📊 品牌名称: " + response.getName());
                System.out.println("📊 图片ID: " + response.getImageId());
                System.out.println("📊 图片宽度: " + response.getWidth() + "px");
                System.out.println("📊 图片高度: " + response.getHeight() + "px");
                System.out.println("📊 图片URL: " + response.getImageUrl());
                System.out.println("📊 创建时间: " + response.getCreatedTime());
            } 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("测试创建品牌形象-指定组织ID")
    void testAddBrandWithOrgId() {
        System.out.println("\n========================================");
        System.out.println("开始创建品牌形象(指定组织ID)");
        System.out.println("账户ID: " + TEST_ACCOUNT_ID);
        System.out.println("组织ID: " + ORG_ACCOUNT_ID);
        System.out.println("========================================\n");

        try {
            // 获取TencentAds实例
            TencentAds tencentAds = clientFactory.getTencentAds(TEST_JOB_NUMBER, ORG_ACCOUNT_ID);
            tencentAds.setDebug(true);

            // 品牌形象图片文件路径（请替换为实际图片路径）
            // 注意：图片尺寸必须为 512x512，大小不超过 400KB，格式：*.jpg、*.jpeg、*.png
            String imagePath = "/Users/pudongliang/moka_workspace/gdt-auto/brand.jpg";
            File imageFile = new File(imagePath);
            
            if (!imageFile.exists()) {
                System.err.println("❌ 品牌形象图片文件不存在: " + imagePath);
                System.err.println("请修改 imagePath 为实际的图片文件路径");
                System.err.println("⚠️  注意：图片尺寸必须为 512x512，大小不超过 400KB");
                return;
            }

            String brandName = "情感美文";
            
            // 输出请求参数
            System.out.println("\n========== 请求参数 ==========");
            System.out.println("账户ID: " + TEST_ACCOUNT_ID);
            System.out.println("组织ID: " + ORG_ACCOUNT_ID);
            System.out.println("品牌名称: " + brandName);
            System.out.println("图片路径: " + imagePath);
            System.out.println("========================================\n");

            // 调用创建品牌形象接口
            System.out.println("正在调用腾讯广告创建品牌形象API...");
            BrandAddResponseData response = tencentAds.brand()
                .brandAdd(
                    TEST_ACCOUNT_ID,      // accountId
                    brandName,            // name
                    imageFile             // brandImageFile
                );

            // 输出响应结果
            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("✅ 成功创建品牌形象");
                System.out.println("📊 账户ID: " + response.getAccountId());
                System.out.println("📊 品牌名称: " + response.getName());
                System.out.println("📊 图片ID: " + response.getImageId());
                System.out.println("📊 图片宽度: " + response.getWidth() + "px");
                System.out.println("📊 图片高度: " + response.getHeight() + "px");
                System.out.println("📊 图片URL: " + response.getImageUrl());
                System.out.println("📊 创建时间: " + response.getCreatedTime());
            } else {
                System.out.println("⚠️  返回结果为空");
            }

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