import { useQuery } from '@tanstack/react-query';
import { verificationApi } from '../apis';
import { verificationKeys } from '../keys';
import { SIGNED_DOCUMENT_URL_GC_TIME, SIGNED_DOCUMENT_URL_STALE_TIME } from '../constants';

/**
 * A document's **short-lived signed GET URL**, fetched on demand when the viewer opens a document (pass
 * `null` while none is open). Short `staleTime` + short `gcTime` keep the URL out of long-term cache — a
 * reopen re-signs a fresh URL rather than reusing an expired one. `retry: false`: a failed/expired sign is
 * surfaced to the viewer's error/re-request path, not silently re-hammered.
 */
export function useVerificationDocumentUrl(documentId: number | null) {
  return useQuery({
    queryKey: verificationKeys.adminDocumentUrl(documentId ?? -1),
    queryFn: () => verificationApi.getDocumentSignedUrl(documentId as number),
    enabled: documentId != null,
    staleTime: SIGNED_DOCUMENT_URL_STALE_TIME,
    gcTime: SIGNED_DOCUMENT_URL_GC_TIME,
    retry: false,
  });
}
