import { Suspense, lazy } from 'react'
import { Navigate } from 'react-router-dom'
import { BrowserRouter as Router, Routes, Route, Outlet } from 'react-router-dom'
import Layout from './components/Layout'
import { ToastContainer } from './components/Toast'
import { SetupGuard } from './components/SetupGuard'
import { LoadingOverlay } from './components/LoadingStates'
import RequireAuth from './components/RequireAuth'
import { AuthProvider } from './context/AuthContext'

// Lazy load pages for code splitting
const Dashboard = lazy(() => import('./pages/Dashboard'))
const ProxyHosts = lazy(() => import('./pages/ProxyHosts'))
const RemoteServers = lazy(() => import('./pages/RemoteServers'))
const ImportCaddy = lazy(() => import('./pages/ImportCaddy'))
const ImportCrowdSec = lazy(() => import('./pages/ImportCrowdSec'))
const Certificates = lazy(() => import('./pages/Certificates'))
const SystemSettings = lazy(() => import('./pages/SystemSettings'))
const SMTPSettings = lazy(() => import('./pages/SMTPSettings'))
const CrowdSecConfig = lazy(() => import('./pages/CrowdSecConfig'))
const Account = lazy(() => import('./pages/Account'))
const Settings = lazy(() => import('./pages/Settings'))
const Backups = lazy(() => import('./pages/Backups'))
const Tasks = lazy(() => import('./pages/Tasks'))
const Logs = lazy(() => import('./pages/Logs'))
const Domains = lazy(() => import('./pages/Domains'))
const Security = lazy(() => import('./pages/Security'))
const AccessLists = lazy(() => import('./pages/AccessLists'))
const WafConfig = lazy(() => import('./pages/WafConfig'))
const RateLimiting = lazy(() => import('./pages/RateLimiting'))
const Uptime = lazy(() => import('./pages/Uptime'))
const Notifications = lazy(() => import('./pages/Notifications'))
const UsersPage = lazy(() => import('./pages/UsersPage'))
const SecurityHeaders = lazy(() => import('./pages/SecurityHeaders'))
const Login = lazy(() => import('./pages/Login'))
const Setup = lazy(() => import('./pages/Setup'))
const AcceptInvite = lazy(() => import('./pages/AcceptInvite'))

export default function App() {
  return (
    <AuthProvider>
      <Router>
        <Suspense fallback={<LoadingOverlay message="Loading application..." />}>
          <Routes>
            <Route path="/login" element={<Login />} />
            <Route path="/setup" element={<Setup />} />
            <Route path="/accept-invite" element={<AcceptInvite />} />
            <Route path="/" element={
              <SetupGuard>
                <RequireAuth>
                  <Layout>
                    <Outlet />
                  </Layout>
                </RequireAuth>
              </SetupGuard>
            }>
              <Route index element={<Dashboard />} />
              <Route path="proxy-hosts" element={<ProxyHosts />} />
              <Route path="remote-servers" element={<RemoteServers />} />
              <Route path="domains" element={<Domains />} />
              <Route path="certificates" element={<Certificates />} />
              <Route path="security" element={<Security />} />
              <Route path="security/access-lists" element={<AccessLists />} />
              <Route path="security/crowdsec" element={<CrowdSecConfig />} />
              <Route path="security/rate-limiting" element={<RateLimiting />} />
              <Route path="security/waf" element={<WafConfig />} />
              <Route path="security/headers" element={<SecurityHeaders />} />
              <Route path="access-lists" element={<AccessLists />} />
              <Route path="uptime" element={<Uptime />} />
              <Route path="users" element={<UsersPage />} />
              <Route path="import" element={<Navigate to="/tasks/import/caddyfile" replace />} />

              {/* Settings Routes */}
              <Route path="settings" element={<Settings />}>
                <Route index element={<SystemSettings />} />
                <Route path="system" element={<SystemSettings />} />
                <Route path="notifications" element={<Notifications />} />
                <Route path="smtp" element={<SMTPSettings />} />
                <Route path="crowdsec" element={<Navigate to="/security/crowdsec" replace />} />
                <Route path="account" element={<Account />} />
                <Route path="account-management" element={<UsersPage />} />
              </Route>

              {/* Tasks Routes */}
              <Route path="tasks" element={<Tasks />}>
                <Route index element={<Backups />} />
                <Route path="backups" element={<Backups />} />
                <Route path="logs" element={<Logs />} />
                <Route path="import">
                  <Route path="caddyfile" element={<ImportCaddy />} />
                  <Route path="crowdsec" element={<ImportCrowdSec />} />
                </Route>
              </Route>

            </Route>
          </Routes>
        </Suspense>
        <ToastContainer />
      </Router>
    </AuthProvider>
  )
}
