/**
 * useMarketplaceProducts Hook
 * TanStack Query hook for fetching marketplace products with filters
 */

import { useQuery, useInfiniteQuery } from '@tanstack/react-query';
import { marketplaceService } from '../../../services/marketplace.service';
import type {
  MarketplaceProductListItem,
  MarketplaceFilters,
  PaginatedResponse,
} from '../../../types/marketplace.types';

// ============================================================================
// Query Keys
// ============================================================================

export const marketplaceKeys = {
  all: ['marketplace'] as const,
  products: () => [...marketplaceKeys.all, 'products'] as const,
  productsList: (filters: MarketplaceFilters) =>
    [...marketplaceKeys.products(), filters] as const,
  productsInfinite: (filters: MarketplaceFilters) =>
    [...marketplaceKeys.products(), 'infinite', filters] as const,
  featured: () => [...marketplaceKeys.all, 'featured'] as const,
  popular: () => [...marketplaceKeys.all, 'popular'] as const,
  related: (productId: string) =>
    [...marketplaceKeys.all, 'related', productId] as const,
};

// ============================================================================
// Hooks
// ============================================================================

/**
 * Hook for fetching paginated marketplace products
 */
export function useMarketplaceProducts(filters: MarketplaceFilters = {}) {
  return useQuery<PaginatedResponse<MarketplaceProductListItem>, Error>({
    queryKey: marketplaceKeys.productsList(filters),
    queryFn: () => marketplaceService.getProducts(filters),
    staleTime: 1000 * 60 * 5, // 5 minutes
  });
}

/**
 * Hook for infinite scroll products loading
 */
export function useMarketplaceProductsInfinite(
  filters: Omit<MarketplaceFilters, 'page'>
) {
  return useInfiniteQuery({
    queryKey: marketplaceKeys.productsInfinite(filters),
    queryFn: ({ pageParam = 1 }) =>
      marketplaceService.getProducts({ ...filters, page: pageParam as number }),
    getNextPageParam: (lastPage) => {
      if (lastPage.page < lastPage.totalPages) {
        return lastPage.page + 1;
      }
      return undefined;
    },
    initialPageParam: 1,
    staleTime: 1000 * 60 * 5,
  });
}

/**
 * Hook for fetching featured products
 */
export function useFeaturedProducts(limit = 6) {
  return useQuery<MarketplaceProductListItem[], Error>({
    queryKey: [...marketplaceKeys.featured(), limit],
    queryFn: () => marketplaceService.getFeaturedProducts(limit),
    staleTime: 1000 * 60 * 10, // 10 minutes
  });
}

/**
 * Hook for fetching popular products
 */
export function usePopularProducts(limit = 6) {
  return useQuery<MarketplaceProductListItem[], Error>({
    queryKey: [...marketplaceKeys.popular(), limit],
    queryFn: () => marketplaceService.getPopularProducts(limit),
    staleTime: 1000 * 60 * 10,
  });
}

/**
 * Hook for fetching related products
 */
export function useRelatedProducts(productId: string, limit = 4) {
  return useQuery<MarketplaceProductListItem[], Error>({
    queryKey: [...marketplaceKeys.related(productId), limit],
    queryFn: () => marketplaceService.getRelatedProducts(productId, limit),
    enabled: !!productId,
    staleTime: 1000 * 60 * 10,
  });
}
