package kr.co.accura.wbx.spring.common;

import kr.co.accura.wbx.spring.auth.WbxUserDetails;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

public final class SecurityUtils {

    private SecurityUtils() {}

    public static Long getCurrentUserId() {
        var user = getCurrentUser();
        return user != null ? user.getId() : null;
    }

    public static String getCurrentUsername() {
        var user = getCurrentUser();
        return user != null ? user.getUsername() : null;
    }

    public static Long getCurrentDeptId() {
        var user = getCurrentUser();
        return user != null ? user.getDepartmentId() : null;
    }

    public static WbxUserDetails getCurrentUser() {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth != null && auth.getPrincipal() instanceof WbxUserDetails details) {
            return details;
        }
        return null;
    }
}
