/**
 * CSV Rate Search Hook
 *
 * React hook for searching CSV-based rates with filters
 */

import { useState } from 'react';
import { searchCsvRates } from '@/lib/api/csv-rates';
import type { CsvRateSearchRequest, CsvRateSearchResponse } from '@/types/rate-filters';

interface UseCsvRateSearchResult {
  data: CsvRateSearchResponse | null;
  loading: boolean;
  error: string | null;
  search: (request: CsvRateSearchRequest) => Promise<void>;
  reset: () => void;
}

export function useCsvRateSearch(): UseCsvRateSearchResult {
  const [data, setData] = useState<CsvRateSearchResponse | null>(null);
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState<string | null>(null);

  const search = async (request: CsvRateSearchRequest) => {
    setLoading(true);
    setError(null);

    try {
      const response = await searchCsvRates(request);
      setData(response);
    } catch (err: any) {
      setError(err?.message || 'Failed to search rates');
      setData(null);
    } finally {
      setLoading(false);
    }
  };

  const reset = () => {
    setData(null);
    setError(null);
    setLoading(false);
  };

  return {
    data,
    loading,
    error,
    search,
    reset,
  };
}
