import { useAuthContext } from "@/auth/AuthContext";
import { RoleEnum } from "@/constants/roleEnum";

export const useRoleAccess = () => {
  const { user } = useAuthContext();
  const currentRole = user?.role;

  const isUser = currentRole === RoleEnum.USER;
  const isAdmin =
    currentRole === RoleEnum.ADMIN || currentRole === RoleEnum.MANAGER;
  const isManager = currentRole === RoleEnum.MANAGER;

  return {
    currentRole,
    isUser,
    isAdmin,
    isManager,
    hasAccess: (level: RoleEnum) => {
      const hierarchy = [RoleEnum.USER, RoleEnum.ADMIN, RoleEnum.MANAGER];

      return (
        currentRole !== undefined &&
        hierarchy.indexOf(currentRole) >= hierarchy.indexOf(level)
      );
    },
  };
};
