import { Navigate, Outlet } from "react-router-dom";
import { useAuth } from "./context/AuthContext";
import Loading from './pages/Loading';
import Home from "./pages/Home";
import Login from "./pages/Login";
import User from "./pages/User";
import Profile from "./pages/User/Profile";
import Flow from "./pages/Flow";
import LayoutPage from "./pages/Layout";


// 受保护的路由：没登录就跳转到登录页
function PrivateRoute() {
    const { user, loading } = useAuth();
    return (user || loading) ? <Outlet /> : <Navigate to="/login" replace />;
}

const routes = [
    // 登录页公开访问
    { path: "/", element: <Loading /> },
    { path: "/login", element: <Login /> },

    // 需要登录才能访问的路由
    {
        element: <PrivateRoute />,
        children: [
            {
                element: <LayoutPage />,
                children: [
                    { path: "/flow", element: <Flow /> },
                    { path: "/user", element: <User /> },
                    { path: "/user/detail", element: <Profile /> }
                ]
            }
        ],
    },

];

export default routes;
