import 'dart:async';

import 'package:firebase_analytics/firebase_analytics.dart';

import 'analytics_service.dart';

/// Routes typed events to Firebase / GA4. Wired in main() for release builds
/// only; debug builds keep the console logger so development traffic never
/// pollutes production analytics.
///
/// Lives in its own file so [AnalyticsService] and [DebugAnalyticsBackend]
/// stay free of the firebase_analytics dependency (and so unit tests of the
/// typed event surface never pull in platform channels).
class FirebaseAnalyticsBackend implements AnalyticsBackend {
  FirebaseAnalyticsBackend([FirebaseAnalytics? analytics])
      : _analytics = analytics ?? FirebaseAnalytics.instance;

  final FirebaseAnalytics _analytics;

  @override
  void logEvent(String name, Map<String, Object> params) {
    // Fire-and-forget: analytics must never block or break gameplay, and a
    // logging failure (offline, quota) is swallowed rather than surfaced.
    unawaited(
      _analytics
          .logEvent(
            name: name,
            parameters: params.isEmpty ? null : params,
          )
          .catchError((Object _) {}),
    );
  }
}
