// src/providers/FlyersProvider.tsx
import React, { ReactNode } from 'react';
import { FlyersContext, FlyersContextType } from '../contexts/FlyersContext';
import type { Flyer } from '../types';
import * as apiClient from '../services/apiClient';
import { useInfiniteQuery } from '../hooks/useInfiniteQuery';

export const FlyersProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
  const {
    data: flyers,
    isLoading: isLoadingFlyers,
    error: flyersError,
    fetchNextPage: fetchNextFlyersPage,
    hasNextPage: hasNextFlyersPage,
    refetch: refetchFlyers,
    isRefetching: isRefetchingFlyers
  } = useInfiniteQuery<Flyer>(apiClient.fetchFlyers);

  const value: FlyersContextType = {
    flyers: flyers || [],
    isLoadingFlyers,
    flyersError,
    fetchNextFlyersPage,
    hasNextFlyersPage,
    isRefetchingFlyers,
    refetchFlyers,
  };

  return <FlyersContext.Provider value={value}>{children}</FlyersContext.Provider>;
};