import api from "src/api";

export const getStoreItemsAssociado = async () => {
  const { data } = await api.get("/associado/store-item");
  return data.payload;
};

export const getMyInterestsAssociado = async () => {
  const { data } = await api.get("/associado/store-item/my/interests");
  return data.payload;
};

export const toggleInterestAssociado = async (id) => {
  const { data } = await api.post(`/associado/store-item/${id}/interest`);
  return data.payload;
};

export const getStoreItems = async () => {
  const { data } = await api.get("/store-item");
  return data.payload;
};

export const getStoreItemsPaginated = async ({ page = 1, perPage = 10, filter, status, category_id } = {}) => {
  const params = { page, per_page: perPage };
  if (filter)      params.search      = filter;
  if (status)      params.status      = status;
  if (category_id) params.category_id = category_id;
  const { data } = await api.get("/store-item/paginated", { params });
  return { data: { result: data.payload } };
};

export const getStoreItem = async (id) => {
  const { data } = await api.get(`/store-item/${id}`);
  return data.payload;
};

export const createStoreItem = async (payload) => {
  const { data } = await api.post("/store-item", payload);
  return data.payload;
};

export const updateStoreItem = async (id, payload) => {
  const { data } = await api.put(`/store-item/${id}`, payload);
  return data.payload;
};

export const deleteStoreItem = async (id) => {
  await api.delete(`/store-item/${id}`);
};

export const toggleStoreItemStatus = async (id) => {
  const { data } = await api.patch(`/store-item/${id}/toggle-status`);
  return data.payload;
};

export const getMyInterests = async () => {
  const { data } = await api.get("/store-item/my/interests");
  return data.payload;
};

export const toggleInterest = async (id) => {
  const { data } = await api.post(`/store-item/${id}/interest`);
  return data.payload;
};

export const uploadStoreItemMedia = async (id, file) => {
  const form = new FormData();
  form.append("file", file);
  const { data } = await api.post(`/store-item/${id}/media`, form, {
    headers: { "Content-Type": "multipart/form-data" },
  });
  return data.payload;
};

export const deleteStoreItemMedia = async (id, mediaId) => {
  await api.delete(`/store-item/${id}/media/${mediaId}`);
};

export const getStoreItemInterests = async (id) => {
  const { data } = await api.get(`/store-item/${id}/interests`);
  return data.payload;
};

export const addStoreItemVariation = async (itemId, payload) => {
  const { data } = await api.post(`/store-item/${itemId}/variation`, payload);
  return data.payload;
};

export const updateStoreItemVariation = async (itemId, variationId, payload) => {
  const { data } = await api.patch(`/store-item/${itemId}/variation/${variationId}`, payload);
  return data.payload;
};

export const deleteStoreItemVariation = async (itemId, variationId) => {
  await api.delete(`/store-item/${itemId}/variation/${variationId}`);
};
