import type { ComponentType } from "react";
import { lazy, Suspense } from "react";
import { createBrowserRouter } from "react-router-dom";
import App from "@/App";
import Spinner from "@/components/Spinner";
import { ProtectedRoute } from "@/components/ProtectedRoute";
import MainLayout from "@/layouts/MainLayout";
import RootLayout from "@/layouts/RootLayout";
import Home from "@/pages/Home";
import NotFound from "@/pages/NotFound";

const Archived = lazy(() => import("@/pages/Archived"));
const Explore = lazy(() => import("@/pages/Explore"));
const Inboxes = lazy(() => import("@/pages/Inboxes"));
const MemoDetail = lazy(() => import("@/pages/MemoDetail"));
const PermissionDenied = lazy(() => import("@/pages/PermissionDenied"));
const Attachments = lazy(() => import("@/pages/Attachments"));
const Setting = lazy(() => import("@/pages/Setting"));
const UserProfile = lazy(() => import("@/pages/UserProfile"));
const MemoDetailRedirect = lazy(() => import("./MemoDetailRedirect"));
const AuthPage = lazy(() => import("@/pages/Auth"));
const AuthCallback = lazy(() => import("@/pages/AuthCallback"));
const AuthSsoCallback = lazy(() => import("@/pages/AuthSsoCallback"));
const Landing = lazy(() => import("@/pages/Landing"));
const AboutCuCu = lazy(() => import("@/pages/AboutCuCu"));

import { ROUTES } from "./routes";

// Backward compatibility alias
export const Routes = ROUTES;
export { ROUTES };

// Helper component to reduce Suspense boilerplate for lazy routes
const LazyRoute = ({ component: Component }: { component: ComponentType }) => (
  <Suspense
    fallback={
      <div className="w-full h-64 flex items-center justify-center">
        <Spinner size="lg" />
      </div>
    }
  >
    <Component />
  </Suspense>
);

const router = createBrowserRouter([
  {
    path: "/",
    element: <App />,
    errorElement: <NotFound />,
    children: [
      // Landing page at root
      { index: true, element: <LazyRoute component={Landing} /> },
      { path: "landing", element: <LazyRoute component={Landing} /> },
      // Auth page (separate from main app - no sidebar)
      { path: Routes.AUTH, element: <LazyRoute component={AuthPage} /> },
      { path: "auth/callback", element: <LazyRoute component={AuthCallback} /> },
      { path: "auth/sso-callback", element: <LazyRoute component={AuthSsoCallback} /> },
      {
        path: Routes.ROOT,
        element: <RootLayout />,
        children: [
          {
            element: <MainLayout />,
            children: [
              { path: "", element: <Home /> },
              {
                path: "explore",
                element: (
                  <ProtectedRoute>
                    <LazyRoute component={Explore} />
                  </ProtectedRoute>
                ),
              },
              { path: "u/:username", element: <LazyRoute component={UserProfile} /> },
            ],
          },
          { path: "attachments", element: <LazyRoute component={Attachments} /> },
          { path: "archived", element: <LazyRoute component={Archived} /> },
          { path: "inbox", element: <LazyRoute component={Inboxes} /> },
          { path: "setting", element: <LazyRoute component={Setting} /> },
          { path: "about", element: <LazyRoute component={AboutCuCu} /> },
          { path: "memos/:uid", element: <LazyRoute component={MemoDetail} /> },
          // Redirect old path to new path
          { path: "m/:uid", element: <LazyRoute component={MemoDetailRedirect} /> },
          { path: "403", element: <LazyRoute component={PermissionDenied} /> },
          { path: "404", element: <LazyRoute component={NotFound} /> },
          { path: "*", element: <LazyRoute component={NotFound} /> },
        ],
      },
      // Support direct /memos/:uid path (without /app prefix) for sharing
      { path: "memos/:uid", element: <LazyRoute component={MemoDetail} /> },
      { path: "*", element: <LazyRoute component={NotFound} /> },
    ],
  },
]);

export default router;
