import { lazy, Suspense } from 'react';
import { Routes, Route } from 'react-router-dom';
import { AuthProvider } from './contexts/AuthContext';
import { ProtectedRoute } from './components/shared/ProtectedRoute';
import { PublicLayout } from './components/layout/PublicLayout';
import { IntranetLayout } from './components/layout/IntranetLayout';
import { PageLoader } from './components/shared/PageLoader';

// ── Public Pages (lazy-loaded) ────────────────────────────────────────────────

const HomePage        = lazy(() => import('./pages/public/HomePage'));
const StudioPage      = lazy(() => import('./pages/public/StudioPage'));
const EventsPage      = lazy(() => import('./pages/public/EventsPage'));
const ForumPage       = lazy(() => import('./pages/public/ForumPage'));
const ThreadPage      = lazy(() => import('./pages/public/ThreadPage'));
const BugReportPage   = lazy(() => import('./pages/public/BugReportPage'));
const BugDetailPage   = lazy(() => import('./pages/public/BugDetailPage'));
const AccountPage     = lazy(() => import('./pages/public/AccountPage'));
const LoginPage       = lazy(() => import('./pages/public/LoginPage'));
const RegisterPage    = lazy(() => import('./pages/public/RegisterPage'));
const NotFoundPage    = lazy(() => import('./pages/public/NotFoundPage'));

// ── Intranet Pages (lazy-loaded) ──────────────────────────────────────────────

const IntranetDashboard   = lazy(() => import('./pages/intranet/IntranetDashboard'));
const IntranetBugs        = lazy(() => import('./pages/intranet/IntranetBugs'));
const IntranetFeed        = lazy(() => import('./pages/intranet/IntranetFeed'));
const IntranetEvents      = lazy(() => import('./pages/intranet/IntranetEvents'));
const IntranetUsers       = lazy(() => import('./pages/intranet/IntranetUsers'));
const IntranetModeration  = lazy(() => import('./pages/intranet/IntranetModeration'));

// ── App ────────────────────────────────────────────────────────────────────────

export default function App() {
  return (
    <AuthProvider>
      <Suspense fallback={<PageLoader />}>
        <Routes>
          {/* Public Routes */}
          <Route element={<PublicLayout />}>
            <Route index element={<HomePage />} />
            <Route path="studio" element={<StudioPage />} />
            <Route path="events" element={<EventsPage />} />
            <Route path="forum" element={<ForumPage />} />
            <Route path="forum/thread/:id" element={<ThreadPage />} />
            <Route path="bugs" element={<BugReportPage />} />
            <Route path="bugs/:id" element={<BugDetailPage />} />
            <Route
              path="account"
              element={
                <ProtectedRoute>
                  <AccountPage />
                </ProtectedRoute>
              }
            />
            <Route path="login" element={<LoginPage />} />
            <Route path="register" element={<RegisterPage />} />
            <Route path="*" element={<NotFoundPage />} />
          </Route>

          {/* Intranet Routes — staff only */}
          <Route
            path="intranet"
            element={
              <ProtectedRoute staffOnly redirectTo="/">
                <IntranetLayout />
              </ProtectedRoute>
            }
          >
            <Route index element={<IntranetDashboard />} />
            <Route path="bugs" element={<IntranetBugs />} />
            <Route path="feed" element={<IntranetFeed />} />
            <Route path="events" element={<IntranetEvents />} />
            <Route path="users" element={<IntranetUsers />} />
            <Route path="moderation" element={<IntranetModeration />} />
          </Route>
        </Routes>
      </Suspense>
    </AuthProvider>
  );
}
