import { Routes, Route, Navigate } from "react-router-dom";
import { LoginPage } from "./features/auth/LoginPage";
import { ExpensesPage } from "./features/expenses/ExpensesPage";
import { getToken } from "./lib/auth";
import type { ReactNode } from "react";

function ProtectedRoute({ children }: { children: ReactNode }) {
  return getToken() ? children : <Navigate to="/login" replace />;
}

export default function App() {
  const token = getToken();

  return (
    <Routes>
      <Route
        path="/"
        element={<Navigate to={token ? "/expenses" : "/login"} replace />}
      />

      <Route path="/login" element={<LoginPage />} />

      <Route
        path="/expenses"
        element={
          <ProtectedRoute>
            <ExpensesPage />
          </ProtectedRoute>
        }
      />

      <Route
        path="*"
        element={<Navigate to={token ? "/expenses" : "/login"} replace />}
      />
    </Routes>
  );
}
