"use client";

import { type ReactNode,useEffect } from "react";

import { fetchMe } from "@/entities/user/actions";
import { analyticsService } from "@/services/analytics";

interface AnalyticsProviderProps {
  children: ReactNode;
}

/**
 * Analytics Provider Component
 *
 * Initializes analytics service with user data when the app loads.
 * Provides analytics context to the entire application.
 */
export function AnalyticsProvider({ children }: AnalyticsProviderProps) {
  useEffect(() => {
    // Initialize analytics service with server-side data
    const initAnalytics = async () => {
      try {
        const response = await fetchMe();
        if (response.data && !response.error) {
          await analyticsService.initialize(response.data.user);
        } else {
          await analyticsService.initialize(null);
        }
      } catch {
        await analyticsService.initialize(null);
      }
    };

    initAnalytics();
  }, []);

  return <>{children}</>;
}
