package com.ruoyi.xq.controller.app;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.util.PhoneUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.xq.dto.app.auth.LoginUser;
import com.ruoyi.xq.dto.app.auth.LoginVo;
import com.ruoyi.xq.dto.app.auth.RegisterCode;
import com.ruoyi.xq.dto.app.auth.RegisterCodeCheck;
import com.ruoyi.xq.dto.app.user.ResetPasswordReq;
import com.ruoyi.xq.dto.common.sms.SmsCodeReq;
import com.ruoyi.xq.enums.common.CodeEnum;
import com.ruoyi.xq.manager.LoginManager;
import com.ruoyi.xq.manager.CurrentUserManager;
import com.ruoyi.xq.service.SmsVerifyService;
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 LoginManager loginManager;
    @Autowired
    private SmsVerifyService smsVerifyService;
    @Autowired
    private CurrentUserManager currentUserManager;

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

    @PostMapping("/login")
    @Operation(summary = "登陆（选择手机号登陆，会自动注册）")
    @Log(title = "登陆", businessType = BusinessType.OTHER, isSaveDb = true)
    public R<LoginVo> login(@Validated @RequestBody LoginUser loginBody){
        LoginVo vo = new LoginVo();
        String token;
        if(loginBody.getLoginType() == 2){
            token = loginManager.login(loginBody.getUsername(), loginBody.getPassword());
        }else if(loginBody.getLoginType() == 1){
            token = loginManager.loginSms(loginBody.getUsername(), loginBody.getCode(), loginBody.getInviteCode());
        }else{
            return R.fail(600,"登陆失败，参数异常");
        }
        vo.setToken(token);
        vo.setUserInfo(currentUserManager.currentBaseInfo());
        return R.ok(vo);
    }

    @PostMapping("/loginOut")
    @Operation(summary = "登出")
    @Log(title = "登出", businessType = BusinessType.OTHER, isSaveDb = true)
    public R<Void> loginOut(){
        loginManager.logout();
        return R.ok();
    }


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


    @PostMapping("/resetPassword/checkCode")
    @Operation(summary = "重置密码-验证码校验")
    @Log(title = "重置密码-验证码校验", businessType = BusinessType.OTHER, isSaveDb = true)
    public R<Map<String,String>> resetPasswordCheck(@RequestBody RegisterCodeCheck code){
        String token = loginManager.resetPasswordCheck(code.getMobile(), code.getCode());
        Map<String,String> map = new HashMap<>();
        map.put("token",token);
        return R.ok(map);
    }

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





}
