import { lazy } from "react";
import { Route, Navigate, Outlet } from "react-router-dom";
import HomeLayout from "../layout/HomeLayout";
import { isAuthenticated } from "../utills/auth";

const HomePage = lazy(() => import("../pages/HomePage"));
const LoginPage = lazy(() => import("../pages/auth/LoginPage"));
const ForgotPasswordPage = lazy(() => import("../pages/auth/ForgotPasswordPage"));
const StepperForm = lazy(() => import("../feature/StepperForm"));
const NotFound = lazy(() => import("../pages/NotFound"));

const PublicGuard = () => {
  const auth = isAuthenticated();
  return auth ? <Navigate to="/dashboard-home" replace /> : <Outlet />;
};

const PublicRoutes = () => {
  return (
    <>
      <Route element={<HomeLayout />}>
           <Route path="/" element={<HomePage />} />
           <Route element={<PublicGuard />}>
              <Route path="/registration" element={<StepperForm />} />
           </Route>
       </Route>

       <Route element={<PublicGuard />}>
          <Route path="/login" element={<LoginPage />} />
          <Route path="/forgot-password" element={<ForgotPasswordPage />} />
       </Route>
       
      {/* 404 route MUST be last */}
        <Route path="*" element={<NotFound />} />
    </>
  )
}

export default PublicRoutes
