import {StrictMode} from "react"
import {createRoot} from "react-dom/client"
import {QueryClient, QueryClientProvider} from "@tanstack/react-query"
import "@/fonts.css"
import "@/index.css"
import {ThemeProvider} from "@/components/theme-provider.tsx"
import {Toaster} from "@/components/ui/sonner"
import {performMaintenance} from "@/lib/db/maintenance";
import {applyThemePreset} from "@/components/theme/ThemePresetSelector";
import {loadThemeVars} from "@/lib/theme-vars";
import App from "@/App.tsx";
import {initRum} from "@/rum-core";
import {RumUserContext} from "@/rum";

initRum();

const PRESET_KEY = "app-theme-preset";
const savedPreset = localStorage.getItem(PRESET_KEY) || "soft-mono";
applyThemePreset(savedPreset);
loadThemeVars();

performMaintenance().catch(console.error);

const queryClient = new QueryClient({
    defaultOptions: {
        queries: {
            staleTime: 5 * 60 * 1000,
            retry: 1,
        },
    },
});

createRoot(document.getElementById("root")!).render(
    <StrictMode>
        <QueryClientProvider client={queryClient}>
            <RumUserContext/>
            <ThemeProvider>
                <App/>
                <Toaster/>
            </ThemeProvider>
        </QueryClientProvider>
    </StrictMode>
)
