import { useMutation, useQueryClient } from '@tanstack/react-query';
import { verificationApi } from '../apis';
import { verificationKeys } from '../keys';
import type { CredentialDetailsInput } from '../types';

/**
 * Persists the structured professional-credential details (INO number, specialties, license fields) the
 * registry needs. The credential **documents** themselves move their steps to `in_review` as they
 * upload (via `useUploadVerificationDocument`); this finalises B5 by saving the structured metadata,
 * then invalidates the status so the checklist / B6 reflect the in-review credential steps. A missing
 * INO number surfaces as `400` inline. (No nurse-facing b6 endpoint accepts these yet — gap filed;
 * mock-persisted meanwhile.)
 */
export function useSubmitCredentials() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: (input: CredentialDetailsInput) => verificationApi.submitCredentialDetails(input),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: verificationKeys.status() });
    },
  });
}
