import axios from "axios";

export const createItem = async (settings) => {
  try {
    const formData = new FormData();
    formData.append("room", settings.room);
    formData.append("brand", settings.brand);
    formData.append("model", settings.model);
    formData.append("price", settings.price);
    formData.append("purchaseDate", settings.purchaseDate);
    formData.append("link", settings.link);
    formData.append("description", settings.description);
    formData.append("image", settings.image);
    formData.append("invoice", settings.invoice);

    const response = await axios.post("/item", formData);

    return response.data;
  } catch (err) {
    console.error(err);
    return null;
  }
};

export const updateItem = async (settings) => {
  try {
    const formData = new FormData();
    formData.append("brand", settings.brand);
    formData.append("model", settings.model);
    formData.append("price", settings.price);
    formData.append("description", settings.description);
    formData.append("purchaseDate", settings.purchaseDate);
    formData.append("link", settings.link);
    formData.append("invoice", settings.invoice);

    // je ne sais pas si ca va bug sans je verrai apres
    if (settings.image) {
      formData.append("image", settings.image);
    }

    const response = await axios.put(`/item/${settings._id}`, formData);
    return response.data;
  } catch (err) {
    console.error(err);
    return null;
  }
};

export const deleteItem = async (id) => {
  try {
    const response = await axios.delete(`/item/${id}`);
    return response.data;
  } catch (err) {
    console.error(err);
    return null;
  }
};
