package org.xyzh.auth.strategy.impl;

import org.springframework.stereotype.Component;
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.user.TbSysUser;
import org.xyzh.api.system.user.UserService;

import java.util.List;

/**
 * @description    WechatLoginStrategy.java文件描述 微信登录策略
 * @filename    WechatLoginStrategy.java
 * @author    yslg
 * @copyright xyzh
 * @since 2025-09-28
 */
@Component
public class WechatLoginStrategy implements LoginStrategy {

    @Autowired
    private UserService userService;


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

    @Override
    public boolean validate(LoginParam loginParam) {
        return loginParam.getWechatID() != null && !loginParam.getWechatID().trim().isEmpty();
    }

    @Override
    public TbSysUser findUser(LoginParam loginParam) {
        TbSysUser filter = new TbSysUser();
        filter.setWechatID(loginParam.getWechatID());
        List<TbSysUser> users = userService.getUserByFilter(filter).getDataList();
        if(users.isEmpty()) {
            return null;
        }
        return users.get(0);
    }

    @Override
    public boolean verifyCredential(String inputCredential, String storedCredential) {
        // 微信登录通常不需要密码验证，通过微信授权码验证
        // 这里可以添加微信授权验证逻辑
        return true;
    }
}
