package com.ruoyi.cai.controller.app;

import cn.dev33.satoken.annotation.SaIgnore;
import com.ruoyi.cai.auth.*;
import com.ruoyi.cai.enums.CodeEnum;
import com.ruoyi.cai.manager.CodeManager;
import com.ruoyi.cai.service.SmsVerifyService;
import com.ruoyi.common.core.domain.R;
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.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 = "权限相关接口，免鉴权")
public class AuthAppController {

    @Autowired
    private CaiLoginManager caiLoginManager;
    @Autowired
    private SmsVerifyService smsVerifyService;

    @PostMapping("/register")
    @Operation(summary = "注册")
    public R<Map<String, Object>> register(@RequestBody CaiRegisterUser caiUser){
        String token = caiLoginManager.register(caiUser);
        Map<String, Object> ajax = new HashMap<>();
        ajax.put("token",token);
        return R.ok(ajax);
    }

    @PostMapping("/register/code")
    @Operation(summary = "获取注册验证码")
    public R<Map<String,String>> registerCode(@RequestBody RegisterCode code){
        smsVerifyService.put(CodeEnum.REGISTER,code.getMobile());
        return R.ok("发送成功");
    }

    @PostMapping("/resetPassword/code")
    @Operation(summary = "获取充值密码验证码")
    public R<Map<String,String>> resetPasswordCode(@RequestBody RegisterCode code){
        smsVerifyService.put(CodeEnum.RESET_PASSWORD,code.getMobile());
        return R.ok("发送成功");
    }

    @PostMapping("/login")
    @Operation(summary = "登陆")
    public R<Map<String,Object>> login(@RequestBody LoginCaiUser loginBody){
        Map<String, Object> ajax = new HashMap<>();
        String token = caiLoginManager.login(loginBody.getUsername(), loginBody.getPassword());
        ajax.put("token",token);
        return R.ok(ajax);
    }

    @PostMapping("/resetPassword")
    @Operation(summary = "获取充值密码验证码")
    public R<Boolean> resetPassword(@RequestBody ResetPasswordReq code){
        caiLoginManager.resetPassword(code);
        return R.ok("发送成功");
    }


}
