import { axiosInstance } from "../config/api";
import { userDataMock } from "../mock/users";

// export const getUsers = (params) => {
//   let searchParams = new URLSearchParams();
//   searchParams.append('size', params?.pagination?.pageSize ?? 10);
//   searchParams.append('page', params?.pagination.pageIndex ?? 0);
//   if (params?.globalFilter) searchParams.append('search', params?.globalFilter);
//   if (params?.sortingString) {
//     const sortingParams = new URLSearchParams(params.sortingString);
//     sortingParams.forEach((value, key) => {
//       searchParams.append(key, value);
//     });
//   }

//   let url = `/users?${searchParams.toString()}`;
//   return new Promise((resolve, reject) => {
//     axiosInstance
//       .get(url)
//       .then((response) => resolve(response?.data))
//       .catch((err) => reject(err));
//   });
// };

export const getUsers = (params) => {
  const data = userDataMock;
  return data
};

export const getUserById = (id) => {
  const data = userDataMock;
  return data.data.records.find((item) => item.id === id)
};

// export const getUserById = (id) => {
//   const url = `/users/${id}`;
//   return new Promise((resolve, reject) => {
//     axiosInstance
//       .get(url)
//       .then((response) => resolve(response))
//       .catch((err) => reject(err));
//   });
// };

export const getRoles = ({ page }) =>
  page == 0
    ? {
        // data: {
        data: [
          {
            id: 1,
            name: "Adult Immunisation",
          },
          {
            id: 2,
            name: "Allied health",
          },
          {
            id: 3,
            name: "Assist",
          },
          {
            id: 4,
            name: "Care plan",
          },
          {
            id: 5,
            name: "Care plan reviews",
          },
          {
            id: 6,
            name: "Cervical Screening",
          },
          {
            id: 7,
            name: "Child Immunisation",
          },
          {
            id: 8,
            name: "Health assessments",
          },
          {
            id: 9,
            name: "Mental Health Care Plans (MHCP)",
          },
          {
            id: 10,
            name: "Travel Immunisation",
          },
        ],
        // },
      }
    : { data: [] };
// const url = '/roles';
// return new Promise((resolve, reject) => {
//   axiosInstance
//     .get(url)
//     .then((response) => resolve(response))
//     .catch((err) => reject(err));
// });

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

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

export const revokeAdminTransferAccess = () => {
  const url = `/users/admin-access-transfer-revoke`;
  return new Promise((resolve, reject) => {
    axiosInstance
      .put(url)
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};

export const approveAdminTransferAccess = (id, token) => {
  const url = `/users/admin-access-transfer-approve`;
  return new Promise((resolve, reject) => {
    axiosInstance
      .put(url, { id, token })
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};

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

export const logout = () => {
  // const url = '/auth/companies/logout';
  // return new Promise((resolve, reject) => {
  //   axiosInstance
  //     .get(url)
  //     .then((response) => resolve(response))
  //     .catch((err) => reject(err));
  // });
  return;
};

export const changePassword = (data) => {
  const url = "/users/me/password";
  return new Promise((resolve, reject) => {
    axiosInstance
      .put(url, data)
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};
export const updateMailersSettings = (data) => {
  const url = "/users/me/mail-notification";
  return new Promise((resolve, reject) => {
    axiosInstance
      .put(url, data)
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};

export const updateUser = (imageUrl, isProfileDelete = false) => {
  let url = "/users/me";
  const data = { profilePhoto: imageUrl, isProfileDelete };

  return new Promise((resolve, reject) => {
    axiosInstance
      .put(url, data)
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};
export const companyMe = () => {
  let url = "/companies/me";

  return new Promise((resolve, reject) => {
    axiosInstance
      .get(url)
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};
export const updateCompanyDetails = (data) => {
  let url = "/companies/me";

  return new Promise((resolve, reject) => {
    axiosInstance
      .put(url, data)
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};

export const bsAdminLogin = (token) => {
  const url = "/auth/admin/login";
  return new Promise((resolve, reject) => {
    axiosInstance
      .post(url, {
        token,
      })
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};

export const resendPasswordMailer = (data) => {
  const url = "/users/remind-password-setup";
  return new Promise((resolve, reject) => {
    axiosInstance
      .patch(url, {
        email: data,
      })
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};

export const updateUserEmailAndContact = (data) => {
  let url = "/users/me";

  return new Promise((resolve, reject) => {
    axiosInstance
      .put(url, data)
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};

export const transferMasterAdminAccess = (id) => {
  if (!id) throw new Error("User id is required");
  const url = `/users/${id}/transfer-master-admin-access`;

  return new Promise((resolve, reject) => {
    axiosInstance
      .put(url)
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};
