import { Suspense } from 'react';
import { Routes, Route, Navigate } from 'react-router-dom';
import { useAuth } from '@/features/auth/hooks/useAuth';
import { ProtectedRoute } from '@/components/auth/ProtectedRoute';
import { DashboardLayout } from '@/components/layout/DashboardLayout';
import { LoadingSpinner } from '@/components/ui/loading-spinner';
import {
  LazyLogin,
  LazyRegister,
  LazyForgotPassword,
  LazyVerifyEmail,
  LazyResetPassword,
  LazyDashboard,
  LazyChat,
  LazyLibrary,
  LazyProfile,
  LazySettings,
  LazySessions,
  LazyNotFound,
  LazyServerError,
  LazyUserProfile,
  LazyRoles,
  LazyTrackDetail,
  LazyPlaylistRoutes,
  LazyMarketplace,
} from '@/components/ui/LazyComponent';

// Composant wrapper pour les routes protégées avec DashboardLayout
function ProtectedLayoutRoute({ children }: { children: React.ReactNode }) {
  // On pourrait ajouter isLoading ici si on avait un état de chargement global
  return <DashboardLayout>{children}</DashboardLayout>;
}

// Composant pour les routes publiques
function PublicRoute({ children }: { children: React.ReactNode }) {
  const { isAuthenticated } = useAuth();

  if (isAuthenticated) {
    return <Navigate to='/dashboard' replace />;
  }

  return <>{children}</>;
}

export const AppRouter = () => (
  <Suspense fallback={<LoadingSpinner />}>
    <Routes>
      {/* Routes publiques */}
      <Route
        path='/login'
        element={
          <PublicRoute>
            <LazyLogin />
          </PublicRoute>
        }
      />
      <Route
        path='/register'
        element={
          <PublicRoute>
            <LazyRegister />
          </PublicRoute>
        }
      />
      <Route
        path='/forgot-password'
        element={
          <PublicRoute>
            <LazyForgotPassword />
          </PublicRoute>
        }
      />
      <Route
        path='/verify-email'
        element={
          <PublicRoute>
            <LazyVerifyEmail />
          </PublicRoute>
        }
      />
      <Route
        path='/reset-password'
        element={
          <PublicRoute>
            <LazyResetPassword />
          </PublicRoute>
        }
      />

      {/* T0213: Public user profile page */}
      <Route
        path='/u/:username'
        element={<LazyUserProfile />}
      />

      {/* Routes protégées */}
      <Route
        path='/dashboard'
        element={
          <ProtectedRoute>
            <ProtectedLayoutRoute>
              <LazyDashboard />
            </ProtectedLayoutRoute>
          </ProtectedRoute>
        }
      />
      <Route
        path='/marketplace'
        element={
          <ProtectedRoute>
            <ProtectedLayoutRoute>
              <LazyMarketplace />
            </ProtectedLayoutRoute>
          </ProtectedRoute>
        }
      />
      <Route
        path='/chat'
        element={
          <ProtectedRoute>
            <ProtectedLayoutRoute>
              <LazyChat />
            </ProtectedLayoutRoute>
          </ProtectedRoute>
        }
      />
      <Route
        path='/library'
        element={
          <ProtectedRoute>
            <ProtectedLayoutRoute>
              <LazyLibrary />
            </ProtectedLayoutRoute>
          </ProtectedRoute>
        }
      />
      <Route
        path='/profile'
        element={
          <ProtectedRoute>
            <ProtectedLayoutRoute>
              <LazyProfile />
            </ProtectedLayoutRoute>
          </ProtectedRoute>
        }
      />
      <Route
        path='/settings'
        element={
          <ProtectedRoute>
            <ProtectedLayoutRoute>
              <LazySettings />
            </ProtectedLayoutRoute>
          </ProtectedRoute>
        }
      />
      <Route
        path='/settings/sessions'
        element={
          <ProtectedRoute>
            <ProtectedLayoutRoute>
              <LazySessions />
            </ProtectedLayoutRoute>
          </ProtectedRoute>
        }
      />
      <Route
        path='/admin/roles'
        element={
          <ProtectedRoute>
            <ProtectedLayoutRoute>
              <LazyRoles />
            </ProtectedLayoutRoute>
          </ProtectedRoute>
        }
      />
      <Route
        path='/tracks/:id'
        element={
          <ProtectedRoute>
            <ProtectedLayoutRoute>
              <LazyTrackDetail />
            </ProtectedLayoutRoute>
          </ProtectedRoute>
        }
      />
      <Route
        path='/playlists/*'
        element={
          <ProtectedRoute>
            <ProtectedLayoutRoute>
              <LazyPlaylistRoutes />
            </ProtectedLayoutRoute>
          </ProtectedRoute>
        }
      />

      {/* Routes d'erreur */}
      <Route path='/404' element={<LazyNotFound />} />
      <Route path='/500' element={<LazyServerError />} />

      {/* Routes par défaut */}
      <Route path='/' element={<Navigate to='/dashboard' replace />} />
      <Route path='*' element={<Navigate to='/404' replace />} />
    </Routes>
  </Suspense>
);
