"use client";

import { useAuth } from "@/hooks/useAuth";
import { useRouter } from "next/navigation";
import { useEffect } from "react";

export function AuthGuard({ children }: { children: React.ReactNode }) {
  const { isAuthenticated, loading, login } = useAuth();
  const router = useRouter();

  useEffect(() => {
    if (!loading && !isAuthenticated) {
      login(typeof window !== "undefined" ? window.location.href : undefined);
    }
  }, [loading, isAuthenticated, login]);

  if (loading) {
    return <p className="text-sm text-gray-500">در حال بررسی احراز هویت...</p>;
  }

  if (!isAuthenticated) {
    return <p className="text-sm text-gray-500">در حال هدایت به صفحه ورود...</p>;
  }

  return <>{children}</>;
}
