import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import * as api from "../service/ServiceService";
import {
  CreatePlanType,
  CreateServiceCategoryType,
  CreateServiceType,
  ToggleStatusCategoryType,
  UpdatePlanType,
} from "../types/ServiceTypes";

export const useGetAllServices = (
  search: string,
  page: number,
  category: string,
  status: string,
  isDanakSuggest?: boolean,
  limit?: number
) => {
  return useQuery({
    queryKey: [
      "services",
      search,
      page,
      category,
      status,
      isDanakSuggest,
      limit,
    ],
    queryFn: () =>
      api.getAllServicess(
        search,
        page,
        category,
        status,
        isDanakSuggest,
        limit
      ),
  });
};

export const useGetCategoryParents = () => {
  return useQuery({
    queryKey: ["service-category"],
    queryFn: api.getCategoryParent,
  });
};

export const useGetAllCategory = (
  page: number,
  search: string,
  isActive: "active" | "deactive" | ""
) => {
  return useQuery({
    queryKey: ["service-category-all", page, search, isActive],
    queryFn: () => api.getAllCategory(page, search, isActive),
  });
};

export const useCreateServiceCategory = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: (variables: CreateServiceCategoryType) =>
      api.createServiceCategory(variables),
    onSuccess: () => {
      queryClient.refetchQueries({
        queryKey: ["service-category-all"],
      });
    },
  });
};

export const useChangeStatusCategory = () => {
  return useMutation({
    mutationFn: (variables: ToggleStatusCategoryType) =>
      api.changeStatusCategory(variables),
  });
};

export const useCreateService = () => {
  return useMutation({
    mutationFn: (variables: CreateServiceType) => api.createService(variables),
  });
};

export const useSingleUpload = () => {
  return useMutation({
    mutationFn: (variables: FormData) => api.uploadSingle(variables),
  });
};
export const useMultiUpload = () => {
  return useMutation({
    mutationFn: (variables: FormData) => api.uploadMultiple(variables),
  });
};

export const useToggleStatusService = () => {
  return useMutation({
    mutationFn: (id: string) => api.toggleStatusService(id),
  });
};

export const useToggleSliderService = () => {
  return useMutation({
    mutationFn: (id: string) => api.toggleSliderService(id),
  });
};

export const useCreatePlan = () => {
  return useMutation({
    mutationFn: (variables: CreatePlanType) => api.createPlan(variables),
  });
};

export const useGetPlans = (serviceId?: string) => {
  return useQuery({
    queryKey: ["plans", serviceId],
    queryFn: () => api.getPlans(serviceId),
    enabled: !!serviceId,
  });
};

export const useToggleStatusPlan = () => {
  return useMutation({
    mutationFn: (variables: string) => api.planToggleStatus(variables),
  });
};

export const useGetPlanDetail = (planId?: string) => {
  return useQuery({
    queryKey: ["plans", planId],
    queryFn: () => api.getPlanDetail(planId),
    enabled: !!planId,
  });
};

export const useUpdatePlan = (id: string) => {
  return useMutation({
    mutationFn: (variables: UpdatePlanType) => api.updatePlan(id, variables),
  });
};

export const useGetReviews = (page: number = 1) => {
  return useQuery({
    queryKey: ["reviews", page],
    queryFn: () => api.getReviews(page),
  });
};

export const useChangeStatusReview = () => {
  return useMutation({
    mutationFn: (variables: { status: string; id: string }) =>
      api.changeStatusReview(variables),
  });
};

export const useGetDetailService = (id?: string) => {
  return useQuery({
    queryKey: ["service-detail", id],
    queryFn: () => api.getServiceDetail(id ? id : ""),
    enabled: !!id,
  });
};

export const useUpdateService = (id: string) => {
  return useMutation({
    mutationFn: (variables: CreateServiceType) =>
      api.updateService(id, variables),
  });
};

export const useGetCategoryDetail = (id: string, enabled: boolean) => {
  return useQuery({
    queryKey: ["category-detail", id],
    queryFn: () => api.getCategoryDetail(id),
    enabled: enabled,
  });
};

export const useUpdateCategory = (id: string) => {
  return useMutation({
    mutationFn: (variables: CreateServiceCategoryType) =>
      api.updateCategory(id, variables),
  });
};

export const useDeleteCategory = () => {
  return useMutation({
    mutationFn: (variables: string) => api.deleteCategory(variables),
  });
};
