'use client';

import { useEffect } from 'react';
import { usePathname, useSearchParams } from 'next/navigation';
import { useAnalytics } from './useAnalytics';

/**
 * AnalyticsProvider Component
 *
 * Automatically tracks pageviews on client-side route changes.
 * This component handles navigation events for the Umami analytics service.
 */
export default function AnalyticsProvider() {
  const pathname = usePathname();
  const searchParams = useSearchParams();
  const { trackPageview } = useAnalytics();

  useEffect(() => {
    if (!pathname) return;

    const url = `${pathname}${searchParams?.size ? `?${searchParams.toString()}` : ''}`;

    // Track pageview with the full URL
    // The service will relay this to our internal proxy which injects the Website ID
    trackPageview(url);
  }, [pathname, searchParams, trackPageview]);

  return null;
}
