import api from "src/api";

export const updateProfile = async (id, data) => {
  const { data: res } = await api.put(`/user/${id}`, data);
  return res.payload;
};

export const getDependentsByUser = async (userId) => {
  const { data } = await api.get(`/user-dependent/user/${userId}`);
  return data.payload;
};

export const createDependent = async (dependent) => {
  const form = new FormData();
  form.append("responsible_user_id", dependent.responsible_user_id);
  form.append("name", dependent.name);
  form.append("kinship", dependent.kinship);

  if (dependent.document instanceof File) {
    form.append("document", dependent.document);
  }

  const { data } = await api.post("/user-dependent", form, {
    headers: { "Content-Type": "multipart/form-data" },
  });
  return data.payload;
};

export const updateDependent = async (id, dependent) => {
  const { data } = await api.put(`/user-dependent/${id}`, dependent);
  return data.payload;
};

export const deleteDependent = async (id) => {
  const { data } = await api.delete(`/user-dependent/${id}`);
  return data.payload;
};
