import { useEffect } from "react";
import { Navigate, createBrowserRouter, useParams } from "react-router";
import { RouterProvider } from "react-router/dom";
import { getSavedThemeId, getThemeById, applyTheme, defaultThemeId } from "@/lib/theme";
import RootLayout from "@/app/root-layout";

import AuthLayout from "@/page/auth/layout";
import LoginPage from "@/page/auth/login";
import RegisterPage from "@/page/auth/register";
import ForgotPasswordPage from "@/page/auth/forgot-password";
import ResetPasswordPage from "@/page/auth/reset-password";
import { PersonalShell, WorkspaceShell, SettingsShell } from "@/components/shell/navshell";
import WorkspaceRepositoriesPage from "@/page/workspace/repositories";
import WorkspaceIssuesPage from "@/page/workspace/issues";
import IssueDetailPage from "@/page/workspace/issues/detail";
import RepoLayout, { RepoIndexRedirect } from "@/page/workspace/repo/layout";
import CodeTab from "@/page/workspace/repo/code";
import CommitsTab from "@/page/workspace/repo/commits";
import BranchesTab from "@/page/workspace/repo/branches";
import TagsTab from "@/page/workspace/repo/tags";
import PullsTab from "@/page/workspace/repo/pulls";
import ContributorsTab from "@/page/workspace/repo/contributors";
import WebhooksTab from "@/page/workspace/repo/webhooks";
import RepoSettingsTab from "@/page/workspace/repo/settings";
import ReadmePage from "@/page/workspace/repo/readme-page";
import CommitDetailPage from "@/page/workspace/repo/commit-detail";
import PullCreatePage from "@/page/workspace/repo/pull-create";
import PullDetailPage from "@/page/workspace/repo/pull-detail";
import WorkspaceSettingsPage from "@/page/workspace/settings";
import ChannelPage from "@/page/workspace/channel";
import WorkplanChatList from "@/page/workspace/workplan/chat-list";
import WorkplanChatConversation from "@/page/workspace/workplan/chat-conversation";
import MeOverviewPage from "@/page/me/overview";
import MeReposPage from "@/page/me/repos";
import MeChatListPage from "@/page/me/chat-list.tsx";
import MeChatConversationPage from "@/page/me/chat-conversation.tsx";
import MeNotificationsPage from "@/page/me/notifications";
import MeStarsPage from "@/page/me/stars";
import MeFollowingPage from "@/page/me/following";
import SettingsProfilePage from "@/page/settings/profile";
import SettingsAppearancePage from "@/page/settings/appearance";
import SettingsNotificationsPage from "@/page/settings/notifications";
import SettingsPrivacyPage from "@/page/settings/privacy";
import SettingsAccessibilityPage from "@/page/settings/accessibility";
import SettingsSecurityPage from "@/page/settings/security";
import SettingsTokensPage from "@/page/settings/tokens";
import SettingsSshKeysPage from "@/page/settings/ssh-keys";
import LandingLayout from "@/page/landing/layout";
import LandingHome from "@/page/landing/home";
import FeaturesPage from "@/page/landing/features";
import WorkflowPage from "@/page/landing/workflow";
import PricingPage from "@/page/landing/pricing";

function WorkspaceCompatRedirect() {
  const { projectName = "" } = useParams();

  return <Navigate replace to={`/${projectName}/repos`} />;
}

