import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom';
import { getUser } from './lib/api';
import Login from './pages/Login';
import Dashboard from './pages/Dashboard';
import DatabaseAdmin from './pages/DatabaseAdmin';
import TableView from './pages/TableView';
import ContentHub from './pages/ContentHub';
import ObjectCreation from './pages/ObjectCreation';
import StatementCreation from './pages/StatementCreation';

function RequireAuth({ children }) {
  const user = getUser();
  if (!user) return <Navigate to="/login" replace />;
  return children;
}

export default function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/login" element={<Login />} />
        <Route path="/" element={<RequireAuth><Dashboard /></RequireAuth>} />
        <Route path="/db" element={<RequireAuth><DatabaseAdmin /></RequireAuth>} />
        <Route path="/db/:tableKey" element={<RequireAuth><TableView /></RequireAuth>} />
        <Route path="/content" element={<RequireAuth><ContentHub /></RequireAuth>} />
        <Route path="/content/objects" element={<RequireAuth><ObjectCreation /></RequireAuth>} />
        <Route path="/content/statements" element={<RequireAuth><StatementCreation /></RequireAuth>} />
        <Route path="*" element={<Navigate to="/" replace />} />
      </Routes>
    </BrowserRouter>
  );
}
