import { useQuery } from '@tanstack/react-query';
import { listingsApi, type SearchListingsParams } from '@/lib/listings-api';

export const listingsKeys = {
  all: ['listings'] as const,
  search: (params: SearchListingsParams) => ['listings', 'search', params] as const,
  detail: (id: string) => ['listings', 'detail', id] as const,
};

export function useListingsSearch(params: SearchListingsParams = {}) {
  return useQuery({
    queryKey: listingsKeys.search(params),
    queryFn: () => listingsApi.search(params),
  });
}

export function useListingDetail(id: string) {
  return useQuery({
    queryKey: listingsKeys.detail(id),
    queryFn: () => listingsApi.getById(id),
    enabled: !!id,
  });
}
