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

/**
 * 用户端登录密码（设置密码在 store 服务，登录/忘记密码仍在 gateway）
 */
@Api(tags = {"一期-用户-密码登录"})
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/user/password")
@RequiredArgsConstructor
public class LifeUserPasswordController {

    private final LifeUserPasswordService lifeUserPasswordService;

    @ApiOperation("设置当前账号登录密码（需登录）")
    @ApiOperationSupport(order = 1)
    @PostMapping("/set")
    public R<String> setPassword(@ApiIgnore @TokenInfo UserLoginInfo userLoginInfo,
                                 @RequestBody LifeUserPasswordDto dto) {
        log.info("LifeUserPasswordController.setPassword, userId={}",
                userLoginInfo == null ? null : userLoginInfo.getUserId());
        return lifeUserPasswordService.setPassword(userLoginInfo, dto);
    }

    @ApiOperation("根据用户id查询是否已设置登录密码")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/hasLoginPassword")
    public R<Boolean> hasLoginPassword(@RequestParam("userId") Integer userId) {
        log.info("LifeUserPasswordController.hasLoginPassword?userId={}", userId);
        return lifeUserPasswordService.hasLoginPassword(userId);
    }
}
