"use client";

import { useEffect } from "react";
import { trackPurchase } from "@/lib/analytics";

type AnalyticsPurchaseScriptProps = {
  orderId: string;
  totalAmount: number;
  currency?: string;
  items: Array<{
    productId: string;
    productName: string;
    quantity: number;
    price: number;
    category?: string;
  }>;
};

export function AnalyticsPurchaseScript({
  orderId,
  totalAmount,
  currency = "EUR",
  items,
}: AnalyticsPurchaseScriptProps) {
  useEffect(() => {
    if (window.gtag) {
      trackPurchase({
        transaction_id: orderId,
        value: totalAmount / 100,
        currency,
        items: items.map((item) => ({
          item_id: item.productId,
          item_name: item.productName,
          quantity: item.quantity,
          price: item.price / 100,
          item_category: item.category,
        })),
      });
    }
  }, [orderId, totalAmount, currency, items]);

  return null;
}
