import { useMutation, useQuery } from "@tanstack/react-query";
import * as api from "../service/OrderService";
import { useSharedStore } from "@/shared/store/sharedStore";
import type { CompleteReturnType } from "../types/ReturnTypes";
import type { CompleteCancelType } from "../types/Types";

export const useGetNativeOrders = (
  page: number = 1,
  status: string = "all"
) => {
  const { search } = useSharedStore();
  const { data, isLoading, error } = useQuery({
    queryKey: ["orders", page, status, search],
    queryFn: () => api.getNativeOrders(page, status, search),
  });
  return { data, isLoading, error };
};

export const useGetSellerOrders = (
  page: number = 1,
  status: string = "all"
) => {
  const { search } = useSharedStore();
  const { data, isLoading, error } = useQuery({
    queryKey: ["orders", page, status, search],
    queryFn: () => api.getSellerOrders(page, status, search),
  });
  return { data, isLoading, error };
};

export const useGetOrderDetailUser = (id: string) => {
  return useQuery({
    queryKey: ["order-detail-user", id],
    queryFn: () => api.getOrderDetailUser(id),
    enabled: !!id,
  });
};

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

export const useGetCancelReasons = () => {
  return useQuery({
    queryKey: ["cancel-reasons"],
    queryFn: api.getCancelReasons,
  });
};

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

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

export const useGetReturnReasons = () => {
  return useQuery({
    queryKey: ["return-reasons"],
    queryFn: api.getReturnReasons,
  });
};

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

export const useGetOrderReturns = (page: number = 1) => {
  return useQuery({
    queryKey: ["order-returns", page],
    queryFn: () => api.getOrderReturns(page),
  });
};

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

export const useGetReturnDetails = (id: string) => {
  return useQuery({
    queryKey: ["return-details", id],
    queryFn: () => api.getReturnDetails(id),
    enabled: !!id,
  });
};

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

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

export const useCompleteReturn = () => {
  return useMutation({
    mutationFn: ({ id, params }: { id: string; params: CompleteReturnType }) =>
      api.compeleteReturn(id, params),
  });
};

export const useGetCancels = (page: number = 1) => {
  return useQuery({
    queryKey: ["cancels", page],
    queryFn: () => api.getCancels(page),
  });
};

export const useCompleteCancel = () => {
  return useMutation({
    mutationFn: ({ id, params }: { id: string; params: CompleteCancelType }) =>
      api.completeCancel(id, params),
  });
};
