import { lazy, Suspense } from 'react'
import { Routes, Route, Navigate, useLocation } from 'react-router-dom'
import { LoadingSpinner } from '@core/components/ui/LoadingSpinner'
import { AppShell } from '@core/components/layout/AppShell'
import { CortexSidebar } from './components/CortexSidebar'
import { TooltipProvider } from '@core/components/ui/tooltip'
import { useAppPath } from '@core/hooks/useAppPath'

const CortexDashboard = lazy(() => import('./pages/CortexDashboard'))

// CRM
const AccountsPage = lazy(() => import('./pages/crm/AccountsPage'))
const AccountDetailPage = lazy(() => import('./pages/crm/AccountDetailPage'))
const ContactsPage = lazy(() => import('./pages/crm/ContactsPage'))
const DealsPage = lazy(() => import('./pages/crm/DealsPage'))
const DealDetailPage = lazy(() => import('./pages/crm/DealDetailPage'))
const HealthPage = lazy(() => import('./pages/crm/HealthPage'))
const ActivityPage = lazy(() => import('./pages/crm/ActivityPage'))

// Support
const SupportPage = lazy(() => import('./pages/support/SupportPage'))
const TicketDetailPage = lazy(() => import('./pages/support/TicketDetailPage'))
const RedactionReview = lazy(() => import('./pages/support/RedactionReview'))

// Community
const CommunityPage = lazy(() => import('./pages/community/CommunityPage'))

// KB
const KBPage = lazy(() => import('./pages/kb/KBPage'))
const KBEditorPage = lazy(() => import('./pages/kb/KBEditorPage'))
const KBIngestionPage = lazy(() => import('./pages/kb/KBIngestionPage'))

// Courses
const CoursesPage = lazy(() => import('./pages/courses/CoursesPage'))

// Team
const TeamPage = lazy(() => import('./pages/team/TeamPage').then(m => ({ default: m.TeamPage })))

// Intelligence
const IntelligencePage = lazy(() => import('./pages/intelligence/IntelligencePage'))

// Code Intelligence Layer
const CILProjectsPage = lazy(() => import('./pages/cil/CILProjectsPage'))
const CILIngestionPage = lazy(() => import('./pages/cil/CILIngestionPage'))
const CILChunkDetailPage = lazy(() => import('./pages/cil/CILChunkDetailPage'))
const CILGraphPage = lazy(() => import('./pages/cil/CILGraphPage'))
const CILSearchPage = lazy(() => import('./pages/cil/CILSearchPage'))
const CILDashboardPage = lazy(() => import('./pages/cil/CILDashboardPage'))
const CILAgentPage = lazy(() => import('./pages/cil/CILAgentPage'))
const CILFileListPage = lazy(() => import('./pages/cil/CILFileListPage'))

// Operations
const OperationsDashboard = lazy(() => import('./pages/operations/OperationsDashboard'))
const AuditFunnelPage = lazy(() => import('./pages/operations/AuditFunnelPage'))
const InstallFunnelPage = lazy(() => import('./pages/operations/InstallFunnelPage'))
const ReviewQueuePage = lazy(() => import('./pages/operations/ReviewQueuePage'))
const AIDashboardPage = lazy(() => import('./pages/operations/AIDashboardPage'))

const Fallback = <div className="min-h-[400px] flex items-center justify-center"><LoadingSpinner /></div>

function CortexLayout() {
  const location = useLocation()
  const appPath = useAppPath()
  const segments = location.pathname.split('/').filter(Boolean)
  const breadcrumbs: { title: string; url?: string }[] = [{ title: 'Cortex', url: appPath('/dashboard') }]
  if (segments[1] && segments[1] !== 'dashboard') {
    breadcrumbs.push({ title: segments[1].charAt(0).toUpperCase() + segments[1].slice(1) })
  }
  if (segments[2]) {
    breadcrumbs.push({ title: segments[2].charAt(0).toUpperCase() + segments[2].slice(1) })
  }

  return (
    <AppShell sidebar={<CortexSidebar />} breadcrumbs={breadcrumbs}>
      <Suspense fallback={Fallback}>
        <Routes>
          <Route index element={<Navigate to="dashboard" replace />} />
          <Route path="dashboard" element={<CortexDashboard />} />

          {/* CRM */}
          <Route path="crm/accounts/:id" element={<AccountDetailPage />} />
          <Route path="crm/accounts" element={<AccountsPage />} />
          <Route path="crm/contacts" element={<ContactsPage />} />
          <Route path="crm/deals/new" element={<DealDetailPage />} />
          <Route path="crm/deals/:id" element={<DealDetailPage />} />
          <Route path="crm/deals" element={<DealsPage />} />
          <Route path="crm/health" element={<HealthPage />} />
          <Route path="crm/activity" element={<ActivityPage />} />

          {/* Support */}
          <Route path="support/:id/kb-review" element={<RedactionReview />} />
          <Route path="support/:id" element={<TicketDetailPage />} />
          <Route path="support" element={<SupportPage />} />

          {/* Community */}
          <Route path="community" element={<CommunityPage />} />

          {/* KB */}
          <Route path="kb/new" element={<KBEditorPage />} />
          <Route path="kb/:id/edit" element={<KBEditorPage />} />
          <Route path="kb/ingestion" element={<KBIngestionPage />} />
          <Route path="kb/ingestion/:type" element={<KBIngestionPage />} />
          <Route path="kb" element={<KBPage />} />

          {/* Courses */}
          <Route path="courses/*" element={<CoursesPage />} />

          {/* Team */}
          <Route path="team" element={<TeamPage />} />

          {/* Code Intelligence Layer */}
          <Route path="cil/:id/chunks/:chunkId" element={<CILChunkDetailPage />} />
          <Route path="cil/:id/graph" element={<CILGraphPage />} />
          <Route path="cil/:id/dashboard" element={<CILDashboardPage />} />
          <Route path="cil/:id/agent" element={<CILAgentPage />} />
          <Route path="cil/:id/ingest" element={<CILIngestionPage />} />
          <Route path="cil/:id/files" element={<CILFileListPage />} />
          <Route path="cil/search" element={<CILSearchPage />} />
          <Route path="cil/agent" element={<CILAgentPage />} />
          <Route path="cil" element={<CILProjectsPage />} />

          {/* Intelligence */}
          <Route path="intelligence" element={<IntelligencePage />} />

          {/* Operations */}
          <Route path="operations" element={<OperationsDashboard />} />
          <Route path="operations/audit-funnel" element={<AuditFunnelPage />} />
          <Route path="operations/install-funnel" element={<InstallFunnelPage />} />
          <Route path="review-queue" element={<ReviewQueuePage />} />
          <Route path="ai-dashboard" element={<AIDashboardPage />} />

          <Route path="*" element={<Navigate to="dashboard" replace />} />
        </Routes>
      </Suspense>
    </AppShell>
  )
}

export default function CortexApp() {
  return (
    <TooltipProvider>
      <CortexLayout />
    </TooltipProvider>
  )
}
