import { lazy, Suspense } from 'react';
import { Routes, Route, Navigate } from 'react-router-dom';
import { useAuthStore } from './stores/authStore';
import ProtectedRoute from './components/auth/ProtectedRoute';
import ErrorBoundary from './components/shared/ErrorBoundary';
import PageError from './components/shared/PageError';
import LoadingSpinner from './components/shared/LoadingSpinner';
import SkipLink from './components/shared/SkipLink';

// Layout components (loaded immediately as they're always needed)
import DBISLayout from './components/layout/DBISLayout';
import SCBLayout from './components/layout/SCBLayout';

// Auth page (loaded immediately for faster login)
import LoginPage from './pages/auth/LoginPage';

// Lazy load page components for code splitting
// DBIS Admin Pages
const DBISOverviewPage = lazy(() => import('./pages/dbis/OverviewPage'));
const DBISParticipantsPage = lazy(() => import('./pages/dbis/ParticipantsPage'));
const DBISGRUPage = lazy(() => import('./pages/dbis/GRUPage'));
const DBISGASQPSPage = lazy(() => import('./pages/dbis/GASQPSPage'));
const DBISCBDCFXPage = lazy(() => import('./pages/dbis/CBDCFXPage'));
const DBISMetaverseEdgePage = lazy(() => import('./pages/dbis/MetaverseEdgePage'));
const DBISRiskCompliancePage = lazy(() => import('./pages/dbis/RiskCompliancePage'));

// SCB Admin Pages
const SCBOverviewPage = lazy(() => import('./pages/scb/OverviewPage'));
const SCBFIManagementPage = lazy(() => import('./pages/scb/FIManagementPage'));
const SCBCorridorPolicyPage = lazy(() => import('./pages/scb/CorridorPolicyPage'));

/**
 * Lazy-loaded route wrapper with Suspense fallback
 */
const LazyRoute = ({ children }: { children: React.ReactNode }) => (
  <Suspense fallback={<LoadingSpinner fullPage />}>{children}</Suspense>
);

function App() {
  const { isAuthenticated } = useAuthStore();

  return (
    <ErrorBoundary>
      <SkipLink />
      <Routes>
        <Route path="/login" element={!isAuthenticated ? <LoginPage /> : <Navigate to="/dbis/overview" replace />} />
        
        <Route element={<ProtectedRoute />}>
          <Route path="/dbis/*" element={<DBISLayout />}>
            <Route
              path="overview"
              element={
                <LazyRoute>
                  <DBISOverviewPage />
                </LazyRoute>
              }
            />
            <Route
              path="participants"
              element={
                <LazyRoute>
                  <DBISParticipantsPage />
                </LazyRoute>
              }
            />
            <Route
              path="gru"
              element={
                <LazyRoute>
                  <DBISGRUPage />
                </LazyRoute>
              }
            />
            <Route
              path="gas-qps"
              element={
                <LazyRoute>
                  <DBISGASQPSPage />
                </LazyRoute>
              }
            />
            <Route
              path="cbdc-fx"
              element={
                <LazyRoute>
                  <DBISCBDCFXPage />
                </LazyRoute>
              }
            />
            <Route
              path="metaverse-edge"
              element={
                <LazyRoute>
                  <DBISMetaverseEdgePage />
                </LazyRoute>
              }
            />
            <Route
              path="risk-compliance"
              element={
                <LazyRoute>
                  <DBISRiskCompliancePage />
                </LazyRoute>
              }
            />
            <Route index element={<Navigate to="overview" replace />} />
          </Route>
          
          <Route path="/scb/*" element={<SCBLayout />}>
            <Route
              path="overview"
              element={
                <LazyRoute>
                  <SCBOverviewPage />
                </LazyRoute>
              }
            />
            <Route
              path="fi-management"
              element={
                <LazyRoute>
                  <SCBFIManagementPage />
                </LazyRoute>
              }
            />
            <Route
              path="corridors"
              element={
                <LazyRoute>
                  <SCBCorridorPolicyPage />
                </LazyRoute>
              }
            />
            <Route index element={<Navigate to="overview" replace />} />
          </Route>
          
          <Route path="/" element={<Navigate to="/dbis/overview" replace />} />
        </Route>
        
        <Route path="/404" element={<PageError code={404} />} />
        <Route path="/403" element={<PageError code={403} />} />
        <Route path="/500" element={<PageError code={500} />} />
        <Route path="*" element={<PageError code={404} />} />
      </Routes>
    </ErrorBoundary>
  );
}

export default App;

