package org.jeecg.modules.shop.captcha.enums;

import cn.hutool.crypto.SecureUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum CaptchaKeyEnum {
    
    // KT 是 Key Template 的缩写
    /**
     * 短信验证码 Key 模板
     */
    KT_SMS_CAPTCHA("captcha:sms:%s:%s"),

    /**
     * 邮件验证码 Key 模板
     */
    KT_EMAIL_CAPTCHA("captcha:email:%s:%s"),

    /**
     * 验证码访问次数 Key 模板
     */
    KT_CAPTCHA_ACCESS_COUNT("captcha:ac:%s"),
    /**
     * 验证码Token Key 模板
     */
    KT_CAPTCHA_TOKEN("captcha:token:%s"),
    /**
     * 验证码发送锁 Key 模板
     */
    KT_CAPTCHA_SEND_LOCK("captcha:lock:%s");
    
    private final String value;
    
    public static String generateSmsCaptchaKey(String type, String phoneNumber) {
        return String.format(KT_SMS_CAPTCHA.value, type, SecureUtil.md5(phoneNumber));
    }

    public static String generateEmailCaptchaKey(String type, String email) {
        return String.format(KT_EMAIL_CAPTCHA.value, type, SecureUtil.md5(email));
    }

    public static String generateCaptchaAccessCountKey(String captchaKey) {
        return String.format(KT_CAPTCHA_ACCESS_COUNT.value, captchaKey);
    }

    public static String generateCaptchaTokenKey(String captchaKey) {
        return String.format(KT_CAPTCHA_TOKEN.value, captchaKey);
    }

    public static String generateCaptchaSendLockKey(String receiver) {
        return String.format(KT_CAPTCHA_SEND_LOCK.value, SecureUtil.md5(receiver));
    }
    
}
