// Batch-Upload Funktion für mehrere Bilder
export const uploadImageBatch = async (images, metadata, descriptions = [], onProgress) => {
  if (!images || images.length === 0) {
    throw new Error('Keine Bilder zum Upload ausgewählt');
  }

  const formData = new FormData();
  
  // Füge alle Bilder hinzu
  images.forEach((image) => {
    formData.append('images', image);
  });
  
  // Füge Metadaten hinzu
  formData.append('metadata', JSON.stringify(metadata || {}));
  
  // Füge Beschreibungen hinzu (NEU)
  if (descriptions && descriptions.length > 0) {
    formData.append('descriptions', JSON.stringify(descriptions));
  }

  try {
    const response = await fetch('/api/upload/batch', {
      method: 'POST',
      body: formData
    });

    if (!response.ok) {
      const errorData = await response.json();
      throw new Error(errorData.message || `HTTP error! status: ${response.status}`);
    }

    const result = await response.json();
    return result;
  } catch (error) {
    console.error('Batch upload error:', error);
    throw error;
  }
};

// Einzelne Gruppe abrufen
export const fetchGroup = async (groupId) => {
  try {
    const response = await fetch(`/api/groups/${groupId}`);
    
    if (!response.ok) {
      const errorData = await response.json();
      throw new Error(errorData.message || `HTTP error! status: ${response.status}`);
    }
    
    return await response.json();
  } catch (error) {
    console.error('Error fetching group:', error);
    throw error;
  }
};

// Alle Gruppen abrufen
export const fetchAllGroups = async () => {
  try {
    const response = await fetch('/api/groups');
    
    if (!response.ok) {
      const errorData = await response.json();
      throw new Error(errorData.message || `HTTP error! status: ${response.status}`);
    }
    
    return await response.json();
  } catch (error) {
    console.error('Error fetching groups:', error);
    throw error;
  }
};

// Gruppe löschen
export const deleteGroup = async (groupId) => {
  try {
    const response = await fetch(`/api/groups/${groupId}`, {
      method: 'DELETE'
    });
    
    if (!response.ok) {
      const errorData = await response.json();
      throw new Error(errorData.message || `HTTP error! status: ${response.status}`);
    }
    
    return await response.json();
  } catch (error) {
    console.error('Error deleting group:', error);
    throw error;
  }
};