package com.vetti.web.controller.system;

import cn.hutool.core.collection.CollectionUtil;
import com.vetti.common.core.controller.BaseController;
import com.vetti.common.core.domain.AjaxResult;
import com.vetti.common.core.domain.R;
import com.vetti.common.core.domain.entity.SysUser;
import com.vetti.common.exception.ServiceException;
import com.vetti.common.service.verification.VerificationService;
import com.vetti.common.utils.MessageUtils;
import com.vetti.system.mapper.SysUserMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ID
 * @date 2025/8/28 16:16
 */
@Api(tags ="验证码共通接口")
@RestController
@RequestMapping("/verification")
@RequiredArgsConstructor
public class VerificationController extends BaseController {

    @Resource
    private final VerificationService verificationEmailService;

    @Autowired
    private SysUserMapper userMapper;


    @ApiOperation("发送验证码（标题、内容走的配置文件）")
    @PostMapping("/email/send")
    public AjaxResult sendVerificationCode(@RequestParam String email) {
        verificationEmailService.sendVerificationEm7941VerificationCode(email);
        return AjaxResult.success(MessageUtils.messageCustomize("systemVerificationEmailController10001"));
    }

    @ApiOperation("发送验证码（标题、内容走的配置文件）-注册使用")
    @PostMapping("/email/register/send")
    public AjaxResult sendRegisterVerificationCode(@RequestParam String email) {

        SysUser query = new SysUser();
        query.setUserName(email);
        List<SysUser> userList = userMapper.selectUserList(query);
        if (CollectionUtil.isNotEmpty(userList)) {
            throw new ServiceException(MessageUtils.messageCustomize("systemExceptionSysAppLoginServiceImpl10004"));
        }
        verificationEmailService.sendVerificationEm7941VerificationCode(email);
        return AjaxResult.success(MessageUtils.messageCustomize("systemVerificationEmailController10001"));
    }

    /**
     * 验证邮箱验证码
     */
    @ApiOperation("验证邮箱验证码")
    @PostMapping("/email/verify")
    public R<?> verifyCode(@RequestParam String email, @RequestParam String code) {
        boolean isValid = verificationEmailService.verifyCode(email, code);
        if (isValid) {
            return R.ok(MessageUtils.messageCustomize("systemVerificationEmailController10003"));
        } else {
            return R.fail(MessageUtils.messageCustomize("systemVerificationEmailController10004"));
        }
    }

    @ApiOperation("发送验证码（手机）")
    @PostMapping("/phone/send")
    public AjaxResult sendPhoneVerificationCode(@RequestParam String phone) {
        verificationEmailService.sendPhoneVerificationCode(phone);
        return AjaxResult.success(MessageUtils.messageCustomize("systemVerificationEmailController10003"));
    }
}
