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

import static org.jeecg.modules.adweb.dmp.vo.report.SiteOverviewStatsVO.DailyStatsVO;

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

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
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.IGADailyReportService;
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.SiteOverviewStatsVO;
import org.jeecg.modules.adweb.dmp.vo.report.SourceMediumStatsVO;
import org.jeecg.modules.adweb.enquiry.service.IAdwebEnquiryService;
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;

/**
 * 数据分析 - GA网站流量，询盘数据等
 *
 * @author wfansh
 */
@Tag(name = "DMP数据分析 - 网站流量，询盘数据等")
@RestController
@RequestMapping("/dmp-data")
@Slf4j
public class DMPDataController {

    @Autowired private IGADailyReportService gaDailyReportService;
    @Autowired private IGASourceMediumReportService gaSourceMediumReportService;
    @Autowired private IGACountryReportService gaCountryReportService;
    @Autowired private IGAPagePathReportService gaPagePathReportService;

    @Autowired private IAdwebEnquiryService adwebEnquiryService;

    /** 网站流量整体分析统计 */
    @GetMapping("/site-overview/stats")
    public Result<SiteOverviewStatsVO> getSiteOverviewStats(
            String siteCode,
            String dateType,
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date start,
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date end) {
        // 1. 计算时间区间
        if (StringUtils.isNotBlank(dateType)) {
            Pair<Date, Date> dateRange = DateUtil.getDateRangeByType(dateType);
            start = dateRange.getLeft();
            end = dateRange.getRight();
        }

        // 2. 查询GA Daily Report和Enquiries
        List<DailyStatsVO> dailyStatsVOs =
                gaDailyReportService.getDailyStatsWithinPeriod(siteCode, start, end);

        // 3. 生成SiteOverviewStatsVO并返回
        return Result.ok(SiteOverviewStatsVO.fromDailyStats(dailyStatsVOs));
    }

    @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)) {
            Pair<Date, Date> dateRange = DateUtil.getDateRangeByType(dateType);
            start = dateRange.getLeft();
            end = dateRange.getRight();
        }

        // 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)) {
            Pair<Date, Date> dateRange = DateUtil.getDateRangeByType(dateType);
            start = dateRange.getLeft();
            end = dateRange.getRight();
        }

        // 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)) {
            Pair<Date, Date> dateRange = DateUtil.getDateRangeByType(dateType);
            start = dateRange.getLeft();
            end = dateRange.getRight();
        }

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