// Service Worker for Hera's Planner — 后台提醒通知
self.addEventListener('install', () => self.skipWaiting());
self.addEventListener('activate', () => self.clients.claim());

self.addEventListener('message', event => {
  if (event.data && event.data.type === 'SHOW_NOTIFICATION') {
    self.registration.showNotification(event.data.title, {
      body: event.data.body,
      icon: 'icon-180.png',
      badge: 'icon-180.png',
      requireInteraction: true,
      tag: event.data.tag || 'planner-reminder',
    });
  }
});

self.addEventListener('notificationclick', event => {
  event.notification.close();
  event.waitUntil(
    self.clients.matchAll({ type: 'window' }).then(clients => {
      if (clients.length > 0) { clients[0].focus(); }
      else { self.clients.openWindow('/'); }
    })
  );
});
