import { differenceInDays, format, isBefore } from 'date-fns';
import { de } from 'date-fns/locale';
import { MHD_WARNING_DAYS } from '../constants';

export function formatDate(date: Date | null): string {
  if (!date) return '—';
  return format(date, 'dd.MM.yyyy', { locale: de });
}

export function isExpiringSoon(expiryDate: Date | null): boolean {
  if (!expiryDate) return false;
  const days = differenceInDays(expiryDate, new Date());
  return days >= 0 && days <= MHD_WARNING_DAYS;
}

export function isExpired(expiryDate: Date | null): boolean {
  if (!expiryDate) return false;
  return isBefore(expiryDate, new Date());
}

export function formatQuantity(quantity: number, unit: string): string {
  return `${quantity} ${unit}`;
}

export function buildDeepLink(householdId: string, token: string): string {
  return `houseorg://join?householdId=${encodeURIComponent(householdId)}&token=${encodeURIComponent(token)}`;
}
