import fetch from 'node-fetch';

export async function apiRequest(endpoint, apiKey, method = 'GET', body = null) {
  const headers = {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
    'x-my-mc-auth': apiKey
  };
  try {
    const response = await fetch(`${process.env.API_URL}${endpoint}`, {
      method,
      headers,
      body: body ? JSON.stringify(body) : null
    });
    const data = await response.json();
    if (!response.ok) return { error: data.message || `HTTP ${response.status}` };
    return data;
  } catch (error) {
    return { error: `Network error: ${error.message}` };
  }
}