import { api } from "./axiosInstance";
import type {
  CategoryCount,
  MealCategory,
  RecipeDetail,
  RecipeListItem,
} from "@/types/recipe.types";

export interface RecipeQuery {
  category?: MealCategory;
  search?: string;
  /** Filters to recipes that contain an ingredient whose name matches this term. */
  ingredient?: string;
}

export async function fetchRecipes(query: RecipeQuery = {}): Promise<RecipeListItem[]> {
  const { data } = await api.get<RecipeListItem[]>("/recipes", {
    params: {
      category: query.category,
      search: query.search || undefined,
      ingredient: query.ingredient || undefined,
    },
  });
  return data;
}

export async function fetchRecipeById(id: number): Promise<RecipeDetail> {
  const { data } = await api.get<RecipeDetail>(`/recipes/${id}`);
  return data;
}

export async function fetchCategories(): Promise<CategoryCount[]> {
  const { data } = await api.get<CategoryCount[]>("/recipes/categories");
  return data;
}
