package com.dora.util;

import com.dora.exception.AuthenticationException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * 安全工具类
 */
public class SecurityUtil {
    
    private static final String ANONYMOUS_USER = "anonymousUser";
    
    /**
     * 私有构造函数，防止实例化
     */
    private SecurityUtil() {
        throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
    }

    /**
     * 获取当前认证用户的ID
     * @return 用户ID
     * @throws AuthenticationException 如果用户未认证或ID格式错误
     */
    public static Long getCurrentUserId() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        
        // 检查用户是否已认证
        if (authentication == null || ANONYMOUS_USER.equals(authentication.getName()) || !authentication.isAuthenticated()) {
            throw new AuthenticationException("用户未认证，请先登录");
        }
        
        // 安全地获取用户ID
        try {
            return Long.valueOf(authentication.getName());
        } catch (NumberFormatException e) {
            throw new AuthenticationException("用户ID格式错误，请重新登录");
        }
    }

    /**
     * 获取当前认证用户的ID（可为null）
     * 用于不需要强制登录的场景，如浏览广场时判断是否点赞
     * 
     * @return 用户ID，如果未登录则返回null
     */
    public static Long getCurrentUserIdOrNull() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        
        // 检查用户是否已认证
        if (authentication == null || ANONYMOUS_USER.equals(authentication.getName()) || !authentication.isAuthenticated()) {
            return null;
        }
        
        // 安全地获取用户ID
        try {
            return Long.valueOf(authentication.getName());
        } catch (NumberFormatException e) {
            return null;
        }
    }

    /**
     * 检查用户是否已认证
     * @return true表示已认证，false表示未认证
     */
    public static boolean isAuthenticated() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return authentication != null && 
               !ANONYMOUS_USER.equals(authentication.getName()) && 
               authentication.isAuthenticated();
    }

    /**
     * 获取当前认证用户的用户名
     * @return 用户名（通常是用户ID的字符串形式）
     * @throws AuthenticationException 如果用户未认证
     */
    public static String getUsername() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        
        // 检查用户是否已认证
        if (authentication == null || ANONYMOUS_USER.equals(authentication.getName()) || !authentication.isAuthenticated()) {
            throw new AuthenticationException("用户未认证，请先登录");
        }
        
        return authentication.getName();
    }

    /**
     * 获取当前认证用户的用户名（可为null）
     * @return 用户名，如果未登录则返回null
     */
    public static String getUsernameOrNull() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        
        // 检查用户是否已认证
        if (authentication == null || ANONYMOUS_USER.equals(authentication.getName()) || !authentication.isAuthenticated()) {
            return null;
        }
        
        return authentication.getName();
    }
} 