import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import './index.css'
import * as Sentry from '@sentry/react'

// Production crash capture + Session Replay. This is SEPARATE from and additive
// to the existing reportError→/api/log-error email pipeline (main.jsx window
// listeners + ErrorBoundary): Sentry gives a real (source-mapped) stack and a
// replay of what the user did, which the email path can't. Default integrations
// stay on, so unhandled errors and promise rejections are captured too.
Sentry.init({
  dsn: 'https://3fd908033e9e20bb96badb65bbc31db5@o4511728847749120.ingest.us.sentry.io/4511728856989696',
  integrations: [Sentry.replayIntegration()],
  replaysSessionSampleRate: 0.1,   // 10% of ordinary sessions
  replaysOnErrorSampleRate: 1.0,   // 100% of sessions that hit an error
})

import { initDevClock } from './lib/devClock'
initDevClock() // dev-only fake-today override via ?asOf=YYYY-MM-DD, must run before anything reads Date
import './i18n' // initialize i18next + set <html dir/lang> before first paint
import App from './App.jsx'
import { AuthProvider } from './lib/auth'
import ErrorBoundary from './components/ErrorBoundary'
import { reportError } from './lib/reportError'

// Catch errors outside React's render tree (event handlers, async, promises).
if (typeof window !== 'undefined') {
  window.addEventListener('error', (e) =>
    reportError(e.message || 'window error', { stack: e.error?.stack }));
  window.addEventListener('unhandledrejection', (e) =>
    reportError('unhandledrejection: ' + (e.reason?.message || e.reason), { stack: e.reason?.stack }));
}

createRoot(document.getElementById('root')).render(
  <StrictMode>
    <ErrorBoundary>
      <AuthProvider>
        <App />
      </AuthProvider>
    </ErrorBoundary>
  </StrictMode>,
)
