import {StrictMode} from "react"
import {createRoot} from "react-dom/client"
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"

import "@/index.css"
import {ThemeProvider} from "@/components/theme-provider.tsx"
import { performMaintenance } from "@/lib/db/maintenance";
import { applyThemePreset } from "@/components/theme/ThemePresetSelector";
import App from "@/App.tsx";
import { initRum } from "@/rum-core";
import { RumUserContext } from "@/rum";

initRum();

// Apply saved theme preset on startup
const PRESET_KEY = "app-theme-preset";
const savedPreset = localStorage.getItem(PRESET_KEY) || "soft-mono";
applyThemePreset(savedPreset);

// Trigger background maintenance on startup
performMaintenance().catch(console.error);

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

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