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

function ProtectedRoute({ children, adminOnly }) {
  const { userLogin } = useContext(AuthContext);

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

  if (adminOnly && userLogin.role !== "admin") {
    return <Navigate to="/dashboard-user" replace />;
  }

  return children;
}

export default ProtectedRoute;
