import { useEffect, useRef } from "react";
import { useMutation } from "convex/react";
import { api } from "../../convex/_generated/api";

/** Legt Standard-Kategorien an, sobald die Auth-Session am Client aktiv ist. */
export function SeedInitializer() {
  const ensureSeeded = useMutation(api.users.ensureSeeded);
  const ran = useRef(false);

  useEffect(() => {
    if (ran.current) return;
    ran.current = true;
    void ensureSeeded({}).catch((error) => {
      ran.current = false;
      console.error("ensureSeeded fehlgeschlagen:", error);
    });
  }, [ensureSeeded]);

  return null;
}
