/**
 * Router configuration
 */
import { lazy, Suspense } from 'react';
import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom';
import { auth } from './utils/auth';
import { ToastProvider } from './contexts/ToastContext';

const App = lazy(() => import('./App'));
const Login = lazy(() => import('./pages/Login').then((module) => ({ default: module.Login })));
const RocketSimulator = lazy(() => import('./pages/RocketSimulator').then((module) => ({ default: module.RocketSimulator })));
const AdminLayout = lazy(() => import('./pages/admin/AdminLayout').then((module) => ({ default: module.AdminLayout })));
const Dashboard = lazy(() => import('./pages/admin/Dashboard').then((module) => ({ default: module.Dashboard })));
const CelestialBodies = lazy(() => import('./pages/admin/CelestialBodies').then((module) => ({ default: module.CelestialBodies })));
const CelestialEvents = lazy(() => import('./pages/admin/CelestialEvents').then((module) => ({ default: module.CelestialEvents })));
const StarSystems = lazy(() => import('./pages/admin/StarSystems').then((module) => ({ default: module.StarSystems })));
const StaticData = lazy(() => import('./pages/admin/StaticData').then((module) => ({ default: module.StaticData })));
const Users = lazy(() => import('./pages/admin/Users').then((module) => ({ default: module.Users })));
const NASADownload = lazy(() => import('./pages/admin/NASADownload').then((module) => ({ default: module.NASADownload })));
const SystemSettings = lazy(() => import('./pages/admin/SystemSettings').then((module) => ({ default: module.SystemSettings })));
const Tasks = lazy(() => import('./pages/admin/Tasks').then((module) => ({ default: module.Tasks })));
const ScheduledJobs = lazy(() => import('./pages/admin/ScheduledJobs').then((module) => ({ default: module.ScheduledJobs })));
const UserProfile = lazy(() => import('./pages/admin/UserProfile').then((module) => ({ default: module.UserProfile })));
const ChangePassword = lazy(() => import('./pages/admin/ChangePassword').then((module) => ({ default: module.ChangePassword })));
const MyCelestialBodies = lazy(() => import('./pages/admin/MyCelestialBodies').then((module) => ({ default: module.MyCelestialBodies })));
const Rockets = lazy(() => import('./pages/admin/Rockets').then((module) => ({ default: module.Rockets })));

function RouteFallback() {
  return (
    <div className="fixed inset-0 flex items-center justify-center bg-black" role="status" aria-label="页面加载中">
      <div className="h-10 w-10 animate-spin rounded-full border-2 border-white/20 border-t-green-500" />
    </div>
  );
}

// Protected Route wrapper
function ProtectedRoute({ children }: { children: React.ReactNode }) {
  if (!auth.isLoggedIn()) {
    return <Navigate to="/login" replace />;
  }
  return <>{children}</>;
}

export function Router() {
  return (
    <ToastProvider>
      <BrowserRouter>
        <Suspense fallback={<RouteFallback />}>
          <Routes>
          {/* Public routes */}
          <Route path="/login" element={<Login />} />

          {/* Main app (3D visualization) */}
          <Route path="/" element={<App />} />
          <Route path="/rocket-simulator" element={<RocketSimulator />} />

          {/* User routes (protected) */}
          <Route
            path="/user"
            element={
              <ProtectedRoute>
                <AdminLayout />
              </ProtectedRoute>
            }
          >
            <Route path="profile" element={<UserProfile />} />
            <Route path="follow" element={<MyCelestialBodies />} />
          </Route>

          {/* Admin routes (protected) */}
          <Route
            path="/admin"
            element={
              <ProtectedRoute>
                <AdminLayout />
              </ProtectedRoute>
            }
          >
            <Route path="dashboard" element={<Dashboard />} />
            <Route path="change-password" element={<ChangePassword />} />
            <Route path="celestial-bodies" element={<CelestialBodies />} />
            <Route path="celestial-events" element={<CelestialEvents />} />
            <Route path="star-systems" element={<StarSystems />} />
            <Route path="static-data" element={<StaticData />} />
            <Route path="users" element={<Users />} />
            <Route path="nasa-data" element={<NASADownload />} />
            <Route path="tasks" element={<Tasks />} />
            <Route path="scheduled-jobs" element={<ScheduledJobs />} />
            <Route path="settings" element={<SystemSettings />} />
            <Route path="rockets" element={<Rockets />} />
          </Route>

          {/* Fallback */}
          <Route path="*" element={<Navigate to="/" replace />} />
          </Routes>
        </Suspense>
      </BrowserRouter>
    </ToastProvider>
  );
}
