import { todayIso } from '@/src/types/diet';

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

export function formatDisplayDate(isoDate: string, locale: string): string {
  const d = new Date(`${isoDate}T12:00:00`);
  return d.toLocaleDateString(locale.startsWith('pl') ? 'pl-PL' : 'en-US', {
    weekday: 'short',
    month: 'short',
    day: 'numeric',
  });
}

export function weekRange(anchorDate = todayIso()): { from: string; to: string } {
  const d = new Date(`${anchorDate}T12:00:00`);
  const day = d.getDay();
  const diffToMonday = day === 0 ? -6 : 1 - day;
  const monday = new Date(d);
  monday.setDate(d.getDate() + diffToMonday);
  const sunday = new Date(monday);
  sunday.setDate(monday.getDate() + 6);
  return {
    from: monday.toISOString().slice(0, 10),
    to: sunday.toISOString().slice(0, 10),
  };
}

export function weekDays(from: string): string[] {
  return Array.from({ length: 7 }, (_, i) => addDays(from, i));
}

export function historyRange(days = 14): { from: string; to: string } {
  const to = todayIso();
  return { from: addDays(to, -(days - 1)), to };
}
