import { axiosInstance } from "../config/api";

export const signup = (data) => {
  const url = "/auth/register";
  return new Promise((resolve, reject) => {
    axiosInstance
      .post(url, data)
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};

export const createAdmin = (data) => {
  const url = "/admin/user";
  return new Promise((resolve, reject) => {
    axiosInstance
      .post(url, data)
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};

export const getAdmins = (params) => {
  
  let searchParams = new URLSearchParams();
  searchParams.append("limit", params?.pagination?.pageSize ?? 10);
  searchParams.append("page", params?.pagination.pageIndex+1 ?? 1);
  searchParams.append("search", params?.globalFilter ?? "");
  
  const url = `/admin/?${searchParams.toString()}`;
  return new Promise((resolve, reject) => {
    axiosInstance
      .get(url)
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};

export const getEmailOtp = (data) => {
  const url = "/auth/send-otp";
  return new Promise((resolve, reject) => {
    axiosInstance
      .post(url, null, { params: data })
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};

export const verifyOtp = (data) => {
  const url = "/auth/verify-otp";
  return new Promise((resolve, reject) => {
    axiosInstance
      .post(url, data)
      .catch((err) => {
        if (err.status == 200) {
          resolve(err);
        } else {
          reject(err);
        }
      })
      .then((response) => resolve(response));
  });
};

export const forgotPassword = (data) => {
  const url = "/auth/admin/forget-password";
  return new Promise((resolve, reject) => {
    axiosInstance
      .post(url, null, { params: data })
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};

export const resetPassword = (data) => {
  const url = "/auth/admin/reset-password";
  return new Promise((resolve, reject) => {
    axiosInstance
      .post(url, data)
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};

export const updateAdmin = (data, id) => {
  const url = `/admin/user/${id}`;
  return new Promise((resolve, reject) => {
    axiosInstance
      .put(url, data)
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};

export const deleteAdmin = (id) => {
  const url = `/admin/user/${id}`;
  return new Promise((resolve, reject) => {
    axiosInstance
      .delete(url)
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};
