import { Navigate, Outlet, useLocation } from 'react-router-dom';
import LoadingAnimation from '@/components/ui/loading-animation';
import { useUser } from '@/contexts/user-context';

export function ProtectedRoute() {
    const { isAuthenticated, isLoading } = useUser();
    const location = useLocation();

    if (isLoading) {
        return <LoadingAnimation />;
    }

    if (!isAuthenticated) {
        return <Navigate to="/auth/login" state={{ from: location.pathname }} replace />;
    }

    return <Outlet />;
}
