import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom'
import './App.css'
import Register from './pages/Auth/Register'
import Login from './pages/Auth/Login'
import RecipeList from './pages/Recipes/RecipeList'
import RecipeDetail from './pages/Recipes/RecipeDetail'
// import Favorites from './pages/Recipes/Favorites'
import Profile from './pages/Profile'
import Home from './pages/Home'
import { MainLayout } from './layouts/MainLayout'
import RecipeForm from "@/pages/Recipes/RecipeForm"
import useAuth from '@/hooks/useAuth'
import { ProtectedRoute, AuthRoute } from '@/components/RouteGuards'
import ResetPassword from "@/pages/ResetPassword"

function App() {
  const { isAuthenticated, isLoading } = useAuth();

  if (isLoading) {
    return <div>Chargement de l'application...</div>;
  }

  return (
    <BrowserRouter>
      <MainLayout>
        <Routes>
          {/* Routes d'authentification */}
          <Route path="/auth/register" element={<AuthRoute><Register /></AuthRoute>} />
          <Route path="/auth/login" element={<AuthRoute><Login /></AuthRoute>} />

          {/* Routes publiques */}
          <Route path="/recipes" element={<ProtectedRoute><RecipeList /></ProtectedRoute>} />
          <Route path="/recipes/:id" element={<ProtectedRoute><RecipeDetail /></ProtectedRoute>} />

          {/* Routes protégées */}
          <Route path="/recipes/new" element={<ProtectedRoute><RecipeForm /></ProtectedRoute>} />
          <Route path="/profile" element={<ProtectedRoute><Profile /></ProtectedRoute>} />

          {/* Route racine avec redirection conditionnelle */}
          <Route path="/" element={isAuthenticated ? <Navigate to="/recipes" replace /> : <Home />} />

          {/* Route de fallback pour les URLs non trouvées */}
          <Route path="*" element={<Navigate to="/" replace />} />

          {/* Nouvelle route pour la réinitialisation du mot de passe */}
          <Route path="/reset-password" element={<ResetPassword />} />
        </Routes>
      </MainLayout>
    </BrowserRouter>
  )
}

export default App
