import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { Toaster } from '@/components/ui/toaster'
import { useAuthStore } from '@/store/auth'
import LoginPage from '@/pages/Login'
import Dashboard from '@/pages/Dashboard'
import StoragePage from '@/pages/Storage'
import AlertsPage from '@/pages/Alerts'
import TapeLibrariesPage from '@/pages/TapeLibraries'
import VTLDetailPage from '@/pages/VTLDetail'
import ISCSITargetsPage from '@/pages/ISCSITargets'
import ISCSITargetDetailPage from '@/pages/ISCSITargetDetail'
import SystemPage from '@/pages/System'
import BackupManagementPage from '@/pages/BackupManagement'
import TerminalConsolePage from '@/pages/TerminalConsole'
import SharesPage from '@/pages/Shares'
import IAMPage from '@/pages/IAM'
import ProfilePage from '@/pages/Profile'
import MonitoringPage from '@/pages/Monitoring'
import ObjectStoragePage from '@/pages/ObjectStorage'
import SnapshotReplicationPage from '@/pages/SnapshotReplication'
import ShareShieldPage from '@/pages/ShareShield'
import Layout from '@/components/Layout'

// Create a client
const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      refetchOnWindowFocus: false,
      retry: 1,
      staleTime: 5 * 60 * 1000, // 5 minutes
    },
  },
})

// Protected Route Component
function ProtectedRoute({ children }: { children: React.ReactNode }) {
  const { isAuthenticated } = useAuthStore()
  
  if (!isAuthenticated) {
    return <Navigate to="/login" replace />
  }
  
  return <>{children}</>
}

function App() {
  return (
    <QueryClientProvider client={queryClient}>
      <BrowserRouter>
        <Routes>
          <Route path="/login" element={<LoginPage />} />
          <Route
            path="/"
            element={
              <ProtectedRoute>
                <Layout />
              </ProtectedRoute>
            }
          >
            <Route index element={<Dashboard />} />
            <Route path="storage" element={<StoragePage />} />
            <Route path="tape" element={<TapeLibrariesPage />} />
            <Route path="tape/vtl/:id" element={<VTLDetailPage />} />
            <Route path="iscsi" element={<ISCSITargetsPage />} />
            <Route path="iscsi/:id" element={<ISCSITargetDetailPage />} />
            <Route path="backup" element={<BackupManagementPage />} />
            <Route path="shares" element={<SharesPage />} />
            <Route path="terminal" element={<TerminalConsolePage />} />
            <Route path="object-storage" element={<ObjectStoragePage />} />
            <Route path="snapshots" element={<SnapshotReplicationPage />} />
            <Route path="share-shield" element={<ShareShieldPage />} />
            <Route path="monitoring" element={<MonitoringPage />} />
            <Route path="alerts" element={<AlertsPage />} />
            <Route path="system" element={<SystemPage />} />
            <Route path="iam" element={<IAMPage />} />
            <Route path="profile" element={<ProfilePage />} />
            <Route path="profile/:id" element={<ProfilePage />} />
          </Route>
        </Routes>
        <Toaster />
      </BrowserRouter>
    </QueryClientProvider>
  )
}

export default App

