package shop.alien.store.controller;

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.LifeUserPushDevice;
import shop.alien.entity.store.UserLoginInfo;
import shop.alien.store.dto.LifeUserPushBindDto;
import shop.alien.store.service.LifeUserPushDeviceService;
import shop.alien.util.common.TokenInfo;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * uni-push 等设备 cid 绑定；业务主键与类型取自登录态 JWT（userId + userType），不信任客户端传参区分身份。
 * <p>userType：user → life_user.id；store/merchant → store_user.id；lawyer → lawyer_user.id</p>
 */
@Api(tags = {"用户推送设备"})
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/lifeUserPushDevice")
@RequiredArgsConstructor
public class LifeUserPushDeviceController {

    private final LifeUserPushDeviceService lifeUserPushDeviceService;

    @ApiOperation("绑定当前用户与 push cid（登录后调用）")
    @ApiOperationSupport(order = 1)
    @PostMapping("/bind")
    public R<String> bind(@ApiIgnore @TokenInfo UserLoginInfo userLoginInfo,
                          @RequestBody LifeUserPushBindDto dto) {
        log.info("LifeUserPushDeviceController.bind, userId={}",
                userLoginInfo == null ? null : userLoginInfo.getUserId());
        return lifeUserPushDeviceService.bind(userLoginInfo, dto);
    }

    @ApiOperation("解绑指定 cid（仅删除当前用户名下记录）")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pushClientId", value = "cid", required = true, paramType = "query", dataType = "String")
    })
    @PostMapping("/unbind")
    public R<String> unbind(@ApiIgnore @TokenInfo UserLoginInfo userLoginInfo,
                            @RequestParam String pushClientId) {
        return lifeUserPushDeviceService.unbindByCid(userLoginInfo, pushClientId);
    }

    @ApiOperation("当前用户已绑定的推送设备列表")
    @ApiOperationSupport(order = 3)
    @GetMapping("/listMine")
    public R<List<LifeUserPushDevice>> listMine(@ApiIgnore @TokenInfo UserLoginInfo userLoginInfo) {
        return lifeUserPushDeviceService.listMine(userLoginInfo);
    }
}
