package org.jeecg.modules.adweb.dmp.service.google;

import org.apache.commons.lang3.tuple.Pair;
import org.jeecg.common.util.FastJsonUtil;
import org.jeecg.modules.adweb.dmp.dto.google.analytics.report.GAReportRequestDTO;
import org.jeecg.modules.adweb.dmp.dto.google.analytics.report.OrderByType;
import org.jeecg.modules.adweb.dmp.dto.google.analytics.report.ReportConstant;
import org.jeecg.modules.adweb.dmp.dto.google.analytics.report.ReportType;
import org.jeecg.modules.adweb.dmp.dto.google.analytics.report.data.CustomReportData;
import org.jeecg.modules.adweb.dmp.entity.GoogleGTM;
import org.junit.jupiter.api.Disabled;
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.List;

/**
 * @author wfansh
 */
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("dev")
public class GoogleServiceTest {

    @Autowired private GAReportService gaReportService;

    @Autowired private GTMAdminService gtmAdminService;

    @Test
    public void testGAReport() {
        GAReportRequestDTO gaReportRequest = new GAReportRequestDTO();
        gaReportRequest.setPropertyResourceName("properties/457183952");
        gaReportRequest.setReportType(ReportType.ADWEB_CUSTOM_REPORT);
        gaReportRequest.setStartDate("2023-10-01");
        gaReportRequest.setEndDate("2024-10-01");
        gaReportRequest.setMetrics(List.of(ReportConstant.METRIC_TOTAL_USERS));
        gaReportRequest.setDimensions(
                List.of(ReportConstant.DIMENSION_DATE, ReportConstant.DIMENSION_COUNTRY));
        gaReportRequest.setOrderByType(OrderByType.DIMENSIONS);
        gaReportRequest.setOrderBy(ReportConstant.DIMENSION_DATE);

        List<CustomReportData> countryReport =
                gaReportService.runGAReport(gaReportRequest, CustomReportData.class);

        System.out.println(FastJsonUtil.toJSONString(countryReport));
    }

    @Test
    @Disabled("GTM + GA帐户资源限制")
    public void testCreateAndDeleteGTMContainer() {
        GoogleGTM googleGTM =
                gtmAdminService.createContainer(
                        "230206anit40",
                        "https://www.sourcingstone.com",
                        "泉州索兴石业有限公司#" + this.getClass().getSimpleName(),
                        "191734056");
        System.out.println(FastJsonUtil.toJSONString(googleGTM));

        gtmAdminService.deleteContainer(googleGTM.getSiteCode());
    }

    @Test
    public void testGetGTMSnippets() {
        Pair<String, String> snippets = gtmAdminService.getSnippets("GTM-T87NNQ4F");
        System.out.println(snippets.getLeft());
        System.out.println(snippets.getRight());
    }
}
