import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { BrowserRouter, Navigate, Route, Routes } from 'react-router-dom';
import { AppShell } from './layout/AppShell';
import { AuthGate } from './auth/AuthGate';
import { RoutePage } from './routing/RouteBoundary';
import { RoutePages } from './routing/routeModules';
import { ROUTER_FUTURE } from './routing/routerConfig';

const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      staleTime: 15_000,
      gcTime: 5 * 60_000,
      retry: 1,
      refetchOnWindowFocus: false
    }
  }
});

export function AppV2() {
  return (
    <QueryClientProvider client={queryClient}>
      <AuthGate><BrowserRouter future={ROUTER_FUTURE}>
        <Routes>
          <Route element={<AppShell />}>
            <Route index element={<Navigate to="/monitor" replace />} />
            <Route path="/monitor" element={<RoutePage page={RoutePages.Monitor} label="全局监控" />} />
            <Route path="/vehicles/:vin?" element={<RoutePage page={RoutePages.Vehicles} label="车辆查询" />} />
            <Route path="/tracks" element={<RoutePage page={RoutePages.Tracks} label="轨迹回放" />} />
            <Route path="/history" element={<RoutePage page={RoutePages.History} label="历史数据" />} />
            <Route path="/statistics" element={<RoutePage page={RoutePages.Statistics} label="里程查询" />} />
            <Route path="/access" element={<RoutePage page={RoutePages.Access} label="接入管理" />} />
            <Route path="/alerts/*" element={<RoutePage page={RoutePages.Alerts} label="告警中心" />} />
            <Route path="/operations" element={<RoutePage page={RoutePages.Operations} label="运维质量" />} />
            <Route path="*" element={<Navigate to="/monitor" replace />} />
          </Route>
        </Routes>
      </BrowserRouter></AuthGate>
    </QueryClientProvider>
  );
}
