import React from 'react'
import ReactDOM from 'react-dom/client'
import * as Sentry from '@sentry/react'
import App from './App'
import './index.css'
import { initAnalytics } from './services/analytics'

// ── Sentry — Error monitoring (activates only when DSN is set) ────────────────
const SENTRY_DSN = import.meta.env.VITE_SENTRY_DSN
if (SENTRY_DSN) {
  Sentry.init({
    dsn: SENTRY_DSN,
    environment: import.meta.env.MODE,   // 'production' | 'development'
    release: import.meta.env.VITE_APP_VERSION || '1.0.0',
    integrations: [
      Sentry.browserTracingIntegration(),
      Sentry.replayIntegration({
        maskAllText: false,
        blockAllMedia: false,
      }),
    ],
    tracesSampleRate: import.meta.env.PROD ? 0.2 : 0,   // 20% in prod, 0% in dev
    replaysSessionSampleRate: 0.05,                       // 5% of sessions
    replaysOnErrorSampleRate: 1.0,                        // 100% of sessions with errors
    beforeSend(event) {
      // Strip sensitive data from breadcrumbs
      if (event.request?.cookies) delete event.request.cookies
      return event
    },
  })
}

// ── PostHog — Product analytics ───────────────────────────────────────────────
initAnalytics()

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

