export type Holiday = {
  id: number;
  name: string;
  start_date: string;
  end_date: string;
  region?: string | null;
  source_file_name?: string | null;
  imported_at?: string | null;
};

export async function listHolidays(region?: string) {
  const url = region ? `/api/holidays?region=${encodeURIComponent(region)}` : '/api/holidays';
  const res = await fetch(url);
  const data = await res.json();
  if (!res.ok || data.error) throw new Error(data.error || 'Fehler beim Laden der Ferien');
  return data as { holidays: Holiday[] };
}

export async function uploadHolidaysCsv(file: File) {
  const form = new FormData();
  form.append('file', file);
  const res = await fetch('/api/holidays/upload', { method: 'POST', body: form });
  const data = await res.json();
  if (!res.ok || data.error) throw new Error(data.error || 'Fehler beim Import der Ferien');
  return data as { success: boolean; inserted: number; updated: number };
}
