import { api } from "@/api/axiosInstance";
import type {
  CopyMealsResult,
  CreateDietReminderInput,
  DailySummary,
  DietReminder,
  DietSuggestion,
  DrinkCatalogItem,
  DueReminder,
  FavoriteCatalogItem,
  FavoriteRecipe,
  HistoryDay,
  LogDrinkInput,
  LogMealInput,
  MealConsumption,
  MealPreview,
  RecentMeal,
  UpdateDietReminderInput,
  UserDailyGoals,
} from "@/types/diet.types";
import { normalizeDailySummary } from "@/utils/diet-normalize";
import { todayIso } from "@/utils/date.util";

export { todayIso };

export async function fetchDailySummary(date = todayIso()): Promise<DailySummary> {
  const { data } = await api.get<unknown>("/diet/summary", { params: { date } });
  return normalizeDailySummary(data);
}

export async function fetchGoals(): Promise<UserDailyGoals> {
  const { data } = await api.get<UserDailyGoals>("/diet/goals");
  return data;
}

export async function saveGoals(goals: UserDailyGoals): Promise<UserDailyGoals> {
  const { data } = await api.put<UserDailyGoals>("/diet/goals", goals);
  return data;
}

export async function logMeal(input: LogMealInput): Promise<MealConsumption> {
  const { data } = await api.post<MealConsumption>("/diet/meals", input);
  return data;
}

export async function previewMeal(input: LogMealInput, date = todayIso()): Promise<MealPreview> {
  const { data } = await api.post<MealPreview>("/diet/meals/preview", input, { params: { date } });
  return data;
}

export async function fetchMealSuggestions(date = todayIso(), limit = 5): Promise<DietSuggestion[]> {
  const { data } = await api.get<DietSuggestion[]>("/diet/suggestions", { params: { date, limit } });
  return data;
}

export async function deleteMeal(id: number): Promise<void> {
  await api.delete(`/diet/meals/${id}`);
}

export async function fetchDrinkCatalog(): Promise<DrinkCatalogItem[]> {
  const { data } = await api.get<DrinkCatalogItem[]>("/diet/drinks/catalog");
  return data;
}

export async function logDrink(input: LogDrinkInput): Promise<void> {
  await api.post("/diet/drinks", input);
}

export async function deleteDrink(id: number): Promise<void> {
  await api.delete(`/diet/drinks/${id}`);
}

export async function fetchReminders(): Promise<DietReminder[]> {
  const { data } = await api.get<DietReminder[]>("/diet/reminders");
  return data;
}

export async function createReminder(input: CreateDietReminderInput): Promise<DietReminder> {
  const { data } = await api.post<DietReminder>("/diet/reminders", input);
  return data;
}

export async function updateReminder(id: number, input: UpdateDietReminderInput): Promise<DietReminder> {
  const { data } = await api.put<DietReminder>(`/diet/reminders/${id}`, input);
  return data;
}

export async function deleteReminder(id: number): Promise<void> {
  await api.delete(`/diet/reminders/${id}`);
}

export async function fetchDueReminders(date?: string, time?: string): Promise<DueReminder[]> {
  const { data } = await api.get<DueReminder[]>("/diet/reminders/due", { params: { date, time } });
  return data;
}

export async function fetchHistory(from: string, to: string): Promise<HistoryDay[]> {
  const { data } = await api.get<HistoryDay[]>("/diet/history", { params: { from, to } });
  return data;
}

export async function fetchRecentMeals(limit = 10): Promise<RecentMeal[]> {
  const { data } = await api.get<RecentMeal[]>("/diet/meals/recent", { params: { limit } });
  return data;
}

export async function copyYesterdayMeals(date?: string): Promise<CopyMealsResult> {
  const { data } = await api.post<CopyMealsResult>("/diet/meals/copy-yesterday", null, { params: { date } });
  return data;
}

export async function fetchFavoriteRecipes(): Promise<FavoriteRecipe[]> {
  const { data } = await api.get<FavoriteRecipe[]>("/diet/favorites/recipes");
  return data;
}

export async function addFavoriteRecipe(recipeId: number): Promise<FavoriteRecipe> {
  const { data } = await api.post<FavoriteRecipe>(`/diet/favorites/recipes/${recipeId}`);
  return data;
}

export async function removeFavoriteRecipe(recipeId: number): Promise<void> {
  await api.delete(`/diet/favorites/recipes/${recipeId}`);
}

export async function fetchFavoriteCatalogItems(): Promise<FavoriteCatalogItem[]> {
  const { data } = await api.get<FavoriteCatalogItem[]>("/diet/favorites/catalog");
  return data;
}

export async function addFavoriteCatalogItem(catalogItemId: number): Promise<FavoriteCatalogItem> {
  const { data } = await api.post<FavoriteCatalogItem>(`/diet/favorites/catalog/${catalogItemId}`);
  return data;
}

export async function removeFavoriteCatalogItem(catalogItemId: number): Promise<void> {
  await api.delete(`/diet/favorites/catalog/${catalogItemId}`);
}
