"use client";

import { useSession } from "next-auth/react";
import { usePathname, useRouter } from "next/navigation";
import { useEffect, useState } from "react";

export function AuthCheck({ children }: { children: React.ReactNode }) {
  const { data: session, status } = useSession();
  const pathname = usePathname();
  const router = useRouter();
  const [isRedirecting, setIsRedirecting] = useState(false);

  useEffect(() => {
    if (status === "unauthenticated" && pathname !== "/signin" && !isRedirecting) {
      setIsRedirecting(true);
      router.push("/signin");
    }
  }, [status, router, pathname, isRedirecting]);

  if (status === "loading") {
    return <div>Chargement...</div>;
  }

  if (status === "unauthenticated" && pathname !== "/signin") {
    return null;
  }

  return <>{children}</>;
} 