'use client';

import Script from 'next/script';
import { usePathname } from 'next/navigation';

export default function PlausibleAnalytics() {
  const pathname = usePathname();

  // Get Plausible configuration from environment variables
  const plausibleUrl = process.env.NEXT_PUBLIC_PLAUSIBLE_URL;
  const plausibleDomain = process.env.NEXT_PUBLIC_PLAUSIBLE_DOMAIN;

  // Don't render on admin pages or if configuration is missing
  const isAdminPage = pathname?.startsWith('/admin');
  
  if (isAdminPage || !plausibleUrl || !plausibleDomain) {
    return null;
  }

  return (
    <Script
      defer
      data-domain={plausibleDomain}
      src={`${plausibleUrl}/js/script.js`}
      strategy="afterInteractive"
    />
  );
}
