import { useQuery, type UseQueryResult } from '@tanstack/react-query';
import { searchAll } from '@/api/search';
import type { UnifiedSearchResponse } from '@/types';
import { STALE_TIME } from './refreshConfig';

export function useUnifiedSearch(
  query: string,
  opts: { deviceId?: string; limit?: number } = {},
): UseQueryResult<UnifiedSearchResponse> {
  const trimmed = query.trim();
  return useQuery({
    queryKey: ['search', 'unified', trimmed, opts.deviceId ?? null, opts.limit ?? null],
    queryFn: () => searchAll(trimmed, opts),
    enabled: trimmed.length >= 2,
    staleTime: STALE_TIME,
  });
}
