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.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.CommonPushTaskUser;
import shop.alien.store.dto.CommonPushTaskUserCallbackDto;
import shop.alien.store.service.CommonPushTaskUserService;

@Api(tags = {"推送任务用户管理"})
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/commonPushTaskUser")
@RequiredArgsConstructor
public class CommonPushTaskUserController {

    private final CommonPushTaskUserService commonPushTaskUserService;

    @ApiOperation("新增推送任务用户关联")
    @ApiOperationSupport(order = 1)
    @PostMapping("/add")
    public R<String> add(@RequestBody CommonPushTaskUser taskUser) {
        return commonPushTaskUserService.add(taskUser);
    }

    @ApiOperation("根据主键删除（逻辑删除）")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "Long", paramType = "query", required = true)
    })
    @GetMapping("/deleteById")
    public R<String> deleteById(@RequestParam Long id) {
        return commonPushTaskUserService.deleteById(id);
    }

    @ApiOperation("更新推送任务用户关联")
    @ApiOperationSupport(order = 3)
    @PostMapping("/update")
    public R<String> update(@RequestBody CommonPushTaskUser taskUser) {
        return commonPushTaskUserService.update(taskUser);
    }

    @ApiOperation("根据主键查询")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "Long", paramType = "query", required = true)
    })
    @GetMapping("/getById")
    public R<CommonPushTaskUser> getById(@RequestParam Long id) {
        return commonPushTaskUserService.getInfoById(id);
    }

    @ApiOperation("分页列表")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "pushTaskId", value = "推送任务ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "用户ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "deviceId", value = "设备ID", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public R<IPage<CommonPushTaskUser>> list(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize,
            @RequestParam(required = false) Long pushTaskId,
            @RequestParam(required = false) Long userId,
            @RequestParam(required = false) String deviceId) {
        return commonPushTaskUserService.list(pageNum, pageSize, pushTaskId, userId, deviceId);
    }

    @ApiOperation("推送状态回调（各渠道统一入口，支持 JSON 与 OPPO 表单回执）")
    @ApiOperationSupport(order = 6)
    @PostMapping("/callback")
    public R<String> callback(@RequestBody(required = false) CommonPushTaskUserCallbackDto callbackDto,
                              @RequestParam(required = false) String callBackParameter,
                              @RequestParam(required = false) String param,
                              @RequestParam(required = false) String status,
                              @RequestParam(required = false) String registrationIds,
                              @RequestParam(required = false) String registration_id,
                              @RequestParam(required = false) String eventType) {
        if (callbackDto != null && StringUtils.isNotBlank(callbackDto.getParam())
                && StringUtils.isBlank(callBackParameter) && StringUtils.isBlank(param)) {
            param = callbackDto.getParam();
        }
        String vendorParam = StringUtils.defaultIfBlank(callBackParameter, param);
        if (StringUtils.isBlank(vendorParam) && callbackDto != null) {
            vendorParam = callbackDto.getParam();
        }
        String regIds = StringUtils.defaultIfBlank(registrationIds, registration_id);
        if (StringUtils.isBlank(regIds) && callbackDto != null) {
            regIds = callbackDto.getRegistrationIds();
        }
        if (StringUtils.isNotBlank(vendorParam)) {
            return commonPushTaskUserService.callbackFromVendor(vendorParam, status, regIds, eventType);
        }
        if (callbackDto != null && (callbackDto.getId() != null || callbackDto.getPushTaskId() != null
                || StringUtils.isNotBlank(callbackDto.getTaskNo()) || StringUtils.isNotBlank(callbackDto.getDeviceId()))) {
            return commonPushTaskUserService.callback(callbackDto);
        }
        return commonPushTaskUserService.callback(callbackDto);
    }
}
