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.api.system.service.SysUserService;
import org.xyzh.api.system.vo.SysUserVO;
import org.xyzh.common.redis.service.RedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @description    PhoneLoginStrategy.java文件描述 手机号登录策略
 * @filename    PhoneLoginStrategy.java
 * @author    yslg
 * @copyright xyzh
 * @since 2025-12-05
 */
@Component
public class PhoneLoginStrategy implements LoginStrategy {
    private static final Logger logger = LoggerFactory.getLogger(PhoneLoginStrategy.class);

    @Autowired
    private SysUserService userService;

    @Autowired
    private PasswordEncoder passwordEncoder;

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

    @Override
    public boolean validate(LoginParam loginParam) {
        if (loginParam.getPhone() == null || loginParam.getPhone().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.setPhone(loginParam.getPhone());
        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 phone = loginParam.getPhone();

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

        // 从Redis获取验证码
        String codeKey = "sms: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 storedPhone = parts[0];
        String storedCaptcha = parts[1];

        // 验证手机号和验证码是否匹配
        if (!storedPhone.equals(phone) || !storedCaptcha.equals(inputCaptcha)) {
            return false;
        }

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