import { BrowserRouter, Navigate, Route, Routes } from "react-router-dom";
import { Provider } from "react-redux";
import { store } from "@/store";
import {
  ChangePasswordPage,
  ForgotPasswordPage,
  LoginPage,
  RegisterPage,
  ResetPasswordPage,
  TwoFactorPage,
  VerifyEmailPage,
} from "@/app/auth";
import { RootLayout } from "@/app/layout";
import { MePage, MeLayout } from "@/app/me";
import { ChannelLayout } from "@/app/channel";
import {
  ProjectLayout,
  ReposPage,
  IssuesPage,
  NewIssuePage,
  SkillsPage,
  BoardPage,
  ChannelPage,
  RepoDetailPage,
  CommitDetailPage,
  IssueDetailPage,
  SkillDetailPage,
  PullRequestDetailPage,
  ProjectSettingsLayout,
  GeneralSettings,
  MembersSettings,
  AccessSettings,
  LabelsSettings,
  BillingSettings,
} from "@/app/project";
import { ChatPage } from "@/app/chat";
import CodePage from "@/app/project/repo/code";
import CommitsPage from "@/app/project/repo/commits";
import PullsPage from "@/app/project/repo/pulls";
import BranchesPage from "@/app/project/repo/branches";
import TagsPage from "@/app/project/repo/tags";
import RepoSettingsLayout from "@/app/project/repo/settings/RepoSettingsLayout";
import RepoGeneralSettings from "@/app/project/repo/settings/GeneralSettings";
import BranchProtectionSettings from "@/app/project/repo/settings/BranchProtectionSettings";
import TreePage from "@/app/project/repo/tree";
import { RedirectIfAuth, RequireAuth } from "@/components/auth";
import {
  SettingsLayout,
  MyAccountPage,
  BillingPage,
  AppearancePage,
  NotificationsPage,
  PasswordPage,
  EmailPage,
  SshKeysPage,
  AccessKeysPage,
  PushSettingsPage,
} from "@/app/settings";

export default function App() {
  return (
    <Provider store={store}>
      <BrowserRouter>
        <Routes>
          <Route element={<RedirectIfAuth />}>
            <Route path="/auth/login" element={<LoginPage />} />
            <Route path="/auth/register" element={<RegisterPage />} />
            <Route path="/auth/forgot-password" element={<ForgotPasswordPage />} />
            <Route path="/auth/reset-password" element={<ResetPasswordPage />} />
            <Route path="/auth/two-factor" element={<TwoFactorPage />} />
            <Route path="/auth/verify-email" element={<VerifyEmailPage />} />
          </Route>
          
          <Route path="/auth/change-password" element={<ChangePasswordPage />} />

          <Route element={<RequireAuth />}>
            <Route element={<RootLayout />}>
              <Route element={<MeLayout />}>
                <Route path="/me" element={<MePage />} />
                <Route path="/me/repositories" element={<MePage />} />
                <Route path="/me/projects" element={<MePage />} />
                <Route path="/me/activity" element={<MePage />} />
                <Route path="/me/stars" element={<MePage />} />
                <Route path="/me/following" element={<MePage />} />
                <Route path="/me/chat" element={<ChatPage scope="personal" />} />
                <Route path="/me/chat/:conversationId" element={<ChatPage scope="personal" />} />
              </Route>

              <Route path="/me/settings" element={<SettingsLayout />}>
                <Route index element={<MyAccountPage />} />
                <Route path="billing" element={<BillingPage />} />
                <Route path="appearance" element={<AppearancePage />} />
                <Route path="notifications" element={<NotificationsPage />} />
                <Route path="password" element={<PasswordPage />} />
                <Route path="email" element={<EmailPage />} />
                <Route path="ssh-keys" element={<SshKeysPage />} />
                <Route path="access-keys" element={<AccessKeysPage />} />
                <Route path="push" element={<PushSettingsPage />} />
              </Route>

              <Route element={<ChannelLayout />}>
                {/* Channel-based routes if any */}
              </Route>

              <Route path="/:projectName" element={<ProjectLayout />}>
                <Route index element={<Navigate to="repos" replace />} />
                <Route path="repos" element={<ReposPage />} />
                <Route path="issues" element={<IssuesPage />} />
                <Route path="issues/new" element={<NewIssuePage />} />
                <Route path="skills" element={<SkillsPage />} />
                <Route path="board" element={<BoardPage />} />
                <Route path="board/:boardId" element={<BoardPage />} />
                <Route path="settings" element={<ProjectSettingsLayout />}>
                  <Route index element={<GeneralSettings />} />
                  <Route path="members" element={<MembersSettings />} />
                  <Route path="access" element={<AccessSettings />} />
                  <Route path="labels" element={<LabelsSettings />} />
                  <Route path="billing" element={<BillingSettings />} />
                </Route>
                <Route path="chat" element={<ChatPage scope="project" />} />
                <Route path="chat/:conversationId" element={<ChatPage scope="project" />} />
                <Route path="channel/:roomId" element={<ChannelPage />} />
                <Route path="issues/:issueNumber" element={<IssueDetailPage />} />
                <Route path="skills/:skillSlug" element={<SkillDetailPage />} />
                <Route path="repo/:repoName" element={<RepoDetailPage />}>
                  <Route element={<CodePage />}>
                    <Route index element={<TreePage />} />
                    <Route path="tree/:treeOid" element={<TreePage />} />
                  </Route>
                  <Route path="commits" element={<CommitsPage />} />
                  <Route path="commit/:commitOid" element={<CommitDetailPage />} />
                  <Route path="pulls" element={<PullsPage />} />
                  <Route path="pulls/:prNumber" element={<PullRequestDetailPage />} />
                  <Route path="branches" element={<BranchesPage />} />
                  <Route path="tags" element={<TagsPage />} />
                  <Route path="settings" element={<RepoSettingsLayout />}>
                    <Route index element={<RepoGeneralSettings />} />
                    <Route path="general" element={<RepoGeneralSettings />} />
                    <Route path="branches" element={<BranchProtectionSettings />} />
                  </Route>
                </Route>
              </Route>
            </Route>
          </Route>

          <Route path="/" element={<Navigate to="/me" replace />} />
          <Route path="*" element={<Navigate to="/me" replace />} />
        </Routes>
      </BrowserRouter>
    </Provider>
  );
}
