/**
 * Filter Options Hook
 *
 * React hook for fetching available filter options
 */

import { useState, useEffect } from 'react';
import { getFilterOptions } from '@/lib/api/csv-rates';
import type { FilterOptions } from '@/types/rate-filters';

interface UseFilterOptionsResult extends FilterOptions {
  loading: boolean;
  error: string | null;
  refetch: () => Promise<void>;
}

export function useFilterOptions(): UseFilterOptionsResult {
  const [options, setOptions] = useState<FilterOptions>({
    companies: [],
    containerTypes: [],
    currencies: [],
  });
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState<string | null>(null);

  const fetchOptions = async () => {
    setLoading(true);
    setError(null);

    try {
      const response = await getFilterOptions();
      setOptions(response);
    } catch (err: any) {
      setError(err?.message || 'Failed to fetch filter options');
    } finally {
      setLoading(false);
    }
  };

  useEffect(() => {
    fetchOptions();
  }, []);

  return {
    ...options,
    loading,
    error,
    refetch: fetchOptions,
  };
}
