import 'package:flutter/foundation.dart' show debugPrint;

import 'nexo_notification_client.dart';

/// Owns the Nexo notification host/plugin responsibility boundary.
///
/// The integration:
///   - initializes the push client exactly once,
///   - registers navigation callbacks in one place (not from a widget build),
///   - exposes the notification stream for app-level consumers.
class NexoNotificationHostIntegration {
  final NexoNotificationClient pushClient;

  bool _initialized = false;

  NexoNotificationHostIntegration({required this.pushClient});

  Stream<Map<String, dynamic>> get onNotification => pushClient.onNotification;

  bool get isInitialized => _initialized;

  /// Initialize plugin, and register navigation callbacks.
  /// Safe to call once at bootstrap; subsequent calls are no-ops.
  ///
  /// [onNavigateToChannel] / [onNavigateToThread] are optional. When omitted,
  /// the integration installs debug-logging fallbacks so the navigation path
  /// remains observable without forcing the caller to wire routing.
  Future<void> initialize({
    void Function(String serverUrl, String channelId)? onNavigateToChannel,
    void Function(String serverUrl, String rootId)? onNavigateToThread,
  }) async {
    if (_initialized) return;
    _initialized = true;

    await pushClient.initialize();

    pushClient.onNavigateToChannel =
        onNavigateToChannel ??
        (serverUrl, channelId) {
          debugPrint(
            '[NexoNotification] Navigate to channel: $channelId on $serverUrl',
          );
        };
    pushClient.onNavigateToThread =
        onNavigateToThread ??
        (serverUrl, rootId) {
          debugPrint(
            '[NexoNotification] Navigate to thread: $rootId on $serverUrl',
          );
        };
  }
}
