package com.zsElectric.boot.security.model;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;

import java.io.Serial;
import java.util.Collection;

/**
 * 微信小程序手机号Code认证Token（新版接口）
 *
 * @author Ray.Hao
 * @since 2.0.0
 */
public class WxMiniAppPhoneCodeAuthenticationToken extends AbstractAuthenticationToken {
    @Serial
    private static final long serialVersionUID = 623L;
    
    private final Object principal; // phoneCode
    private String code; // 微信登录code，用于换取openid

    /**
     * 微信小程序手机号Code认证Token (未认证)
     *
     * @param code 微信登录code，用于换取openid
     * @param phoneCode 微信手机号code
     */
    public WxMiniAppPhoneCodeAuthenticationToken(String code, String phoneCode) {
        super(null);
        this.code = code;
        this.principal = phoneCode;
        this.setAuthenticated(false);
    }

    /**
     * 微信小程序手机号Code认证Token (已认证)
     *
     * @param principal 用户信息
     * @param authorities 授权信息
     */
    public WxMiniAppPhoneCodeAuthenticationToken(Object principal, Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        this.principal = principal;
        super.setAuthenticated(true);
    }

    /**
     * 创建已认证的Token
     *
     * @param principal   用户信息
     * @param authorities 授权信息
     * @return 已认证的Token
     */
    public static WxMiniAppPhoneCodeAuthenticationToken authenticated(Object principal, Collection<? extends GrantedAuthority> authorities) {
        return new WxMiniAppPhoneCodeAuthenticationToken(principal, authorities);
    }

    @Override
    public Object getCredentials() {
        return null;
    }

    @Override
    public Object getPrincipal() {
        return this.principal;
    }

    /**
     * 获取微信登录code
     * 
     * @return 微信登录code
     */
    public String getCode() {
        return code;
    }
}
