import { TourAnalyticsEvent } from '../types';

export const trackTourEvent = (event: TourAnalyticsEvent): void => {
  try {
    const demoSessionId = localStorage.getItem('demo_session_id');

    const enrichedEvent = {
      ...event,
      sessionId: demoSessionId || undefined,
    };

    console.log('[Tour Analytics]', enrichedEvent);

    if (window.gtag) {
      window.gtag('event', event.event, {
        event_category: 'demo_tour',
        event_label: event.step !== undefined ? `step_${event.step}` : undefined,
        session_id: demoSessionId,
      });
    }

    if (window.plausible) {
      window.plausible(event.event, {
        props: {
          step: event.step,
          session_id: demoSessionId,
        },
      });
    }
  } catch (error) {
    console.error('Error tracking tour event:', error);
  }
};

declare global {
  interface Window {
    gtag?: (...args: any[]) => void;
    plausible?: (event: string, options?: { props?: Record<string, any> }) => void;
  }
}
