'use client';

import { useEffect } from 'react';
import { usePathname, useSearchParams } from 'next/navigation';
import { getAppServices } from '@/lib/services/create-services';

/**
 * AnalyticsProvider Component
 *
 * Automatically tracks pageviews on client-side route changes.
 * This component handles navigation events for the Umami analytics service.
 *
 * Note: Website ID is now centrally managed on the server side via a proxy,
 * so it's no longer needed as a prop here.
 */
export default function AnalyticsProvider() {
  const pathname = usePathname();
  const searchParams = useSearchParams();

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

    const services = getAppServices();
    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
    services.analytics.trackPageview(url);

    // Services like logger are already sub-initialized in getAppServices()
    // so we don't need to log here manually.
  }, [pathname, searchParams]);

  return null;
}
