// Service Worker pour Firebase Cloud Messaging (Web Push)
importScripts(
  'https://www.gstatic.com/firebasejs/10.7.1/firebase-app-compat.js',
  'https://www.gstatic.com/firebasejs/10.7.1/firebase-messaging-compat.js'
);

/** @type {ServiceWorkerGlobalScope} */
const swSelf = /** @type {unknown} */ (globalThis);

// Configuration Firebase
firebase.initializeApp({
  apiKey: "AIzaSyB_T4whbgp5_AgGX2m_SjJPh7Ui2e1T5S0",
  authDomain: "faildaily-8efdd.firebaseapp.com",
  projectId: "faildaily-8efdd",
  storageBucket: "faildaily-8efdd.firebasestorage.app",
  messagingSenderId: "903740944900",
  appId: "1:903740944900:web:8fad55851ecbdddbb7fb51",
  measurementId: "G-3MW14JBHJB"
});

const messaging = firebase.messaging();

// Gérer les notifications en arrière-plan
messaging.onBackgroundMessage((payload) => {
  console.log('📬 Notification reçue en arrière-plan:', payload);
  
  const notificationTitle = payload.notification?.title || 'FailDaily';
  const notificationOptions = {
    body: payload.notification?.body || 'Nouvelle notification',
    icon: '/assets/icon.png',
    badge: '/assets/badge.png',
    data: payload.data
  };

  return swSelf.registration.showNotification(notificationTitle, notificationOptions);
});

// Gérer les clics sur les notifications
globalThis.addEventListener('notificationclick', (event) => {
  console.log('🖱️ Clic sur notification:', event);
  event.notification.close();
  
  // Ouvrir l'application
  event.waitUntil(
    clients.openWindow('https://faildaily.com')
  );
});
