"use client";

import { useCallback } from "react";
import { getDefaultAnalytics } from "../../utils/analytics";
import type { AnalyticsEventName } from "./analytics-events";

/**
 * Custom hook for tracking analytics events.
 * Wraps the analytics service for easy use in components.
 */
export function useAnalytics() {
  const trackEvent = useCallback(
    (
      eventName: string | AnalyticsEventName,
      properties?: Record<string, any>,
    ) => {
      const analytics = getDefaultAnalytics();
      analytics.trackEvent(eventName, properties);

      if (process.env.NODE_ENV === "development") {
        console.debug("[Analytics] Tracked event:", eventName, properties);
      }
    },
    [],
  );

  const trackPageview = useCallback((url?: string) => {
    const analytics = getDefaultAnalytics();
    analytics.page(url || window.location.pathname);

    if (process.env.NODE_ENV === "development") {
      console.debug("[Analytics] Tracked pageview:", url ?? "current location");
    }
  }, []);

  return {
    trackEvent,
    trackPageview,
  };
}
