import type { ReactNode } from "react";
import { Navigate } from "react-router-dom";
import { useAdminAuth } from "./AdminAuthContext";

/**
 * Route guard for every admin page. Renders nothing while the initial
 * `GET /admin/auth/me` check is pending (avoids a flash-then-redirect);
 * once resolved, renders `children` or redirects to `/admin/login`. Mirror
 * of `RequireAuth` (`features/auth/`), but keyed to the separate admin
 * session (`admin_session` cookie), not the user one.
 */
export function RequireAdmin({ children }: { children: ReactNode }) {
  const { admin, isLoading } = useAdminAuth();

  if (isLoading) {
    return null;
  }
  if (!admin) {
    return <Navigate to="/admin/login" replace />;
  }
  return <>{children}</>;
}
