package com.ruoyi.dk.controller.app;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.RandomUtil;
import com.ruoyi.component.redis.util.RedisUtils;
import com.ruoyi.dk.domain.Customer;
import com.ruoyi.dk.dto.app.req.CustomerRegisterReq;
import com.ruoyi.dk.dto.app.req.UpdatePwdOpenReq;
import com.ruoyi.dk.service.CustomerService;
import com.ruoyi.dk.sms.CodeService;
import com.ruoyi.dk.sms.CodeType;
import com.ruoyi.component.core.domain.R;
import com.ruoyi.component.core.exception.CustomException;
import com.ruoyi.component.core.util.MessageUtils;
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.*;

@RequestMapping("/app/customer/open")
@RestController
@Tag(name = "用户开放接口")
@SaIgnore
public class AppCustomerOpenController {
    @Autowired
    private CodeService codeService;
    @Autowired
    private CustomerService customerService;

    @GetMapping("/sms/register")
    @Operation(summary = "用户注册-验证码")
    public R<String> customerRegister(String phoneNumber) {
        String numbers = RandomUtil.randomNumbers(6);
        codeService.put(phoneNumber, CodeType.CUSTOMER_REGISTER,numbers);
        R<String> success = R.ok();
        success.setData(numbers);
        return success;
    }

    @PostMapping("/register")
    @Operation(summary = "用户注册")
    public R<Void> customerRegister(@RequestBody CustomerRegisterReq register) {
        if(!codeService.check(register.getPhoneNumber(), CodeType.CUSTOMER_REGISTER,register.getCode())){
            throw new CustomException(MessageUtils.message("dk.code.error"));
        }
        customerService.register(register);
        return R.ok();
    }

    @GetMapping("/sms/forget")
    @Operation(summary = "忘记密码-验证码")
    public R<String> customerForgetPassword(String phoneNumber) {
        String numbers = RandomUtil.randomNumbers(6);
        codeService.put(phoneNumber, CodeType.CUSTOMER_FORGET_PASSWORD,numbers);
        R<String> success = R.ok();
        success.setData(numbers);
        return success;
    }

    @GetMapping("/sms/forget/check")
    @Operation(summary = "忘记密码-验证码-校验")
    public R<String> customerForgetPasswordCheck(String phoneNumber,String code) {
        if(!codeService.check(phoneNumber, CodeType.CUSTOMER_REGISTER,code)){
            throw new CustomException(MessageUtils.message("dk.code.error"));
        }
        Customer customer = customerService.getCustomerByName(phoneNumber);
        String uuid = UUID.randomUUID().toString();
        RedisUtils.setCacheObject(uuid, customer);
        R<String> success = R.ok();
        success.setData(uuid);
        return success;
    }

    @PostMapping("/updatePwd")
    @Operation(summary = "修改密码")
    public R<Void> customerForgetPasswordCheck(@RequestBody UpdatePwdOpenReq updatePwdOpenReq) {
        Customer customer = RedisUtils.getCacheObject(updatePwdOpenReq.getCheckCode());
        if(customer == null){
            throw new CustomException(MessageUtils.message("dk.password.update.error"));
        }
        if(!updatePwdOpenReq.getPassword().equals(updatePwdOpenReq.getConfirmPassword())){
            throw new CustomException(MessageUtils.message("dk.password.check.error"));
        }
        customerService.updatePwd(customer.getId(),updatePwdOpenReq.getPassword(),true);
        return R.ok();
    }

}
