/**
 * Router configuration
 */
import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom';
import { Login } from './pages/Login';
import { AdminLayout } from './pages/admin/AdminLayout';
import { Dashboard } from './pages/admin/Dashboard';
import { CelestialBodies } from './pages/admin/CelestialBodies';
import { CelestialEvents } from './pages/admin/CelestialEvents';
import { StarSystems } from './pages/admin/StarSystems';
import { StaticData } from './pages/admin/StaticData';
import { Users } from './pages/admin/Users';
import { NASADownload } from './pages/admin/NASADownload';
import { SystemSettings } from './pages/admin/SystemSettings';
import { Tasks } from './pages/admin/Tasks';
import { ScheduledJobs } from './pages/admin/ScheduledJobs';
import { UserProfile } from './pages/admin/UserProfile';
import { ChangePassword } from './pages/admin/ChangePassword';
import { MyCelestialBodies } from './pages/admin/MyCelestialBodies';
import { auth } from './utils/auth';
import { ToastProvider } from './contexts/ToastContext';
import App from './App';

// 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>
        <Routes>
          {/* Public routes */}
          <Route path="/login" element={<Login />} />

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

          {/* 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>

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