import * as api from "../service/SellerService";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import type {
  ChangeStatusDocumentType,
  CreateCategoryLearning,
  CreateLearningType,
} from "../types/Types";
import { useSharedStore } from "@/shared/store/sharedStore";

export const useGetSellers = (page: number = 1, enabled: boolean = true) => {
  const { search } = useSharedStore();
  return useQuery({
    queryKey: ["sellers", page, search],
    queryFn: () => api.getSellers(page, search),
    enabled,
  });
};

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

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

export const useGetWholesaleRequests = (page: number = 1) => {
  return useQuery({
    queryKey: ["wholesale-requests", page],
    queryFn: () => api.getWholesaleRequests(page),
  });
};

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

export const useGetWithdrawalRequests = (page: number = 1) => {
  return useQuery({
    queryKey: ["withdrawal-requests", page],
    queryFn: () => api.getWithdrawalRequests(page),
  });
};

export const useGetAnnouncementSeller = (page: number = 1) => {
  return useQuery({
    queryKey: ["announcement-seller", page],
    queryFn: () => api.getAnnouncementSeller(page),
  });
};

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

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

export const useGetRequestCreateProduct = (page: number = 1) => {
  return useQuery({
    queryKey: ["request-create-product", page],
    queryFn: () => api.getRequestCreateProduct(page),
  });
};

export const useGetLearningCategory = () => {
  return useQuery({
    queryKey: ["learning-category"],
    queryFn: () => api.getLearningCategory(),
  });
};

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

export const useUpdateLearningCategory = () => {
  return useMutation({
    mutationFn: ({
      id,
      params,
    }: {
      id: string;
      params: CreateCategoryLearning;
    }) => api.updateLearningCategory(id, params),
  });
};

export const useGetLearning = () => {
  return useQuery({
    queryKey: ["learning"],
    queryFn: () => api.getLearning(),
  });
};

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

export const useUpdateLearning = () => {
  return useMutation({
    mutationFn: ({ id, params }: { id: string; params: CreateLearningType }) =>
      api.updateLearning(id, params),
  });
};

export const useGetLearningById = (id: string, enabled: boolean = true) => {
  return useQuery({
    queryKey: ["learning", id],
    queryFn: () => api.getLearningById(id),
    enabled: enabled && !!id,
  });
};

export const useDeleteLearning = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: api.deleteLearning,
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["learning"] });
    },
  });
};

export const useGetContentContract = () => {
  return useQuery({
    queryKey: ["content-contract"],
    queryFn: api.getContentContract,
  });
};

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

export const useChangeStatusDocument = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: ({
      sellerId,
      params,
    }: {
      sellerId: string;
      params: ChangeStatusDocumentType;
    }) => api.changeStatusDocument(sellerId, params),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["sellers"] });
    },
  });
};
