import { lazy, Suspense } from 'react'
import { Routes, Route, Navigate } from 'react-router'
import { LoadingPage, AppErrorBoundary } from './components/ui'
import { AppShell } from './components/layout'
import { ProtectedRoute } from './components/auth'
import { WorkspaceType } from './domain/enums'
import { useSessionStore } from './stores/sessionStore'

const WORKSPACE_HOME: Record<string, string> = {
  [WorkspaceType.SUPPLY]: '/supply/dashboard',
  [WorkspaceType.DEMAND]: '/demand/ai-search',
  [WorkspaceType.OPERATIONS]: '/ops/review-queue',
}

function RoleRedirect() {
  const { currentUser } = useSessionStore()
  const first = currentUser?.allowedWorkspaces[0] ?? WorkspaceType.SUPPLY
  return <Navigate to={WORKSPACE_HOME[first] ?? '/supply/dashboard'} replace />
}

const LoginScreen = lazy(() => import('./pages/auth/LoginScreen'))

const SupplyDashboard = lazy(() => import('./pages/supply/SupplyDashboard'))
const Properties = lazy(() => import('./pages/supply/Properties'))
const MatchCenter = lazy(() => import('./pages/supply/MatchCenter'))
const FutureAvailability = lazy(() => import('./pages/supply/FutureAvailability'))
const DataQuality = lazy(() => import('./pages/supply/DataQuality'))

const AISearch = lazy(() => import('./pages/demand/AISearch'))
const Results = lazy(() => import('./pages/demand/Results'))
const MatchDetail = lazy(() => import('./pages/demand/MatchDetail'))
const Compare = lazy(() => import('./pages/demand/Compare'))
const Shortlists = lazy(() => import('./pages/demand/Shortlists'))

const ReviewQueue = lazy(() => import('./pages/ops/ReviewQueue'))
const AIMonitoring = lazy(() => import('./pages/ops/AIMonitoring'))
const Governance = lazy(() => import('./pages/ops/Governance'))
const MarketIntelligence = lazy(() => import('./pages/ops/MarketIntelligence'))
const SourceMonitoring = lazy(() => import('./pages/ops/SourceMonitoring'))
const ActivityTimeline = lazy(() => import('./pages/ops/ActivityTimeline'))
const SignalPipeline = lazy(() => import('./pages/ops/SignalPipeline'))

function App() {
  return (
    <AppErrorBoundary>
      <Suspense fallback={<LoadingPage />}>
        <Routes>
          {/* Public */}
          <Route path="/auth/login" element={<LoginScreen />} />

          {/* Protected: auth check only */}
          <Route element={<ProtectedRoute />}>
            <Route element={<AppShell />}>
              <Route path="/" element={<RoleRedirect />} />

              {/* Supply Workspace */}
              <Route element={<ProtectedRoute workspace={WorkspaceType.SUPPLY} />}>
                <Route path="/supply/dashboard" element={<SupplyDashboard />} />
                <Route path="/supply/properties" element={<Properties />} />
                <Route path="/supply/match-center" element={<MatchCenter />} />
                <Route path="/supply/future-availability" element={<FutureAvailability />} />
                <Route path="/supply/data-quality" element={<DataQuality />} />
              </Route>

              {/* Demand Workspace */}
              <Route element={<ProtectedRoute workspace={WorkspaceType.DEMAND} />}>
                <Route path="/demand/ai-search" element={<AISearch />} />
                <Route path="/demand/results" element={<Results />} />
                <Route path="/demand/results/:matchId" element={<MatchDetail />} />
                <Route path="/demand/compare" element={<Compare />} />
                <Route path="/demand/shortlists" element={<Shortlists />} />
              </Route>

              {/* Operations Workspace */}
              <Route element={<ProtectedRoute workspace={WorkspaceType.OPERATIONS} />}>
                <Route path="/ops/review-queue" element={<ReviewQueue />} />
                <Route path="/ops/ai-monitoring" element={<AIMonitoring />} />
                <Route path="/ops/governance" element={<Governance />} />
                <Route path="/ops/market-intelligence" element={<MarketIntelligence />} />
                <Route path="/ops/source-monitoring" element={<SourceMonitoring />} />
                <Route path="/ops/activity-timeline" element={<ActivityTimeline />} />
                <Route path="/ops/signal-pipeline" element={<SignalPipeline />} />
              </Route>
            </Route>
          </Route>

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

export default App
