"use client";

/**
 * PostHog Analytics Provider
 *
 * Initializes PostHog on the client side and provides page view tracking.
 * Uses window.location instead of useSearchParams to avoid Suspense boundary requirements.
 */

import { useEffect } from "react";
import { usePathname } from "next/navigation";
import { initPostHog, trackPageView } from "@/lib/posthog-tracker";

export function PostHogProvider({ children }: { children: React.ReactNode }) {
  const pathname = usePathname();

  useEffect(() => {
    // Initialize PostHog on mount
    initPostHog();
  }, []);

  useEffect(() => {
    // Track page views on route changes
    // Use window.location.search instead of useSearchParams to avoid Suspense requirement
    if (pathname && typeof window !== "undefined") {
      const url = pathname + window.location.search;
      trackPageView(url, {
        referrer: document.referrer,
      });
    }
  }, [pathname]);

  return <>{children}</>;
}
