import "./global.css";

import { Toaster } from "@/components/ui/toaster";
import { TooltipProvider } from "@/components/ui/tooltip";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { BrowserRouter, Routes, Route } from "react-router-dom";
import { useDiscordActivity } from "./contexts/DiscordActivityContext";
import { AuthProvider } from "./contexts/AuthContext";
import { Web3Provider } from "./contexts/Web3Context";
import { DocsThemeProvider } from "./contexts/DocsThemeContext";
import { ArmThemeProvider } from "./contexts/ArmThemeContext";
import { MaintenanceProvider } from "./contexts/MaintenanceContext";
import MaintenanceGuard from "./components/MaintenanceGuard";
import PageTransition from "./components/PageTransition";
import SkipAgentController from "./components/SkipAgentController";
import Onboarding from "./pages/Onboarding";
import Dashboard from "./pages/Dashboard";
import Login from "./pages/Login";
import Link from "./pages/Link";
import GameDevelopment from "./pages/GameDevelopment";
import MentorshipPrograms from "./pages/MentorshipPrograms";
import ResearchLabs from "./pages/ResearchLabs";
import Labs from "./pages/Labs";
import GameForge from "./pages/GameForge";
import Foundation from "./pages/Foundation";
import Nexus from "./pages/Nexus";
import Arms from "./pages/Arms";
import ExternalRedirect from "./components/ExternalRedirect";
import RequireAccess from "@/components/RequireAccess";
import Engage from "./pages/Pricing";
import DocsLayout from "@/components/docs/DocsLayout";
import DocsOverview from "./pages/docs/DocsOverview";
import DocsTutorials from "./pages/docs/DocsTutorials";
import DocsGettingStarted from "./pages/docs/DocsGettingStarted";
import DocsPlatform from "./pages/docs/DocsPlatform";
import DocsApiReference from "./pages/docs/DocsApiReference";
import DocsCli from "./pages/docs/DocsCli";
import DocsExamples from "./pages/docs/DocsExamples";
import DocsIntegrations from "./pages/docs/DocsIntegrations";
import VRChatIntegration from "./pages/docs/integrations/VRChat";
import RecRoomIntegration from "./pages/docs/integrations/RecRoom";
import SpatialIntegration from "./pages/docs/integrations/Spatial";
import DecentralandIntegration from "./pages/docs/integrations/Decentraland";
import TheSandboxIntegration from "./pages/docs/integrations/TheSandbox";
import GodotIntegration from "./pages/docs/integrations/Godot";
import GameMakerIntegration from "./pages/docs/integrations/GameMaker";
import GameJoltIntegration from "./pages/docs/integrations/GameJolt";
import ItchIoIntegration from "./pages/docs/integrations/ItchIo";
import DocsCurriculum from "./pages/docs/DocsCurriculum";
import DocsCurriculumEthos from "./pages/docs/DocsCurriculumEthos";
import TrackLibrary from "./pages/ethos/TrackLibrary";
import ArtistProfile from "./pages/ethos/ArtistProfile";
import ArtistSettings from "./pages/ethos/ArtistSettings";
import LicensingDashboard from "./pages/ethos/LicensingDashboard";
import Tutorials from "./pages/Tutorials";
import Blog from "./pages/Blog";
import BlogPost from "./pages/BlogPost";
import Community from "./pages/Community";
import Support from "./pages/Support";
import Status from "./pages/Status";
import Changelog from "./pages/Changelog";
import DevelopersDirectory from "./pages/DevelopersDirectory";
import ProfilePassport from "./pages/ProfilePassport";
import SubdomainPassport from "./pages/SubdomainPassport";
import Profile from "./pages/Profile";
import { SubdomainPassportProvider } from "./contexts/SubdomainPassportContext";
import About from "./pages/About";
import Contact from "./pages/Contact";
import GetStarted from "./pages/GetStarted";
import Careers from "./pages/Careers";
import Privacy from "./pages/Privacy";
import Terms from "./pages/Terms";
import Admin from "./pages/Admin";
import AdminModeration from "./pages/admin/AdminModeration";
import AdminAnalytics from "./pages/admin/AdminAnalytics";
import AdminFeed from "./pages/AdminFeed";
import ProjectsNew from "./pages/ProjectsNew";
import Explore from "./pages/Explore";
import ResetPassword from "./pages/ResetPassword";
import Teams from "./pages/Teams";
import Squads from "./pages/Squads";
import MenteeHub from "./pages/MenteeHub";
import ProjectBoard from "./pages/ProjectBoard";
import ProjectDetail from "./pages/ProjectDetail";
import { Navigate } from "react-router-dom";
import FourOhFourPage from "./pages/404";
import SignupRedirect from "./pages/SignupRedirect";
import Realms from "./pages/Realms";
import Investors from "./pages/Investors";
import NexusDashboard from "./pages/dashboards/NexusDashboard";
import GameForgeDashboard from "./pages/dashboards/GameForgeDashboard";
import Roadmap from "./pages/Roadmap";
import Trust from "./pages/Trust";
import PressKit from "./pages/PressKit";
import Downloads from "./pages/Downloads";
import Projects from "./pages/Projects";
import ProjectsAdmin from "./pages/ProjectsAdmin";
import Directory from "./pages/Directory";
import Wix from "./pages/Wix";
import WixCaseStudies from "./pages/WixCaseStudies";
import WixFaq from "./pages/WixFaq";
import DocsSync from "./pages/DocsSync";
import { DiscordProvider } from "./contexts/DiscordContext";
import { DiscordActivityProvider } from "./contexts/DiscordActivityContext";
import Activity from "./pages/Activity";
import DiscordActivity from "./pages/DiscordActivity";
import DiscordOAuthCallback from "./pages/DiscordOAuthCallback";
import RobloxCallback from "./pages/RobloxCallback";
import Web3Callback from "./pages/Web3Callback";
import DiscordVerify from "./pages/DiscordVerify";
import { Analytics } from "@vercel/analytics/react";
import CreatorDirectory from "./pages/creators/CreatorDirectory";
import CreatorProfile from "./pages/creators/CreatorProfile";
import BotPanel from "./pages/BotPanel";
import OpportunitiesHub from "./pages/opportunities/OpportunitiesHub";
import OpportunityDetail from "./pages/opportunities/OpportunityDetail";
import OpportunityPostForm from "./pages/opportunities/OpportunityPostForm";
import MyApplications from "./pages/profile/MyApplications";
// Hub pages moved to aethex.co (aethex-corp app)
import Space1Welcome from "./pages/internal-docs/Space1Welcome";
import Space1AxiomModel from "./pages/internal-docs/Space1AxiomModel";
import Space1FindYourRole from "./pages/internal-docs/Space1FindYourRole";
import Space1OwnershipFlows from "./pages/internal-docs/Space1OwnershipFlows";
import Space2CodeOfConduct from "./pages/internal-docs/Space2CodeOfConduct";
import Space2Communication from "./pages/internal-docs/Space2Communication";
import Space2MeetingCadence from "./pages/internal-docs/Space2MeetingCadence";
import Space2BrandVoice from "./pages/internal-docs/Space2BrandVoice";
import Space2TechStack from "./pages/internal-docs/Space2TechStack";
import Space3FoundationGovernance from "./pages/internal-docs/Space3FoundationGovernance";
import Space3OpenSourceProtocol from "./pages/internal-docs/Space3OpenSourceProtocol";
import Space3CommunityPrograms from "./pages/internal-docs/Space3CommunityPrograms";
import Space4ProductOps from "./pages/internal-docs/Space4ProductOps";
import Space4CorpBlueprints from "./pages/internal-docs/Space4CorpBlueprints";
import Space4ClientOps from "./pages/internal-docs/Space4ClientOps";
import Space4PlatformStrategy from "./pages/internal-docs/Space4PlatformStrategy";
import Space5Onboarding from "./pages/internal-docs/Space5Onboarding";
import Space5Finance from "./pages/internal-docs/Space5Finance";
// Staff/Candidate pages moved to staff.aethex.tech (aethex-staff app)
import DeveloperDashboard from "./pages/dev-platform/DeveloperDashboard";
import ApiReference from "./pages/dev-platform/ApiReference";
import QuickStart from "./pages/dev-platform/QuickStart";
import Templates from "./pages/dev-platform/Templates";
import TemplateDetail from "./pages/dev-platform/TemplateDetail";
import Marketplace from "./pages/dev-platform/Marketplace";
import MarketplaceItemDetail from "./pages/dev-platform/MarketplaceItemDetail";
import CodeExamples from "./pages/dev-platform/CodeExamples";
import ExampleDetail from "./pages/dev-platform/ExampleDetail";
import DeveloperPlatform from "./pages/dev-platform/DeveloperPlatform";

