// components/AnalyticsProvider.js - VERSIÓN SIMPLIFICADA
import { useEffect } from 'react';
import { useRouter } from 'next/router';
import analyticsService from '@/services/analyticsService';

const AnalyticsProvider = ({ children }) => {
  const router = useRouter();

  useEffect(() => {
    if (!router.isReady) return;

    try {
      // 1. Inicializar tracking del router para navegaciones SPA
      analyticsService.initializeRouterTracking(router);

      // 2. Registrar vista inicial usando el método original
      analyticsService.trackPageView(router.asPath, {
        initialRoute: router.asPath,
        query: router.query,
        isInitialLoad: true
      });

          } catch (error) {
      console.error('❌ Error initializing AnalyticsProvider:', error);
    }

    return () => {
      try {
        analyticsService.destroy();
      } catch (error) {
        console.error('❌ Error destroying analytics:', error);
      }
    };
  }, [router.isReady]);

  return children;
};

export default AnalyticsProvider;
