package org.jeecg.modules.adweb.marketing.googleads.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

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.marketing.googleads.entity.GoogleAds;
import org.jeecg.modules.adweb.marketing.googleads.service.GoogleAdsReportService;
import org.jeecg.modules.adweb.marketing.googleads.service.IGoogleAdsService;
import org.jeecg.modules.adweb.marketing.googleads.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * Google Ads - 帐号，广告，报表数据等
 *
 * @author wfansh
 */
@Tag(name = "Google Ads - 帐号，广告，报表数据等")
@RestController
@RequestMapping("/marketing/googleads")
@Slf4j
public class GoogleAdsController {

    @Autowired private IGoogleAdsService googleAdsService;
    @Autowired private GoogleAdsReportService googleAdsReportService;

    /** 获取站点绑定的Google Ads account */
    @GetMapping("/account/get")
    @ResponseBody
    public Result<GoogleAds> getAdsAccount(@RequestParam String siteCode) {
        GoogleAds googleAds =
                googleAdsService.getOne(
                        new LambdaQueryWrapper<GoogleAds>().eq(GoogleAds::getSiteCode, siteCode),
                        false);

        return Result.ok(googleAds);
    }

    /** 为站点绑定Google Ads account */
    @PostMapping("/account/add")
    @ResponseBody
    public Result<String> addAdsAccount(
            @RequestParam String siteCode,
            @RequestParam String customerId,
            @RequestParam(required = false) String refreshToken) {
        GoogleAds googleAds =
                googleAdsService.getOne(
                        new LambdaQueryWrapper<GoogleAds>().eq(GoogleAds::getSiteCode, siteCode),
                        false);
        googleAds = Optional.ofNullable(googleAds).orElse(new GoogleAds());

        googleAds.setSiteCode(siteCode);
        googleAds.setCustomerId(customerId);
        googleAds.setRefreshToken(refreshToken);
        googleAdsService.saveOrUpdate(googleAds);

        return Result.ok("添加成功");
    }

    /** Google Ads - 帐号信息 */
    @GetMapping("/customer/stats")
    public Result<CustomerStatsVO> getCustomerStats(String siteCode) {
        return Result.ok(googleAdsReportService.getCustomerStats(siteCode));
    }

    /** Google Ads - 每日数据报表 */
    @GetMapping("/daily/stats")
    public Result<List<DailyStatsVO>> getDailyStats(
            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(googleAdsReportService.getDailyStats(siteCode, start, end));
    }

    /** Google Ads - 广告系列数据报表 */
    @GetMapping("/campaign/stats")
    public Result<List<CampaignStatsVO>> getCampaignStats(
            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(googleAdsReportService.getCampaignStats(siteCode, start, end));
    }

    /** Google Ads - Top关键字报表 */
    @GetMapping("/keyword/stats")
    public Result<List<KeywordStatsVO>> getKeywordStats(
            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(googleAdsReportService.getKeywordStats(siteCode, start, end));
    }

    /** Google Ads - Top展示位报表 */
    @GetMapping("/placement/stats")
    public Result<List<PlacementStatsVO>> getPlacementStats(
            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(googleAdsReportService.getPlacementStats(siteCode, start, end));
    }

    /** Google Ads - Top国家报表 */
    @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(googleAdsReportService.getCountryStats(siteCode, start, end));
    }
}
