package com.ga.common.util;

import com.ga.common.auth.LoginUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Optional;

/**
 * 현재 로그인 사용자 조회.
 */
public final class SecurityUtil {

    private SecurityUtil() {}

    public static Optional<LoginUser> getLoginUser() {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth == null || !auth.isAuthenticated()) return Optional.empty();
        Object principal = auth.getPrincipal();
        if (principal instanceof LoginUser lu) return Optional.of(lu);
        return Optional.empty();
    }

    public static Long getCurrentUserId() {
        return getLoginUser().map(LoginUser::getUserId).orElse(null);
    }

    public static String getCurrentLoginId() {
        return getLoginUser().map(LoginUser::getLoginId).orElse(null);
    }
}
