import 'package:firebase_messaging/firebase_messaging.dart';
import '../utils/logger.dart';

class PushNotificationService {
  static final FirebaseMessaging _fcm = FirebaseMessaging.instance;

  static Future<void> initialize() async {
    // 1. Request permissions (iOS)
    NotificationSettings settings = await _fcm.requestPermission(
      alert: true,
      badge: true,
      sound: true,
    );

    AppLogger.print('User granted permission: ${settings.authorizationStatus}');

    // 2. Handle foreground messages
    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      AppLogger.print('Received foreground message: ${message.notification?.title}');
      // You can show a local notification here if needed
    });
  }

  static Future<String?> getToken() async {
    try {
      String? token = await _fcm.getToken();
      AppLogger.print('FCM Token: $token');
      return token;
    } catch (e) {
      AppLogger.error('Failed to get FCM token', e);
      return null;
    }
  }
}
