package com.tashow.cloud.user.controller.app.user;

import com.tashow.cloud.common.pojo.CommonResult;
import com.tashow.cloud.user.controller.app.user.vo.*;
import com.tashow.cloud.user.convert.user.MemberUserConvert;
import com.tashow.cloud.user.dal.dataobject.user.UserLoginDO;
import com.tashow.cloud.user.service.user.UserLoginService;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.tashow.cloud.common.pojo.CommonResult.success;
import static com.tashow.cloud.security.security.core.util.SecurityFrameworkUtils.getLoginUserId;


// 用户 APP - 用户个人中心
@RestController
@RequestMapping("/user/member")
@Validated
@Slf4j
public class AppUserMemberController {

    @Resource
    private UserLoginService userService;

    @GetMapping("/get")
    // 获得基本信息
    public CommonResult<AppMemberUserInfoRespVO> getUserInfo() {
        UserLoginDO user = userService.getUser(getLoginUserId());
        return success(MemberUserConvert.INSTANCE.convert(user));
    }

    @PutMapping("/update")
    // 修改基本信息
    public CommonResult<Boolean> updateUser(@RequestBody @Valid AppMemberUserUpdateReqVO reqVO) {
        userService.updateUser(getLoginUserId(), reqVO);
        return success(true);
    }

    @PutMapping("/update-mobile")
    // 修改用户手机
    public CommonResult<Boolean> updateUserMobile(@RequestBody @Valid AppMemberUserUpdateMobileReqVO reqVO) {
        userService.updateUserMobile(getLoginUserId(), reqVO);
        return success(true);
    }

    @PutMapping("/update-mobile-by-weixin")
    // 基于微信小程序的授权码，修改用户手机
    public CommonResult<Boolean> updateUserMobileByWeixin(@RequestBody @Valid AppMemberUserUpdateMobileByWeixinReqVO reqVO) {
        userService.updateUserMobileByWeixin(getLoginUserId(), reqVO);
        return success(true);
    }

    @PutMapping("/update-password")
    // 修改用户密码
    // 用户修改密码时使用
    public CommonResult<Boolean> updateUserPassword(@RequestBody @Valid AppMemberUserUpdatePasswordReqVO reqVO) {
        userService.updateUserPassword(getLoginUserId(), reqVO);
        return success(true);
    }

    @PutMapping("/reset-password")
    // 重置密码
    // 用户忘记密码时使用
    @PermitAll
    public CommonResult<Boolean> resetUserPassword(@RequestBody @Valid AppMemberUserResetPasswordReqVO reqVO) {
        userService.resetUserPassword(reqVO);
        return success(true);
    }

}