import { Navigate, Outlet } from 'react-router-dom';
import { useAuth } from '../../contexts/AuthContext'; // Adjust path if necessary

const GuestRoute = () => {
  const { user, isInitialized } = useAuth();

  // Wait for auth state to be initialized
  if (!isInitialized) {
    // You might want a loading spinner here
    return (
      <div className="min-h-screen flex items-center justify-center bg-campfire-dark">
         <div className="animate-spin rounded-full h-12 w-12 border-t-2 border-b-2 border-campfire-amber"></div>
      </div>
    );
  }

  // If user is authenticated, redirect them away from guest-only routes
  // Redirect to profile page or home page
  if (user) {
    // Assuming userProfile contains username, otherwise redirect to a default authenticated page like '/'
    // If userProfile is not immediately available, you might redirect to a loading page or '/'
    // For now, let's redirect to the home page or profile if username is available
    // A simple redirect to '/' is safer if userProfile isn't guaranteed here
    return <Navigate to="/" replace />;
  }

  // If user is not authenticated, render the child routes
  return <Outlet />;
};

export default GuestRoute;
