package com.yinlihupo.enlish.service.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.yinlihupo.enlish.service.domain.dataobject.UserDO;
import com.yinlihupo.enlish.service.model.vo.user.FindUserInfoRspVO;
import com.yinlihupo.enlish.service.model.vo.user.UpdateUserInfoReqVO;
import com.yinlihupo.enlish.service.service.UserService;
import com.yinlihupo.framework.biz.operationlog.aspect.ApiOperationLog;
import com.yinlihupo.framework.common.response.Response;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
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;

@RestController
@RequestMapping("/user/")
@Slf4j
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("info")
    public Response<FindUserInfoRspVO> info() {

        UserDO user = userService.findUser();
        FindUserInfoRspVO findUserInfoRspVO = FindUserInfoRspVO.builder()
                .id(user.getId())
                .name(user.getName())
                .build();
        return Response.success(findUserInfoRspVO);

    }

    @PostMapping("update-user-info")
    @ApiOperationLog(description = "修改用户信息")
    public Response<String> updatePassword(@RequestBody UpdateUserInfoReqVO updateUserInfoReqVO) {
        try {
            String code = updateUserInfoReqVO.getCode();
            String newPassword = updateUserInfoReqVO.getNewPassword();
            String phone = updateUserInfoReqVO.getPhone();
            String name = updateUserInfoReqVO.getName();
            userService.updateUserInfo(newPassword, code, phone, name);

            StpUtil.logout();

            return Response.success();
        } catch (Exception e) {
            log.error("修改密码失败 {}", e.getMessage());
            return Response.fail(e.getMessage());
        }
    }
}
