package com.ga.common.auth;

import lombok.Builder;
import lombok.Getter;
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.List;

/**
 * SecurityContext 에 들어갈 인증 주체.
 */
@Getter
@Builder
public class LoginUser implements UserDetails {

    private final Long userId;
    private final String loginId;
    private final String userName;
    private final Long agentId;
    private final List<String> roleCodes;

    @Override public Collection<? extends GrantedAuthority> getAuthorities() {
        return roleCodes == null ? List.of() :
                roleCodes.stream().<GrantedAuthority>map(r -> new SimpleGrantedAuthority("ROLE_" + r)).toList();
    }

    @Override public String getPassword() { return null; }
    @Override public String getUsername() { return loginId; }
    @Override public boolean isAccountNonExpired()     { return true; }
    @Override public boolean isAccountNonLocked()      { return true; }
    @Override public boolean isCredentialsNonExpired() { return true; }
    @Override public boolean isEnabled()               { return true; }
}
