package com.qqchen.deploy.backend.framework.security;

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

import java.util.Collection;

/**
 * 自定义 UserDetails 实现
 * 扩展标准 UserDetails，包含用户ID、租户ID等额外信息
 *
 * @author qqchen
 * @date 2025-10-25
 */
@Getter
public class CustomUserDetails implements UserDetails {

    /**
     * 用户ID
     */
    private final Long userId;

    /**
     * 用户名
     */
    private final String username;

    /**
     * 密码
     */
    private final String password;

    /**
     * 是否启用
     */
    private final boolean enabled;

    /**
     * 账户是否未过期
     */
    private final boolean accountNonExpired;

    /**
     * 账户是否未锁定
     */
    private final boolean accountNonLocked;

    /**
     * 凭证是否未过期
     */
    private final boolean credentialsNonExpired;

    /**
     * 权限列表
     */
    private final Collection<? extends GrantedAuthority> authorities;

    /**
     * 租户ID（可选）
     */
    private final Long tenantId;

    /**
     * 部门ID（可选）
     */
    private final Long departmentId;

    /**
     * 构造函数
     */
    public CustomUserDetails(Long userId,
                             String username,
                             String password,
                             boolean enabled,
                             boolean accountNonExpired,
                             boolean accountNonLocked,
                             boolean credentialsNonExpired,
                             Collection<? extends GrantedAuthority> authorities,
                             Long tenantId,
                             Long departmentId) {
        this.userId = userId;
        this.username = username;
        this.password = password;
        this.enabled = enabled;
        this.accountNonExpired = accountNonExpired;
        this.accountNonLocked = accountNonLocked;
        this.credentialsNonExpired = credentialsNonExpired;
        this.authorities = authorities;
        this.tenantId = tenantId;
        this.departmentId = departmentId;
    }

    /**
     * 简化构造函数（不包含租户和部门信息）
     */
    public CustomUserDetails(Long userId,
                             String username,
                             String password,
                             boolean enabled,
                             Collection<? extends GrantedAuthority> authorities) {
        this(userId, username, password, enabled, true, true, true, authorities, null, null);
    }

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

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

    @Override
    public String getUsername() {
        return username;
    }

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

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

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

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

