package shop.alien.lawyer.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiSort;
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.LawyerUser;
import shop.alien.entity.store.dto.LawyerUserDto;
import shop.alien.entity.store.vo.LawyerUserVo;
import shop.alien.lawyer.service.LawyerUserLogInService;
import shop.alien.lawyer.service.LawyerUserService;

/**
 * 门店用户 前端控制器
 *
 * @author ssk
 * @since 2024-12-11
 */
@Slf4j
@Api(tags = {"律师用户"})
@ApiSort(8)
@CrossOrigin
@RestController
@RequestMapping("/lawyer/user")
@RequiredArgsConstructor
public class LawyerUserLogInController {

    private final LawyerUserLogInService lawyerUserService;
    private final LawyerUserService lawyerUserBizService;

    @ApiOperation("律师用户注册")
    @ApiOperationSupport(order = 1)
    @PostMapping("/register")
    public R<LawyerUserVo> register(@RequestBody LawyerUserDto lawyerUserDto) {
        log.info("LawyerUserLogInController.login?lawyerUserDto={}", lawyerUserDto);
        return lawyerUserService.register(lawyerUserDto);
    }

    @ApiOperation(value = "完善律师个人信息", notes = "登录后补全资料，返回更新后的用户信息，不重新签发 token")
    @ApiOperationSupport(order = 8)
    @PostMapping("/completeProfile")
    public R<LawyerUserVo> completeProfile(@RequestBody LawyerUserDto lawyerUserDto) {
        log.info("LawyerUserLogInController.completeProfile?lawyerUserDto={}", lawyerUserDto);
        return lawyerUserService.completeProfile(lawyerUserDto);
    }

    @ApiOperation("律师用户验证码校验")
    @ApiOperationSupport(order = 2)
    @PostMapping("/checkMsgCode")
    public R checkMsgCode(@RequestBody LawyerUserDto lawyerUserDto) {
        log.info("LawyerUserLogInController.checkMsgCode?lawyerUserDto={}", lawyerUserDto);
        return lawyerUserService.checkMsgCode(lawyerUserDto);
    }

    @ApiOperation("律师用户登录")
    @ApiOperationSupport(order = 3)
    @PostMapping("/login")
    public R<LawyerUserVo> login(@RequestBody LawyerUserDto lawyerUserDto) {
        log.info("LawyerUserLogInController.login?lawyerUserDto={}", lawyerUserDto);
        return lawyerUserService.login(lawyerUserDto);
    }

    @ApiOperation("律师登录前校验")
    @ApiOperationSupport(order = 7)
    @GetMapping("/checkLogin")
    public R<LawyerUser> checkLogin(String phone) {
        log.info("LawyerUserLogInController.checkLogin?phone={}", phone);
        return lawyerUserBizService.checkLogin(phone);
    }

    @ApiOperation("律师用户登出")
    @ApiOperationSupport(order = 4)
    @PostMapping("/logout")
    public R logout(@RequestBody LawyerUserDto lawyerUserDto) {
        log.info("LawyerUserLogInController.logout?lawyerUserDto={}", lawyerUserDto);
        return lawyerUserService.logout(lawyerUserDto);
    }


    @ApiOperation("律师用户修改密码")
    @ApiOperationSupport(order = 5)
    @PostMapping("/updatePassWord")
    public R updatePassWord(@RequestBody LawyerUserDto lawyerUserDto) {
        log.info("LawyerUserLogInController.updatePassWord?lawyerUserDto={}", lawyerUserDto);
        try {
            LawyerUser lawyerUser = lawyerUserBizService.getOne(new LambdaQueryWrapper<LawyerUser>()
                    .eq(LawyerUser::getPhone, lawyerUserDto.getPhone()));
            if (null == lawyerUser) {
                return R.fail("当前账号不存在，请先去注册账号！");
            }
            if (lawyerUser.getStatus() == 0) {
                return R.fail("账号被禁用");
            }
            return lawyerUserService.updatePassWord(lawyerUserDto);
        }catch (Exception e){
            return R.fail("修改密码失败");
        }
    }

    @ApiOperation("判断用户是否已经注册")
    @ApiOperationSupport(order = 6)
    @PostMapping("/checkUserRegister")
    public R checkUserRegister(@RequestBody LawyerUserDto lawyerUserDto) {
        log.info("LawyerUserLogInController.updatePassWord?lawyerUserDto={}", lawyerUserDto);
        try {
            Boolean res = lawyerUserService.checkRegister(lawyerUserDto);
            return R.data(res);
        }catch (Exception e){
            return R.fail("判断失败");
        }
    }

}
