import { PushNotifications } from '@capacitor/push-notifications';

export async function registerPush() {
  // Kolla om vi har tillstånd
  let permStatus = await PushNotifications.checkPermissions();

  if (permStatus.receive !== 'granted') {
    permStatus = await PushNotifications.requestPermissions();
  }

  if (permStatus.receive === 'granted') {
    await PushNotifications.register();
  }

  // Lyssna på token
  PushNotifications.addListener('registration', (token) => {
    console.log('Push registration success, token: ' + token.value);

    // Skicka token till servern
    fetch('https://kran.hansengfeldt.se/api/register_token.php', {
      method: 'POST',
      body: JSON.stringify({ token: token.value }),
      headers: { 'Content-Type': 'application/json' }
    });
  });

  // Lyssna på push-meddelanden
  PushNotifications.addListener('pushNotificationReceived', (notification) => {
    console.log('Push received: ', notification);
  });
}
