import { initializeFaro } from '@grafana/faro-react-native';

let initialized = false;

/**
 * Initializes Grafana Faro RUM for the React Native app.
 *
 * Telemetry (crashes, JS errors, console, app-start, memory/ANR vitals,
 * session + screen tracking) is sent OTLP/HTTP to the Alloy collector.
 *
 * NOTE: `@grafana/faro-react-native` ships a native module, so it requires a
 * custom dev build / EAS build (not Expo Go). Initialization is wrapped in a
 * try/catch so a collector outage never crashes the app.
 */
export function initFaro(): void {
  if (initialized) return;
  initialized = true;

  try {
    const url =
      process.env.EXPO_PUBLIC_FARO_URL || 'http://grafana.hacecalor.net:4318';

    initializeFaro({
      app: {
        name: 'farmafinder-mobile',
        version: '1.0.0',
        environment: __DEV__ ? 'development' : 'production',
      },
      url,
      sessionTracking: { enabled: true },
      // Crash/JS error/console capture are on by default.
    });
  } catch (err) {
    console.warn('[faro] initialization failed:', err);
  }
}
