package com.ruoyi.cai.controller.app;


import cn.hutool.core.util.PhoneUtil;
import com.ruoyi.cai.auth.RegisterCode;
import com.ruoyi.cai.domain.Adolescent;
import com.ruoyi.cai.domain.User;
import com.ruoyi.cai.dto.app.query.user.AdolescentSettingReq;
import com.ruoyi.cai.dto.app.query.user.AdolescentSmsCheckReq;
import com.ruoyi.cai.dto.app.query.user.AdolescentSwitchReq;
import com.ruoyi.cai.dto.app.vo.user.AdolescentVo;
import com.ruoyi.cai.enums.CodeEnum;
import com.ruoyi.cai.service.AdolescentService;
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.helper.LoginHelper;
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.*;

import java.util.Map;

@RestController
@RequestMapping("/api/adolescent")
@Tag(name = "青少年模式接口")
public class AdolescentAppController {

    @Autowired
    private AdolescentService adolescentService;
    @Autowired
    private UserService userService;

    @PostMapping("/set")
    @Operation(summary = "设置青少年模式")
    @Log(title = "设置青少年模式", businessType = BusinessType.OTHER, isSaveDb = false)
    public R<Void> set(@RequestBody AdolescentSettingReq setting){
        adolescentService.setAdolescent(setting);
        return R.ok();
    }

    @GetMapping("/get")
    @Operation(summary = "获取用户青少年模式状态")
    @Log(title = "获取用户青少年模式状态", businessType = BusinessType.OTHER, isSaveDb = false)
    public R<AdolescentVo> get(){
        Long userId = LoginHelper.getUserId();
        Adolescent adolescent = adolescentService.getByUserId(userId);
        AdolescentVo vo = new AdolescentVo();
        if(adolescent != null){
            vo.setIsSetAdolescent(1);
            vo.setIsSetPwd(1);
            vo.setIsOpen(adolescent.getOpen());
        }
        return R.ok(vo);
    }

    @PostMapping("/switch")
    @Operation(summary = "青少年模式开关")
    @Log(title = "青少年模式开关", businessType = BusinessType.OTHER, isSaveDb = false)
    public R<Void> switchAdolescent(@RequestBody AdolescentSwitchReq adolescentSwitchReq){
        adolescentService.switchAdolescent(adolescentSwitchReq);
        return R.ok();
    }

    @PostMapping("/verifyMobileCode")
    @Operation(summary = "校验验证码-青少年模式")
    @Log(title = "校验验证码-青少年模式", businessType = BusinessType.OTHER, isSaveDb = false)
    public R<Void> verifyMobileCode(@RequestBody AdolescentSmsCheckReq req){
        adolescentService.verifyMobileCode(req);
        return R.ok();
    }

    @Autowired
    private SmsVerifyService smsVerifyService;

    @PostMapping("/sms/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.getById(LoginHelper.getUserId());
        if(user == null || !user.getMobile().equals(code.getMobile())){
            return R.fail(600,"手机号码和绑定的手机号码不一致");
        }
        smsVerifyService.put(CodeEnum.RESET_ADOLESCENT,code.getMobile());
        return R.ok("发送成功");
    }

}
