/**
 * React Hook for Analytics
 * Provides easy access to analytics functions in components
 */

import { useEffect } from 'react'
import { useLocation } from 'react-router-dom'
import {
  trackPageView,
  captureUTMParams,
  getAllTrackingParams,
  trackSignup,
  trackTrialStart,
  trackPurchase,
  trackEmailConnected,
  setUserId,
  type TrackingParams,
} from '@/lib/analytics'

/**
 * Hook to automatically track page views on route changes
 */
export function usePageTracking() {
  const location = useLocation()

  useEffect(() => {
    // Capture UTM parameters on every navigation
    captureUTMParams()

    // Track page view
    trackPageView(location.pathname)
  }, [location])
}

/**
 * Hook to get tracking parameters
 */
export function useTrackingParams(): TrackingParams {
  return getAllTrackingParams()
}

/**
 * Export analytics functions for use in components
 */
export const analytics = {
  trackSignup,
  trackTrialStart,
  trackPurchase,
  trackEmailConnected,
  setUserId,
}

export default usePageTracking
