package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.OceanEngineClickMonitorLog;
import shop.alien.store.dto.OceanEngineClickBindUserDto;
import shop.alien.store.service.OceanEngineClickMonitorLogService;

import javax.servlet.http.HttpServletRequest;

/**
 * 巨量引擎广告点击监测
 * 文档：https://open.oceanengine.com/labels/7/docs/1696710655781900
 */
@Api(tags = {"巨量引擎广告监测"})
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/oceanEngine")
@RequiredArgsConstructor
public class OceanEngineAdTrackController {

    private final OceanEngineClickMonitorLogService clickMonitorLogService;

    /**
     * 点击监测链接接收入口（巨量引擎以 GET 方式调用）
     * 配置示例：https://your-domain.com/oceanEngine/click/monitor?clickid=__CLICKID__&callback=__CALLBACK_PARAM__&...
     */
    @ApiOperation("点击监测链接接收（巨量引擎 GET 回调）")
    @ApiOperationSupport(order = 1)
    @GetMapping("/click/monitor")
    public R<Long> receiveClickMonitor(HttpServletRequest request) {
        return clickMonitorLogService.receiveClickMonitor(request);
    }

    @ApiOperation("点击记录绑定用户")
    @ApiOperationSupport(order = 2)
    @PostMapping("/click/bindUser")
    public R<String> bindUser(@RequestBody OceanEngineClickBindUserDto bindDto) {
        return clickMonitorLogService.bindUser(bindDto);
    }

    @ApiOperation("根据主键查询点击监测记录")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "Long", paramType = "query", required = true)
    })
    @GetMapping("/click/getById")
    public R<OceanEngineClickMonitorLog> getClickById(@RequestParam Long id) {
        return clickMonitorLogService.getInfoById(id);
    }

    @ApiOperation("点击监测记录分页列表")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "clickId", value = "点击ID", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "advertiserId", value = "广告主ID", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "用户ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "bindStatus", value = "绑定状态", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/click/list")
    public R<IPage<OceanEngineClickMonitorLog>> clickList(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize,
            @RequestParam(required = false) String clickId,
            @RequestParam(required = false) String advertiserId,
            @RequestParam(required = false) Integer userId,
            @RequestParam(required = false) Integer bindStatus) {
        return clickMonitorLogService.list(pageNum, pageSize, clickId, advertiserId, userId, bindStatus);
    }
}
