import React from 'react'
import ReactDOM from 'react-dom/client'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { BrowserRouter } from 'react-router-dom'
import { Toaster } from 'react-hot-toast'

import App from './App.tsx'
import './index.css'

// Create a client for React Query
const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      retry: 3,
      retryDelay: attemptIndex => Math.min(1000 * 2 ** attemptIndex, 30000),
      staleTime: 5 * 60 * 1000, // 5 minutes
      gcTime: 10 * 60 * 1000, // 10 minutes
    },
    mutations: {
      retry: 1,
    },
  },
})

// Configure toast notifications for industrial environment
const toastConfig = {
  duration: 4000,
  position: 'top-center' as const,
  reverseOrder: false,
  gutter: 8,
  containerClassName: 'toast-container',
  containerStyle: {
    top: 20,
    left: 20,
    bottom: 20,
    right: 20,
  },
  toastOptions: {
    // Default options for all toasts
    className: 'toast-default',
    duration: 4000,
    style: {
      minHeight: '60px',
      fontSize: '16px',
      fontWeight: 'bold',
      borderRadius: '8px',
      padding: '16px 20px',
    },
    // Success toasts
    success: {
      style: {
        background: '#22c55e',
        color: 'white',
      },
      iconTheme: {
        primary: 'white',
        secondary: '#22c55e',
      },
    },
    // Error toasts
    error: {
      style: {
        background: '#ef4444',
        color: 'white',
      },
      iconTheme: {
        primary: 'white',
        secondary: '#ef4444',
      },
    },
    // Loading toasts
    loading: {
      style: {
        background: '#64748b',
        color: 'white',
      },
    },
  },
}

ReactDOM.createRoot(document.getElementById('root')!).render(
  <React.StrictMode>
    <QueryClientProvider client={queryClient}>
      <BrowserRouter>
        <App />
        <Toaster {...toastConfig} />
      </BrowserRouter>
    </QueryClientProvider>
  </React.StrictMode>,
)
