package test05.enums;

/**
 * 用户角色枚举
 */
public enum UserRole {
    ADMIN("a_", "管理员"),
    TEACHER("t_", "老师"),
    STUDENT("s_", "学生");

    private final String prefix;
    private final String description;

    UserRole(String prefix, String description) {
        this.prefix = prefix;
        this.description = description;
    }

    public String getPrefix() {
        return prefix;
    }

    public String getDescription() {
        return description;
    }

    /**
     * 根据用户名获取角色
     */
    public static UserRole getRoleByUsername(String username) {
        if (username == null || username.isEmpty()) {
            return null;
        }
        
        if (username.startsWith(ADMIN.getPrefix())) {
            return ADMIN;
        } else if (username.startsWith(TEACHER.getPrefix())) {
            return TEACHER;
        } else if (username.startsWith(STUDENT.getPrefix())) {
            return STUDENT;
        }
        
        return null;
    }

    /**
     * 检查用户名格式是否有效
     */
    public static boolean isValidUsername(String username) {
        return getRoleByUsername(username) != null;
    }

    /**
     * 检查角色是否有权限访问指定路径
     */
    public boolean hasAccessToPath(String path) {
        if (path == null) return false;
        
        switch (this) {
            case ADMIN:
                // 管理员可以访问所有API
                return true;
            case TEACHER:
                // 老师可以访问老师端API
                return path.contains("/teacherAction/") || path.contains("/studentAction/");
            case STUDENT:
                // 学生只能访问学生端API
                return path.contains("/studentAction/");
            default:
                return false;
        }
    }
}