import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import * as api from "../service/Service";
import { ProductsResponse } from "@/types/products.types";

interface UseProductsDataParams {
  page?: number;
  limit?: number;
  category?: string;
  brand?: string;
  minPrice?: number;
  maxPrice?: number;
  attributes?: string[];
  sizes?: number[];
  meterages?: number[];
  sort?: string;
  wholeSale?: string;
  stock?: string;
  rating?: number;
}

export const useProductsData = (params?: UseProductsDataParams) => {
  return useQuery<ProductsResponse>({
    queryKey: ["products", params],
    queryFn: () => api.getProducts(params),
  });
};

export const useCategoryProductsData = (
  categoryUrl: string,
  params?: UseProductsDataParams
) => {
  return useQuery<ProductsResponse>({
    queryKey: ["category-products", categoryUrl, params],
    queryFn: () => api.getCategoryProducts(categoryUrl, params),
  });
};

export const useAddWishlist = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: api.addWishlist,
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["with-list"] });
    },
  });
};

export const useRemoveWishlist = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: api.removeWishlist,
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["with-list"] });
    },
  });
};