function App() {
  useEffect(() => {
    const id = getSavedThemeId();
    const preset = getThemeById(id) || getThemeById(defaultThemeId)!;
    applyTheme(preset);
  }, []);

  const router = createBrowserRouter([
    {
      element: <RootLayout />,
      children: [
    {
      element: <LandingLayout />,
      children: [
        {
          index: true,
          element: <LandingHome />,
        },
        {
          path: "features",
          element: <FeaturesPage />,
        },
        {
          path: "workflow",
          element: <WorkflowPage />,
        },
        {
          path: "pricing",
          element: <PricingPage />,
        },
      ],
    },
    {
      path: "/workspace/:projectName/*",
      element: <WorkspaceCompatRedirect />,
    },
    {
      path: "/:projectName",
      element: <WorkspaceShell />,
      children: [
        {
          index: true,
          element: <Navigate replace to="repos" />,
        },
        {
          path: "repos",
          element: <WorkspaceRepositoriesPage />,
        },
        {
          path: "repo/:repoName",
          element: <RepoLayout />,
          children: [
            { index: true, element: <RepoIndexRedirect /> },
            { path: "code", element: <CodeTab /> },
            { path: "readme", element: <ReadmePage /> },
            { path: "commits", element: <CommitsTab /> },
            { path: "branches", element: <BranchesTab /> },
            { path: "tags", element: <TagsTab /> },
            { path: "pulls", element: <PullsTab /> },
            { path: "contributors", element: <ContributorsTab /> },
            { path: "webhooks", element: <WebhooksTab /> },
            { path: "settings", element: <RepoSettingsTab /> },
            { path: "commits/:oid", element: <CommitDetailPage /> },
            { path: "pulls/new", element: <PullCreatePage /> },
            { path: "pulls/:number", element: <PullDetailPage /> },
          ],
        },
        {
          path: "issues",
          element: <WorkspaceIssuesPage />,
        },
        {
          path: "issues/:issueNumber",
          element: <IssueDetailPage />,
        },


        {
          path: "chat",
          element: <Navigate replace to="workplan/chat" />,
        },
        {
          path: "chat/:channel",
          element: <Navigate replace to="workplan/chat" />,
        },
        {
          path: "workplan",
          element: <Navigate replace to="workplan/chat" />,
        },
        {
          path: "workplan/chat",
          element: <WorkplanChatList />,
        },
        {
          path: "workplan/chat/:conversationId",
          element: <WorkplanChatConversation />,
        },
        {
          path: "channel",
          element: <ChannelPage />,
        },
        {
          path: "channel/:roomId",
          element: <ChannelPage />,
        },
        {
          path: "settings",
          element: <WorkspaceSettingsPage />,
        },
      ],
    },
    {
      path: "/me",
      element: <PersonalShell />,
      children: [
        {
          index: true,
          element: <MeOverviewPage />,
        },
        {
          path: "repos",
          element: <MeReposPage />,
        },
        {
          path: "chat",
          element: <MeChatListPage />,
        },
        {
          path: "chat/:conversationId",
          element: <MeChatConversationPage />,
        },
        {
          path: "notifications",
          element: <MeNotificationsPage />,
        },
        {
          path: "stars",
          element: <MeStarsPage />,
        },
        {
          path: "following",
          element: <MeFollowingPage />,
        },
      ],
    },
    {
      path: "/settings",
      element: <SettingsShell />,
      children: [
        {
          index: true,
          element: <Navigate replace to="profile" />,
        },
        {
          path: "profile",
          element: <SettingsProfilePage />,
        },
        {
          path: "appearance",
          element: <SettingsAppearancePage />,
        },
        {
          path: "notifications",
          element: <SettingsNotificationsPage />,
        },
        {
          path: "privacy",
          element: <SettingsPrivacyPage />,
        },
        {
          path: "accessibility",
          element: <SettingsAccessibilityPage />,
        },
        {
          path: "security",
          element: <SettingsSecurityPage />,
        },
        {
          path: "tokens",
          element: <SettingsTokensPage />,
        },
        {
          path: "ssh-keys",
          element: <SettingsSshKeysPage />,
        },
      ],
    },
    {
      path: "/auth",
      element: <AuthLayout />,
      children: [
        {
          index: true,
          element: <Navigate replace to="/auth/login" />,
        },
        {
          path: "login",
          element: <LoginPage />,
        },
        {
          path: "register",
          element: <RegisterPage />,
        },
        {
          path: "forgot-password",
          element: <ForgotPasswordPage />,
        },
        {
          path: "reset-password",
          element: <ResetPasswordPage />,
        },
      ],
      },
      ],
      },
  ]);
  return (
    <RouterProvider router={router}/>
  )
}

export default App