// Utility functions for tracking events with Umami and backend analytics
// This provides a centralized way to track events throughout the app

import { trackEvent as trackBackendEvent } from '../services/analyticsService';

/**
 * Track a custom event with Umami and backend analytics
 * @param eventName - Name of the event (max 50 chars)
 * @param eventData - Optional data object to attach to the event
 */
export const trackEvent = (eventName: string, eventData?: Record<string, any>) => {
  // Track with Umami if available
  if (typeof window !== 'undefined' && window.umami) {
    window.umami.track(eventName, eventData);
  }

  // Also track with backend
  trackBackendEvent({
    event_type: eventName,
    page: typeof window !== 'undefined' ? window.location.pathname : undefined,
    data: eventData,
  }).catch(err => {
    console.debug('Backend tracking failed:', err);
  });
};

/**
 * Track button clicks and interactions
 */
export const trackButtonClick = (buttonName: string, location?: string) => {
  const pagePath = location || (typeof window !== 'undefined' ? window.location.pathname : '/');
  
  // Track with standard event
  trackEvent('Button Click', {
    button: buttonName,
    location: pagePath,
  });

  // Also track as interaction for analytics
  trackBackendEvent({
    event_type: 'click',
    page: pagePath,
    element: buttonName,
  }).catch(err => {
    console.debug('Interaction tracking failed:', err);
  });
};

/**
 * Track form submissions
 */
export const trackFormSubmit = (formName: string, success: boolean = true) => {
  trackEvent('Form Submit', {
    form: formName,
    success,
  });
};

/**
 * Track navigation
 */
export const trackNavigation = (destination: string, source?: string) => {
  trackEvent('Navigation', {
    destination,
    source: source || window.location.pathname,
  });
};

/**
 * Track article views
 */
export const trackArticleView = (articleId: string | number, articleTitle: string) => {
  trackEvent('Article View', {
    id: articleId,
    title: articleTitle,
  });
};

/**
 * Track match views
 */
export const trackMatchView = (matchId: string | number) => {
  trackEvent('Match View', {
    id: matchId,
  });
};

/**
 * Track search queries
 */
export const trackSearch = (query: string, resultsCount?: number) => {
  trackEvent('Search', {
    query,
    results: resultsCount,
  });
};

/**
 * Track video plays
 */
export const trackVideoPlay = (videoId: string, videoTitle?: string) => {
  trackEvent('Video Play', {
    id: videoId,
    title: videoTitle,
  });
};

/**
 * Track newsletter subscriptions
 */
export const trackNewsletterSubscribe = (source?: string) => {
  trackEvent('Newsletter Subscribe', {
    source: source || window.location.pathname,
  });
};

/**
 * Track file downloads
 */
export const trackDownload = (fileName: string, fileType?: string) => {
  trackEvent('Download', {
    file: fileName,
    type: fileType,
  });
};

/**
 * Track social media shares
 */
export const trackSocialShare = (platform: string, url?: string) => {
  trackEvent('Social Share', {
    platform,
    url: url || window.location.href,
  });
};

/**
 * Track contact form submissions
 */
export const trackContactSubmit = (success: boolean = true) => {
  trackEvent('Contact Form', {
    success,
  });
};

// Extend window interface
declare global {
  interface Window {
    umami?: {
      track: (eventName: string, eventData?: Record<string, any>) => void;
    };
  }
}
