import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App.jsx';
import './index.css';

// Apply saved theme immediately to avoid flash of wrong theme
const savedTheme = localStorage.getItem('jama-theme') || 'light';
document.documentElement.setAttribute('data-theme', savedTheme);

// Register service worker
if ('serviceWorker' in navigator) {
  window.addEventListener('load', () => {
    navigator.serviceWorker.register('/sw.js')
      .then(reg => console.log('[SW] Registered, scope:', reg.scope))
      .catch(err => console.error('[SW] Registration failed:', err));
  });
}

// Clear badge count when user focuses the app
window.addEventListener('focus', () => {
  if (navigator.clearAppBadge) navigator.clearAppBadge().catch(() => {});
  navigator.serviceWorker?.controller?.postMessage({ type: 'CLEAR_BADGE' });
});

ReactDOM.createRoot(document.getElementById('root')).render(
  <React.StrictMode>
    <App />
  </React.StrictMode>
);
