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

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

    if (isLoading) {
        return (
            <div className="flex h-svh items-center justify-center">
                <Spinner className="size-6" />
            </div>
        );
    }

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

    return <Outlet />;
}
