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 `/login`. Mirror of
 * apps/web's `RequireAuth`.
 */
export function RequireAdmin({ children }: { children: ReactNode }) {
  const { admin, isLoading } = useAdminAuth();

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