import { apiFetch } from '@/src/api/client';
import type {
  CopyMealsResult,
  CreateDietReminderInput,
  DailySummary,
  DietReminder,
  DietSuggestion,
  DrinkCatalogItem,
  DueReminder,
  FavoriteCatalogItem,
  FavoriteRecipe,
  HistoryDay,
  LogDrinkInput,
  LogMealInput,
  MealConsumption,
  MealPreview,
  RecentMeal,
  UpdateDietReminderInput,
  UserDailyGoals,
} from '@/src/types/diet';
import { todayIso } from '@/src/types/diet';
import { normalizeDailySummary } from '@/src/utils/diet-normalize';

export async function fetchDailySummary(date = todayIso()): Promise<DailySummary> {
  const data = await apiFetch<unknown>(`/diet/summary?date=${date}`);
  return normalizeDailySummary(data);
}

export async function fetchGoals(): Promise<UserDailyGoals> {
  return apiFetch<UserDailyGoals>('/diet/goals');
}

export async function saveGoals(goals: UserDailyGoals): Promise<UserDailyGoals> {
  return apiFetch<UserDailyGoals>('/diet/goals', { method: 'PUT', body: goals });
}

export async function logMeal(input: LogMealInput): Promise<MealConsumption> {
  return apiFetch<MealConsumption>('/diet/meals', { method: 'POST', body: input });
}

export async function previewMeal(input: LogMealInput, date = todayIso()): Promise<MealPreview> {
  return apiFetch<MealPreview>(`/diet/meals/preview?date=${date}`, { method: 'POST', body: input });
}

export async function fetchMealSuggestions(date = todayIso(), limit = 5): Promise<DietSuggestion[]> {
  return apiFetch<DietSuggestion[]>(`/diet/suggestions?date=${date}&limit=${limit}`);
}

export async function deleteMeal(id: number): Promise<void> {
  await apiFetch(`/diet/meals/${id}`, { method: 'DELETE' });
}

export async function fetchDrinkCatalog(): Promise<DrinkCatalogItem[]> {
  return apiFetch<DrinkCatalogItem[]>('/diet/drinks/catalog');
}

export async function logDrink(input: LogDrinkInput): Promise<void> {
  await apiFetch('/diet/drinks', { method: 'POST', body: input });
}

export async function deleteDrink(id: number): Promise<void> {
  await apiFetch(`/diet/drinks/${id}`, { method: 'DELETE' });
}

export async function fetchReminders(): Promise<DietReminder[]> {
  return apiFetch<DietReminder[]>('/diet/reminders');
}

export async function createReminder(input: CreateDietReminderInput): Promise<DietReminder> {
  return apiFetch<DietReminder>('/diet/reminders', { method: 'POST', body: input });
}

export async function updateReminder(id: number, input: UpdateDietReminderInput): Promise<DietReminder> {
  return apiFetch<DietReminder>(`/diet/reminders/${id}`, { method: 'PUT', body: input });
}

export async function deleteReminder(id: number): Promise<void> {
  await apiFetch(`/diet/reminders/${id}`, { method: 'DELETE' });
}

export async function fetchDueReminders(date?: string, time?: string): Promise<DueReminder[]> {
  const params = new URLSearchParams();
  if (date) params.set('date', date);
  if (time) params.set('time', time);
  const qs = params.toString();
  return apiFetch<DueReminder[]>(`/diet/reminders/due${qs ? `?${qs}` : ''}`);
}

export async function fetchHistory(from: string, to: string): Promise<HistoryDay[]> {
  return apiFetch<HistoryDay[]>(`/diet/history?from=${from}&to=${to}`);
}

export async function fetchRecentMeals(limit = 10): Promise<RecentMeal[]> {
  return apiFetch<RecentMeal[]>(`/diet/meals/recent?limit=${limit}`);
}

export async function copyYesterdayMeals(date = todayIso()): Promise<CopyMealsResult> {
  return apiFetch<CopyMealsResult>(`/diet/meals/copy-yesterday?date=${date}`, { method: 'POST' });
}

export async function fetchFavoriteRecipes(): Promise<FavoriteRecipe[]> {
  return apiFetch<FavoriteRecipe[]>('/diet/favorites/recipes');
}

export async function fetchFavoriteCatalogItems(): Promise<FavoriteCatalogItem[]> {
  return apiFetch<FavoriteCatalogItem[]>('/diet/favorites/catalog');
}
