import React from 'react';
import ReactDOM from 'react-dom/client';
import { BrowserRouter } from 'react-router-dom';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { Toaster } from 'sonner';
import App from './App';
import './index.css';

// Mark forms as submitted so CSS validation outlines only appear after a submit attempt.
// The attribute is cleared naturally when Sheet/Dialog forms unmount and remount.
document.addEventListener('submit', (e) => {
  if (e.target instanceof HTMLFormElement) {
    e.target.setAttribute('data-submitted', '');
  }
}, true);

const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      refetchOnWindowFocus: false,
      retry: 1,
      staleTime: 5 * 60 * 1000,
      gcTime: 30 * 60 * 1000, // keep cache 30 min to avoid skeleton flash on tab switch
    },
  },
});

ReactDOM.createRoot(document.getElementById('root')!).render(
  <React.StrictMode>
    <QueryClientProvider client={queryClient}>
      <BrowserRouter>
        <App />
        <Toaster
          position="top-right"
          theme="dark"
          style={{ pointerEvents: 'none' }}
          toastOptions={{ style: { pointerEvents: 'auto' } }}
        />
      </BrowserRouter>
    </QueryClientProvider>
  </React.StrictMode>
);
