import { useMutation, useQueryClient } from '@tanstack/react-query';
import { patientsApi } from '../apis';
import { patientKeys } from '../keys';
import type { UpdatePatientInput } from '../types';

/**
 * Updates a patient (the A4 form reused for edit) and invalidates the lists so the card
 * reflects the change. A cross-tenant id returns 404 server-side (tenancy is enforced there).
 */
export function useUpdatePatient() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({ id, input }: { id: number; input: UpdatePatientInput }) => patientsApi.update(id, input),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: patientKeys.lists() });
    },
  });
}
