package com.zsElectric.boot.security.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Collections;

/**
 * 小程序用户认证详情
 * <p>
 * 用于小程序用户的认证和授权
 *
 * @author zsElectric
 * @since 2025-12-17
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class AppletUserDetails implements UserDetails {

    /**
     * 用户ID (c_user_info表的ID)
     */
    private Long userId;

    /**
     * 手机号（作为用户名）
     */
    private String phone;

    /**
     * 微信openid
     */
    private String openid;

    /**
     * 昵称
     */
    private String nickname;

    /**
     * 账号是否启用
     */
    private Boolean enabled = true;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        // 小程序用户默认角色
        return Collections.singleton(new SimpleGrantedAuthority("ROLE_APPLET_USER"));
    }

    @Override
    public String getPassword() {
        // 小程序用户无需密码
        return null;
    }

    @Override
    public String getUsername() {
        // 使用手机号作为用户名
        return this.phone;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return this.enabled;
    }
}
