import 'dart:convert';
import 'package:http/http.dart' as http;

/// Analytics Service für Umami Integration
/// 
/// Umami ist eine datenschutzfreundliche Analytics-Lösung.
/// Konfiguriere deine Umami-URL und Website-ID in den statischen Variablen.
class AnalyticsService {
  // Ersetze mit deiner Umami-Instanz URL
  static const String umamiUrl = 'https://analytics.hyggecraftery.com';
  
  // Ersetze mit deiner Website-ID von Umami
  static const String websiteId = 'c43cd023-ff64-43c8-a42a-ac23d32366f6';
  
  // Optionale API-Key für Authentifizierung
  static const String? apiKey = null;

  /// Sendet ein Event an Umami
  static Future<void> trackEvent({
    required String eventName,
    Map<String, dynamic>? eventData,
    String? url,
    String? referrer,
  }) async {
    try {
      final uri = Uri.parse('$umamiUrl/api/send');
      
      final payload = {
        'website': websiteId,
        'hostname': 'hyggecraftery.com',
        'url': url ?? '/',
        'referrer': referrer,
        'name': eventName,
        'data': eventData ?? {},
      };

      final headers = <String, String>{
        'Content-Type': 'application/json',
      };

      if (apiKey != null) {
        headers['Authorization'] = 'Bearer $apiKey';
      }

      final response = await http.post(
        uri,
        headers: headers,
        body: json.encode(payload),
      );

      if (response.statusCode != 200) {
        print('Umami Tracking Fehler: ${response.statusCode}');
      }
    } catch (e) {
      // Fail silently - Analytics sollten die App nicht blockieren
      print('Analytics Fehler: $e');
    }
  }

  /// Trackt einen Page View
  static Future<void> trackPageView(String page) async {
    await trackEvent(
      eventName: 'pageview',
      url: page,
    );
  }

  /// Trackt eine Produktansicht
  static Future<void> trackProductView(int productId, String productName) async {
    await trackEvent(
      eventName: 'product_view',
      eventData: {
        'product_id': productId,
        'product_name': productName,
      },
    );
  }

  /// Trackt einen "Zum Warenkorb hinzufügen" Event
  static Future<void> trackAddToCart(int productId, String productName, double price) async {
    await trackEvent(
      eventName: 'add_to_cart',
      eventData: {
        'product_id': productId,
        'product_name': productName,
        'price': price,
      },
    );
  }

  /// Trackt einen Checkout-Start
  static Future<void> trackCheckoutStart(double total) async {
    await trackEvent(
      eventName: 'checkout_start',
      eventData: {
        'total': total,
      },
    );
  }

  /// Trackt eine erfolgreiche Bestellung
  static Future<void> trackPurchase(int orderId, double total) async {
    await trackEvent(
      eventName: 'purchase',
      eventData: {
        'order_id': orderId,
        'total': total,
      },
    );
  }

  /// Trackt eine Suche
  static Future<void> trackSearch(String searchQuery) async {
    await trackEvent(
      eventName: 'search',
      eventData: {
        'query': searchQuery,
      },
    );
  }

  /// Trackt eine Kategorie-Ansicht
  static Future<void> trackCategoryView(String categoryName) async {
    await trackEvent(
      eventName: 'category_view',
      eventData: {
        'category': categoryName,
      },
    );
  }
}

