package com.lesingle.creation.common.security;

import lombok.Getter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;

/**
 * 用户 Principal 实现类
 *
 * @author lesingle
 * @since 1.0.0
 */
@Getter
public class UserPrincipal implements UserDetails {

    private final Long userId;
    private final String username;       // 用户名（用于显示）
    private final String password;
    private final Collection<? extends GrantedAuthority> authorities;
    private final Long tenantId;
    private final String tenantCode;     // 租户编码
    private final boolean isSuperTenant; // 是否超级租户

    public UserPrincipal(Long userId, String username, String password,
                         Collection<? extends GrantedAuthority> authorities,
                         Long tenantId, String tenantCode, boolean isSuperTenant) {
        this.userId = userId;
        this.username = username;
        this.password = password;
        this.authorities = authorities;
        this.tenantId = tenantId;
        this.tenantCode = tenantCode;
        this.isSuperTenant = isSuperTenant;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return String.valueOf(this.userId); // 返回用户 ID 字符串，方便 SecurityUtils 获取
    }

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

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

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

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

    /**
     * 获取显示用户名（用于日志等）
     *
     * @return 显示用户名
     */
    public String getDisplayName() {
        return this.username;
    }

}
