import { Layout } from '@/components/layout';
import { NotFoundPage } from '@/features/not-found/not-found-page';
import { ServerErrorPage } from '@/features/server-error/server-error-page';
import type { ApiClient } from '@/lib/api-client';
import type { AuthContextValue } from '@/lib/auth';
import { Outlet, createRootRouteWithContext, useLocation } from '@tanstack/react-router';

type RouterContext = {
  auth: AuthContextValue;
  api: ApiClient;
};

export const Route = createRootRouteWithContext<RouterContext>()({
  component: RootRoute,
  errorComponent: ServerErrorPage,
  notFoundComponent: NotFoundPage,
});

function RootRoute() {
  const location = useLocation();
  const isPublicBookingRoute = /^\/[^/]+\/booking(?:\/|$)/.test(location.pathname);

  if (isPublicBookingRoute) {
    return <Outlet />;
  }

  return (
    <Layout>
      <div className="flex flex-col">
        <Outlet />
      </div>
    </Layout>
  );
}
