import { useQuery } from '@tanstack/react-query';
import { patientRecordsApi } from '../apis';
import { recordKeys } from '../keys';
import { FAMILY_RECORD_STALE_TIME, PATIENT_RECORDS_GC_TIME } from '../constants';

/**
 * The family-owned, patient-scoped editable record (medications/routine/tasks). Keyed per patient; a customer
 * edit invalidates this key. A `403` (access-denied) surfaces as the query's error — the E2 screen gates on
 * `useRecordAccess` first, so this normally only runs for an authorized viewer.
 */
export function usePatientCareRecord(patientId: number, options?: { enabled?: boolean }) {
  return useQuery({
    queryKey: recordKeys.patient(patientId),
    queryFn: () => patientRecordsApi.getFamilyRecord(patientId),
    enabled: (options?.enabled ?? true) && patientId > 0,
    staleTime: FAMILY_RECORD_STALE_TIME,
    gcTime: PATIENT_RECORDS_GC_TIME,
  });
}
