import { AuthProvider, ThemeProvider, UnsavedWarnProvider } from "@/lib/hooks";
import { Toaster, TooltipProvider } from "@/ui";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
import { Suspense } from "react";
import "react-toastify/dist/ReactToastify.css";
import { Routes } from "./Routes";
import { LoadingOverlay, TailwindIndicator } from "./components";
import { createAxiosAuthActions, createAxiosDataProvider } from "./lib/axios";
import { DataSourceProvider } from "./lib/hooks";

function App() {
  const queryClient = new QueryClient({
    defaultOptions: {
      queries: {
        retry: false,
        staleTime: 10000, // Specify a staleTime to only fetch when the data is older than a certain amount of time
      },
    },
  });

  return (
    <QueryClientProvider client={queryClient}>
      <DataSourceProvider
        dataSource={createAxiosDataProvider(process.env.API_URL || import.meta.env.VITE_API_URL)}
      >
        <AuthProvider
          authActions={createAxiosAuthActions(process.env.API_URL || import.meta.env.VITE_API_URL)}
        >
          <ThemeProvider defaultTheme='light' storageKey='vite-ui-theme'>
            <TooltipProvider delayDuration={0}>
              <UnsavedWarnProvider>
                <Suspense fallback={<LoadingOverlay />}>
                  <Routes />

                  <Toaster />
                </Suspense>
              </UnsavedWarnProvider>
            </TooltipProvider>
            <TailwindIndicator />
            <ReactQueryDevtools initialIsOpen={false} />
          </ThemeProvider>
        </AuthProvider>
      </DataSourceProvider>
    </QueryClientProvider>
  );
}

export default App;
