package shop.alien.lawyer.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
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.beans.BeanUtils;
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.config.BaseRedisService;
import shop.alien.lawyer.service.LawyerUserLogInService;
import shop.alien.mapper.LawyerUserMapper;

import java.util.Objects;
import java.util.Optional;

/**
 * 门店用户 前端控制器
 *
 * @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 LawyerUserMapper lawyerUserMapper;
    private final BaseRedisService baseRedisService;

    @ApiOperation("律师用户注册")
    @ApiOperationSupport(order = 1)
    @PostMapping("/register")
    public R<LawyerUserVo> register(@RequestBody LawyerUserDto lawyerUserDto) {
        log.info("LawyerUserLogInController.login?lawyerUserDto={}", lawyerUserDto);
        return lawyerUserService.register(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);
        LawyerUser lawyerUser = lawyerUserMapper.selectOne(new LambdaQueryWrapper<LawyerUser>()
                .eq(LawyerUser::getPhone, lawyerUserDto.getPhone()));
        if (null == lawyerUser) {
            return R.fail("当前账号不存在，请先去注册账号！");
        }
        if (lawyerUser.getStatus() == 0) {
            return R.fail("账号被禁用");
        }
        LawyerUser lawyerUser1 = new LawyerUser();
        lawyerUser1.setId(lawyerUser.getId());
        lawyerUser1.setIsOnline(1);
        lawyerUserMapper.updateById(lawyerUser1);
        return Optional.ofNullable(lawyerUser).
                map(user -> lawyerUserDto.getIsPassword() ? checkPassword(user, lawyerUserDto.getPassword()) : lawyerUserService.createToKen(user)).
                orElseGet(() -> R.fail("手机号不存在"));
    }

    @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 = lawyerUserMapper.selectOne(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("判断失败");
        }
    }

    private R<LawyerUserVo> checkPassword(LawyerUser lawyerUser, String password) {
        return Objects.equals(password, lawyerUser.getPassword())
                ? lawyerUserService.createToKen(lawyerUser)
                : R.fail("密码错误");
    }
}
