import { Navigate, Outlet } from 'react-router-dom';
import useAuth from '../roles/guest/auth/hooks/useAuth';

const ProtectedRoute = ({ role }) => {
  const { user, getUserFromToken } = useAuth();
  // console.log(user);
  if (!user) {
    getUserFromToken();
  }

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

  if (user.ROLE !== role) {
    let path = "";
    if (user.role == 'student') {
      path = '/learning';
    }else if (user.role == 'teacher') {
      path = '/teacher';
    }else if (user.role == 'admin') {
      path = '/admin';
    }
    return <Navigate to={`/${path}`} />;
  }

  return <Outlet />;
};

export default ProtectedRoute;
