import axios from "../../../config/axios";
import type {
  AddTrackCodeType,
  CompleteReturnType,
} from "../types/ReturnTypes";
import type {
  CancelReasonsResponseType,
  CancelsResponseType,
  CompleteCancelType,
  CreateCancelReasonType,
  CreateReturnReasonType,
  NativeOrdersResponseType,
  OrderDetailResponseType,
  ReturnOrderDetailResponseType,
  ReturnReasonsResponseType,
} from "../types/Types";

export const getNativeOrders = async (
  page: number = 1,
  status: string,
  search: string
): Promise<NativeOrdersResponseType> => {
  const { data } = await axios.get("/admin/orders/native", {
    params: { page, status: status === "all" ? undefined : status, q: search },
  });
  return data;
};

export const getSellerOrders = async (
  page: number = 1,
  status: string,
  search: string
): Promise<NativeOrdersResponseType> => {
  const { data } = await axios.get("/admin/orders/seller", {
    params: { page, status, q: search },
  });
  return data;
};

export const getOrderDetailUser = async (
  id: string
): Promise<OrderDetailResponseType> => {
  const { data } = await axios.get(`/admin/orders/${id}/user`);
  return data;
};

export const createCancelReason = async (params: CreateCancelReasonType) => {
  const { data } = await axios.post(`/admin/orders/cancel/reasons`, params);
  return data;
};

export const getCancelReasons = async (): Promise<
  CancelReasonsResponseType
> => {
  const { data } = await axios.get(`/cancel/reasons`);
  return data;
};

export const deleteCancelReason = async (id: string) => {
  const { data } = await axios.delete(`/admin/orders/reasons/${id}/delete`);
  return data;
};

export const createReturnReason = async (params: CreateReturnReasonType) => {
  const { data } = await axios.post(`/admin/orders/returns/reasons`, params);
  return data;
};

export const getReturnReasons = async (): Promise<
  ReturnReasonsResponseType
> => {
  const { data } = await axios.get(`/returns/reasons`);
  return data;
};

export const deleteReturnReason = async (id: string) => {
  const { data } = await axios.delete(`/admin/orders/returns/reasons/${id}`);
  return data;
};

export const getOrderReturns = async (page: number = 1) => {
  const { data } = await axios.get(`/admin/orders/returns`, {
    params: { page },
  });
  return data;
};

export const addTrackCode = async (params: AddTrackCodeType) => {
  const { data } = await axios.post(
    `/order/${params.orderId}/admin/track-code`,
    params
  );
  return data;
};

export const getReturnDetails = async (
  id: string
): Promise<ReturnOrderDetailResponseType> => {
  const { data } = await axios.get(`/admin/orders/returns/${id}`);
  return data;
};

export const approveReturn = async (id: string) => {
  const { data } = await axios.post(`/admin/orders/returns/${id}/approve`);
  return data;
};

export const rejectReturn = async (id: string) => {
  const { data } = await axios.post(`/admin/orders/returns/${id}/reject`);
  return data;
};

export const compeleteReturn = async (
  id: string,
  params: CompleteReturnType
) => {
  const { data } = await axios.post(
    `/admin/orders/returns/${id}/complete`,
    params
  );
  return data;
};

export const getCancels = async (
  page: number = 1
): Promise<CancelsResponseType> => {
  const { data } = await axios.get(`/admin/orders/cancel`, {
    params: { page },
  });
  return data;
};

export const completeCancel = async (
  id: string,
  params: CompleteCancelType
) => {
  const { data } = await axios.post(
    `/admin/orders/cancel/${id}/complete`,
    params
  );
  return data;
};
