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

/**
 * Who may view/edit/append this patient's record. The E2 viewer gates on `canView` **before** fetching the
 * record/history, so an unauthorized viewer never pulls clinical data (the two-stage-disclosure discipline —
 * the access decision lives here, not in the presentational card). Effectively static per session.
 */
export function useRecordAccess(patientId: number, options?: { enabled?: boolean }) {
  return useQuery({
    queryKey: recordKeys.access(patientId),
    queryFn: () => patientRecordsApi.getRecordAccess(patientId),
    enabled: (options?.enabled ?? true) && patientId > 0,
    staleTime: RECORD_ACCESS_STALE_TIME,
    gcTime: PATIENT_RECORDS_GC_TIME,
  });
}
