import { keepPreviousData, useQuery } from '@tanstack/react-query';
import { adminApi } from '../apis';
import { adminKeys } from '../keys';
import { ADMIN_AUDIT_STALE_TIME, ADMIN_GC_TIME, AUDIT_PAGE_SIZE } from '../constants';
import type { AuditFilters } from '../types';

/** The append-only audit trail (read-only), filtered + paginated. Filters + page key the cache. */
export function useAuditLogs(filters: AuditFilters, page = 1) {
  return useQuery({
    queryKey: adminKeys.auditList(filters, { page, pageSize: AUDIT_PAGE_SIZE }),
    queryFn: () => adminApi.listAuditLogs(filters, { page, pageSize: AUDIT_PAGE_SIZE }),
    staleTime: ADMIN_AUDIT_STALE_TIME,
    gcTime: ADMIN_GC_TIME,
    placeholderData: keepPreviousData,
  });
}
