from django.shortcuts import redirect
from functools import wraps

from apps.carers.models import Profile


def check_setup_completion(view_func):
    @wraps(view_func)
    def _wrapped_view(request, *args, **kwargs):
        # Assuming the request.user is the User instance and has a related Profile
        if not request.user.is_authenticated:
            return redirect('login')  # Redirect to login if user is not logged in

        # Check if the profile exists and has completed setup
        profile, created = Profile.objects.get_or_create(user=request.user)
        if not profile.has_completed_setup:
            return redirect('setup_flow_1')  # Redirect to the setup flow start

        return view_func(request, *args, **kwargs)
    return _wrapped_view