/**
 * @ai-summary Hook for searching nearby gas stations
 */

import { useMutation } from '@tanstack/react-query';
import { stationsApi } from '../api/stations.api';
import { Station, StationSearchRequest, ApiError } from '../types/stations.types';

interface UseStationsSearchOptions {
  onSuccess?: (stations: Station[]) => void;
  onError?: (error: ApiError) => void;
}

/**
 * Mutation hook for searching nearby stations
 * Not cached by default - each search is independent
 *
 * @example
 * ```tsx
 * const { mutate: search, isPending, data } = useStationsSearch();
 *
 * const handleSearch = async () => {
 *   search({
 *     latitude: 37.7749,
 *     longitude: -122.4194,
 *     radius: 5000
 *   });
 * };
 * ```
 */
export function useStationsSearch(options?: UseStationsSearchOptions) {
  return useMutation({
    mutationFn: async (request: StationSearchRequest) => {
      const stations = await stationsApi.searchStations(request);
      return stations;
    },
    onSuccess: (data) => {
      options?.onSuccess?.(data);
    },
    onError: (error) => {
      options?.onError?.(error as ApiError);
    }
  });
}
