import { api } from "@/api/axiosInstance";
import type {
  IngredientCategory,
  IngredientNutritionItem,
  UpsertIngredientNutritionItemInput,
} from "@/types/ingredient-catalog.types";

export async function fetchIngredientCategories(): Promise<IngredientCategory[]> {
  const { data } = await api.get<IngredientCategory[]>("/ingredient-catalog/categories");
  return data;
}

export async function fetchIngredientCatalogItems(params?: {
  category?: string;
  search?: string;
}): Promise<IngredientNutritionItem[]> {
  const { data } = await api.get<IngredientNutritionItem[]>("/ingredient-catalog", { params });
  return data;
}

export async function fetchIngredientCatalogItem(id: number): Promise<IngredientNutritionItem> {
  const { data } = await api.get<IngredientNutritionItem>(`/ingredient-catalog/${id}`);
  return data;
}

export async function createIngredientCatalogItem(
  input: UpsertIngredientNutritionItemInput,
): Promise<IngredientNutritionItem> {
  const { data } = await api.post<IngredientNutritionItem>("/ingredient-catalog", input);
  return data;
}

export async function updateIngredientCatalogItem(
  id: number,
  input: UpsertIngredientNutritionItemInput,
): Promise<IngredientNutritionItem> {
  const { data } = await api.put<IngredientNutritionItem>(`/ingredient-catalog/${id}`, input);
  return data;
}

export async function deleteIngredientCatalogItem(id: number): Promise<void> {
  await api.delete(`/ingredient-catalog/${id}`);
}
