import 'package:laravel_echo_null/laravel_echo_null.dart';
import 'package:niogu_ecommerce_v1/core/constant/app_config.dart';
import 'package:niogu_ecommerce_v1/core/constant/app_temp.dart';
import 'package:pusher_client_socket/pusher_client_socket.dart' as PUSHER;

class EchoService {
  Echo<PUSHER.PusherClient, PusherChannel>? _echo;

  Future<void> init({
    required Function(String event, Map<String, dynamic> data) listener,
  }) async {
    if (_echo != null) return;
    
    final tenantId = AppTemp.TENANT_ID;

    _echo = Echo.pusher(
      AppConfig.REVERB_APP_KEY,
      authEndPoint: AppConfig.BASE_URL,
      authHeaders: () async => {},
      host: '192.168.137.1',
      wsPort: 8080,
      wssPort: 8080,
      encrypted: false,
      enableLogging: true,
      autoConnect: true,
    );

    _echo!.channel('tenant.$tenantId').listen('.data.batch.updated', (data) {
      listener('data.batch.updated', data);
    });
  }

  void disconnect() {
    _echo?.disconnect();
    _echo = null;
  }
}
