package shop.alien.gateway.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import shop.alien.entity.result.R;
import shop.alien.entity.store.vo.LifeUserThirdBindInfoVo;
import shop.alien.gateway.dto.LifeUserThirdBindWechatRequest;
import shop.alien.gateway.service.LifeUserThirdBindService;
import shop.alien.util.common.JwtUtil;

import javax.validation.Valid;

/**
 * 用户第三方账号绑定（当前仅开放微信）
 */
@Api(tags = {"一期-用户-第三方绑定"})
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/user/third-bind")
@RequiredArgsConstructor
public class LifeUserThirdBindController {

    private final LifeUserThirdBindService lifeUserThirdBindService;

    @ApiOperation("查询当前用户微信绑定（昵称、openid、渠道手机号等）")
    @ApiOperationSupport(order = 1)
    @GetMapping("/info")
    public R<LifeUserThirdBindInfoVo> thirdBindInfo() {
        try {
            JSONObject sub = JwtUtil.getCurrentUserInfo();
            if (sub == null) {
                return R.fail("请先登录");
            }
            Integer userId = lifeUserThirdBindService.resolveLifeUserId(sub);
            if (userId == null) {
                return R.fail("仅支持用户端查询");
            }
            return R.data(lifeUserThirdBindService.getThirdBindInfo(userId));
        } catch (Exception e) {
            log.warn("thirdBindInfo token parse failed", e);
            return R.fail("登录已失效");
        }
    }

    @ApiOperation("绑定微信（App 移动应用授权 code）")
    @ApiOperationSupport(order = 2)
    @PostMapping("/wechat")
    public R<LifeUserThirdBindInfoVo> thirdBindWechat(@Valid @RequestBody LifeUserThirdBindWechatRequest body) {
        try {
            JSONObject sub = JwtUtil.getCurrentUserInfo();
            if (sub == null) {
                return R.fail("请先登录");
            }
            Integer userId = lifeUserThirdBindService.resolveLifeUserId(sub);
            if (userId == null) {
                return R.fail("仅支持用户端操作");
            }
            return lifeUserThirdBindService.bindWechat(
                    userId, body.getCode(), body.getNickName(), body.getBindPhone());
        } catch (Exception e) {
            log.warn("thirdBindWechat failed", e);
            return R.fail("登录已失效");
        }
    }

    @ApiOperation("解绑微信")
    @ApiOperationSupport(order = 3)
    @PostMapping("/wechat/unbind")
    public R<LifeUserThirdBindInfoVo> thirdBindWechatUnbind() {
        try {
            JSONObject sub = JwtUtil.getCurrentUserInfo();
            if (sub == null) {
                return R.fail("请先登录");
            }
            Integer userId = lifeUserThirdBindService.resolveLifeUserId(sub);
            if (userId == null) {
                return R.fail("仅支持用户端操作");
            }
            return lifeUserThirdBindService.unbindWechat(userId);
        } catch (Exception e) {
            log.warn("thirdBindWechatUnbind failed", e);
            return R.fail("登录已失效");
        }
    }
}
