package org.jeecg.modules.adweb.dmp.controller;

import io.swagger.v3.oas.annotations.tags.Tag;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.adweb.common.util.DateUtil;
import org.jeecg.modules.adweb.dmp.service.IGACountryReportService;
import org.jeecg.modules.adweb.dmp.service.IGAPagePathReportService;
import org.jeecg.modules.adweb.dmp.service.IGASourceMediumReportService;
import org.jeecg.modules.adweb.dmp.vo.report.CountryStatsVO;
import org.jeecg.modules.adweb.dmp.vo.report.PagePathStatsVO;
import org.jeecg.modules.adweb.dmp.vo.report.SourceMediumStatsVO;
import org.jeecg.modules.adweb.site.service.IAdwebSiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * GA网站流量
 *
 * @author wfansh
 */
@Tag(name = "Google Analytics网站流量")
@RestController
@RequestMapping("/ga-data")
@Slf4j
public class GADataController {

    @Autowired private IAdwebSiteService adwebSiteService;
    @Autowired private IGASourceMediumReportService gaSourceMediumReportService;
    @Autowired private IGACountryReportService gaCountryReportService;
    @Autowired private IGAPagePathReportService gaPagePathReportService;

    //    /** 网站流量分析统计 */
    //    public Result<?> getSiteTraffic(
    //            String siteCode,
    //            String dateType,
    //            @DateTimeFormat(pattern = "yyyy-MM-dd") Date start,
    //            @DateTimeFormat(pattern = "yyyy-MM-dd") Date end) {
    //        // 1. 计算时间区间
    //        if (StringUtils.isNotBlank(dateType)) {
    //            Map<String, Date> map = DateUtil.getDateRangeByType(dateType);
    //            start = map.get("start");
    //            end = map.get("end");
    //        } else {
    //            if (end != null) {
    //                end = DateUtil.addDays(end, 1);
    //            }
    //        }
    //    }

    @GetMapping("/source-medium/stats")
    public Result<List<SourceMediumStatsVO>> getSourceMediumStats(
            String siteCode,
            String dateType,
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date start,
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date end) {
        // 1. 计算时间区间
        if (StringUtils.isNotBlank(dateType)) {
            Map<String, Date> dateRange = DateUtil.getDateRangeByType(dateType);
            start = dateRange.get("start");
            end = dateRange.get("end");
        }

        // 2. 查询并返回
        return Result.ok(gaSourceMediumReportService.getSourceMediumStats(siteCode, start, end));
    }

    @GetMapping("/country/stats")
    public Result<List<CountryStatsVO>> getCountryStats(
            String siteCode,
            String dateType,
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date start,
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date end) {
        // 1. 计算时间区间
        if (StringUtils.isNotBlank(dateType)) {
            Map<String, Date> dateRange = DateUtil.getDateRangeByType(dateType);
            start = dateRange.get("start");
            end = dateRange.get("end");
        }

        // 2. 查询并返回
        return Result.ok(gaCountryReportService.getCountryStats(siteCode, start, end));
    }

    @GetMapping("/page-path/stats")
    public Result<List<PagePathStatsVO>> getPagePathStats(
            String siteCode,
            String dateType,
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date start,
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date end,
            int limit) {
        // 1. 计算时间区间
        if (StringUtils.isNotBlank(dateType)) {
            Map<String, Date> dateRange = DateUtil.getDateRangeByType(dateType);
            start = dateRange.get("start");
            end = dateRange.get("end");
        }

        // 2. 查询并返回
        return Result.ok(
                gaPagePathReportService.getPagePathStats(
                        siteCode, start, end, limit >= 0 ? limit : 10));
    }
}
