create or replace function public.delete_current_user_profile_data()
returns void
language plpgsql
security definer
set search_path = public
as $$
declare
  v_user_id uuid;
begin
  v_user_id := auth.uid();

  if v_user_id is null then
    raise exception 'Not authenticated';
  end if;

  delete from public.training_plans
  where user_id = v_user_id;

  delete from public.assessments
  where user_id = v_user_id;

  delete from public.user_profiles
  where user_id = v_user_id;
end;
$$;

revoke all on function public.delete_current_user_profile_data() from public;
grant execute on function public.delete_current_user_profile_data() to authenticated;
