import { useMutation, useQueryClient } from '@tanstack/react-query';
import { patientRecordsApi } from '../apis';
import { recordKeys } from '../keys';
import type { FamilyCareRecord, UpdateFamilyRecordRequest } from '../types';

/**
 * The **customer** edit of the family-owned record (medications/routine/tasks). Bound to one patient. On
 * success we write the returned record straight into the cache (`setQueryData`) so the edit shows without a
 * refetch flash. This hook is **customer-only** — it must **never** be wired into the nurse view (the nurse is
 * append-only).
 */
export function useUpdateCareRecord(patientId: number) {
  const queryClient = useQueryClient();
  return useMutation<FamilyCareRecord, unknown, UpdateFamilyRecordRequest>({
    mutationFn: (body) => patientRecordsApi.updateFamilyRecord(patientId, body),
    onSuccess: (updated) => {
      queryClient.setQueryData(recordKeys.patient(patientId), updated);
    },
  });
}
