import { useMutation, useQueryClient } from '@tanstack/react-query';
import { authKeys } from '@/services/auth/keys';
import { profilesApi } from '../apis';
import { profileKeys } from '../keys';
import type { UpsertNurseProfileInput } from '../types';

/**
 * Bootstraps (first entry) or edits the nurse profile via the single b3 upsert. `isVerified`
 * is never sent — it stays server-owned/false. Writes the fresh profile to cache and
 * invalidates `/me` so `hasNurseProfile` reflects the bootstrap.
 */
export function useUpsertNurseProfile() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (input: UpsertNurseProfileInput) => profilesApi.upsertNurseProfile(input),
    onSuccess: (profile) => {
      queryClient.setQueryData(profileKeys.nurse(), profile);
      queryClient.invalidateQueries({ queryKey: authKeys.me() });
    },
  });
}
