"use client";

import Script from "next/script";

interface GoogleAnalyticsProps {
  measurementId?: string;
}

/**
 * Google Analytics Integration Component
 * 
 * Loads Google Analytics (GA4) tracking script dynamically based on measurement ID
 * received from the funnel configuration.
 * 
 * Page views are tracked by PageViewTracker component on route changes.
 * 
 * @param measurementId - Google Analytics Measurement ID (e.g., "G-XXXXXXXXXX")
 */
export function GoogleAnalytics({ measurementId }: GoogleAnalyticsProps) {
  if (!measurementId) {
    return null;
  }

  return (
    <>
      <Script
        id="google-analytics"
        strategy="afterInteractive"
        src={`https://www.googletagmanager.com/gtag/js?id=${measurementId}`}
      />
      <Script
        id="google-analytics-init"
        strategy="afterInteractive"
        dangerouslySetInnerHTML={{
          __html: `
            window.dataLayer = window.dataLayer || [];
            function gtag(){dataLayer.push(arguments);}
            gtag('js', new Date());
            gtag('config', '${measurementId}', {
              send_page_view: false
            });
          `,
        }}
      />
    </>
  );
}
