package test05.utils;

import java.util.Map;

import com.alibaba.fastjson2.JSONObject;

import test05.enums.UserRole;

/**
 * 用户角色工具类
 */
public class UserRoleUtils {
    
    /**
     * 从JWT token中提取用户名并获取角色
     */
    public static UserRole getUserRoleFromToken(String token) {
        try {
            if (token == null || token.isEmpty()) {
                return null;
            }
            
            // 解析JWT token获取用户信息
            Map<String, Object> claims_map = test05.utils.JwtUtils.parseJwt(token);
            com.alibaba.fastjson2.JSONObject claims = new JSONObject(claims_map) ;
            if (claims == null || !claims.containsKey("data")) {
                return null;
            }
            
            Object dataObj = claims.get("data");
            if (dataObj == null) {
                return null;
            }
            
            com.alibaba.fastjson2.JSONObject data;
            if (dataObj instanceof com.alibaba.fastjson2.JSONObject) {
                data = (com.alibaba.fastjson2.JSONObject) dataObj;
            } else {
                data = com.alibaba.fastjson2.JSONObject.parseObject(dataObj.toString());
            }
            
            if (!data.containsKey("name")) {
                return null;
            }
            
            String username = data.getString("name");
            return UserRole.getRoleByUsername(username);
            
        } catch (Exception e) {
            return null;
        }
    }
    
    /**
     * 从JWT token中提取用户名
     */
    public static String getUsernameFromToken(String token) {
        try {
            if (token == null || token.isEmpty()) {
                return null;
            }
            
            com.alibaba.fastjson2.JSONObject claims = new JSONObject(test05.utils.JwtUtils.parseJwt(token));
            if (claims == null || !claims.containsKey("data")) {
                return null;
            }
            
            Object dataObj = claims.get("data");
            if (dataObj == null) {
                return null;
            }
            
            com.alibaba.fastjson2.JSONObject data;
            if (dataObj instanceof com.alibaba.fastjson2.JSONObject) {
                data = (com.alibaba.fastjson2.JSONObject) dataObj;
            } else {
                data = com.alibaba.fastjson2.JSONObject.parseObject(dataObj.toString());
            }
            
            if (!data.containsKey("name")) {
                return null;
            }
            
            return data.getString("name");
            
        } catch (Exception e) {
            return null;
        }
    }
    
    /**
     * 检查用户是否有权限创建指定角色的用户
     */
    public static boolean canCreateUser(UserRole creatorRole, UserRole targetRole) {
        if (creatorRole == null || targetRole == null) {
            return false;
        }
        
        switch (creatorRole) {
            case ADMIN:
                // 管理员可以创建老师和用户
                return targetRole == UserRole.TEACHER || targetRole == UserRole.STUDENT;
            case TEACHER:
                // 老师可以创建学生
                return targetRole == UserRole.STUDENT;
            case STUDENT:
                // 学生不能创建用户
                return false;
            default:
                return false;
        }
    }
}