import React from 'react'
import ReactDOM from 'react-dom/client'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
import App from './App'
import './index.css'

// Configura React Query con retry intelligente
const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      staleTime: 30 * 1000, // 30 secondi
      retry: (failureCount, error) => {
        // Non ritentare per 401/403/404
        if (
          typeof error === 'object' &&
          error !== null &&
          'response' in error
        ) {
          const status = (error as { response?: { status?: number } }).response?.status
          if (status === 401 || status === 403 || status === 404) return false
        }
        return failureCount < 2
      },
    },
    mutations: {
      retry: false,
    },
  },
})

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