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

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

  // Wait for auth state to be initialized
  if (!isInitialized) {
    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 not authenticated, redirect to internal login page
  if (!user) {
    return <Navigate to="/auth/login" replace />;
  }

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

export default AuthRoute;
