package org.jeecg.modules.adweb.dmp.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.lang3.tuple.Pair;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.modules.adweb.dmp.dto.google.analytics.GAAccountDTO;
import org.jeecg.modules.adweb.dmp.entity.GoogleGTM;
import org.jeecg.modules.adweb.dmp.service.IGoogleGTMService;
import org.jeecg.modules.adweb.dmp.service.google.GTMAdminService;
import org.jeecg.modules.adweb.site.entity.AdwebSite;
import org.jeecg.modules.adweb.site.service.IAdwebSiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * GTM容器和标签管理
 *
 * @author wfansh
 */
@Tag(name = "Google Tag Manager")
@RestController
@RequestMapping("/gtm")
@Slf4j
public class GTMController {
    @Autowired private IAdwebSiteService adwebSiteService;
    @Autowired private IGoogleGTMService googleGTMService;
    @Autowired private GTMAdminService gtmAdminService;

    /** 获取站点绑定的GTM snippets */
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ResponseBody
    public Result<AdwebSite> getGTMSnippets(String siteCode) {
        GoogleGTM googleGTM =
                googleGTMService.getOne(
                        new LambdaQueryWrapper<GoogleGTM>().eq(GoogleGTM::getSiteCode, siteCode));
        if (Objects.isNull(googleGTM)) {
            log.info("站点 {} 对应的GoogleGTM不存在", siteCode);
            return Result.error(CommonConstant.SC_INTERNAL_NOT_FOUND_404, "GoogleGTM不存在");
        }

        Pair<String, String> snippets = gtmAdminService.getSnippets(googleGTM.getGtmPublicId());

        AdwebSite adwebSite = new AdwebSite();
        adwebSite.setGtmHead(snippets.getLeft());
        adwebSite.setGtmBody(snippets.getRight());

        return Result.ok(adwebSite);
    }

    /** 为站点添加GTM snippets */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Result<AdwebSite> addGTMSnippets(String siteCode, String gaAccountId) {
        AdwebSite adwebSite = adwebSiteService.getSiteByCode(siteCode);

        // 如果GoogleGTM表中已存在，不再创建，返回现有记录
        GoogleGTM googleGTM =
                gtmAdminService.createContainer(
                        adwebSite.getCode(),
                        adwebSite.getDomain(),
                        adwebSite.getName(),
                        gaAccountId);

        Pair<String, String> snippets = gtmAdminService.getSnippets(googleGTM.getGtmPublicId());
        adwebSite.setGtmHead(snippets.getLeft());
        adwebSite.setGtmBody(snippets.getRight());

        return Result.ok(adwebSite);
    }

    /** 获取所有可用的GA帐号 */
    @RequestMapping(value = "/listGAAccounts", method = RequestMethod.GET)
    @ResponseBody
    public Result<List<GAAccountDTO>> listGAAccounts() {
        List<GAAccountDTO> gaAccounts = gtmAdminService.listGAAccounts();

        return Result.ok(gaAccounts);
    }
}
