import {
  API_ENDPOINTS,
  companiesService,
  companyCategoriesService,
} from "@/lib/api";
import {
  useInfiniteQuery,
  useMutation,
  useQuery,
  useQueryClient,
} from "@tanstack/react-query";
import { useRouter } from "next/navigation";
import { useMemo } from "react";
import { toast } from "react-toastify";

export const useCreateCompany = () => {
  const queryClient = useQueryClient();
  const router = useRouter();
  const mutationKey = useMemo(() => [API_ENDPOINTS.COMPANIES, "create"], []);
  return useMutation({
    mutationKey,
    mutationFn: companiesService.createCompany,
    onSuccess: () => {
      toast.success("Company created successfully");
      queryClient.invalidateQueries({
        queryKey: [API_ENDPOINTS.COMPANIES],
      });
      router.push("/companies");
    },
  });
};

export const useUpdateCompany = (id: string) => {
  const queryClient = useQueryClient();
  const router = useRouter();
  const mutationKey = useMemo(
    () => [API_ENDPOINTS.COMPANIES.UPDATE(id), "update"],
    [id],
  );
  return useMutation({
    mutationKey,
    mutationFn: companiesService.updateCompany,
    onSuccess: () => {
      toast.success("Company updated successfully");
      queryClient.invalidateQueries({
        queryKey: [API_ENDPOINTS.COMPANIES.READ_ALL],
      });
      router.push("/companies");
    },
  });
};

export const useCompanyDetails = (id: string) => {
  const queryKey = useMemo(
    () => [API_ENDPOINTS.COMPANIES.DETAILS(id), "details"],
    [id],
  );
  return useQuery({
    queryKey,
    queryFn: () => companiesService.companyDetails(id),
  });
};
export const useDeleteCompanyQuery = (successCallback?: () => void) => {
  const queryClient = useQueryClient();
  const mutationKey = useMemo(
    () => [API_ENDPOINTS.COMPANIES.DELETE, "delete-company"],
    [],
  );
  return useMutation({
    mutationKey,
    mutationFn: companiesService.deleteCompany,
    onSuccess: (response: { data: { message: string } }) => {
      toast.success(response.data.message);
      if (successCallback) {
        successCallback();
      }
      queryClient.invalidateQueries({
        queryKey: [API_ENDPOINTS.COMPANIES.READ_ALL],
      });
    },
  });
};
export const useCompanyFullList = () => {
  return useQuery({
    queryKey: ["READ ALL Companies"],
    queryFn: companiesService.getCompanies,
  });
};
export const useCompaniesInfiniteQuery = (params?: Record<string, any>) => {
  const queryKey = useMemo(
    () => [API_ENDPOINTS.COMPANIES.READ_ALL, params],
    [params],
  );

  return useInfiniteQuery({
    queryKey,

    queryFn: ({ pageParam = 0 }) =>
      companiesService.getCompanies({ ...params, offset: pageParam }),

    initialPageParam: 0,

    getNextPageParam: (lastPage: any) => {
      const { offset, limit, total } = lastPage.meta;

      const nextOffset = offset + limit;

      return nextOffset < total ? nextOffset : undefined;
    },
  });
};
export const useCompanyCategoriesQuery = (params?: Record<string, any>) => {
  const queryKey = useMemo(
    () => [API_ENDPOINTS.COMPANIES.CATEGORIES.READ_ALL, params],
    [params],
  );
  return useQuery({
    queryKey,
    queryFn: () => companyCategoriesService.getCategories({ ...params }),
  });
};
export const useCreateCompanyCategory = (successCallback?: () => void) => {
  const queryClient = useQueryClient();
  const router = useRouter();
  const mutationKey = useMemo(
    () => [API_ENDPOINTS.COMPANIES.CATEGORIES, "create"],
    [],
  );
  return useMutation({
    mutationKey,
    mutationFn: companyCategoriesService.createCategory,
    onSuccess: (response: { data: { message: string } }) => {
      toast.success(response.data.message);
      if (successCallback) {
        successCallback();
      }
      queryClient.invalidateQueries({
        queryKey: [API_ENDPOINTS.COMPANIES.CATEGORIES.READ_ALL],
      });
      router.push("/companies/categories");
    },
  });
};
export const useDeleteCompanyCategoryQuery = (successCallback?: () => void) => {
  const queryClient = useQueryClient();
  const mutationKey = useMemo(
    () => [
      API_ENDPOINTS.COMPANIES.CATEGORIES.DELETE,
      "delete-company-category",
    ],
    [],
  );
  return useMutation({
    mutationKey,
    mutationFn: companyCategoriesService.deleteCategory,
    onSuccess: (response: { data: { message: string } }) => {
      toast.success(response.data.message);
      if (successCallback) {
        successCallback();
      }
      queryClient.invalidateQueries({
        queryKey: [API_ENDPOINTS.COMPANIES.CATEGORIES.READ_ALL],
      });
    },
  });
};
export const useToggleCompanyCategoryPublished = () => {
  const queryClient = useQueryClient();
  const mutationKey = useMemo(
    () => [
      API_ENDPOINTS.COMPANIES.CATEGORIES.TOGGLE_PUBLISHED("published"),
      "published",
    ],
    [],
  );
  return useMutation({
    mutationKey,
    mutationFn: companyCategoriesService.togglePublished,
    onSuccess: ({ data }: { data: { message: string } }) => {
      toast.success(data.message);
      queryClient.invalidateQueries({
        queryKey: [API_ENDPOINTS.COMPANIES.CATEGORIES.READ_ALL],
      });
    },
  });
};
export const useCompanyCategoriesDetails = (id: string) => {
  const queryKey = useMemo(
    () => [API_ENDPOINTS.COMPANIES.CATEGORIES.DETAILS(id), "details"],
    [id],
  );
  return useQuery({
    queryKey,
    queryFn: () => companyCategoriesService.getSingleCategory(id),
  });
};
export const useUpdateCompanyCategories = (id: string) => {
  const queryClient = useQueryClient();
  const router = useRouter();
  const mutationKey = useMemo(
    () => [API_ENDPOINTS.COMPANIES.CATEGORIES.UPDATE(id), "update"],
    [id],
  );
  return useMutation({
    mutationKey,
    mutationFn: companyCategoriesService.updateCategories,
    onSuccess: ({ data }: { data: { message: string } }) => {
      toast.success(data.message);
      queryClient.invalidateQueries({
        queryKey: [API_ENDPOINTS.COMPANIES.CATEGORIES.READ_ALL],
      });
      router.push("/companies/categories");
    },
  });
};
