import type { UsersData, UsersDetaiData } from "@/pages/users/lib/type";
import httpClient from "@/shared/config/api/httpClient";
import {
  DOWNLOAD_PDF,
  GET_ALL_USERS,
  UPDATE_USER,
} from "@/shared/config/api/URLs";
import type { AxiosResponse } from "axios";

const getAllUsers = async ({
  page,
  page_size,
  search,
}: {
  page: number;
  page_size: number;
  search: string;
}): Promise<AxiosResponse<UsersData>> => {
  const response = await httpClient.get(GET_ALL_USERS, {
    params: {
      page,
      page_size,
      search,
    },
  });
  return response;
};

const getUserDetail = async ({
  id,
}: {
  id: number;
}): Promise<AxiosResponse<UsersDetaiData>> => {
  const response = await httpClient.get(`${GET_ALL_USERS}${id}`);
  return response;
};

const downloadPdf = async (body: {
  order_id: number | null;
  lang: string;
}): Promise<AxiosResponse<any>> => {
  const response = await httpClient.post(DOWNLOAD_PDF, body, {
    responseType: "blob",
  });
  return response;
};

const updateUser = async ({
  body,
  id,
}: {
  body: {
    first_name: string;
    last_name: string;
    email: string | null;
    phone: string | null;
  };
  id: number;
}) => {
  const response = await httpClient.patch(`${UPDATE_USER}${id}/`, body);
  return response;
};

export { downloadPdf, getAllUsers, getUserDetail, updateUser };
