package com.ruoyi.cai.controller.app;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.util.PhoneUtil;
import com.ruoyi.cai.auth.*;
import com.ruoyi.cai.domain.User;
import com.ruoyi.cai.dto.app.vo.LoginVo;
import com.ruoyi.cai.enums.CodeEnum;
import com.ruoyi.cai.manager.CurrentUserManager;
import com.ruoyi.cai.service.SmsVerifyService;
import com.ruoyi.cai.service.UserService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
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 java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/auth")
@SaIgnore
@Tag(name = "权限相关接口，免鉴权")
@Validated
public class AuthAppController {

    @Autowired
    private CaiLoginManager caiLoginManager;
    @Autowired
    private SmsVerifyService smsVerifyService;
    @Autowired
    private UserService userService;
    @Autowired
    private CurrentUserManager currentUserManager;

    @PostMapping("/register")
    @Operation(summary = "注册")
    @Log(title = "注册", businessType = BusinessType.OTHER, isSaveDb = false)
    public R<LoginVo> register(@Validated @RequestBody CaiRegisterUser caiUser){
        boolean mobile = PhoneUtil.isMobile(caiUser.getUsername());
        if(!mobile){
            return R.fail(600,"请输入正确的手机格式");
        }
        String token = caiLoginManager.register(caiUser);
        LoginVo vo = new LoginVo();
        vo.setToken(token);
        vo.setUserInfo(currentUserManager.currentInfo());
        return R.ok(vo);
    }

    @PostMapping("/register/code")
    @Operation(summary = "获取注册验证码")
    @Log(title = "获取注册验证码", businessType = BusinessType.OTHER, isSaveDb = false)
    public R<Map<String,String>> registerCode(@Validated @RequestBody RegisterCode code){
        boolean mobile = PhoneUtil.isMobile(code.getMobile());
        if(!mobile){
            return R.fail(600,"请输入正确的手机格式");
        }
        smsVerifyService.put(CodeEnum.REGISTER,code.getMobile());
        return R.ok("发送成功");
    }

    @PostMapping("/resetPassword/code")
    @Operation(summary = "获取重置密码验证码")
    @Log(title = "获取重置密码验证码", businessType = BusinessType.OTHER, isSaveDb = false)
    public R<Map<String,String>> resetPasswordCode(@Validated @RequestBody RegisterCode code){
        boolean mobile = PhoneUtil.isMobile(code.getMobile());
        if(!mobile){
            return R.fail(600,"请输入正确的手机格式");
        }
        User user = userService.getByUsername(code.getMobile());
        if(user == null){
            throw new ServiceException("用户不存在");
        }
        smsVerifyService.put(CodeEnum.RESET_PASSWORD,code.getMobile());
        return R.ok("发送成功");
    }

    @PostMapping("/login")
    @Operation(summary = "登陆")
    @Log(title = "登陆", businessType = BusinessType.OTHER, isSaveDb = false)
    public R<LoginVo> login(@Validated @RequestBody LoginCaiUser loginBody){
        LoginVo vo = new LoginVo();
        String token = caiLoginManager.login(loginBody.getUsername(), loginBody.getPassword());
        vo.setToken(token);
        vo.setUserInfo(currentUserManager.currentInfo());
        return R.ok(vo);
    }

    @PostMapping("/resetPassword")
    @Operation(summary = "重置密码")
    @Log(title = "重置密码", businessType = BusinessType.OTHER, isSaveDb = false)
    public R<Boolean> resetPassword(@RequestBody ResetPasswordReq code){
        caiLoginManager.resetPassword(code);
        return R.ok(true);
    }


}
