import api from "./axios";

/**
 * Get grocery list for household and store
 */
export const getList = (householdId, storeId) =>
  api.get(`/households/${householdId}/stores/${storeId}/list`);

/**
 * Get specific item by name
 */
export const getItemByName = (householdId, storeId, itemName) =>
  api.get(`/households/${householdId}/stores/${storeId}/list/item`, {
    params: { item_name: itemName }
  });

/**
 * Add item to list
 */
export const addItem = (
  householdId,
  storeId,
  itemName,
  quantity,
  imageFile = null,
  notes = null,
  addedForUserId = null
) => {
  const formData = new FormData();
  formData.append("item_name", itemName);
  formData.append("quantity", quantity);
  if (notes) {
    formData.append("notes", notes);
  }
  if (addedForUserId != null) {
    formData.append("added_for_user_id", addedForUserId);
  }
  if (imageFile) {
    formData.append("image", imageFile);
  }

  return api.post(`/households/${householdId}/stores/${storeId}/list/add`, formData, {
    headers: {
      "Content-Type": "multipart/form-data",
    },
  });
};

/**
 * Get item classification
 */
export const getClassification = (householdId, storeId, itemName) =>
  api.get(`/households/${householdId}/stores/${storeId}/list/classification`, {
    params: { item_name: itemName }
  });

/**
 * Set item classification
 */
export const setClassification = (householdId, storeId, itemName, classification) =>
  api.post(`/households/${householdId}/stores/${storeId}/list/classification`, {
    item_name: itemName,
    classification
  });

/**
 * Update item with classification (legacy method - split into separate calls)
 */
export const updateItemWithClassification = (householdId, storeId, itemName, quantity, classification) => {
  // This is now two operations: update item + set classification
  return Promise.all([
    updateItem(householdId, storeId, itemName, quantity),
    classification ? setClassification(householdId, storeId, itemName, classification) : Promise.resolve()
  ]);
};

/**
 * Update item details (quantity, notes)
 */
export const updateItem = (householdId, storeId, itemName, quantity, notes) =>
  api.put(`/households/${householdId}/stores/${storeId}/list/item`, {
    item_name: itemName,
    quantity,
    notes
  });

/**
 * Mark item as bought or unbought
 */
export const markBought = (householdId, storeId, itemName, quantityBought = null, bought = true) =>
  api.patch(`/households/${householdId}/stores/${storeId}/list/item`, {
    item_name: itemName,
    bought,
    quantity_bought: quantityBought
  });

/**
 * Delete item from list
 */
export const deleteItem = (householdId, storeId, itemName) =>
  api.delete(`/households/${householdId}/stores/${storeId}/list/item`, {
    data: { item_name: itemName }
  });

/**
 * Get suggestions based on query
 */
export const getSuggestions = (householdId, storeId, query) =>
  api.get(`/households/${householdId}/stores/${storeId}/list/suggestions`, {
    params: { query }
  });

/**
 * Get recently bought items
 */
export const getRecentlyBought = (householdId, storeId) =>
  api.get(`/households/${householdId}/stores/${storeId}/list/recent`);

/**
 * Update item image
 */
export const updateItemImage = (
  householdId,
  storeId,
  itemName,
  quantity,
  imageFile,
  options = {}
) => {
  const formData = new FormData();
  formData.append("item_name", itemName);
  formData.append("quantity", quantity);
  formData.append("image", imageFile);

  return api.post(`/households/${householdId}/stores/${storeId}/list/update-image`, formData, {
    headers: {
      "Content-Type": "multipart/form-data",
    },
    onUploadProgress: options.onUploadProgress,
    signal: options.signal,
    timeout: options.timeoutMs,
  });
};
