import { BrowserRouter, Routes, Route, Navigate } from "react-router-dom";
import Dashboard from "./pages/Dashboard";
import PartDetail from "./pages/PartDetail";
import AddPartPage from "./pages/AddPartPage";
import AuthForm from "./components/AuthForm";

const PrivateRoute = ({ children }) => {
  const token = localStorage.getItem("token");

  if (!token) {
    return <Navigate to="/login" replace />;
  }

  return children;
};

export default function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/login" element={<AuthForm />} />
        <Route
          path="/"
          element={
            <PrivateRoute>
              <Dashboard />
            </PrivateRoute>
          }
        />
        <Route
          path="/part/:id"
          element={
            <PrivateRoute>
              <PartDetail />
            </PrivateRoute>
          }
        />
        <Route
          path="/add"
          element={
            <PrivateRoute>
              <AddPartPage />
            </PrivateRoute>
          }
        />
      </Routes>
    </BrowserRouter>
  );
}
