package com.jojubanking.boot.module.system.controller.app.auth;

import cn.hutool.core.util.StrUtil;
import com.jojubanking.boot.framework.common.pojo.CommonResult;
import com.jojubanking.boot.framework.operatelog.core.annotations.OperateLog;
import com.jojubanking.boot.framework.security.config.SecurityProperties;
import com.jojubanking.boot.framework.security.core.util.SecurityFrameworkUtils;
import com.jojubanking.boot.module.system.controller.app.auth.vo.AppAuthLoginReqVO;
import com.jojubanking.boot.module.system.controller.app.auth.vo.AppAuthLoginRespVO;
import com.jojubanking.boot.module.system.controller.app.auth.vo.AppAuthSocialLoginReqVO;
import com.jojubanking.boot.module.system.service.authgz.GzAuthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import static com.jojubanking.boot.framework.common.pojo.CommonResult.success;

@Api(tags = "用户 APP - 认证")
@RestController
@RequestMapping("/member/auth")
@Validated
@Slf4j
public class AppAuthController {

    @Resource
    private GzAuthService authService;

    @Resource
    private SecurityProperties securityProperties;

//    @PostMapping("/logout")
//    @PermitAll
//    @ApiOperation("登出系统")
//    public CommonResult<Boolean> logout(HttpServletRequest request) {
//        String token = SecurityFrameworkUtils.obtainAuthorization(request, securityProperties.getTokenHeader());
//        if (StrUtil.isNotBlank(token)) {
//            authService.logout(token);
//        }
//        return success(true);
//    }

    @PostMapping("/refresh-token")
    @ApiOperation("刷新令牌")
    @ApiImplicitParam(name = "refreshToken", value = "刷新令牌", required = true, dataTypeClass = String.class)
    @OperateLog(enable = false) // 避免 Post 请求被记录操作日志
    public CommonResult<AppAuthLoginRespVO> refreshToken(@RequestParam("refreshToken") String refreshToken) {
        return success(authService.refreshToken(refreshToken));
    }

    // ========== 社交登录相关 ==========

    @PostMapping("/social-login")
    @ApiOperation(value = "社交快捷登录，使用 code 授权码", notes = "适合未登录的用户，但是社交账号已绑定用户")
    public CommonResult<AppAuthLoginRespVO> socialLogin(@RequestBody @Valid AppAuthSocialLoginReqVO reqVO) {
        return success(authService.socialLogin(reqVO));
    }


}
