/**
 * useSellerProducts Hook
 * TanStack Query hooks for managing seller's products
 */

import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { marketplaceService } from '../../../services/marketplace.service';
import type {
  SellerProduct,
  SellerProductFilters,
  PaginatedResponse,
} from '../../../types/marketplace.types';

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

export const sellerProductKeys = {
  all: ['seller', 'products'] as const,
  list: (filters?: SellerProductFilters) => [...sellerProductKeys.all, 'list', filters] as const,
  detail: (id: string) => [...sellerProductKeys.all, 'detail', id] as const,
};

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

/**
 * Hook for fetching seller's products
 */
export function useSellerProducts(filters?: SellerProductFilters) {
  return useQuery<PaginatedResponse<SellerProduct>, Error>({
    queryKey: sellerProductKeys.list(filters),
    queryFn: () => marketplaceService.getSellerProducts(filters),
    staleTime: 1000 * 60 * 5, // 5 minutes
  });
}

/**
 * Hook for fetching a single seller product
 */
export function useSellerProduct(id: string) {
  return useQuery<SellerProduct, Error>({
    queryKey: sellerProductKeys.detail(id),
    queryFn: () => marketplaceService.getSellerProductById(id),
    enabled: !!id,
    staleTime: 1000 * 60 * 5,
  });
}

/**
 * Hook for updating a seller product
 */
export function useUpdateSellerProduct() {
  const queryClient = useQueryClient();

  return useMutation<
    SellerProduct,
    Error,
    { id: string; data: Partial<SellerProduct> }
  >({
    mutationFn: ({ id, data }) => marketplaceService.updateSellerProduct(id, data),
    onSuccess: (data, variables) => {
      // Update cache with updated product
      queryClient.setQueryData<SellerProduct>(
        sellerProductKeys.detail(variables.id),
        data
      );
      // Invalidate list to reflect changes
      queryClient.invalidateQueries({ queryKey: sellerProductKeys.list() });
    },
  });
}

/**
 * Hook for toggling product status (active/inactive)
 */
export function useToggleProductStatus() {
  const queryClient = useQueryClient();

  return useMutation<
    SellerProduct,
    Error,
    { id: string; status: 'active' | 'inactive' }
  >({
    mutationFn: ({ id, status }) => marketplaceService.toggleProductStatus(id, status),
    onSuccess: (data, variables) => {
      queryClient.setQueryData<SellerProduct>(
        sellerProductKeys.detail(variables.id),
        data
      );
      queryClient.invalidateQueries({ queryKey: sellerProductKeys.list() });
    },
  });
}

/**
 * Hook for deleting a seller product
 */
export function useDeleteSellerProduct() {
  const queryClient = useQueryClient();

  return useMutation<void, Error, string>({
    mutationFn: (id) => marketplaceService.deleteSellerProduct(id),
    onSuccess: (_, id) => {
      // Remove from cache
      queryClient.removeQueries({ queryKey: sellerProductKeys.detail(id) });
      // Invalidate list
      queryClient.invalidateQueries({ queryKey: sellerProductKeys.list() });
    },
  });
}
