import { useMutation, useQuery } from '@tanstack/react-query';
import * as api from "../service/ProductService";
import {
  type CreateProductDetailRequestType,
  type CreateProductAttributeRequestType,
  type SaveProductRequestType,
  type UpdateVariantRequestType,
  type UpdateProductDetailRequestType,
  type UpdateProductAttributeRequestType,
  type UpdateProductRequestType
} from "../types/Types";
import { useSharedStore } from '@/shared/store/sharedStore';

export const useCreateProductDetail = () => {
  return useMutation({
    mutationFn: (variables: CreateProductDetailRequestType) => api.createProductDetail(variables),
  });
};

export const useCreateProductAttribute = () => {
  return useMutation({
    mutationFn: (variables: CreateProductAttributeRequestType) => api.createProductAttribute(variables),
  });
};

export const useSaveProduct = () => {
  return useMutation({
    mutationFn: (variables: SaveProductRequestType) => api.saveProduct(variables),
  });
};

export const useGetProducts = (page: number = 1, limit: number = 10) => {
  const { search } = useSharedStore();
  return useQuery({
    queryKey: ['products', page, limit, search],
    queryFn: () => api.getProducts(page, limit, search),
  });
};

export const useGetProductVariants = (productId: string) => {
  return useQuery({
    queryKey: ['product-variants', productId],
    queryFn: () => api.getProductVariants(productId),
    enabled: !!productId,
  });
};

export const useCreateProductVariant = () => {
  return useMutation({
    mutationFn: api.createProductVariant,
  });
};

export const useGetProductById = (id: string) => {
  return useQuery({
    queryKey: ['product-by-id', id],
    queryFn: () => api.getProductById(id),
    enabled: !!id,
  });
};

export const useUpdateVariant = () => {
  return useMutation({
    mutationFn: (variables: UpdateVariantRequestType) => api.updateVariant(variables),
  });
};

export const useGetVariantById = (productId: string, variantId: string) => {
  return useQuery({
    queryKey: ['variant-by-id', productId, variantId],
    queryFn: () => api.getVariantById(productId, variantId),
    enabled: !!productId && !!variantId,
  });
};

export const useUpdateProductDetail = () => {
  return useMutation({
    mutationFn: (variables: UpdateProductDetailRequestType) => api.updateProductDetail(variables),
  });
};

export const useUpdateProductAttribute = () => {
  return useMutation({
    mutationFn: (variables: UpdateProductAttributeRequestType) => api.updateProductAttribute(variables),
  });
};

export const useUpdateProduct = () => {
  return useMutation({
    mutationFn: (variables: UpdateProductRequestType) => api.updateProduct(variables),
  });
};

export const useSearchProducts = (search: string, enabled: boolean = true) => {
  return useQuery({
    queryKey: ['products-search', search],
    queryFn: () => api.getProducts(1, 50, search),
    enabled: enabled,
  });
};

export const useDeleteProduct = () => {
  return useMutation({
    mutationFn: (id: number) => api.deleteProduct(id),
  });
};

export const useGetProductReviews = (page: number = 1, limit: number = 10) => {
  return useQuery({
    queryKey: ['product-reviews', page, limit],
    queryFn: () => api.getProductReviews(page, limit),
  });
};

export const useApproveProductReview = () => {
  return useMutation({
    mutationFn:  api.approveProductReview,
  });
};

export const useGetProductQuestions = (page: number = 1, limit: number = 10) => {
  return useQuery({
    queryKey: ['product-questions', page, limit],
    queryFn: () => api.getProductQuestions(page, limit),
  });
};

export const useApproveProductQuestion = () => {
  return useMutation({
    mutationFn: api.approveProductQuestion,
  });
};

export const useAnswerProductQuestion = () => {
  return useMutation({
    mutationFn: api.answerProductQuestion,
  });
};

export const useDraftProduct = () => {
  return useMutation({
    mutationFn: api.draftProduct,
  });
};