import React from "react";
import { Navigate, Outlet } from "react-router-dom";
import { useAuth } from "../AuthContext";

const RoleRoute = ({ allowedRoles }) => {
  const { isAuthenticated, role } = useAuth();
  const user = JSON.parse(localStorage.getItem("user"));

  if (!isAuthenticated) {
    return <Navigate to="/login" />;
  }

  if (allowedRoles && !allowedRoles.includes(user.role_name)) {
    return <Navigate to="/" />;
  }

  return <Outlet />;
};

export default RoleRoute;
