import { Navigate } from "react-router-dom";
import { useAuth } from "../context/auth_context";
import { UserRole } from "../constants/roles";
import { AdminHomePage } from "../../presentation/admin/panel/admin_home_page";
import { SuperAdminHomePage } from "../../presentation/superadmin/panel/super_admin_home_page";

// Component to handle protected routes based on user role
export const ProtectedRoute = () => {
  const { user, logout } = useAuth();

  // If no user is authenticated, redirect to login page
  if (!user) {
    return <Navigate to="/login" replace={true} />;
  } else {
    // Redirect based on user role
    if (user.role === UserRole.ADMIN) {
      return <AdminHomePage />;
    } else if (user.role === UserRole.SUPERADMIN) {
      return <SuperAdminHomePage />;
    } else {
      // Logout and redirect to login if user role is not recognized
      logout();
      return <Navigate to="/login" replace={true} />;
    }
  }
};