const queryClient = new QueryClient();

const DiscordActivityWrapper = ({ children }: { children: React.ReactNode }) => {
  const { isActivity } = useDiscordActivity();
  
  if (isActivity) {
    return <Activity />;
  }
  
  return <>{children}</>;
};

const App = () => (
  <QueryClientProvider client={queryClient}>
    <AuthProvider>
      <DiscordActivityProvider>
        <Web3Provider>
          <DiscordProvider>
            <TooltipProvider>
              <Toaster />
              <Analytics />
              <BrowserRouter>
                <DiscordActivityWrapper>
                <SubdomainPassportProvider>
                  <ArmThemeProvider>
                    <MaintenanceProvider>
                      <MaintenanceGuard>
                        <SkipAgentController />
                        <PageTransition>
                          <Routes>
                        {/* Subdomain Passport (aethex.me and aethex.space) handles its own redirect if not a subdomain */}
                        <Route path="/" element={<SubdomainPassport />} />
                        <Route path="/onboarding" element={<Onboarding />} />
                        <Route path="/dashboard" element={<Dashboard />} />
                        <Route
                          path="/dashboard/nexus"
                          element={<NexusDashboard />}
                        />
                        {/* Labs Dashboard redirects to aethex.studio */}
                        <Route
                          path="/dashboard/labs"
                          element={<ExternalRedirect to="https://aethex.studio/dashboard" />}
                        />
                        {/* GameForge Dashboard stays local on aethex.dev (Management Hub per Axiom Model) */}
                        <Route
                          path="/dashboard/gameforge"
                          element={
                            <RequireAccess>
                              <GameForgeDashboard />
                            </RequireAccess>
                          }
                        />
                        {/* Dev-Link dashboard redirects to Nexus dashboard */}
                        <Route
                          path="/dashboard/dev-link"
                          element={<Navigate to="/dashboard/nexus" replace />}
                        />
                        {/* Hub routes → aethex.co */}
                        <Route path="/hub/*" element={<ExternalRedirect to="https://aethex.co/hub" />} />
                        <Route path="/realms" element={<Realms />} />
                        <Route path="/investors" element={<Investors />} />
                        <Route path="/roadmap" element={<Roadmap />} />
                        <Route path="/trust" element={<Trust />} />
                        <Route path="/press" element={<PressKit />} />
                        <Route path="/downloads" element={<Downloads />} />
                        <Route path="/projects" element={<Projects />} />
                        <Route
                          path="/projects/admin"
                          element={<ProjectsAdmin />}
                        />
                        <Route path="/directory" element={<Directory />} />
                        <Route path="/admin" element={<Admin />} />
                        <Route path="/admin/feed" element={<AdminFeed />} />
                        <Route path="/admin/docs-sync" element={<DocsSync />} />
                        <Route
                          path="/admin/moderation"
                          element={
                            <RequireAccess>
                              <AdminModeration />
                            </RequireAccess>
                          }
                        />
                        <Route
                          path="/admin/analytics"
                          element={
                            <RequireAccess>
                              <AdminAnalytics />
                            </RequireAccess>
                          }
                        />
                        <Route path="/arms" element={<Arms />} />
                        <Route path="/feed" element={<Navigate to="/community/feed" replace />} />
                        <Route path="/teams" element={<Teams />} />
                        <Route path="/squads" element={<Squads />} />
                        <Route path="/mentee-hub" element={<MenteeHub />} />
                        <Route path="/projects/new" element={<ProjectsNew />} />
                        <Route
                          path="/projects/:projectId/board"
                          element={<ProjectBoard />}
                        />
                        <Route
                          path="/projects/:projectId"
                          element={<ProjectDetail />}
                        />
                        <Route path="/profile" element={<Profile />} />
                        <Route path="/profile/me" element={<Profile />} />
                        <Route
                          path="/profile/applications"
                          element={<MyApplications />}
                        />
                        <Route
                          path="/profile/link-discord"
                          element={<DiscordVerify />}
                        />

                        <Route
                          path="/passport"
                          element={<Navigate to="/passport/me" replace />}
                        />
                        <Route
                          path="/passport/me"
                          element={<ProfilePassport />}
                        />
                        <Route
                          path="/passport/:username"
                          element={<ProfilePassport />}
                        />
                        <Route path="/login" element={<Login />} />
                        <Route path="/link" element={<Link />} />
                        <Route path="/signup" element={<SignupRedirect />} />
                        <Route
                          path="/reset-password"
                          element={<ResetPassword />}
                        />
                        <Route
                          path="/roblox-callback"
                          element={<RobloxCallback />}
                        />
                        <Route
                          path="/web3-callback"
                          element={<Web3Callback />}
                        />
                        <Route
                          path="/discord-verify"
                          element={<DiscordVerify />}
                        />
                        <Route
                          path="/bot-panel"
                          element={
                            <RequireAccess>
                              <BotPanel />
                            </RequireAccess>
                          }
                        />
                        <Route path="/activity" element={<Activity />} />
                        <Route path="/discord" element={<DiscordActivity />} />
                        <Route
                          path="/discord/callback"
                          element={<DiscordOAuthCallback />}
                        />

                        {/* Creator Network routes */}
                        <Route
                          path="/creators"
                          element={<CreatorDirectory />}
                        />
                        <Route
                          path="/creators/:username"
                          element={<CreatorProfile />}
                        />
                        <Route
                          path="/opportunities"
                          element={<OpportunitiesHub />}
                        />
                        <Route
                          path="/opportunities/post"
                          element={<OpportunityPostForm />}
                        />
                        <Route
                          path="/opportunities/:id"
                          element={<OpportunityDetail />}
                        />

                        {/* Service routes */}
                        <Route
                          path="/game-development"
                          element={<GameDevelopment />}
                        />
                        {/* Legacy redirects - consolidated into /corp */}
                        <Route
                          path="/consulting"
                          element={<Navigate to="/corp" replace />}
                        />
                        <Route
                          path="/services"
                          element={<Navigate to="/corp" replace />}
                        />

                        <Route
                          path="/mentorship"
                          element={<MentorshipPrograms />}
                        />
                        <Route path="/engage" element={<Engage />} />
                        <Route
                          path="/pricing"
                          element={<Navigate to="/engage" replace />}
                        />
                        <Route path="/research" element={<ResearchLabs />} />

                        {/* Labs page with auto-redirect to aethex.studio (Skunkworks R&D) */}
                        <Route path="/labs" element={<Labs />} />

                        {/* GameForge Management routes stay local on aethex.dev (Axiom Model - Write/Control) */}
                        <Route
                          path="/gameforge/manage"
                          element={
                            <RequireAccess>
                              <GameForgeDashboard />
                            </RequireAccess>
                          }
                        />
                        <Route
                          path="/gameforge/manage/*"
                          element={
                            <RequireAccess>
                              <GameForgeDashboard />
                            </RequireAccess>
                          }
                        />
                        
                        {/* GameForge public route with auto-redirect to aethex.foundation/gameforge (Axiom Model - Read-Only Showcase) */}
                        <Route path="/gameforge" element={<GameForge />} />

                        {/* Foundation page with auto-redirect to aethex.foundation (Non-Profit Guardian - Axiom Model) */}
                        <Route path="/foundation" element={<Foundation />} />

                        {/* Corp routes → aethex.co */}
                        <Route path="/corp" element={<ExternalRedirect to="https://aethex.co" />} />
                        <Route path="/corp/*" element={<ExternalRedirect to="https://aethex.co" />} />

                        {/* Staff + Candidate routes → staff.aethex.tech */}
                        <Route path="/staff" element={<ExternalRedirect to="https://staff.aethex.tech" />} />
                        <Route path="/staff/*" element={<ExternalRedirect to="https://staff.aethex.tech" />} />
                        <Route path="/candidate" element={<ExternalRedirect to="https://staff.aethex.tech/candidate" />} />
                        <Route path="/candidate/*" element={<ExternalRedirect to="https://staff.aethex.tech/candidate" />} />

                        {/* Dev-Link routes - now redirect to Nexus Opportunities with ecosystem filter */}
                        <Route path="/dev-link" element={<Navigate to="/opportunities?ecosystem=roblox" replace />} />
                        <Route
                          path="/dev-link/*"
                          element={<Navigate to="/opportunities?ecosystem=roblox" replace />}
                        />

                        {/* Client Hub routes → aethex.co */}
                        <Route path="/hub/client/*" element={<ExternalRedirect to="https://aethex.co/hub" />} />

                        {/* Nexus routes */}
                        <Route path="/nexus" element={<Nexus />} />

                        {/* Resource routes */}
                        <Route
                          path="/docs"
                          element={
                            <DocsThemeProvider>
                              <DocsLayout />
                            </DocsThemeProvider>
                          }
                        >
                          <Route index element={<DocsOverview />} />
                          <Route path="tutorials" element={<DocsTutorials />} />
                          <Route
                            path="curriculum"
                            element={<DocsCurriculum />}
                          />
                          <Route
                            path="curriculum/ethos"
                            element={<DocsCurriculumEthos />}
                          />
                          <Route
                            path="getting-started"
                            element={<DocsGettingStarted />}
                          />
                          <Route path="platform" element={<DocsPlatform />} />
                          <Route path="api" element={<DocsApiReference />} />
                          <Route path="cli" element={<DocsCli />} />
                          <Route path="examples" element={<DocsExamples />} />
                          <Route
                            path="integrations"
                            element={<DocsIntegrations />}
                          />
                          <Route
                            path="integrations/vrchat"
                            element={<VRChatIntegration />}
                          />
                          <Route
                            path="integrations/recroom"
                            element={<RecRoomIntegration />}
                          />
                          <Route
                            path="integrations/spatial"
                            element={<SpatialIntegration />}
                          />
                          <Route
                            path="integrations/decentraland"
                            element={<DecentralandIntegration />}
                          />
                          <Route
                            path="integrations/thesandbox"
                            element={<TheSandboxIntegration />}
                          />
                          <Route
                            path="integrations/godot"
                            element={<GodotIntegration />}
                          />
                          <Route
                            path="integrations/gamemaker"
                            element={<GameMakerIntegration />}
                          />
                          <Route
                            path="integrations/gamejolt"
                            element={<GameJoltIntegration />}
                          />
                          <Route
                            path="integrations/itchio"
                            element={<ItchIoIntegration />}
                          />
                        </Route>
                        <Route path="/tutorials" element={<Tutorials />} />
                        <Route path="/community/*" element={<Community />} />
                        <Route path="/developers" element={<DevelopersDirectory />} />
                        <Route path="/blog" element={<Blog />} />
                        <Route path="/blog/:slug" element={<BlogPost />} />

                        {/* Ethos Guild Routes */}
                        <Route
                          path="/ethos/library"
                          element={<TrackLibrary />}
                        />
                        <Route
                          path="/ethos/artists/:userId"
                          element={<ArtistProfile />}
                        />
                        <Route
                          path="/ethos/settings"
                          element={<ArtistSettings />}
                        />
                        <Route
                          path="/ethos/licensing"
                          element={<LicensingDashboard />}
                        />

                        <Route path="/support" element={<Support />} />
                        <Route path="/status" element={<Status />} />
                        <Route path="/changelog" element={<Changelog />} />

                        {/* Informational routes */}
                        <Route path="/wix" element={<Wix />} />
                        <Route
                          path="/wix/case-studies"
                          element={<WixCaseStudies />}
                        />
                        <Route path="/wix/faq" element={<WixFaq />} />
                        <Route path="/about" element={<About />} />
                        <Route path="/contact" element={<Contact />} />
                        <Route path="/get-started" element={<GetStarted />} />
                        <Route path="/explore" element={<Explore />} />
                        {/* Legacy /services redirect to /corp */}
                        <Route
                          path="/services"
                          element={<Navigate to="/corp" replace />}
                        />
                        <Route path="/careers" element={<Careers />} />

                        {/* Legal routes */}
                        <Route path="/privacy" element={<Privacy />} />
                        <Route path="/terms" element={<Terms />} />

                        {/* Discord Activity route */}
                        <Route path="/activity" element={<Activity />} />

                        {/* Internal Docs Hub Routes */}
                        <Route
                          path="/internal-docs"
                          element={<Space1Welcome />}
                        />
                        <Route
                          path="/internal-docs/axiom-model"
                          element={<Space1AxiomModel />}
                        />
                        <Route
                          path="/internal-docs/find-your-role"
                          element={<Space1FindYourRole />}
                        />
                        <Route
                          path="/internal-docs/ownership-flows"
                          element={<Space1OwnershipFlows />}
                        />
                        <Route
                          path="/internal-docs/code-of-conduct"
                          element={<Space2CodeOfConduct />}
                        />
                        <Route
                          path="/internal-docs/communication"
                          element={<Space2Communication />}
                        />
                        <Route
                          path="/internal-docs/meetings"
                          element={<Space2MeetingCadence />}
                        />
                        <Route
                          path="/internal-docs/brand"
                          element={<Space2BrandVoice />}
                        />
                        <Route
                          path="/internal-docs/tech-stack"
                          element={<Space2TechStack />}
                        />
                        <Route
                          path="/internal-docs/foundation-governance"
                          element={<Space3FoundationGovernance />}
                        />
                        <Route
                          path="/internal-docs/foundation-protocol"
                          element={<Space3OpenSourceProtocol />}
                        />
                        <Route
                          path="/internal-docs/foundation-programs"
                          element={<Space3CommunityPrograms />}
                        />
                        <Route
                          path="/internal-docs/corp-product"
                          element={<Space4ProductOps />}
                        />
                        <Route
                          path="/internal-docs/corp-blueprints"
                          element={<Space4CorpBlueprints />}
                        />
                        <Route
                          path="/internal-docs/corp-clients"
                          element={<Space4ClientOps />}
                        />
                        <Route
                          path="/internal-docs/corp-platform"
                          element={<Space4PlatformStrategy />}
                        />
                        <Route
                          path="/internal-docs/onboarding"
                          element={<Space5Onboarding />}
                        />
                        <Route
                          path="/internal-docs/finance"
                          element={<Space5Finance />}
                        />

                        {/* Developer Platform Routes */}
                        <Route path="/dev-platform" element={<DeveloperPlatform />} />
                        <Route 
                          path="/dev-platform/dashboard" 
                          element={
                            <RequireAccess>
                              <DeveloperDashboard />
                            </RequireAccess>
                          } 
                        />
                        <Route path="/dev-platform/api-reference" element={<ApiReference />} />
                        <Route path="/dev-platform/quick-start" element={<QuickStart />} />
                        <Route path="/dev-platform/templates" element={<Templates />} />
                        <Route path="/dev-platform/templates/:id" element={<TemplateDetail />} />
                        <Route path="/dev-platform/marketplace" element={<Marketplace />} />
                        <Route path="/dev-platform/marketplace/:id" element={<MarketplaceItemDetail />} />
                        <Route path="/dev-platform/examples" element={<CodeExamples />} />
                        <Route path="/dev-platform/examples/:id" element={<ExampleDetail />} />

                        {/* Explicit 404 route for static hosting fallbacks */}
                        <Route path="/404" element={<FourOhFourPage />} />
                        {/* ADD ALL CUSTOM ROUTES ABOVE THE CATCH-ALL "*" ROUTE */}
                        <Route path="*" element={<FourOhFourPage />} />
                          </Routes>
                        </PageTransition>
                      </MaintenanceGuard>
                    </MaintenanceProvider>
                  </ArmThemeProvider>
                </SubdomainPassportProvider>
                </DiscordActivityWrapper>
              </BrowserRouter>
            </TooltipProvider>
          </DiscordProvider>
        </Web3Provider>
      </DiscordActivityProvider>
    </AuthProvider>
  </QueryClientProvider>
);

export default App;
