const DEFAULT_BASE_URL = "http://localhost:8000/api/v1";

export async function apiClient<T>(
  path: string,
  init?: RequestInit
): Promise<T> {
  const response = await fetch(`${process.env.NEXT_PUBLIC_API_BASE_URL ?? DEFAULT_BASE_URL}${path}`, {
    ...init,
    headers: {
      "Content-Type": "application/json",
      ...(init?.headers ?? {})
    },
    cache: "no-store"
  });

  if (!response.ok) {
    const message = `API request failed with status ${response.status}`;
    throw new Error(message);
  }

  return response.json() as Promise<T>;
}
