const API_BASE_URL = 'http://portainer.white-enciso.pro:4001/api/v1/MongoSample';

export async function getExternalData() {
  const response = await fetch(`${API_BASE_URL}/GetAll`);
  if (!response.ok) throw new Error('Failed to fetch external data');
  const data = await response.json();
  return data;
}

export async function createExternalData(data) {
  const response = await fetch(`${API_BASE_URL}/Create`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify(data),
  });
  if (!response.ok) throw new Error('Failed to create external data');
  return await response.json();
}

export async function updateExternalData(data) {
  const response = await fetch(`${API_BASE_URL}/Update`, {
    method: 'PUT',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify(data),
  });
  if (!response.ok) throw new Error('Failed to update item');
  return await response.json();
}

export async function deleteExternalData(_Id) {
  const response = await fetch(`${API_BASE_URL}/Delete`, {
    method: 'DELETE',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({ _Id }),
  });
  if (!response.ok) throw new Error('Failed to delete external data');
  return await response.json();
}