// import { getApp } from '@react-native-firebase/app';
// import AsyncStorage from '@react-native-async-storage/async-storage';
// import { PermissionsAndroid, Platform } from 'react-native';
// import {
//   getMessaging,
//   getToken,
//   onMessage,
//   onNotificationOpenedApp,
//   getInitialNotification,
//   requestPermission,
//   AuthorizationStatus
// } from '@react-native-firebase/messaging';

// export async function requestUserPermission() {

//   if (Platform.OS === 'android') {
//     try {
//       const granted = await PermissionsAndroid.check(
//         PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS
//       );

//       if (!granted) {
//         const result = await PermissionsAndroid.request(
//           PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS,
//           {
//             title: 'Notification Permission',
//             message: 'This app needs access to notifications to send you updates.',
//             buttonNeutral: 'Ask Me Later',
//             buttonNegative: 'Cancel',
//             buttonPositive: 'OK',
//           }
//         );

//         if (result !== PermissionsAndroid.RESULTS.GRANTED) {
//           return;
//         }
//       }
//     } catch (err) {
//       return;
//     }
//   }

//   // iOS Firebase messaging permission
//   try {
//     const app = getApp();
//     const messaging = getMessaging(app);
//     const authStatus = await requestPermission(messaging);

//     const enabled =
//       authStatus === AuthorizationStatus.AUTHORIZED ||
//       authStatus === AuthorizationStatus.PROVISIONAL;

//     if (enabled) {
//       await getFcmToken();
//     } else {
//     }
//   } catch (e) {
//   }
// }

// export const getFcmToken = async () => {

//   try {
//     let fcmToken = await AsyncStorage.getItem('fcmToken');

//     if (!fcmToken) {
//       const app = getApp();
//       const messaging = getMessaging(app);
//       fcmToken = await getToken(messaging);

//       if (fcmToken) {
//         await AsyncStorage.setItem('fcmToken', fcmToken);
//         return fcmToken;
//       } else {
//         return null;
//       }
//     }

//     return fcmToken;
//   } catch (error) {
//     return null;
//   }
// };

// export const NotificationServices = () => {

//   const app = getApp();
//   const messaging = getMessaging(app);

//   onNotificationOpenedApp(messaging, remoteMessage => {

//   });

//   onMessage(messaging, async remoteMessage => {
//   });

//   getInitialNotification(messaging).then(remoteMessage => {
//     if (remoteMessage) {

//     }
//   });
// };

import messaging from '@react-native-firebase/messaging'
import { Platform } from 'react-native'
import PushNotification from 'react-native-push-notification'

// 🔹 Ask for notification permissions
export async function requestUserPermission() {
  const authStatus = await messaging().requestPermission()
  const enabled =
    authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
    authStatus === messaging.AuthorizationStatus.PROVISIONAL

  if (enabled) {
    await getFCMToken()
  } else {
  }
}

// 🔹 Get FCM token
async function getFCMToken() {
  try {
    const fcmToken = await messaging().getToken()
    if (fcmToken) {
    } else {
    }
  } catch (e) {}
}

// 🔹 Handle notification services
export function NotificationServices() {
  // ✅ Create a local channel (Android only, must match with FCM channelId if you send custom)
  if (Platform.OS === 'android') {
    PushNotification.createChannel(
      {
        channelId: 'default-channel',
        channelName: 'Default Channel',
        importance: 4, // High importance
        vibrate: true,
        playSound: true,
        soundName: 'notification_sound.mp3',
      },
      created => console.log(`📢 Notification channel created: '${created}'`),
    )
  }

  // ✅ Foreground notifications
  const unsubscribe = messaging().onMessage(async remoteMessage => {
    PushNotification.localNotification({
      channelId: 'default-channel',
      title: remoteMessage.notification?.title || 'New Message',
      message: remoteMessage.notification?.body || '',
      bigPictureUrl: remoteMessage.notification?.android?.imageUrl,
      smallIcon: 'ic_notification',
      playSound: true,
      soundName: 'default',
      priority: 'high',
    })
  })

  return unsubscribe
}

messaging().setBackgroundMessageHandler(async remoteMessage => {})
