package org.xyzh.auth.strategy.impl;

import org.springframework.stereotype.Component;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.xyzh.auth.strategy.LoginStrategy;
import org.xyzh.common.core.domain.LoginParam;
import org.xyzh.common.dto.sys.TbSysUserDTO;
import org.xyzh.api.system.service.SysUserService;
import org.xyzh.api.system.vo.SysUserVO;
import org.xyzh.common.redis.service.RedisService;

/**
 * @description    EmailLoginStrategy.java文件描述 邮箱登录策略
 * @filename    EmailLoginStrategy.java
 * @author    yslg
 * @copyright xyzh
 * @since 2025-12-05
 */
@Component
public class EmailLoginStrategy implements LoginStrategy {

    @Autowired
    private SysUserService userService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public String getLoginType() {
        return "email";
    }

    @Override
    public boolean validate(LoginParam loginParam) {
        if (loginParam.getEmail() == null || loginParam.getEmail().trim().isEmpty()) {
            return false;
        }
        // 密码登录或验证码登录都可以
        return (loginParam.getPassword() != null && !loginParam.getPassword().trim().isEmpty())
                || (loginParam.getCaptcha() != null && !loginParam.getCaptcha().trim().isEmpty());
    }

    @Override
    public SysUserVO findUser(LoginParam loginParam) {
        SysUserVO filter = new SysUserVO();
        filter.setEmail(loginParam.getEmail());
        SysUserVO user = userService.getLoginUser(filter).getData();
        if(user == null) {
            return null;
        }
        return user;
    }

    @Autowired
    private RedisService redisService;

    @Override
    public boolean verifyCredential(String inputCredential, String storedCredential) {
        // 密码验证
        return passwordEncoder.matches(inputCredential, storedCredential);
    }

    @Override
    public boolean verifyCaptchaWithSession(LoginParam loginParam) {
        String captchaId = loginParam.getCaptchaId();
        String inputCaptcha = loginParam.getCaptcha();
        String email = loginParam.getEmail();

        // 验证参数
        if (captchaId == null || captchaId.trim().isEmpty()) {
            return false;
        }
        if (inputCaptcha == null || inputCaptcha.trim().isEmpty()) {
            return false;
        }

        // 从Redis获取验证码
        String codeKey = "email:code:" + captchaId;
        String storedValue = redisService.get(codeKey, String.class);
        
        if (storedValue == null) {
            return false;
        }

        // 解析存储的值：邮箱:验证码
        String[] parts = storedValue.split(":");
        if (parts.length != 2) {
            return false;
        }

        String storedEmail = parts[0];
        String storedCaptcha = parts[1];

        // 验证邮箱和验证码是否匹配
        if (!storedEmail.equals(email) || !storedCaptcha.equals(inputCaptcha)) {
            return false;
        }

        // 验证码使用后删除
        redisService.delete(codeKey);
        return true;
    }
}