package com.moka.gdtauto.service;

import com.moka.gdtauto.client.TencentAdsApiClientFactory;
import com.tencent.ads.exception.TencentAdsResponseException;
import com.tencent.ads.exception.TencentAdsSDKException;
import com.tencent.ads.model.v3.FilteringStruct;
import com.tencent.ads.model.v3.AdgroupsGetResponseData;
import com.tencent.ads.v3.TencentAds;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 腾讯广告账户API服务
 * 
 * @author moka
 * @since 2026-02-03
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class TencentAdsAccountService {

    private final TencentAdsApiClientFactory clientFactory;

    /**
     * 获取TencentAds实例
     * 可以在此基础上调用SDK提供的API
     * 
     * @param accountId 账户ID
     * @return TencentAds实例
     * @throws Exception API异常
     */
    public TencentAds getTencentAdsInstance(String jobNumber, Long accountId) throws Exception {
        return clientFactory.getTencentAdsWithoutUserToken(jobNumber, accountId);
    }

    /**
     * 获取广告组列表
     * 
     * @param accountId 广告主账户ID
     * @param page 页码，最小值1，最大值100，默认1
     * @param pageSize 每页数量，最小值1，最大值100，默认10
     * @return 广告组列表响应数据
     * @throws Exception API异常
     */
    public AdgroupsGetResponseData getAdgroups(String jobNumber, Long orgAccountId, Long accountId, Long page, Long pageSize) 
            throws TencentAdsResponseException, TencentAdsSDKException, Exception {
        log.info("开始获取广告组列表,accountId={}, page={}, pageSize={}", orgAccountId, page, pageSize);
            
        // 获取TencentAds实例
        TencentAds tencentAds = clientFactory.getTencentAdsWithoutUserToken(jobNumber,orgAccountId);
            
        // 调用SDK的adgroups().adgroupsGet()方法
        List<FilteringStruct> filtering = null; // 不传过滤条件,获取全部
        Boolean isDeleted = false; // 不包含已删除的广告组
        List<String> fields = buildFields(); // 返回所有字段
        String paginationMode = null; // 使用默认分页模式
        String cursor = null; // 游标分页用,普通分页不需要
            
        AdgroupsGetResponseData response = tencentAds.adgroups()
            .adgroupsGet(accountId, filtering, page, pageSize, isDeleted, fields, paginationMode, cursor);
            
        log.info("获取广告组列表成功,返回{}条数据", 
            response != null && response.getList() != null ? response.getList().size() : 0);
            
        return response;
    }



    private List<String> buildFields() {
        return List.of(
            "adgroup_id",
            "adgroup_name",
            "targeting",
            "marketing_goal",
            "marketing_carrier_type",
            "marketing_target_type",
            "marketing_asset_id",
            "begin_date",
            "end_date",
            "optimization_goal",
            "time_series",
            "site_set",
            "bid_amount",
            "daily_budget",
            "conversion_id",
            "bid_mode",
            "auto_derived_creative_enabled",
            "bid_scene"
        );
    }

    /**
     * 获取广告组列表（带过滤条件和字段）
     * 
     * @param accountId 广告主账户ID
     * @param page 页码
     * @param pageSize 每页数量
     * @param filtering 过滤条件
     * @param fields 返回字段
     * @return 广告组列表响应数据
     * @throws Exception API异常
     */
    public AdgroupsGetResponseData getAdgroupsWithFiltering(
        String jobNumber, Long orgAccountId, 
            Long accountId, Long page, Long pageSize, 
            List<FilteringStruct> filtering, List<String> fields) 
            throws TencentAdsResponseException, TencentAdsSDKException, Exception {
        log.info("开始获取广告组列表，accountId={}, page={}, pageSize={}, filtering={}", 
            accountId, page, pageSize, filtering);
        
        // 获取TencentAds实例
        TencentAds tencentAds = clientFactory.getTencentAdsWithoutUserToken(jobNumber, orgAccountId);
        
        // 调用SDK的adgroups().adgroupsGet()方法
        Boolean isDeleted = false;
        String paginationMode = null;
        String cursor = null;
        
        AdgroupsGetResponseData response = tencentAds.adgroups()
            .adgroupsGet(accountId, filtering, page, pageSize, isDeleted, fields, paginationMode, cursor);
        
        log.info("获取广告组列表成功，返回{}条数据", 
            response != null && response.getList() != null ? response.getList().size() : 0);
        
        return response;
    }
}
