import { BrowserRouter, Routes, Route, Outlet, useLocation } from "react-router-dom";
import { Toaster } from "sonner";
import { useEffect } from "react";
import Navbar from "@/components/Navbar";
import Footer from "@/components/Footer";
import Home from "@/pages/Home";
import FindPart from "@/pages/FindPart";
import HowItWorks from "@/pages/HowItWorks";
import Inventory from "@/pages/Inventory";
import ForMechanics from "@/pages/ForMechanics";
import About from "@/pages/About";
import Contact from "@/pages/Contact";
import AdminLogin from "@/pages/AdminLogin";
import AdminDashboard from "@/pages/AdminDashboard";
import UserAuth from "@/pages/UserAuth";
import UserDashboard from "@/pages/UserDashboard";

function ScrollToTop() {
  const { pathname } = useLocation();
  useEffect(() => { window.scrollTo(0, 0); }, [pathname]);
  return null;
}

function Layout() {
  return (
    <div className="min-h-screen flex flex-col">
      <Navbar />
      <main className="flex-1 pt-[70px]">
        <Outlet />
      </main>
      <Footer />
    </div>
  );
}

function App() {
  return (
    <BrowserRouter>
      <ScrollToTop />
      <Toaster
        theme="dark"
        position="top-right"
        toastOptions={{
          style: { background: '#1a1a1a', border: '1px solid rgba(255,255,255,0.1)', color: '#e5e5e5' },
        }}
      />
      <Routes>
        <Route path="/admin/login" element={<AdminLogin />} />
        <Route path="/admin/dashboard" element={<AdminDashboard />} />
        <Route path="/account" element={<UserAuth />} />
        <Route path="/dashboard" element={<UserDashboard />} />
        <Route element={<Layout />}>
          <Route path="/" element={<Home />} />
          <Route path="/find-part" element={<FindPart />} />
          <Route path="/how-it-works" element={<HowItWorks />} />
          <Route path="/inventory" element={<Inventory />} />
          <Route path="/for-mechanics" element={<ForMechanics />} />
          <Route path="/about" element={<About />} />
          <Route path="/contact" element={<Contact />} />
        </Route>
      </Routes>
    </BrowserRouter>
  );
}

export default App;
