export interface UserDailyGoals {
  calorieGoal?: number | null;
  proteinGoalG?: number | null;
  fatGoalG?: number | null;
  carbsGoalG?: number | null;
  waterGoalMl?: number | null;
}

export interface MacroRemaining {
  goal?: number | null;
  consumed: number;
  remaining?: number | null;
  progressPercent?: number | null;
}

export interface DecimalMacroRemaining {
  goal?: number | null;
  consumed: number;
  remaining?: number | null;
  progressPercent?: number | null;
}

export interface MealConsumption {
  id: number;
  recipeId?: number | null;
  mealCategory: number;
  logDate: string;
  consumedAt: string;
  name: string;
  portions: number;
  calories?: number | null;
  proteinG?: number | null;
  fatG?: number | null;
  carbsG?: number | null;
  notes?: string | null;
}

export interface DrinkCatalogItem {
  id: number;
  code: string;
  name: string;
  defaultVolumeMl: number;
  caloriesPer100Ml?: number | null;
  iconEmoji?: string | null;
}

export interface DrinkConsumption {
  id: number;
  drinkCatalogId?: number | null;
  logDate: string;
  consumedAt: string;
  name: string;
  volumeMl: number;
  calories?: number | null;
}

export interface DailySummary {
  date: string;
  goals?: UserDailyGoals | null;
  calories: MacroRemaining;
  proteinG: DecimalMacroRemaining;
  fatG: DecimalMacroRemaining;
  carbsG: DecimalMacroRemaining;
  waterMl: MacroRemaining;
  meals: MealConsumption[];
  drinks: DrinkConsumption[];
}

export interface LogMealInput {
  recipeId?: number;
  catalogItemId?: number;
  grams?: number;
  name?: string;
  mealCategory: number;
  logDate?: string;
  portions?: number;
  calories?: number;
  proteinG?: number;
  fatG?: number;
  carbsG?: number;
  notes?: string;
}

export interface MealPreview {
  meal: {
    name: string;
    portions: number;
    calories?: number | null;
    proteinG?: number | null;
    fatG?: number | null;
    carbsG?: number | null;
  };
  remainingAfter: {
    calories?: number | null;
    proteinG?: number | null;
    fatG?: number | null;
    carbsG?: number | null;
  };
}

export interface DietSuggestion {
  catalogItemId: number;
  name: string;
  namePl?: string | null;
  categoryCode: string;
  suggestedGrams: number;
  calories?: number | null;
  proteinG?: number | null;
  fatG?: number | null;
  carbsG?: number | null;
  reasonKey: string;
}

export interface LogDrinkInput {
  drinkCatalogId?: number;
  name?: string;
  volumeMl: number;
  logDate?: string;
  calories?: number;
}

export interface DietReminder {
  id: number;
  reminderType: number;
  title: string;
  message?: string | null;
  timeOfDay: string;
  daysOfWeekMask: number;
  mealCategory?: number | null;
  isEnabled: boolean;
}

export interface CreateDietReminderInput {
  reminderType: number;
  title: string;
  message?: string;
  timeOfDay: string;
  daysOfWeekMask?: number;
  mealCategory?: number;
  isEnabled?: boolean;
}

export type UpdateDietReminderInput = CreateDietReminderInput;

export interface DueReminder {
  id: number;
  reminderType: number;
  title: string;
  message?: string | null;
  timeOfDay: string;
  mealCategory?: number | null;
}

export interface HistoryDay {
  date: string;
  calories: number;
  proteinG: number;
  fatG: number;
  carbsG: number;
  waterMl: number;
  mealCount: number;
}

export interface RecentMeal {
  recipeId?: number | null;
  catalogItemId?: number | null;
  name: string;
  mealCategory: number;
  portions: number;
  grams?: number | null;
  calories?: number | null;
  proteinG?: number | null;
  fatG?: number | null;
  carbsG?: number | null;
  lastLoggedAt: string;
}

export interface FavoriteRecipe {
  recipeId: number;
  name: string;
  mealCategory: number;
  calories: number | null;
}

export interface FavoriteCatalogItem {
  catalogItemId: number;
  name: string;
  namePl?: string | null;
  categoryCode: string;
}

export interface CopyMealsResult {
  sourceDate: string;
  targetDate: string;
  copiedCount: number;
}

export enum DietReminderType {
  Water = 0,
  Meal = 1,
  Snack = 2,
  Custom = 3,
}

export const CONSUMPTION_CATEGORIES = [
  { value: 0, labelKey: 'categories.breakfast' },
  { value: 1, labelKey: 'categories.secondBreakfast' },
  { value: 2, labelKey: 'categories.lunch' },
  { value: 3, labelKey: 'categories.dinner' },
  { value: 4, labelKey: 'diet.snack' },
] as const;

export const REMINDER_DAYS_MASK_ALL = 127;

function todayIso(): string {
  return new Date().toISOString().slice(0, 10);
}

function addDaysIso(iso: string, days: number): string {
  const d = new Date(`${iso}T12:00:00`);
  d.setDate(d.getDate() + days);
  return d.toISOString().slice(0, 10);
}

function startOfWeekIso(iso: string): string {
  const d = new Date(`${iso}T12:00:00`);
  const day = d.getDay();
  const diff = (day === 0 ? -6 : 1) - day;
  d.setDate(d.getDate() + diff);
  return d.toISOString().slice(0, 10);
}

function endOfWeekIso(iso: string): string {
  return addDaysIso(startOfWeekIso(iso), 6);
}

export { todayIso, addDaysIso, startOfWeekIso, endOfWeekIso };
