// Service für generische CRUD-Operationen auf Admin-Ressourcen (users, clients)
const backendUrl = process.env.REACT_APP_BACKEND || '';

function authHeaders(contentType = false) {
  const token = localStorage.getItem('jwt');
  const headers = { 'Authorization': 'Bearer ' + token };
  if (contentType) headers['Content-Type'] = 'application/json';
  return headers;
}

export async function getAll(type) {
  const res = await fetch(`${backendUrl}/api/admin/${type}`, {
    headers: authHeaders()
  });
  return res.json();
}

export async function create(type, data) {
  const res = await fetch(`${backendUrl}/api/admin/${type}`, {
    method: 'POST',
    headers: authHeaders(true),
    body: JSON.stringify(data)
  });
  return res.json();
}

export async function update(type, id, data) {
  const res = await fetch(`${backendUrl}/api/admin/${type}/${id}`, {
    method: 'PUT',
    headers: authHeaders(true),
    body: JSON.stringify(data)
  });
  return res.json();
}

export async function remove(type, id) {
  const res = await fetch(`${backendUrl}/api/admin/${type}/${id}`, {
    method: 'DELETE',
    headers: authHeaders()
  });
  return res.json();
}
