import { useState, useEffect } from 'react';
import { homeService, HomeBanner } from '../api/homeService';

export interface UseHomeBannersReturn {
  data: HomeBanner[] | null;
  loading: boolean;
  error: string | null;
  refetch: () => Promise<void>;
}

/**
 * Hook to fetch home banners
 */
export const useHomeBanners = (): UseHomeBannersReturn => {
  const [data, setData] = useState<HomeBanner[] | null>(null);
  const [loading, setLoading] = useState<boolean>(true);
  const [error, setError] = useState<string | null>(null);

  const fetchData = async () => {
    try {
      setLoading(true);
      setError(null);
      const result = await homeService.getBanners();
      setData(result);
    } catch (err) {
      setError(err instanceof Error ? err.message : 'An error occurred');
    } finally {
      setLoading(false);
    }
  };

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

  return {
    data,
    loading,
    error,
    refetch: fetchData,
  };
};

