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 FlowDetail from "./pages/Flow/Detail";
import LayoutPage from "./pages/Layout";
import ClientPage from "./pages/Client";


// 受保护的路由：没登录就跳转到登录页
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: "/client", element: <ClientPage /> },
                    { path: "/flow/detail/:work_uuid", element: <FlowDetail /> },
                    { path: "/user", element: <User /> },
                    { path: "/user/detail", element: <Profile /> }
                ]
            }
        ],
    },

];

export default routes;
