package com.reading.platform.common.security;

import com.reading.platform.common.exception.BusinessException;
import com.reading.platform.common.enums.ErrorCode;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * Security Utilities
 */
public class SecurityUtils {

    private SecurityUtils() {
    }

    public static JwtPayload getCurrentUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null || !authentication.isAuthenticated()) {
            throw new BusinessException(ErrorCode.UNAUTHORIZED);
        }
        return (JwtPayload) authentication.getPrincipal();
    }

    public static Long getCurrentUserId() {
        return getCurrentUser().getUserId();
    }

    public static String getCurrentUsername() {
        return getCurrentUser().getUsername();
    }

    public static String getCurrentRole() {
        return getCurrentUser().getRole();
    }

    public static Long getCurrentTenantId() {
        return getCurrentUser().getTenantId();
    }

    public static String getCurrentName() {
        return getCurrentUser().getName();
    }

    public static boolean isAdmin() {
        return "admin".equals(getCurrentRole());
    }

    public static boolean isSchool() {
        return "school".equals(getCurrentRole());
    }

    public static boolean isTeacher() {
        return "teacher".equals(getCurrentRole());
    }

    public static boolean isParent() {
        return "parent".equals(getCurrentRole());
    }

}
