/**
 * Application Routes
 * @description Defines public and authenticated routes using TanStack Router.
 */

import { createRootRoute, createRoute, createRouter, Outlet } from '@tanstack/react-router';
import { RootLayout } from '@/components/layout';
import { Dashboard } from '@/pages/dashboard';
import { SignUpPage } from '@/pages/sign-up';
import { LoginPage } from '@/pages/login-page';

const rootRoute = createRootRoute({
    component: () => <Outlet />,
});

const publicRoute = createRoute({
    getParentRoute: () => rootRoute,
    id: 'public',
    component: () => <Outlet />,
});

const appRoute = createRoute({
    getParentRoute: () => rootRoute,
    id: 'app',
    component: () => (
        <RootLayout>
            <Outlet />
        </RootLayout>
    ),
});

const loginRoute = createRoute({
    getParentRoute: () => publicRoute,
    path: '/',
    component: LoginPage,
});

const signUpRoute = createRoute({
    getParentRoute: () => publicRoute,
    path: '/signup',
    component: SignUpPage,
});

const dashboardRoute = createRoute({
    getParentRoute: () => appRoute,
    path: '/dashboard',
    component: Dashboard,
});

const routeTree = rootRoute.addChildren([
    publicRoute.addChildren([loginRoute, signUpRoute]),
    appRoute.addChildren([dashboardRoute]),
]);

export const router = createRouter({
    routeTree,
    defaultPreload: 'intent',
});

declare module '@tanstack/react-router' {
    interface Register {
        router: typeof router;
    }
}

